home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / collate.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  8.9 KB  |  287 lines

  1. #ifndef __COLLATE_H
  2. #define __COLLATE_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. /***************************************************************************
  6.  *
  7.  * collate - Declarations for the Standard Library character collation facet
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  12.  *
  13.  * This computer software is owned by Rogue Wave Software, Inc. and is
  14.  * protected by U.S. copyright laws and other laws and by international
  15.  * treaties.  This computer software is furnished by Rogue Wave Software,
  16.  * Inc. pursuant to a written license agreement and may be used, copied,
  17.  * transmitted, and stored only in accordance with the terms of such
  18.  * license and with the inclusion of the above copyright notice.  This
  19.  * computer software or any other copies thereof may not be provided or
  20.  * otherwise made available to any other person.
  21.  *
  22.  * U.S. Government Restricted Rights.  This computer software is provided
  23.  * with Restricted Rights.  Use, duplication, or disclosure by the
  24.  * Government is subject to restrictions as set forth in subparagraph (c)
  25.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  26.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  27.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  28.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  29.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  30.  *
  31.  **************************************************************************/
  32.  
  33. #ifndef __STD_COLLATE__
  34. #define __STD_COLLATE__
  35.  
  36. #ifndef __STD_RWLOCALE__
  37. #include <rw/rwlocale> 
  38. #endif
  39.  
  40. #ifndef _RWSTD_NO_NAMESPACE
  41. namespace __rwstd {
  42. #endif
  43.  
  44. // ------------------------------------------------------
  45. // Implementation class template -- collate_table<charT>.
  46. // ------------------------------------------------------
  47.  
  48. // Data structure that drives the collate<charT> process.  It contains a
  49. // binary tree of nodes, each of which defines the collation treatment to be
  50. // applied to a range of characters.
  51.  
  52. template <class charT>
  53. class _RWSTDExportTemplate collate_table {
  54.  public:
  55.   struct node {
  56.     node *left;             // Node for charT values that are too small (resp.
  57.     node *right;            //  too large) for this node
  58.     const charT *table;     // Translate table or NULL
  59.     charT minC,maxC;        // Range of charT-s covered by this node
  60.     charT offset;           // Amount to add to charT if table is NULL
  61.   };
  62.  
  63.   long length;              // Length of memory block containing all the nodes
  64.   node root;                // Root node of table
  65. };
  66.  
  67. // -----------------------------------------------------
  68. // Implementation class template -- collate_data<charT>.
  69. // -----------------------------------------------------
  70.  
  71. // collate<charT> derives from this (via rwstd::collate_impl) to get its
  72. // private data members.
  73.  
  74. template <class charT>
  75. class _RWSTDExportTemplate collate_data
  76. {
  77.  public:
  78.   typedef collate_table<charT> table;
  79.   typedef _TYPENAME table::node node;
  80.  
  81.   const table *__table;
  82.  
  83.   collate_data (const table &t): __table(&t) { }
  84.   charT __coll_order (charT) const;
  85. };
  86.  
  87. template <class charT>
  88. inline charT collate_data<charT>::__coll_order (charT c) const
  89. {
  90.   const node *t=&__table->root;
  91.   while (t)
  92.     if (c < t->minC)
  93.       t=t->left;
  94.     else if (c > t->maxC)
  95.       t=t->right;
  96.     else {
  97.       if (t->table)
  98.         c=t->table[size_t(c-t->minC)];
  99.       else
  100.         c+=t->offset;
  101.       break;
  102.     }
  103.  
  104.   return c;
  105. }
  106.  
  107. // -----------------------------------------------------
  108. // Implementation class template -- collate_impl<charT>.
  109. // -----------------------------------------------------
  110.  
  111. // Facet collate<charT> derives from this to get the part of its behavior that
  112. // is specialized for char and wchar_t.
  113.  
  114. template <class charT>
  115. class _RWSTDExportTemplate collate_impl :
  116.     public collate_data<charT>
  117. {
  118.   static collate_table<charT> __default_table;
  119.  public:
  120.   collate_impl (void):
  121.       collate_data<charT>(__default_table) { }
  122. };
  123.  
  124. // Specialization for char.
  125. _RWSTD_TEMPLATE
  126. class _RWSTDExport collate_impl<char>:
  127.     public collate_data<char>
  128. {
  129.   static collate_table<char> _RWSTDExportStatic __default_table;
  130.  public:
  131.   collate_impl (void):
  132.       collate_data<char>(__default_table) { }
  133. };
  134.  
  135. // Specialization for wchar_t.
  136. #ifndef _RWSTD_NO_WIDE_CHAR
  137. _RWSTD_TEMPLATE
  138. class _RWSTDExport collate_impl<wchar_t>:
  139.     public collate_data<wchar_t>
  140. {
  141.   static collate_table<wchar_t> _RWSTDExportStatic __default_table;
  142.  
  143.  public:
  144.   collate_impl (void):
  145.       collate_data<wchar_t>(__default_table) { }
  146. };
  147. #endif
  148.  
  149. #ifndef _RWSTD_NO_NAMESPACE
  150. } namespace std {
  151. #endif
  152.  
  153. // -----------------------------------------------------
  154. // Standard character collation facet -- collate<charT>.
  155. // -----------------------------------------------------
  156.  
  157. template <class charT>
  158. class _RWSTDExportTemplate collate: public locale::facet,
  159.     public __RWSTD::collate_impl<charT>
  160. {
  161.  public:
  162.   typedef charT char_type;
  163.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  164.  
  165.   _EXPLICIT collate (size_t refs=0):
  166.       locale::facet(refs,locale::__rw_collate_category)
  167.     { }
  168.  
  169.   int compare (const charT* low1, const charT* high1,
  170.                const charT* low2, const charT* high2) const
  171.     { return do_compare(low1, high1, low2, high2); }
  172.  
  173.   string_type transform (const charT* low, const charT* high) const
  174.     { return do_transform(low, high); }
  175.  
  176.   long hash (const charT* low, const charT* high) const
  177.     { return do_hash(low, high); }
  178.  
  179.   static locale::id id;
  180.  
  181.   // Implementation:
  182.   enum { __facet_cat = locale::__rw_collate_category, __ok_implicit = 1 };
  183.  
  184.  protected:
  185.   virtual ~collate();
  186.  
  187.   virtual int do_compare (const charT* low1, const charT* high1,
  188.                           const charT* low2, const charT* high2) const;
  189.   virtual string_type do_transform (const charT* low, const charT* high) const;
  190.   virtual long do_hash (const charT* low, const charT* high) const;
  191.  
  192.  private:
  193.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  194.   locale::id &__get_id (void) const { return id; }
  195.   #endif
  196. };
  197.  
  198. // ------------------------------------------------------
  199. // Standard facet specialization -- collate_byname<char>.
  200. // ------------------------------------------------------
  201.  
  202. template <class charT>
  203. class collate_byname;
  204.  
  205. _RWSTD_TEMPLATE
  206. class _RWSTDExport collate_byname<char>: public collate<char> {
  207.  public:
  208.   _EXPLICIT collate_byname (const char*, size_t refs=0);
  209.  protected:
  210.   virtual ~collate_byname ();
  211.  
  212.   // Virtual member functions, override functions in collate<char>.
  213.   virtual int do_compare (const char* low1, const char* high1,
  214.                           const char* low2, const char* high2) const;
  215.   virtual string do_transform (const char* low, const char* high) const;
  216.  
  217.   // Virtual member functions inherited from collate<char>:
  218.   // virtual long do_hash (const char* low, const char* high) const;
  219.  
  220.  private:
  221.   // Implementation.
  222.   string __collate_name;
  223. };
  224.  
  225. // ------------------------------------------------
  226. // Standard derived facet -- collate_byname<charT>.
  227. // ------------------------------------------------
  228.  
  229. template <class charT>
  230. class  collate_byname: public collate<charT> {
  231.  public:
  232.   typedef basic_string<charT,char_traits<charT>,
  233.                                 allocator<charT> > string_type;
  234.  
  235.   _EXPLICIT collate_byname (const char*, size_t refs=0);
  236.  
  237.  protected:
  238.   virtual ~collate_byname();
  239.  
  240.   // Virtual member functions overridden from collate<charT>.
  241.   virtual int do_compare (const charT* low1, const charT* high1,
  242.                           const charT* low2, const charT* high2) const;
  243.   virtual string_type do_transform (const charT* low, const charT* high) const;
  244.  
  245.   // Virtual member functions inherited from collate<charT>:
  246.   // virtual long do_hash (const charT* low, const charT* high) const;
  247.  
  248.  private:
  249.   // Implementation.
  250.   string_type __collate_name;
  251. };
  252.  
  253. #ifndef _RWSTD_NO_NAMESPACE
  254. } namespace __rwstd {
  255. #endif
  256.  
  257. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  258. template <class charT>
  259. inline _RW_STD::collate<charT>* create_named_facet
  260.     (_RW_STD::collate<charT>*,const char *name,size_t refs)
  261. { return new _RW_STD::collate_byname<charT>(name,refs); }
  262. #else
  263.  
  264. inline _RW_STD::collate<char>* create_named_facet
  265.     (_RW_STD::collate<char>*,const char *name,size_t refs)
  266. { return new _RW_STD::collate_byname<char>(name,refs); }
  267. #ifndef _RWSTD_NO_WIDE_CHAR
  268.  
  269. inline _RW_STD::collate<wchar_t>* create_named_facet
  270.     (_RW_STD::collate<wchar_t>*,const char *name,size_t refs)
  271. { return new _RW_STD::collate_byname<wchar_t>(name,refs); }
  272. #endif
  273. #endif
  274.  
  275. #ifndef _RWSTD_NO_NAMESPACE
  276. #endif
  277.  
  278. #ifdef _RWSTD_COMPILE_INSTANTIATE
  279. #include <rw/collate.cc>
  280. #endif
  281.  
  282. #endif // __STD_COLLATE__
  283.  
  284. #pragma option pop
  285. #endif /* __COLLATE_H */
  286.