FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / config / locale / gnu / codecvt_members.cc
blobb093385a586435578d462a724c74289c07398be8
1 // std::codecvt implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2002 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.
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.
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.
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.
31 // ISO C++ 14882: 22.2.1.5 - Template class codecvt
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
36 #include <locale>
37 #include <bits/c++locale_internal.h>
39 namespace std
41 // Specializations.
42 #ifdef _GLIBCPP_USE_WCHAR_T
43 codecvt_base::result
44 codecvt<wchar_t, char, mbstate_t>::
45 do_out(state_type& __state, const intern_type* __from,
46 const intern_type* __from_end, const intern_type*& __from_next,
47 extern_type* __to, extern_type* __to_end,
48 extern_type*& __to_next) const
50 result __ret = ok;
51 // The conversion must be done using a temporary destination buffer
52 // since it is not possible to pass the size of the buffer to wcrtomb
53 extern_type __buf[MB_LEN_MAX];
54 // A temporary state must be used since the result of the last
55 // conversion may be thrown away.
56 state_type __tmp_state(__state);
58 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
59 __c_locale __old = __uselocale(_M_c_locale_codecvt);
60 #endif
62 // The conversion must be done by calling wcrtomb in a loop rather
63 // than using wcsrtombs because wcsrtombs assumes that the input is
64 // zero-terminated.
65 while (__from < __from_end && __to < __to_end)
67 size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
68 if (__conv == static_cast<size_t>(-1))
70 __ret = error;
71 break;
73 else if (__conv > static_cast<size_t>(__to_end - __to))
75 __ret = partial;
76 break;
79 memcpy(__to, __buf, __conv);
80 __state = __tmp_state;
81 __to += __conv;
82 __from++;
85 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
86 __uselocale(__old);
87 #endif
89 if (__ret == ok && __from < __from_end)
90 __ret = partial;
92 __from_next = __from;
93 __to_next = __to;
94 return __ret;
97 codecvt_base::result
98 codecvt<wchar_t, char, mbstate_t>::
99 do_in(state_type& __state, const extern_type* __from,
100 const extern_type* __from_end, const extern_type*& __from_next,
101 intern_type* __to, intern_type* __to_end,
102 intern_type*& __to_next) const
104 result __ret = ok;
105 // This temporary state object is neccessary so __state won't be modified
106 // if [__from, __from_end) is a partial multibyte character.
107 state_type __tmp_state(__state);
108 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
109 __c_locale __old = __uselocale(_M_c_locale_codecvt);
110 #endif
112 // Conversion must be done by calling mbrtowc in a loop rather than
113 // by calling mbsrtowcs because mbsrtowcs assumes that the input
114 // sequence is zero-terminated.
115 while (__from < __from_end && __to < __to_end)
117 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
118 &__tmp_state);
119 if (__conv == static_cast<size_t>(-1))
121 __ret = error;
122 break;
124 else if (__conv == static_cast<size_t>(-2))
126 // It is unclear what to return in this case (see DR 382).
127 __ret = partial;
128 break;
130 else if (__conv == 0)
132 // XXX Probably wrong for stateful encodings
133 __conv = 1;
134 *__to = L'\0';
137 __state = __tmp_state;
138 __to++;
139 __from += __conv;
142 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
143 __uselocale(__old);
144 #endif
146 // It is not clear that __from < __from_end implies __ret != ok
147 // (see DR 382).
148 if (__ret == ok && __from < __from_end)
149 __ret = partial;
151 __from_next = __from;
152 __to_next = __to;
153 return __ret;
156 int
157 codecvt<wchar_t, char, mbstate_t>::
158 do_encoding() const throw()
160 // XXX This implementation assumes that the encoding is
161 // stateless and is either single-byte or variable-width.
162 int __ret = 0;
163 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
164 __c_locale __old = __uselocale(_M_c_locale_codecvt);
165 #endif
166 if (MB_CUR_MAX == 1)
167 __ret = 1;
168 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
169 __uselocale(__old);
170 #endif
171 return __ret;
174 int
175 codecvt<wchar_t, char, mbstate_t>::
176 do_max_length() const throw()
178 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
179 __c_locale __old = __uselocale(_M_c_locale_codecvt);
180 #endif
181 // XXX Probably wrong for stateful encodings.
182 int __ret = MB_CUR_MAX;
183 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
184 __uselocale(__old);
185 #endif
186 return __ret;
189 int
190 codecvt<wchar_t, char, mbstate_t>::
191 do_length(state_type& __state, const extern_type* __from,
192 const extern_type* __end, size_t __max) const
194 int __ret = 0;
195 state_type __tmp_state(__state);
196 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
197 __c_locale __old = __uselocale(_M_c_locale_codecvt);
198 #endif
200 while (__from < __end && __max)
202 size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
203 if (__conv == static_cast<size_t>(-1))
205 // Invalid source character
206 break;
208 else if (__conv == static_cast<size_t>(-2))
210 // Remainder of input does not form a complete destination
211 // character.
212 break;
214 else if (__conv == 0)
216 // XXX Probably wrong for stateful encodings
217 __conv = 1;
220 __state = __tmp_state;
221 __from += __conv;
222 __ret += __conv;
223 __max--;
226 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
227 __uselocale(__old);
228 #endif
229 return __ret;
231 #endif