1 /* Copyright (C) 1995, 1996, 1997, 1999 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. */
25 struct _IO_FILE_memstream
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
=
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
=
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. */
91 open_memstream (bufloc
, sizeloc
)
97 struct _IO_FILE_memstream fp
;
101 struct _IO_wide_data wd
;
105 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
109 new_f
->fp
._sf
._sbf
._f
._lock
= &new_f
->lock
;
112 buf
= malloc (_IO_BUFSIZ
);
115 _IO_no_init (&new_f
->fp
._sf
._sbf
._f
, 0, 0, &new_f
->wd
, &_IO_wmem_jumps
);
116 _IO_JUMPS (&new_f
->fp
._sf
._sbf
._f
) = &_IO_mem_jumps
;
117 _IO_str_init_static (&new_f
->fp
._sf
._sbf
._f
, 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 &new_f
->fp
._sf
._sbf
._f
;
133 struct _IO_FILE_memstream
*mp
= (struct _IO_FILE_memstream
*) fp
;
136 res
= _IO_default_sync (fp
);
140 if (fp
->_IO_write_ptr
== fp
->_IO_write_end
)
142 _IO_str_overflow (fp
, '\0');
146 *fp
->_IO_write_ptr
= '\0';
148 *mp
->bufloc
= fp
->_IO_write_base
;
149 *mp
->sizeloc
= fp
->_IO_write_ptr
- fp
->_IO_write_base
;
156 _IO_mem_finish (fp
, 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);
180 struct _IO_FILE_memstream
*mp
= (struct _IO_FILE_memstream
*) fp
;
183 res
= _IO_default_sync (fp
);
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
;
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
);
204 _IO_wmem_finish (fp
, 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)
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);