Handle narrowing function sNaN test disabling based on argument format.
[glibc.git] / libio / wgenops.c
blob5ae699c9412afed4d262a8e0ad1b0d5825082070
1 /* Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper <drepper@cygnus.com>.
4 Based on the single byte version by Per Bothner <bothner@cygnus.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 /* Generic or default I/O operations. */
31 #include "libioP.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <wchar.h>
37 static int save_for_wbackup (_IO_FILE *fp, wchar_t *end_p) __THROW;
39 /* Return minimum _pos markers
40 Assumes the current get area is the main get area. */
41 _IO_ssize_t
42 _IO_least_wmarker (_IO_FILE *fp, wchar_t *end_p)
44 _IO_ssize_t least_so_far = end_p - fp->_wide_data->_IO_read_base;
45 struct _IO_marker *mark;
46 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
47 if (mark->_pos < least_so_far)
48 least_so_far = mark->_pos;
49 return least_so_far;
51 libc_hidden_def (_IO_least_wmarker)
53 /* Switch current get area from backup buffer to (start of) main get area. */
54 void
55 _IO_switch_to_main_wget_area (_IO_FILE *fp)
57 wchar_t *tmp;
58 fp->_flags &= ~_IO_IN_BACKUP;
59 /* Swap _IO_read_end and _IO_save_end. */
60 tmp = fp->_wide_data->_IO_read_end;
61 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_save_end;
62 fp->_wide_data->_IO_save_end= tmp;
63 /* Swap _IO_read_base and _IO_save_base. */
64 tmp = fp->_wide_data->_IO_read_base;
65 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_save_base;
66 fp->_wide_data->_IO_save_base = tmp;
67 /* Set _IO_read_ptr. */
68 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_base;
70 libc_hidden_def (_IO_switch_to_main_wget_area)
73 /* Switch current get area from main get area to (end of) backup area. */
74 void
75 _IO_switch_to_wbackup_area (_IO_FILE *fp)
77 wchar_t *tmp;
78 fp->_flags |= _IO_IN_BACKUP;
79 /* Swap _IO_read_end and _IO_save_end. */
80 tmp = fp->_wide_data->_IO_read_end;
81 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_save_end;
82 fp->_wide_data->_IO_save_end = tmp;
83 /* Swap _IO_read_base and _IO_save_base. */
84 tmp = fp->_wide_data->_IO_read_base;
85 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_save_base;
86 fp->_wide_data->_IO_save_base = tmp;
87 /* Set _IO_read_ptr. */
88 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
90 libc_hidden_def (_IO_switch_to_wbackup_area)
93 void
94 _IO_wsetb (_IO_FILE *f, wchar_t *b, wchar_t *eb, int a)
96 if (f->_wide_data->_IO_buf_base && !(f->_flags2 & _IO_FLAGS2_USER_WBUF))
97 free (f->_wide_data->_IO_buf_base);
98 f->_wide_data->_IO_buf_base = b;
99 f->_wide_data->_IO_buf_end = eb;
100 if (a)
101 f->_flags2 &= ~_IO_FLAGS2_USER_WBUF;
102 else
103 f->_flags2 |= _IO_FLAGS2_USER_WBUF;
105 libc_hidden_def (_IO_wsetb)
108 wint_t
109 _IO_wdefault_pbackfail (_IO_FILE *fp, wint_t c)
111 if (fp->_wide_data->_IO_read_ptr > fp->_wide_data->_IO_read_base
112 && !_IO_in_backup (fp)
113 && (wint_t) fp->_IO_read_ptr[-1] == c)
114 --fp->_IO_read_ptr;
115 else
117 /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
118 if (!_IO_in_backup (fp))
120 /* We need to keep the invariant that the main get area
121 logically follows the backup area. */
122 if (fp->_wide_data->_IO_read_ptr > fp->_wide_data->_IO_read_base
123 && _IO_have_wbackup (fp))
125 if (save_for_wbackup (fp, fp->_wide_data->_IO_read_ptr))
126 return WEOF;
128 else if (!_IO_have_wbackup (fp))
130 /* No backup buffer: allocate one. */
131 /* Use nshort buffer, if unused? (probably not) FIXME */
132 int backup_size = 128;
133 wchar_t *bbuf = (wchar_t *) malloc (backup_size
134 * sizeof (wchar_t));
135 if (bbuf == NULL)
136 return WEOF;
137 fp->_wide_data->_IO_save_base = bbuf;
138 fp->_wide_data->_IO_save_end = (fp->_wide_data->_IO_save_base
139 + backup_size);
140 fp->_wide_data->_IO_backup_base = fp->_wide_data->_IO_save_end;
142 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr;
143 _IO_switch_to_wbackup_area (fp);
145 else if (fp->_wide_data->_IO_read_ptr <= fp->_wide_data->_IO_read_base)
147 /* Increase size of existing backup buffer. */
148 _IO_size_t new_size;
149 _IO_size_t old_size = (fp->_wide_data->_IO_read_end
150 - fp->_wide_data->_IO_read_base);
151 wchar_t *new_buf;
152 new_size = 2 * old_size;
153 new_buf = (wchar_t *) malloc (new_size * sizeof (wchar_t));
154 if (new_buf == NULL)
155 return WEOF;
156 __wmemcpy (new_buf + (new_size - old_size),
157 fp->_wide_data->_IO_read_base, old_size);
158 free (fp->_wide_data->_IO_read_base);
159 _IO_wsetg (fp, new_buf, new_buf + (new_size - old_size),
160 new_buf + new_size);
161 fp->_wide_data->_IO_backup_base = fp->_wide_data->_IO_read_ptr;
164 *--fp->_wide_data->_IO_read_ptr = c;
166 return c;
168 libc_hidden_def (_IO_wdefault_pbackfail)
171 void
172 _IO_wdefault_finish (_IO_FILE *fp, int dummy)
174 struct _IO_marker *mark;
175 if (fp->_wide_data->_IO_buf_base && !(fp->_flags2 & _IO_FLAGS2_USER_WBUF))
177 free (fp->_wide_data->_IO_buf_base);
178 fp->_wide_data->_IO_buf_base = fp->_wide_data->_IO_buf_end = NULL;
181 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
182 mark->_sbuf = NULL;
184 if (fp->_IO_save_base)
186 free (fp->_wide_data->_IO_save_base);
187 fp->_IO_save_base = NULL;
190 #ifdef _IO_MTSAFE_IO
191 if (fp->_lock != NULL)
192 _IO_lock_fini (*fp->_lock);
193 #endif
195 _IO_un_link ((struct _IO_FILE_plus *) fp);
197 libc_hidden_def (_IO_wdefault_finish)
200 wint_t
201 _IO_wdefault_uflow (_IO_FILE *fp)
203 wint_t wch;
204 wch = _IO_UNDERFLOW (fp);
205 if (wch == WEOF)
206 return WEOF;
207 return *fp->_wide_data->_IO_read_ptr++;
209 libc_hidden_def (_IO_wdefault_uflow)
212 wint_t
213 __woverflow (_IO_FILE *f, wint_t wch)
215 if (f->_mode == 0)
216 _IO_fwide (f, 1);
217 return _IO_OVERFLOW (f, wch);
219 libc_hidden_def (__woverflow)
222 wint_t
223 __wuflow (_IO_FILE *fp)
225 if (fp->_mode < 0 || (fp->_mode == 0 && _IO_fwide (fp, 1) != 1))
226 return WEOF;
228 if (fp->_mode == 0)
229 _IO_fwide (fp, 1);
230 if (_IO_in_put_mode (fp))
231 if (_IO_switch_to_wget_mode (fp) == EOF)
232 return WEOF;
233 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
234 return *fp->_wide_data->_IO_read_ptr++;
235 if (_IO_in_backup (fp))
237 _IO_switch_to_main_wget_area (fp);
238 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
239 return *fp->_wide_data->_IO_read_ptr++;
241 if (_IO_have_markers (fp))
243 if (save_for_wbackup (fp, fp->_wide_data->_IO_read_end))
244 return WEOF;
246 else if (_IO_have_wbackup (fp))
247 _IO_free_wbackup_area (fp);
248 return _IO_UFLOW (fp);
250 libc_hidden_def (__wuflow)
252 wint_t
253 __wunderflow (_IO_FILE *fp)
255 if (fp->_mode < 0 || (fp->_mode == 0 && _IO_fwide (fp, 1) != 1))
256 return WEOF;
258 if (fp->_mode == 0)
259 _IO_fwide (fp, 1);
260 if (_IO_in_put_mode (fp))
261 if (_IO_switch_to_wget_mode (fp) == EOF)
262 return WEOF;
263 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
264 return *fp->_wide_data->_IO_read_ptr;
265 if (_IO_in_backup (fp))
267 _IO_switch_to_main_wget_area (fp);
268 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
269 return *fp->_wide_data->_IO_read_ptr;
271 if (_IO_have_markers (fp))
273 if (save_for_wbackup (fp, fp->_wide_data->_IO_read_end))
274 return WEOF;
276 else if (_IO_have_backup (fp))
277 _IO_free_wbackup_area (fp);
278 return _IO_UNDERFLOW (fp);
280 libc_hidden_def (__wunderflow)
283 _IO_size_t
284 _IO_wdefault_xsputn (_IO_FILE *f, const void *data, _IO_size_t n)
286 const wchar_t *s = (const wchar_t *) data;
287 _IO_size_t more = n;
288 if (more <= 0)
289 return 0;
290 for (;;)
292 /* Space available. */
293 _IO_ssize_t count = (f->_wide_data->_IO_write_end
294 - f->_wide_data->_IO_write_ptr);
295 if (count > 0)
297 if ((_IO_size_t) count > more)
298 count = more;
299 if (count > 20)
301 f->_wide_data->_IO_write_ptr =
302 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
303 s += count;
305 else if (count <= 0)
306 count = 0;
307 else
309 wchar_t *p = f->_wide_data->_IO_write_ptr;
310 _IO_ssize_t i;
311 for (i = count; --i >= 0; )
312 *p++ = *s++;
313 f->_wide_data->_IO_write_ptr = p;
315 more -= count;
317 if (more == 0 || __woverflow (f, *s++) == WEOF)
318 break;
319 more--;
321 return n - more;
323 libc_hidden_def (_IO_wdefault_xsputn)
326 _IO_size_t
327 _IO_wdefault_xsgetn (_IO_FILE *fp, void *data, _IO_size_t n)
329 _IO_size_t more = n;
330 wchar_t *s = (wchar_t*) data;
331 for (;;)
333 /* Data available. */
334 _IO_ssize_t count = (fp->_wide_data->_IO_read_end
335 - fp->_wide_data->_IO_read_ptr);
336 if (count > 0)
338 if ((_IO_size_t) count > more)
339 count = more;
340 if (count > 20)
342 s = __wmempcpy (s, fp->_wide_data->_IO_read_ptr, count);
343 fp->_wide_data->_IO_read_ptr += count;
345 else if (count <= 0)
346 count = 0;
347 else
349 wchar_t *p = fp->_wide_data->_IO_read_ptr;
350 int i = (int) count;
351 while (--i >= 0)
352 *s++ = *p++;
353 fp->_wide_data->_IO_read_ptr = p;
355 more -= count;
357 if (more == 0 || __wunderflow (fp) == WEOF)
358 break;
360 return n - more;
362 libc_hidden_def (_IO_wdefault_xsgetn)
365 void
366 _IO_wdoallocbuf (_IO_FILE *fp)
368 if (fp->_wide_data->_IO_buf_base)
369 return;
370 if (!(fp->_flags & _IO_UNBUFFERED))
371 if ((wint_t)_IO_WDOALLOCATE (fp) != WEOF)
372 return;
373 _IO_wsetb (fp, fp->_wide_data->_shortbuf,
374 fp->_wide_data->_shortbuf + 1, 0);
376 libc_hidden_def (_IO_wdoallocbuf)
380 _IO_wdefault_doallocate (_IO_FILE *fp)
382 wchar_t *buf;
384 buf = malloc (_IO_BUFSIZ);
385 if (__glibc_unlikely (buf == NULL))
386 return EOF;
387 _IO_wsetb (fp, buf, buf + _IO_BUFSIZ, 1);
388 return 1;
390 libc_hidden_def (_IO_wdefault_doallocate)
394 _IO_switch_to_wget_mode (_IO_FILE *fp)
396 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
397 if ((wint_t)_IO_WOVERFLOW (fp, WEOF) == WEOF)
398 return EOF;
399 if (_IO_in_backup (fp))
400 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_backup_base;
401 else
403 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_buf_base;
404 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_read_end)
405 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_write_ptr;
407 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_write_ptr;
409 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
410 = fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_read_ptr;
412 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
413 return 0;
415 libc_hidden_def (_IO_switch_to_wget_mode)
417 void
418 _IO_free_wbackup_area (_IO_FILE *fp)
420 if (_IO_in_backup (fp))
421 _IO_switch_to_main_wget_area (fp); /* Just in case. */
422 free (fp->_wide_data->_IO_save_base);
423 fp->_wide_data->_IO_save_base = NULL;
424 fp->_wide_data->_IO_save_end = NULL;
425 fp->_wide_data->_IO_backup_base = NULL;
427 libc_hidden_def (_IO_free_wbackup_area)
429 #if 0
431 _IO_switch_to_wput_mode (_IO_FILE *fp)
433 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_read_ptr;
434 fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_read_ptr;
435 /* Following is wrong if line- or un-buffered? */
436 fp->_wide_data->_IO_write_end = (fp->_flags & _IO_IN_BACKUP
437 ? fp->_wide_data->_IO_read_end
438 : fp->_wide_data->_IO_buf_end);
440 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
441 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_end;
443 fp->_flags |= _IO_CURRENTLY_PUTTING;
444 return 0;
446 #endif
449 static int
450 save_for_wbackup (_IO_FILE *fp, wchar_t *end_p)
452 /* Append [_IO_read_base..end_p] to backup area. */
453 _IO_ssize_t least_mark = _IO_least_wmarker (fp, end_p);
454 /* needed_size is how much space we need in the backup area. */
455 _IO_size_t needed_size = ((end_p - fp->_wide_data->_IO_read_base)
456 - least_mark);
457 /* FIXME: Dubious arithmetic if pointers are NULL */
458 _IO_size_t current_Bsize = (fp->_wide_data->_IO_save_end
459 - fp->_wide_data->_IO_save_base);
460 _IO_size_t avail; /* Extra space available for future expansion. */
461 _IO_ssize_t delta;
462 struct _IO_marker *mark;
463 if (needed_size > current_Bsize)
465 wchar_t *new_buffer;
466 avail = 100;
467 new_buffer = (wchar_t *) malloc ((avail + needed_size)
468 * sizeof (wchar_t));
469 if (new_buffer == NULL)
470 return EOF; /* FIXME */
471 if (least_mark < 0)
473 __wmempcpy (__wmempcpy (new_buffer + avail,
474 fp->_wide_data->_IO_save_end + least_mark,
475 -least_mark),
476 fp->_wide_data->_IO_read_base,
477 end_p - fp->_wide_data->_IO_read_base);
479 else
481 __wmemcpy (new_buffer + avail,
482 fp->_wide_data->_IO_read_base + least_mark,
483 needed_size);
485 free (fp->_wide_data->_IO_save_base);
486 fp->_wide_data->_IO_save_base = new_buffer;
487 fp->_wide_data->_IO_save_end = new_buffer + avail + needed_size;
489 else
491 avail = current_Bsize - needed_size;
492 if (least_mark < 0)
494 __wmemmove (fp->_wide_data->_IO_save_base + avail,
495 fp->_wide_data->_IO_save_end + least_mark,
496 -least_mark);
497 __wmemcpy (fp->_wide_data->_IO_save_base + avail - least_mark,
498 fp->_wide_data->_IO_read_base,
499 end_p - fp->_wide_data->_IO_read_base);
501 else if (needed_size > 0)
502 __wmemcpy (fp->_wide_data->_IO_save_base + avail,
503 fp->_wide_data->_IO_read_base + least_mark,
504 needed_size);
506 fp->_wide_data->_IO_backup_base = fp->_wide_data->_IO_save_base + avail;
507 /* Adjust all the streammarkers. */
508 delta = end_p - fp->_wide_data->_IO_read_base;
509 for (mark = fp->_markers; mark != NULL; mark = mark->_next)
510 mark->_pos -= delta;
511 return 0;
514 wint_t
515 _IO_sputbackwc (_IO_FILE *fp, wint_t c)
517 wint_t result;
519 if (fp->_wide_data->_IO_read_ptr > fp->_wide_data->_IO_read_base
520 && (wchar_t)fp->_wide_data->_IO_read_ptr[-1] == (wchar_t) c)
522 fp->_wide_data->_IO_read_ptr--;
523 result = c;
525 else
526 result = _IO_PBACKFAIL (fp, c);
528 if (result != WEOF)
529 fp->_flags &= ~_IO_EOF_SEEN;
531 return result;
533 libc_hidden_def (_IO_sputbackwc)
535 wint_t
536 _IO_sungetwc (_IO_FILE *fp)
538 wint_t result;
540 if (fp->_wide_data->_IO_read_ptr > fp->_wide_data->_IO_read_base)
542 fp->_wide_data->_IO_read_ptr--;
543 result = *fp->_wide_data->_IO_read_ptr;
545 else
546 result = _IO_PBACKFAIL (fp, EOF);
548 if (result != WEOF)
549 fp->_flags &= ~_IO_EOF_SEEN;
551 return result;
555 unsigned
556 _IO_adjust_wcolumn (unsigned start, const wchar_t *line, int count)
558 const wchar_t *ptr = line + count;
559 while (ptr > line)
560 if (*--ptr == L'\n')
561 return line + count - ptr - 1;
562 return start + count;
565 void
566 _IO_init_wmarker (struct _IO_marker *marker, _IO_FILE *fp)
568 marker->_sbuf = fp;
569 if (_IO_in_put_mode (fp))
570 _IO_switch_to_wget_mode (fp);
571 if (_IO_in_backup (fp))
572 marker->_pos = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
573 else
574 marker->_pos = (fp->_wide_data->_IO_read_ptr
575 - fp->_wide_data->_IO_read_base);
577 /* Should perhaps sort the chain? */
578 marker->_next = fp->_markers;
579 fp->_markers = marker;
582 #define BAD_DELTA EOF
584 /* Return difference between MARK and current position of MARK's stream. */
586 _IO_wmarker_delta (struct _IO_marker *mark)
588 int cur_pos;
589 if (mark->_sbuf == NULL)
590 return BAD_DELTA;
591 if (_IO_in_backup (mark->_sbuf))
592 cur_pos = (mark->_sbuf->_wide_data->_IO_read_ptr
593 - mark->_sbuf->_wide_data->_IO_read_end);
594 else
595 cur_pos = (mark->_sbuf->_wide_data->_IO_read_ptr
596 - mark->_sbuf->_wide_data->_IO_read_base);
597 return mark->_pos - cur_pos;
601 _IO_seekwmark (_IO_FILE *fp, struct _IO_marker *mark, int delta)
603 if (mark->_sbuf != fp)
604 return EOF;
605 if (mark->_pos >= 0)
607 if (_IO_in_backup (fp))
608 _IO_switch_to_main_wget_area (fp);
609 fp->_wide_data->_IO_read_ptr = (fp->_wide_data->_IO_read_base
610 + mark->_pos);
612 else
614 if (!_IO_in_backup (fp))
615 _IO_switch_to_wbackup_area (fp);
616 fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end + mark->_pos;
618 return 0;
621 void
622 _IO_unsave_wmarkers (_IO_FILE *fp)
624 struct _IO_marker *mark = fp->_markers;
625 if (mark)
627 #ifdef TODO
628 streampos offset = seekoff (0, ios::cur, ios::in);
629 if (offset != EOF)
631 offset += eGptr () - Gbase ();
632 for ( ; mark != NULL; mark = mark->_next)
633 mark->set_streampos (mark->_pos + offset);
635 else
637 for ( ; mark != NULL; mark = mark->_next)
638 mark->set_streampos (EOF);
640 #endif
641 fp->_markers = 0;
644 if (_IO_have_backup (fp))
645 _IO_free_wbackup_area (fp);