1 // std::codecvt implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2002 Free Software Foundation, Inc.
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)
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,
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>
37 #include <bits/c++locale_internal.h>
42 #ifdef _GLIBCXX_USE_WCHAR_T
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
51 // A temporary state must be used since the result of the last
52 // conversion may be thrown away.
53 state_type __tmp_state(__state);
55 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
56 __c_locale __old = __uselocale(_M_c_locale_codecvt);
59 // The conversion must be done by calling wcrtomb in a loop rather
60 // than using wcsrtombs because wcsrtombs assumes that the input is
63 // Either we can upper bound the total number of external characters to
64 // something smaller than __to_end - __to or the conversion must be done
65 // using a temporary destination buffer since it is not possible to
66 // pass the size of the buffer to wcrtomb
67 if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
68 while (__from < __from_end)
70 const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
71 if (__conv == static_cast<size_t>(-1))
76 __state = __tmp_state;
82 extern_type __buf[MB_LEN_MAX];
83 while (__from < __from_end && __to < __to_end)
85 const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
86 if (__conv == static_cast<size_t>(-1))
91 else if (__conv > static_cast<size_t>(__to_end - __to))
97 memcpy(__to, __buf, __conv);
98 __state = __tmp_state;
104 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
108 if (__ret == ok && __from < __from_end)
111 __from_next = __from;
117 codecvt<wchar_t, char, mbstate_t>::
118 do_in(state_type& __state, const extern_type* __from,
119 const extern_type* __from_end, const extern_type*& __from_next,
120 intern_type* __to, intern_type* __to_end,
121 intern_type*& __to_next) const
124 // This temporary state object is neccessary so __state won't be modified
125 // if [__from, __from_end) is a partial multibyte character.
126 state_type __tmp_state(__state);
127 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
128 __c_locale __old = __uselocale(_M_c_locale_codecvt);
131 // Conversion must be done by calling mbrtowc in a loop rather than
132 // by calling mbsrtowcs because mbsrtowcs assumes that the input
133 // sequence is zero-terminated.
134 while (__from < __from_end && __to < __to_end)
136 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
138 if (__conv == static_cast<size_t>(-1))
143 else if (__conv == static_cast<size_t>(-2))
145 // It is unclear what to return in this case (see DR 382).
149 else if (__conv == 0)
151 // XXX Probably wrong for stateful encodings
156 __state = __tmp_state;
161 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
165 // It is not clear that __from < __from_end implies __ret != ok
167 if (__ret == ok && __from < __from_end)
170 __from_next = __from;
176 codecvt<wchar_t, char, mbstate_t>::
177 do_encoding() const throw()
179 // XXX This implementation assumes that the encoding is
180 // stateless and is either single-byte or variable-width.
182 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
183 __c_locale __old = __uselocale(_M_c_locale_codecvt);
187 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
194 codecvt<wchar_t, char, mbstate_t>::
195 do_max_length() const throw()
197 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
198 __c_locale __old = __uselocale(_M_c_locale_codecvt);
200 // XXX Probably wrong for stateful encodings.
201 int __ret = MB_CUR_MAX;
202 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
209 codecvt<wchar_t, char, mbstate_t>::
210 do_length(state_type& __state, const extern_type* __from,
211 const extern_type* __end, size_t __max) const
214 state_type __tmp_state(__state);
215 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
216 __c_locale __old = __uselocale(_M_c_locale_codecvt);
219 while (__from < __end && __max)
221 size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
222 if (__conv == static_cast<size_t>(-1))
224 // Invalid source character
227 else if (__conv == static_cast<size_t>(-2))
229 // Remainder of input does not form a complete destination
233 else if (__conv == 0)
235 // XXX Probably wrong for stateful encodings
239 __state = __tmp_state;
245 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)