Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

char_traits.h

Go to the documentation of this file.
00001 // Character Traits for use by standard string and iostream -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 21  Strings library
00032 //
00033 
00034 #ifndef _CPP_BITS_CHAR_TRAITS_H
00035 #define _CPP_BITS_CHAR_TRAITS_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/std_cstring.h>   // For memmove, memset, memchr
00040 #include <bits/fpos.h>      // For streampos
00041 
00042 namespace std 
00043 {
00047   template<class _CharT>
00048     struct char_traits
00049     {
00050       typedef _CharT        char_type;
00051       typedef unsigned long     int_type;   // Unsigned as wint_t is unsigned.
00052       typedef streampos     pos_type;
00053       typedef streamoff     off_type;
00054       typedef mbstate_t     state_type;
00055       
00057       static void 
00058       assign(char_type& __c1, const char_type& __c2)
00059       { __c1 = __c2; }
00060 
00062       static bool 
00063       eq(const char_type& __c1, const char_type& __c2)
00064       { return __c1 == __c2; }
00065 
00067       static bool 
00068       lt(const char_type& __c1, const char_type& __c2)
00069       { return __c1 < __c2; }
00070 
00072       static int 
00073       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00074       { 
00075     for (size_t __i = 0; __i < __n; ++__i)
00076       if (!eq(__s1[__i], __s2[__i]))
00077         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00078     return 0; 
00079       }
00080 
00082       static size_t
00083       length(const char_type* __s)
00084       { 
00085     const char_type* __p = __s; 
00086     while (*__p) ++__p; 
00087     return (__p - __s); 
00088       }
00089 
00091       static const char_type* 
00092       find(const char_type* __s, size_t __n, const char_type& __a)
00093       { 
00094     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00095       if (*__p == __a) return __p;
00096     return 0;
00097       }
00098 
00100       static char_type* 
00101       move(char_type* __s1, const char_type* __s2, size_t __n)
00102       { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
00103 
00105       static char_type* 
00106       copy(char_type* __s1, const char_type* __s2, size_t __n)
00107       { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
00108 
00110       static char_type* 
00111       assign(char_type* __s, size_t __n, char_type __a)
00112       { 
00113     for (char_type* __p = __s; __p < __s + __n; ++__p) 
00114       assign(*__p, __a);
00115         return __s; 
00116       }
00117 
00119       static char_type 
00120       to_char_type(const int_type& __c)
00121       { return char_type(__c); }
00122 
00124       static int_type 
00125       to_int_type(const char_type& __c) { return int_type(__c); }
00126 
00128       static bool 
00129       eq_int_type(const int_type& __c1, const int_type& __c2)
00130       { return __c1 == __c2; }
00131 
00133       static int_type 
00134       eof() { return static_cast<int_type>(-1); }
00135 
00137       static int_type 
00138       not_eof(const int_type& __c)
00139       { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
00140     };
00141 
00142 
00145   template<>
00146     struct char_traits<char>
00147     {
00148       typedef char      char_type;
00149       typedef int           int_type;
00150       typedef streampos     pos_type;
00151       typedef streamoff     off_type;
00152       typedef mbstate_t     state_type;
00153 
00155       static void 
00156       assign(char_type& __c1, const char_type& __c2)
00157       { __c1 = __c2; }
00158 
00160       static bool 
00161       eq(const char_type& __c1, const char_type& __c2)
00162       { return __c1 == __c2; }
00163 
00165       static bool 
00166       lt(const char_type& __c1, const char_type& __c2)
00167       { return __c1 < __c2; }
00168 
00170       static int 
00171       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00172       { return memcmp(__s1, __s2, __n); }
00173 
00175       static size_t
00176       length(const char_type* __s)
00177       { return strlen(__s); }
00178 
00180       static const char_type* 
00181       find(const char_type* __s, size_t __n, const char_type& __a)
00182       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
00183 
00185       static char_type* 
00186       move(char_type* __s1, const char_type* __s2, size_t __n)
00187       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
00188 
00190       static char_type* 
00191       copy(char_type* __s1, const char_type* __s2, size_t __n)
00192       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
00193 
00195       static char_type* 
00196       assign(char_type* __s, size_t __n, char_type __a)
00197       { return static_cast<char_type*>(memset(__s, __a, __n)); }
00198 
00200       static char_type 
00201       to_char_type(const int_type& __c)
00202       { return static_cast<char_type>(__c); }
00203 
00206       static int_type 
00207       to_int_type(const char_type& __c)
00208       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00209 
00211       static bool 
00212       eq_int_type(const int_type& __c1, const int_type& __c2)
00213       { return __c1 == __c2; }
00214 
00216       static int_type 
00217       eof() { return static_cast<int_type>(EOF); }
00218 
00220       static int_type 
00221       not_eof(const int_type& __c)
00222       { return (__c == eof()) ? 0 : __c; }
00223   };
00224 
00225 
00226 #ifdef _GLIBCPP_USE_WCHAR_T
00229   template<>
00230     struct char_traits<wchar_t>
00231     {
00232       typedef wchar_t       char_type;
00233       typedef wint_t        int_type;
00234       typedef streamoff     off_type;
00235       typedef wstreampos    pos_type;
00236       typedef mbstate_t     state_type;
00237       
00239       static void 
00240       assign(char_type& __c1, const char_type& __c2)
00241       { __c1 = __c2; }
00242 
00244       static bool 
00245       eq(const char_type& __c1, const char_type& __c2)
00246       { return __c1 == __c2; }
00247 
00249       static bool 
00250       lt(const char_type& __c1, const char_type& __c2)
00251       { return __c1 < __c2; }
00252 
00254       static int 
00255       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00256       { return wmemcmp(__s1, __s2, __n); }
00257 
00259       static size_t
00260       length(const char_type* __s)
00261       { return wcslen(__s); }
00262 
00264       static const char_type* 
00265       find(const char_type* __s, size_t __n, const char_type& __a)
00266       { return wmemchr(__s, __a, __n); }
00267 
00269       static char_type* 
00270       move(char_type* __s1, const char_type* __s2, int_type __n)
00271       { return wmemmove(__s1, __s2, __n); }
00272 
00274       static char_type* 
00275       copy(char_type* __s1, const char_type* __s2, size_t __n)
00276       { return wmemcpy(__s1, __s2, __n); }
00277 
00279       static char_type* 
00280       assign(char_type* __s, size_t __n, char_type __a)
00281       { return wmemset(__s, __a, __n); }
00282 
00284       static char_type 
00285       to_char_type(const int_type& __c) { return char_type(__c); }
00286 
00288       static int_type 
00289       to_int_type(const char_type& __c) { return int_type(__c); }
00290 
00292       static bool 
00293       eq_int_type(const int_type& __c1, const int_type& __c2)
00294       { return __c1 == __c2; }
00295 
00297       static int_type 
00298       eof() { return static_cast<int_type>(WEOF); }
00299 
00301       static int_type 
00302       not_eof(const int_type& __c)
00303       { return eq_int_type(__c, eof()) ? 0 : __c; }
00304   };
00305 #endif //_GLIBCPP_USE_WCHAR_T
00306 
00308   template<typename _CharT, typename _Traits>
00309     struct _Char_traits_match
00310     {
00311       _CharT _M_c;
00312       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
00313 
00314       bool 
00315       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
00316     };
00317 
00318 } // namespace std
00319 
00320 #endif

Generated at Tue May 1 16:28:37 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001