Update contrib.
2 * Copyright (c) 1996,1997
3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
19 #ifndef _STLP_CHAR_TRAITS_H
20 #define _STLP_CHAR_TRAITS_H
24 #ifndef _STLP_INTERNAL_CSTDDEF
25 # include <stl/_cstddef.h>
28 #ifndef _STLP_INTERNAL_CSTRING
29 # include <stl/_cstring.h>
32 #if defined (__unix) || defined (N_PLAT_NLM)
33 # include <sys/types.h> // For off_t
37 # include _STLP_NATIVE_C_HEADER(mem.h)
38 # include _STLP_NATIVE_C_HEADER(string.h)
39 # include _STLP_NATIVE_C_HEADER(_stddef.h)
42 #ifndef _STLP_INTERNAL_CONSTRUCT_H
43 # include <stl/_construct.h>
46 #ifndef _STLP_INTERNAL_CWCHAR
47 # include <stl/_cwchar.h>
52 template <class _Tp> class allocator;
54 #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT)
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;
66 typedef ptrdiff_t streamoff;
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;
75 typedef off_t streamoff;
79 #if defined (_STLP_WIN32)
80 typedef streamoff streamsize;
82 typedef ptrdiff_t streamsize;
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)) {}
93 operator streamoff() const { return _M_pos; }
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; }
100 fpos& operator+=(streamoff __off) {
104 fpos& operator-=(streamoff __off) {
109 fpos operator+(streamoff __off) {
114 fpos operator-(streamoff __off) {
120 public: // Manipulation of the state member.
121 _StateT state() const { return _M_st; }
122 void state(_StateT __st) { _M_st = __st; }
128 #if !defined (_STLP_NO_MBSTATE_T)
129 typedef fpos<mbstate_t> streampos;
130 typedef fpos<mbstate_t> wstreampos;
133 // Class __char_traits_base.
134 template <class _CharT, class _IntT>
135 class __char_traits_base {
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;
144 typedef mbstate_t state_type;
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; }
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;
160 static size_t _STLP_CALL length(const char_type* __s) {
161 const char_type _NullChar = _STLP_DEFAULT_CONSTRUCTED(char_type);
163 for (; !eq(__s[__i], _NullChar); ++__i) {}
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)
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))); }
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)));
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)
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); }
191 static char_type _STLP_CALL to_char_type(const int_type& __c)
192 { return (char_type)__c; }
194 static int_type _STLP_CALL to_int_type(const char_type& __c)
195 { return (int_type)__c; }
197 static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2)
198 { return __c1 == __c2; }
200 static int_type _STLP_CALL eof()
201 { return (int_type)-1; }
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.
209 template <class _CharT>
211 : public __char_traits_base<_CharT, _CharT> {};
213 // Specialization for char.
216 class _STLP_CLASS_DECLSPEC char_traits<char>
217 : public __char_traits_base<char, int> {
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;
227 static char _STLP_CALL to_char_type(const int& __c)
228 { return (char)(unsigned char)__c; }
230 static int _STLP_CALL to_int_type(const char& __c)
231 { return (unsigned char)__c; }
233 static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n)
234 { return memcmp(__s1, __s2, __n); }
236 static size_t _STLP_CALL length(const char* __s)
237 { return strlen(__s); }
239 static void _STLP_CALL assign(char& __c1, const char& __c2)
242 static char* _STLP_CALL assign(char* __s, size_t __n, char __c) {
243 memset(__s, __c, __n);
248 #if defined (_STLP_HAS_WCHAR_T)
249 // Specialization for wchar_t.
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)
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); }
261 static wchar_t* _STLP_CALL copy(wchar_t* __dest, const wchar_t* __src, size_t __n)
262 { return wmemcpy(__dest, __src, __n); }
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); }
269 static wchar_t* _STLP_CALL assign(wchar_t* __s, size_t __n, wchar_t __c)
270 { return wmemset(__s, __c, __n); }
273 static size_t _STLP_CALL length(const wchar_t* __s)
274 { return wcslen(__s); }
276 static void _STLP_CALL assign(wchar_t& __c1, const wchar_t& __c2)
284 #endif /* _STLP_CHAR_TRAITS_H */