1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 22.2.1.5 Template class codecvt
34 // Written by Benjamin Kosnik <bkoz@cygnus.com>
37 * This is an internal header file, included by other library headers.
38 * You should not attempt to use it directly.
44 #pragma GCC system_header
46 // 22.2.1.5 Template class codecvt
47 /// Base class for codecvt facet providing conversion result enum.
60 // Template class __codecvt_abstract_base
61 // NB: An abstract base class that fills in the public inlines, so
62 // that the specializations don't have to re-copy the public
65 * @brief Common base for codecvt facet
67 * This template class provides implementations of the public functions
68 * that forward to the protected virtual functions.
70 * This template also provides abstract stubs for the protected virtual
73 template<typename _InternT
, typename _ExternT
, typename _StateT
>
74 class __codecvt_abstract_base
75 : public locale::facet
, public codecvt_base
79 typedef codecvt_base::result result
;
80 typedef _InternT intern_type
;
81 typedef _ExternT extern_type
;
82 typedef _StateT state_type
;
84 // 22.2.1.5.1 codecvt members
86 * @brief Convert from internal to external character set.
88 * Converts input string of intern_type to output string of
89 * extern_type. This is analogous to wcsrtombs. It does this by
90 * calling codecvt::do_out.
92 * The source and destination character sets are determined by the
93 * facet's locale, internal and external types.
95 * The characters in [from,from_end) are converted and written to
96 * [to,to_end). from_next and to_next are set to point to the
97 * character following the last successfully converted character,
98 * respectively. If the result needed no conversion, from_next and
99 * to_next are not affected.
101 * The @a state argument should be intialized if the input is at the
102 * beginning and carried from a previous call if continuing
103 * conversion. There are no guarantees about how @a state is used.
105 * The result returned is a member of codecvt_base::result. If all the
106 * input is converted, returns codecvt_base::ok. If no conversion is
107 * necessary, returns codecvt_base::noconv. If the input ends early or
108 * there is insufficient space in the output, returns codecvt_base::partial.
109 * Otherwise the conversion failed and codecvt_base::error is returned.
111 * @param state Persistent conversion state data.
112 * @param from Start of input.
113 * @param from_end End of input.
114 * @param from_next Returns start of unconverted data.
115 * @param to Start of output buffer.
116 * @param to_end End of output buffer.
117 * @param to_next Returns start of unused output area.
118 * @return codecvt_base::result.
121 out(state_type
& __state
, const intern_type
* __from
,
122 const intern_type
* __from_end
, const intern_type
*& __from_next
,
123 extern_type
* __to
, extern_type
* __to_end
,
124 extern_type
*& __to_next
) const
126 return this->do_out(__state
, __from
, __from_end
, __from_next
,
127 __to
, __to_end
, __to_next
);
131 * @brief Reset conversion state.
133 * Writes characters to output that would restore @a state to initial
134 * conditions. The idea is that if a partial conversion occurs, then
135 * the converting the characters written by this function would leave
136 * the state in initial conditions, rather than partial conversion
137 * state. It does this by calling codecvt::do_unshift().
139 * For example, if 4 external characters always converted to 1 internal
140 * character, and input to in() had 6 external characters with state
141 * saved, this function would write two characters to the output and
142 * set the state to initialized conditions.
144 * The source and destination character sets are determined by the
145 * facet's locale, internal and external types.
147 * The result returned is a member of codecvt_base::result. If the
148 * state could be reset and data written, returns codecvt_base::ok. If
149 * no conversion is necessary, returns codecvt_base::noconv. If the
150 * output has insufficient space, returns codecvt_base::partial.
151 * Otherwise the reset failed and codecvt_base::error is returned.
153 * @param state Persistent conversion state data.
154 * @param to Start of output buffer.
155 * @param to_end End of output buffer.
156 * @param to_next Returns start of unused output area.
157 * @return codecvt_base::result.
160 unshift(state_type
& __state
, extern_type
* __to
, extern_type
* __to_end
,
161 extern_type
*& __to_next
) const
162 { return this->do_unshift(__state
, __to
,__to_end
,__to_next
); }
165 * @brief Convert from external to internal character set.
167 * Converts input string of extern_type to output string of
168 * intern_type. This is analogous to mbsrtowcs. It does this by
169 * calling codecvt::do_in.
171 * The source and destination character sets are determined by the
172 * facet's locale, internal and external types.
174 * The characters in [from,from_end) are converted and written to
175 * [to,to_end). from_next and to_next are set to point to the
176 * character following the last successfully converted character,
177 * respectively. If the result needed no conversion, from_next and
178 * to_next are not affected.
180 * The @a state argument should be intialized if the input is at the
181 * beginning and carried from a previous call if continuing
182 * conversion. There are no guarantees about how @a state is used.
184 * The result returned is a member of codecvt_base::result. If all the
185 * input is converted, returns codecvt_base::ok. If no conversion is
186 * necessary, returns codecvt_base::noconv. If the input ends early or
187 * there is insufficient space in the output, returns codecvt_base::partial.
188 * Otherwise the 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 // 22.2.1.5 Template class codecvt
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 // codecvt<char, char, mbstate_t> required 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 // codecvt<wchar_t, char, mbstate_t> required 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 // 22.2.1.6 Template class codecvt_byname
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 // Include host and configuration specific partial specializations
473 // with additional functionality, if possible.
474 #ifdef _GLIBCXX_USE_WCHAR_T
475 #include <bits/codecvt_specializations.h>