1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 // WARNING: This is an internal header file, included by other C++
19 // standard library headers.  You should not attempt to use this header
20 // file directly.
21 
22 
23 #ifndef _STLP_INTERNAL_CODECVT_H
24 #define _STLP_INTERNAL_CODECVT_H
25 
26 #ifndef _STLP_C_LOCALE_H
27 #  include <stl/c_locale.h>
28 #endif
29 
30 #ifndef _STLP_INTERNAL_LOCALE_H
31 #  include <stl/_locale.h>
32 #endif
33 
34 #ifndef _STLP_INTERNAL_ALGOBASE_H
35 #  include <stl/_algobase.h>
36 #endif
37 
38 _STLP_BEGIN_NAMESPACE
39 
40 class _STLP_CLASS_DECLSPEC codecvt_base {
41 public:
42   enum result {ok, partial, error, noconv};
43 };
44 
45 template <class _InternT, class _ExternT, class _StateT>
46 class codecvt : public locale::facet, public codecvt_base {
47 public:
48   typedef _InternT intern_type;
49   typedef _ExternT extern_type;
50   typedef _StateT state_type;
51 
52 #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300)
53   /* For the moment VC6 do not support this facet default implementation
54    * because of the static locale::id instance. When VC6 see this definition
55    * it goes crasy with locale::id static instances and all the has_facet tests
56    * unit tests are failing.
57    */
58 };
59 #else
60   explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
61 
62   result out(state_type&          __state,
63              const intern_type*   __from,
64              const intern_type*   __from_end,
65              const intern_type*&  __from_next,
66              extern_type*         __to,
67              extern_type*         __to_limit,
68              extern_type*&        __to_next) const {
69     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
70     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
71     return do_out(__state,
72                   __from, __from_end, __from_next,
73                   __to,   __to_limit, __to_next);
74   }
75 
76   result unshift(state_type&    __state,
77                  extern_type*   __to,
78                  extern_type*   __to_limit,
79                  extern_type*&  __to_next) const {
80     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
81     return do_unshift(__state, __to, __to_limit, __to_next);
82   }
83 
84   result in(state_type&         __state,
85             const extern_type*  __from,
86             const extern_type*  __from_end,
87             const extern_type*& __from_next,
88             intern_type*        __to,
89             intern_type*        __to_limit,
90             intern_type*&       __to_next) const {
91     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
92     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
93     return do_in(__state,
94                  __from, __from_end, __from_next,
95                  __to,  __to_limit, __to_next);
96   }
97 
98   int encoding() const _STLP_NOTHROW { return do_encoding(); }
99 
100   bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
101 
102   int length(state_type&  __state,
103              const extern_type* __from,
104              const extern_type* __from_end,
105              size_t             __max) const {
106     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
107     return do_length(__state, __from, __from_end, __max);
108   }
109 
110   int max_length() const _STLP_NOTHROW { return do_max_length(); }
111 
112   static locale::id id;
113 
114 protected:
115   ~codecvt() {}
116 
117   virtual result do_out(state_type&,
118                         const intern_type*  __from,
119                         const intern_type*,
120                         const intern_type*& __from_next,
121                         extern_type*        __to,
122                         extern_type*,
123                         extern_type*&       __to_next) const
124   { __from_next = __from; __to_next   = __to; return noconv; }
125 
126   virtual result do_in (state_type&,
127                         const extern_type*  __from,
128                         const extern_type*,
129                         const extern_type*& __from_next,
130                         intern_type*        __to,
131                         intern_type*,
132                         intern_type*&       __to_next) const
133   { __from_next = __from; __to_next = __to; return noconv; }
134 
135   virtual result do_unshift(state_type&,
136                             extern_type* __to,
137                             extern_type*,
138                             extern_type*& __to_next) const
139   { __to_next = __to; return noconv; }
140 
141   virtual int do_encoding() const _STLP_NOTHROW
142   { return 1; }
143 
144   virtual bool do_always_noconv() const _STLP_NOTHROW
145   { return true; }
146 
147   virtual int do_length(state_type&,
148                         const extern_type* __from,
149                         const extern_type* __end,
150                         size_t __max) const
151   { return (int)(min) ( __STATIC_CAST(size_t, (__end - __from)), __max); }
152 
153   virtual int do_max_length() const _STLP_NOTHROW
154   { return 1; }
155 
156 private:
157   codecvt(const codecvt<intern_type, extern_type, state_type>&);
158   codecvt<intern_type, extern_type, state_type>& operator = (const codecvt<intern_type, extern_type, state_type>&);
159 };
160 
161 #  if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
162 #    if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
163 template <class _InternT, class _ExternT, class _StateT>
164 locale::id codecvt<_InternT, _ExternT, _StateT>::id;
165 #    endif
166 #  endif
167 #endif
168 
169 template <class _InternT, class _ExternT, class _StateT>
170 class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {};
171 
172 _STLP_TEMPLATE_NULL
173 class _STLP_CLASS_DECLSPEC codecvt<char, char, mbstate_t>
174   : public locale::facet, public codecvt_base {
175 public:
176   typedef char       intern_type;
177   typedef char       extern_type;
178   typedef mbstate_t  state_type;
179 
facet(__refs)180   explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
181 
out(state_type & __state,const char * __from,const char * __from_end,const char * & __from_next,char * __to,char * __to_limit,char * & __to_next)182   result out(state_type&   __state,
183              const char*  __from,
184              const char*  __from_end,
185              const char*& __from_next,
186              char*        __to,
187              char*        __to_limit,
188              char*&       __to_next) const {
189     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
190     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
191     return do_out(__state,
192                   __from, __from_end, __from_next,
193                   __to,   __to_limit, __to_next);
194   }
195 
unshift(state_type & __state,char * __to,char * __to_limit,char * & __to_next)196   result unshift(state_type& __state,
197                  char* __to, char* __to_limit, char*& __to_next) const {
198     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
199     return do_unshift(__state, __to, __to_limit, __to_next);
200   }
201 
in(state_type & __state,const char * __from,const char * __from_end,const char * & __from_next,char * __to,char * __to_limit,char * & __to_next)202   result in(state_type&   __state,
203             const char*  __from,
204             const char*  __from_end,
205             const char*& __from_next,
206             char*        __to,
207             char*        __to_limit,
208             char*&       __to_next) const {
209     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
210     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
211     return do_in(__state,
212                  __from, __from_end, __from_next,
213                  __to,   __to_limit, __to_next);
214   }
215 
encoding()216   int encoding() const _STLP_NOTHROW { return do_encoding(); }
217 
always_noconv()218   bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
219 
length(state_type & __state,const char * __from,const char * __from_end,size_t __max)220   int length(state_type& __state,
221              const char* __from, const char* __from_end,
222              size_t __max) const {
223     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
224     return do_length(__state, __from, __from_end, __max);
225   }
226 
max_length()227   int max_length() const _STLP_NOTHROW { return do_max_length(); }
228 
229   static _STLP_STATIC_DECLSPEC locale::id id;
230 
231 protected:
232   ~codecvt();
233 
234   virtual result do_out(state_type&   /* __state */,
235                         const char*  __from,
236                         const char*  /* __from_end */,
237                         const char*& __from_next,
238                         char*        __to,
239                         char*        /* __to_limit */,
240                         char*&       __to_next) const;
241 
242   virtual result do_in (state_type&   /* __state */ ,
243                         const char*  __from,
244                         const char*  /* __from_end */,
245                         const char*& __from_next,
246                         char*        __to,
247                         char*        /* __to_end */,
248                         char*&       __to_next) const;
249 
250   virtual result do_unshift(state_type& /* __state */,
251                             char*      __to,
252                             char*      /* __to_limit */,
253                             char*&     __to_next) const;
254 
255   virtual int do_encoding() const _STLP_NOTHROW;
256   virtual bool do_always_noconv() const _STLP_NOTHROW;
257   virtual int do_length(state_type&  __state,
258                         const  char* __from,
259                         const  char* __end,
260                         size_t __max) const;
261   virtual int do_max_length() const _STLP_NOTHROW;
262 private:
263   codecvt(const codecvt<char, char, mbstate_t>&);
264   codecvt<char, char, mbstate_t>& operator =(const codecvt<char, char, mbstate_t>&);
265 };
266 
267 # ifndef _STLP_NO_WCHAR_T
268 
269 _STLP_TEMPLATE_NULL
270 class _STLP_CLASS_DECLSPEC codecvt<wchar_t, char, mbstate_t>
271   : public locale::facet, public codecvt_base {
272 public:
273   typedef wchar_t    intern_type;
274   typedef char       extern_type;
275   typedef mbstate_t  state_type;
276 
facet(__refs)277   explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
278 
out(state_type & __state,const wchar_t * __from,const wchar_t * __from_end,const wchar_t * & __from_next,char * __to,char * __to_limit,char * & __to_next)279   result out(state_type&      __state,
280              const wchar_t*  __from,
281              const wchar_t*  __from_end,
282              const wchar_t*& __from_next,
283              char*           __to,
284              char*           __to_limit,
285              char*&          __to_next) const {
286     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
287     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
288     return do_out(__state,
289                   __from, __from_end, __from_next,
290                   __to,   __to_limit, __to_next);
291   }
292 
unshift(state_type & __state,char * __to,char * __to_limit,char * & __to_next)293   result unshift(state_type& __state,
294                  char*  __to, char*  __to_limit, char*& __to_next) const {
295     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
296     return do_unshift(__state, __to, __to_limit, __to_next);
297   }
298 
in(state_type & __state,const char * __from,const char * __from_end,const char * & __from_next,wchar_t * __to,wchar_t * __to_limit,wchar_t * & __to_next)299   result in(state_type&   __state,
300             const char*  __from,
301             const char*  __from_end,
302             const char*& __from_next,
303             wchar_t*     __to,
304             wchar_t*     __to_limit,
305             wchar_t*&    __to_next) const {
306     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
307     _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
308     return do_in(__state,
309                  __from, __from_end, __from_next,
310                  __to,   __to_limit, __to_next);
311   }
312 
encoding()313   int encoding() const _STLP_NOTHROW { return do_encoding(); }
314 
always_noconv()315   bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
316 
length(state_type & __state,const char * __from,const char * __from_end,size_t __max)317   int length(state_type& __state,
318              const char* __from, const char* __from_end,
319              size_t __max) const {
320     _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
321     return do_length(__state, __from, __from_end, __max);
322   }
323 
max_length()324   int max_length() const _STLP_NOTHROW { return do_max_length(); }
325 
326   static _STLP_STATIC_DECLSPEC locale::id id;
327 
328 protected:
329   ~codecvt();
330 
331   virtual result do_out(state_type&         __state,
332                         const wchar_t*  __from,
333                         const wchar_t*  __from_end,
334                         const wchar_t*& __from_next,
335                         char*        __to,
336                         char*        __to_limit,
337                         char*&       __to_next) const;
338 
339   virtual result do_in (state_type&         __state,
340                         const char*  __from,
341                         const char*  __from_end,
342                         const char*& __from_next,
343                         wchar_t*        __to,
344                         wchar_t*        __to_limit,
345                         wchar_t*&       __to_next) const;
346 
347   virtual result do_unshift(state_type&   __state,
348                             char*  __to,
349                             char*  __to_limit,
350                             char*& __to_next) const;
351 
352   virtual int do_encoding() const _STLP_NOTHROW;
353 
354   virtual bool do_always_noconv() const _STLP_NOTHROW;
355 
356   virtual int do_length(state_type&  __state,
357                         const  char* __from,
358                         const  char* __end,
359                         size_t __max) const;
360 
361   virtual int do_max_length() const _STLP_NOTHROW;
362 
363 private:
364   codecvt(const codecvt<wchar_t, char, mbstate_t>&);
365   codecvt<wchar_t, char, mbstate_t>& operator = (const codecvt<wchar_t, char, mbstate_t>&);
366 };
367 
368 # endif
369 
370 _STLP_TEMPLATE_NULL
371 class _STLP_CLASS_DECLSPEC codecvt_byname<char, char, mbstate_t>
372   : public codecvt<char, char, mbstate_t> {
373 public:
374   explicit codecvt_byname(const char* __name, size_t __refs = 0);
375   ~codecvt_byname();
376 private:
377   codecvt_byname(const codecvt_byname<char, char, mbstate_t>&);
378   codecvt_byname<char, char, mbstate_t>& operator =(const codecvt_byname<char, char, mbstate_t>&);
379 };
380 
381 # ifndef _STLP_NO_WCHAR_T
382 _STLP_TEMPLATE_NULL
383 class _STLP_CLASS_DECLSPEC codecvt_byname<wchar_t, char, mbstate_t>
384   : public codecvt<wchar_t, char, mbstate_t> {
385   friend class _Locale_impl;
386 public:
387   explicit codecvt_byname(const char * __name, size_t __refs = 0);
388 
389 protected:
390   ~codecvt_byname();
391 
392   virtual result do_out(state_type&         __state,
393                         const wchar_t*  __from,
394                         const wchar_t*  __from_end,
395                         const wchar_t*& __from_next,
396                         char*        __to,
397                         char*        __to_limit,
398                         char*&       __to_next) const;
399 
400   virtual result do_in (state_type&         __state,
401                         const char*  __from,
402                         const char*  __from_end,
403                         const char*& __from_next,
404                         wchar_t*        __to,
405                         wchar_t*        __to_limit,
406                         wchar_t*&       __to_next) const;
407 
408   virtual result do_unshift(state_type&   __state,
409                             char*  __to,
410                             char*  __to_limit,
411                             char*& __to_next) const;
412 
413   virtual int do_encoding() const _STLP_NOTHROW;
414 
415   virtual bool do_always_noconv() const _STLP_NOTHROW;
416 
417   virtual int do_length(state_type&  __state,
418                         const  char* __from,
419                         const  char* __end,
420                         size_t __max) const;
421 
422   virtual int do_max_length() const _STLP_NOTHROW;
423 
424 private:
codecvt_byname(_Locale_codecvt * __cvt)425   codecvt_byname(_Locale_codecvt* __cvt)
426     : _M_codecvt(__cvt) {}
427 
428   codecvt_byname(const codecvt_byname<wchar_t, char, mbstate_t>&);
429   codecvt_byname<wchar_t, char, mbstate_t>& operator =(const codecvt_byname<wchar_t, char, mbstate_t>&);
430   _Locale_codecvt* _M_codecvt;
431 };
432 
433 # endif
434 
435 _STLP_END_NAMESPACE
436 
437 #endif /* _STLP_INTERNAL_CODECVT_H */
438 
439 // Local Variables:
440 // mode:C++
441 // End:
442 
443