Update resolver news.
[glibc.git] / libio / memstream.c
blobceda7c76f23a35a25bef0810b336a0722c23241b
1 /* Copyright (C) 1995, 1996, 1997, 1999, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include "libioP.h"
20 #include "strfile.h"
21 #include <stdio.h>
22 #include <stdlib.h>
25 struct _IO_FILE_memstream
27 _IO_strfile _sf;
28 char **bufloc;
29 _IO_size_t *sizeloc;
33 static int _IO_mem_sync __P ((_IO_FILE* fp));
34 static void _IO_mem_finish __P ((_IO_FILE* fp, int));
35 static int _IO_wmem_sync __P ((_IO_FILE* fp));
36 static void _IO_wmem_finish __P ((_IO_FILE* fp, int));
39 static struct _IO_jump_t _IO_mem_jumps =
41 JUMP_INIT_DUMMY,
42 JUMP_INIT (finish, _IO_mem_finish),
43 JUMP_INIT (overflow, _IO_str_overflow),
44 JUMP_INIT (underflow, _IO_str_underflow),
45 JUMP_INIT (uflow, _IO_default_uflow),
46 JUMP_INIT (pbackfail, _IO_str_pbackfail),
47 JUMP_INIT (xsputn, _IO_default_xsputn),
48 JUMP_INIT (xsgetn, _IO_default_xsgetn),
49 JUMP_INIT (seekoff, _IO_str_seekoff),
50 JUMP_INIT (seekpos, _IO_default_seekpos),
51 JUMP_INIT (setbuf, _IO_default_setbuf),
52 JUMP_INIT (sync, _IO_mem_sync),
53 JUMP_INIT (doallocate, _IO_default_doallocate),
54 JUMP_INIT (read, _IO_default_read),
55 JUMP_INIT (write, _IO_default_write),
56 JUMP_INIT (seek, _IO_default_seek),
57 JUMP_INIT (close, _IO_default_close),
58 JUMP_INIT (stat, _IO_default_stat),
59 JUMP_INIT(showmanyc, _IO_default_showmanyc),
60 JUMP_INIT(imbue, _IO_default_imbue)
63 static struct _IO_jump_t _IO_wmem_jumps =
65 JUMP_INIT_DUMMY,
66 JUMP_INIT (finish, (_IO_finish_t) _IO_wmem_finish),
67 JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
68 JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
69 JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow),
70 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
71 JUMP_INIT (xsputn, (_IO_xsputn_t) _IO_wdefault_xsputn),
72 JUMP_INIT (xsgetn, (_IO_xsgetn_t) _IO_wdefault_xsgetn),
73 JUMP_INIT (seekoff, _IO_wstr_seekoff),
74 JUMP_INIT (seekpos, _IO_default_seekpos),
75 JUMP_INIT (setbuf, (_IO_setbuf_t) _IO_wdefault_setbuf),
76 JUMP_INIT (sync, (_IO_sync_t) _IO_wmem_sync),
77 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
78 JUMP_INIT (read, _IO_default_read),
79 JUMP_INIT (write, _IO_default_write),
80 JUMP_INIT (seek, _IO_default_seek),
81 JUMP_INIT (close, _IO_default_close),
82 JUMP_INIT (stat, _IO_default_stat),
83 JUMP_INIT(showmanyc, _IO_default_showmanyc),
84 JUMP_INIT(imbue, _IO_default_imbue)
87 /* Open a stream that writes into a malloc'd buffer that is expanded as
88 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
89 and the number of characters written on fflush or fclose. */
90 _IO_FILE *
91 open_memstream (bufloc, sizeloc)
92 char **bufloc;
93 _IO_size_t *sizeloc;
95 struct locked_FILE
97 struct _IO_FILE_memstream fp;
98 #ifdef _IO_MTSAFE_IO
99 _IO_lock_t lock;
100 #endif
101 struct _IO_wide_data wd;
102 } *new_f;
103 char *buf;
105 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
106 if (new_f == NULL)
107 return NULL;
108 #ifdef _IO_MTSAFE_IO
109 new_f->fp._sf._sbf._f._lock = &new_f->lock;
110 #endif
112 buf = malloc (_IO_BUFSIZ);
113 if (buf == NULL)
114 return NULL;
115 _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
116 _IO_JUMPS ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf) = &_IO_mem_jumps;
117 _IO_str_init_static (&new_f->fp._sf, buf, _IO_BUFSIZ, buf);
118 new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
119 new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc;
120 new_f->fp._sf._s._free_buffer = (_IO_free_type) free;
122 new_f->fp.bufloc = bufloc;
123 new_f->fp.sizeloc = sizeloc;
125 return (_IO_FILE *) &new_f->fp._sf._sbf;
129 static int
130 _IO_mem_sync (fp)
131 _IO_FILE* fp;
133 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
134 int res;
136 res = _IO_default_sync (fp);
137 if (res < 0)
138 return res;
140 if (fp->_IO_write_ptr == fp->_IO_write_end)
142 _IO_str_overflow (fp, '\0');
143 --fp->_IO_write_ptr;
145 else
146 *fp->_IO_write_ptr = '\0';
148 *mp->bufloc = fp->_IO_write_base;
149 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
151 return 0;
155 static void
156 _IO_mem_finish (fp, dummy)
157 _IO_FILE* fp;
158 int dummy;
160 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
162 *mp->bufloc = (char *) realloc (fp->_IO_write_base,
163 fp->_IO_write_ptr - fp->_IO_write_base + 1);
164 if (*mp->bufloc != NULL)
166 (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
167 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
170 fp->_IO_buf_base = NULL;
172 _IO_default_finish (fp, 0);
176 static int
177 _IO_wmem_sync (fp)
178 _IO_FILE* fp;
180 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
181 int res;
183 res = _IO_default_sync (fp);
184 if (res < 0)
185 return res;
187 if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
189 _IO_wstr_overflow (fp, L'\0');
190 --fp->_wide_data->_IO_write_ptr;
192 else
193 *fp->_wide_data->_IO_write_ptr = '\0';
195 *mp->bufloc = (char *) fp->_wide_data->_IO_write_base;
196 *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
197 - fp->_wide_data->_IO_write_base);
199 return 0;
203 static void
204 _IO_wmem_finish (fp, dummy)
205 _IO_FILE* fp;
206 int dummy;
208 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
210 *mp->bufloc = (char *) realloc (fp->_wide_data->_IO_write_base,
211 (fp->_wide_data->_IO_write_ptr
212 - fp->_wide_data->_IO_write_base + 1)
213 * sizeof (wchar_t));
214 if (*mp->bufloc != NULL)
216 ((wchar_t *) (*mp->bufloc))[fp->_wide_data->_IO_write_ptr
217 - fp->_wide_data->_IO_write_base] = '\0';
218 *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
219 - fp->_wide_data->_IO_write_base);
222 fp->_wide_data->_IO_buf_base = NULL;
224 _IO_default_finish (fp, 0);