37 #ifndef _EXT_CODECVT_SPECIALIZATIONS_H
38 #define _EXT_CODECVT_SPECIALIZATIONS_H 1
44 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 typedef iconv_t descriptor_type;
68 descriptor_type _M_in_desc;
71 descriptor_type _M_out_desc;
87 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0), _M_bytes(0)
92 int __ibom = 0,
int __ebom = 0,
int __bytes = 1)
93 : _M_int_enc(__int), _M_ext_enc(__ext), _M_in_desc(0), _M_out_desc(0),
94 _M_ext_bom(__ebom), _M_int_bom(__ibom), _M_bytes(__bytes)
106 { construct(__obj); }
122 const descriptor_type __err = (iconv_t)(-1);
123 bool __test = _M_in_desc && _M_in_desc != __err;
124 __test &= _M_out_desc && _M_out_desc != __err;
129 character_ratio()
const
133 internal_encoding()
const
134 {
return _M_int_enc; }
138 {
return _M_int_bom; }
141 external_encoding()
const
142 {
return _M_ext_enc; }
146 {
return _M_ext_bom; }
148 const descriptor_type&
149 in_descriptor()
const
150 {
return _M_in_desc; }
152 const descriptor_type&
153 out_descriptor()
const
154 {
return _M_out_desc; }
160 const descriptor_type __err = (iconv_t)(-1);
161 const bool __have_encodings = _M_int_enc.
size() && _M_ext_enc.
size();
162 if (!_M_in_desc && __have_encodings)
164 _M_in_desc = iconv_open(_M_int_enc.
c_str(), _M_ext_enc.
c_str());
165 if (_M_in_desc == __err)
166 std::__throw_runtime_error(__N(
"encoding_state::_M_init "
167 "creating iconv input descriptor failed"));
169 if (!_M_out_desc && __have_encodings)
171 _M_out_desc = iconv_open(_M_ext_enc.
c_str(), _M_int_enc.
c_str());
172 if (_M_out_desc == __err)
173 std::__throw_runtime_error(__N(
"encoding_state::_M_init "
174 "creating iconv output descriptor failed"));
182 _M_int_enc = __obj._M_int_enc;
183 _M_ext_enc = __obj._M_ext_enc;
184 _M_ext_bom = __obj._M_ext_bom;
185 _M_int_bom = __obj._M_int_bom;
186 _M_bytes = __obj._M_bytes;
193 const descriptor_type __err = (iconv_t)(-1);
194 if (_M_in_desc && _M_in_desc != __err)
196 iconv_close(_M_in_desc);
199 if (_M_out_desc && _M_out_desc != __err)
201 iconv_close(_M_out_desc);
211 template<
typename _CharT>
218 _GLIBCXX_END_NAMESPACE_VERSION
222 namespace std _GLIBCXX_VISIBILITY(default)
224 _GLIBCXX_BEGIN_NAMESPACE_VERSION
231 template<
typename _InternT,
typename _ExternT>
237 typedef codecvt_base::result result;
238 typedef _InternT intern_type;
239 typedef _ExternT extern_type;
241 typedef state_type::descriptor_type descriptor_type;
262 const intern_type* __from_end,
const intern_type*& __from_next,
263 extern_type* __to, extern_type* __to_end,
264 extern_type*& __to_next)
const;
267 do_unshift(
state_type& __state, extern_type* __to,
268 extern_type* __to_end, extern_type*& __to_next)
const;
271 do_in(
state_type& __state,
const extern_type* __from,
272 const extern_type* __from_end,
const extern_type*& __from_next,
273 intern_type* __to, intern_type* __to_end,
274 intern_type*& __to_next)
const;
277 do_encoding()
const throw();
280 do_always_noconv()
const throw();
283 do_length(
state_type&,
const extern_type* __from,
284 const extern_type* __end,
size_t __max)
const;
287 do_max_length()
const throw();
290 template<
typename _InternT,
typename _ExternT>
298 template<
typename _Tp>
300 __iconv_adaptor(
size_t(*__func)(iconv_t, _Tp,
size_t*,
char**,
size_t*),
301 iconv_t __cd,
char** __inbuf,
size_t* __inbytes,
302 char** __outbuf,
size_t* __outbytes)
303 {
return __func(__cd, (_Tp)__inbuf, __inbytes, __outbuf, __outbytes); }
305 template<
typename _InternT,
typename _ExternT>
309 const intern_type* __from_end,
const intern_type*& __from_next,
310 extern_type* __to, extern_type* __to_end,
311 extern_type*& __to_next)
const
313 result __ret = codecvt_base::error;
316 const descriptor_type& __desc = __state.out_descriptor();
317 const size_t __fmultiple =
sizeof(intern_type);
318 size_t __fbytes = __fmultiple * (__from_end - __from);
319 const size_t __tmultiple =
sizeof(extern_type);
320 size_t __tbytes = __tmultiple * (__to_end - __to);
324 char* __cto =
reinterpret_cast<char*
>(__to);
333 int __int_bom = __state.internal_bom();
336 size_t __size = __from_end - __from;
337 intern_type* __cfixed =
static_cast<intern_type*
>
338 (__builtin_alloca(
sizeof(intern_type) * (__size + 1)));
339 __cfixed[0] =
static_cast<intern_type
>(__int_bom);
341 __cfrom =
reinterpret_cast<char*
>(__cfixed);
342 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
343 &__fbytes, &__cto, &__tbytes);
347 intern_type* __cfixed =
const_cast<intern_type*
>(__from);
348 __cfrom =
reinterpret_cast<char*
>(__cfixed);
349 __conv = __iconv_adaptor(iconv, __desc, &__cfrom, &__fbytes,
353 if (__conv !=
size_t(-1))
355 __from_next =
reinterpret_cast<const intern_type*
>(__cfrom);
356 __to_next =
reinterpret_cast<extern_type*
>(__cto);
357 __ret = codecvt_base::ok;
361 if (__fbytes < __fmultiple * (__from_end - __from))
363 __from_next =
reinterpret_cast<const intern_type*
>(__cfrom);
364 __to_next =
reinterpret_cast<extern_type*
>(__cto);
365 __ret = codecvt_base::partial;
368 __ret = codecvt_base::error;
374 template<
typename _InternT,
typename _ExternT>
377 do_unshift(state_type& __state, extern_type* __to,
378 extern_type* __to_end, extern_type*& __to_next)
const
380 result __ret = codecvt_base::error;
383 const descriptor_type& __desc = __state.in_descriptor();
384 const size_t __tmultiple =
sizeof(intern_type);
385 size_t __tlen = __tmultiple * (__to_end - __to);
389 char* __cto =
reinterpret_cast<char*
>(__to);
390 size_t __conv = __iconv_adaptor(iconv,__desc, 0, 0,
393 if (__conv !=
size_t(-1))
395 __to_next =
reinterpret_cast<extern_type*
>(__cto);
396 if (__tlen == __tmultiple * (__to_end - __to))
397 __ret = codecvt_base::noconv;
398 else if (__tlen == 0)
399 __ret = codecvt_base::ok;
401 __ret = codecvt_base::partial;
404 __ret = codecvt_base::error;
409 template<
typename _InternT,
typename _ExternT>
411 codecvt<_InternT, _ExternT, encoding_state>::
412 do_in(state_type& __state,
const extern_type* __from,
413 const extern_type* __from_end,
const extern_type*& __from_next,
414 intern_type* __to, intern_type* __to_end,
415 intern_type*& __to_next)
const
417 result __ret = codecvt_base::error;
420 const descriptor_type& __desc = __state.in_descriptor();
421 const size_t __fmultiple =
sizeof(extern_type);
422 size_t __flen = __fmultiple * (__from_end - __from);
423 const size_t __tmultiple =
sizeof(intern_type);
424 size_t __tlen = __tmultiple * (__to_end - __to);
428 char* __cto =
reinterpret_cast<char*
>(__to);
437 int __ext_bom = __state.external_bom();
440 size_t __size = __from_end - __from;
441 extern_type* __cfixed =
static_cast<extern_type*
>
442 (__builtin_alloca(
sizeof(extern_type) * (__size + 1)));
443 __cfixed[0] =
static_cast<extern_type
>(__ext_bom);
444 char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
445 __cfrom =
reinterpret_cast<char*
>(__cfixed);
446 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
447 &__flen, &__cto, &__tlen);
451 extern_type* __cfixed =
const_cast<extern_type*
>(__from);
452 __cfrom =
reinterpret_cast<char*
>(__cfixed);
453 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
454 &__flen, &__cto, &__tlen);
458 if (__conv !=
size_t(-1))
460 __from_next =
reinterpret_cast<const extern_type*
>(__cfrom);
461 __to_next =
reinterpret_cast<intern_type*
>(__cto);
462 __ret = codecvt_base::ok;
466 if (__flen < static_cast<size_t>(__from_end - __from))
468 __from_next =
reinterpret_cast<const extern_type*
>(__cfrom);
469 __to_next =
reinterpret_cast<intern_type*
>(__cto);
470 __ret = codecvt_base::partial;
473 __ret = codecvt_base::error;
479 template<
typename _InternT,
typename _ExternT>
481 codecvt<_InternT, _ExternT, encoding_state>::
482 do_encoding()
const throw()
485 if (
sizeof(_ExternT) <=
sizeof(_InternT))
486 __ret =
sizeof(_InternT) /
sizeof(_ExternT);
490 template<
typename _InternT,
typename _ExternT>
492 codecvt<_InternT, _ExternT, encoding_state>::
493 do_always_noconv()
const throw()
496 template<
typename _InternT,
typename _ExternT>
498 codecvt<_InternT, _ExternT, encoding_state>::
499 do_length(state_type&,
const extern_type* __from,
500 const extern_type* __end,
size_t __max)
const
501 {
return std::min(__max, static_cast<size_t>(__end - __from)); }
505 template<
typename _InternT,
typename _ExternT>
507 codecvt<_InternT, _ExternT, encoding_state>::
508 do_max_length()
const throw()
511 _GLIBCXX_END_NAMESPACE_VERSION
Basis for explicit traits specializations.
Extension to use iconv for dealing with character encodings.
Class representing stream positions.
size_type size() const
Returns the number of characters in the string, not including any null-termination.
Primary class template codecvt.NB: Generic, mostly useless implementation.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
virtual result do_out(state_type &__state, const intern_type *__from, const intern_type *__from_end, const intern_type *&__from_next, extern_type *__to, extern_type *__to_end, extern_type *&__to_next) const
Convert from internal to external character set.
const _CharT * c_str() const
Return const pointer to null-terminated contents.
Facet ID class.The ID class provides facets with an index used to identify them. Every facet class mu...
Common base for codecvt functions.