char_traits.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifndef _STLP_CHAR_TRAITS_H
  19. #define _STLP_CHAR_TRAITS_H
  20. // Define char_traits
  21. #ifndef _STLP_INTERNAL_CSTDDEF
  22. # include <stl/_cstddef.h>
  23. #endif
  24. #ifndef _STLP_INTERNAL_CSTRING
  25. # include <stl/_cstring.h>
  26. #endif
  27. #if defined (__unix)
  28. # include <sys/types.h> // For off_t
  29. #endif /* __unix */
  30. #if defined (__BORLANDC__)
  31. # include <mem.h>
  32. # include <string.h>
  33. # include <_stddef.h>
  34. # include <sys/types.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  37. # include <stl/_construct.h>
  38. #endif
  39. #ifndef _STLP_INTERNAL_CWCHAR
  40. # include <stl/_cwchar.h>
  41. #endif
  42. _STLP_BEGIN_NAMESPACE
  43. template <class _Tp> class allocator;
  44. #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT)
  45. #if defined(_STLP_WCE)
  46. typedef long streamoff;
  47. #elif defined (_STLP_WIN32)
  48. # if defined (_STLP_LONG_LONG) && !defined (__CYGWIN__)
  49. //The Win32 file io API support 64 bits access so streamoff and streamsize
  50. //has to reflect that. Do not change the stringbuf behavior.
  51. typedef _STLP_LONG_LONG streamoff;
  52. # else
  53. typedef ptrdiff_t streamoff;
  54. # endif
  55. #else // __unix
  56. # ifdef _STLP_USE_DEFAULT_FILE_OFFSET
  57. typedef off_t streamoff;
  58. # elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
  59. /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) */
  60. typedef off64_t streamoff;
  61. # else
  62. typedef off_t streamoff;
  63. # endif
  64. #endif /* __unix */
  65. #if defined (_STLP_WIN32)
  66. typedef streamoff streamsize;
  67. #else
  68. typedef ptrdiff_t streamsize;
  69. #endif
  70. // Class fpos, which represents a position within a file. (The C++
  71. // standard calls for it to be defined in <ios>. This implementation
  72. // moves it to <iosfwd>, which is included by <ios>.)
  73. template <class _StateT> class fpos {
  74. public: // From table 88 of the C++ standard.
  75. fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
  76. fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
  77. operator streamoff() const { return _M_pos; }
  78. bool operator==(const fpos& __y) const
  79. { return _M_pos == __y._M_pos; }
  80. bool operator!=(const fpos& __y) const
  81. { return _M_pos != __y._M_pos; }
  82. fpos& operator+=(streamoff __off) {
  83. _M_pos += __off;
  84. return *this;
  85. }
  86. fpos& operator-=(streamoff __off) {
  87. _M_pos -= __off;
  88. return *this;
  89. }
  90. fpos operator+(streamoff __off) {
  91. fpos __tmp(*this);
  92. __tmp += __off;
  93. return __tmp;
  94. }
  95. fpos operator-(streamoff __off) {
  96. fpos __tmp(*this);
  97. __tmp -= __off;
  98. return __tmp;
  99. }
  100. public: // Manipulation of the state member.
  101. _StateT state() const { return _M_st; }
  102. void state(_StateT __st) { _M_st = __st; }
  103. private:
  104. streamoff _M_pos;
  105. _StateT _M_st;
  106. };
  107. typedef fpos<mbstate_t> streampos;
  108. typedef fpos<mbstate_t> wstreampos;
  109. // Class __char_traits_base.
  110. template <class _CharT, class _IntT>
  111. class __char_traits_base {
  112. public:
  113. typedef _CharT char_type;
  114. typedef _IntT int_type;
  115. typedef streamoff off_type;
  116. typedef streampos pos_type;
  117. typedef mbstate_t state_type;
  118. static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
  119. static bool _STLP_CALL eq(const char_type& __c1, const char_type& __c2)
  120. { return __c1 == __c2; }
  121. static bool _STLP_CALL lt(const char_type& __c1, const char_type& __c2)
  122. { return __c1 < __c2; }
  123. static int _STLP_CALL compare(const char_type* __s1, const char_type* __s2, size_t __n) {
  124. for (size_t __i = 0; __i < __n; ++__i)
  125. if (!eq(__s1[__i], __s2[__i]))
  126. return __s1[__i] < __s2[__i] ? -1 : 1;
  127. return 0;
  128. }
  129. static size_t _STLP_CALL length(const char_type* __s) {
  130. const char_type _NullChar = _STLP_DEFAULT_CONSTRUCTED(char_type);
  131. size_t __i(0);
  132. for (; !eq(__s[__i], _NullChar); ++__i) {}
  133. return __i;
  134. }
  135. static const char_type* _STLP_CALL find(const char_type* __s, size_t __n, const char_type& __c) {
  136. for ( ; __n > 0 ; ++__s, --__n)
  137. if (eq(*__s, __c))
  138. return __s;
  139. return 0;
  140. }
  141. static char_type* _STLP_CALL move(char_type* __s1, const char_type* __s2, size_t _Sz)
  142. { return (_Sz == 0 ? __s1 : (char_type*)memmove(__s1, __s2, _Sz * sizeof(char_type))); }
  143. static char_type* _STLP_CALL copy(char_type* __s1, const char_type* __s2, size_t __n) {
  144. return (__n == 0 ? __s1 :
  145. (char_type*)memcpy(__s1, __s2, __n * sizeof(char_type)));
  146. }
  147. static char_type* _STLP_CALL assign(char_type* __s, size_t __n, char_type __c) {
  148. for (size_t __i = 0; __i < __n; ++__i)
  149. __s[__i] = __c;
  150. return __s;
  151. }
  152. static int_type _STLP_CALL not_eof(const int_type& __c)
  153. { return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0); }
  154. static char_type _STLP_CALL to_char_type(const int_type& __c)
  155. { return (char_type)__c; }
  156. static int_type _STLP_CALL to_int_type(const char_type& __c)
  157. { return (int_type)__c; }
  158. static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2)
  159. { return __c1 == __c2; }
  160. static int_type _STLP_CALL eof()
  161. { return (int_type)-1; }
  162. };
  163. // Generic char_traits class. Note that this class is provided only
  164. // as a base for explicit specialization; it is unlikely to be useful
  165. // as is for any particular user-defined type. In particular, it
  166. // *will not work* for a non-POD type.
  167. template <class _CharT>
  168. class char_traits
  169. : public __char_traits_base<_CharT, _CharT> {};
  170. // Specialization for char.
  171. _STLP_TEMPLATE_NULL
  172. class _STLP_CLASS_DECLSPEC char_traits<char>
  173. : public __char_traits_base<char, int>,
  174. public __stlport_class<char_traits<char> > {
  175. public:
  176. typedef char char_type;
  177. typedef int int_type;
  178. typedef streamoff off_type;
  179. typedef streampos pos_type;
  180. typedef mbstate_t state_type;
  181. static char _STLP_CALL to_char_type(const int& __c)
  182. { return (char)(unsigned char)__c; }
  183. static int _STLP_CALL to_int_type(const char& __c)
  184. { return (unsigned char)__c; }
  185. static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n)
  186. { return memcmp(__s1, __s2, __n); }
  187. static size_t _STLP_CALL length(const char* __s)
  188. { return strlen(__s); }
  189. static void _STLP_CALL assign(char& __c1, const char& __c2)
  190. { __c1 = __c2; }
  191. static char* _STLP_CALL assign(char* __s, size_t __n, char __c) {
  192. memset(__s, __c, __n);
  193. return __s;
  194. }
  195. };
  196. #if defined (_STLP_HAS_WCHAR_T)
  197. // Specialization for wchar_t.
  198. _STLP_TEMPLATE_NULL
  199. class _STLP_CLASS_DECLSPEC char_traits<wchar_t>
  200. : public __char_traits_base<wchar_t, wint_t> {
  201. # if !defined (_STLP_NO_NATIVE_WIDE_FUNCTIONS) && !defined (_STLP_WCHAR_HPACC_EXCLUDE)
  202. public:
  203. # if !defined (__BORLANDC__)
  204. static wchar_t* _STLP_CALL move(wchar_t* __dest, const wchar_t* __src, size_t __n)
  205. { return wmemmove(__dest, __src, __n); }
  206. # endif
  207. static wchar_t* _STLP_CALL copy(wchar_t* __dest, const wchar_t* __src, size_t __n)
  208. { return wmemcpy(__dest, __src, __n); }
  209. # if !defined (__DMC__) && !defined (__BORLANDC__)
  210. static int _STLP_CALL compare(const wchar_t* __s1, const wchar_t* __s2, size_t __n)
  211. { return wmemcmp(__s1, __s2, __n); }
  212. # endif
  213. static wchar_t* _STLP_CALL assign(wchar_t* __s, size_t __n, wchar_t __c)
  214. { return wmemset(__s, __c, __n); }
  215. static size_t _STLP_CALL length(const wchar_t* __s)
  216. { return wcslen(__s); }
  217. static void _STLP_CALL assign(wchar_t& __c1, const wchar_t& __c2)
  218. { __c1 = __c2; }
  219. # endif
  220. };
  221. #endif
  222. _STLP_END_NAMESPACE
  223. #endif /* _STLP_CHAR_TRAITS_H */
  224. // Local Variables:
  225. // mode:C++
  226. // End: