]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/char_traits.h
minor doc updates
[polintos/scott/priv.git] / include / c++ / stl / stl / char_traits.h
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 #  ifdef _STLP_USE_DEFAULT_FILE_OFFSET
70 typedef off_t streamoff;
71 #  elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
72        /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) */
73 typedef off64_t streamoff;
74 #  else
75 typedef off_t streamoff;
76 #  endif
77 #endif /* ___unix */
78
79 #if defined (_STLP_WIN32)
80 typedef streamoff streamsize;
81 #else
82 typedef ptrdiff_t streamsize;
83 #endif
84
85 // Class fpos, which represents a position within a file.  (The C++
86 // standard calls for it to be defined in <ios>.  This implementation
87 // moves it to <iosfwd>, which is included by <ios>.)
88 template <class _StateT> class fpos {
89 public:                         // From table 88 of the C++ standard.
90   fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
91   fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
92
93   operator streamoff() const { return _M_pos; }
94
95   bool operator==(const fpos& __y) const
96   { return _M_pos == __y._M_pos; }
97   bool operator!=(const fpos& __y) const
98   { return _M_pos != __y._M_pos; }
99
100   fpos& operator+=(streamoff __off) {
101     _M_pos += __off;
102     return *this;
103   }
104   fpos& operator-=(streamoff __off) {
105     _M_pos -= __off;
106     return *this;
107   }
108
109   fpos operator+(streamoff __off) {
110     fpos __tmp(*this);
111     __tmp += __off;
112     return __tmp;
113   }
114   fpos operator-(streamoff __off) {
115     fpos __tmp(*this);
116     __tmp -= __off;
117     return __tmp;
118   }
119
120 public:                         // Manipulation of the state member.
121   _StateT state() const { return _M_st; }
122   void state(_StateT __st) { _M_st = __st; }
123 private:
124   streamoff _M_pos;
125   _StateT _M_st;
126 };
127
128 #if !defined (_STLP_NO_MBSTATE_T)
129 typedef fpos<mbstate_t> streampos;
130 typedef fpos<mbstate_t> wstreampos;
131 #endif
132
133 // Class __char_traits_base.
134 template <class _CharT, class _IntT>
135 class __char_traits_base {
136 public:
137   typedef _CharT char_type;
138   typedef _IntT int_type;
139   typedef streamoff off_type;
140   typedef streampos pos_type;
141 #if defined (_STLP_NO_MBSTATE_T)
142   typedef char      state_type;
143 #else
144   typedef mbstate_t state_type;
145 #endif
146
147   static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
148   static bool _STLP_CALL eq(const char_type& __c1, const char_type& __c2)
149   { return __c1 == __c2; }
150   static bool _STLP_CALL lt(const char_type& __c1, const char_type& __c2)
151   { return __c1 < __c2; }
152
153   static int _STLP_CALL compare(const char_type* __s1, const char_type* __s2, size_t __n) {
154     for (size_t __i = 0; __i < __n; ++__i)
155       if (!eq(__s1[__i], __s2[__i]))
156         return __s1[__i] < __s2[__i] ? -1 : 1;
157     return 0;
158   }
159
160   static size_t _STLP_CALL length(const char_type* __s) {
161     const char_type _NullChar = _STLP_DEFAULT_CONSTRUCTED(char_type);
162     size_t __i(0);
163     for (; !eq(__s[__i], _NullChar); ++__i) {}
164     return __i;
165   }
166
167   static const char_type* _STLP_CALL find(const char_type* __s, size_t __n, const char_type& __c) {
168     for ( ; __n > 0 ; ++__s, --__n)
169       if (eq(*__s, __c))
170         return __s;
171     return 0;
172   }
173
174   static char_type* _STLP_CALL move(char_type* __s1, const char_type* __s2, size_t _Sz)
175   { return (_Sz == 0 ? __s1 : (char_type*)memmove(__s1, __s2, _Sz * sizeof(char_type))); }
176
177   static char_type* _STLP_CALL copy(char_type* __s1, const char_type* __s2, size_t __n) {
178     return (__n == 0 ? __s1 :
179       (char_type*)memcpy(__s1, __s2, __n * sizeof(char_type)));
180   }
181
182   static char_type* _STLP_CALL assign(char_type* __s, size_t __n, char_type __c) {
183     for (size_t __i = 0; __i < __n; ++__i)
184       __s[__i] = __c;
185     return __s;
186   }
187
188   static int_type _STLP_CALL not_eof(const int_type& __c)
189   { return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0); }
190
191   static char_type _STLP_CALL to_char_type(const int_type& __c)
192   { return (char_type)__c; }
193
194   static int_type _STLP_CALL to_int_type(const char_type& __c)
195   { return (int_type)__c; }
196
197   static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2)
198   { return __c1 == __c2; }
199
200   static int_type _STLP_CALL eof()
201   { return (int_type)-1; }
202 };
203
204 // Generic char_traits class.  Note that this class is provided only
205 //  as a base for explicit specialization; it is unlikely to be useful
206 //  as is for any particular user-defined type.  In particular, it
207 //  *will not work* for a non-POD type.
208
209 template <class _CharT>
210 class char_traits
211   : public __char_traits_base<_CharT, _CharT> {};
212
213 // Specialization for char.
214
215 _STLP_TEMPLATE_NULL
216 class _STLP_CLASS_DECLSPEC char_traits<char>
217   : public __char_traits_base<char, int> {
218 public:
219   typedef char char_type;
220   typedef int int_type;
221   typedef streamoff off_type;
222 #if !defined (_STLP_NO_MBSTATE_T)
223   typedef streampos pos_type;
224   typedef mbstate_t state_type;
225 #endif
226
227   static char _STLP_CALL to_char_type(const int& __c)
228   { return (char)(unsigned char)__c; }
229
230   static int _STLP_CALL to_int_type(const char& __c)
231   { return (unsigned char)__c; }
232
233   static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n)
234   { return memcmp(__s1, __s2, __n); }
235
236   static size_t _STLP_CALL length(const char* __s)
237   { return strlen(__s); }
238
239   static void _STLP_CALL assign(char& __c1, const char& __c2)
240   { __c1 = __c2; }
241
242   static char* _STLP_CALL assign(char* __s, size_t __n, char __c) {
243     memset(__s, __c, __n);
244     return __s;
245   }
246 };
247
248 #if defined (_STLP_HAS_WCHAR_T)
249 // Specialization for wchar_t.
250 _STLP_TEMPLATE_NULL
251 class _STLP_CLASS_DECLSPEC char_traits<wchar_t>
252   : public __char_traits_base<wchar_t, wint_t> {
253 #  if !defined (_STLP_NO_NATIVE_WIDE_FUNCTIONS) && !defined (_STLP_WCHAR_HPACC_EXCLUDE)
254 public:
255 #    if !defined (N_PLAT_NLM)
256 #      if !defined (__BORLANDC__)
257   static wchar_t* _STLP_CALL move(wchar_t* __dest, const wchar_t* __src, size_t __n)
258   { return wmemmove(__dest, __src, __n); }
259 #      endif
260
261   static wchar_t* _STLP_CALL copy(wchar_t* __dest, const wchar_t* __src, size_t __n)
262   { return wmemcpy(__dest, __src, __n); }
263
264 #      if !defined (__DMC__) && !defined (__BORLANDC__)
265   static int _STLP_CALL compare(const wchar_t* __s1, const wchar_t* __s2, size_t __n)
266   { return wmemcmp(__s1, __s2, __n); }
267 #      endif
268
269   static wchar_t* _STLP_CALL assign(wchar_t* __s, size_t __n, wchar_t __c)
270   { return wmemset(__s, __c, __n); }
271 #    endif
272
273   static size_t _STLP_CALL length(const wchar_t* __s)
274   { return wcslen(__s); }
275
276   static void _STLP_CALL assign(wchar_t& __c1, const wchar_t& __c2)
277   { __c1 = __c2; }
278 #  endif
279 };
280 #endif
281
282 _STLP_END_NAMESPACE
283
284 #endif /* _STLP_CHAR_TRAITS_H */
285
286 // Local Variables:
287 // mode:C++
288 // End: