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.
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.
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.
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/>. */
19 #include <printf_buffer_as_file.h>
22 #include <printf_buffer.h>
24 /* Commit the data directly written through the stdio stream. */
26 __wprintf_buffer_as_file_commit (struct __wprintf_buffer_as_file
*file
)
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
);
35 file
->next
->write_ptr
= file
->wide_stream
._IO_write_ptr
;
38 /* Pointer the FILE * write buffer into the active struct __wprintf_buffer
41 __wprintf_buffer_as_file_switch_to_buffer (struct __wprintf_buffer_as_file
*file
)
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
;
48 /* Only a small subset of the vtable functions is implemented here,
49 following _IO_obstack_jumps. */
52 __wprintf_buffer_as_file_overflow (FILE *fp
, int ch
)
54 struct __wprintf_buffer_as_file
*file
55 = (struct __wprintf_buffer_as_file
*) fp
;
57 __wprintf_buffer_as_file_commit (file
);
59 /* EOF means only a flush is requested. */
61 __wprintf_buffer_putc (file
->next
, ch
);
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
);
70 __wprintf_buffer_as_file_switch_to_buffer (file
);
72 if (!__wprintf_buffer_has_failed (file
->next
))
73 return (unsigned char) ch
;
79 __wprintf_buffer_as_file_xsputn (FILE *fp
, const void *buf
, size_t len
)
81 struct __wprintf_buffer_as_file
*file
82 = (struct __wprintf_buffer_as_file
*) fp
;
84 __wprintf_buffer_as_file_commit (file
);
87 __wprintf_buffer_write (file
->next
, buf
, len
);
89 __wprintf_buffer_as_file_switch_to_buffer (file
);
91 if (!__wprintf_buffer_has_failed (file
->next
))
94 /* We may actually have written something. But the stream is
95 corrupted in this case anyway, so try not to divine the write
101 __wprintf_buffer_as_file_init (struct __wprintf_buffer_as_file
*file
,
102 struct __wprintf_buffer
*next
)
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);
109 /* Set up the write buffer from the next buffer. */
111 __wprintf_buffer_as_file_switch_to_buffer (file
);
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
;
120 __wprintf_buffer_as_file_terminate (struct __wprintf_buffer_as_file
*file
)
122 if (file
->stream
._flags
& _IO_ERR_SEEN
)
126 __wprintf_buffer_as_file_commit (file
);