OSDN Git Service

2002-02-07 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / codecvt.cc
1 // Copyright (C) 2000, 2002 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING.  If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
18
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction.  Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License.  This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
27
28 // Written by Benjamin Kosnik <bkoz@cygnus.com>
29
30 #include <locale>
31
32 namespace std 
33 {
34 #ifdef _GLIBCPP_USE___ENC_TRAITS
35   // Definitions for static const data members of __enc_traits.
36   const int __enc_traits::_S_max_size;
37 #endif 
38
39   codecvt<char, char, mbstate_t>::
40   codecvt(size_t __refs)
41   : __codecvt_abstract_base<char, char, mbstate_t>(__refs)
42   { }
43
44   codecvt<char, char, mbstate_t>::
45   ~codecvt() { }
46   
47   codecvt_base::result
48   codecvt<char, char, mbstate_t>::
49   do_out(state_type&, const intern_type* __from, 
50          const intern_type* __from_end, const intern_type*& __from_next,
51          extern_type* __to, extern_type* __to_end, 
52          extern_type*& __to_next) const
53   { 
54     size_t __len = min(__from_end - __from, __to_end - __to);
55     memcpy(__to, __from, __len);
56     __from_next = __from; 
57     __to_next = __to;
58     return noconv;  
59   }
60   
61   codecvt_base::result
62   codecvt<char, char, mbstate_t>::
63   do_unshift(state_type&, extern_type* __to,
64              extern_type*, extern_type*& __to_next) const
65   { 
66     __to_next = __to; 
67     return noconv; 
68   }
69   
70   codecvt_base::result
71   codecvt<char, char, mbstate_t>::
72   do_in(state_type&, const extern_type* __from, 
73         const extern_type* __from_end, const extern_type*& __from_next,
74         intern_type* __to, intern_type* __to_end, 
75         intern_type*& __to_next) const
76   { 
77     size_t __len = min(__from_end - __from, __to_end - __to);
78     memcpy(__to, __from, __len);
79     __from_next = __from; 
80     __to_next = __to;
81     return noconv;  
82   }
83
84   int 
85   codecvt<char, char, mbstate_t>::
86   do_encoding() const throw() 
87   { return 1; }
88   
89   bool 
90   codecvt<char, char, mbstate_t>::
91   do_always_noconv() const throw() 
92   { return true; }
93   
94   int 
95   codecvt<char, char, mbstate_t>::
96   do_length (const state_type&, const extern_type* __from,
97              const extern_type* __end, size_t __max) const
98   { return min(__max, static_cast<size_t>(__end - __from)); }
99   
100   int 
101   codecvt<char, char, mbstate_t>::
102   do_max_length() const throw() 
103   { return 1; }
104   
105 #ifdef _GLIBCPP_USE_WCHAR_T
106   // codecvt<wchar_t, char, mbstate_t> required specialization
107   codecvt<wchar_t, char, mbstate_t>::
108   codecvt(size_t __refs)
109   : __codecvt_abstract_base<wchar_t, char, mbstate_t>(__refs) { }
110
111   codecvt<wchar_t, char, mbstate_t>::
112   ~codecvt() { }
113   
114   codecvt_base::result
115   codecvt<wchar_t, char, mbstate_t>::
116   do_out(state_type& __state, const intern_type* __from, 
117          const intern_type* __from_end, const intern_type*& __from_next,
118          extern_type* __to, extern_type* __to_end,
119          extern_type*& __to_next) const
120   {
121     result __ret = error;
122     size_t __len = min(__from_end - __from, __to_end - __to);
123     size_t __conv = wcsrtombs(__to, &__from, __len, &__state);
124
125     if (__conv == __len)
126       {
127         __from_next = __from;
128         __to_next = __to + __conv;
129         __ret = ok;
130       }
131     else if (__conv > 0 && __conv < __len)
132       {
133         __from_next = __from;
134         __to_next = __to + __conv;
135         __ret = partial;
136       }
137     else
138       __ret = error;
139         
140     return __ret; 
141   }
142   
143   codecvt_base::result
144   codecvt<wchar_t, char, mbstate_t>::
145   do_unshift(state_type&, extern_type* __to,
146              extern_type*, extern_type*& __to_next) const
147   {
148     __to_next = __to;
149     return noconv;
150   }
151   
152   codecvt_base::result
153   codecvt<wchar_t, char, mbstate_t>::
154   do_in(state_type& __state, const extern_type* __from, 
155         const extern_type* __from_end, const extern_type*& __from_next,
156         intern_type* __to, intern_type* __to_end,
157         intern_type*& __to_next) const
158   {
159     result __ret = error;
160     size_t __len = min(__from_end - __from, __to_end - __to);
161     size_t __conv = mbsrtowcs(__to, &__from, __len, &__state);
162
163     if (__conv == __len)
164       {
165         __from_next = __from;
166         __to_next = __to + __conv;
167         __ret = ok;
168       }
169     else if (__conv > 0 && __conv < __len)
170       {
171         __from_next = __from;
172         __to_next = __to + __conv;
173         __ret = partial;
174       }
175     else
176       __ret = error;
177         
178     return __ret; 
179   }
180   
181   int 
182   codecvt<wchar_t, char, mbstate_t>::
183   do_encoding() const throw()
184   { return sizeof(wchar_t); }
185   
186   bool 
187   codecvt<wchar_t, char, mbstate_t>::
188   do_always_noconv() const throw()
189   { return false; }
190   
191   int 
192   codecvt<wchar_t, char, mbstate_t>::
193   do_length(const state_type&, const extern_type* __from,
194             const extern_type* __end, size_t __max) const
195   { return min(__max, static_cast<size_t>(__end - __from)); }
196
197   int 
198   codecvt<wchar_t, char, mbstate_t>::
199   do_max_length() const throw()
200   { return 1; }
201 #endif //  _GLIBCPP_USE_WCHAR_T
202 } // namespace std