1 // std::codecvt implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 22.2.1.5 - Template class codecvt
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
38 #include <cstdlib> // For MB_CUR_MAX
39 #include <climits> // For MB_LEN_MAX
42 _GLIBCXX_BEGIN_NAMESPACE(std)
45 #ifdef _GLIBCXX_USE_WCHAR_T
47 codecvt<wchar_t, char, mbstate_t>::
48 do_out(state_type& __state, const intern_type* __from,
49 const intern_type* __from_end, const intern_type*& __from_next,
50 extern_type* __to, extern_type* __to_end,
51 extern_type*& __to_next) const
54 // The conversion must be done using a temporary destination buffer
55 // since it is not possible to pass the size of the buffer to wcrtomb
56 state_type __tmp_state(__state);
58 // The conversion must be done by calling wcrtomb in a loop rather
59 // than using wcsrtombs because wcsrtombs assumes that the input is
62 // Either we can upper bound the total number of external characters to
63 // something smaller than __to_end - __to or the conversion must be done
64 // using a temporary destination buffer since it is not possible to
65 // pass the size of the buffer to wcrtomb
66 if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
67 while (__from < __from_end)
69 const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
70 if (__conv == static_cast<size_t>(-1))
75 __state = __tmp_state;
81 extern_type __buf[MB_LEN_MAX];
82 while (__from < __from_end && __to < __to_end)
84 const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
85 if (__conv == static_cast<size_t>(-1))
90 else if (__conv > static_cast<size_t>(__to_end - __to))
96 memcpy(__to, __buf, __conv);
97 __state = __tmp_state;
103 if (__ret == ok && __from < __from_end)
106 __from_next = __from;
112 codecvt<wchar_t, char, mbstate_t>::
113 do_in(state_type& __state, const extern_type* __from,
114 const extern_type* __from_end, const extern_type*& __from_next,
115 intern_type* __to, intern_type* __to_end,
116 intern_type*& __to_next) const
119 // This temporary state object is neccessary so __state won't be modified
120 // if [__from, __from_end) is a partial multibyte character.
121 state_type __tmp_state(__state);
123 // Conversion must be done by calling mbrtowc in a loop rather than
124 // by calling mbsrtowcs because mbsrtowcs assumes that the input
125 // sequence is zero-terminated.
126 while (__from < __from_end && __to < __to_end)
128 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
130 if (__conv == static_cast<size_t>(-1))
135 else if (__conv == static_cast<size_t>(-2))
137 // It is unclear what to return in this case (see DR 382).
141 else if (__conv == 0)
143 // XXX Probably wrong for stateful encodings
148 __state = __tmp_state;
153 // It is not clear that __from < __from_end implies __ret != ok
155 if (__ret == ok && __from < __from_end)
158 __from_next = __from;
164 codecvt<wchar_t, char, mbstate_t>::
165 do_encoding() const throw()
167 // XXX This implementation assumes that the encoding is
168 // stateless and is either single-byte or variable-width.
176 codecvt<wchar_t, char, mbstate_t>::
177 do_max_length() const throw()
179 // XXX Probably wrong for stateful encodings.
180 int __ret = MB_CUR_MAX;
185 codecvt<wchar_t, char, mbstate_t>::
186 do_length(state_type& __state, const extern_type* __from,
187 const extern_type* __end, size_t __max) const
190 state_type __tmp_state(__state);
192 while (__from < __end && __max)
194 size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
195 if (__conv == static_cast<size_t>(-1))
197 // Invalid source character
200 else if (__conv == static_cast<size_t>(-2))
202 // Remainder of input does not form a complete destination
206 else if (__conv == 0)
208 // XXX Probably wrong for stateful encodings
212 __state = __tmp_state;
222 _GLIBCXX_END_NAMESPACE