35 #ifndef _EXT_CODECVT_SPECIALIZATIONS_H
36 #define _EXT_CODECVT_SPECIALIZATIONS_H 1
38 #include <bits/c++config.h>
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 typedef iconv_t descriptor_type;
66 descriptor_type _M_in_desc;
69 descriptor_type _M_out_desc;
85 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0), _M_bytes(0)
89 encoding_state(
const char* __int,
const char* __ext,
90 int __ibom = 0,
int __ebom = 0,
int __bytes = 1)
91 : _M_int_enc(__int), _M_ext_enc(__ext), _M_in_desc(0), _M_out_desc(0),
92 _M_ext_bom(__ebom), _M_int_bom(__ibom), _M_bytes(__bytes)
103 encoding_state(
const encoding_state& __obj) : _M_in_desc(0), _M_out_desc(0)
104 { construct(__obj); }
108 operator=(
const encoding_state& __obj)
120 const descriptor_type __err = (iconv_t)(-1);
121 bool __test = _M_in_desc && _M_in_desc != __err;
122 __test &= _M_out_desc && _M_out_desc != __err;
127 character_ratio()
const
131 internal_encoding()
const
132 {
return _M_int_enc; }
136 {
return _M_int_bom; }
139 external_encoding()
const
140 {
return _M_ext_enc; }
144 {
return _M_ext_bom; }
146 const descriptor_type&
147 in_descriptor()
const
148 {
return _M_in_desc; }
150 const descriptor_type&
151 out_descriptor()
const
152 {
return _M_out_desc; }
158 const descriptor_type __err = (iconv_t)(-1);
159 const bool __have_encodings = _M_int_enc.size() && _M_ext_enc.size();
160 if (!_M_in_desc && __have_encodings)
162 _M_in_desc = iconv_open(_M_int_enc.c_str(), _M_ext_enc.c_str());
163 if (_M_in_desc == __err)
164 std::__throw_runtime_error(__N(
"encoding_state::_M_init "
165 "creating iconv input descriptor failed"));
167 if (!_M_out_desc && __have_encodings)
169 _M_out_desc = iconv_open(_M_ext_enc.c_str(), _M_int_enc.c_str());
170 if (_M_out_desc == __err)
171 std::__throw_runtime_error(__N(
"encoding_state::_M_init "
172 "creating iconv output descriptor failed"));
177 construct(
const encoding_state& __obj)
180 _M_int_enc = __obj._M_int_enc;
181 _M_ext_enc = __obj._M_ext_enc;
182 _M_ext_bom = __obj._M_ext_bom;
183 _M_int_bom = __obj._M_int_bom;
184 _M_bytes = __obj._M_bytes;
191 const descriptor_type __err = (iconv_t)(-1);
192 if (_M_in_desc && _M_in_desc != __err)
194 iconv_close(_M_in_desc);
197 if (_M_out_desc && _M_out_desc != __err)
199 iconv_close(_M_out_desc);
209 template<
typename _CharT>
210 struct encoding_char_traits :
public std::char_traits<_CharT>
212 typedef encoding_state state_type;
213 typedef typename std::fpos<state_type> pos_type;
216 _GLIBCXX_END_NAMESPACE_VERSION
222 _GLIBCXX_BEGIN_NAMESPACE_VERSION
224 using __gnu_cxx::encoding_state;
229 template<
typename _InternT,
typename _ExternT>
230 class codecvt<_InternT, _ExternT, encoding_state>
231 :
public __codecvt_abstract_base<_InternT, _ExternT, encoding_state>
235 typedef codecvt_base::result result;
236 typedef _InternT intern_type;
237 typedef _ExternT extern_type;
238 typedef __gnu_cxx::encoding_state state_type;
239 typedef state_type::descriptor_type descriptor_type;
245 codecvt(
size_t __refs = 0)
246 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
250 codecvt(state_type& __enc,
size_t __refs = 0)
251 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
259 do_out(state_type& __state,
const intern_type* __from,
260 const intern_type* __from_end,
const intern_type*& __from_next,
261 extern_type* __to, extern_type* __to_end,
262 extern_type*& __to_next)
const;
265 do_unshift(state_type& __state, extern_type* __to,
266 extern_type* __to_end, extern_type*& __to_next)
const;
269 do_in(state_type& __state,
const extern_type* __from,
270 const extern_type* __from_end,
const extern_type*& __from_next,
271 intern_type* __to, intern_type* __to_end,
272 intern_type*& __to_next)
const;
275 do_encoding()
const throw();
278 do_always_noconv() const throw();
281 do_length(state_type&, const extern_type* __from,
282 const extern_type* __end,
size_t __max) const;
285 do_max_length() const throw();
288 template<typename _InternT, typename _ExternT>
290 codecvt<_InternT, _ExternT, encoding_state>::
id;
296 template<typename _Tp>
298 __iconv_adaptor(
size_t(*__func)(iconv_t, _Tp,
size_t*,
char**,
size_t*),
299 iconv_t __cd,
char** __inbuf,
size_t* __inbytes,
300 char** __outbuf,
size_t* __outbytes)
301 {
return __func(__cd, (_Tp)__inbuf, __inbytes, __outbuf, __outbytes); }
303 template<
typename _InternT,
typename _ExternT>
305 codecvt<_InternT, _ExternT, encoding_state>::
306 do_out(state_type& __state,
const intern_type* __from,
307 const intern_type* __from_end,
const intern_type*& __from_next,
308 extern_type* __to, extern_type* __to_end,
309 extern_type*& __to_next)
const
311 result __ret = codecvt_base::error;
314 const descriptor_type& __desc = __state.out_descriptor();
315 const size_t __fmultiple =
sizeof(intern_type);
316 size_t __fbytes = __fmultiple * (__from_end - __from);
317 const size_t __tmultiple =
sizeof(extern_type);
318 size_t __tbytes = __tmultiple * (__to_end - __to);
322 char* __cto =
reinterpret_cast<char*
>(__to);
331 int __int_bom = __state.internal_bom();
334 size_t __size = __from_end - __from;
335 intern_type* __cfixed =
static_cast<intern_type*
>
336 (__builtin_alloca(
sizeof(intern_type) * (__size + 1)));
337 __cfixed[0] =
static_cast<intern_type
>(__int_bom);
338 char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
339 __cfrom =
reinterpret_cast<char*
>(__cfixed);
340 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
341 &__fbytes, &__cto, &__tbytes);
345 intern_type* __cfixed =
const_cast<intern_type*
>(__from);
346 __cfrom =
reinterpret_cast<char*
>(__cfixed);
347 __conv = __iconv_adaptor(iconv, __desc, &__cfrom, &__fbytes,
351 if (__conv !=
size_t(-1))
353 __from_next =
reinterpret_cast<const intern_type*
>(__cfrom);
354 __to_next =
reinterpret_cast<extern_type*
>(__cto);
355 __ret = codecvt_base::ok;
359 if (__fbytes < __fmultiple * (__from_end - __from))
361 __from_next =
reinterpret_cast<const intern_type*
>(__cfrom);
362 __to_next =
reinterpret_cast<extern_type*
>(__cto);
363 __ret = codecvt_base::partial;
366 __ret = codecvt_base::error;
372 template<
typename _InternT,
typename _ExternT>
374 codecvt<_InternT, _ExternT, encoding_state>::
375 do_unshift(state_type& __state, extern_type* __to,
376 extern_type* __to_end, extern_type*& __to_next)
const
378 result __ret = codecvt_base::error;
381 const descriptor_type& __desc = __state.in_descriptor();
382 const size_t __tmultiple =
sizeof(intern_type);
383 size_t __tlen = __tmultiple * (__to_end - __to);
387 char* __cto =
reinterpret_cast<char*
>(__to);
388 size_t __conv = __iconv_adaptor(iconv,__desc, 0, 0,
391 if (__conv !=
size_t(-1))
393 __to_next =
reinterpret_cast<extern_type*
>(__cto);
394 if (__tlen == __tmultiple * (__to_end - __to))
395 __ret = codecvt_base::noconv;
396 else if (__tlen == 0)
397 __ret = codecvt_base::ok;
399 __ret = codecvt_base::partial;
402 __ret = codecvt_base::error;
407 template<
typename _InternT,
typename _ExternT>
409 codecvt<_InternT, _ExternT, encoding_state>::
410 do_in(state_type& __state,
const extern_type* __from,
411 const extern_type* __from_end,
const extern_type*& __from_next,
412 intern_type* __to, intern_type* __to_end,
413 intern_type*& __to_next)
const
415 result __ret = codecvt_base::error;
418 const descriptor_type& __desc = __state.in_descriptor();
419 const size_t __fmultiple =
sizeof(extern_type);
420 size_t __flen = __fmultiple * (__from_end - __from);
421 const size_t __tmultiple =
sizeof(intern_type);
422 size_t __tlen = __tmultiple * (__to_end - __to);
426 char* __cto =
reinterpret_cast<char*
>(__to);
435 int __ext_bom = __state.external_bom();
438 size_t __size = __from_end - __from;
439 extern_type* __cfixed =
static_cast<extern_type*
>
440 (__builtin_alloca(
sizeof(extern_type) * (__size + 1)));
441 __cfixed[0] =
static_cast<extern_type
>(__ext_bom);
442 char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
443 __cfrom =
reinterpret_cast<char*
>(__cfixed);
444 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
445 &__flen, &__cto, &__tlen);
449 extern_type* __cfixed =
const_cast<extern_type*
>(__from);
450 __cfrom =
reinterpret_cast<char*
>(__cfixed);
451 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
452 &__flen, &__cto, &__tlen);
456 if (__conv !=
size_t(-1))
458 __from_next =
reinterpret_cast<const extern_type*
>(__cfrom);
459 __to_next =
reinterpret_cast<intern_type*
>(__cto);
460 __ret = codecvt_base::ok;
464 if (__flen < static_cast<size_t>(__from_end - __from))
466 __from_next =
reinterpret_cast<const extern_type*
>(__cfrom);
467 __to_next =
reinterpret_cast<intern_type*
>(__cto);
468 __ret = codecvt_base::partial;
471 __ret = codecvt_base::error;
477 template<
typename _InternT,
typename _ExternT>
479 codecvt<_InternT, _ExternT, encoding_state>::
480 do_encoding()
const throw()
483 if (
sizeof(_ExternT) <=
sizeof(_InternT))
484 __ret =
sizeof(_InternT) /
sizeof(_ExternT);
488 template<
typename _InternT,
typename _ExternT>
490 codecvt<_InternT, _ExternT, encoding_state>::
491 do_always_noconv()
const throw()
494 template<
typename _InternT,
typename _ExternT>
496 codecvt<_InternT, _ExternT, encoding_state>::
497 do_length(state_type&,
const extern_type* __from,
498 const extern_type* __end,
size_t __max)
const
499 {
return std::min(__max, static_cast<size_t>(__end - __from)); }
503 template<
typename _InternT,
typename _ExternT>
505 codecvt<_InternT, _ExternT, encoding_state>::
506 do_max_length()
const throw()
509 _GLIBCXX_END_NAMESPACE_VERSION
std::size_t __size(__stack_t __stack)
Definition: profiler_node.h:68
basic_string< char > string
Definition: string:1153
const _Tp & min(const _Tp &__a, const _Tp &__b)
Equivalent to std::min.
Definition: base.h:144
namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
Definition: codecvt_specializations.h:42