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
88 // UCS4 (linux, solaris)
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 __desc_type __err
= reinterpret_cast<iconv_t
>(-1);
125 if (_M_in_desc
&& _M_in_desc
!= __err
)
126 iconv_close(_M_in_desc
);
127 if (_M_out_desc
&& _M_out_desc
!= __err
)
128 iconv_close(_M_out_desc
);
134 __desc_type __err
= reinterpret_cast<iconv_t
>(-1);
135 _M_in_desc
= iconv_open(_M_int_enc
, _M_ext_enc
);
136 if (_M_in_desc
== __err
)
137 __throw_runtime_error("creating iconv input descriptor failed.");
138 _M_out_desc
= iconv_open(_M_ext_enc
, _M_int_enc
);
139 if (_M_out_desc
== __err
)
140 __throw_runtime_error("creating iconv output descriptor failed.");
146 __desc_type __err
= reinterpret_cast<iconv_t
>(-1);
147 bool __test
= _M_in_desc
&& _M_in_desc
!= __err
;
148 __test
&= _M_out_desc
&& _M_out_desc
!= __err
;
153 _M_get_in_descriptor()
154 { return &_M_in_desc
; }
157 _M_get_out_descriptor()
158 { return &_M_out_desc
; }
161 _M_get_internal_enc()
162 { return _M_int_enc
; }
165 _M_get_external_enc()
166 { return _M_ext_enc
; }
169 _M_get_external_bom()
170 { return _M_ext_bom
; }
173 _M_get_internal_bom()
174 { return _M_int_bom
; }
177 // Partial specialization
178 // This specialization takes advantage of iconv to provide code
179 // conversions between a large number of character encodings.
180 template<typename _InternT
, typename _ExternT
>
181 class codecvt
<_InternT
, _ExternT
, __enc_traits
>
182 : public __codecvt_abstract_base
<_InternT
, _ExternT
, __enc_traits
>
186 typedef codecvt_base::result result
;
187 typedef _InternT intern_type
;
188 typedef _ExternT extern_type
;
189 typedef __enc_traits state_type
;
190 typedef __enc_traits::__desc_type __desc_type
;
191 typedef __enc_traits __enc_type
;
194 static locale::id id
;
197 codecvt(size_t __refs
= 0)
198 : __codecvt_abstract_base
<intern_type
, extern_type
, state_type
>(__refs
)
202 codecvt(__enc_type
* __enc
, size_t __refs
= 0)
203 : __codecvt_abstract_base
<intern_type
, extern_type
, state_type
>(__refs
)
211 do_out(state_type
& __state
, const intern_type
* __from
,
212 const intern_type
* __from_end
, const intern_type
*& __from_next
,
213 extern_type
* __to
, extern_type
* __to_end
,
214 extern_type
*& __to_next
) const;
217 do_unshift(state_type
& __state
, extern_type
* __to
,
218 extern_type
* __to_end
, extern_type
*& __to_next
) const;
221 do_in(state_type
& __state
, const extern_type
* __from
,
222 const extern_type
* __from_end
, const extern_type
*& __from_next
,
223 intern_type
* __to
, intern_type
* __to_end
,
224 intern_type
*& __to_next
) const;
227 do_encoding() const throw();
230 do_always_noconv() const throw();
233 do_length(const state_type
&, const extern_type
* __from
,
234 const extern_type
* __end
, size_t __max
) const;
237 do_max_length() const throw();
240 template<typename _InternT
, typename _ExternT
>
242 codecvt
<_InternT
, _ExternT
, __enc_traits
>::id
;
244 // This adaptor works around the signature problems of the second
245 // argument to iconv(): SUSv2 and others use 'const char**', but glibc 2.2
246 // uses 'char**', which is what the standard is (apparently) due to use
247 // in the future. Using this adaptor, g++ will do the work for us.
248 template<typename _T
>
250 __iconv_adaptor(size_t(*iconv_func
)(iconv_t
, _T
, size_t*, char**, size_t*),
251 iconv_t cd
, char** inbuf
, size_t* inbytesleft
,
252 char** outbuf
, size_t* outbytesleft
)
254 return iconv_func(cd
, (_T
)inbuf
, inbytesleft
, outbuf
, outbytesleft
);
257 template<typename _InternT
, typename _ExternT
>
259 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
260 do_out(state_type
& __state
, const intern_type
* __from
,
261 const intern_type
* __from_end
, const intern_type
*& __from_next
,
262 extern_type
* __to
, extern_type
* __to_end
,
263 extern_type
*& __to_next
) const
265 result __ret
= error
;
266 if (__state
._M_good())
268 typedef state_type::__desc_type __desc_type
;
269 const __desc_type
* __desc
= __state
._M_get_out_descriptor();
270 const size_t __fmultiple
= sizeof(intern_type
) / sizeof(char);
271 size_t __flen
= __fmultiple
* (__from_end
- __from
);
272 const size_t __tmultiple
= sizeof(extern_type
) / sizeof(char);
273 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
275 // Argument list for iconv specifies a byte sequence. Thus,
276 // all to/from arrays must be brutally casted to char*.
277 char* __cto
= reinterpret_cast<char*>(__to
);
281 // Some encodings need a byte order marker as the first item
282 // in the byte stream, to designate endian-ness. The default
283 // value for the byte order marker is NULL, so if this is
284 // the case, it's not necessary and we can just go on our
286 int __int_bom
= __state
._M_get_internal_bom();
289 size_t __size
= __from_end
- __from
;
290 intern_type
* __cfixed
= static_cast<intern_type
*>(__builtin_alloca(sizeof(intern_type
) * (__size
+ 1)));
291 __cfixed
[0] = static_cast<intern_type
>(__int_bom
);
292 char_traits
<intern_type
>::copy(__cfixed
+ 1, __from
, __size
);
293 __cfrom
= reinterpret_cast<char*>(__cfixed
);
294 __conv
= __iconv_adaptor(iconv
, *__desc
, &__cfrom
,
295 &__flen
, &__cto
, &__tlen
);
299 intern_type
* __cfixed
= const_cast<intern_type
*>(__from
);
300 __cfrom
= reinterpret_cast<char*>(__cfixed
);
301 __conv
= __iconv_adaptor(iconv
, *__desc
, &__cfrom
,
302 &__flen
, &__cto
, &__tlen
);
305 if (__conv
!= size_t(-1))
307 __from_next
= reinterpret_cast<const intern_type
*>(__cfrom
);
308 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
313 if (__flen
< static_cast<size_t>(__from_end
- __from
))
315 __from_next
= reinterpret_cast<const intern_type
*>(__cfrom
);
316 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
326 template<typename _InternT
, typename _ExternT
>
328 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
329 do_unshift(state_type
& __state
, extern_type
* __to
,
330 extern_type
* __to_end
, extern_type
*& __to_next
) const
332 result __ret
= error
;
333 if (__state
._M_good())
335 typedef state_type::__desc_type __desc_type
;
336 const __desc_type
* __desc
= __state
._M_get_in_descriptor();
337 const size_t __tmultiple
= sizeof(intern_type
) / sizeof(char);
338 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
340 // Argument list for iconv specifies a byte sequence. Thus,
341 // all to/from arrays must be brutally casted to char*.
342 char* __cto
= reinterpret_cast<char*>(__to
);
343 size_t __conv
= __iconv_adaptor(iconv
,*__desc
, NULL
, NULL
,
346 if (__conv
!= size_t(-1))
348 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
349 if (__tlen
== __tmultiple
* (__to_end
- __to
))
351 else if (__tlen
== 0)
362 template<typename _InternT
, typename _ExternT
>
364 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
365 do_in(state_type
& __state
, const extern_type
* __from
,
366 const extern_type
* __from_end
, const extern_type
*& __from_next
,
367 intern_type
* __to
, intern_type
* __to_end
,
368 intern_type
*& __to_next
) const
370 result __ret
= error
;
371 if (__state
._M_good())
373 typedef state_type::__desc_type __desc_type
;
374 const __desc_type
* __desc
= __state
._M_get_in_descriptor();
375 const size_t __fmultiple
= sizeof(extern_type
) / sizeof(char);
376 size_t __flen
= __fmultiple
* (__from_end
- __from
);
377 const size_t __tmultiple
= sizeof(intern_type
) / sizeof(char);
378 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
380 // Argument list for iconv specifies a byte sequence. Thus,
381 // all to/from arrays must be brutally casted to char*.
382 char* __cto
= reinterpret_cast<char*>(__to
);
386 // Some encodings need a byte order marker as the first item
387 // in the byte stream, to designate endian-ness. The default
388 // value for the byte order marker is NULL, so if this is
389 // the case, it's not necessary and we can just go on our
391 int __ext_bom
= __state
._M_get_external_bom();
394 size_t __size
= __from_end
- __from
;
395 extern_type
* __cfixed
= static_cast<extern_type
*>(__builtin_alloca(sizeof(extern_type
) * (__size
+ 1)));
396 __cfixed
[0] = static_cast<extern_type
>(__ext_bom
);
397 char_traits
<extern_type
>::copy(__cfixed
+ 1, __from
, __size
);
398 __cfrom
= reinterpret_cast<char*>(__cfixed
);
399 __conv
= __iconv_adaptor(iconv
, *__desc
, &__cfrom
,
400 &__flen
, &__cto
, &__tlen
);
404 extern_type
* __cfixed
= const_cast<extern_type
*>(__from
);
405 __cfrom
= reinterpret_cast<char*>(__cfixed
);
406 __conv
= __iconv_adaptor(iconv
, *__desc
, &__cfrom
,
407 &__flen
, &__cto
, &__tlen
);
411 if (__conv
!= size_t(-1))
413 __from_next
= reinterpret_cast<const extern_type
*>(__cfrom
);
414 __to_next
= reinterpret_cast<intern_type
*>(__cto
);
419 if (__flen
< static_cast<size_t>(__from_end
- __from
))
421 __from_next
= reinterpret_cast<const extern_type
*>(__cfrom
);
422 __to_next
= reinterpret_cast<intern_type
*>(__cto
);
432 template<typename _InternT
, typename _ExternT
>
434 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
435 do_encoding() const throw()
438 template<typename _InternT
, typename _ExternT
>
440 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
441 do_always_noconv() const throw()
444 template<typename _InternT
, typename _ExternT
>
446 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
447 do_length(const state_type
&, const extern_type
* __from
,
448 const extern_type
* __end
, size_t __max
) const
449 { return min(__max
, static_cast<size_t>(__end
- __from
)); }
451 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
452 // 74. Garbled text for codecvt::do_max_length
453 template<typename _InternT
, typename _ExternT
>
455 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
456 do_max_length() const throw()