1 // std::codecvt implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 // ISO C++ 14882: 22.2.1.5 - Template class codecvt
30 // Written by Benjamin Kosnik <bkoz@redhat.com>
33 #include <cstdlib> // For MB_CUR_MAX
34 #include <climits> // For MB_LEN_MAX
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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 state_type __tmp_state(__state);
55 // The conversion must be done by calling wcrtomb in a loop rather
56 // than using wcsrtombs because wcsrtombs assumes that the input is
59 // Either we can upper bound the total number of external characters to
60 // something smaller than __to_end - __to or the conversion must be done
61 // using a temporary destination buffer since it is not possible to
62 // pass the size of the buffer to wcrtomb
63 if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
64 while (__from < __from_end)
66 const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
67 if (__conv == static_cast<size_t>(-1))
72 __state = __tmp_state;
78 extern_type __buf[MB_LEN_MAX];
79 while (__from < __from_end && __to < __to_end)
81 const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
82 if (__conv == static_cast<size_t>(-1))
87 else if (__conv > static_cast<size_t>(__to_end - __to))
93 memcpy(__to, __buf, __conv);
94 __state = __tmp_state;
100 if (__ret == ok && __from < __from_end)
103 __from_next = __from;
109 codecvt<wchar_t, char, mbstate_t>::
110 do_in(state_type& __state, const extern_type* __from,
111 const extern_type* __from_end, const extern_type*& __from_next,
112 intern_type* __to, intern_type* __to_end,
113 intern_type*& __to_next) const
116 // This temporary state object is neccessary so __state won't be modified
117 // if [__from, __from_end) is a partial multibyte character.
118 state_type __tmp_state(__state);
120 // Conversion must be done by calling mbrtowc in a loop rather than
121 // by calling mbsrtowcs because mbsrtowcs assumes that the input
122 // sequence is zero-terminated.
123 while (__from < __from_end && __to < __to_end)
125 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
127 if (__conv == static_cast<size_t>(-1))
132 else if (__conv == static_cast<size_t>(-2))
134 // It is unclear what to return in this case (see DR 382).
138 else if (__conv == 0)
140 // XXX Probably wrong for stateful encodings
145 __state = __tmp_state;
150 // It is not clear that __from < __from_end implies __ret != ok
152 if (__ret == ok && __from < __from_end)
155 __from_next = __from;
161 codecvt<wchar_t, char, mbstate_t>::
162 do_encoding() const throw()
164 // XXX This implementation assumes that the encoding is
165 // stateless and is either single-byte or variable-width.
173 codecvt<wchar_t, char, mbstate_t>::
174 do_max_length() const throw()
176 // XXX Probably wrong for stateful encodings.
177 int __ret = MB_CUR_MAX;
182 codecvt<wchar_t, char, mbstate_t>::
183 do_length(state_type& __state, const extern_type* __from,
184 const extern_type* __end, size_t __max) const
187 state_type __tmp_state(__state);
189 while (__from < __end && __max)
191 size_t __conv = mbrtowc(0, __from, __end - __from, &__tmp_state);
192 if (__conv == static_cast<size_t>(-1))
194 // Invalid source character
197 else if (__conv == static_cast<size_t>(-2))
199 // Remainder of input does not form a complete destination
203 else if (__conv == 0)
205 // XXX Probably wrong for stateful encodings
209 __state = __tmp_state;
219 _GLIBCXX_END_NAMESPACE_VERSION