/home/ntakagi/work/STLport-5.1.5/stlport/stl/char_traits.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996,1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 #ifndef _STLP_CHAR_TRAITS_H
00020 #define _STLP_CHAR_TRAITS_H
00021 
00022 // Define char_traits
00023 
00024 #ifndef _STLP_INTERNAL_CSTDDEF
00025 #  include <stl/_cstddef.h>
00026 #endif
00027 
00028 #ifndef _STLP_INTERNAL_CSTRING
00029 #  include <stl/_cstring.h>
00030 #endif
00031 
00032 #if defined (__unix) || defined (N_PLAT_NLM)
00033 #  include <sys/types.h>         // For off_t
00034 #endif /* __unix */
00035 
00036 #ifdef __BORLANDC__
00037 #  include _STLP_NATIVE_C_HEADER(mem.h)
00038 #  include _STLP_NATIVE_C_HEADER(string.h)
00039 #  include _STLP_NATIVE_C_HEADER(_stddef.h)
00040 #endif
00041 
00042 #ifndef _STLP_INTERNAL_CONSTRUCT_H
00043 #  include <stl/_construct.h>
00044 #endif
00045 
00046 #ifndef _STLP_INTERNAL_CWCHAR
00047 #  include <stl/_cwchar.h>
00048 #endif
00049 
00050 _STLP_BEGIN_NAMESPACE
00051 
00052 template <class _Tp> class allocator;
00053 
00054 #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT)
00055 
00056 #if defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* IRIX */
00057 typedef off64_t streamoff;
00058 #elif defined(_STLP_WCE)
00059 typedef long streamoff;
00060 #elif defined (_STLP_WIN32)
00061 #  if defined (_STLP_LONG_LONG) && !defined (__CYGWIN__)
00062 //The Win32 file io API support 64 bits access so streamoff and streamsize
00063 //has to reflect that. Do not change the stringbuf behavior.
00064 typedef _STLP_LONG_LONG streamoff;
00065 #  else
00066 typedef ptrdiff_t streamoff;
00067 #  endif
00068 #else // __unix
00069 #  ifdef _STLP_USE_DEFAULT_FILE_OFFSET
00070 typedef off_t streamoff;
00071 #  elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
00072        /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) */
00073 typedef off64_t streamoff;
00074 #  else
00075 typedef off_t streamoff;
00076 #  endif
00077 #endif /* ___unix */
00078 
00079 #if defined (_STLP_WIN32)
00080 typedef streamoff streamsize;
00081 #else
00082 typedef ptrdiff_t streamsize;
00083 #endif
00084 
00085 // Class fpos, which represents a position within a file.  (The C++
00086 // standard calls for it to be defined in <ios>.  This implementation
00087 // moves it to <iosfwd>, which is included by <ios>.)
00088 template <class _StateT> class fpos {
00089 public:                         // From table 88 of the C++ standard.
00090   fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
00091   fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
00092 
00093   operator streamoff() const { return _M_pos; }
00094 
00095   bool operator==(const fpos& __y) const
00096   { return _M_pos == __y._M_pos; }
00097   bool operator!=(const fpos& __y) const
00098   { return _M_pos != __y._M_pos; }
00099 
00100   fpos& operator+=(streamoff __off) {
00101     _M_pos += __off;
00102     return *this;
00103   }
00104   fpos& operator-=(streamoff __off) {
00105     _M_pos -= __off;
00106     return *this;
00107   }
00108 
00109   fpos operator+(streamoff __off) {
00110     fpos __tmp(*this);
00111     __tmp += __off;
00112     return __tmp;
00113   }
00114   fpos operator-(streamoff __off) {
00115     fpos __tmp(*this);
00116     __tmp -= __off;
00117     return __tmp;
00118   }
00119 
00120 public:                         // Manipulation of the state member.
00121   _StateT state() const { return _M_st; }
00122   void state(_StateT __st) { _M_st = __st; }
00123 private:
00124   streamoff _M_pos;
00125   _StateT _M_st;
00126 };
00127 
00128 #if !defined (_STLP_NO_MBSTATE_T)
00129 typedef fpos<mbstate_t> streampos;
00130 typedef fpos<mbstate_t> wstreampos;
00131 #endif
00132 
00133 // Class __char_traits_base.
00134 template <class _CharT, class _IntT>
00135 class __char_traits_base {
00136 public:
00137   typedef _CharT char_type;
00138   typedef _IntT int_type;
00139   typedef streamoff off_type;
00140   typedef streampos pos_type;
00141 #if defined (_STLP_NO_MBSTATE_T)
00142   typedef char      state_type;
00143 #else
00144   typedef mbstate_t state_type;
00145 #endif
00146 
00147   static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
00148   static bool _STLP_CALL eq(const char_type& __c1, const char_type& __c2)
00149   { return __c1 == __c2; }
00150   static bool _STLP_CALL lt(const char_type& __c1, const char_type& __c2)
00151   { return __c1 < __c2; }
00152 
00153   static int _STLP_CALL compare(const char_type* __s1, const char_type* __s2, size_t __n) {
00154     for (size_t __i = 0; __i < __n; ++__i)
00155       if (!eq(__s1[__i], __s2[__i]))
00156         return __s1[__i] < __s2[__i] ? -1 : 1;
00157     return 0;
00158   }
00159 
00160   static size_t _STLP_CALL length(const char_type* __s) {
00161     const char_type _NullChar = _STLP_DEFAULT_CONSTRUCTED(char_type);
00162     size_t __i(0);
00163     for (; !eq(__s[__i], _NullChar); ++__i) {}
00164     return __i;
00165   }
00166 
00167   static const char_type* _STLP_CALL find(const char_type* __s, size_t __n, const char_type& __c) {
00168     for ( ; __n > 0 ; ++__s, --__n)
00169       if (eq(*__s, __c))
00170         return __s;
00171     return 0;
00172   }
00173 
00174   static char_type* _STLP_CALL move(char_type* __s1, const char_type* __s2, size_t _Sz)
00175   { return (_Sz == 0 ? __s1 : (char_type*)memmove(__s1, __s2, _Sz * sizeof(char_type))); }
00176 
00177   static char_type* _STLP_CALL copy(char_type* __s1, const char_type* __s2, size_t __n) {
00178     return (__n == 0 ? __s1 :
00179       (char_type*)memcpy(__s1, __s2, __n * sizeof(char_type)));
00180   }
00181 
00182   static char_type* _STLP_CALL assign(char_type* __s, size_t __n, char_type __c) {
00183     for (size_t __i = 0; __i < __n; ++__i)
00184       __s[__i] = __c;
00185     return __s;
00186   }
00187 
00188   static int_type _STLP_CALL not_eof(const int_type& __c)
00189   { return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0); }
00190 
00191   static char_type _STLP_CALL to_char_type(const int_type& __c)
00192   { return (char_type)__c; }
00193 
00194   static int_type _STLP_CALL to_int_type(const char_type& __c)
00195   { return (int_type)__c; }
00196 
00197   static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2)
00198   { return __c1 == __c2; }
00199 
00200   static int_type _STLP_CALL eof()
00201   { return (int_type)-1; }
00202 };
00203 
00204 // Generic char_traits class.  Note that this class is provided only
00205 //  as a base for explicit specialization; it is unlikely to be useful
00206 //  as is for any particular user-defined type.  In particular, it
00207 //  *will not work* for a non-POD type.
00208 
00209 template <class _CharT>
00210 class char_traits
00211   : public __char_traits_base<_CharT, _CharT> {};
00212 
00213 // Specialization for char.
00214 
00215 _STLP_TEMPLATE_NULL
00216 class _STLP_CLASS_DECLSPEC char_traits<char>
00217   : public __char_traits_base<char, int> {
00218 public:
00219   typedef char char_type;
00220   typedef int int_type;
00221   typedef streamoff off_type;
00222 #if !defined (_STLP_NO_MBSTATE_T)
00223   typedef streampos pos_type;
00224   typedef mbstate_t state_type;
00225 #endif
00226 
00227   static char _STLP_CALL to_char_type(const int& __c)
00228   { return (char)(unsigned char)__c; }
00229 
00230   static int _STLP_CALL to_int_type(const char& __c)
00231   { return (unsigned char)__c; }
00232 
00233   static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n)
00234   { return memcmp(__s1, __s2, __n); }
00235 
00236   static size_t _STLP_CALL length(const char* __s)
00237   { return strlen(__s); }
00238 
00239   static void _STLP_CALL assign(char& __c1, const char& __c2)
00240   { __c1 = __c2; }
00241 
00242   static char* _STLP_CALL assign(char* __s, size_t __n, char __c) {
00243     memset(__s, __c, __n);
00244     return __s;
00245   }
00246 };
00247 
00248 #if defined (_STLP_HAS_WCHAR_T)
00249 // Specialization for wchar_t.
00250 _STLP_TEMPLATE_NULL
00251 class _STLP_CLASS_DECLSPEC char_traits<wchar_t>
00252   : public __char_traits_base<wchar_t, wint_t> {
00253 #  if !defined (_STLP_NO_NATIVE_WIDE_FUNCTIONS) && !defined (_STLP_WCHAR_HPACC_EXCLUDE)
00254 public:
00255 #    if !defined (N_PLAT_NLM)
00256 #      if !defined (__BORLANDC__)
00257   static wchar_t* _STLP_CALL move(wchar_t* __dest, const wchar_t* __src, size_t __n)
00258   { return wmemmove(__dest, __src, __n); }
00259 #      endif
00260 
00261   static wchar_t* _STLP_CALL copy(wchar_t* __dest, const wchar_t* __src, size_t __n)
00262   { return wmemcpy(__dest, __src, __n); }
00263 
00264 #      if !defined (__DMC__) && !defined (__BORLANDC__)
00265   static int _STLP_CALL compare(const wchar_t* __s1, const wchar_t* __s2, size_t __n)
00266   { return wmemcmp(__s1, __s2, __n); }
00267 #      endif
00268 
00269   static wchar_t* _STLP_CALL assign(wchar_t* __s, size_t __n, wchar_t __c)
00270   { return wmemset(__s, __c, __n); }
00271 #    endif
00272 
00273   static size_t _STLP_CALL length(const wchar_t* __s)
00274   { return wcslen(__s); }
00275 
00276   static void _STLP_CALL assign(wchar_t& __c1, const wchar_t& __c2)
00277   { __c1 = __c2; }
00278 #  endif
00279 };
00280 #endif
00281 
00282 _STLP_END_NAMESPACE
00283 
00284 #endif /* _STLP_CHAR_TRAITS_H */
00285 
00286 // Local Variables:
00287 // mode:C++
00288 // End:



Generated on Mon Mar 10 15:32:46 2008 by  doxygen 1.5.1