1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 // 2009 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/>.
26 /** @file bits/codecvt.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
32 // ISO C++ 14882: 22.2.1.5 Template class codecvt
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
40 #pragma GCC system_header
42 _GLIBCXX_BEGIN_NAMESPACE(std)
44 /// Empty base class for codecvt facet [22.2.1.5].
58 * @brief Common base for codecvt functions.
60 * This template class provides implementations of the public functions
61 * that forward to the protected virtual functions.
63 * This template also provides abstract stubs for the protected virtual
66 template<typename _InternT, typename _ExternT, typename _StateT>
67 class __codecvt_abstract_base
68 : public locale::facet, public codecvt_base
72 typedef codecvt_base::result result;
73 typedef _InternT intern_type;
74 typedef _ExternT extern_type;
75 typedef _StateT state_type;
77 // 22.2.1.5.1 codecvt members
79 * @brief Convert from internal to external character set.
81 * Converts input string of intern_type to output string of
82 * extern_type. This is analogous to wcsrtombs. It does this by
83 * calling codecvt::do_out.
85 * The source and destination character sets are determined by the
86 * facet's locale, internal and external types.
88 * The characters in [from,from_end) are converted and written to
89 * [to,to_end). from_next and to_next are set to point to the
90 * character following the last successfully converted character,
91 * respectively. If the result needed no conversion, from_next and
92 * to_next are not affected.
94 * The @a state argument should be initialized if the input is at the
95 * beginning and carried from a previous call if continuing
96 * conversion. There are no guarantees about how @a state is used.
98 * The result returned is a member of codecvt_base::result. If
99 * all the input is converted, returns codecvt_base::ok. If no
100 * conversion is necessary, returns codecvt_base::noconv. If
101 * the input ends early or there is insufficient space in the
102 * output, returns codecvt_base::partial. Otherwise the
103 * conversion failed and codecvt_base::error is returned.
105 * @param state Persistent conversion state data.
106 * @param from Start of input.
107 * @param from_end End of input.
108 * @param from_next Returns start of unconverted data.
109 * @param to Start of output buffer.
110 * @param to_end End of output buffer.
111 * @param to_next Returns start of unused output area.
112 * @return codecvt_base::result.
115 out(state_type& __state, const intern_type* __from,
116 const intern_type* __from_end, const intern_type*& __from_next,
117 extern_type* __to, extern_type* __to_end,
118 extern_type*& __to_next) const
120 return this->do_out(__state, __from, __from_end, __from_next,
121 __to, __to_end, __to_next);
125 * @brief Reset conversion state.
127 * Writes characters to output that would restore @a state to initial
128 * conditions. The idea is that if a partial conversion occurs, then
129 * the converting the characters written by this function would leave
130 * the state in initial conditions, rather than partial conversion
131 * state. It does this by calling codecvt::do_unshift().
133 * For example, if 4 external characters always converted to 1 internal
134 * character, and input to in() had 6 external characters with state
135 * saved, this function would write two characters to the output and
136 * set the state to initialized conditions.
138 * The source and destination character sets are determined by the
139 * facet's locale, internal and external types.
141 * The result returned is a member of codecvt_base::result. If the
142 * state could be reset and data written, returns codecvt_base::ok. If
143 * no conversion is necessary, returns codecvt_base::noconv. If the
144 * output has insufficient space, returns codecvt_base::partial.
145 * Otherwise the reset failed and codecvt_base::error is returned.
147 * @param state Persistent conversion state data.
148 * @param to Start of output buffer.
149 * @param to_end End of output buffer.
150 * @param to_next Returns start of unused output area.
151 * @return codecvt_base::result.
154 unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
155 extern_type*& __to_next) const
156 { return this->do_unshift(__state, __to,__to_end,__to_next); }
159 * @brief Convert from external to internal character set.
161 * Converts input string of extern_type to output string of
162 * intern_type. This is analogous to mbsrtowcs. It does this by
163 * calling codecvt::do_in.
165 * The source and destination character sets are determined by the
166 * facet's locale, internal and external types.
168 * The characters in [from,from_end) are converted and written to
169 * [to,to_end). from_next and to_next are set to point to the
170 * character following the last successfully converted character,
171 * respectively. If the result needed no conversion, from_next and
172 * to_next are not affected.
174 * The @a state argument should be initialized if the input is at the
175 * beginning and carried from a previous call if continuing
176 * conversion. There are no guarantees about how @a state is used.
178 * The result returned is a member of codecvt_base::result. If
179 * all the input is converted, returns codecvt_base::ok. If no
180 * conversion is necessary, returns codecvt_base::noconv. If
181 * the input ends early or there is insufficient space in the
182 * output, returns codecvt_base::partial. Otherwise the
183 * conversion failed and codecvt_base::error is returned.
185 * @param state Persistent conversion state data.
186 * @param from Start of input.
187 * @param from_end End of input.
188 * @param from_next Returns start of unconverted data.
189 * @param to Start of output buffer.
190 * @param to_end End of output buffer.
191 * @param to_next Returns start of unused output area.
192 * @return codecvt_base::result.
195 in(state_type& __state, const extern_type* __from,
196 const extern_type* __from_end, const extern_type*& __from_next,
197 intern_type* __to, intern_type* __to_end,
198 intern_type*& __to_next) const
200 return this->do_in(__state, __from, __from_end, __from_next,
201 __to, __to_end, __to_next);
205 encoding() const throw()
206 { return this->do_encoding(); }
209 always_noconv() const throw()
210 { return this->do_always_noconv(); }
213 length(state_type& __state, const extern_type* __from,
214 const extern_type* __end, size_t __max) const
215 { return this->do_length(__state, __from, __end, __max); }
218 max_length() const throw()
219 { return this->do_max_length(); }
223 __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
226 ~__codecvt_abstract_base() { }
229 * @brief Convert from internal to external character set.
231 * Converts input string of intern_type to output string of
232 * extern_type. This function is a hook for derived classes to change
233 * the value returned. @see out for more information.
236 do_out(state_type& __state, const intern_type* __from,
237 const intern_type* __from_end, const intern_type*& __from_next,
238 extern_type* __to, extern_type* __to_end,
239 extern_type*& __to_next) const = 0;
242 do_unshift(state_type& __state, extern_type* __to,
243 extern_type* __to_end, extern_type*& __to_next) const = 0;
246 do_in(state_type& __state, const extern_type* __from,
247 const extern_type* __from_end, const extern_type*& __from_next,
248 intern_type* __to, intern_type* __to_end,
249 intern_type*& __to_next) const = 0;
252 do_encoding() const throw() = 0;
255 do_always_noconv() const throw() = 0;
258 do_length(state_type&, const extern_type* __from,
259 const extern_type* __end, size_t __max) const = 0;
262 do_max_length() const throw() = 0;
265 /// @brief class codecvt [22.2.1.5].
266 /// NB: Generic, mostly useless implementation.
267 template<typename _InternT, typename _ExternT, typename _StateT>
269 : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
273 typedef codecvt_base::result result;
274 typedef _InternT intern_type;
275 typedef _ExternT extern_type;
276 typedef _StateT state_type;
279 __c_locale _M_c_locale_codecvt;
282 static locale::id id;
285 codecvt(size_t __refs = 0)
286 : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
289 codecvt(__c_locale __cloc, size_t __refs = 0);
296 do_out(state_type& __state, const intern_type* __from,
297 const intern_type* __from_end, const intern_type*& __from_next,
298 extern_type* __to, extern_type* __to_end,
299 extern_type*& __to_next) const;
302 do_unshift(state_type& __state, extern_type* __to,
303 extern_type* __to_end, extern_type*& __to_next) const;
306 do_in(state_type& __state, const extern_type* __from,
307 const extern_type* __from_end, const extern_type*& __from_next,
308 intern_type* __to, intern_type* __to_end,
309 intern_type*& __to_next) const;
312 do_encoding() const throw();
315 do_always_noconv() const throw();
318 do_length(state_type&, const extern_type* __from,
319 const extern_type* __end, size_t __max) const;
322 do_max_length() const throw();
325 template<typename _InternT, typename _ExternT, typename _StateT>
326 locale::id codecvt<_InternT, _ExternT, _StateT>::id;
328 /// class codecvt<char, char, mbstate_t> specialization.
330 class codecvt<char, char, mbstate_t>
331 : public __codecvt_abstract_base<char, char, mbstate_t>
335 typedef char intern_type;
336 typedef char extern_type;
337 typedef mbstate_t state_type;
340 __c_locale _M_c_locale_codecvt;
343 static locale::id id;
346 codecvt(size_t __refs = 0);
349 codecvt(__c_locale __cloc, size_t __refs = 0);
356 do_out(state_type& __state, const intern_type* __from,
357 const intern_type* __from_end, const intern_type*& __from_next,
358 extern_type* __to, extern_type* __to_end,
359 extern_type*& __to_next) const;
362 do_unshift(state_type& __state, extern_type* __to,
363 extern_type* __to_end, extern_type*& __to_next) const;
366 do_in(state_type& __state, const extern_type* __from,
367 const extern_type* __from_end, const extern_type*& __from_next,
368 intern_type* __to, intern_type* __to_end,
369 intern_type*& __to_next) const;
372 do_encoding() const throw();
375 do_always_noconv() const throw();
378 do_length(state_type&, const extern_type* __from,
379 const extern_type* __end, size_t __max) const;
382 do_max_length() const throw();
385 #ifdef _GLIBCXX_USE_WCHAR_T
386 /// class codecvt<wchar_t, char, mbstate_t> specialization.
388 class codecvt<wchar_t, char, mbstate_t>
389 : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
393 typedef wchar_t intern_type;
394 typedef char extern_type;
395 typedef mbstate_t state_type;
398 __c_locale _M_c_locale_codecvt;
401 static locale::id id;
404 codecvt(size_t __refs = 0);
407 codecvt(__c_locale __cloc, size_t __refs = 0);
414 do_out(state_type& __state, const intern_type* __from,
415 const intern_type* __from_end, const intern_type*& __from_next,
416 extern_type* __to, extern_type* __to_end,
417 extern_type*& __to_next) const;
420 do_unshift(state_type& __state,
421 extern_type* __to, extern_type* __to_end,
422 extern_type*& __to_next) const;
425 do_in(state_type& __state,
426 const extern_type* __from, const extern_type* __from_end,
427 const extern_type*& __from_next,
428 intern_type* __to, intern_type* __to_end,
429 intern_type*& __to_next) const;
432 int do_encoding() const throw();
435 bool do_always_noconv() const throw();
438 int do_length(state_type&, const extern_type* __from,
439 const extern_type* __end, size_t __max) const;
442 do_max_length() const throw();
444 #endif //_GLIBCXX_USE_WCHAR_T
446 /// class codecvt_byname [22.2.1.6].
447 template<typename _InternT, typename _ExternT, typename _StateT>
448 class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
452 codecvt_byname(const char* __s, size_t __refs = 0)
453 : codecvt<_InternT, _ExternT, _StateT>(__refs)
455 if (__builtin_strcmp(__s, "C") != 0
456 && __builtin_strcmp(__s, "POSIX") != 0)
458 this->_S_destroy_c_locale(this->_M_c_locale_codecvt);
459 this->_S_create_c_locale(this->_M_c_locale_codecvt, __s);
465 ~codecvt_byname() { }
468 // Inhibit implicit instantiations for required instantiations,
469 // which are defined via explicit instantiations elsewhere.
470 // NB: This syntax is a GNU extension.
471 #if _GLIBCXX_EXTERN_TEMPLATE
472 extern template class codecvt_byname<char, char, mbstate_t>;
475 const codecvt<char, char, mbstate_t>&
476 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
480 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
482 #ifdef _GLIBCXX_USE_WCHAR_T
483 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
486 const codecvt<wchar_t, char, mbstate_t>&
487 use_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
491 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
495 _GLIBCXX_END_NAMESPACE