1 /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
35 # include <langinfo.h>
36 # include <locale/localeinfo.h>
37 # include <wcsmbs/wcsmbsload.h>
41 /* Prototypes of libio's codecvt functions. */
42 static enum __codecvt_result
do_out (struct _IO_codecvt
*codecvt
,
44 const wchar_t *from_start
,
45 const wchar_t *from_end
,
46 const wchar_t **from_stop
, char *to_start
,
47 char *to_end
, char **to_stop
);
48 static enum __codecvt_result
do_unshift (struct _IO_codecvt
*codecvt
,
49 __mbstate_t
*statep
, char *to_start
,
50 char *to_end
, char **to_stop
);
51 static enum __codecvt_result
do_in (struct _IO_codecvt
*codecvt
,
53 const char *from_start
,
55 const char **from_stop
, wchar_t *to_start
,
56 wchar_t *to_end
, wchar_t **to_stop
);
57 static int do_encoding (struct _IO_codecvt
*codecvt
);
58 static int do_length (struct _IO_codecvt
*codecvt
, __mbstate_t
*statep
,
59 const char *from_start
,
60 const char *from_end
, _IO_size_t max
);
61 static int do_max_length (struct _IO_codecvt
*codecvt
);
62 static int do_always_noconv (struct _IO_codecvt
*codecvt
);
65 /* The functions used in `codecvt' for libio are always the same. */
66 struct _IO_codecvt __libio_codecvt
=
68 .__codecvt_destr
= NULL
, /* Destructor, never used. */
69 .__codecvt_do_out
= do_out
,
70 .__codecvt_do_unshift
= do_unshift
,
71 .__codecvt_do_in
= do_in
,
72 .__codecvt_do_encoding
= do_encoding
,
73 .__codecvt_do_always_noconv
= do_always_noconv
,
74 .__codecvt_do_length
= do_length
,
75 .__codecvt_do_max_length
= do_max_length
79 /* Return orientation of stream. If mode is nonzero try to change
80 the orientation first. */
87 /* Normalize the value. */
88 mode
= mode
< 0 ? -1 : (mode
== 0 ? 0 : 1);
90 if (mode
== 0 || fp
->_mode
!= 0)
91 /* The caller simply wants to know about the current orientation
92 or the orientation already has been determined. */
95 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile
, fp
);
98 /* Set the orientation appropriately. */
101 fp
->_wide_data
->_IO_read_ptr
= fp
->_wide_data
->_IO_read_end
;
102 fp
->_wide_data
->_IO_write_ptr
= fp
->_wide_data
->_IO_write_base
;
104 /* Clear the state. We start all over again. */
105 memset (&fp
->_wide_data
->_IO_state
, '\0', sizeof (__mbstate_t
));
106 memset (&fp
->_wide_data
->_IO_last_state
, '\0', sizeof (__mbstate_t
));
108 /* Get the character conversion functions based on the currently
109 selected locale for LC_CTYPE. */
112 struct gconv_fcts fcts
;
113 struct _IO_codecvt
*cc
= &fp
->_wide_data
->_codecvt
;
115 __wcsmbs_clone_conv (&fcts
);
117 /* The functions are always the same. */
118 *cc
= __libio_codecvt
;
120 cc
->__cd_in
.__cd
.__nsteps
= 1; /* Only one step allowed. */
121 cc
->__cd_in
.__cd
.__steps
= fcts
.towc
;
123 cc
->__cd_in
.__cd
.__data
[0].__invocation_counter
= 0;
124 cc
->__cd_in
.__cd
.__data
[0].__internal_use
= 1;
125 cc
->__cd_in
.__cd
.__data
[0].__flags
= __GCONV_IS_LAST
;
126 cc
->__cd_in
.__cd
.__data
[0].__statep
= &fp
->_wide_data
->_IO_state
;
127 /* XXX For now no transliteration. */
128 memset (&cc
->__cd_in
.__cd
.__data
[0].__trans
, '\0',
129 sizeof (struct __gconv_trans_data
));
131 cc
->__cd_out
.__cd
.__nsteps
= 1; /* Only one step allowed. */
132 cc
->__cd_out
.__cd
.__steps
= fcts
.tomb
;
134 cc
->__cd_out
.__cd
.__data
[0].__invocation_counter
= 0;
135 cc
->__cd_out
.__cd
.__data
[0].__internal_use
= 1;
136 cc
->__cd_out
.__cd
.__data
[0].__flags
= __GCONV_IS_LAST
;
137 cc
->__cd_out
.__cd
.__data
[0].__statep
= &fp
->_wide_data
->_IO_state
;
138 /* XXX For now no transliteration. */
139 memset (&cc
->__cd_out
.__cd
.__data
[0].__trans
, '\0',
140 sizeof (struct __gconv_trans_data
));
143 # error "somehow determine this from LC_CTYPE"
146 /* From now on use the wide character callback functions. */
147 ((struct _IO_FILE_plus
*) fp
)->vtable
= fp
->_wide_data
->_wide_vtable
;
150 /* Set the mode now. */
153 _IO_funlockfile (fp
);
154 _IO_cleanup_region_end (0);
160 weak_alias (_IO_fwide
, fwide
)
164 static enum __codecvt_result
165 do_out (struct _IO_codecvt
*codecvt
, __mbstate_t
*statep
,
166 const wchar_t *from_start
, const wchar_t *from_end
,
167 const wchar_t **from_stop
, char *to_start
, char *to_end
,
170 enum __codecvt_result result
;
173 struct __gconv_step
*gs
= codecvt
->__cd_out
.__cd
.__steps
;
176 const unsigned char *from_start_copy
= (unsigned char *) from_start
;
178 codecvt
->__cd_out
.__cd
.__data
[0].__outbuf
= to_start
;
179 codecvt
->__cd_out
.__cd
.__data
[0].__outbufend
= to_end
;
180 codecvt
->__cd_out
.__cd
.__data
[0].__statep
= statep
;
182 status
= DL_CALL_FCT (gs
->__fct
,
183 (gs
, codecvt
->__cd_out
.__cd
.__data
, &from_start_copy
,
184 (const unsigned char *) from_end
, to_start
,
187 *from_stop
= (wchar_t *) from_start_copy
;
188 *to_stop
= codecvt
->__cd_out
.__cd
.__data
[0].__outbuf
;
193 case __GCONV_EMPTY_INPUT
:
194 result
= __codecvt_ok
;
197 case __GCONV_FULL_OUTPUT
:
198 case __GCONV_INCOMPLETE_INPUT
:
199 result
= __codecvt_partial
;
203 result
= __codecvt_error
;
207 /* Decide what to do. */
208 result
= __codecvt_error
;
215 static enum __codecvt_result
216 do_unshift (struct _IO_codecvt
*codecvt
, __mbstate_t
*statep
,
217 char *to_start
, char *to_end
, char **to_stop
)
219 enum __codecvt_result result
;
222 struct __gconv_step
*gs
= codecvt
->__cd_out
.__cd
.__steps
;
226 codecvt
->__cd_out
.__cd
.__data
[0].__outbuf
= to_start
;
227 codecvt
->__cd_out
.__cd
.__data
[0].__outbufend
= to_end
;
228 codecvt
->__cd_out
.__cd
.__data
[0].__statep
= statep
;
230 status
= DL_CALL_FCT (gs
->__fct
,
231 (gs
, codecvt
->__cd_out
.__cd
.__data
, NULL
, NULL
,
232 to_start
, &dummy
, 1, 0));
234 *to_stop
= codecvt
->__cd_out
.__cd
.__data
[0].__outbuf
;
239 case __GCONV_EMPTY_INPUT
:
240 result
= __codecvt_ok
;
243 case __GCONV_FULL_OUTPUT
:
244 case __GCONV_INCOMPLETE_INPUT
:
245 result
= __codecvt_partial
;
249 result
= __codecvt_error
;
253 /* Decide what to do. */
254 result
= __codecvt_error
;
261 static enum __codecvt_result
262 do_in (struct _IO_codecvt
*codecvt
, __mbstate_t
*statep
,
263 const char *from_start
, const char *from_end
, const char **from_stop
,
264 wchar_t *to_start
, wchar_t *to_end
, wchar_t **to_stop
)
266 enum __codecvt_result result
;
269 struct __gconv_step
*gs
= codecvt
->__cd_in
.__cd
.__steps
;
272 const unsigned char *from_start_copy
= (unsigned char *) from_start
;
274 codecvt
->__cd_in
.__cd
.__data
[0].__outbuf
= (char *) to_start
;
275 codecvt
->__cd_in
.__cd
.__data
[0].__outbufend
= (char *) to_end
;
276 codecvt
->__cd_in
.__cd
.__data
[0].__statep
= statep
;
278 status
= DL_CALL_FCT (gs
->__fct
,
279 (gs
, codecvt
->__cd_in
.__cd
.__data
, &from_start_copy
,
280 from_end
, (char *) to_start
, &dummy
, 0, 0));
282 *from_stop
= from_start_copy
;
283 *to_stop
= (wchar_t *) codecvt
->__cd_in
.__cd
.__data
[0].__outbuf
;
288 case __GCONV_EMPTY_INPUT
:
289 result
= __codecvt_ok
;
292 case __GCONV_FULL_OUTPUT
:
293 case __GCONV_INCOMPLETE_INPUT
:
294 result
= __codecvt_partial
;
298 result
= __codecvt_error
;
302 /* Decide what to do. */
303 result
= __codecvt_error
;
311 do_encoding (struct _IO_codecvt
*codecvt
)
314 /* See whether the encoding is stateful. */
315 if (codecvt
->__cd_in
.__cd
.__steps
[0].__stateful
)
317 /* Fortunately not. Now determine the input bytes for the conversion
318 necessary for each wide character. */
319 if (codecvt
->__cd_in
.__cd
.__steps
[0].__min_needed_from
320 != codecvt
->__cd_in
.__cd
.__steps
[0].__max_needed_from
)
321 /* Not a constant value. */
324 return codecvt
->__cd_in
.__cd
.__steps
[0].__min_needed_from
;
326 /* Worst case scenario. */
333 do_always_noconv (struct _IO_codecvt
*codecvt
)
340 do_length (struct _IO_codecvt
*codecvt
, __mbstate_t
*statep
,
341 const char *from_start
, const char *from_end
, _IO_size_t max
)
345 const unsigned char *cp
= (const unsigned char *) from_start
;
347 struct __gconv_step
*gs
= codecvt
->__cd_in
.__cd
.__steps
;
351 codecvt
->__cd_in
.__cd
.__data
[0].__outbuf
= (char *) to_buf
;
352 codecvt
->__cd_in
.__cd
.__data
[0].__outbufend
= (char *) &to_buf
[max
];
353 codecvt
->__cd_in
.__cd
.__data
[0].__statep
= statep
;
355 status
= DL_CALL_FCT (gs
->__fct
,
356 (gs
, codecvt
->__cd_in
.__cd
.__data
, &cp
, from_end
,
357 (char *) to_buf
, &dummy
, 0, 0));
359 result
= cp
- (const unsigned char *) from_start
;
361 /* Decide what to do. */
370 do_max_length (struct _IO_codecvt
*codecvt
)
373 return codecvt
->__cd_in
.__cd
.__steps
[0].__max_needed_from
;