epoc32/include/tools/stlport/stl/char_traits.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     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 #ifndef _STLP_INTERNAL_CSTDDEF
    25 #  include <stl/_cstddef.h>
    26 #endif
    27 
    28 #ifndef _STLP_INTERNAL_CSTRING
    29 #  include <stl/_cstring.h>
    30 #endif
    31 
    32 #if defined (__unix) || defined (N_PLAT_NLM)
    33 #  include <sys/types.h>         // For off_t
    34 #endif /* __unix */
    35 
    36 #ifdef __BORLANDC__
    37 #  include _STLP_NATIVE_C_HEADER(mem.h)
    38 #  include _STLP_NATIVE_C_HEADER(string.h)
    39 #  include _STLP_NATIVE_C_HEADER(_stddef.h)
    40 #endif
    41 
    42 #ifndef _STLP_INTERNAL_CONSTRUCT_H
    43 #  include <stl/_construct.h>
    44 #endif
    45 
    46 #ifndef _STLP_INTERNAL_CWCHAR
    47 #  include <stl/_cwchar.h>
    48 #endif
    49 
    50 _STLP_BEGIN_NAMESPACE
    51 
    52 template <class _Tp> class allocator;
    53 
    54 #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT)
    55 
    56 #if defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* IRIX */
    57 typedef off64_t streamoff;
    58 #elif defined(_STLP_WCE)
    59 typedef long streamoff;
    60 #elif defined (_STLP_WIN32)
    61 #  if defined (_STLP_LONG_LONG) && !defined (__CYGWIN__)
    62 //The Win32 file io API support 64 bits access so streamoff and streamsize
    63 //has to reflect that. Do not change the stringbuf behavior.
    64 typedef _STLP_LONG_LONG streamoff;
    65 #  else
    66 typedef ptrdiff_t streamoff;
    67 #  endif
    68 #else // __unix
    69 typedef off_t streamoff;
    70 #endif /* _STLP_HAS_NO_NEW_C_HEADERS */
    71 
    72 #if defined (_STLP_WIN32)
    73 typedef streamoff streamsize;
    74 #else
    75 typedef ptrdiff_t streamsize;
    76 #endif
    77 
    78 // Class fpos, which represents a position within a file.  (The C++
    79 // standard calls for it to be defined in <ios>.  This implementation
    80 // moves it to <iosfwd>, which is included by <ios>.)
    81 template <class _StateT> class fpos {
    82 public:                         // From table 88 of the C++ standard.
    83   fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
    84   fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
    85 
    86   operator streamoff() const { return _M_pos; }
    87 
    88   bool operator==(const fpos& __y) const
    89   { return _M_pos == __y._M_pos; }
    90   bool operator!=(const fpos& __y) const
    91   { return _M_pos != __y._M_pos; }
    92 
    93   fpos& operator+=(streamoff __off) {
    94     _M_pos += __off;
    95     return *this;
    96   }
    97   fpos& operator-=(streamoff __off) {
    98     _M_pos -= __off;
    99     return *this;
   100   }
   101 
   102   fpos operator+(streamoff __off) {
   103     fpos __tmp(*this);
   104     __tmp += __off;
   105     return __tmp;
   106   }
   107   fpos operator-(streamoff __off) {
   108     fpos __tmp(*this);
   109     __tmp -= __off;
   110     return __tmp;
   111   }
   112 
   113 public:                         // Manipulation of the state member.
   114   _StateT state() const { return _M_st; }
   115   void state(_StateT __st) { _M_st = __st; }
   116 private:
   117   streamoff _M_pos;
   118   _StateT _M_st;
   119 };
   120 
   121 #if !defined (_STLP_NO_MBSTATE_T)
   122 typedef fpos<mbstate_t> streampos;
   123 typedef fpos<mbstate_t> wstreampos;
   124 #endif
   125 
   126 // Class __char_traits_base.
   127 template <class _CharT, class _IntT>
   128 class __char_traits_base {
   129 public:
   130   typedef _CharT char_type;
   131   typedef _IntT int_type;
   132   typedef streamoff off_type;
   133   typedef streampos pos_type;
   134 #if defined (_STLP_NO_MBSTATE_T)
   135   typedef char      state_type;
   136 #else
   137   typedef mbstate_t state_type;
   138 #endif
   139 
   140   static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
   141   static bool _STLP_CALL eq(const char_type& __c1, const char_type& __c2)
   142   { return __c1 == __c2; }
   143   static bool _STLP_CALL lt(const char_type& __c1, const char_type& __c2)
   144   { return __c1 < __c2; }
   145 
   146   static int _STLP_CALL compare(const char_type* __s1, const char_type* __s2, size_t __n) {
   147     for (size_t __i = 0; __i < __n; ++__i)
   148       if (!eq(__s1[__i], __s2[__i]))
   149         return __s1[__i] < __s2[__i] ? -1 : 1;
   150     return 0;
   151   }
   152 
   153   static size_t _STLP_CALL length(const char_type* __s) {
   154     const char_type _NullChar = _STLP_DEFAULT_CONSTRUCTED(char_type);
   155     size_t __i(0);
   156     for (; !eq(__s[__i], _NullChar); ++__i) {}
   157     return __i;
   158   }
   159 
   160   static const char_type* _STLP_CALL find(const char_type* __s, size_t __n, const char_type& __c) {
   161     for ( ; __n > 0 ; ++__s, --__n)
   162       if (eq(*__s, __c))
   163         return __s;
   164     return 0;
   165   }
   166 
   167   static char_type* _STLP_CALL move(char_type* __s1, const char_type* __s2, size_t _Sz)
   168   { return (_Sz == 0 ? __s1 : (char_type*)memmove(__s1, __s2, _Sz * sizeof(char_type))); }
   169 
   170   static char_type* _STLP_CALL copy(char_type* __s1, const char_type* __s2, size_t __n) {
   171     return (__n == 0 ? __s1 :
   172       (char_type*)memcpy(__s1, __s2, __n * sizeof(char_type)));
   173   }
   174 
   175   static char_type* _STLP_CALL assign(char_type* __s, size_t __n, char_type __c) {
   176     for (size_t __i = 0; __i < __n; ++__i)
   177       __s[__i] = __c;
   178     return __s;
   179   }
   180 
   181   static int_type _STLP_CALL not_eof(const int_type& __c)
   182   { return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0); }
   183 
   184   static char_type _STLP_CALL to_char_type(const int_type& __c)
   185   { return (char_type)__c; }
   186 
   187   static int_type _STLP_CALL to_int_type(const char_type& __c)
   188   { return (int_type)__c; }
   189 
   190   static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2)
   191   { return __c1 == __c2; }
   192 
   193   static int_type _STLP_CALL eof()
   194   { return (int_type)-1; }
   195 };
   196 
   197 // Generic char_traits class.  Note that this class is provided only
   198 //  as a base for explicit specialization; it is unlikely to be useful
   199 //  as is for any particular user-defined type.  In particular, it
   200 //  *will not work* for a non-POD type.
   201 
   202 template <class _CharT>
   203 class char_traits
   204   : public __char_traits_base<_CharT, _CharT> {};
   205 
   206 // Specialization for char.
   207 
   208 _STLP_TEMPLATE_NULL
   209 class _STLP_CLASS_DECLSPEC char_traits<char>
   210   : public __char_traits_base<char, int> {
   211 public:
   212   typedef char char_type;
   213   typedef int int_type;
   214   typedef streamoff off_type;
   215 #if !defined (_STLP_NO_MBSTATE_T)
   216   typedef streampos pos_type;
   217   typedef mbstate_t state_type;
   218 #endif
   219 
   220   static char _STLP_CALL to_char_type(const int& __c)
   221   { return (char)(unsigned char)__c; }
   222 
   223   static int _STLP_CALL to_int_type(const char& __c)
   224   { return (unsigned char)__c; }
   225 
   226   static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n)
   227   { return memcmp(__s1, __s2, __n); }
   228 
   229   static size_t _STLP_CALL length(const char* __s)
   230   { return strlen(__s); }
   231 
   232   static void _STLP_CALL assign(char& __c1, const char& __c2)
   233   { __c1 = __c2; }
   234 
   235   static char* _STLP_CALL assign(char* __s, size_t __n, char __c) {
   236     memset(__s, __c, __n);
   237     return __s;
   238   }
   239 };
   240 
   241 #if defined (_STLP_HAS_WCHAR_T)
   242 // Specialization for wchar_t.
   243 _STLP_TEMPLATE_NULL
   244 class _STLP_CLASS_DECLSPEC char_traits<wchar_t>
   245   : public __char_traits_base<wchar_t, wint_t> {
   246 #  if !defined (_STLP_NO_NATIVE_WIDE_FUNCTIONS) && !defined (_STLP_WCHAR_HPACC_EXCLUDE)
   247 public:
   248 #    if !defined (N_PLAT_NLM)
   249 #      if !defined (__BORLANDC__)
   250   static wchar_t* _STLP_CALL move(wchar_t* __dest, const wchar_t* __src, size_t __n)
   251   { return wmemmove(__dest, __src, __n); }
   252 #      endif
   253 
   254   static wchar_t* _STLP_CALL copy(wchar_t* __dest, const wchar_t* __src, size_t __n)
   255   { return wmemcpy(__dest, __src, __n); }
   256 
   257 #      if !defined (__DMC__) && !defined (__BORLANDC__)
   258   static int _STLP_CALL compare(const wchar_t* __s1, const wchar_t* __s2, size_t __n)
   259   { return wmemcmp(__s1, __s2, __n); }
   260 #      endif
   261 
   262   static wchar_t* _STLP_CALL assign(wchar_t* __s, size_t __n, wchar_t __c)
   263   { return wmemset(__s, __c, __n); }
   264 #    endif
   265 
   266   static size_t _STLP_CALL length(const wchar_t* __s)
   267   { return wcslen(__s); }
   268 
   269   static void _STLP_CALL assign(wchar_t& __c1, const wchar_t& __c2)
   270   { __c1 = __c2; }
   271 #  endif
   272 };
   273 #endif
   274 
   275 _STLP_END_NAMESPACE
   276 
   277 #endif /* _STLP_CHAR_TRAITS_H */
   278 
   279 // Local Variables:
   280 // mode:C++
   281 // End: