OSDN Git Service

2003-10-12 Paolo Carlini <pcarlini@unitus.it>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / ieee_1003.1-2001 / codecvt_specializations.h
1 // Locale support (codecvt) -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 22.2.1.5 Template class codecvt
32 //
33
34 // Warning: this file is not meant for user inclusion.  Use <locale>.
35
36 // Written by Benjamin Kosnik <bkoz@cygnus.com>
37
38   // XXX
39   // Define this here so codecvt.cc can have _S_max_size definition.
40 #define _GLIBCXX_USE___ENC_TRAITS 1
41
42   // Extension to use icov for dealing with character encodings,
43   // including conversions and comparisons between various character
44   // sets.  This object encapsulates data that may need to be shared between
45   // char_traits, codecvt and ctype.
46   class __enc_traits
47   {
48   public:
49     // Types: 
50     // NB: A conversion descriptor subsumes and enhances the
51     // functionality of a simple state type such as mbstate_t.
52     typedef iconv_t     __desc_type;
53     
54   protected:
55     // Data Members:
56     // Max size of charset encoding name
57     static const int    _S_max_size = 32;
58     // Name of internal character set encoding.
59     char                _M_int_enc[_S_max_size];
60     // Name of external character set encoding.
61     char                _M_ext_enc[_S_max_size];
62
63     // Conversion descriptor between external encoding to internal encoding.
64     __desc_type         _M_in_desc;
65     // Conversion descriptor between internal encoding to external encoding.
66     __desc_type         _M_out_desc;
67
68     // Details the byte-order marker for the external encoding, if necessary.
69     int                 _M_ext_bom;
70
71     // Details the byte-order marker for the internal encoding, if necessary.
72     int                 _M_int_bom;
73
74   public:
75     explicit __enc_traits() 
76     : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0) 
77     {
78       memset(_M_int_enc, 0, _S_max_size);
79       memset(_M_ext_enc, 0, _S_max_size);
80     }
81
82     explicit __enc_traits(const char* __int, const char* __ext, 
83                           int __ibom = 0, int __ebom = 0)
84     : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(__ebom), _M_int_bom(__ibom)
85     {
86       strncpy(_M_int_enc, __int, _S_max_size);
87       strncpy(_M_ext_enc, __ext, _S_max_size);
88     }
89
90     // 21.1.2 traits typedefs
91     // p4
92     // typedef STATE_T state_type
93     // requires: state_type shall meet the requirements of
94     // CopyConstructible types (20.1.3)
95     __enc_traits(const __enc_traits& __obj): _M_in_desc(0), _M_out_desc(0)
96     {
97       strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
98       strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
99       _M_ext_bom = __obj._M_ext_bom;
100       _M_int_bom = __obj._M_int_bom;
101     }
102
103     // Need assignment operator as well.
104     __enc_traits&
105     operator=(const __enc_traits& __obj)
106     {
107       strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
108       strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
109       _M_in_desc = 0;
110       _M_out_desc = 0;
111       _M_ext_bom = __obj._M_ext_bom;
112       _M_int_bom = __obj._M_int_bom;
113       return *this;
114     }
115
116     ~__enc_traits()
117     {
118       __desc_type __err = reinterpret_cast<iconv_t>(-1);
119       if (_M_in_desc && _M_in_desc != __err) 
120         iconv_close(_M_in_desc);
121       if (_M_out_desc && _M_out_desc != __err) 
122         iconv_close(_M_out_desc);
123     } 
124
125     void
126     _M_init()
127     {
128       const __desc_type __err = reinterpret_cast<iconv_t>(-1);
129       if (!_M_in_desc)
130         {
131           _M_in_desc = iconv_open(_M_int_enc, _M_ext_enc);
132           if (_M_in_desc == __err)
133             __throw_runtime_error("__enc_traits::_M_init "
134                                   "creating iconv input descriptor failed");
135         }
136       if (!_M_out_desc)
137         {
138           _M_out_desc = iconv_open(_M_ext_enc, _M_int_enc);
139           if (_M_out_desc == __err)
140             __throw_runtime_error("__enc_traits::_M_init "
141                                   "creating iconv output descriptor failed");
142         }
143     }
144
145     bool
146     _M_good()
147     { 
148       const __desc_type __err = reinterpret_cast<iconv_t>(-1);
149       bool __test = _M_in_desc && _M_in_desc != __err; 
150       __test &=  _M_out_desc && _M_out_desc != __err;
151       return __test;
152     }
153
154     const __desc_type* 
155     _M_get_in_descriptor()
156     { return &_M_in_desc; }
157
158     const __desc_type* 
159     _M_get_out_descriptor()
160     { return &_M_out_desc; }
161
162     int 
163     _M_get_external_bom()
164     { return _M_ext_bom; }
165
166     int 
167     _M_get_internal_bom()
168     { return _M_int_bom; }
169
170     const char* 
171     _M_get_internal_enc()
172     { return _M_int_enc; }
173
174     const char* 
175     _M_get_external_enc()
176     { return _M_ext_enc; }
177   };
178
179   // Partial specialization
180   // This specialization takes advantage of iconv to provide code
181   // conversions between a large number of character encodings.
182   template<typename _InternT, typename _ExternT>
183     class codecvt<_InternT, _ExternT, __enc_traits>
184     : public __codecvt_abstract_base<_InternT, _ExternT, __enc_traits>
185     {
186     public:      
187       // Types:
188       typedef codecvt_base::result                      result;
189       typedef _InternT                                  intern_type;
190       typedef _ExternT                                  extern_type;
191       typedef __enc_traits                              state_type;
192       typedef __enc_traits::__desc_type                 __desc_type;
193       typedef __enc_traits                              __enc_type;
194
195       // Data Members:
196       static locale::id                 id;
197
198       explicit 
199       codecvt(size_t __refs = 0)
200       : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
201       { }
202
203       explicit 
204       codecvt(__enc_type* __enc, size_t __refs = 0)
205       : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
206       { }
207
208     protected:
209       virtual 
210       ~codecvt() { }
211
212       virtual result
213       do_out(state_type& __state, const intern_type* __from, 
214              const intern_type* __from_end, const intern_type*& __from_next,
215              extern_type* __to, extern_type* __to_end,
216              extern_type*& __to_next) const;
217
218       virtual result
219       do_unshift(state_type& __state, extern_type* __to, 
220                  extern_type* __to_end, extern_type*& __to_next) const;
221
222       virtual result
223       do_in(state_type& __state, const extern_type* __from, 
224             const extern_type* __from_end, const extern_type*& __from_next,
225             intern_type* __to, intern_type* __to_end, 
226             intern_type*& __to_next) const;
227
228       virtual int 
229       do_encoding() const throw();
230
231       virtual bool 
232       do_always_noconv() const throw();
233
234       virtual int 
235       do_length(state_type&, const extern_type* __from, 
236                 const extern_type* __end, size_t __max) const;
237
238       virtual int 
239       do_max_length() const throw();
240     };
241
242   template<typename _InternT, typename _ExternT>
243     locale::id 
244     codecvt<_InternT, _ExternT, __enc_traits>::id;
245
246   // This adaptor works around the signature problems of the second
247   // argument to iconv():  SUSv2 and others use 'const char**', but glibc 2.2
248   // uses 'char**', which matches the POSIX 1003.1-2001 standard.
249   // Using this adaptor, g++ will do the work for us.
250   template<typename _T>
251     inline size_t
252     __iconv_adaptor(size_t(*__func)(iconv_t, _T, size_t*, char**, size_t*),
253                     iconv_t __cd, char** __inbuf, size_t* __inbytes,
254                     char** __outbuf, size_t* __outbytes)
255     { return __func(__cd, (_T)__inbuf, __inbytes, __outbuf, __outbytes); }
256
257   template<typename _InternT, typename _ExternT>
258     codecvt_base::result
259     codecvt<_InternT, _ExternT, __enc_traits>::
260     do_out(state_type& __state, const intern_type* __from, 
261            const intern_type* __from_end, const intern_type*& __from_next,
262            extern_type* __to, extern_type* __to_end,
263            extern_type*& __to_next) const
264     {
265       result __ret = codecvt_base::error;
266       if (__state._M_good())
267         {
268           typedef state_type::__desc_type       __desc_type;
269           const __desc_type* __desc = __state._M_get_out_descriptor();
270           const size_t __fmultiple = sizeof(intern_type);
271           size_t __fbytes = __fmultiple * (__from_end - __from);
272           const size_t __tmultiple = sizeof(extern_type);
273           size_t __tbytes = __tmultiple * (__to_end - __to); 
274           
275           // Argument list for iconv specifies a byte sequence. Thus,
276           // all to/from arrays must be brutally casted to char*.
277           char* __cto = reinterpret_cast<char*>(__to);
278           char* __cfrom;
279           size_t __conv;
280
281           // Some encodings need a byte order marker as the first item
282           // in the byte stream, to designate endian-ness. The default
283           // value for the byte order marker is NULL, so if this is
284           // the case, it's not necessary and we can just go on our
285           // merry way.
286           int __int_bom = __state._M_get_internal_bom();
287           if (__int_bom)
288             {     
289               size_t __size = __from_end - __from;
290               intern_type* __cfixed = static_cast<intern_type*>(__builtin_alloca(sizeof(intern_type) * (__size + 1)));
291               __cfixed[0] = static_cast<intern_type>(__int_bom);
292               char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
293               __cfrom = reinterpret_cast<char*>(__cfixed);
294               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
295                                         &__fbytes, &__cto, &__tbytes); 
296             }
297           else
298             {
299               intern_type* __cfixed = const_cast<intern_type*>(__from);
300               __cfrom = reinterpret_cast<char*>(__cfixed);
301               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom, &__fbytes, 
302                                        &__cto, &__tbytes); 
303             }
304
305           if (__conv != size_t(-1))
306             {
307               __from_next = reinterpret_cast<const intern_type*>(__cfrom);
308               __to_next = reinterpret_cast<extern_type*>(__cto);
309               __ret = codecvt_base::ok;
310             }
311           else 
312             {
313               if (__fbytes < __fmultiple * (__from_end - __from))
314                 {
315                   __from_next = reinterpret_cast<const intern_type*>(__cfrom);
316                   __to_next = reinterpret_cast<extern_type*>(__cto);
317                   __ret = codecvt_base::partial;
318                 }
319               else
320                 __ret = codecvt_base::error;
321             }
322         }
323       return __ret; 
324     }
325
326   template<typename _InternT, typename _ExternT>
327     codecvt_base::result
328     codecvt<_InternT, _ExternT, __enc_traits>::
329     do_unshift(state_type& __state, extern_type* __to, 
330                extern_type* __to_end, extern_type*& __to_next) const
331     {
332       result __ret = codecvt_base::error;
333       if (__state._M_good())
334         {
335           typedef state_type::__desc_type       __desc_type;
336           const __desc_type* __desc = __state._M_get_in_descriptor();
337           const size_t __tmultiple = sizeof(intern_type);
338           size_t __tlen = __tmultiple * (__to_end - __to); 
339           
340           // Argument list for iconv specifies a byte sequence. Thus,
341           // all to/from arrays must be brutally casted to char*.
342           char* __cto = reinterpret_cast<char*>(__to);
343           size_t __conv = __iconv_adaptor(iconv,*__desc, NULL, NULL,
344                                           &__cto, &__tlen); 
345           
346           if (__conv != size_t(-1))
347             {
348               __to_next = reinterpret_cast<extern_type*>(__cto);
349               if (__tlen == __tmultiple * (__to_end - __to))
350                 __ret = codecvt_base::noconv;
351               else if (__tlen == 0)
352                 __ret = codecvt_base::ok;
353               else
354                 __ret = codecvt_base::partial;
355             }
356           else 
357             __ret = codecvt_base::error;
358         }
359       return __ret; 
360     }
361    
362   template<typename _InternT, typename _ExternT>
363     codecvt_base::result
364     codecvt<_InternT, _ExternT, __enc_traits>::
365     do_in(state_type& __state, const extern_type* __from, 
366           const extern_type* __from_end, const extern_type*& __from_next,
367           intern_type* __to, intern_type* __to_end, 
368           intern_type*& __to_next) const
369     { 
370       result __ret = codecvt_base::error;
371       if (__state._M_good())
372         {
373           typedef state_type::__desc_type       __desc_type;
374           const __desc_type* __desc = __state._M_get_in_descriptor();
375           const size_t __fmultiple = sizeof(extern_type);
376           size_t __flen = __fmultiple * (__from_end - __from);
377           const size_t __tmultiple = sizeof(intern_type);
378           size_t __tlen = __tmultiple * (__to_end - __to); 
379           
380           // Argument list for iconv specifies a byte sequence. Thus,
381           // all to/from arrays must be brutally casted to char*.
382           char* __cto = reinterpret_cast<char*>(__to);
383           char* __cfrom;
384           size_t __conv;
385
386           // Some encodings need a byte order marker as the first item
387           // in the byte stream, to designate endian-ness. The default
388           // value for the byte order marker is NULL, so if this is
389           // the case, it's not necessary and we can just go on our
390           // merry way.
391           int __ext_bom = __state._M_get_external_bom();
392           if (__ext_bom)
393             {     
394               size_t __size = __from_end - __from;
395               extern_type* __cfixed =  static_cast<extern_type*>(__builtin_alloca(sizeof(extern_type) * (__size + 1)));
396               __cfixed[0] = static_cast<extern_type>(__ext_bom);
397               char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
398               __cfrom = reinterpret_cast<char*>(__cfixed);
399               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
400                                        &__flen, &__cto, &__tlen); 
401             }
402           else
403             {
404               extern_type* __cfixed = const_cast<extern_type*>(__from);
405               __cfrom = reinterpret_cast<char*>(__cfixed);
406               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
407                                        &__flen, &__cto, &__tlen); 
408             }
409
410           
411           if (__conv != size_t(-1))
412             {
413               __from_next = reinterpret_cast<const extern_type*>(__cfrom);
414               __to_next = reinterpret_cast<intern_type*>(__cto);
415               __ret = codecvt_base::ok;
416             }
417           else 
418             {
419               if (__flen < static_cast<size_t>(__from_end - __from))
420                 {
421                   __from_next = reinterpret_cast<const extern_type*>(__cfrom);
422                   __to_next = reinterpret_cast<intern_type*>(__cto);
423                   __ret = codecvt_base::partial;
424                 }
425               else
426                 __ret = codecvt_base::error;
427             }
428         }
429       return __ret; 
430     }
431   
432   template<typename _InternT, typename _ExternT>
433     int 
434     codecvt<_InternT, _ExternT, __enc_traits>::
435     do_encoding() const throw()
436     {
437       int __ret = 0;
438       if (sizeof(_ExternT) <= sizeof(_InternT))
439         __ret = sizeof(_InternT)/sizeof(_ExternT);
440       return __ret; 
441     }
442   
443   template<typename _InternT, typename _ExternT>
444     bool 
445     codecvt<_InternT, _ExternT, __enc_traits>::
446     do_always_noconv() const throw()
447     { return false; }
448   
449   template<typename _InternT, typename _ExternT>
450     int 
451     codecvt<_InternT, _ExternT, __enc_traits>::
452     do_length(state_type&, const extern_type* __from, 
453               const extern_type* __end, size_t __max) const
454     { return std::min(__max, static_cast<size_t>(__end - __from)); }
455
456   // _GLIBCXX_RESOLVE_LIB_DEFECTS
457   // 74.  Garbled text for codecvt::do_max_length
458   template<typename _InternT, typename _ExternT>
459     int 
460     codecvt<_InternT, _ExternT, __enc_traits>::
461     do_max_length() const throw()
462     { return 1; }
463