2.9
[glibc/nacl-glibc.git] / libio / wmemstream.c
blob7bf6a429ac8b9a8c6d00f754779f84bfb53aebe6
1 /* Copyright (C) 1995-97,99,2000,2002-2004,2006 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include "libioP.h"
20 #include "strfile.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <wchar.h>
26 struct _IO_FILE_wmemstream
28 _IO_strfile _sf;
29 wchar_t **bufloc;
30 _IO_size_t *sizeloc;
34 static int _IO_wmem_sync (_IO_FILE* fp) __THROW;
35 static void _IO_wmem_finish (_IO_FILE* fp, int) __THROW;
38 static const struct _IO_jump_t _IO_wmem_jumps =
40 JUMP_INIT_DUMMY,
41 JUMP_INIT (finish, _IO_wmem_finish),
42 JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
43 JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
44 JUMP_INIT (uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
45 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
46 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
47 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
48 JUMP_INIT (seekoff, _IO_wstr_seekoff),
49 JUMP_INIT (seekpos, _IO_default_seekpos),
50 JUMP_INIT (setbuf, _IO_default_setbuf),
51 JUMP_INIT (sync, _IO_wmem_sync),
52 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
53 JUMP_INIT (read, _IO_default_read),
54 JUMP_INIT (write, _IO_default_write),
55 JUMP_INIT (seek, _IO_default_seek),
56 JUMP_INIT (close, _IO_default_close),
57 JUMP_INIT (stat, _IO_default_stat),
58 JUMP_INIT (showmanyc, _IO_default_showmanyc),
59 JUMP_INIT (imbue, _IO_default_imbue)
62 /* Open a stream that writes into a malloc'd buffer that is expanded as
63 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
64 and the number of characters written on fflush or fclose. */
65 _IO_FILE *
66 open_wmemstream (bufloc, sizeloc)
67 wchar_t **bufloc;
68 _IO_size_t *sizeloc;
70 struct locked_FILE
72 struct _IO_FILE_wmemstream fp;
73 #ifdef _IO_MTSAFE_IO
74 _IO_lock_t lock;
75 #endif
76 struct _IO_wide_data wd;
77 } *new_f;
78 wchar_t *buf;
80 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
81 if (new_f == NULL)
82 return NULL;
83 #ifdef _IO_MTSAFE_IO
84 new_f->fp._sf._sbf._f._lock = &new_f->lock;
85 #endif
87 buf = calloc (1, _IO_BUFSIZ);
88 if (buf == NULL)
89 return NULL;
91 _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
92 _IO_fwide (&new_f->fp._sf._sbf._f, 1);
93 _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
94 _IO_BUFSIZ / sizeof (wchar_t), buf);
95 new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
96 new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc;
97 new_f->fp._sf._s._free_buffer = (_IO_free_type) free;
99 new_f->fp.bufloc = bufloc;
100 new_f->fp.sizeloc = sizeloc;
102 return (_IO_FILE *) &new_f->fp._sf._sbf;
106 static int
107 _IO_wmem_sync (fp)
108 _IO_FILE* fp;
110 struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
112 if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
114 _IO_wstr_overflow (fp, '\0');
115 --fp->_wide_data->_IO_write_ptr;
117 else
118 *fp->_wide_data->_IO_write_ptr = '\0';
120 *mp->bufloc = fp->_wide_data->_IO_write_base;
121 *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
122 - fp->_wide_data->_IO_write_base);
124 return 0;
128 static void
129 _IO_wmem_finish (fp, dummy)
130 _IO_FILE* fp;
131 int dummy;
133 struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
135 *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base,
136 (fp->_wide_data->_IO_write_ptr
137 - fp->_wide_data->_IO_write_base + 1)
138 * sizeof (wchar_t));
139 if (*mp->bufloc != NULL)
141 size_t len = (fp->_wide_data->_IO_write_ptr
142 - fp->_wide_data->_IO_write_base);
143 (*mp->bufloc)[len] = '\0';
144 *mp->sizeloc = len;
146 fp->_wide_data->_IO_buf_base = NULL;
149 _IO_wstr_finish (fp, 0);