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 _GLIBCXX_BEGIN_NAMESPACE(std
)
40 #ifdef _GLIBCXX_USE_WCHAR_T
42 codecvt
<wchar_t, char, mbstate_t>::
43 do_out(state_type
& __state
, const intern_type
* __from
,
44 const intern_type
* __from_end
, const intern_type
*& __from_next
,
45 extern_type
* __to
, extern_type
* __to_end
,
46 extern_type
*& __to_next
) const
49 // The conversion must be done using a temporary destination buffer
50 // since it is not possible to pass the size of the buffer to wcrtomb
51 state_type
__tmp_state(__state
);
53 // The conversion must be done by calling wcrtomb in a loop rather
54 // than using wcsrtombs because wcsrtombs assumes that the input is
57 // Either we can upper bound the total number of external characters to
58 // something smaller than __to_end - __to or the conversion must be done
59 // using a temporary destination buffer since it is not possible to
60 // pass the size of the buffer to wcrtomb
61 if (MB_CUR_MAX
* (__from_end
- __from
) - (__to_end
- __to
) <= 0)
62 while (__from
< __from_end
)
64 const size_t __conv
= wcrtomb(__to
, *__from
, &__tmp_state
);
65 if (__conv
== static_cast<size_t>(-1))
70 __state
= __tmp_state
;
76 extern_type __buf
[MB_LEN_MAX
];
77 while (__from
< __from_end
&& __to
< __to_end
)
79 const size_t __conv
= wcrtomb(__buf
, *__from
, &__tmp_state
);
80 if (__conv
== static_cast<size_t>(-1))
85 else if (__conv
> static_cast<size_t>(__to_end
- __to
))
91 memcpy(__to
, __buf
, __conv
);
92 __state
= __tmp_state
;
98 if (__ret
== ok
&& __from
< __from_end
)
101 __from_next
= __from
;
107 codecvt
<wchar_t, char, mbstate_t>::
108 do_in(state_type
& __state
, const extern_type
* __from
,
109 const extern_type
* __from_end
, const extern_type
*& __from_next
,
110 intern_type
* __to
, intern_type
* __to_end
,
111 intern_type
*& __to_next
) const
114 // This temporary state object is neccessary so __state won't be modified
115 // if [__from, __from_end) is a partial multibyte character.
116 state_type
__tmp_state(__state
);
118 // Conversion must be done by calling mbrtowc in a loop rather than
119 // by calling mbsrtowcs because mbsrtowcs assumes that the input
120 // sequence is zero-terminated.
121 while (__from
< __from_end
&& __to
< __to_end
)
123 size_t __conv
= mbrtowc(__to
, __from
, __from_end
- __from
,
125 if (__conv
== static_cast<size_t>(-1))
130 else if (__conv
== static_cast<size_t>(-2))
132 // It is unclear what to return in this case (see DR 382).
136 else if (__conv
== 0)
138 // XXX Probably wrong for stateful encodings
143 __state
= __tmp_state
;
148 // It is not clear that __from < __from_end implies __ret != ok
150 if (__ret
== ok
&& __from
< __from_end
)
153 __from_next
= __from
;
159 codecvt
<wchar_t, char, mbstate_t>::
160 do_encoding() const throw()
162 // XXX This implementation assumes that the encoding is
163 // stateless and is either single-byte or variable-width.
171 codecvt
<wchar_t, char, mbstate_t>::
172 do_max_length() const throw()
174 // XXX Probably wrong for stateful encodings.
175 int __ret
= MB_CUR_MAX
;
180 codecvt
<wchar_t, char, mbstate_t>::
181 do_length(state_type
& __state
, const extern_type
* __from
,
182 const extern_type
* __end
, size_t __max
) const
185 state_type
__tmp_state(__state
);
187 while (__from
< __end
&& __max
)
189 size_t __conv
= mbrtowc(0, __from
, __end
- __from
, &__tmp_state
);
190 if (__conv
== static_cast<size_t>(-1))
192 // Invalid source character
195 else if (__conv
== static_cast<size_t>(-2))
197 // Remainder of input does not form a complete destination
201 else if (__conv
== 0)
203 // XXX Probably wrong for stateful encodings
207 __state
= __tmp_state
;
217 _GLIBCXX_END_NAMESPACE