1*404b540aSrobert // std::codecvt implementation details, generic version -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2002, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert //
31*404b540aSrobert // ISO C++ 14882: 22.2.1.5 - Template class codecvt
32*404b540aSrobert //
33*404b540aSrobert 
34*404b540aSrobert // Written by Benjamin Kosnik <bkoz@redhat.com>
35*404b540aSrobert 
36*404b540aSrobert #include <locale>
37*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)38*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
39*404b540aSrobert 
40*404b540aSrobert   // Specializations.
41*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
42*404b540aSrobert   codecvt_base::result
43*404b540aSrobert   codecvt<wchar_t, char, mbstate_t>::
44*404b540aSrobert   do_out(state_type& __state, const intern_type* __from,
45*404b540aSrobert 	 const intern_type* __from_end, const intern_type*& __from_next,
46*404b540aSrobert 	 extern_type* __to, extern_type* __to_end,
47*404b540aSrobert 	 extern_type*& __to_next) const
48*404b540aSrobert   {
49*404b540aSrobert     result __ret = ok;
50*404b540aSrobert     // The conversion must be done using a temporary destination buffer
51*404b540aSrobert     // since it is not possible to pass the size of the buffer to wcrtomb
52*404b540aSrobert     state_type __tmp_state(__state);
53*404b540aSrobert 
54*404b540aSrobert     // The conversion must be done by calling wcrtomb in a loop rather
55*404b540aSrobert     // than using wcsrtombs because wcsrtombs assumes that the input is
56*404b540aSrobert     // zero-terminated.
57*404b540aSrobert 
58*404b540aSrobert     // Either we can upper bound the total number of external characters to
59*404b540aSrobert     // something smaller than __to_end - __to or the conversion must be done
60*404b540aSrobert     // using a temporary destination buffer since it is not possible to
61*404b540aSrobert     // pass the size of the buffer to wcrtomb
62*404b540aSrobert     if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
63*404b540aSrobert       while (__from < __from_end)
64*404b540aSrobert 	{
65*404b540aSrobert 	  const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
66*404b540aSrobert 	  if (__conv == static_cast<size_t>(-1))
67*404b540aSrobert 	    {
68*404b540aSrobert 	      __ret = error;
69*404b540aSrobert 	      break;
70*404b540aSrobert 	    }
71*404b540aSrobert 	  __state = __tmp_state;
72*404b540aSrobert 	  __to += __conv;
73*404b540aSrobert 	  __from++;
74*404b540aSrobert 	}
75*404b540aSrobert     else
76*404b540aSrobert       {
77*404b540aSrobert 	extern_type __buf[MB_LEN_MAX];
78*404b540aSrobert 	while (__from < __from_end && __to < __to_end)
79*404b540aSrobert 	  {
80*404b540aSrobert 	    const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
81*404b540aSrobert 	    if (__conv == static_cast<size_t>(-1))
82*404b540aSrobert 	      {
83*404b540aSrobert 		__ret = error;
84*404b540aSrobert 		break;
85*404b540aSrobert 	      }
86*404b540aSrobert 	    else if (__conv > static_cast<size_t>(__to_end - __to))
87*404b540aSrobert 	      {
88*404b540aSrobert 		__ret = partial;
89*404b540aSrobert 		break;
90*404b540aSrobert 	      }
91*404b540aSrobert 
92*404b540aSrobert 	    memcpy(__to, __buf, __conv);
93*404b540aSrobert 	    __state = __tmp_state;
94*404b540aSrobert 	    __to += __conv;
95*404b540aSrobert 	    __from++;
96*404b540aSrobert 	  }
97*404b540aSrobert       }
98*404b540aSrobert 
99*404b540aSrobert     if (__ret == ok && __from < __from_end)
100*404b540aSrobert       __ret = partial;
101*404b540aSrobert 
102*404b540aSrobert     __from_next = __from;
103*404b540aSrobert     __to_next = __to;
104*404b540aSrobert     return __ret;
105*404b540aSrobert   }
106*404b540aSrobert 
107*404b540aSrobert   codecvt_base::result
108*404b540aSrobert   codecvt<wchar_t, char, mbstate_t>::
do_in(state_type & __state,const extern_type * __from,const extern_type * __from_end,const extern_type * & __from_next,intern_type * __to,intern_type * __to_end,intern_type * & __to_next) const109*404b540aSrobert   do_in(state_type& __state, const extern_type* __from,
110*404b540aSrobert 	const extern_type* __from_end, const extern_type*& __from_next,
111*404b540aSrobert 	intern_type* __to, intern_type* __to_end,
112*404b540aSrobert 	intern_type*& __to_next) const
113*404b540aSrobert   {
114*404b540aSrobert     result __ret = ok;
115*404b540aSrobert     // This temporary state object is neccessary so __state won't be modified
116*404b540aSrobert     // if [__from, __from_end) is a partial multibyte character.
117*404b540aSrobert     state_type __tmp_state(__state);
118*404b540aSrobert 
119*404b540aSrobert     // Conversion must be done by calling mbrtowc in a loop rather than
120*404b540aSrobert     // by calling mbsrtowcs because mbsrtowcs assumes that the input
121*404b540aSrobert     // sequence is zero-terminated.
122*404b540aSrobert     while (__from < __from_end && __to < __to_end)
123*404b540aSrobert       {
124*404b540aSrobert 	size_t __conv = mbrtowc(__to, __from, __from_end - __from,
125*404b540aSrobert 				&__tmp_state);
126*404b540aSrobert 	if (__conv == static_cast<size_t>(-1))
127*404b540aSrobert 	  {
128*404b540aSrobert 	    __ret = error;
129*404b540aSrobert 	    break;
130*404b540aSrobert 	  }
131*404b540aSrobert 	else if (__conv == static_cast<size_t>(-2))
132*404b540aSrobert 	  {
133*404b540aSrobert 	    // It is unclear what to return in this case (see DR 382).
134*404b540aSrobert 	    __ret = partial;
135*404b540aSrobert 	    break;
136*404b540aSrobert 	  }
137*404b540aSrobert 	else if (__conv == 0)
138*404b540aSrobert 	  {
139*404b540aSrobert 	    // XXX Probably wrong for stateful encodings
140*404b540aSrobert 	    __conv = 1;
141*404b540aSrobert 	    *__to = L'\0';
142*404b540aSrobert 	  }
143*404b540aSrobert 
144*404b540aSrobert 	__state = __tmp_state;
145*404b540aSrobert 	__to++;
146*404b540aSrobert 	__from += __conv;
147*404b540aSrobert       }
148*404b540aSrobert 
149*404b540aSrobert     // It is not clear that __from < __from_end implies __ret != ok
150*404b540aSrobert     // (see DR 382).
151*404b540aSrobert     if (__ret == ok && __from < __from_end)
152*404b540aSrobert       __ret = partial;
153*404b540aSrobert 
154*404b540aSrobert     __from_next = __from;
155*404b540aSrobert     __to_next = __to;
156*404b540aSrobert     return __ret;
157*404b540aSrobert   }
158*404b540aSrobert 
159*404b540aSrobert   int
160*404b540aSrobert   codecvt<wchar_t, char, mbstate_t>::
do_encoding() const161*404b540aSrobert   do_encoding() const throw()
162*404b540aSrobert   {
163*404b540aSrobert     // XXX This implementation assumes that the encoding is
164*404b540aSrobert     // stateless and is either single-byte or variable-width.
165*404b540aSrobert     int __ret = 0;
166*404b540aSrobert     if (MB_CUR_MAX == 1)
167*404b540aSrobert       __ret = 1;
168*404b540aSrobert     return __ret;
169*404b540aSrobert   }
170*404b540aSrobert 
171*404b540aSrobert   int
172*404b540aSrobert   codecvt<wchar_t, char, mbstate_t>::
do_max_length() const173*404b540aSrobert   do_max_length() const throw()
174*404b540aSrobert   {
175*404b540aSrobert     // XXX Probably wrong for stateful encodings.
176*404b540aSrobert     int __ret = MB_CUR_MAX;
177*404b540aSrobert     return __ret;
178*404b540aSrobert   }
179*404b540aSrobert 
180*404b540aSrobert   int
181*404b540aSrobert   codecvt<wchar_t, char, mbstate_t>::
do_length(state_type & __state,const extern_type * __from,const extern_type * __end,size_t __max) const182*404b540aSrobert   do_length(state_type& __state, const extern_type* __from,
183*404b540aSrobert 	    const extern_type* __end, size_t __max) const
184*404b540aSrobert   {
185*404b540aSrobert     int __ret = 0;
186*404b540aSrobert     state_type __tmp_state(__state);
187*404b540aSrobert 
188*404b540aSrobert     while (__from < __end && __max)
189*404b540aSrobert       {
190*404b540aSrobert 	size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
191*404b540aSrobert 	if (__conv == static_cast<size_t>(-1))
192*404b540aSrobert 	  {
193*404b540aSrobert 	    // Invalid source character
194*404b540aSrobert 	    break;
195*404b540aSrobert 	  }
196*404b540aSrobert 	else if (__conv == static_cast<size_t>(-2))
197*404b540aSrobert 	  {
198*404b540aSrobert 	    // Remainder of input does not form a complete destination
199*404b540aSrobert 	    // character.
200*404b540aSrobert 	    break;
201*404b540aSrobert 	  }
202*404b540aSrobert 	else if (__conv == 0)
203*404b540aSrobert 	  {
204*404b540aSrobert 	    // XXX Probably wrong for stateful encodings
205*404b540aSrobert 	    __conv = 1;
206*404b540aSrobert 	  }
207*404b540aSrobert 
208*404b540aSrobert 	__state = __tmp_state;
209*404b540aSrobert 	__from += __conv;
210*404b540aSrobert 	__ret += __conv;
211*404b540aSrobert 	__max--;
212*404b540aSrobert       }
213*404b540aSrobert 
214*404b540aSrobert     return __ret;
215*404b540aSrobert   }
216*404b540aSrobert #endif
217*404b540aSrobert 
218*404b540aSrobert _GLIBCXX_END_NAMESPACE
219