home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / char_traits.h < prev    next >
C/C++ Source or Header  |  2001-09-29  |  7KB  |  267 lines

  1. /*
  2.  * Copyright (c) 1996,1997
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */
  18.  
  19. #ifndef _STLP_CHAR_TRAITS_H
  20. #define _STLP_CHAR_TRAITS_H
  21.  
  22. // Define char_traits
  23.  
  24. # if defined (_STLP_OWN_IOSTREAMS) || ! defined (_STLP_USE_NEW_IOSTREAMS)
  25.  
  26. # if ! defined (_STLP_CSTDDEF)
  27. #  include <cstddef>
  28. # endif
  29.  
  30. # if !defined (_STLP_CWCHAR)
  31. #  include <cwchar>
  32. # endif            
  33.  
  34. #if ! defined (_STLP_CSTRING)
  35. #  include <cstring>
  36. #endif
  37.  
  38. #ifndef __TYPE_TRAITS_H
  39. # include <stl/type_traits.h>
  40. #endif
  41.  
  42. #if defined (_STLP_UNIX) && defined (_STLP_HAS_NO_NEW_C_HEADERS)
  43. #include <sys/types.h>          // For off_t
  44. #endif /* __unix */
  45.  
  46. #ifdef __BORLANDC__
  47. # include <mem.h>
  48. # include <string.h>
  49. # include <_stddef.h>
  50. class mbstate_t;
  51. #endif
  52.  
  53. _STLP_BEGIN_NAMESPACE
  54.  
  55. # ifdef _STLP_OWN_IOSTREAMS
  56.  
  57. template <class _Tp> class allocator;
  58.  
  59. #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT)
  60.  
  61. #if defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* IRIX */
  62. typedef off64_t   streamoff;
  63. // #elif defined (__unix) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* Other version of UNIX */
  64. // typedef off_t     streamoff;
  65. #else /* __unix */
  66. // boris : here, it's not ptrdiff_t as some Solaris systems have confusing definitions of these.
  67. typedef long streamoff;
  68. #endif /* _STLP_HAS_NO_NEW_C_HEADERS */
  69.  
  70. typedef ptrdiff_t streamsize;
  71.  
  72. // Class fpos, which represents a position within a file.  (The C++
  73. // standard calls for it to be defined in <ios>.  This implementation
  74. // moves it to <iosfwd>, which is included by <ios>.)
  75. template <class _StateT> class fpos
  76. {
  77. public:                         // From table 88 of the C++ standard.
  78.   fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
  79.   fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
  80.  
  81.   operator streamoff() const { return _M_pos; }
  82.  
  83.   bool  _STLP_CALL operator==(const fpos<_StateT>& __y) const
  84.     { return _M_pos == __y._M_pos; }
  85.   bool _STLP_CALL operator!=(const fpos<_StateT>& __y) const
  86.     { return _M_pos != __y._M_pos; }
  87.  
  88.   fpos<_StateT>& operator+=(streamoff __off) {
  89.     _M_pos += __off;
  90.     return *this;
  91.   }
  92.   fpos<_StateT>& operator-=(streamoff __off) {
  93.     _M_pos -= __off;
  94.     return *this;
  95.   }
  96.  
  97.   fpos<_StateT> operator+(streamoff __off) {
  98.     fpos<_StateT> __tmp(*this);
  99.     __tmp += __off;
  100.     return __tmp;
  101.   }
  102.   fpos<_StateT> operator-(streamoff __off) {
  103.     fpos<_StateT> __tmp(*this);
  104.     __tmp -= __off;
  105.     return __tmp;
  106.   }
  107.  
  108. public:                         // Manipulation of the state member.
  109.   _StateT state() const { return _M_st; }
  110.   void state(_StateT __st) { _M_st = __st; }
  111. private:
  112.   streamoff _M_pos;
  113.   _StateT _M_st;
  114. };
  115.  
  116. typedef fpos<mbstate_t> streampos;
  117. typedef fpos<mbstate_t> wstreampos;
  118. # endif
  119.  
  120. // Class __char_traits_base.
  121.  
  122. template <class _CharT, class _IntT> class __char_traits_base {
  123. public:
  124.   typedef _CharT char_type;
  125.   typedef _IntT int_type;
  126. #ifdef _STLP_USE_NEW_IOSTREAMS
  127.   typedef streamoff off_type;
  128.   typedef streampos pos_type;
  129. # ifdef _STLP_NO_MBSTATE_T
  130.   typedef char      state_type;
  131. # else
  132.   typedef mbstate_t state_type;
  133. # endif
  134. #endif /* _STLP_USE_NEW_IOSTREAMS */
  135.  
  136.   static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
  137.   static bool _STLP_CALL eq(const _CharT& __c1, const _CharT& __c2) 
  138.     { return __c1 == __c2; }
  139.   static bool _STLP_CALL lt(const _CharT& __c1, const _CharT& __c2) 
  140.     { return __c1 < __c2; }
  141.  
  142.   static int _STLP_CALL compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {
  143.     for (size_t __i = 0; __i < __n; ++__i)
  144.       if (!eq(__s1[__i], __s2[__i]))
  145.         return __s1[__i] < __s2[__i] ? -1 : 1;
  146.     return 0;
  147.   }
  148.  
  149.   static size_t _STLP_CALL length(const _CharT* __s) {
  150.     const _CharT _NullChar = _STLP_DEFAULT_CONSTRUCTED(_CharT);
  151.     size_t __i;
  152.     for (__i = 0; !eq(__s[__i], _NullChar); ++__i)
  153.       {}
  154.     return __i;
  155.   }
  156.  
  157.   static const _CharT* _STLP_CALL find(const _CharT* __s, size_t __n, const _CharT& __c) {
  158.     for ( ; __n > 0 ; ++__s, --__n)
  159.       if (eq(*__s, __c))
  160.         return __s;
  161.     return 0;
  162.   }
  163.  
  164.  
  165.   static _CharT* _STLP_CALL move(_CharT* __s1, const _CharT* __s2, size_t _Sz) {    
  166.     return (_Sz == 0 ? __s1 : (_CharT*)memmove(__s1, __s2, _Sz * sizeof(_CharT)));
  167.   }
  168.   
  169.   static _CharT* _STLP_CALL copy(_CharT* __s1, const _CharT* __s2, size_t __n) {
  170.     return (__n == 0 ? __s1 :
  171.         (_CharT*)memcpy(__s1, __s2, __n * sizeof(_CharT)));
  172.     } 
  173.  
  174.   static _CharT* _STLP_CALL assign(_CharT* __s, size_t __n, _CharT __c) {
  175.     for (size_t __i = 0; __i < __n; ++__i)
  176.       __s[__i] = __c;
  177.     return __s;
  178.   }
  179.  
  180.   static int_type _STLP_CALL not_eof(const int_type& __c) {
  181.     return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0);
  182.   }
  183.  
  184.   static char_type _STLP_CALL to_char_type(const int_type& __c) {
  185.     return (char_type)__c;
  186.   }
  187.  
  188.   static int_type _STLP_CALL to_int_type(const char_type& __c) {
  189.     return (int_type)__c;
  190.   }
  191.  
  192.   static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2) {
  193.     return __c1 == __c2;
  194.   }
  195.  
  196.   static int_type _STLP_CALL eof() {
  197.     return (int_type)-1;
  198.     //    return __STATIC_CAST(int_type,-1);
  199.   }
  200. };
  201.  
  202. // Generic char_traits class.  Note that this class is provided only
  203. //  as a base for explicit specialization; it is unlikely to be useful
  204. //  as is for any particular user-defined type.  In particular, it 
  205. //  *will not work* for a non-POD type.
  206.  
  207. template <class _CharT> class char_traits
  208.   : public __char_traits_base<_CharT, _CharT>
  209. {};
  210.  
  211. // Specialization for char.
  212.  
  213. _STLP_TEMPLATE_NULL class _STLP_CLASS_DECLSPEC char_traits<char> 
  214.   : public __char_traits_base<char, int>
  215. {
  216. public:
  217.   typedef char char_type;
  218.   typedef int int_type;
  219. #ifdef _STLP_USE_NEW_IOSTREAMS
  220.   typedef streamoff off_type;
  221. # ifndef _STLP_NO_MBSTATE_T
  222.   typedef streampos pos_type;
  223.   typedef mbstate_t state_type;
  224. # endif
  225. #endif /* _STLP_USE_NEW_IOSTREAMS */
  226.  
  227.   static char _STLP_CALL to_char_type(const int& __c) {
  228.     return (char)(unsigned char)__c;
  229.   }
  230.  
  231.   static int _STLP_CALL to_int_type(const char& __c) {
  232.     return (unsigned char)__c;
  233.   }
  234.  
  235.   static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n) 
  236.     { return memcmp(__s1, __s2, __n); }
  237.   
  238.   static size_t _STLP_CALL length(const char* __s) { return strlen(__s); }
  239.  
  240.   static void _STLP_CALL assign(char& __c1, const char& __c2) { __c1 = __c2; }
  241.  
  242.   static char* _STLP_CALL assign(char* __s, size_t __n, char __c)
  243.     { memset(__s, __c, __n); return __s; }
  244. };
  245.  
  246. # if defined (_STLP_HAS_WCHAR_T)
  247. // Specialization for wchar_t.
  248. _STLP_TEMPLATE_NULL class _STLP_CLASS_DECLSPEC char_traits<wchar_t>
  249.   : public __char_traits_base<wchar_t, wint_t>
  250. {};
  251. # endif
  252.  
  253. _STLP_END_NAMESPACE
  254.  
  255. # else /* OWN_IOSTREAMS */
  256.  
  257. #  include <wrap_std/iosfwd>
  258.  
  259. # endif /* OWN_IOSTREAMS */
  260.  
  261. #endif /* _STLP_CHAR_TRAITS_H */
  262.  
  263. // Local Variables:
  264. // mode:C++
  265. // End:
  266.  
  267.