]>
Commit | Line | Data |
---|---|---|
fa0bc87c | 1 | /* Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
96aa2d94 RM |
2 | This file is part of the GNU C Library. |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
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 | |
16 | not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
18 | ||
96aa2d94 | 19 | #include "libioP.h" |
edf5b2d7 | 20 | #include "strfile.h" |
fa0bc87c | 21 | #include <stdio.h> |
96aa2d94 RM |
22 | #include <stdlib.h> |
23 | ||
24 | ||
25 | struct _IO_FILE_memstream | |
26 | { | |
27 | _IO_strfile _sf; | |
28 | char **bufloc; | |
29 | _IO_size_t *sizeloc; | |
30 | }; | |
31 | ||
32 | ||
33 | static int _IO_mem_sync __P ((_IO_FILE* fp)); | |
77a58cad | 34 | static void _IO_mem_finish __P ((_IO_FILE* fp)); |
96aa2d94 RM |
35 | |
36 | ||
37 | static const struct _IO_jump_t _IO_mem_jumps = | |
38 | { | |
39 | JUMP_INIT_DUMMY, | |
77a58cad | 40 | JUMP_INIT (finish, _IO_mem_finish), |
96aa2d94 RM |
41 | JUMP_INIT (overflow, _IO_str_overflow), |
42 | JUMP_INIT (underflow, _IO_str_underflow), | |
43 | JUMP_INIT (uflow, _IO_default_uflow), | |
44 | JUMP_INIT (pbackfail, _IO_str_pbackfail), | |
45 | JUMP_INIT (xsputn, _IO_default_xsputn), | |
46 | JUMP_INIT (xsgetn, _IO_default_xsgetn), | |
47 | JUMP_INIT (seekoff, _IO_str_seekoff), | |
48 | JUMP_INIT (seekpos, _IO_default_seekpos), | |
49 | JUMP_INIT (setbuf, _IO_default_setbuf), | |
50 | JUMP_INIT (sync, _IO_mem_sync), | |
51 | JUMP_INIT (doallocate, _IO_default_doallocate), | |
52 | JUMP_INIT (read, _IO_default_read), | |
53 | JUMP_INIT (write, _IO_default_write), | |
54 | JUMP_INIT (seek, _IO_default_seek), | |
77a58cad | 55 | JUMP_INIT (close, _IO_default_close), |
96aa2d94 RM |
56 | JUMP_INIT (stat, _IO_default_stat) |
57 | }; | |
58 | ||
59 | /* Open a stream that writes into a malloc'd buffer that is expanded as | |
60 | necessary. *BUFLOC and *SIZELOC are updated with the buffer's location | |
61 | and the number of characters written on fflush or fclose. */ | |
62 | _IO_FILE * | |
63 | open_memstream (bufloc, sizeloc) | |
64 | char **bufloc; | |
65 | _IO_size_t *sizeloc; | |
66 | { | |
edf5b2d7 UD |
67 | struct locked_FILE |
68 | { | |
69 | struct _IO_FILE_memstream fp; | |
499e7464 | 70 | #ifdef _IO_MTSAFE_IO |
edf5b2d7 | 71 | _IO_lock_t lock; |
499e7464 | 72 | #endif |
edf5b2d7 | 73 | } *new_f; |
96aa2d94 RM |
74 | char *buf; |
75 | ||
edf5b2d7 UD |
76 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
77 | if (new_f == NULL) | |
96aa2d94 | 78 | return NULL; |
499e7464 | 79 | #ifdef _IO_MTSAFE_IO |
f65fd747 | 80 | new_f->fp._sf._sbf._f._lock = &new_f->lock; |
499e7464 | 81 | #endif |
96aa2d94 RM |
82 | |
83 | buf = ALLOC_BUF (_IO_BUFSIZ); | |
f65fd747 UD |
84 | _IO_init (&new_f->fp._sf._sbf._f, 0); |
85 | _IO_JUMPS (&new_f->fp._sf._sbf._f) = &_IO_mem_jumps; | |
86 | _IO_str_init_static (&new_f->fp._sf._sbf._f, buf, _IO_BUFSIZ, buf); | |
87 | new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF; | |
edf5b2d7 UD |
88 | new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc; |
89 | new_f->fp._sf._s._free_buffer = (_IO_free_type) free; | |
96aa2d94 | 90 | |
edf5b2d7 UD |
91 | new_f->fp.bufloc = bufloc; |
92 | new_f->fp.sizeloc = sizeloc; | |
77a58cad | 93 | |
f65fd747 | 94 | return &new_f->fp._sf._sbf._f; |
96aa2d94 RM |
95 | } |
96 | ||
97 | ||
98 | static int | |
99 | _IO_mem_sync (fp) | |
100 | _IO_FILE* fp; | |
101 | { | |
102 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; | |
103 | int res; | |
104 | ||
105 | res = _IO_default_sync (fp); | |
106 | if (res < 0) | |
107 | return res; | |
108 | ||
109 | if (fp->_IO_write_ptr == fp->_IO_write_end) | |
110 | { | |
111 | _IO_str_overflow (fp, '\0'); | |
112 | --fp->_IO_write_ptr; | |
113 | } | |
114 | else | |
115 | *fp->_IO_write_ptr = '\0'; | |
116 | ||
77a58cad RM |
117 | *mp->bufloc = fp->_IO_write_base; |
118 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; | |
96aa2d94 RM |
119 | |
120 | return 0; | |
121 | } | |
122 | ||
123 | ||
77a58cad RM |
124 | static void |
125 | _IO_mem_finish (fp) | |
96aa2d94 RM |
126 | _IO_FILE* fp; |
127 | { | |
128 | struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; | |
96aa2d94 | 129 | |
77a58cad RM |
130 | *mp->bufloc = (char *) realloc (fp->_IO_write_base, |
131 | fp->_IO_write_ptr - fp->_IO_write_base + 1); | |
132 | if (*mp->bufloc != NULL) | |
133 | { | |
134 | (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0'; | |
135 | *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; | |
136 | } | |
96aa2d94 | 137 | |
77a58cad | 138 | fp->_IO_buf_base = NULL; |
96aa2d94 | 139 | |
77a58cad | 140 | _IO_default_finish (fp); |
96aa2d94 | 141 | } |