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