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, 2002
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 21  Strings library
00033 //
00034 
00040 #ifndef _CPP_BITS_CHAR_TRAITS_H
00041 #define _CPP_BITS_CHAR_TRAITS_H 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <cstring>  // For memmove, memset, memchr
00046 #include <bits/fpos.h>      // For streampos
00047 
00048 namespace std 
00049 {
00053   template<class _CharT>
00054     struct char_traits
00055     {
00056       typedef _CharT        char_type;
00057       // Unsigned as wint_t is unsigned.
00058       typedef unsigned long     int_type;
00059       typedef streampos     pos_type;
00060       typedef streamoff     off_type;
00061       typedef mbstate_t     state_type;
00062       
00063       static void 
00064       assign(char_type& __c1, const char_type& __c2);
00065 
00066       static bool 
00067       eq(const char_type& __c1, const char_type& __c2);
00068 
00069       static bool 
00070       lt(const char_type& __c1, const char_type& __c2);
00071 
00072       static int 
00073       compare(const char_type* __s1, const char_type* __s2, size_t __n);
00074 
00075       static size_t
00076       length(const char_type* __s);
00077 
00078       static const char_type* 
00079       find(const char_type* __s, size_t __n, const char_type& __a);
00080 
00081       static char_type* 
00082       move(char_type* __s1, const char_type* __s2, size_t __n);
00083 
00084       static char_type* 
00085       copy(char_type* __s1, const char_type* __s2, size_t __n);
00086 
00087       static char_type* 
00088       assign(char_type* __s, size_t __n, char_type __a);
00089 
00090       static char_type 
00091       to_char_type(const int_type& __c);
00092 
00093       static int_type 
00094       to_int_type(const char_type& __c);
00095 
00096       static bool 
00097       eq_int_type(const int_type& __c1, const int_type& __c2);
00098 
00099       static int_type 
00100       eof(); 
00101 
00102       static int_type 
00103       not_eof(const int_type& __c);
00104     };
00105 
00106 
00108   template<>
00109     struct char_traits<char>
00110     {
00111       typedef char      char_type;
00112       typedef int           int_type;
00113       typedef streampos     pos_type;
00114       typedef streamoff     off_type;
00115       typedef mbstate_t     state_type;
00116 
00117       static void 
00118       assign(char_type& __c1, const char_type& __c2)
00119       { __c1 = __c2; }
00120 
00121       static bool 
00122       eq(const char_type& __c1, const char_type& __c2)
00123       { return __c1 == __c2; }
00124 
00125       static bool 
00126       lt(const char_type& __c1, const char_type& __c2)
00127       { return __c1 < __c2; }
00128 
00129       static int 
00130       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00131       { return memcmp(__s1, __s2, __n); }
00132 
00133       static size_t
00134       length(const char_type* __s)
00135       { return strlen(__s); }
00136 
00137       static const char_type* 
00138       find(const char_type* __s, size_t __n, const char_type& __a)
00139       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
00140 
00141       static char_type* 
00142       move(char_type* __s1, const char_type* __s2, size_t __n)
00143       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
00144 
00145       static char_type* 
00146       copy(char_type* __s1, const char_type* __s2, size_t __n)
00147       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
00148 
00149       static char_type* 
00150       assign(char_type* __s, size_t __n, char_type __a)
00151       { return static_cast<char_type*>(memset(__s, __a, __n)); }
00152 
00153       static char_type 
00154       to_char_type(const int_type& __c)
00155       { return static_cast<char_type>(__c); }
00156 
00157       // To keep both the byte 0xff and the eof symbol 0xffffffff
00158       // from ending up as 0xffffffff.
00159       static int_type 
00160       to_int_type(const char_type& __c)
00161       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00162 
00163       static bool 
00164       eq_int_type(const int_type& __c1, const int_type& __c2)
00165       { return __c1 == __c2; }
00166 
00167       static int_type 
00168       eof() { return static_cast<int_type>(EOF); }
00169 
00170       static int_type 
00171       not_eof(const int_type& __c)
00172       { return (__c == eof()) ? 0 : __c; }
00173   };
00174 
00175 
00176 #ifdef _GLIBCPP_USE_WCHAR_T
00177   template<>
00178     struct char_traits<wchar_t>
00179     {
00180       typedef wchar_t       char_type;
00181       typedef wint_t        int_type;
00182       typedef streamoff     off_type;
00183       typedef wstreampos    pos_type;
00184       typedef mbstate_t     state_type;
00185       
00186       static void 
00187       assign(char_type& __c1, const char_type& __c2)
00188       { __c1 = __c2; }
00189 
00190       static bool 
00191       eq(const char_type& __c1, const char_type& __c2)
00192       { return __c1 == __c2; }
00193 
00194       static bool 
00195       lt(const char_type& __c1, const char_type& __c2)
00196       { return __c1 < __c2; }
00197 
00198       static int 
00199       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00200       { return wmemcmp(__s1, __s2, __n); }
00201 
00202       static size_t
00203       length(const char_type* __s)
00204       { return wcslen(__s); }
00205 
00206       static const char_type* 
00207       find(const char_type* __s, size_t __n, const char_type& __a)
00208       { return wmemchr(__s, __a, __n); }
00209 
00210       static char_type* 
00211       move(char_type* __s1, const char_type* __s2, int_type __n)
00212       { return wmemmove(__s1, __s2, __n); }
00213 
00214       static char_type* 
00215       copy(char_type* __s1, const char_type* __s2, size_t __n)
00216       { return wmemcpy(__s1, __s2, __n); }
00217 
00218       static char_type* 
00219       assign(char_type* __s, size_t __n, char_type __a)
00220       { return wmemset(__s, __a, __n); }
00221 
00222       static char_type 
00223       to_char_type(const int_type& __c) { return char_type(__c); }
00224 
00225       static int_type 
00226       to_int_type(const char_type& __c) { return int_type(__c); }
00227 
00228       static bool 
00229       eq_int_type(const int_type& __c1, const int_type& __c2)
00230       { return __c1 == __c2; }
00231 
00232       static int_type 
00233       eof() { return static_cast<int_type>(WEOF); }
00234 
00235       static int_type 
00236       not_eof(const int_type& __c)
00237       { return eq_int_type(__c, eof()) ? 0 : __c; }
00238   };
00239 #endif //_GLIBCPP_USE_WCHAR_T
00240 
00241   template<typename _CharT, typename _Traits>
00242     struct _Char_traits_match
00243     {
00244       _CharT _M_c;
00245       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
00246 
00247       bool 
00248       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
00249     };
00250 } // namespace std
00251 
00252 #endif

Generated on Tue Dec 23 12:33:39 2003 for libstdc++-v3 Source by doxygen 1.3.4