williamr@2: /* williamr@2: * Copyright (c) 1996,1997 williamr@2: * Silicon Graphics Computer Systems, Inc. williamr@2: * williamr@2: * Copyright (c) 1999 williamr@2: * Boris Fomitchev williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@2: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@2: */ williamr@2: williamr@2: #ifndef _STLP_CHAR_TRAITS_H williamr@2: #define _STLP_CHAR_TRAITS_H williamr@2: williamr@2: // Define char_traits williamr@2: williamr@2: # if defined (_STLP_OWN_IOSTREAMS) || ! defined (_STLP_USE_NEW_IOSTREAMS) williamr@2: williamr@2: # if ! defined (_STLP_CSTDDEF) williamr@2: # include williamr@2: # endif williamr@2: williamr@2: #if ! defined (_STLP_CSTRING) williamr@2: # include williamr@2: #endif williamr@2: williamr@2: #if defined (_STLP_UNIX) && defined (_STLP_HAS_NO_NEW_C_HEADERS) williamr@2: #include // For off_t williamr@2: #endif /* __unix */ williamr@2: williamr@2: #ifdef __BORLANDC__ williamr@2: # include williamr@2: # include williamr@2: # include <_stddef.h> williamr@2: // class mbstate_t; williamr@2: #endif williamr@2: williamr@2: #ifndef __TYPE_TRAITS_H williamr@2: # include williamr@2: #endif williamr@2: williamr@2: # if !defined (_STLP_CWCHAR) williamr@2: # include williamr@2: # endif williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: # ifdef _STLP_OWN_IOSTREAMS williamr@2: williamr@2: template class allocator; williamr@2: williamr@2: #define _STLP_NULL_CHAR_INIT(_ChT) _STLP_DEFAULT_CONSTRUCTED(_ChT) williamr@2: williamr@2: #if defined (__sgi) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* IRIX */ williamr@2: typedef off64_t streamoff; williamr@2: // #elif defined (__unix) && defined (_STLP_HAS_NO_NEW_C_HEADERS) /* Other version of UNIX */ williamr@2: // typedef off_t streamoff; williamr@2: #else /* __unix */ williamr@2: // boris : here, it's not ptrdiff_t as some Solaris systems have confusing definitions of these. williamr@2: typedef long streamoff; williamr@2: #endif /* _STLP_HAS_NO_NEW_C_HEADERS */ williamr@2: williamr@2: typedef ptrdiff_t streamsize; williamr@2: williamr@2: // Class fpos, which represents a position within a file. (The C++ williamr@2: // standard calls for it to be defined in . This implementation williamr@2: // moves it to , which is included by .) williamr@2: template class fpos williamr@2: { williamr@2: public: // From table 88 of the C++ standard. williamr@2: fpos(streamoff __pos) : _M_pos(__pos), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {} williamr@2: fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {} williamr@2: williamr@2: operator streamoff() const { return _M_pos; } williamr@2: williamr@2: bool _STLP_CALL operator==(const fpos<_StateT>& __y) const williamr@2: { return _M_pos == __y._M_pos; } williamr@2: bool _STLP_CALL operator!=(const fpos<_StateT>& __y) const williamr@2: { return _M_pos != __y._M_pos; } williamr@2: williamr@2: fpos<_StateT>& operator+=(streamoff __off) { williamr@2: _M_pos += __off; williamr@2: return *this; williamr@2: } williamr@2: fpos<_StateT>& operator-=(streamoff __off) { williamr@2: _M_pos -= __off; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: fpos<_StateT> operator+(streamoff __off) { williamr@2: fpos<_StateT> __tmp(*this); williamr@2: __tmp += __off; williamr@2: return __tmp; williamr@2: } williamr@2: fpos<_StateT> operator-(streamoff __off) { williamr@2: fpos<_StateT> __tmp(*this); williamr@2: __tmp -= __off; williamr@2: return __tmp; williamr@2: } williamr@2: williamr@2: public: // Manipulation of the state member. williamr@2: _StateT state() const { return _M_st; } williamr@2: void state(_StateT __st) { _M_st = __st; } williamr@2: private: williamr@2: streamoff _M_pos; williamr@2: _StateT _M_st; williamr@2: }; williamr@2: williamr@2: typedef fpos streampos; williamr@2: typedef fpos wstreampos; williamr@2: # endif williamr@2: williamr@2: // Class __char_traits_base. williamr@2: williamr@2: template class __char_traits_base { williamr@2: public: williamr@2: typedef _CharT char_type; williamr@2: typedef _IntT int_type; williamr@2: #ifdef _STLP_USE_NEW_IOSTREAMS williamr@2: typedef streamoff off_type; williamr@2: typedef streampos pos_type; williamr@2: # ifdef _STLP_NO_MBSTATE_T williamr@2: typedef char state_type; williamr@2: # else williamr@2: typedef mbstate_t state_type; williamr@2: # endif williamr@2: #endif /* _STLP_USE_NEW_IOSTREAMS */ williamr@2: williamr@2: static void _STLP_CALL assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; } williamr@2: static bool _STLP_CALL eq(const _CharT& __c1, const _CharT& __c2) williamr@2: { return __c1 == __c2; } williamr@2: static bool _STLP_CALL lt(const _CharT& __c1, const _CharT& __c2) williamr@2: { return __c1 < __c2; } williamr@2: williamr@2: static int _STLP_CALL compare(const _CharT* __s1, const _CharT* __s2, size_t __n) { williamr@2: for (size_t __i = 0; __i < __n; ++__i) williamr@2: if (!eq(__s1[__i], __s2[__i])) williamr@2: return __s1[__i] < __s2[__i] ? -1 : 1; williamr@2: return 0; williamr@2: } williamr@2: williamr@2: static size_t _STLP_CALL length(const _CharT* __s) { williamr@2: const _CharT _NullChar = _STLP_DEFAULT_CONSTRUCTED(_CharT); williamr@2: size_t __i; williamr@2: for (__i = 0; !eq(__s[__i], _NullChar); ++__i) williamr@2: {} williamr@2: return __i; williamr@2: } williamr@2: williamr@2: static const _CharT* _STLP_CALL find(const _CharT* __s, size_t __n, const _CharT& __c) { williamr@2: for ( ; __n > 0 ; ++__s, --__n) williamr@2: if (eq(*__s, __c)) williamr@2: return __s; williamr@2: return 0; williamr@2: } williamr@2: williamr@2: williamr@2: static _CharT* _STLP_CALL move(_CharT* __s1, const _CharT* __s2, size_t _Sz) { williamr@2: return (_Sz == 0 ? __s1 : (_CharT*)memmove(__s1, __s2, _Sz * sizeof(_CharT))); williamr@2: } williamr@2: williamr@2: static _CharT* _STLP_CALL copy(_CharT* __s1, const _CharT* __s2, size_t __n) { williamr@2: return (__n == 0 ? __s1 : williamr@2: (_CharT*)memcpy(__s1, __s2, __n * sizeof(_CharT))); williamr@2: } williamr@2: williamr@2: static _CharT* _STLP_CALL assign(_CharT* __s, size_t __n, _CharT __c) { williamr@2: for (size_t __i = 0; __i < __n; ++__i) williamr@2: __s[__i] = __c; williamr@2: return __s; williamr@2: } williamr@2: williamr@2: static int_type _STLP_CALL not_eof(const int_type& __c) { williamr@2: return !eq_int_type(__c, eof()) ? __c : __STATIC_CAST(int_type, 0); williamr@2: } williamr@2: williamr@2: static char_type _STLP_CALL to_char_type(const int_type& __c) { williamr@2: return (char_type)__c; williamr@2: } williamr@2: williamr@2: static int_type _STLP_CALL to_int_type(const char_type& __c) { williamr@2: return (int_type)__c; williamr@2: } williamr@2: williamr@2: static bool _STLP_CALL eq_int_type(const int_type& __c1, const int_type& __c2) { williamr@2: return __c1 == __c2; williamr@2: } williamr@2: williamr@2: static int_type _STLP_CALL eof() { williamr@2: return (int_type)-1; williamr@2: // return __STATIC_CAST(int_type,-1); williamr@2: } williamr@2: }; williamr@2: williamr@2: // Generic char_traits class. Note that this class is provided only williamr@2: // as a base for explicit specialization; it is unlikely to be useful williamr@2: // as is for any particular user-defined type. In particular, it williamr@2: // *will not work* for a non-POD type. williamr@2: williamr@2: template class char_traits williamr@2: : public __char_traits_base<_CharT, _CharT> williamr@2: {}; williamr@2: williamr@2: // Specialization for char. williamr@2: williamr@2: _STLP_TEMPLATE_NULL class _STLP_CLASS_DECLSPEC char_traits williamr@2: : public __char_traits_base williamr@2: { williamr@2: public: williamr@2: typedef char char_type; williamr@2: typedef int int_type; williamr@2: #ifdef _STLP_USE_NEW_IOSTREAMS williamr@2: typedef streamoff off_type; williamr@2: # ifndef _STLP_NO_MBSTATE_T williamr@2: typedef streampos pos_type; williamr@2: typedef mbstate_t state_type; williamr@2: # endif williamr@2: #endif /* _STLP_USE_NEW_IOSTREAMS */ williamr@2: williamr@2: static char _STLP_CALL to_char_type(const int& __c) { williamr@2: return (char)(unsigned char)__c; williamr@2: } williamr@2: williamr@2: static int _STLP_CALL to_int_type(const char& __c) { williamr@2: return (unsigned char)__c; williamr@2: } williamr@2: williamr@2: static int _STLP_CALL compare(const char* __s1, const char* __s2, size_t __n) williamr@2: { return memcmp(__s1, __s2, __n); } williamr@2: williamr@2: static size_t _STLP_CALL length(const char* __s) { return strlen(__s); } williamr@2: williamr@2: static void _STLP_CALL assign(char& __c1, const char& __c2) { __c1 = __c2; } williamr@2: williamr@2: static char* _STLP_CALL assign(char* __s, size_t __n, char __c) williamr@2: { memset(__s, __c, __n); return __s; } williamr@2: }; williamr@2: williamr@2: # if defined (_STLP_HAS_WCHAR_T) williamr@2: // Specialization for wchar_t. williamr@2: _STLP_TEMPLATE_NULL class _STLP_CLASS_DECLSPEC char_traits williamr@2: : public __char_traits_base williamr@2: {}; williamr@2: # endif williamr@2: williamr@2: _STLP_END_NAMESPACE williamr@2: williamr@2: # else /* OWN_IOSTREAMS */ williamr@2: williamr@2: # include williamr@2: williamr@2: # endif /* OWN_IOSTREAMS */ williamr@2: williamr@2: #endif /* _STLP_CHAR_TRAITS_H */ williamr@2: williamr@2: // Local Variables: williamr@2: // mode:C++ williamr@2: // End: williamr@2: