]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/codecvt_specializations.h
extend.texi: Follow spelling conventions.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / codecvt_specializations.h
CommitLineData
33590f13
BK
1// Locale support (codecvt) -*- C++ -*-
2
8fc81078
PC
3// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4// 2008, 2009, 2010
5// Free Software Foundation, Inc.
33590f13
BK
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
33590f13
BK
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
748086b7
JJ
18// Under Section 7 of GPL version 3, you are granted additional
19// permissions described in the GCC Runtime Library Exception, version
20// 3.1, as published by the Free Software Foundation.
21
22// You should have received a copy of the GNU General Public License and
23// a copy of the GCC Runtime Library Exception along with this program;
24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25// <http://www.gnu.org/licenses/>.
33590f13
BK
26
27//
28// ISO C++ 14882: 22.2.1.5 Template class codecvt
29//
30
825bd0e1 31// Written by Benjamin Kosnik <bkoz@redhat.com>
33590f13 32
6ab63942 33/** @file ext/codecvt_specializations.h
825bd0e1 34 * This file is a GNU extension to the Standard C++ Library.
00aca6e8
BK
35 */
36
6ab63942
PC
37#ifndef _EXT_CODECVT_SPECIALIZATIONS_H
38#define _EXT_CODECVT_SPECIALIZATIONS_H 1
39
d5fa9a0d 40#include <bits/c++config.h>
6ab63942
PC
41#include <locale>
42#include <iconv.h>
43
3cbc7af0
BK
44_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45
939759fc 46 /// Extension to use iconv for dealing with character encodings.
6309eefc 47 // This includes conversions and comparisons between various character
6f48900c
BK
48 // sets. This object encapsulates data that may need to be shared between
49 // char_traits, codecvt and ctype.
825bd0e1 50 class encoding_state
33590f13
BK
51 {
52 public:
53 // Types:
54 // NB: A conversion descriptor subsumes and enhances the
55 // functionality of a simple state type such as mbstate_t.
825bd0e1 56 typedef iconv_t descriptor_type;
33590f13
BK
57
58 protected:
33590f13 59 // Name of internal character set encoding.
825bd0e1
BK
60 std::string _M_int_enc;
61
33590f13 62 // Name of external character set encoding.
825bd0e1 63 std::string _M_ext_enc;
33590f13
BK
64
65 // Conversion descriptor between external encoding to internal encoding.
825bd0e1
BK
66 descriptor_type _M_in_desc;
67
33590f13 68 // Conversion descriptor between internal encoding to external encoding.
825bd0e1 69 descriptor_type _M_out_desc;
33590f13 70
825bd0e1 71 // The byte-order marker for the external encoding, if necessary.
33590f13
BK
72 int _M_ext_bom;
73
825bd0e1 74 // The byte-order marker for the internal encoding, if necessary.
33590f13
BK
75 int _M_int_bom;
76
825bd0e1
BK
77 // Number of external bytes needed to construct one complete
78 // character in the internal encoding.
79 // NB: -1 indicates variable, or stateful, encodings.
80 int _M_bytes;
6f48900c 81
825bd0e1
BK
82 public:
83 explicit
84 encoding_state()
85 : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0), _M_bytes(0)
86 { }
87
88 explicit
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)
93 { init(); }
33590f13
BK
94
95 // 21.1.2 traits typedefs
96 // p4
97 // typedef STATE_T state_type
98 // requires: state_type shall meet the requirements of
99 // CopyConstructible types (20.1.3)
28dac70a 100 // NB: This does not preserve the actual state of the conversion
a5f105b5
BK
101 // descriptor member, but it does duplicate the encoding
102 // information.
825bd0e1
BK
103 encoding_state(const encoding_state& __obj) : _M_in_desc(0), _M_out_desc(0)
104 { construct(__obj); }
33590f13 105
6f48900c 106 // Need assignment operator as well.
825bd0e1
BK
107 encoding_state&
108 operator=(const encoding_state& __obj)
6f48900c 109 {
825bd0e1 110 construct(__obj);
7aacf989 111 return *this;
6f48900c
BK
112 }
113
825bd0e1
BK
114 ~encoding_state()
115 { destroy(); }
33590f13 116
825bd0e1
BK
117 bool
118 good() const throw()
119 {
5f232232 120 const descriptor_type __err = (iconv_t)(-1);
825bd0e1
BK
121 bool __test = _M_in_desc && _M_in_desc != __err;
122 __test &= _M_out_desc && _M_out_desc != __err;
123 return __test;
124 }
125
126 int
127 character_ratio() const
128 { return _M_bytes; }
129
130 const std::string
131 internal_encoding() const
132 { return _M_int_enc; }
133
134 int
135 internal_bom() const
136 { return _M_int_bom; }
137
138 const std::string
139 external_encoding() const
140 { return _M_ext_enc; }
141
142 int
143 external_bom() const
144 { return _M_ext_bom; }
145
146 const descriptor_type&
147 in_descriptor() const
148 { return _M_in_desc; }
149
150 const descriptor_type&
151 out_descriptor() const
152 { return _M_out_desc; }
153
154 protected:
33590f13 155 void
825bd0e1 156 init()
33590f13 157 {
5f232232 158 const descriptor_type __err = (iconv_t)(-1);
825bd0e1
BK
159 const bool __have_encodings = _M_int_enc.size() && _M_ext_enc.size();
160 if (!_M_in_desc && __have_encodings)
6f48900c 161 {
825bd0e1 162 _M_in_desc = iconv_open(_M_int_enc.c_str(), _M_ext_enc.c_str());
6f48900c 163 if (_M_in_desc == __err)
825bd0e1
BK
164 std::__throw_runtime_error(__N("encoding_state::_M_init "
165 "creating iconv input descriptor failed"));
6f48900c 166 }
825bd0e1 167 if (!_M_out_desc && __have_encodings)
6f48900c 168 {
825bd0e1 169 _M_out_desc = iconv_open(_M_ext_enc.c_str(), _M_int_enc.c_str());
6f48900c 170 if (_M_out_desc == __err)
825bd0e1 171 std::__throw_runtime_error(__N("encoding_state::_M_init "
ba9119ec 172 "creating iconv output descriptor failed"));
6f48900c 173 }
33590f13
BK
174 }
175
a5f105b5 176 void
825bd0e1 177 construct(const encoding_state& __obj)
a5f105b5 178 {
825bd0e1
BK
179 destroy();
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;
185 init();
186 }
187
188 void
189 destroy() throw()
190 {
5f232232 191 const descriptor_type __err = (iconv_t)(-1);
a5f105b5
BK
192 if (_M_in_desc && _M_in_desc != __err)
193 {
194 iconv_close(_M_in_desc);
195 _M_in_desc = 0;
196 }
197 if (_M_out_desc && _M_out_desc != __err)
198 {
199 iconv_close(_M_out_desc);
200 _M_out_desc = 0;
201 }
202 }
825bd0e1 203 };
a5f105b5 204
939759fc 205 /// encoding_char_traits
825bd0e1
BK
206 // Custom traits type with encoding_state for the state type, and the
207 // associated fpos<encoding_state> for the position type, all other
208 // bits equivalent to the required char_traits instantiations.
209 template<typename _CharT>
210 struct encoding_char_traits : public std::char_traits<_CharT>
211 {
212 typedef encoding_state state_type;
213 typedef typename std::fpos<state_type> pos_type;
214 };
6f48900c 215
3cbc7af0
BK
216_GLIBCXX_END_NAMESPACE
217
218
219_GLIBCXX_BEGIN_NAMESPACE(std)
220
825bd0e1 221 using __gnu_cxx::encoding_state;
33590f13 222
939759fc 223 /// codecvt<InternT, _ExternT, encoding_state> specialization.
00aca6e8
BK
224 // This partial specialization takes advantage of iconv to provide
225 // code conversions between a large number of character encodings.
33590f13 226 template<typename _InternT, typename _ExternT>
825bd0e1
BK
227 class codecvt<_InternT, _ExternT, encoding_state>
228 : public __codecvt_abstract_base<_InternT, _ExternT, encoding_state>
33590f13
BK
229 {
230 public:
231 // Types:
232 typedef codecvt_base::result result;
233 typedef _InternT intern_type;
234 typedef _ExternT extern_type;
825bd0e1
BK
235 typedef __gnu_cxx::encoding_state state_type;
236 typedef state_type::descriptor_type descriptor_type;
33590f13
BK
237
238 // Data Members:
239 static locale::id id;
240
241 explicit
242 codecvt(size_t __refs = 0)
243 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
244 { }
245
246 explicit
825bd0e1 247 codecvt(state_type& __enc, size_t __refs = 0)
33590f13
BK
248 : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
249 { }
250
825bd0e1 251 protected:
33590f13
BK
252 virtual
253 ~codecvt() { }
254
255 virtual result
256 do_out(state_type& __state, const intern_type* __from,
257 const intern_type* __from_end, const intern_type*& __from_next,
258 extern_type* __to, extern_type* __to_end,
259 extern_type*& __to_next) const;
260
261 virtual result
262 do_unshift(state_type& __state, extern_type* __to,
263 extern_type* __to_end, extern_type*& __to_next) const;
264
265 virtual result
266 do_in(state_type& __state, const extern_type* __from,
267 const extern_type* __from_end, const extern_type*& __from_next,
268 intern_type* __to, intern_type* __to_end,
269 intern_type*& __to_next) const;
270
271 virtual int
272 do_encoding() const throw();
273
274 virtual bool
275 do_always_noconv() const throw();
276
277 virtual int
e61c8e23 278 do_length(state_type&, const extern_type* __from,
33590f13
BK
279 const extern_type* __end, size_t __max) const;
280
281 virtual int
282 do_max_length() const throw();
283 };
284
285 template<typename _InternT, typename _ExternT>
286 locale::id
825bd0e1 287 codecvt<_InternT, _ExternT, encoding_state>::id;
33590f13
BK
288
289 // This adaptor works around the signature problems of the second
290 // argument to iconv(): SUSv2 and others use 'const char**', but glibc 2.2
07814743
BK
291 // uses 'char**', which matches the POSIX 1003.1-2001 standard.
292 // Using this adaptor, g++ will do the work for us.
bd11bebe 293 template<typename _Tp>
33590f13 294 inline size_t
bd11bebe 295 __iconv_adaptor(size_t(*__func)(iconv_t, _Tp, size_t*, char**, size_t*),
07814743
BK
296 iconv_t __cd, char** __inbuf, size_t* __inbytes,
297 char** __outbuf, size_t* __outbytes)
bd11bebe 298 { return __func(__cd, (_Tp)__inbuf, __inbytes, __outbuf, __outbytes); }
33590f13
BK
299
300 template<typename _InternT, typename _ExternT>
301 codecvt_base::result
825bd0e1 302 codecvt<_InternT, _ExternT, encoding_state>::
33590f13
BK
303 do_out(state_type& __state, const intern_type* __from,
304 const intern_type* __from_end, const intern_type*& __from_next,
305 extern_type* __to, extern_type* __to_end,
306 extern_type*& __to_next) const
307 {
8fbc5ae7 308 result __ret = codecvt_base::error;
825bd0e1 309 if (__state.good())
33590f13 310 {
825bd0e1 311 const descriptor_type& __desc = __state.out_descriptor();
99b629fa 312 const size_t __fmultiple = sizeof(intern_type);
07814743 313 size_t __fbytes = __fmultiple * (__from_end - __from);
99b629fa 314 const size_t __tmultiple = sizeof(extern_type);
07814743 315 size_t __tbytes = __tmultiple * (__to_end - __to);
33590f13
BK
316
317 // Argument list for iconv specifies a byte sequence. Thus,
318 // all to/from arrays must be brutally casted to char*.
319 char* __cto = reinterpret_cast<char*>(__to);
320 char* __cfrom;
321 size_t __conv;
322
323 // Some encodings need a byte order marker as the first item
324 // in the byte stream, to designate endian-ness. The default
325 // value for the byte order marker is NULL, so if this is
326 // the case, it's not necessary and we can just go on our
327 // merry way.
825bd0e1 328 int __int_bom = __state.internal_bom();
33590f13
BK
329 if (__int_bom)
330 {
331 size_t __size = __from_end - __from;
e76382b5
VR
332 intern_type* __cfixed = static_cast<intern_type*>
333 (__builtin_alloca(sizeof(intern_type) * (__size + 1)));
33590f13
BK
334 __cfixed[0] = static_cast<intern_type>(__int_bom);
335 char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
336 __cfrom = reinterpret_cast<char*>(__cfixed);
825bd0e1 337 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
07814743 338 &__fbytes, &__cto, &__tbytes);
33590f13
BK
339 }
340 else
341 {
342 intern_type* __cfixed = const_cast<intern_type*>(__from);
343 __cfrom = reinterpret_cast<char*>(__cfixed);
825bd0e1 344 __conv = __iconv_adaptor(iconv, __desc, &__cfrom, &__fbytes,
07814743 345 &__cto, &__tbytes);
33590f13
BK
346 }
347
348 if (__conv != size_t(-1))
349 {
350 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
351 __to_next = reinterpret_cast<extern_type*>(__cto);
8fbc5ae7 352 __ret = codecvt_base::ok;
33590f13
BK
353 }
354 else
355 {
07814743 356 if (__fbytes < __fmultiple * (__from_end - __from))
33590f13
BK
357 {
358 __from_next = reinterpret_cast<const intern_type*>(__cfrom);
359 __to_next = reinterpret_cast<extern_type*>(__cto);
8fbc5ae7 360 __ret = codecvt_base::partial;
33590f13
BK
361 }
362 else
8fbc5ae7 363 __ret = codecvt_base::error;
33590f13
BK
364 }
365 }
366 return __ret;
367 }
368
369 template<typename _InternT, typename _ExternT>
370 codecvt_base::result
825bd0e1 371 codecvt<_InternT, _ExternT, encoding_state>::
33590f13
BK
372 do_unshift(state_type& __state, extern_type* __to,
373 extern_type* __to_end, extern_type*& __to_next) const
374 {
8fbc5ae7 375 result __ret = codecvt_base::error;
825bd0e1 376 if (__state.good())
33590f13 377 {
825bd0e1 378 const descriptor_type& __desc = __state.in_descriptor();
99b629fa 379 const size_t __tmultiple = sizeof(intern_type);
33590f13
BK
380 size_t __tlen = __tmultiple * (__to_end - __to);
381
382 // Argument list for iconv specifies a byte sequence. Thus,
383 // all to/from arrays must be brutally casted to char*.
384 char* __cto = reinterpret_cast<char*>(__to);
8fc81078 385 size_t __conv = __iconv_adaptor(iconv,__desc, 0, 0,
33590f13
BK
386 &__cto, &__tlen);
387
388 if (__conv != size_t(-1))
389 {
390 __to_next = reinterpret_cast<extern_type*>(__cto);
391 if (__tlen == __tmultiple * (__to_end - __to))
8fbc5ae7 392 __ret = codecvt_base::noconv;
33590f13 393 else if (__tlen == 0)
8fbc5ae7 394 __ret = codecvt_base::ok;
33590f13 395 else
8fbc5ae7 396 __ret = codecvt_base::partial;
33590f13
BK
397 }
398 else
8fbc5ae7 399 __ret = codecvt_base::error;
33590f13
BK
400 }
401 return __ret;
402 }
403
404 template<typename _InternT, typename _ExternT>
405 codecvt_base::result
825bd0e1 406 codecvt<_InternT, _ExternT, encoding_state>::
33590f13
BK
407 do_in(state_type& __state, const extern_type* __from,
408 const extern_type* __from_end, const extern_type*& __from_next,
409 intern_type* __to, intern_type* __to_end,
410 intern_type*& __to_next) const
411 {
8fbc5ae7 412 result __ret = codecvt_base::error;
825bd0e1 413 if (__state.good())
33590f13 414 {
825bd0e1 415 const descriptor_type& __desc = __state.in_descriptor();
99b629fa 416 const size_t __fmultiple = sizeof(extern_type);
33590f13 417 size_t __flen = __fmultiple * (__from_end - __from);
99b629fa 418 const size_t __tmultiple = sizeof(intern_type);
33590f13
BK
419 size_t __tlen = __tmultiple * (__to_end - __to);
420
421 // Argument list for iconv specifies a byte sequence. Thus,
422 // all to/from arrays must be brutally casted to char*.
423 char* __cto = reinterpret_cast<char*>(__to);
424 char* __cfrom;
425 size_t __conv;
426
427 // Some encodings need a byte order marker as the first item
428 // in the byte stream, to designate endian-ness. The default
429 // value for the byte order marker is NULL, so if this is
430 // the case, it's not necessary and we can just go on our
431 // merry way.
825bd0e1 432 int __ext_bom = __state.external_bom();
33590f13
BK
433 if (__ext_bom)
434 {
435 size_t __size = __from_end - __from;
e76382b5
VR
436 extern_type* __cfixed = static_cast<extern_type*>
437 (__builtin_alloca(sizeof(extern_type) * (__size + 1)));
33590f13
BK
438 __cfixed[0] = static_cast<extern_type>(__ext_bom);
439 char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
440 __cfrom = reinterpret_cast<char*>(__cfixed);
825bd0e1 441 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
33590f13
BK
442 &__flen, &__cto, &__tlen);
443 }
444 else
445 {
446 extern_type* __cfixed = const_cast<extern_type*>(__from);
447 __cfrom = reinterpret_cast<char*>(__cfixed);
825bd0e1 448 __conv = __iconv_adaptor(iconv, __desc, &__cfrom,
33590f13
BK
449 &__flen, &__cto, &__tlen);
450 }
451
452
453 if (__conv != size_t(-1))
454 {
455 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
456 __to_next = reinterpret_cast<intern_type*>(__cto);
8fbc5ae7 457 __ret = codecvt_base::ok;
33590f13
BK
458 }
459 else
460 {
461 if (__flen < static_cast<size_t>(__from_end - __from))
462 {
463 __from_next = reinterpret_cast<const extern_type*>(__cfrom);
464 __to_next = reinterpret_cast<intern_type*>(__cto);
8fbc5ae7 465 __ret = codecvt_base::partial;
33590f13
BK
466 }
467 else
8fbc5ae7 468 __ret = codecvt_base::error;
33590f13
BK
469 }
470 }
471 return __ret;
472 }
473
474 template<typename _InternT, typename _ExternT>
475 int
825bd0e1 476 codecvt<_InternT, _ExternT, encoding_state>::
33590f13 477 do_encoding() const throw()
07814743
BK
478 {
479 int __ret = 0;
480 if (sizeof(_ExternT) <= sizeof(_InternT))
825bd0e1 481 __ret = sizeof(_InternT) / sizeof(_ExternT);
07814743
BK
482 return __ret;
483 }
33590f13
BK
484
485 template<typename _InternT, typename _ExternT>
486 bool
825bd0e1 487 codecvt<_InternT, _ExternT, encoding_state>::
33590f13
BK
488 do_always_noconv() const throw()
489 { return false; }
490
491 template<typename _InternT, typename _ExternT>
492 int
825bd0e1 493 codecvt<_InternT, _ExternT, encoding_state>::
e61c8e23 494 do_length(state_type&, const extern_type* __from,
33590f13 495 const extern_type* __end, size_t __max) const
4977bab6 496 { return std::min(__max, static_cast<size_t>(__end - __from)); }
33590f13 497
f5677b15
PC
498 // _GLIBCXX_RESOLVE_LIB_DEFECTS
499 // 74. Garbled text for codecvt::do_max_length
33590f13
BK
500 template<typename _InternT, typename _ExternT>
501 int
825bd0e1 502 codecvt<_InternT, _ExternT, encoding_state>::
33590f13
BK
503 do_max_length() const throw()
504 { return 1; }
3cbc7af0
BK
505
506_GLIBCXX_END_NAMESPACE
f5677b15 507
6ab63942 508#endif