1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000, 2001 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 // __enc_traits may need to move up the locale header hierarchy,
40 // depending on if ctype ends up using it.
42 // Extensions to use icov for dealing with character encodings,
43 // including conversions and comparisons between various character
44 // sets. This object encapsulates data that may need to be shared between
45 // char_traits, codecvt and ctype.
47 #if _GLIBCPP_USE_SHADOW_HEADERS
48 using _C_legacy::CODESET;
52 // Define this here to codecvt.cc can have _S_max_size definition.
53 #define _GLIBCPP_USE___ENC_TRAITS 1
59 // NB: A conversion descriptor subsumes and enhances the
60 // functionality of a simple state type such as mbstate_t.
61 typedef iconv_t __desc_type;
65 // Max size of charset encoding name
66 static const int _S_max_size = 32;
67 // Name of internal character set encoding.
68 char _M_int_enc[_S_max_size];
69 // Name of external character set encoding.
70 char _M_ext_enc[_S_max_size];
72 // Conversion descriptor between external encoding to internal encoding.
73 __desc_type _M_in_desc;
74 // Conversion descriptor between internal encoding to external encoding.
75 __desc_type _M_out_desc;
77 // Details the byte-order marker for the external encoding, if necessary.
80 // Details the byte-order marker for the internal encoding, if necessary.
84 __enc_traits(const locale& __loc = locale())
85 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
87 // __intc_end = whatever we are using internally, which is
89 // UCS2 == UNICODE (microsoft, java, aix, whatever...)
90 // XXX Currently don't know how to get this data from target system...
91 strcpy(_M_int_enc, "UCS4");
93 // __extc_end = external codeset in current locale
94 // XXX There has got to be a better way to do this.
96 locale::facet::_S_create_c_locale(__cloc, __loc.name().c_str());
97 strcpy(_M_ext_enc, __nl_langinfo_l(CODESET, __cloc));
98 locale::facet::_S_destroy_c_locale(__cloc);
101 __enc_traits(const char* __int, const char* __ext, int __ibom = 0,
103 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
105 strncpy(_M_int_enc, __int, _S_max_size);
106 strncpy(_M_ext_enc, __ext, _S_max_size);
109 // 21.1.2 traits typedefs
111 // typedef STATE_T state_type
112 // requires: state_type shall meet the requirements of
113 // CopyConstructible types (20.1.3)
114 __enc_traits(const __enc_traits& __obj)
116 strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
117 strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
118 _M_ext_bom = __obj._M_ext_bom;
119 _M_int_bom = __obj._M_int_bom;
124 iconv_close(_M_in_desc);
125 iconv_close(_M_out_desc);
132 _M_in_desc = iconv_open(_M_int_enc, _M_ext_enc);
133 _M_out_desc = iconv_open(_M_ext_enc, _M_int_enc);
134 if (_M_out_desc == iconv_t(-1) || _M_in_desc == iconv_t(-1))
136 // XXX Extended error checking.
143 return _M_out_desc && _M_in_desc
144 && _M_out_desc != iconv_t(-1) && _M_in_desc != iconv_t(-1);
148 _M_get_in_descriptor()
149 { return &_M_in_desc; }
152 _M_get_out_descriptor()
153 { return &_M_out_desc; }
156 _M_get_internal_enc()
157 { return _M_int_enc; }
160 _M_get_external_enc()
161 { return _M_ext_enc; }
164 _M_get_external_bom()
165 { return _M_ext_bom; }
168 _M_get_internal_bom()
169 { return _M_int_bom; }
172 // Partial specialization
173 // This specialization takes advantage of iconv to provide code
174 // conversions between a large number of character encodings.
175 template<typename _InternT, typename _ExternT>
176 class codecvt<_InternT, _ExternT, __enc_traits>
177 : public __codecvt_abstract_base<_InternT, _ExternT, __enc_traits>
181 typedef codecvt_base::result result;
182 typedef _InternT intern_type;
183 typedef _ExternT extern_type;
184 typedef __enc_traits state_type;
185 typedef __enc_traits::__desc_type __desc_type;
186 typedef __enc_traits __enc_type;
189 static locale::id id;
192 codecvt(size_t __refs = 0)
193 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
197 codecvt(__enc_type* __enc, size_t __refs = 0)
198 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
206 do_out(state_type& __state, const intern_type* __from,
207 const intern_type* __from_end, const intern_type*& __from_next,
208 extern_type* __to, extern_type* __to_end,
209 extern_type*& __to_next) const;
212 do_unshift(state_type& __state, extern_type* __to,
213 extern_type* __to_end, extern_type*& __to_next) const;
216 do_in(state_type& __state, const extern_type* __from,
217 const extern_type* __from_end, const extern_type*& __from_next,
218 intern_type* __to, intern_type* __to_end,
219 intern_type*& __to_next) const;
222 do_encoding() const throw();
225 do_always_noconv() const throw();
228 do_length(const state_type&, const extern_type* __from,
229 const extern_type* __end, size_t __max) const;
232 do_max_length() const throw();
235 template<typename _InternT, typename _ExternT>
237 codecvt<_InternT, _ExternT, __enc_traits>::id;
239 // This adaptor works around the signature problems of the second
240 // argument to iconv(): SUSv2 and others use 'const char**', but glibc 2.2
241 // uses 'char**', which is what the standard is (apparently) due to use
242 // in the future. Using this adaptor, g++ will do the work for us.
243 template<typename _T>
245 __iconv_adaptor(size_t(*iconv_func)(iconv_t, _T, size_t*, char**, size_t*),
246 iconv_t cd, char** inbuf, size_t* inbytesleft,
247 char** outbuf, size_t* outbytesleft)
249 return iconv_func(cd, (_T)inbuf, inbytesleft, outbuf, outbytesleft);
252 template<typename _InternT, typename _ExternT>
254 codecvt<_InternT, _ExternT, __enc_traits>::
255 do_out(state_type& __state, const intern_type* __from,
256 const intern_type* __from_end, const intern_type*& __from_next,
257 extern_type* __to, extern_type* __to_end,
258 extern_type*& __to_next) const
260 result __ret = error;
261 if (__state._M_good())
263 typedef state_type::__desc_type __desc_type;
264 const __desc_type* __desc = __state._M_get_out_descriptor();
265 const size_t __fmultiple = sizeof(intern_type) / sizeof(char);
266 size_t __flen = __fmultiple * (__from_end - __from);
267 const size_t __tmultiple = sizeof(extern_type) / sizeof(char);
268 size_t __tlen = __tmultiple * (__to_end - __to);
270 // Argument list for iconv specifies a byte sequence. Thus,
271 // all to/from arrays must be brutally casted to char*.
272 char* __cto = reinterpret_cast<char*>(__to);
276 // Some encodings need a byte order marker as the first item
277 // in the byte stream, to designate endian-ness. The default
278 // value for the byte order marker is NULL, so if this is
279 // the case, it's not necessary and we can just go on our
281 int __int_bom = __state._M_get_internal_bom();
284 size_t __size = __from_end - __from;
285 intern_type* __cfixed = static_cast<intern_type*>(__builtin_alloca(sizeof(intern_type) * (__size + 1)));
286 __cfixed[0] = static_cast<intern_type>(__int_bom);
287 char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
288 __cfrom = reinterpret_cast<char*>(__cfixed);
289 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
290 &__flen, &__cto, &__tlen);
294 intern_type* __cfixed = const_cast<intern_type*>(__from);
295 __cfrom = reinterpret_cast<char*>(__cfixed);
296 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
297 &__flen, &__cto, &__tlen);
300 if (__conv != size_t(-1))
302 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
303 __to_next = reinterpret_cast<extern_type*>(__cto);
308 if (__flen < static_cast<size_t>(__from_end - __from))
310 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
311 __to_next = reinterpret_cast<extern_type*>(__cto);
321 template<typename _InternT, typename _ExternT>
323 codecvt<_InternT, _ExternT, __enc_traits>::
324 do_unshift(state_type& __state, extern_type* __to,
325 extern_type* __to_end, extern_type*& __to_next) const
327 result __ret = error;
328 if (__state._M_good())
330 typedef state_type::__desc_type __desc_type;
331 const __desc_type* __desc = __state._M_get_in_descriptor();
332 const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
333 size_t __tlen = __tmultiple * (__to_end - __to);
335 // Argument list for iconv specifies a byte sequence. Thus,
336 // all to/from arrays must be brutally casted to char*.
337 char* __cto = reinterpret_cast<char*>(__to);
338 size_t __conv = __iconv_adaptor(iconv,*__desc, NULL, NULL,
341 if (__conv != size_t(-1))
343 __to_next = reinterpret_cast<extern_type*>(__cto);
344 if (__tlen == __tmultiple * (__to_end - __to))
346 else if (__tlen == 0)
357 template<typename _InternT, typename _ExternT>
359 codecvt<_InternT, _ExternT, __enc_traits>::
360 do_in(state_type& __state, const extern_type* __from,
361 const extern_type* __from_end, const extern_type*& __from_next,
362 intern_type* __to, intern_type* __to_end,
363 intern_type*& __to_next) const
365 result __ret = error;
366 if (__state._M_good())
368 typedef state_type::__desc_type __desc_type;
369 const __desc_type* __desc = __state._M_get_in_descriptor();
370 const size_t __fmultiple = sizeof(extern_type) / sizeof(char);
371 size_t __flen = __fmultiple * (__from_end - __from);
372 const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
373 size_t __tlen = __tmultiple * (__to_end - __to);
375 // Argument list for iconv specifies a byte sequence. Thus,
376 // all to/from arrays must be brutally casted to char*.
377 char* __cto = reinterpret_cast<char*>(__to);
381 // Some encodings need a byte order marker as the first item
382 // in the byte stream, to designate endian-ness. The default
383 // value for the byte order marker is NULL, so if this is
384 // the case, it's not necessary and we can just go on our
386 int __ext_bom = __state._M_get_external_bom();
389 size_t __size = __from_end - __from;
390 extern_type* __cfixed = static_cast<extern_type*>(__builtin_alloca(sizeof(extern_type) * (__size + 1)));
391 __cfixed[0] = static_cast<extern_type>(__ext_bom);
392 char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
393 __cfrom = reinterpret_cast<char*>(__cfixed);
394 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
395 &__flen, &__cto, &__tlen);
399 extern_type* __cfixed = const_cast<extern_type*>(__from);
400 __cfrom = reinterpret_cast<char*>(__cfixed);
401 __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
402 &__flen, &__cto, &__tlen);
406 if (__conv != size_t(-1))
408 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
409 __to_next = reinterpret_cast<intern_type*>(__cto);
414 if (__flen < static_cast<size_t>(__from_end - __from))
416 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
417 __to_next = reinterpret_cast<intern_type*>(__cto);
427 template<typename _InternT, typename _ExternT>
429 codecvt<_InternT, _ExternT, __enc_traits>::
430 do_encoding() const throw()
433 template<typename _InternT, typename _ExternT>
435 codecvt<_InternT, _ExternT, __enc_traits>::
436 do_always_noconv() const throw()
439 template<typename _InternT, typename _ExternT>
441 codecvt<_InternT, _ExternT, __enc_traits>::
442 do_length(const state_type&, const extern_type* __from,
443 const extern_type* __end, size_t __max) const
444 { return min(__max, static_cast<size_t>(__end - __from)); }
446 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
447 // 74. Garbled text for codecvt::do_max_length
448 template<typename _InternT, typename _ExternT>
450 codecvt<_InternT, _ExternT, __enc_traits>::
451 do_max_length() const throw()