OSDN Git Service

2003-11-20 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     state_type __tmp_state(__state);
52
53 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
54     __c_locale __old = __uselocale(_M_c_locale_codecvt);
55 #endif
56
57     // wcsnrtombs is *very* fast but stops if encounters NUL characters:
58     // in case we fall back to wcrtomb and then continue, in a loop.
59     // NB: wcsnrtombs is a GNU extension
60     __from_next = __from;
61     __to_next = __to;
62     while (__from_next < __from_end && __to_next < __to_end
63            && __ret == ok)
64       {
65         const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
66                                                       __from_end - __from_next);
67         if (!__from_chunk_end)
68           __from_chunk_end = __from_end;
69
70         const intern_type* __tmp_from = __from_next;
71         const size_t __conv = wcsnrtombs(__to_next, &__from_next,
72                                          __from_chunk_end - __from_next,
73                                          __to_end - __to_next, &__state);
74         if (__conv == static_cast<size_t>(-1))
75           {
76             // In case of error, in order to stop at the exact place we
77             // have to start again from the beginning with a series of
78             // wcrtomb.
79             while (__tmp_from < __from_next)
80               __to_next += wcrtomb(__to_next, *__tmp_from++, &__tmp_state);
81             __state = __tmp_state;          
82             __ret = error;
83           }
84         else if (__from_next && __from_next < __from_chunk_end)
85           {
86             __to_next += __conv;
87             __ret = partial;
88           }
89         else
90           {
91             __from_next = __from_chunk_end;
92             __to_next += __conv;
93           }
94
95         if (__from_next < __from_end && __ret == ok)
96           {
97             extern_type __buf[MB_LEN_MAX];
98             __tmp_state = __state;
99             const size_t __conv = wcrtomb(__buf, *__from_next, &__tmp_state);
100             if (__conv == static_cast<size_t>(-1))
101               __ret = error;
102             else if (__conv > static_cast<size_t>(__to_end - __to_next))
103               __ret = partial;
104             else
105               {
106                 memcpy(__to_next, __buf, __conv);
107                 __state = __tmp_state;
108                 __to_next += __conv;
109                 ++__from_next;
110               }
111           }
112       }
113
114 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
115     __uselocale(__old);
116 #endif
117
118     return __ret; 
119   }
120   
121   codecvt_base::result
122   codecvt<wchar_t, char, mbstate_t>::
123   do_in(state_type& __state, const extern_type* __from, 
124         const extern_type* __from_end, const extern_type*& __from_next,
125         intern_type* __to, intern_type* __to_end,
126         intern_type*& __to_next) const
127   {
128     result __ret = ok;
129     // This temporary state object is neccessary so __state won't be modified
130     // if [__from, __from_end) is a partial multibyte character.
131     state_type __tmp_state(__state);
132 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
133     __c_locale __old = __uselocale(_M_c_locale_codecvt);
134 #endif
135
136     // Conversion must be done by calling mbrtowc in a loop rather than
137     // by calling mbsrtowcs because mbsrtowcs assumes that the input
138     // sequence is zero-terminated.
139     while (__from < __from_end && __to < __to_end)
140       {
141         size_t __conv = mbrtowc(__to, __from, __from_end - __from,
142                                 &__tmp_state);
143         if (__conv == static_cast<size_t>(-1))
144           {
145             __ret = error;
146             break;
147           }
148         else if (__conv == static_cast<size_t>(-2))
149           {
150             // It is unclear what to return in this case (see DR 382).
151             __ret = partial;
152             break;
153           }
154         else if (__conv == 0)
155           {
156             // XXX Probably wrong for stateful encodings
157             __conv = 1;
158             *__to = L'\0';
159           }
160
161         __state = __tmp_state;
162         __to++;
163         __from += __conv;
164       }
165
166 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
167     __uselocale(__old);
168 #endif
169
170     // It is not clear that __from < __from_end implies __ret != ok
171     // (see DR 382).
172     if (__ret == ok && __from < __from_end)
173       __ret = partial;
174
175     __from_next = __from;
176     __to_next = __to;
177     return __ret; 
178   }
179
180   int 
181   codecvt<wchar_t, char, mbstate_t>::
182   do_encoding() const throw()
183   {
184     // XXX This implementation assumes that the encoding is
185     // stateless and is either single-byte or variable-width.
186     int __ret = 0;
187 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
188     __c_locale __old = __uselocale(_M_c_locale_codecvt);
189 #endif
190     if (MB_CUR_MAX == 1)
191       __ret = 1;
192 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
193     __uselocale(__old);
194 #endif
195     return __ret;
196   }  
197
198   int 
199   codecvt<wchar_t, char, mbstate_t>::
200   do_max_length() const throw()
201   {
202 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
203     __c_locale __old = __uselocale(_M_c_locale_codecvt);
204 #endif
205     // XXX Probably wrong for stateful encodings.
206     int __ret = MB_CUR_MAX;
207 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
208     __uselocale(__old);
209 #endif
210     return __ret;
211   }
212   
213   int 
214   codecvt<wchar_t, char, mbstate_t>::
215   do_length(state_type& __state, const extern_type* __from,
216             const extern_type* __end, size_t __max) const
217   {
218     int __ret = 0;
219     state_type __tmp_state(__state);
220 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
221     __c_locale __old = __uselocale(_M_c_locale_codecvt);
222 #endif
223
224     while (__from < __end && __max)
225       {
226         size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
227         if (__conv == static_cast<size_t>(-1))
228           {
229             // Invalid source character
230             break;
231           }
232         else if (__conv == static_cast<size_t>(-2))
233           {
234             // Remainder of input does not form a complete destination
235             // character.
236             break;
237           }
238         else if (__conv == 0)
239           {
240             // XXX Probably wrong for stateful encodings
241             __conv = 1;
242           }
243
244         __state = __tmp_state;
245         __from += __conv;
246         __ret += __conv;
247         __max--;
248       }
249
250 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
251     __uselocale(__old);
252 #endif
253     return __ret; 
254   }
255 #endif
256 }