OSDN Git Service

2003-11-19 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / gnu / codecvt_members.cc
1 // std::codecvt implementation details, GNU version -*- C++ -*-
2
3 // Copyright (C) 2002 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 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37 #include <bits/c++locale_internal.h>
38
39 namespace std
40 {
41   // Specializations.
42 #ifdef _GLIBCXX_USE_WCHAR_T
43   codecvt_base::result
44   codecvt<wchar_t, char, mbstate_t>::
45   do_out(state_type& __state, const intern_type* __from, 
46          const intern_type* __from_end, const intern_type*& __from_next,
47          extern_type* __to, extern_type* __to_end,
48          extern_type*& __to_next) const
49   {
50     result __ret = ok;
51     // A temporary state must be used since the result of the last
52     // conversion may be thrown away.
53     state_type __tmp_state(__state);   
54
55 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
56     __c_locale __old = __uselocale(_M_c_locale_codecvt);
57 #endif
58
59     // The conversion must be done by calling wcrtomb in a loop rather
60     // than using wcsrtombs because wcsrtombs assumes that the input is
61     // zero-terminated.
62
63     // Either we can upper bound the total number of external characters to
64     // something smaller than __to_end - __to or the conversion must be done
65     // using a temporary destination buffer since it is not possible to
66     // pass the size of the buffer to wcrtomb
67     if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
68       while (__from < __from_end)
69         {
70           const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
71           if (__conv == static_cast<size_t>(-1))
72             {
73               __ret = error;
74               break;
75             }
76           __state = __tmp_state;
77           __to += __conv;
78           __from++;
79         }
80     else
81       {
82         extern_type __buf[MB_LEN_MAX];
83         while (__from < __from_end && __to < __to_end)
84           {
85             const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
86             if (__conv == static_cast<size_t>(-1))
87               {
88                 __ret = error;
89                 break;
90               }
91             else if (__conv > static_cast<size_t>(__to_end - __to))
92               {
93                 __ret = partial;
94                 break;
95               }
96             
97             memcpy(__to, __buf, __conv);
98             __state = __tmp_state;
99             __to += __conv;
100             __from++;
101           }
102       }
103
104 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
105     __uselocale(__old);
106 #endif
107
108     if (__ret == ok && __from < __from_end)
109       __ret = partial;
110
111     __from_next = __from;
112     __to_next = __to;
113     return __ret; 
114   }
115   
116   codecvt_base::result
117   codecvt<wchar_t, char, mbstate_t>::
118   do_in(state_type& __state, const extern_type* __from, 
119         const extern_type* __from_end, const extern_type*& __from_next,
120         intern_type* __to, intern_type* __to_end,
121         intern_type*& __to_next) const
122   {
123     result __ret = ok;
124     // This temporary state object is neccessary so __state won't be modified
125     // if [__from, __from_end) is a partial multibyte character.
126     state_type __tmp_state(__state);
127 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
128     __c_locale __old = __uselocale(_M_c_locale_codecvt);
129 #endif
130
131     // Conversion must be done by calling mbrtowc in a loop rather than
132     // by calling mbsrtowcs because mbsrtowcs assumes that the input
133     // sequence is zero-terminated.
134     while (__from < __from_end && __to < __to_end)
135       {
136         size_t __conv = mbrtowc(__to, __from, __from_end - __from,
137                                 &__tmp_state);
138         if (__conv == static_cast<size_t>(-1))
139           {
140             __ret = error;
141             break;
142           }
143         else if (__conv == static_cast<size_t>(-2))
144           {
145             // It is unclear what to return in this case (see DR 382).
146             __ret = partial;
147             break;
148           }
149         else if (__conv == 0)
150           {
151             // XXX Probably wrong for stateful encodings
152             __conv = 1;
153             *__to = L'\0';
154           }
155
156         __state = __tmp_state;
157         __to++;
158         __from += __conv;
159       }
160
161 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
162     __uselocale(__old);
163 #endif
164
165     // It is not clear that __from < __from_end implies __ret != ok
166     // (see DR 382).
167     if (__ret == ok && __from < __from_end)
168       __ret = partial;
169
170     __from_next = __from;
171     __to_next = __to;
172     return __ret; 
173   }
174
175   int 
176   codecvt<wchar_t, char, mbstate_t>::
177   do_encoding() const throw()
178   {
179     // XXX This implementation assumes that the encoding is
180     // stateless and is either single-byte or variable-width.
181     int __ret = 0;
182 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
183     __c_locale __old = __uselocale(_M_c_locale_codecvt);
184 #endif
185     if (MB_CUR_MAX == 1)
186       __ret = 1;
187 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
188     __uselocale(__old);
189 #endif
190     return __ret;
191   }  
192
193   int 
194   codecvt<wchar_t, char, mbstate_t>::
195   do_max_length() const throw()
196   {
197 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
198     __c_locale __old = __uselocale(_M_c_locale_codecvt);
199 #endif
200     // XXX Probably wrong for stateful encodings.
201     int __ret = MB_CUR_MAX;
202 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
203     __uselocale(__old);
204 #endif
205     return __ret;
206   }
207   
208   int 
209   codecvt<wchar_t, char, mbstate_t>::
210   do_length(state_type& __state, const extern_type* __from,
211             const extern_type* __end, size_t __max) const
212   {
213     int __ret = 0;
214     state_type __tmp_state(__state);
215 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
216     __c_locale __old = __uselocale(_M_c_locale_codecvt);
217 #endif
218
219     while (__from < __end && __max)
220       {
221         size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
222         if (__conv == static_cast<size_t>(-1))
223           {
224             // Invalid source character
225             break;
226           }
227         else if (__conv == static_cast<size_t>(-2))
228           {
229             // Remainder of input does not form a complete destination
230             // character.
231             break;
232           }
233         else if (__conv == 0)
234           {
235             // XXX Probably wrong for stateful encodings
236             __conv = 1;
237           }
238
239         __state = __tmp_state;
240         __from += __conv;
241         __ret += __conv;
242         __max--;
243       }
244
245 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
246     __uselocale(__old);
247 #endif
248     return __ret; 
249   }
250 #endif
251 }