OSDN Git Service

2002-03-08 scott snyder <snyder@fnal.gov>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / generic / ctype_members.cc
1 // std::ctype implementation details, generic 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       _S_destroy_c_locale(_M_c_locale_ctype);
47       _S_create_c_locale(_M_c_locale_ctype, __s); 
48     }
49
50 #ifdef _GLIBCPP_USE_WCHAR_T  
51   ctype<wchar_t>::__wmask_type
52   ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
53   {
54     __wmask_type __ret;
55     switch (__m)
56       {
57       case space:
58         __ret = wctype("space");
59         break;
60       case print:
61         __ret = wctype("print");
62         break;
63       case cntrl:
64         __ret = wctype("cntrl");
65         break;
66       case upper:
67         __ret = wctype("upper");
68         break;
69       case lower:
70         __ret = wctype("lower");
71         break;
72       case alpha:
73         __ret = wctype("alpha");
74         break;
75       case digit:
76         __ret = wctype("digit");
77         break;
78       case punct:
79         __ret = wctype("punct");
80         break;
81       case xdigit:
82         __ret = wctype("xdigit");
83         break;
84       case alnum:
85         __ret = wctype("alnum");
86         break;
87       case graph:
88         __ret = wctype("graph");
89         break;
90       default:
91         __ret = 0;
92       }
93     return __ret;
94   };
95   
96   wchar_t
97   ctype<wchar_t>::do_toupper(wchar_t __c) const
98   { return towupper(__c); }
99
100   const wchar_t*
101   ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
102   {
103     while (__lo < __hi)
104       {
105         *__lo = towupper(*__lo);
106         ++__lo;
107       }
108     return __hi;
109   }
110   
111   wchar_t
112   ctype<wchar_t>::do_tolower(wchar_t __c) const
113   { return towlower(__c); }
114   
115   const wchar_t*
116   ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
117   {
118     while (__lo < __hi)
119       {
120         *__lo = towlower(*__lo);
121         ++__lo;
122       }
123     return __hi;
124   }
125
126   bool
127   ctype<wchar_t>::
128   do_is(mask __m, char_type __c) const
129   { return static_cast<bool>(iswctype(__c, _M_convert_to_wmask(__m))); }
130   
131   const wchar_t* 
132   ctype<wchar_t>::
133   do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __m) const
134   {
135     while (__lo < __hi && !this->do_is(*__m, *__lo))
136       ++__lo;
137     return __lo;
138   }
139   
140   const wchar_t* 
141   ctype<wchar_t>::
142   do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
143   {
144     while (__lo < __hi && !this->do_is(__m, *__lo))
145       ++__lo;
146     return __lo;
147   }
148
149   const wchar_t*
150   ctype<wchar_t>::
151   do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
152   {
153     while (__lo < __hi && this->do_is(__m, *__lo) != 0)
154       ++__lo;
155     return __lo;
156   }
157
158   wchar_t
159   ctype<wchar_t>::
160   do_widen(char __c) const
161   { return btowc(__c); }
162   
163   const char* 
164   ctype<wchar_t>::
165   do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
166   {
167     mbstate_t __state;
168     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
169     mbsrtowcs(__dest, &__lo, __hi - __lo, &__state);
170     return __hi;
171   }
172
173   char
174   ctype<wchar_t>::
175   do_narrow(wchar_t __wc, char __dfault) const
176   { 
177     int __c = wctob(__wc);
178     return (__c == EOF ? __dfault : static_cast<char>(__c)); 
179   }
180
181   const wchar_t*
182   ctype<wchar_t>::
183   do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 
184             char* __dest) const
185   {
186     mbstate_t __state;
187     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
188     size_t __len = __hi - __lo;
189     size_t __conv = wcsrtombs(__dest, &__lo, __len, &__state);
190     if (__conv == __len)
191       *__dest = __dfault;
192     return __hi;
193   }
194 #endif //  _GLIBCPP_USE_WCHAR_T
195 }