1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002 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 // Warning: this file is not meant for user inclusion. Use <locale>.
36 // Written by Benjamin Kosnik <bkoz@cygnus.com>
39 // Define this here to codecvt.cc can have _S_max_size definition.
40 #define _GLIBCPP_USE___ENC_TRAITS 1
42 #if _GLIBCPP_USE_SHADOW_HEADERS
43 using _C_legacy::CODESET;
46 // Extension to use icov for dealing with character encodings,
47 // including conversions and comparisons between various character
48 // sets. This object encapsulates data that may need to be shared between
49 // char_traits, codecvt and ctype.
54 // NB: A conversion descriptor subsumes and enhances the
55 // functionality of a simple state type such as mbstate_t.
56 typedef iconv_t __desc_type;
60 // Max size of charset encoding name
61 static const int _S_max_size = 32;
62 // Name of internal character set encoding.
63 char _M_int_enc[_S_max_size];
64 // Name of external character set encoding.
65 char _M_ext_enc[_S_max_size];
67 // Conversion descriptor between external encoding to internal encoding.
68 __desc_type _M_in_desc;
69 // Conversion descriptor between internal encoding to external encoding.
70 __desc_type _M_out_desc;
72 // Details the byte-order marker for the external encoding, if necessary.
75 // Details the byte-order marker for the internal encoding, if necessary.
79 explicit __enc_traits()
80 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
82 memset(_M_int_enc, 0, _S_max_size);
83 memset(_M_ext_enc, 0, _S_max_size);
86 explicit __enc_traits(const locale& __loc)
87 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
89 // __intc_end = whatever we are using internally, which is
90 // UCS4 (linux, solaris)
91 // UCS2 == UNICODE (microsoft, java, aix, whatever...)
92 // XXX Currently don't know how to get this data from target system...
93 strcpy(_M_int_enc, "UCS4");
95 // __extc_end = external codeset in current locale
96 // XXX There has got to be a better way to do this.
98 locale::facet::_S_create_c_locale(__cloc, __loc.name().c_str());
99 strcpy(_M_ext_enc, __nl_langinfo_l(CODESET, __cloc));
100 locale::facet::_S_destroy_c_locale(__cloc);
103 explicit __enc_traits(const char* __int, const char* __ext,
104 int __ibom = 0, int __ebom = 0)
105 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
107 strncpy(_M_int_enc, __int, _S_max_size);
108 strncpy(_M_ext_enc, __ext, _S_max_size);
111 // 21.1.2 traits typedefs
113 // typedef STATE_T state_type
114 // requires: state_type shall meet the requirements of
115 // CopyConstructible types (20.1.3)
116 __enc_traits(const __enc_traits& __obj): _M_in_desc(0), _M_out_desc(0)
118 strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
119 strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
120 _M_ext_bom = __obj._M_ext_bom;
121 _M_int_bom = __obj._M_int_bom;
124 // Need assignment operator as well.
126 operator=(const __enc_traits& __obj)
128 strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
129 strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
132 _M_ext_bom = __obj._M_ext_bom;
133 _M_int_bom = __obj._M_int_bom;
138 __desc_type __err = reinterpret_cast<iconv_t>(-1);
139 if (_M_in_desc && _M_in_desc != __err)
140 iconv_close(_M_in_desc);
141 if (_M_out_desc && _M_out_desc != __err)
142 iconv_close(_M_out_desc);
148 const __desc_type __err = reinterpret_cast<iconv_t>(-1);
151 _M_in_desc = iconv_open(_M_int_enc, _M_ext_enc);
152 if (_M_in_desc == __err)
153 __throw_runtime_error("creating iconv input descriptor failed.");
157 _M_out_desc = iconv_open(_M_ext_enc, _M_int_enc);
158 if (_M_out_desc == __err)
159 __throw_runtime_error("creating iconv output descriptor failed.");
166 const __desc_type __err = reinterpret_cast<iconv_t>(-1);
167 bool __test = _M_in_desc && _M_in_desc != __err;
168 __test &= _M_out_desc && _M_out_desc != __err;
173 _M_get_in_descriptor()
174 { return &_M_in_desc; }
177 _M_get_out_descriptor()
178 { return &_M_out_desc; }
181 _M_get_external_bom()
182 { return _M_ext_bom; }
185 _M_get_internal_bom()
186 { return _M_int_bom; }
189 _M_get_internal_enc()
190 { return _M_int_enc; }
193 _M_get_external_enc()
194 { return _M_ext_enc; }
197 // Partial specialization
198 // This specialization takes advantage of iconv to provide code
199 // conversions between a large number of character encodings.
200 template<typename _InternT, typename _ExternT>
201 class codecvt<_InternT, _ExternT, __enc_traits>
202 : public __codecvt_abstract_base<_InternT, _ExternT, __enc_traits>
206 typedef codecvt_base::result result;
207 typedef _InternT intern_type;
208 typedef _ExternT extern_type;
209 typedef __enc_traits state_type;
210 typedef __enc_traits::__desc_type __desc_type;
211 typedef __enc_traits __enc_type;
214 static locale::id id;
217 codecvt(size_t __refs = 0)
218 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
222 codecvt(__enc_type* __enc, size_t __refs = 0)
223 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
231 do_out(state_type& __state, const intern_type* __from,
232 const intern_type* __from_end, const intern_type*& __from_next,
233 extern_type* __to, extern_type* __to_end,
234 extern_type*& __to_next) const;
237 do_unshift(state_type& __state, extern_type* __to,
238 extern_type* __to_end, extern_type*& __to_next) const;
241 do_in(state_type& __state, const extern_type* __from,
242 const extern_type* __from_end, const extern_type*& __from_next,
243 intern_type* __to, intern_type* __to_end,
244 intern_type*& __to_next) const;
247 do_encoding() const throw();
250 do_always_noconv() const throw();
253 do_length(const state_type&, const extern_type* __from,
254 const extern_type* __end, size_t __max) const;
257 do_max_length() const throw();
260 template<typename _InternT, typename _ExternT>
262 codecvt<_InternT, _ExternT, __enc_traits>::id;
264 // This adaptor works around the signature problems of the second
265 // argument to iconv(): SUSv2 and others use 'const char**', but glibc 2.2
266 // uses 'char**', which matches the POSIX 1003.1-2001 standard.
267 // Using this adaptor, g++ will do the work for us.
268 template<typename _T>
270 __iconv_adaptor(size_t(*__func)(iconv_t, _T, size_t*, char**, size_t*),
271 iconv_t __cd, char** __inbuf, size_t* __inbytes,
272 char** __outbuf, size_t* __outbytes)
273 { return __func(__cd, (_T)__inbuf, __inbytes, __outbuf, __outbytes); }
275 template<typename _InternT, typename _ExternT>
277 codecvt<_InternT, _ExternT, __enc_traits>::
278 do_out(state_type& __state, const intern_type* __from,
279 const intern_type* __from_end, const intern_type*& __from_next,
280 extern_type* __to, extern_type* __to_end,
281 extern_type*& __to_next) const
283 result __ret = error;
284 if (__state._M_good())
286 typedef state_type::__desc_type __desc_type;
287 const __desc_type* __desc = __state._M_get_out_descriptor();
288 const size_t __fmultiple = sizeof(intern_type) / sizeof(char);
289 size_t __fbytes = __fmultiple * (__from_end - __from);
290 const size_t __tmultiple = sizeof(extern_type) / sizeof(char);
291 size_t __tbytes = __tmultiple * (__to_end - __to);
293 // Argument list for iconv specifies a byte sequence. Thus,
294 // all to/from arrays must be brutally casted to char*.
295 char* __cto = reinterpret_cast<char*>(__to);
299 // Some encodings need a byte order marker as the first item
300 // in the byte stream, to designate endian-ness. The default
301 // value for the byte order marker is NULL, so if this is
302 // the case, it's not necessary and we can just go on our
304 int __int_bom = __state._M_get_internal_bom();
307 size_t __size = __from_end - __from;
308 intern_type* __cfixed = static_cast<intern_type*>(__builtin_alloca(sizeof(intern_type) * (__size + 1)));
309 __cfixed[0] = static_cast<intern_type>(__int_bom);
310 char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
311 __cfrom = reinterpret_cast<char*>(__cfixed);
312 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
313 &__fbytes, &__cto, &__tbytes);
317 intern_type* __cfixed = const_cast<intern_type*>(__from);
318 __cfrom = reinterpret_cast<char*>(__cfixed);
319 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom, &__fbytes,
323 if (__conv != size_t(-1))
325 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
326 __to_next = reinterpret_cast<extern_type*>(__cto);
331 if (__fbytes < __fmultiple * (__from_end - __from))
333 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
334 __to_next = reinterpret_cast<extern_type*>(__cto);
344 template<typename _InternT, typename _ExternT>
346 codecvt<_InternT, _ExternT, __enc_traits>::
347 do_unshift(state_type& __state, extern_type* __to,
348 extern_type* __to_end, extern_type*& __to_next) const
350 result __ret = error;
351 if (__state._M_good())
353 typedef state_type::__desc_type __desc_type;
354 const __desc_type* __desc = __state._M_get_in_descriptor();
355 const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
356 size_t __tlen = __tmultiple * (__to_end - __to);
358 // Argument list for iconv specifies a byte sequence. Thus,
359 // all to/from arrays must be brutally casted to char*.
360 char* __cto = reinterpret_cast<char*>(__to);
361 size_t __conv = __iconv_adaptor(iconv,*__desc, NULL, NULL,
364 if (__conv != size_t(-1))
366 __to_next = reinterpret_cast<extern_type*>(__cto);
367 if (__tlen == __tmultiple * (__to_end - __to))
369 else if (__tlen == 0)
380 template<typename _InternT, typename _ExternT>
382 codecvt<_InternT, _ExternT, __enc_traits>::
383 do_in(state_type& __state, const extern_type* __from,
384 const extern_type* __from_end, const extern_type*& __from_next,
385 intern_type* __to, intern_type* __to_end,
386 intern_type*& __to_next) const
388 result __ret = error;
389 if (__state._M_good())
391 typedef state_type::__desc_type __desc_type;
392 const __desc_type* __desc = __state._M_get_in_descriptor();
393 const size_t __fmultiple = sizeof(extern_type) / sizeof(char);
394 size_t __flen = __fmultiple * (__from_end - __from);
395 const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
396 size_t __tlen = __tmultiple * (__to_end - __to);
398 // Argument list for iconv specifies a byte sequence. Thus,
399 // all to/from arrays must be brutally casted to char*.
400 char* __cto = reinterpret_cast<char*>(__to);
404 // Some encodings need a byte order marker as the first item
405 // in the byte stream, to designate endian-ness. The default
406 // value for the byte order marker is NULL, so if this is
407 // the case, it's not necessary and we can just go on our
409 int __ext_bom = __state._M_get_external_bom();
412 size_t __size = __from_end - __from;
413 extern_type* __cfixed = static_cast<extern_type*>(__builtin_alloca(sizeof(extern_type) * (__size + 1)));
414 __cfixed[0] = static_cast<extern_type>(__ext_bom);
415 char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
416 __cfrom = reinterpret_cast<char*>(__cfixed);
417 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
418 &__flen, &__cto, &__tlen);
422 extern_type* __cfixed = const_cast<extern_type*>(__from);
423 __cfrom = reinterpret_cast<char*>(__cfixed);
424 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
425 &__flen, &__cto, &__tlen);
429 if (__conv != size_t(-1))
431 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
432 __to_next = reinterpret_cast<intern_type*>(__cto);
437 if (__flen < static_cast<size_t>(__from_end - __from))
439 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
440 __to_next = reinterpret_cast<intern_type*>(__cto);
450 template<typename _InternT, typename _ExternT>
452 codecvt<_InternT, _ExternT, __enc_traits>::
453 do_encoding() const throw()
456 if (sizeof(_ExternT) <= sizeof(_InternT))
457 __ret = sizeof(_InternT)/sizeof(_ExternT);
461 template<typename _InternT, typename _ExternT>
463 codecvt<_InternT, _ExternT, __enc_traits>::
464 do_always_noconv() const throw()
467 template<typename _InternT, typename _ExternT>
469 codecvt<_InternT, _ExternT, __enc_traits>::
470 do_length(const state_type&, const extern_type* __from,
471 const extern_type* __end, size_t __max) const
472 { return min(__max, static_cast<size_t>(__end - __from)); }
474 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
475 // 74. Garbled text for codecvt::do_max_length
476 template<typename _InternT, typename _ExternT>
478 codecvt<_InternT, _ExternT, __enc_traits>::
479 do_max_length() const throw()