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 // 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);
62 // The conversion must be done by calling wcrtomb in a loop rather
63 // than using wcsrtombs because wcsrtombs assumes that the input is
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))
73 else if (__conv > static_cast<size_t>(__to_end - __to))
79 memcpy(__to, __buf, __conv);
80 __state = __tmp_state;
85 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
89 if (__ret == ok && __from < __from_end)
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
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);
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,
119 if (__conv == static_cast<size_t>(-1))
124 else if (__conv == static_cast<size_t>(-2))
126 // It is unclear what to return in this case (see DR 382).
130 else if (__conv == 0)
132 // XXX Probably wrong for stateful encodings
137 __state = __tmp_state;
142 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
146 // It is not clear that __from < __from_end implies __ret != ok
148 if (__ret == ok && __from < __from_end)
151 __from_next = __from;
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.
163 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
164 __c_locale __old = __uselocale(_M_c_locale_codecvt);
168 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
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);
181 // XXX Probably wrong for stateful encodings.
182 int __ret = MB_CUR_MAX;
183 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
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
195 state_type __tmp_state(__state);
196 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
197 __c_locale __old = __uselocale(_M_c_locale_codecvt);
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
208 else if (__conv == static_cast<size_t>(-2))
210 // Remainder of input does not form a complete destination
214 else if (__conv == 0)
216 // XXX Probably wrong for stateful encodings
220 __state = __tmp_state;
226 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)