]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/codecvt.h
1 // Locale support (codecvt) -*- C++ -*-
3 // Copyright (C) 2000 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>
38 #ifndef _CPP_BITS_CODECVT_H
39 #define _CPP_BITS_CODECVT_H 1
41 #ifdef _GLIBCPP_USE_WCHAR_T
42 #include <iconv.h> // For iconv, iconv_t
48 // XXX __enc_traits may need to move up the locale header hierarchy,
49 // depending on if ctype ends up using it.
50 #ifdef _GLIBCPP_USE_WCHAR_T
51 // Extensions to use icov for dealing with character encodings,
52 // including conversions and comparisons between various character
53 // sets. This object encapsulates data that may need to be shared between
54 // char_traits, codecvt and ctype.
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_intc_enc
[_S_max_size
];
69 // Name of external character set encoding.
70 char _M_extc_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
;
78 __enc_traits() : _M_in_desc(0), _M_out_desc(0)
80 // __intc_end = whatever we are using internally, which is
82 // UCS2 == UNICODE (microsoft, java, aix, whatever...)
83 // XXX Currently don't know how to get this data from target system...
84 strcpy(_M_intc_enc
, "UCS4");
86 // __extc_end = external codeset in current locale
87 strcpy(_M_extc_enc
, nl_langinfo(CODESET
));
90 __enc_traits(const char* __int
, const char* __ext
)
91 : _M_in_desc(0), _M_out_desc(0)
93 strncpy(_M_intc_enc
, __int
, _S_max_size
);
94 strncpy(_M_extc_enc
, __ext
, _S_max_size
);
97 // 21.1.2 traits typedefs
99 // typedef STATE_T state_type
100 // requires: state_type shall meet the requirements of
101 // CopyConstructible types (20.1.3)
102 __enc_traits(const __enc_traits
& __obj
)
104 strncpy(_M_intc_enc
, __obj
._M_intc_enc
, _S_max_size
);
105 strncpy(_M_extc_enc
, __obj
._M_extc_enc
, _S_max_size
);
110 iconv_close(_M_in_desc
);
111 iconv_close(_M_out_desc
);
118 _M_in_desc
= iconv_open(_M_intc_enc
, _M_extc_enc
);
119 _M_out_desc
= iconv_open(_M_extc_enc
, _M_intc_enc
);
120 if (_M_out_desc
== iconv_t(-1) || _M_in_desc
== iconv_t(-1))
122 // XXX Extended error checking.
129 return _M_out_desc
&& _M_in_desc
130 && _M_out_desc
!= iconv_t(-1) && _M_in_desc
!= iconv_t(-1);
134 _M_get_in_descriptor()
135 { return &_M_in_desc
; }
138 _M_get_out_descriptor()
139 { return &_M_out_desc
; }
142 _M_get_internal_enc()
143 { return _M_intc_enc
; }
146 _M_get_external_enc()
147 { return _M_extc_enc
; }
149 #endif //_GLIBCPP_USE_WCHAR_T
152 // 22.2.1.5 Template class codecvt
165 // Template class __codecvt_abstract_base
166 // NB: An abstract base class that fills in the public inlines, so
167 // that the specializations don't have to re-copy the public
169 template<typename _InternT
, typename _ExternT
, typename _StateT
>
170 class __codecvt_abstract_base
171 : public locale::facet
, public codecvt_base
175 typedef codecvt_base::result result
;
176 typedef _InternT intern_type
;
177 typedef _ExternT extern_type
;
178 typedef _StateT state_type
;
180 // 22.2.1.5.1 codecvt members
182 out(state_type
& __state
, const intern_type
* __from
,
183 const intern_type
* __from_end
, const intern_type
*& __from_next
,
184 extern_type
* __to
, extern_type
* __to_end
,
185 extern_type
*& __to_next
) const
187 return this->do_out(__state
, __from
, __from_end
, __from_next
,
188 __to
, __to_end
, __to_next
);
192 unshift(state_type
& __state
, extern_type
* __to
, extern_type
* __to_end
,
193 extern_type
*& __to_next
) const
194 { return this->do_unshift(__state
, __to
,__to_end
,__to_next
); }
197 in(state_type
& __state
, const extern_type
* __from
,
198 const extern_type
* __from_end
, const extern_type
*& __from_next
,
199 intern_type
* __to
, intern_type
* __to_end
,
200 intern_type
*& __to_next
) const
202 return this->do_in(__state
, __from
, __from_end
, __from_next
,
203 __to
, __to_end
, __to_next
);
207 encoding() const throw()
208 { return this->do_encoding(); }
211 always_noconv() const throw()
212 { return this->do_always_noconv(); }
215 length(const state_type
& __state
, const extern_type
* __from
,
216 const extern_type
* __end
, size_t __max
) const
217 { return this->do_length(__state
, __from
, __end
, __max
); }
220 max_length() const throw()
221 { return this->do_max_length(); }
225 __codecvt_abstract_base(size_t __refs
= 0) : locale::facet(__refs
) { }
228 ~__codecvt_abstract_base() { }
231 do_out(state_type
& __state
, const intern_type
* __from
,
232 const intern_type
* __from_end
, const intern_type
*& __from_next
,
233 extern_type
* __to
, extern_type
* __to_end
,
234 extern_type
*& __to_next
) const = 0;
237 do_unshift(state_type
& __state
, extern_type
* __to
,
238 extern_type
* __to_end
, extern_type
*& __to_next
) const = 0;
241 do_in(state_type
& __state
, const extern_type
* __from
,
242 const extern_type
* __from_end
, const extern_type
*& __from_next
,
243 intern_type
* __to
, intern_type
* __to_end
,
244 intern_type
*& __to_next
) const = 0;
247 do_encoding() const throw() = 0;
250 do_always_noconv() const throw() = 0;
253 do_length(const state_type
&, const extern_type
* __from
,
254 const extern_type
* __end
, size_t __max
) const = 0;
257 do_max_length() const throw() = 0;
260 // 22.2.1.5 Template class codecvt
261 // NB: Generic, mostly useless implementation.
262 template<typename _InternT
, typename _ExternT
, typename _StateT
>
264 : public __codecvt_abstract_base
<_InternT
, _ExternT
, _StateT
>
268 typedef codecvt_base::result result
;
269 typedef _InternT intern_type
;
270 typedef _ExternT extern_type
;
271 typedef _StateT state_type
;
274 static locale::id id
;
277 codecvt(size_t __refs
= 0)
278 : __codecvt_abstract_base
<_InternT
,_ExternT
,_StateT
> (__refs
) { }
285 template<typename _InternT
, typename _ExternT
, typename _StateT
>
286 locale::id codecvt
<_InternT
, _ExternT
, _StateT
>::id
;
288 // partial specialization
289 // This specialization takes advantage of iconv to provide code
290 // conversions between a large number of character encodings.
291 template<typename _InternT
, typename _ExternT
>
292 class codecvt
<_InternT
, _ExternT
, __enc_traits
>
293 : public __codecvt_abstract_base
<_InternT
, _ExternT
, __enc_traits
>
297 typedef codecvt_base::result result
;
298 typedef _InternT intern_type
;
299 typedef _ExternT extern_type
;
300 typedef __enc_traits state_type
;
301 typedef __enc_traits::__desc_type __desc_type
;
302 typedef __enc_traits __enc_type
;
305 static locale::id id
;
308 codecvt(size_t __refs
= 0)
309 : __codecvt_abstract_base
<intern_type
, extern_type
, state_type
>(__refs
)
313 codecvt(__enc_type
* __enc
, size_t __refs
= 0)
314 : __codecvt_abstract_base
<intern_type
, extern_type
, state_type
>(__refs
)
322 do_out(state_type
& __state
, const intern_type
* __from
,
323 const intern_type
* __from_end
, const intern_type
*& __from_next
,
324 extern_type
* __to
, extern_type
* __to_end
,
325 extern_type
*& __to_next
) const;
328 do_unshift(state_type
& __state
, extern_type
* __to
,
329 extern_type
* __to_end
, extern_type
*& __to_next
) const;
332 do_in(state_type
& __state
, const extern_type
* __from
,
333 const extern_type
* __from_end
, const extern_type
*& __from_next
,
334 intern_type
* __to
, intern_type
* __to_end
,
335 intern_type
*& __to_next
) const;
338 do_encoding() const throw();
341 do_always_noconv() const throw();
344 do_length(const state_type
&, const extern_type
* __from
,
345 const extern_type
* __end
, size_t __max
) const;
348 do_max_length() const throw();
351 template<typename _InternT
, typename _ExternT
>
353 codecvt
<_InternT
, _ExternT
, __enc_traits
>::id
;
355 template<typename _InternT
, typename _ExternT
>
357 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
358 do_out(state_type
& __state
, const intern_type
* __from
,
359 const intern_type
* __from_end
, const intern_type
*& __from_next
,
360 extern_type
* __to
, extern_type
* __to_end
,
361 extern_type
*& __to_next
) const
363 result __ret
= error
;
364 if (__state
._M_good())
366 typedef state_type::__desc_type __desc_type
;
367 const __desc_type
* __desc
= __state
._M_get_out_descriptor();
368 const size_t __fmultiple
= sizeof(intern_type
) / sizeof(char);
369 size_t __flen
= __fmultiple
* (__from_end
- __from
);
370 const size_t __tmultiple
= sizeof(extern_type
) / sizeof(char);
371 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
373 // Argument list for iconv specifies a byte sequence. Thus,
374 // all to/from arrays must be brutally casted to char*.
375 char* __cfrom
= reinterpret_cast<char*>(const_cast<intern_type
*>(__from
));
376 char* __cto
= reinterpret_cast<char*>(__to
);
377 size_t __conv
= iconv(*__desc
, &__cfrom
, &__flen
, &__cto
, &__tlen
);
379 if (__conv
!= size_t(-1))
381 __from_next
= reinterpret_cast<const intern_type
*>(__cfrom
);
382 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
387 if (__flen
< __from_end
- __from
)
389 __from_next
= reinterpret_cast<const intern_type
*>(__cfrom
);
390 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
400 template<typename _InternT
, typename _ExternT
>
402 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
403 do_unshift(state_type
& __state
, extern_type
* __to
,
404 extern_type
* __to_end
, extern_type
*& __to_next
) const
406 result __ret
= error
;
407 if (__state
._M_good())
409 typedef state_type::__desc_type __desc_type
;
410 const __desc_type
* __desc
= __state
._M_get_in_descriptor();
411 const size_t __tmultiple
= sizeof(intern_type
) / sizeof(char);
412 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
414 // Argument list for iconv specifies a byte sequence. Thus,
415 // all to/from arrays must be brutally casted to char*.
416 char* __cto
= reinterpret_cast<char*>(__to
);
417 size_t __conv
= iconv(*__desc
, NULL
, NULL
, &__cto
, &__tlen
);
419 if (__conv
!= size_t(-1))
421 __to_next
= reinterpret_cast<extern_type
*>(__cto
);
422 if (__tlen
== __tmultiple
* (__to_end
- __to
))
424 else if (__tlen
== 0)
435 template<typename _InternT
, typename _ExternT
>
437 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
438 do_in(state_type
& __state
, const extern_type
* __from
,
439 const extern_type
* __from_end
, const extern_type
*& __from_next
,
440 intern_type
* __to
, intern_type
* __to_end
,
441 intern_type
*& __to_next
) const
443 result __ret
= error
;
444 if (__state
._M_good())
446 typedef state_type::__desc_type __desc_type
;
447 const __desc_type
* __desc
= __state
._M_get_in_descriptor();
448 const size_t __fmultiple
= sizeof(extern_type
) / sizeof(char);
449 size_t __flen
= __fmultiple
* (__from_end
- __from
);
450 const size_t __tmultiple
= sizeof(intern_type
) / sizeof(char);
451 size_t __tlen
= __tmultiple
* (__to_end
- __to
);
453 // Argument list for iconv specifies a byte sequence. Thus,
454 // all to/from arrays must be brutally casted to char*.
455 char* __cfrom
= reinterpret_cast<char*>(const_cast<extern_type
*>(__from
));
456 char* __cto
= reinterpret_cast<char*>(__to
);
457 size_t __conv
= iconv(*__desc
, &__cfrom
, &__flen
, &__cto
, &__tlen
);
459 if (__conv
!= size_t(-1))
461 __from_next
= reinterpret_cast<const extern_type
*>(__cfrom
);
462 __to_next
= reinterpret_cast<intern_type
*>(__cto
);
467 if (__flen
< __from_end
- __from
)
469 __from_next
= reinterpret_cast<const extern_type
*>(__cfrom
);
470 __to_next
= reinterpret_cast<intern_type
*>(__cto
);
480 template<typename _InternT
, typename _ExternT
>
482 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
483 do_encoding() const throw()
486 template<typename _InternT
, typename _ExternT
>
488 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
489 do_always_noconv() const throw()
492 template<typename _InternT
, typename _ExternT
>
494 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
495 do_length(const state_type
& __state
, const extern_type
* __from
,
496 const extern_type
* __end
, size_t __max
) const
497 { return min(__max
, static_cast<size_t>(__end
- __from
)); }
499 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
500 // 74. Garbled text for codecvt::do_max_length
501 template<typename _InternT
, typename _ExternT
>
503 codecvt
<_InternT
, _ExternT
, __enc_traits
>::
504 do_max_length() const throw()
508 // codecvt<char, char, mbstate_t> required specialization
510 class codecvt
<char, char, mbstate_t>
511 : public __codecvt_abstract_base
<char, char, mbstate_t>
515 typedef char intern_type
;
516 typedef char extern_type
;
517 typedef mbstate_t state_type
;
520 static locale::id id
;
523 codecvt(size_t __refs
= 0);
530 do_out(state_type
& __state
, const intern_type
* __from
,
531 const intern_type
* __from_end
, const intern_type
*& __from_next
,
532 extern_type
* __to
, extern_type
* __to_end
,
533 extern_type
*& __to_next
) const;
536 do_unshift(state_type
& __state
, extern_type
* __to
,
537 extern_type
* __to_end
, extern_type
*& __to_next
) const;
540 do_in(state_type
& __state
, const extern_type
* __from
,
541 const extern_type
* __from_end
, const extern_type
*& __from_next
,
542 intern_type
* __to
, intern_type
* __to_end
,
543 intern_type
*& __to_next
) const;
546 do_encoding() const throw();
549 do_always_noconv() const throw();
552 do_length(const state_type
&, const extern_type
* __from
,
553 const extern_type
* __end
, size_t __max
) const;
556 do_max_length() const throw();
559 #ifdef _GLIBCPP_USE_WCHAR_T
560 // codecvt<wchar_t, char, mbstate_t> required specialization
562 class codecvt
<wchar_t, char, mbstate_t>
563 : public __codecvt_abstract_base
<wchar_t, char, mbstate_t>
567 typedef wchar_t intern_type
;
568 typedef char extern_type
;
569 typedef mbstate_t state_type
;
572 static locale::id id
;
575 codecvt(size_t __refs
= 0);
582 do_out(state_type
& __state
, const intern_type
* __from
,
583 const intern_type
* __from_end
, const intern_type
*& __from_next
,
584 extern_type
* __to
, extern_type
* __to_end
,
585 extern_type
*& __to_next
) const;
588 do_unshift(state_type
& __state
,
589 extern_type
* __to
, extern_type
* __to_end
,
590 extern_type
*& __to_next
) const;
593 do_in(state_type
& __state
,
594 const extern_type
* __from
, const extern_type
* __from_end
,
595 const extern_type
*& __from_next
,
596 intern_type
* __to
, intern_type
* __to_end
,
597 intern_type
*& __to_next
) const;
600 int do_encoding() const throw();
603 bool do_always_noconv() const throw();
606 int do_length(const state_type
&, const extern_type
* __from
,
607 const extern_type
* __end
, size_t __max
) const;
610 do_max_length() const throw();
612 #endif //_GLIBCPP_USE_WCHAR_T
614 // 22.2.1.6 Template class codecvt_byname
615 template<typename _InternT
, typename _ExternT
, typename _StateT
>
616 class codecvt_byname
: public codecvt
<_InternT
, _ExternT
, _StateT
>
620 codecvt_byname(const char*, size_t __refs
= 0)
621 : codecvt
<_InternT
,_ExternT
,_StateT
> (__refs
) { }
624 ~codecvt_byname() { }
628 class codecvt_byname
<char, char, mbstate_t>
629 : public codecvt
<char, char, mbstate_t>
633 codecvt_byname(const char*, size_t __refs
= 0);
640 #ifdef _GLIBCPP_USE_WCHAR_T
642 class codecvt_byname
<wchar_t, char, mbstate_t>
643 : public codecvt
<wchar_t, char, mbstate_t>
647 codecvt_byname(const char*, size_t __refs
= 0);
657 #endif // _CPP_BITS_CODECVT_H