1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 struct _IO_FILE_wmemstream
33 /* Open a stream that writes into a malloc'd buffer that is expanded as
34 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
35 and the number of characters written on fflush or fclose. */
37 open_wmemstream (wchar_t **bufloc
, size_t *sizeloc
)
41 struct _IO_FILE_wmemstream fp
;
45 struct _IO_wide_data wd
;
49 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
53 new_f
->fp
._sf
._sbf
._f
._lock
= &new_f
->lock
;
56 buf
= calloc (1, BUFSIZ
);
62 _IO_no_init (&new_f
->fp
._sf
._sbf
._f
, 0, 0, &new_f
->wd
, &_IO_wmem_jumps
);
63 _IO_fwide (&new_f
->fp
._sf
._sbf
._f
, 1);
64 _IO_wstr_init_static (&new_f
->fp
._sf
._sbf
._f
, buf
,
65 BUFSIZ
/ sizeof (wchar_t), buf
);
66 new_f
->fp
._sf
._sbf
._f
._flags2
&= ~_IO_FLAGS2_USER_WBUF
;
67 new_f
->fp
._sf
._s
._allocate_buffer_unused
= (_IO_alloc_type
) malloc
;
68 new_f
->fp
._sf
._s
._free_buffer_unused
= (_IO_free_type
) free
;
70 new_f
->fp
.bufloc
= bufloc
;
71 new_f
->fp
.sizeloc
= sizeloc
;
73 /* Disable single thread optimization. BZ 21735. */
74 new_f
->fp
._sf
._sbf
._f
._flags2
|= _IO_FLAGS2_NEED_LOCK
;
76 return (FILE *) &new_f
->fp
._sf
._sbf
;
81 _IO_wmem_sync (FILE *fp
)
83 struct _IO_FILE_wmemstream
*mp
= (struct _IO_FILE_wmemstream
*) fp
;
85 if (fp
->_wide_data
->_IO_write_ptr
== fp
->_wide_data
->_IO_write_end
)
87 _IO_wstr_overflow (fp
, '\0');
88 --fp
->_wide_data
->_IO_write_ptr
;
91 *mp
->bufloc
= fp
->_wide_data
->_IO_write_base
;
92 *mp
->sizeloc
= (fp
->_wide_data
->_IO_write_ptr
93 - fp
->_wide_data
->_IO_write_base
);
100 _IO_wmem_finish (FILE *fp
, int dummy
)
102 struct _IO_FILE_wmemstream
*mp
= (struct _IO_FILE_wmemstream
*) fp
;
104 *mp
->bufloc
= (wchar_t *) realloc (fp
->_wide_data
->_IO_write_base
,
105 (fp
->_wide_data
->_IO_write_ptr
106 - fp
->_wide_data
->_IO_write_base
+ 1)
108 if (*mp
->bufloc
!= NULL
)
110 size_t len
= (fp
->_wide_data
->_IO_write_ptr
111 - fp
->_wide_data
->_IO_write_base
);
112 (*mp
->bufloc
)[len
] = '\0';
115 fp
->_wide_data
->_IO_buf_base
= NULL
;
118 _IO_wstr_finish (fp
, 0);