]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/gnu/codecvt_members.cc
Move from CPP to CXX.
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / gnu / codecvt_members.cc
1 // std::codecvt implementation details, GNU version -*- C++ -*-
2
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.
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.5 - Template class codecvt
32 //
33
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37 #include <bits/c++locale_internal.h>
38
39 namespace std
40 {
41 // Specializations.
42 #ifdef _GLIBCXX_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
49 {
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);
57
58 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
59 __c_locale __old = __uselocale(_M_c_locale_codecvt);
60 #endif
61
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)
66 {
67 size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
68 if (__conv == static_cast<size_t>(-1))
69 {
70 __ret = error;
71 break;
72 }
73 else if (__conv > static_cast<size_t>(__to_end - __to))
74 {
75 __ret = partial;
76 break;
77 }
78
79 memcpy(__to, __buf, __conv);
80 __state = __tmp_state;
81 __to += __conv;
82 __from++;
83 }
84
85 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
86 __uselocale(__old);
87 #endif
88
89 if (__ret == ok && __from < __from_end)
90 __ret = partial;
91
92 __from_next = __from;
93 __to_next = __to;
94 return __ret;
95 }
96
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
103 {
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
111
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)
116 {
117 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
118 &__tmp_state);
119 if (__conv == static_cast<size_t>(-1))
120 {
121 __ret = error;
122 break;
123 }
124 else if (__conv == static_cast<size_t>(-2))
125 {
126 // It is unclear what to return in this case (see DR 382).
127 __ret = partial;
128 break;
129 }
130 else if (__conv == 0)
131 {
132 // XXX Probably wrong for stateful encodings
133 __conv = 1;
134 *__to = L'\0';
135 }
136
137 __state = __tmp_state;
138 __to++;
139 __from += __conv;
140 }
141
142 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
143 __uselocale(__old);
144 #endif
145
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;
150
151 __from_next = __from;
152 __to_next = __to;
153 return __ret;
154 }
155
156 int
157 codecvt<wchar_t, char, mbstate_t>::
158 do_encoding() const throw()
159 {
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;
172 }
173
174 int
175 codecvt<wchar_t, char, mbstate_t>::
176 do_max_length() const throw()
177 {
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;
187 }
188
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
193 {
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
199
200 while (__from < __end && __max)
201 {
202 size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
203 if (__conv == static_cast<size_t>(-1))
204 {
205 // Invalid source character
206 break;
207 }
208 else if (__conv == static_cast<size_t>(-2))
209 {
210 // Remainder of input does not form a complete destination
211 // character.
212 break;
213 }
214 else if (__conv == 0)
215 {
216 // XXX Probably wrong for stateful encodings
217 __conv = 1;
218 }
219
220 __state = __tmp_state;
221 __from += __conv;
222 __ret += __conv;
223 __max--;
224 }
225
226 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
227 __uselocale(__old);
228 #endif
229 return __ret;
230 }
231 #endif
232 }