OSDN Git Service

2001-08-07 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / codecvt_specializations_ieee_1003.1-200x.h
1 // Locale support (codecvt) -*- C++ -*-
2
3 // Copyright (C) 2000, 2001 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   // __enc_traits may need to move up the locale header hierarchy,
40   // depending on if ctype ends up using it.
41
42   // Extensions 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
47 #if _GLIBCPP_USE_SHADOW_HEADERS
48   using _C_legacy::CODESET;
49 #endif
50
51   // XXX
52   // Define this here to codecvt.cc can have _S_max_size definition.
53 #define _GLIBCPP_USE___ENC_TRAITS 1
54
55   class __enc_traits
56   {
57   public:
58     // Types: 
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;
62     
63   protected:
64     // Data Members:
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_int_enc[_S_max_size];
69     // Name of external character set encoding.
70     char                _M_ext_enc[_S_max_size];
71
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;
76
77     // Details the byte-order marker for the external encoding, if necessary.
78     int                 _M_ext_bom;
79
80     // Details the byte-order marker for the internal encoding, if necessary.
81     int                 _M_int_bom;
82
83   public:
84     __enc_traits(const locale& __loc = locale())
85     : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
86     {
87       // __intc_end = whatever we are using internally, which is
88       // UCS4 (linux) 
89       // UCS2 == UNICODE  (microsoft, java, aix, whatever...)
90       // XXX Currently don't know how to get this data from target system...
91       strcpy(_M_int_enc, "UCS4");
92
93       // __extc_end = external codeset in current locale
94       // XXX There has got to be a better way to do this.
95       __c_locale __cloc;
96       locale::facet::_S_create_c_locale(__cloc, __loc.name().c_str());
97       strcpy(_M_ext_enc, __nl_langinfo_l(CODESET, __cloc));
98       locale::facet::_S_destroy_c_locale(__cloc);
99     }
100
101     __enc_traits(const char* __int, const char* __ext, int __ibom = 0, 
102                  int __ebom = 0)
103     : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0)
104     {
105       strncpy(_M_int_enc, __int, _S_max_size);
106       strncpy(_M_ext_enc, __ext, _S_max_size);
107     }
108
109     // 21.1.2 traits typedefs
110     // p4
111     // typedef STATE_T state_type
112     // requires: state_type shall meet the requirements of
113     // CopyConstructible types (20.1.3)
114     __enc_traits(const __enc_traits& __obj)
115     {
116       strncpy(_M_int_enc, __obj._M_int_enc, _S_max_size);
117       strncpy(_M_ext_enc, __obj._M_ext_enc, _S_max_size);
118       _M_ext_bom = __obj._M_ext_bom;
119       _M_int_bom = __obj._M_int_bom;
120     }
121
122     ~__enc_traits()
123     {
124       iconv_close(_M_in_desc);
125       iconv_close(_M_out_desc);
126     } 
127
128     // Initializes
129     void
130     _M_init()
131     {
132       _M_in_desc = iconv_open(_M_int_enc, _M_ext_enc);
133       _M_out_desc = iconv_open(_M_ext_enc, _M_int_enc);
134       if (_M_out_desc == iconv_t(-1) || _M_in_desc == iconv_t(-1))
135         {
136           // XXX Extended error checking.
137         }
138     }
139
140     bool
141     _M_good()
142     { 
143       return _M_out_desc && _M_in_desc 
144              && _M_out_desc != iconv_t(-1) && _M_in_desc != iconv_t(-1);
145     }
146
147     const __desc_type* 
148     _M_get_in_descriptor()
149     { return &_M_in_desc; }
150
151     const __desc_type* 
152     _M_get_out_descriptor()
153     { return &_M_out_desc; }
154
155    const char* 
156     _M_get_internal_enc()
157     { return _M_int_enc; }
158
159     const char* 
160     _M_get_external_enc()
161     { return _M_ext_enc; }
162
163     int 
164     _M_get_external_bom()
165     { return _M_ext_bom; }
166
167     int 
168     _M_get_internal_bom()
169     { return _M_int_bom; }
170   };
171
172   // Partial specialization
173   // This specialization takes advantage of iconv to provide code
174   // conversions between a large number of character encodings.
175   template<typename _InternT, typename _ExternT>
176     class codecvt<_InternT, _ExternT, __enc_traits>
177     : public __codecvt_abstract_base<_InternT, _ExternT, __enc_traits>
178     {
179     public:      
180       // Types:
181       typedef codecvt_base::result                      result;
182       typedef _InternT                                  intern_type;
183       typedef _ExternT                                  extern_type;
184       typedef __enc_traits                              state_type;
185       typedef __enc_traits::__desc_type                 __desc_type;
186       typedef __enc_traits                              __enc_type;
187
188       // Data Members:
189       static locale::id                 id;
190
191       explicit 
192       codecvt(size_t __refs = 0)
193       : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
194       { }
195
196       explicit 
197       codecvt(__enc_type* __enc, size_t __refs = 0)
198       : __codecvt_abstract_base<intern_type, extern_type, state_type>(__refs)
199       { }
200
201     protected:
202       virtual 
203       ~codecvt() { }
204
205       virtual result
206       do_out(state_type& __state, const intern_type* __from, 
207              const intern_type* __from_end, const intern_type*& __from_next,
208              extern_type* __to, extern_type* __to_end,
209              extern_type*& __to_next) const;
210
211       virtual result
212       do_unshift(state_type& __state, extern_type* __to, 
213                  extern_type* __to_end, extern_type*& __to_next) const;
214
215       virtual result
216       do_in(state_type& __state, const extern_type* __from, 
217             const extern_type* __from_end, const extern_type*& __from_next,
218             intern_type* __to, intern_type* __to_end, 
219             intern_type*& __to_next) const;
220
221       virtual int 
222       do_encoding() const throw();
223
224       virtual bool 
225       do_always_noconv() const throw();
226
227       virtual int 
228       do_length(const state_type&, const extern_type* __from, 
229                 const extern_type* __end, size_t __max) const;
230
231       virtual int 
232       do_max_length() const throw();
233     };
234
235   template<typename _InternT, typename _ExternT>
236     locale::id 
237     codecvt<_InternT, _ExternT, __enc_traits>::id;
238
239   // This adaptor works around the signature problems of the second
240   // argument to iconv():  SUSv2 and others use 'const char**', but glibc 2.2
241   // uses 'char**', which is what the standard is (apparently) due to use
242   // in the future.  Using this adaptor, g++ will do the work for us.
243   template<typename _T>
244     inline size_t
245     __iconv_adaptor(size_t(*iconv_func)(iconv_t, _T, size_t*, char**, size_t*),
246                     iconv_t cd, char** inbuf, size_t* inbytesleft,
247                     char** outbuf, size_t* outbytesleft)
248     {
249       return iconv_func(cd, (_T)inbuf, inbytesleft, outbuf, outbytesleft);
250     }
251
252   template<typename _InternT, typename _ExternT>
253     codecvt_base::result
254     codecvt<_InternT, _ExternT, __enc_traits>::
255     do_out(state_type& __state, const intern_type* __from, 
256            const intern_type* __from_end, const intern_type*& __from_next,
257            extern_type* __to, extern_type* __to_end,
258            extern_type*& __to_next) const
259     {
260       result __ret = error;
261       if (__state._M_good())
262         {
263           typedef state_type::__desc_type       __desc_type;
264           const __desc_type* __desc = __state._M_get_out_descriptor();
265           const size_t __fmultiple = sizeof(intern_type) / sizeof(char);
266           size_t __flen = __fmultiple * (__from_end - __from);
267           const size_t __tmultiple = sizeof(extern_type) / sizeof(char);
268           size_t __tlen = __tmultiple * (__to_end - __to); 
269           
270           // Argument list for iconv specifies a byte sequence. Thus,
271           // all to/from arrays must be brutally casted to char*.
272           char* __cto = reinterpret_cast<char*>(__to);
273           char* __cfrom;
274           size_t __conv;
275
276           // Some encodings need a byte order marker as the first item
277           // in the byte stream, to designate endian-ness. The default
278           // value for the byte order marker is NULL, so if this is
279           // the case, it's not necessary and we can just go on our
280           // merry way.
281           int __int_bom = __state._M_get_internal_bom();
282           if (__int_bom)
283             {     
284               size_t __size = __from_end - __from;
285               intern_type* __cfixed = static_cast<intern_type*>(__builtin_alloca(sizeof(intern_type) * (__size + 1)));
286               __cfixed[0] = static_cast<intern_type>(__int_bom);
287               char_traits<intern_type>::copy(__cfixed + 1, __from, __size);
288               __cfrom = reinterpret_cast<char*>(__cfixed);
289               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
290                                         &__flen, &__cto, &__tlen); 
291             }
292           else
293             {
294               intern_type* __cfixed = const_cast<intern_type*>(__from);
295               __cfrom = reinterpret_cast<char*>(__cfixed);
296               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
297                                        &__flen, &__cto, &__tlen); 
298             }
299
300           if (__conv != size_t(-1))
301             {
302               __from_next = reinterpret_cast<const intern_type*>(__cfrom);
303               __to_next = reinterpret_cast<extern_type*>(__cto);
304               __ret = ok;
305             }
306           else 
307             {
308               if (__flen < static_cast<size_t>(__from_end - __from))
309                 {
310                   __from_next = reinterpret_cast<const intern_type*>(__cfrom);
311                   __to_next = reinterpret_cast<extern_type*>(__cto);
312                   __ret = partial;
313                 }
314               else
315                 __ret = error;
316             }
317         }
318       return __ret; 
319     }
320
321   template<typename _InternT, typename _ExternT>
322     codecvt_base::result
323     codecvt<_InternT, _ExternT, __enc_traits>::
324     do_unshift(state_type& __state, extern_type* __to, 
325                extern_type* __to_end, extern_type*& __to_next) const
326     {
327       result __ret = error;
328       if (__state._M_good())
329         {
330           typedef state_type::__desc_type       __desc_type;
331           const __desc_type* __desc = __state._M_get_in_descriptor();
332           const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
333           size_t __tlen = __tmultiple * (__to_end - __to); 
334           
335           // Argument list for iconv specifies a byte sequence. Thus,
336           // all to/from arrays must be brutally casted to char*.
337           char* __cto = reinterpret_cast<char*>(__to);
338           size_t __conv = __iconv_adaptor(iconv,*__desc, NULL, NULL,
339                                           &__cto, &__tlen); 
340           
341           if (__conv != size_t(-1))
342             {
343               __to_next = reinterpret_cast<extern_type*>(__cto);
344               if (__tlen == __tmultiple * (__to_end - __to))
345                 __ret = noconv;
346               else if (__tlen == 0)
347                 __ret = ok;
348               else
349                 __ret = partial;
350             }
351           else 
352             __ret = error;
353         }
354       return __ret; 
355     }
356    
357   template<typename _InternT, typename _ExternT>
358     codecvt_base::result
359     codecvt<_InternT, _ExternT, __enc_traits>::
360     do_in(state_type& __state, const extern_type* __from, 
361           const extern_type* __from_end, const extern_type*& __from_next,
362           intern_type* __to, intern_type* __to_end, 
363           intern_type*& __to_next) const
364     { 
365       result __ret = error;
366       if (__state._M_good())
367         {
368           typedef state_type::__desc_type       __desc_type;
369           const __desc_type* __desc = __state._M_get_in_descriptor();
370           const size_t __fmultiple = sizeof(extern_type) / sizeof(char);
371           size_t __flen = __fmultiple * (__from_end - __from);
372           const size_t __tmultiple = sizeof(intern_type) / sizeof(char);
373           size_t __tlen = __tmultiple * (__to_end - __to); 
374           
375           // Argument list for iconv specifies a byte sequence. Thus,
376           // all to/from arrays must be brutally casted to char*.
377           char* __cto = reinterpret_cast<char*>(__to);
378           char* __cfrom;
379           size_t __conv;
380
381           // Some encodings need a byte order marker as the first item
382           // in the byte stream, to designate endian-ness. The default
383           // value for the byte order marker is NULL, so if this is
384           // the case, it's not necessary and we can just go on our
385           // merry way.
386           int __ext_bom = __state._M_get_external_bom();
387           if (__ext_bom)
388             {     
389               size_t __size = __from_end - __from;
390               extern_type* __cfixed =  static_cast<extern_type*>(__builtin_alloca(sizeof(extern_type) * (__size + 1)));
391               __cfixed[0] = static_cast<extern_type>(__ext_bom);
392               char_traits<extern_type>::copy(__cfixed + 1, __from, __size);
393               __cfrom = reinterpret_cast<char*>(__cfixed);
394               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
395                                        &__flen, &__cto, &__tlen); 
396             }
397           else
398             {
399               extern_type* __cfixed = const_cast<extern_type*>(__from);
400               __cfrom = reinterpret_cast<char*>(__cfixed);
401               __conv = __iconv_adaptor(iconv, *__desc, &__cfrom,
402                                        &__flen, &__cto, &__tlen); 
403             }
404
405           
406           if (__conv != size_t(-1))
407             {
408               __from_next = reinterpret_cast<const extern_type*>(__cfrom);
409               __to_next = reinterpret_cast<intern_type*>(__cto);
410               __ret = ok;
411             }
412           else 
413             {
414               if (__flen < static_cast<size_t>(__from_end - __from))
415                 {
416                   __from_next = reinterpret_cast<const extern_type*>(__cfrom);
417                   __to_next = reinterpret_cast<intern_type*>(__cto);
418                   __ret = partial;
419                 }
420               else
421                 __ret = error;
422             }
423         }
424       return __ret; 
425     }
426   
427   template<typename _InternT, typename _ExternT>
428     int 
429     codecvt<_InternT, _ExternT, __enc_traits>::
430     do_encoding() const throw()
431     { return 0; }
432   
433   template<typename _InternT, typename _ExternT>
434     bool 
435     codecvt<_InternT, _ExternT, __enc_traits>::
436     do_always_noconv() const throw()
437     { return false; }
438   
439   template<typename _InternT, typename _ExternT>
440     int 
441     codecvt<_InternT, _ExternT, __enc_traits>::
442     do_length(const state_type&, const extern_type* __from, 
443               const extern_type* __end, size_t __max) const
444     { return min(__max, static_cast<size_t>(__end - __from)); }
445
446 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
447 // 74.  Garbled text for codecvt::do_max_length
448   template<typename _InternT, typename _ExternT>
449     int 
450     codecvt<_InternT, _ExternT, __enc_traits>::
451     do_max_length() const throw()
452     { return 1; }
453 #endif