Update.
[glibc.git] / libio / iofwide.c
blobbb26dddc8587d0a66987190b9d6721bdb5d06955
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,
17 MA 02111-1307, USA.
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. */
26 #include <libioP.h>
27 #ifdef _LIBC
28 # include <dlfcn.h>
29 # include <wchar.h>
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
34 #ifdef _LIBC
35 # include <langinfo.h>
36 # include <locale/localeinfo.h>
37 # include <wcsmbs/wcsmbsload.h>
38 #endif
41 /* Prototypes of libio's codecvt functions. */
42 static enum __codecvt_result do_out (struct _IO_codecvt *codecvt,
43 __mbstate_t *statep,
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,
52 __mbstate_t *statep,
53 const char *from_start,
54 const char *from_end,
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. */
81 #undef _IO_fwide
82 int
83 _IO_fwide (fp, mode)
84 _IO_FILE *fp;
85 int mode;
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. */
93 return fp->_mode;
95 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
96 _IO_flockfile (fp);
98 /* Set the orientation appropriately. */
99 if (mode > 0)
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. */
110 #ifdef _LIBC
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));
142 #else
143 # error "somehow determine this from LC_CTYPE"
144 #endif
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. */
151 fp->_mode = mode;
153 _IO_funlockfile (fp);
154 _IO_cleanup_region_end (0);
156 return mode;
159 #ifdef weak_alias
160 weak_alias (_IO_fwide, fwide)
161 #endif
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,
168 char **to_stop)
170 enum __codecvt_result result;
172 #ifdef _LIBC
173 struct __gconv_step *gs = codecvt->__cd_out.__cd.__steps;
174 int status;
175 size_t dummy;
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,
185 &dummy, 0, 0));
187 *from_stop = (wchar_t *) from_start_copy;
188 *to_stop = codecvt->__cd_out.__cd.__data[0].__outbuf;
190 switch (status)
192 case __GCONV_OK:
193 case __GCONV_EMPTY_INPUT:
194 result = __codecvt_ok;
195 break;
197 case __GCONV_FULL_OUTPUT:
198 case __GCONV_INCOMPLETE_INPUT:
199 result = __codecvt_partial;
200 break;
202 default:
203 result = __codecvt_error;
204 break;
206 #else
207 /* Decide what to do. */
208 result = __codecvt_error;
209 #endif
211 return result;
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;
221 #ifdef _LIBC
222 struct __gconv_step *gs = codecvt->__cd_out.__cd.__steps;
223 int status;
224 size_t dummy;
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;
236 switch (status)
238 case __GCONV_OK:
239 case __GCONV_EMPTY_INPUT:
240 result = __codecvt_ok;
241 break;
243 case __GCONV_FULL_OUTPUT:
244 case __GCONV_INCOMPLETE_INPUT:
245 result = __codecvt_partial;
246 break;
248 default:
249 result = __codecvt_error;
250 break;
252 #else
253 /* Decide what to do. */
254 result = __codecvt_error;
255 #endif
257 return result;
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;
268 #ifdef _LIBC
269 struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
270 int status;
271 size_t dummy;
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;
285 switch (status)
287 case __GCONV_OK:
288 case __GCONV_EMPTY_INPUT:
289 result = __codecvt_ok;
290 break;
292 case __GCONV_FULL_OUTPUT:
293 case __GCONV_INCOMPLETE_INPUT:
294 result = __codecvt_partial;
295 break;
297 default:
298 result = __codecvt_error;
299 break;
301 #else
302 /* Decide what to do. */
303 result = __codecvt_error;
304 #endif
306 return result;
310 static int
311 do_encoding (struct _IO_codecvt *codecvt)
313 #ifdef _LIBC
314 /* See whether the encoding is stateful. */
315 if (codecvt->__cd_in.__cd.__steps[0].__stateful)
316 return -1;
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. */
322 return 0;
324 return codecvt->__cd_in.__cd.__steps[0].__min_needed_from;
325 #else
326 /* Worst case scenario. */
327 return -1;
328 #endif
332 static int
333 do_always_noconv (struct _IO_codecvt *codecvt)
335 return 0;
339 static int
340 do_length (struct _IO_codecvt *codecvt, __mbstate_t *statep,
341 const char *from_start, const char *from_end, _IO_size_t max)
343 int result;
344 #ifdef _LIBC
345 const unsigned char *cp = (const unsigned char *) from_start;
346 wchar_t to_buf[max];
347 struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
348 int status;
349 size_t dummy;
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;
360 #else
361 /* Decide what to do. */
362 result = 0;
363 #endif
365 return result;
369 static int
370 do_max_length (struct _IO_codecvt *codecvt)
372 #ifdef _LIBC
373 return codecvt->__cd_in.__cd.__steps[0].__max_needed_from;
374 #else
375 return MB_CUR_MAX;
376 #endif