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

  1. #ifndef __TRAITS_H
  2. #define __TRAITS_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __RW_TRAITS
  6. #define __RW_TRAITS
  7. /***************************************************************************
  8.  *
  9.  * traits - Declarations for char_traits 
  10.  *
  11.  *
  12.  ***************************************************************************
  13.  *
  14.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  15.  *
  16.  * This computer software is owned by Rogue Wave Software, Inc. and is
  17.  * protected by U.S. copyright laws and other laws and by international
  18.  * treaties.  This computer software is furnished by Rogue Wave Software,
  19.  * Inc. pursuant to a written license agreement and may be used, copied,
  20.  * transmitted, and stored only in accordance with the terms of such
  21.  * license and with the inclusion of the above copyright notice.  This
  22.  * computer software or any other copies thereof may not be provided or
  23.  * otherwise made available to any other person.
  24.  *
  25.  * U.S. Government Restricted Rights.  This computer software is provided
  26.  * with Restricted Rights.  Use, duplication, or disclosure by the
  27.  * Government is subject to restrictions as set forth in subparagraph (c)
  28.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  29.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  30.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  31.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  32.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  33.  *
  34.  **************************************************************************/
  35.  
  36. #ifndef _RWSTD_NO_NEW_HEADER
  37. #include <cstdio>
  38. #include <cstring>
  39. #else
  40. #include <stdio.h>
  41. #include <string.h>
  42. #endif
  43.  
  44. //
  45. // Temporarily turn off the warnings under the Borland compiler that
  46. // say 'Functions containing ... cannot be inlined'
  47. //
  48. #if defined(__BORLANDC__)
  49. #pragma option -w-inl
  50. #endif
  51.  
  52. /*
  53.  * this are all necessary for the "traits" class
  54.  */
  55. #ifdef _RW_STD_IOSTREAM
  56. #include <rw/iotraits> 
  57. #endif
  58.  
  59. #ifndef _RWSTD_NO_NAMESPACE
  60. namespace std {
  61. #endif
  62.  
  63.   /*
  64.    *     STRUCT CHAR_TRAITS
  65.    */
  66.  
  67.   template <class  charT> 
  68.   struct  _RWSTDExportTemplate char_traits 
  69.   {
  70.     typedef charT                    char_type;
  71.     typedef long                     int_type;
  72.  
  73. #ifdef _RW_STD_IOSTREAM
  74.     typedef wstreamoff               off_type;
  75.     typedef mbstate_t                state_type;
  76.     typedef fpos<state_type>         pos_type;
  77. #endif /* _RW_STD_IOSTREAM */
  78.  
  79.     static void assign (char_type& c1, const char_type& c2)   
  80.     { c1 = c2;}
  81.  
  82.     static bool     eq(const char_type& c1,const char_type& c2)
  83.     { return (c1 == c2); }
  84.  
  85.     static bool lt (const char_type& c1, const char_type& c2) 
  86.     { return c1 < c2;}
  87.  
  88.     static int compare (const char_type* s1, const char_type* s2, size_t n)
  89.     {
  90.       size_t i=0;
  91.  
  92.       while ( i < n )
  93.       { 
  94.         if ( !eq( s1[i], s2[i] ) )
  95.         {
  96.           if ( lt( s1[i], s2[i]) )
  97.             return -1;
  98.           else
  99.             return 1;               
  100.         }
  101.         i++;
  102.       }
  103.       return 0;
  104.     }
  105.         
  106.     static size_t length(const char_type *s)
  107.     {
  108.       size_t l = 0;
  109.       while ( !eq(*s++, char_type(0) ) )  ++l;
  110.       return l;
  111.     }
  112.  
  113.     static const char_type* 
  114.     find (const char_type* s, size_t n, const char_type& a)
  115.     {
  116.       while (n-- > 0 && *s != a) 
  117.         ++s;
  118.       return  *s == a ? s : 0;
  119.     }
  120.  
  121.     static char_type* move (char_type* s1, const char_type* s2, size_t n)
  122.     {
  123.       char_type * s = s1;
  124.       if (s1 < s2)
  125.         copy(s1, s2, n);
  126.       else if (s1 > s2)
  127.       {
  128.         s1 += n;
  129.         s2 += n;
  130.         for(size_t i = 0; i < n; ++i) 
  131.           assign(*--s1, *--s2);
  132.       }
  133.       return s1;
  134.     }
  135.  
  136.     static char_type * copy(char_type *dst,const char_type *src, size_t n)
  137.     {
  138.       memcpy((char *)dst,(char *)src,n*sizeof(char_type));
  139.       return dst;
  140.     }
  141.  
  142.     static char_type*  assign (char_type* s, size_t n, char_type a)
  143.     {
  144.       char_type* tmp = s;
  145.       while (n-- > 0) 
  146.         *tmp++ = a;
  147.       return s;
  148.     }
  149.  
  150.     static int_type not_eof(const int_type& c)
  151.     {
  152.       if(c == EOF)
  153.         return 0;
  154.       else
  155.         return c;
  156.     }
  157.  
  158.     static char_type to_char_type(const int_type& c)
  159.     { return c; }
  160.     static int_type to_int_type(const char_type& c)
  161.     { return c; }
  162.  
  163.     static bool     eq_int_type(const int_type& c1,const int_type& c2)
  164.     { return (c1 == c2); }
  165.  
  166. #ifdef _RW_STD_IOSTREAM
  167.     static state_type get_state(pos_type pos)
  168.     { return pos.state(); }
  169. #endif 
  170.  
  171.     static int_type             eof()
  172.     { return EOF; }
  173.   };
  174. /*
  175.  *
  176.  *     STRUCT CHAR_TRAITS SPECIALIZED FOR CHAR
  177.  *
  178.  */
  179.  
  180.   _RWSTD_TEMPLATE  
  181.   struct _RWSTDExport char_traits<char> 
  182.   {
  183.  
  184.     typedef char                      char_type;
  185.     typedef int                       int_type;
  186.        
  187. #ifdef _RW_STD_IOSTREAM
  188.     typedef streamoff                 off_type; 
  189.     typedef streampos                 pos_type;
  190.     typedef mbstate_t                 state_type;
  191. #endif 
  192.  
  193.     static void assign (char_type& c1, const char_type& c2)   
  194.     { c1 = c2; }
  195.  
  196.     static bool eq(const char_type& c1,const char_type& c2)
  197.     { return (c1 == c2); }
  198.  
  199.     static bool lt (const char_type& c1, const char_type& c2) 
  200.     { return c1 < c2; }
  201.  
  202.     static int compare (const char_type* s1, const char_type* s2, size_t n)
  203.     { return memcmp(s1, s2, n); }
  204.  
  205.     static const char_type* find (const char_type* s,
  206.                                   size_t n, const char_type& a)
  207.     { return (char_type*) memchr(s,a,n); }
  208.  
  209.     static size_t length(const char_type *s)
  210.     { return strlen(s); }
  211.  
  212.     static char_type * move (char_type* s1, const char_type* s2, size_t n)
  213.     {
  214. #ifndef _RWSTD_NO_MEMMOVE
  215.       memmove(s1, s2, n);
  216. #else
  217.       char_type * s = s1;
  218.       if (s1 < s2)
  219.         copy(s1, s2, n);
  220.       else if (s1 > s2)
  221.       {
  222.         s1 += n;
  223.         s2 += n;
  224.         for(size_t i = 0; i < n; ++i)
  225.           assign(*--s1, *--s2);
  226.       }
  227. #endif
  228.       return s1;
  229.     }
  230.  
  231.     static char_type  *copy(char_type *dst,const char_type *src, size_t n)
  232.     { 
  233.       memcpy(dst, src, n); 
  234.       return dst;
  235.     }
  236.  
  237.     static char_type* assign (char_type* s, size_t n, char_type a)
  238.     {
  239.       memset(s,a,n);
  240.       return s;
  241.     }
  242.  
  243.     static int_type not_eof(const int_type& c)
  244.     {
  245.       if ( c == EOF )
  246.         return 0;
  247.       else
  248.         return c;
  249.     }
  250.                         
  251.     static char_type to_char_type(const int_type& c)
  252.     { return (char)c; }
  253.     static int_type to_int_type(const char_type& c)
  254.     { return (unsigned char)c; }
  255.  
  256.     static bool eq_int_type(const int_type& c1,const int_type& c2)
  257.     { return (c1 == c2); }
  258.  
  259. #ifdef _RW_STD_IOSTREAM
  260.     static state_type get_state(pos_type pos)
  261.     { return pos.state(); }
  262. #endif 
  263.  
  264.     static int_type eof()
  265.     { return EOF; }
  266.   };
  267. /*
  268.  *
  269.  *     STRUCT CHAR_TRAITS SPECIALIZED FOR WCHAR_T
  270.  *
  271.  */
  272. #ifndef _RWSTD_NO_WIDE_CHAR
  273.  
  274.   _RWSTD_TEMPLATE  
  275.   struct _RWSTDExport char_traits<wchar_t> 
  276.   {
  277.     typedef wchar_t                   char_type;
  278.  
  279. #ifndef _RWSTD_NO_WINT_TYPE
  280.     typedef wint_t                    int_type;
  281. #else
  282.     typedef long                      int_type;
  283. #endif
  284.  
  285. #ifndef WEOF
  286. #define WEOF (-1)
  287. #endif
  288.  
  289. #ifdef _RW_STD_IOSTREAM
  290.     typedef wstreamoff                off_type;
  291.     typedef wstreampos                pos_type;
  292.     typedef mbstate_t                 state_type;
  293. #endif /* _RW_STD_IOSTREAM */        
  294.  
  295.     static void assign (char_type& c1, const char_type& c2)   
  296.     { c1 = c2;}
  297.  
  298.     static bool eq(const char_type& c1,const char_type& c2)
  299.     { return (c1 == c2); }
  300.  
  301.     static bool lt (const char_type& c1, const char_type& c2) 
  302.     { return c1 < c2;}
  303.  
  304.     static int compare (const char_type* s1, const char_type* s2, size_t n)
  305.     {
  306. #ifndef _RWSTD_NO_WSTR
  307.       return memcmp(s1, s2, n*sizeof(char_type));
  308. #else
  309.       size_t i=0;
  310.  
  311.       while ( i < n )
  312.       { 
  313.         if ( !eq( s1[i], s2[i] ) )
  314.         {
  315.           if ( lt( s1[i], s2[i]) )
  316.             return -1;
  317.           else
  318.             return 1;               
  319.         }
  320.         i++;
  321.       }
  322.       return 0;
  323. #endif
  324.     }
  325.     static size_t length(const char_type *s)
  326.     {
  327. #ifndef _RWSTD_NO_WSTR   
  328.       return wcslen(s);
  329. #else
  330.       size_t l = 0;
  331.       while ( !eq(*s++, char_type(0) ) )  ++l;
  332.       return l;
  333. #endif
  334.     }
  335.  
  336.     static const char_type* find (const char_type* s, size_t n,
  337.                                   const char_type& a)
  338.     {
  339. #ifndef _RWSTD_NOT_ALL_WSTR_CFUNCTIONS
  340.       return (char_type*) wmemchr(s,a,n);
  341. #else
  342.       while (n-- > 0 && *s != a) 
  343.         ++s;
  344.       return  *s == a ? s : 0;
  345. #endif
  346.     }
  347.  
  348.     static char_type * move (char_type* s1, const char_type* s2, size_t n)
  349.     {
  350. #ifndef _RWSTD_NO_MEMMOVE
  351.       memmove(s1, s2, n*sizeof(char_type));
  352. #else
  353.       char_type * s = s1;
  354.       if (s1 < s2)
  355.         copy(s1, s2, n);
  356.       else if (s1 > s2)
  357.       {
  358.         s1 += n;
  359.         s2 += n;
  360.         for(size_t i = 0; i < n; ++i) assign(*--s1, *--s2);
  361.       }
  362. #endif
  363.       return s1;
  364.     }
  365.  
  366.     static char_type * copy(char_type *dst,const char_type *src, size_t n)
  367.     {
  368.       memcpy((char *)dst,(char *)src, n*sizeof(char_type));
  369.       return dst;
  370.     }
  371.  
  372.     static char_type* assign (char_type* s, size_t n, char_type a)
  373.     {
  374. #ifndef _RWSTD_NOT_ALL_WSTR_CFUNCTIONS
  375.       wmemset(s,a,n);
  376. #else
  377.       char_type* tmp = s;
  378.       while (n-- > 0) 
  379.         *tmp++ = a;
  380. #endif
  381.       return s;
  382.     }
  383.  
  384.     static int_type not_eof(const int_type& c)
  385.     {
  386.       if(c == WEOF)
  387.         return 0;
  388.       else
  389.         return c;
  390.     }
  391.  
  392.     static char_type to_char_type(const int_type& c)
  393.     { return c; }
  394.     static int_type to_int_type(const char_type& c)
  395.     { return c; }
  396.  
  397.     static bool eq_int_type(const int_type& c1,const int_type& c2)
  398.     { return (c1 == c2); }
  399.  
  400. #ifdef _RW_STD_IOSTREAM
  401.     static state_type get_state(pos_type pos)
  402.     { return pos.state(); }
  403. #endif 
  404.  
  405.     static int_type             eof()
  406.     { return WEOF; }
  407.   };
  408.  
  409. #endif
  410. #ifndef _RWSTD_NO_NAMESPACE
  411. } namespace __rwstd {
  412. #endif
  413. //
  414. // Implementation traits class, rw_traits, provides specialized
  415. // algorithms to speed up find operations on char and wchar_t strings
  416. //
  417.  
  418.   template <class charT, class traits> 
  419.   struct _RWSTDExportTemplate rw_traits
  420.   {
  421.     static const charT* find(const charT* s, const charT* v)
  422.     { 
  423.       size_t slen = traits::length(s);
  424.       size_t vlen = traits::length(v);
  425.       for (size_t xpos = 0; (xpos + vlen) <= slen ; xpos++)
  426.       {
  427.         bool found = true;
  428.         for (size_t i = 0; i < vlen ; i++)
  429.         {
  430.           if (!traits::eq(s[xpos + i], v[i]))
  431.           {
  432.             found = false;
  433.             break;
  434.           }
  435.         }
  436.         if (found)
  437.           return &s[xpos];
  438.       }
  439.       return NULL;
  440.     }
  441.  
  442.     static const charT* rfind(const charT* s, charT v, size_t pos)
  443.     {
  444.       const charT* p = s + pos;
  445.       while (p >= s)
  446.       {
  447.         if (traits::eq(*p,v))
  448.           return p;
  449.         p--;
  450.       }
  451.       return NULL;       
  452.     }
  453.  
  454.     static size_t find_first_of(const charT* s, const charT* v)
  455.     {
  456.       const charT* p = s;
  457.       for (; *p; p++)
  458.       {
  459.         for (const charT* q = v; *q; q++)
  460.           if (traits::eq(*p, *q))
  461.             return p-s;
  462.       }
  463.       return  p-s;
  464.     }
  465.  
  466.     static size_t find_first_not_of(const charT* s, const charT* v)
  467.     {
  468.       const charT* p = s;
  469.       for (; *p; p++)
  470.       {
  471.         for (const charT* q = v; *q; q++)
  472.           if (!traits::eq(*p, *q))
  473.             return p-s;
  474.       }
  475.       return  p-s;
  476.     }
  477.   };
  478.  
  479.   _RWSTD_TEMPLATE 
  480.   struct _RWSTDExport rw_traits<char,_RW_STD::char_traits<char> >
  481.   {
  482.     static const char* find(const char* s, const char* v)
  483. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE)
  484.     { return std::strstr(s,v); }
  485. #else
  486.     { return strstr(s,v); }
  487. #endif
  488.     static const char* rfind(const char* s, char v, size_t pos)
  489.     { 
  490.       const char* p = s + pos;
  491.       while (p >= s)
  492.       {
  493.         if (_RW_STD::char_traits<char>::eq(*p,v))
  494.           return p;
  495.         p--; 
  496.       }
  497.       return NULL;       
  498.     }
  499.     static size_t find_first_of(const char* s, const char* v)
  500. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE)
  501.     { return std::strcspn(s,v); }
  502. #else
  503.     { return strcspn(s,v); }
  504. #endif
  505.     static size_t find_first_not_of(const char* s, const char* v)
  506. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE)
  507.     { return std::strspn(s,v); }
  508. #else
  509.     { return strspn(s,v); }
  510. #endif
  511.   };
  512.  
  513. #ifndef _RWSTD_NO_WSTR
  514.   _RWSTD_TEMPLATE 
  515.   struct _RWSTDExport rw_traits<wchar_t,_RW_STD::char_traits<wchar_t> >
  516.   {
  517.     static const wchar_t* find(const wchar_t* s, const wchar_t* v)
  518. #ifndef _HPACC_
  519. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE)
  520.     { return std::wcsstr(s,v); }
  521. #else
  522.     { return wcsstr(s,v); }
  523. #endif
  524. #else
  525.   // Having _HPACC_ defined sufficiently indicates both function name
  526.   // and that said function is not in namespace std.
  527.   { return wcswcs(s,v); }
  528. #endif //_HPACC_
  529.     static const wchar_t* rfind(const wchar_t* s, wchar_t v, size_t pos)
  530.     { 
  531.       const wchar_t* p = s + pos;
  532.       while (p >= s)
  533.       {
  534.         if (_RW_STD::char_traits<wchar_t>::eq(*p,v))
  535.           return p;
  536.         p--;
  537.       }
  538.       return NULL;       
  539.     }
  540.     static size_t find_first_of(const wchar_t* s, const wchar_t* v)
  541. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE) && !defined(_HPACC_)
  542.     { return std::wcscspn(s,v); }
  543. #else
  544.     { return wcscspn(s,v); }
  545. #endif
  546.     static size_t find_first_not_of(const wchar_t* s, const wchar_t* v)
  547. #if !defined(_RWSTD_NO_NEW_HEADER) && !defined(_RWSTD_NO_NAMESPACE) && !defined(_HPACC_)
  548.     { return std::wcsspn(s,v); }
  549. #else
  550.     { return wcsspn(s,v); }
  551. #endif
  552.   };
  553. #endif // _RWSTD_NO_WSTR
  554.  
  555. #ifndef _RWSTD_NO_NAMESPACE
  556. }
  557. #endif
  558.  
  559. #endif // __RW_TRAITS
  560. #pragma option pop
  561. #endif /* __TRAITS_H */
  562.