OSDN Git Service

2002-03-08 scott snyder <snyder@fnal.gov>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / ctype_members_gnu.cc
1 // std::ctype implementation details, GNU version -*- C++ -*-
2
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 22.2.1.1.2  ctype virtual functions.
32 //
33
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37
38 namespace std
39 {
40   // NB: The other ctype<char> specializations are in src/locale.cc and
41   // various /config/os/* files.
42   template<>
43     ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
44     : ctype<char>(0, false, __refs) 
45     {   
46       if (_M_c_locale_ctype)
47         _S_destroy_c_locale(_M_c_locale_ctype);
48       _S_create_c_locale(_M_c_locale_ctype, __s); 
49       _M_toupper = _M_c_locale_ctype->__ctype_toupper;
50       _M_tolower = _M_c_locale_ctype->__ctype_tolower;
51       _M_table = _M_c_locale_ctype->__ctype_b;
52     }
53
54 #ifdef _GLIBCPP_USE_WCHAR_T  
55   ctype<wchar_t>::__wmask_type
56   ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
57   {
58     __wmask_type __ret;
59     switch (__m)
60       {
61       case space:
62         __ret = __wctype_l("space", _M_c_locale_ctype);
63         break;
64       case print:
65         __ret = __wctype_l("print", _M_c_locale_ctype);
66         break;
67       case cntrl:
68         __ret = __wctype_l("cntrl", _M_c_locale_ctype);
69         break;
70       case upper:
71         __ret = __wctype_l("upper", _M_c_locale_ctype);
72         break;
73       case lower:
74         __ret = __wctype_l("lower", _M_c_locale_ctype);
75         break;
76       case alpha:
77         __ret = __wctype_l("alpha", _M_c_locale_ctype);
78         break;
79       case digit:
80         __ret = __wctype_l("digit", _M_c_locale_ctype);
81         break;
82       case punct:
83         __ret = __wctype_l("punct", _M_c_locale_ctype);
84         break;
85       case xdigit:
86         __ret = __wctype_l("xdigit", _M_c_locale_ctype);
87         break;
88       case alnum:
89         __ret = __wctype_l("alnum", _M_c_locale_ctype);
90         break;
91       case graph:
92         __ret = __wctype_l("graph", _M_c_locale_ctype);
93         break;
94       default:
95         __ret = 0;
96       }
97     return __ret;
98   };
99   
100   wchar_t
101   ctype<wchar_t>::do_toupper(wchar_t __c) const
102   { return __towupper_l(__c, _M_c_locale_ctype); }
103
104   const wchar_t*
105   ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
106   {
107     while (__lo < __hi)
108       {
109         *__lo = __towupper_l(*__lo, _M_c_locale_ctype);
110         ++__lo;
111       }
112     return __hi;
113   }
114   
115   wchar_t
116   ctype<wchar_t>::do_tolower(wchar_t __c) const
117   { return __towlower_l(__c, _M_c_locale_ctype); }
118   
119   const wchar_t*
120   ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
121   {
122     while (__lo < __hi)
123       {
124         *__lo = __towlower_l(*__lo, _M_c_locale_ctype);
125         ++__lo;
126       }
127     return __hi;
128   }
129
130   bool
131   ctype<wchar_t>::
132   do_is(mask __m, char_type __c) const
133   { return static_cast<bool>(__iswctype_l(__c, _M_convert_to_wmask(__m), 
134                                           _M_c_locale_ctype)); }
135   
136   const wchar_t* 
137   ctype<wchar_t>::
138   do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __m) const
139   {
140     while (__lo < __hi && !this->do_is(*__m, *__lo))
141       ++__lo;
142     return __lo;
143   }
144   
145   const wchar_t* 
146   ctype<wchar_t>::
147   do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
148   {
149     while (__lo < __hi && !this->do_is(__m, *__lo))
150       ++__lo;
151     return __lo;
152   }
153
154   const wchar_t*
155   ctype<wchar_t>::
156   do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
157   {
158     while (__lo < __hi && this->do_is(__m, *__lo) != 0)
159       ++__lo;
160     return __lo;
161   }
162
163   wchar_t
164   ctype<wchar_t>::
165   do_widen(char __c) const
166   { return btowc(__c); }
167   
168   const char* 
169   ctype<wchar_t>::
170   do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
171   {
172     mbstate_t __state;
173     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
174     mbsrtowcs(__dest, &__lo, __hi - __lo, &__state);
175     return __hi;
176   }
177
178   char
179   ctype<wchar_t>::
180   do_narrow(wchar_t __wc, char __dfault) const
181   { 
182     int __c = wctob(__wc);
183     return (__c == EOF ? __dfault : static_cast<char>(__c)); 
184   }
185
186   const wchar_t*
187   ctype<wchar_t>::
188   do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 
189             char* __dest) const
190   {
191     mbstate_t __state;
192     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
193     size_t __len = __hi - __lo;
194     size_t __conv = wcsrtombs(__dest, &__lo, __len, &__state);
195     if (__conv == __len)
196       *__dest = __dfault;
197     return __hi;
198   }
199 #endif //  _GLIBCPP_USE_WCHAR_T
200 }