1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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.
31 /** @file bits/codecvt.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 22.2.1.5 Template class codecvt
40 // Written by Benjamin Kosnik <bkoz@redhat.com>
45 #pragma GCC system_header
47 _GLIBCXX_BEGIN_NAMESPACE(std)
49 /// @brief Empty base class for codecvt facet [22.2.1.5].
63 * @brief Common base for codecvt functions.
65 * This template class provides implementations of the public functions
66 * that forward to the protected virtual functions.
68 * This template also provides abstract stubs for the protected virtual
71 template<typename _InternT, typename _ExternT, typename _StateT>
72 class __codecvt_abstract_base
73 : public locale::facet, public codecvt_base
77 typedef codecvt_base::result result;
78 typedef _InternT intern_type;
79 typedef _ExternT extern_type;
80 typedef _StateT state_type;
82 // 22.2.1.5.1 codecvt members
84 * @brief Convert from internal to external character set.
86 * Converts input string of intern_type to output string of
87 * extern_type. This is analogous to wcsrtombs. It does this by
88 * calling codecvt::do_out.
90 * The source and destination character sets are determined by the
91 * facet's locale, internal and external types.
93 * The characters in [from,from_end) are converted and written to
94 * [to,to_end). from_next and to_next are set to point to the
95 * character following the last successfully converted character,
96 * respectively. If the result needed no conversion, from_next and
97 * to_next are not affected.
99 * The @a state argument should be intialized if the input is at the
100 * beginning and carried from a previous call if continuing
101 * conversion. There are no guarantees about how @a state is used.
103 * The result returned is a member of codecvt_base::result. If
104 * all the input is converted, returns codecvt_base::ok. If no
105 * conversion is necessary, returns codecvt_base::noconv. If
106 * the input ends early or there is insufficient space in the
107 * output, returns codecvt_base::partial. Otherwise the
108 * conversion failed and codecvt_base::error is returned.
110 * @param state Persistent conversion state data.
111 * @param from Start of input.
112 * @param from_end End of input.
113 * @param from_next Returns start of unconverted data.
114 * @param to Start of output buffer.
115 * @param to_end End of output buffer.
116 * @param to_next Returns start of unused output area.
117 * @return codecvt_base::result.
120 out(state_type& __state, const intern_type* __from,
121 const intern_type* __from_end, const intern_type*& __from_next,
122 extern_type* __to, extern_type* __to_end,
123 extern_type*& __to_next) const
125 return this->do_out(__state, __from, __from_end, __from_next,
126 __to, __to_end, __to_next);
130 * @brief Reset conversion state.
132 * Writes characters to output that would restore @a state to initial
133 * conditions. The idea is that if a partial conversion occurs, then
134 * the converting the characters written by this function would leave
135 * the state in initial conditions, rather than partial conversion
136 * state. It does this by calling codecvt::do_unshift().
138 * For example, if 4 external characters always converted to 1 internal
139 * character, and input to in() had 6 external characters with state
140 * saved, this function would write two characters to the output and
141 * set the state to initialized conditions.
143 * The source and destination character sets are determined by the
144 * facet's locale, internal and external types.
146 * The result returned is a member of codecvt_base::result. If the
147 * state could be reset and data written, returns codecvt_base::ok. If
148 * no conversion is necessary, returns codecvt_base::noconv. If the
149 * output has insufficient space, returns codecvt_base::partial.
150 * Otherwise the reset failed and codecvt_base::error is returned.
152 * @param state Persistent conversion state data.
153 * @param to Start of output buffer.
154 * @param to_end End of output buffer.
155 * @param to_next Returns start of unused output area.
156 * @return codecvt_base::result.
159 unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
160 extern_type*& __to_next) const
161 { return this->do_unshift(__state, __to,__to_end,__to_next); }
164 * @brief Convert from external to internal character set.
166 * Converts input string of extern_type to output string of
167 * intern_type. This is analogous to mbsrtowcs. It does this by
168 * calling codecvt::do_in.
170 * The source and destination character sets are determined by the
171 * facet's locale, internal and external types.
173 * The characters in [from,from_end) are converted and written to
174 * [to,to_end). from_next and to_next are set to point to the
175 * character following the last successfully converted character,
176 * respectively. If the result needed no conversion, from_next and
177 * to_next are not affected.
179 * The @a state argument should be intialized if the input is at the
180 * beginning and carried from a previous call if continuing
181 * conversion. There are no guarantees about how @a state is used.
183 * The result returned is a member of codecvt_base::result. If
184 * all the input is converted, returns codecvt_base::ok. If no
185 * conversion is necessary, returns codecvt_base::noconv. If
186 * the input ends early or there is insufficient space in the
187 * output, returns codecvt_base::partial. Otherwise the
188 * conversion failed and codecvt_base::error is returned.
190 * @param state Persistent conversion state data.
191 * @param from Start of input.
192 * @param from_end End of input.
193 * @param from_next Returns start of unconverted data.
194 * @param to Start of output buffer.
195 * @param to_end End of output buffer.
196 * @param to_next Returns start of unused output area.
197 * @return codecvt_base::result.
200 in(state_type& __state, const extern_type* __from,
201 const extern_type* __from_end, const extern_type*& __from_next,
202 intern_type* __to, intern_type* __to_end,
203 intern_type*& __to_next) const
205 return this->do_in(__state, __from, __from_end, __from_next,
206 __to, __to_end, __to_next);
210 encoding() const throw()
211 { return this->do_encoding(); }
214 always_noconv() const throw()
215 { return this->do_always_noconv(); }
218 length(state_type& __state, const extern_type* __from,
219 const extern_type* __end, size_t __max) const
220 { return this->do_length(__state, __from, __end, __max); }
223 max_length() const throw()
224 { return this->do_max_length(); }
228 __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
231 ~__codecvt_abstract_base() { }
234 * @brief Convert from internal to external character set.
236 * Converts input string of intern_type to output string of
237 * extern_type. This function is a hook for derived classes to change
238 * the value returned. @see out for more information.
241 do_out(state_type& __state, const intern_type* __from,
242 const intern_type* __from_end, const intern_type*& __from_next,
243 extern_type* __to, extern_type* __to_end,
244 extern_type*& __to_next) const = 0;
247 do_unshift(state_type& __state, extern_type* __to,
248 extern_type* __to_end, extern_type*& __to_next) const = 0;
251 do_in(state_type& __state, const extern_type* __from,
252 const extern_type* __from_end, const extern_type*& __from_next,
253 intern_type* __to, intern_type* __to_end,
254 intern_type*& __to_next) const = 0;
257 do_encoding() const throw() = 0;
260 do_always_noconv() const throw() = 0;
263 do_length(state_type&, const extern_type* __from,
264 const extern_type* __end, size_t __max) const = 0;
267 do_max_length() const throw() = 0;
270 /// @brief class codecvt [22.2.1.5].
271 /// NB: Generic, mostly useless implementation.
272 template<typename _InternT, typename _ExternT, typename _StateT>
274 : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
278 typedef codecvt_base::result result;
279 typedef _InternT intern_type;
280 typedef _ExternT extern_type;
281 typedef _StateT state_type;
284 __c_locale _M_c_locale_codecvt;
287 static locale::id id;
290 codecvt(size_t __refs = 0)
291 : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
294 codecvt(__c_locale __cloc, size_t __refs = 0);
301 do_out(state_type& __state, const intern_type* __from,
302 const intern_type* __from_end, const intern_type*& __from_next,
303 extern_type* __to, extern_type* __to_end,
304 extern_type*& __to_next) const;
307 do_unshift(state_type& __state, extern_type* __to,
308 extern_type* __to_end, extern_type*& __to_next) const;
311 do_in(state_type& __state, const extern_type* __from,
312 const extern_type* __from_end, const extern_type*& __from_next,
313 intern_type* __to, intern_type* __to_end,
314 intern_type*& __to_next) const;
317 do_encoding() const throw();
320 do_always_noconv() const throw();
323 do_length(state_type&, const extern_type* __from,
324 const extern_type* __end, size_t __max) const;
327 do_max_length() const throw();
330 template<typename _InternT, typename _ExternT, typename _StateT>
331 locale::id codecvt<_InternT, _ExternT, _StateT>::id;
333 /// @brief class codecvt<char, char, mbstate_t> specialization.
335 class codecvt<char, char, mbstate_t>
336 : public __codecvt_abstract_base<char, char, mbstate_t>
340 typedef char intern_type;
341 typedef char extern_type;
342 typedef mbstate_t state_type;
345 __c_locale _M_c_locale_codecvt;
348 static locale::id id;
351 codecvt(size_t __refs = 0);
354 codecvt(__c_locale __cloc, size_t __refs = 0);
361 do_out(state_type& __state, const intern_type* __from,
362 const intern_type* __from_end, const intern_type*& __from_next,
363 extern_type* __to, extern_type* __to_end,
364 extern_type*& __to_next) const;
367 do_unshift(state_type& __state, extern_type* __to,
368 extern_type* __to_end, extern_type*& __to_next) const;
371 do_in(state_type& __state, const extern_type* __from,
372 const extern_type* __from_end, const extern_type*& __from_next,
373 intern_type* __to, intern_type* __to_end,
374 intern_type*& __to_next) const;
377 do_encoding() const throw();
380 do_always_noconv() const throw();
383 do_length(state_type&, const extern_type* __from,
384 const extern_type* __end, size_t __max) const;
387 do_max_length() const throw();
390 #ifdef _GLIBCXX_USE_WCHAR_T
391 /// @brief class codecvt<wchar_t, char, mbstate_t> specialization.
393 class codecvt<wchar_t, char, mbstate_t>
394 : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
398 typedef wchar_t intern_type;
399 typedef char extern_type;
400 typedef mbstate_t state_type;
403 __c_locale _M_c_locale_codecvt;
406 static locale::id id;
409 codecvt(size_t __refs = 0);
412 codecvt(__c_locale __cloc, size_t __refs = 0);
419 do_out(state_type& __state, const intern_type* __from,
420 const intern_type* __from_end, const intern_type*& __from_next,
421 extern_type* __to, extern_type* __to_end,
422 extern_type*& __to_next) const;
425 do_unshift(state_type& __state,
426 extern_type* __to, extern_type* __to_end,
427 extern_type*& __to_next) const;
430 do_in(state_type& __state,
431 const extern_type* __from, const extern_type* __from_end,
432 const extern_type*& __from_next,
433 intern_type* __to, intern_type* __to_end,
434 intern_type*& __to_next) const;
437 int do_encoding() const throw();
440 bool do_always_noconv() const throw();
443 int do_length(state_type&, const extern_type* __from,
444 const extern_type* __end, size_t __max) const;
447 do_max_length() const throw();
449 #endif //_GLIBCXX_USE_WCHAR_T
451 /// @brief class codecvt_byname [22.2.1.6].
452 template<typename _InternT, typename _ExternT, typename _StateT>
453 class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
457 codecvt_byname(const char* __s, size_t __refs = 0)
458 : codecvt<_InternT, _ExternT, _StateT>(__refs)
460 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
462 this->_S_destroy_c_locale(this->_M_c_locale_codecvt);
463 this->_S_create_c_locale(this->_M_c_locale_codecvt, __s);
469 ~codecvt_byname() { }
472 _GLIBCXX_END_NAMESPACE