2004-11-22 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / codecvt.h
blob51ad4dd7d1f125cf530b7b4fba9cf3205df4e9a3
1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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>
36 /** @file bits/codecvt.h
37 * This is an internal header file, included by other library headers.
38 * You should not attempt to use it directly.
41 #ifndef _CODECVT_H
42 #define _CODECVT_H 1
44 #pragma GCC system_header
46 /// @brief Empty base class for codecvt facet [22.2.1.5].
47 class codecvt_base
49 public:
50 enum result
52 ok,
53 partial,
54 error,
55 noconv
59 /**
60 * @brief Common base for codecvt functions.
62 * This template class provides implementations of the public functions
63 * that forward to the protected virtual functions.
65 * This template also provides abstract stubs for the protected virtual
66 * functions.
68 template<typename _InternT, typename _ExternT, typename _StateT>
69 class __codecvt_abstract_base
70 : public locale::facet, public codecvt_base
72 public:
73 // Types:
74 typedef codecvt_base::result result;
75 typedef _InternT intern_type;
76 typedef _ExternT extern_type;
77 typedef _StateT state_type;
79 // 22.2.1.5.1 codecvt members
80 /**
81 * @brief Convert from internal to external character set.
83 * Converts input string of intern_type to output string of
84 * extern_type. This is analogous to wcsrtombs. It does this by
85 * calling codecvt::do_out.
87 * The source and destination character sets are determined by the
88 * facet's locale, internal and external types.
90 * The characters in [from,from_end) are converted and written to
91 * [to,to_end). from_next and to_next are set to point to the
92 * character following the last successfully converted character,
93 * respectively. If the result needed no conversion, from_next and
94 * to_next are not affected.
96 * The @a state argument should be intialized if the input is at the
97 * beginning and carried from a previous call if continuing
98 * conversion. There are no guarantees about how @a state is used.
100 * The result returned is a member of codecvt_base::result. If
101 * all the input is converted, returns codecvt_base::ok. If no
102 * conversion is necessary, returns codecvt_base::noconv. If
103 * the input ends early or there is insufficient space in the
104 * output, returns codecvt_base::partial. Otherwise the
105 * conversion failed and codecvt_base::error is returned.
107 * @param state Persistent conversion state data.
108 * @param from Start of input.
109 * @param from_end End of input.
110 * @param from_next Returns start of unconverted data.
111 * @param to Start of output buffer.
112 * @param to_end End of output buffer.
113 * @param to_next Returns start of unused output area.
114 * @return codecvt_base::result.
116 result
117 out(state_type& __state, const intern_type* __from,
118 const intern_type* __from_end, const intern_type*& __from_next,
119 extern_type* __to, extern_type* __to_end,
120 extern_type*& __to_next) const
122 return this->do_out(__state, __from, __from_end, __from_next,
123 __to, __to_end, __to_next);
127 * @brief Reset conversion state.
129 * Writes characters to output that would restore @a state to initial
130 * conditions. The idea is that if a partial conversion occurs, then
131 * the converting the characters written by this function would leave
132 * the state in initial conditions, rather than partial conversion
133 * state. It does this by calling codecvt::do_unshift().
135 * For example, if 4 external characters always converted to 1 internal
136 * character, and input to in() had 6 external characters with state
137 * saved, this function would write two characters to the output and
138 * set the state to initialized conditions.
140 * The source and destination character sets are determined by the
141 * facet's locale, internal and external types.
143 * The result returned is a member of codecvt_base::result. If the
144 * state could be reset and data written, returns codecvt_base::ok. If
145 * no conversion is necessary, returns codecvt_base::noconv. If the
146 * output has insufficient space, returns codecvt_base::partial.
147 * Otherwise the reset failed and codecvt_base::error is returned.
149 * @param state Persistent conversion state data.
150 * @param to Start of output buffer.
151 * @param to_end End of output buffer.
152 * @param to_next Returns start of unused output area.
153 * @return codecvt_base::result.
155 result
156 unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
157 extern_type*& __to_next) const
158 { return this->do_unshift(__state, __to,__to_end,__to_next); }
161 * @brief Convert from external to internal character set.
163 * Converts input string of extern_type to output string of
164 * intern_type. This is analogous to mbsrtowcs. It does this by
165 * calling codecvt::do_in.
167 * The source and destination character sets are determined by the
168 * facet's locale, internal and external types.
170 * The characters in [from,from_end) are converted and written to
171 * [to,to_end). from_next and to_next are set to point to the
172 * character following the last successfully converted character,
173 * respectively. If the result needed no conversion, from_next and
174 * to_next are not affected.
176 * The @a state argument should be intialized if the input is at the
177 * beginning and carried from a previous call if continuing
178 * conversion. There are no guarantees about how @a state is used.
180 * The result returned is a member of codecvt_base::result. If
181 * all the input is converted, returns codecvt_base::ok. If no
182 * conversion is necessary, returns codecvt_base::noconv. If
183 * the input ends early or there is insufficient space in the
184 * output, returns codecvt_base::partial. Otherwise the
185 * conversion failed and codecvt_base::error is returned.
187 * @param state Persistent conversion state data.
188 * @param from Start of input.
189 * @param from_end End of input.
190 * @param from_next Returns start of unconverted data.
191 * @param to Start of output buffer.
192 * @param to_end End of output buffer.
193 * @param to_next Returns start of unused output area.
194 * @return codecvt_base::result.
196 result
197 in(state_type& __state, const extern_type* __from,
198 const extern_type* __from_end, const extern_type*& __from_next,
199 intern_type* __to, intern_type* __to_end,
200 intern_type*& __to_next) const
202 return this->do_in(__state, __from, __from_end, __from_next,
203 __to, __to_end, __to_next);
207 encoding() const throw()
208 { return this->do_encoding(); }
210 bool
211 always_noconv() const throw()
212 { return this->do_always_noconv(); }
215 length(state_type& __state, const extern_type* __from,
216 const extern_type* __end, size_t __max) const
217 { return this->do_length(__state, __from, __end, __max); }
220 max_length() const throw()
221 { return this->do_max_length(); }
223 protected:
224 explicit
225 __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
227 virtual
228 ~__codecvt_abstract_base() { }
231 * @brief Convert from internal to external character set.
233 * Converts input string of intern_type to output string of
234 * extern_type. This function is a hook for derived classes to change
235 * the value returned. @see out for more information.
237 virtual result
238 do_out(state_type& __state, const intern_type* __from,
239 const intern_type* __from_end, const intern_type*& __from_next,
240 extern_type* __to, extern_type* __to_end,
241 extern_type*& __to_next) const = 0;
243 virtual result
244 do_unshift(state_type& __state, extern_type* __to,
245 extern_type* __to_end, extern_type*& __to_next) const = 0;
247 virtual result
248 do_in(state_type& __state, const extern_type* __from,
249 const extern_type* __from_end, const extern_type*& __from_next,
250 intern_type* __to, intern_type* __to_end,
251 intern_type*& __to_next) const = 0;
253 virtual int
254 do_encoding() const throw() = 0;
256 virtual bool
257 do_always_noconv() const throw() = 0;
259 virtual int
260 do_length(state_type&, const extern_type* __from,
261 const extern_type* __end, size_t __max) const = 0;
263 virtual int
264 do_max_length() const throw() = 0;
267 /// @brief class codecvt [22.2.1.5].
268 /// NB: Generic, mostly useless implementation.
269 template<typename _InternT, typename _ExternT, typename _StateT>
270 class codecvt
271 : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
273 public:
274 // Types:
275 typedef codecvt_base::result result;
276 typedef _InternT intern_type;
277 typedef _ExternT extern_type;
278 typedef _StateT state_type;
280 protected:
281 __c_locale _M_c_locale_codecvt;
283 public:
284 static locale::id id;
286 explicit
287 codecvt(size_t __refs = 0)
288 : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
290 explicit
291 codecvt(__c_locale __cloc, size_t __refs = 0);
293 protected:
294 virtual
295 ~codecvt() { }
297 virtual result
298 do_out(state_type& __state, const intern_type* __from,
299 const intern_type* __from_end, const intern_type*& __from_next,
300 extern_type* __to, extern_type* __to_end,
301 extern_type*& __to_next) const;
303 virtual result
304 do_unshift(state_type& __state, extern_type* __to,
305 extern_type* __to_end, extern_type*& __to_next) const;
307 virtual result
308 do_in(state_type& __state, const extern_type* __from,
309 const extern_type* __from_end, const extern_type*& __from_next,
310 intern_type* __to, intern_type* __to_end,
311 intern_type*& __to_next) const;
313 virtual int
314 do_encoding() const throw();
316 virtual bool
317 do_always_noconv() const throw();
319 virtual int
320 do_length(state_type&, const extern_type* __from,
321 const extern_type* __end, size_t __max) const;
323 virtual int
324 do_max_length() const throw();
327 template<typename _InternT, typename _ExternT, typename _StateT>
328 locale::id codecvt<_InternT, _ExternT, _StateT>::id;
330 /// @brief class codecvt<char, char, mbstate_t> specialization.
331 template<>
332 class codecvt<char, char, mbstate_t>
333 : public __codecvt_abstract_base<char, char, mbstate_t>
335 public:
336 // Types:
337 typedef char intern_type;
338 typedef char extern_type;
339 typedef mbstate_t state_type;
341 protected:
342 __c_locale _M_c_locale_codecvt;
344 public:
345 static locale::id id;
347 explicit
348 codecvt(size_t __refs = 0);
350 explicit
351 codecvt(__c_locale __cloc, size_t __refs = 0);
353 protected:
354 virtual
355 ~codecvt();
357 virtual result
358 do_out(state_type& __state, const intern_type* __from,
359 const intern_type* __from_end, const intern_type*& __from_next,
360 extern_type* __to, extern_type* __to_end,
361 extern_type*& __to_next) const;
363 virtual result
364 do_unshift(state_type& __state, extern_type* __to,
365 extern_type* __to_end, extern_type*& __to_next) const;
367 virtual result
368 do_in(state_type& __state, const extern_type* __from,
369 const extern_type* __from_end, const extern_type*& __from_next,
370 intern_type* __to, intern_type* __to_end,
371 intern_type*& __to_next) const;
373 virtual int
374 do_encoding() const throw();
376 virtual bool
377 do_always_noconv() const throw();
379 virtual int
380 do_length(state_type&, const extern_type* __from,
381 const extern_type* __end, size_t __max) const;
383 virtual int
384 do_max_length() const throw();
387 #ifdef _GLIBCXX_USE_WCHAR_T
388 /// @brief class codecvt<wchar_t, char, mbstate_t> specialization.
389 template<>
390 class codecvt<wchar_t, char, mbstate_t>
391 : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
393 public:
394 // Types:
395 typedef wchar_t intern_type;
396 typedef char extern_type;
397 typedef mbstate_t state_type;
399 protected:
400 __c_locale _M_c_locale_codecvt;
402 public:
403 static locale::id id;
405 explicit
406 codecvt(size_t __refs = 0);
408 explicit
409 codecvt(__c_locale __cloc, size_t __refs = 0);
411 protected:
412 virtual
413 ~codecvt();
415 virtual result
416 do_out(state_type& __state, const intern_type* __from,
417 const intern_type* __from_end, const intern_type*& __from_next,
418 extern_type* __to, extern_type* __to_end,
419 extern_type*& __to_next) const;
421 virtual result
422 do_unshift(state_type& __state,
423 extern_type* __to, extern_type* __to_end,
424 extern_type*& __to_next) const;
426 virtual result
427 do_in(state_type& __state,
428 const extern_type* __from, const extern_type* __from_end,
429 const extern_type*& __from_next,
430 intern_type* __to, intern_type* __to_end,
431 intern_type*& __to_next) const;
433 virtual
434 int do_encoding() const throw();
436 virtual
437 bool do_always_noconv() const throw();
439 virtual
440 int do_length(state_type&, const extern_type* __from,
441 const extern_type* __end, size_t __max) const;
443 virtual int
444 do_max_length() const throw();
446 #endif //_GLIBCXX_USE_WCHAR_T
448 /// @brief class codecvt_byname [22.2.1.6].
449 template<typename _InternT, typename _ExternT, typename _StateT>
450 class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
452 public:
453 explicit
454 codecvt_byname(const char* __s, size_t __refs = 0)
455 : codecvt<_InternT, _ExternT, _StateT>(__refs)
457 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
459 this->_S_destroy_c_locale(this->_M_c_locale_codecvt);
460 this->_S_create_c_locale(this->_M_c_locale_codecvt, __s);
464 protected:
465 virtual
466 ~codecvt_byname() { }
469 // Include host and configuration specific partial specializations
470 // with additional functionality, if possible.
471 #ifdef _GLIBCXX_USE_WCHAR_T
472 #include <bits/codecvt_specializations.h>
473 #endif
475 #endif // _CODECVT_H