| 1 | /* FILE * interface to a struct __wprintf_buffer. Wide version. |
| 2 | Copyright (C) 2022-2023 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <printf_buffer_as_file.h> |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <printf_buffer.h> |
| 23 | |
| 24 | /* Commit the data directly written through the stdio stream. */ |
| 25 | static void |
| 26 | __wprintf_buffer_as_file_commit (struct __wprintf_buffer_as_file *file) |
| 27 | { |
| 28 | /* Check that the write pointers in the file stream are consistent |
| 29 | with the next buffer. */ |
| 30 | assert (file->wide_stream._IO_write_ptr >= file->next->write_ptr); |
| 31 | assert (file->wide_stream._IO_write_ptr <= file->next->write_end); |
| 32 | assert (file->wide_stream._IO_write_base == file->next->write_base); |
| 33 | assert (file->wide_stream._IO_write_end == file->next->write_end); |
| 34 | |
| 35 | file->next->write_ptr = file->wide_stream._IO_write_ptr; |
| 36 | } |
| 37 | |
| 38 | /* Pointer the FILE * write buffer into the active struct __wprintf_buffer |
| 39 | area. */ |
| 40 | static void |
| 41 | __wprintf_buffer_as_file_switch_to_buffer (struct __wprintf_buffer_as_file *file) |
| 42 | { |
| 43 | file->wide_stream._IO_write_base = file->next->write_base; |
| 44 | file->wide_stream._IO_write_ptr = file->next->write_ptr; |
| 45 | file->wide_stream._IO_write_end = file->next->write_end; |
| 46 | } |
| 47 | |
| 48 | /* Only a small subset of the vtable functions is implemented here, |
| 49 | following _IO_obstack_jumps. */ |
| 50 | |
| 51 | wint_t |
| 52 | __wprintf_buffer_as_file_overflow (FILE *fp, int ch) |
| 53 | { |
| 54 | struct __wprintf_buffer_as_file *file |
| 55 | = (struct __wprintf_buffer_as_file *) fp; |
| 56 | |
| 57 | __wprintf_buffer_as_file_commit (file); |
| 58 | |
| 59 | /* EOF means only a flush is requested. */ |
| 60 | if (ch != WEOF) |
| 61 | __wprintf_buffer_putc (file->next, ch); |
| 62 | else |
| 63 | ch = 0; |
| 64 | |
| 65 | /* Ensure that flushing actually produces room. */ |
| 66 | if (!__wprintf_buffer_has_failed (file->next) |
| 67 | && file->next->write_ptr == file->next->write_end) |
| 68 | __wprintf_buffer_flush (file->next); |
| 69 | |
| 70 | __wprintf_buffer_as_file_switch_to_buffer (file); |
| 71 | |
| 72 | if (!__wprintf_buffer_has_failed (file->next)) |
| 73 | return (unsigned char) ch; |
| 74 | else |
| 75 | return WEOF; |
| 76 | } |
| 77 | |
| 78 | size_t |
| 79 | __wprintf_buffer_as_file_xsputn (FILE *fp, const void *buf, size_t len) |
| 80 | { |
| 81 | struct __wprintf_buffer_as_file *file |
| 82 | = (struct __wprintf_buffer_as_file *) fp; |
| 83 | |
| 84 | __wprintf_buffer_as_file_commit (file); |
| 85 | |
| 86 | /* Copy the data. */ |
| 87 | __wprintf_buffer_write (file->next, buf, len); |
| 88 | |
| 89 | __wprintf_buffer_as_file_switch_to_buffer (file); |
| 90 | |
| 91 | if (!__wprintf_buffer_has_failed (file->next)) |
| 92 | return len; |
| 93 | else |
| 94 | /* We may actually have written something. But the stream is |
| 95 | corrupted in this case anyway, so try not to divine the write |
| 96 | count here. */ |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | __wprintf_buffer_as_file_init (struct __wprintf_buffer_as_file *file, |
| 102 | struct __wprintf_buffer *next) |
| 103 | { |
| 104 | file->stream._lock = NULL; |
| 105 | _IO_no_init (&file->stream, _IO_USER_LOCK, 0, &file->wide_stream, |
| 106 | &_IO_wprintf_buffer_as_file_jumps); |
| 107 | _IO_fwide (&file->stream, 1); |
| 108 | |
| 109 | /* Set up the write buffer from the next buffer. */ |
| 110 | file->next = next; |
| 111 | __wprintf_buffer_as_file_switch_to_buffer (file); |
| 112 | |
| 113 | /* Mark the read area as inactive, by making all pointers equal. */ |
| 114 | file->stream._IO_read_base = file->stream._IO_write_base; |
| 115 | file->stream._IO_read_ptr = file->stream._IO_write_base; |
| 116 | file->stream._IO_read_end = file->stream._IO_write_base; |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | __wprintf_buffer_as_file_terminate (struct __wprintf_buffer_as_file *file) |
| 121 | { |
| 122 | if (file->stream._flags & _IO_ERR_SEEN) |
| 123 | return false; |
| 124 | else |
| 125 | { |
| 126 | __wprintf_buffer_as_file_commit (file); |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |