1 /* Copyright (C) 1993, 1997-2000, 2001 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
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
31 #include <stdio_ext.h>
34 /* The following definitions are for exposition only.
35 They map the terminology used in the ANSI/ISO C++ draft standard
36 to the implementation. */
38 /* allocated: set when a dynamic array object has been allocated, and
39 hence should be freed by the destructor for the strstreambuf object. */
40 #define ALLOCATED(FP) ((FP)->_f._IO_buf_base && DYNAMIC(FP))
42 /* constant: set when the array object has const elements,
43 so the output sequence cannot be written. */
44 #define CONSTANT(FP) ((FP)->_f._IO_file_flags & _IO_NO_WRITES)
46 /* alsize: the suggested minimum size for a dynamic array object. */
47 #define ALSIZE(FP) ??? /* not stored */
49 /* palloc: points to the function to call to allocate a dynamic array object.*/
51 ((FP)->_s._allocate_buffer == default_alloc ? 0 : (FP)->_s._allocate_buffer)
53 /* pfree: points to the function to call to free a dynamic array object. */
55 ((FP)->_s._free_buffer == default_free ? 0 : (FP)->_s._free_buffer)
60 /* An "unbounded buffer" is when a buffer is supplied, but with no
61 specified length. An example is the buffer argument to sprintf.
66 _IO_str_init_static (sf
, ptr
, size
, pstart
)
72 _IO_FILE
*fp
= &sf
->_sbf
._f
;
78 /* If size is negative 'the characters are assumed to
79 continue indefinitely.' This is kind of messy ... */
82 /* Try increasing powers of 2, as long as we don't wrap around. */
83 for (; s
= 2*size
, s
> 0 && ptr
+ s
> ptr
&& s
< 0x4000000L
; )
85 /* Try increasing size as much as we can without wrapping around. */
86 for (s
= size
>> 1; s
> 0; s
>>= 1)
88 if (ptr
+ size
+ s
> ptr
)
92 _IO_setb (fp
, ptr
, ptr
+ size
, 0);
94 fp
->_IO_write_base
= ptr
;
95 fp
->_IO_read_base
= ptr
;
96 fp
->_IO_read_ptr
= ptr
;
99 fp
->_IO_write_ptr
= pstart
;
100 fp
->_IO_write_end
= ptr
+ size
;
101 fp
->_IO_read_end
= pstart
;
105 fp
->_IO_write_ptr
= ptr
;
106 fp
->_IO_write_end
= ptr
;
107 fp
->_IO_read_end
= ptr
+size
;
109 /* A null _allocate_buffer function flags the strfile as being static. */
110 sf
->_s
._allocate_buffer
= (_IO_alloc_type
) 0;
114 _IO_str_init_readonly (sf
, ptr
, size
)
119 _IO_str_init_static (sf
, (char *) ptr
, size
, NULL
);
120 sf
->_sbf
._f
._IO_file_flags
|= _IO_NO_WRITES
;
124 _IO_str_overflow (fp
, c
)
128 int flush_only
= c
== EOF
;
130 if (fp
->_flags
& _IO_NO_WRITES
)
131 return flush_only
? 0 : EOF
;
132 if ((fp
->_flags
& _IO_TIED_PUT_GET
) && !(fp
->_flags
& _IO_CURRENTLY_PUTTING
))
134 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
135 fp
->_IO_write_ptr
= fp
->_IO_read_ptr
;
136 fp
->_IO_read_ptr
= fp
->_IO_read_end
;
138 pos
= fp
->_IO_write_ptr
- fp
->_IO_write_base
;
139 if (pos
>= (_IO_size_t
) (_IO_blen (fp
) + flush_only
))
141 if (fp
->_flags
& _IO_USER_BUF
) /* not allowed to enlarge */
146 char *old_buf
= fp
->_IO_buf_base
;
147 _IO_size_t new_size
= 2 * _IO_blen (fp
) + 100;
149 = (char *) (*((_IO_strfile
*) fp
)->_s
._allocate_buffer
) (new_size
);
152 /* __ferror(fp) = 1; */
157 memcpy (new_buf
, old_buf
, _IO_blen (fp
));
158 (*((_IO_strfile
*) fp
)->_s
._free_buffer
) (old_buf
);
159 /* Make sure _IO_setb won't try to delete _IO_buf_base. */
160 fp
->_IO_buf_base
= NULL
;
163 if (lenp
== &LEN(fp
)) /* use '\0'-filling */
164 memset (new_buf
+ pos
, 0, blen() - pos
);
166 _IO_setb (fp
, new_buf
, new_buf
+ new_size
, 1);
167 fp
->_IO_read_base
= new_buf
+ (fp
->_IO_read_base
- old_buf
);
168 fp
->_IO_read_ptr
= new_buf
+ (fp
->_IO_read_ptr
- old_buf
);
169 fp
->_IO_read_end
= new_buf
+ (fp
->_IO_read_end
- old_buf
);
170 fp
->_IO_write_ptr
= new_buf
+ (fp
->_IO_write_ptr
- old_buf
);
172 fp
->_IO_write_base
= new_buf
;
173 fp
->_IO_write_end
= fp
->_IO_buf_end
;
178 *fp
->_IO_write_ptr
++ = (unsigned char) c
;
179 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
180 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
185 _IO_str_underflow (fp
)
188 if (fp
->_IO_write_ptr
> fp
->_IO_read_end
)
189 fp
->_IO_read_end
= fp
->_IO_write_ptr
;
190 if ((fp
->_flags
& _IO_TIED_PUT_GET
) && (fp
->_flags
& _IO_CURRENTLY_PUTTING
))
192 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
193 fp
->_IO_read_ptr
= fp
->_IO_write_ptr
;
194 fp
->_IO_write_ptr
= fp
->_IO_write_end
;
196 if (fp
->_IO_read_ptr
< fp
->_IO_read_end
)
197 return *((unsigned char *) fp
->_IO_read_ptr
);
202 /* The size of the valid part of the buffer. */
208 return ((fp
->_IO_write_ptr
> fp
->_IO_read_end
209 ? fp
->_IO_write_ptr
: fp
->_IO_read_end
)
210 - fp
->_IO_read_base
);
214 _IO_str_seekoff (fp
, offset
, dir
, mode
)
222 if (mode
== 0 && (fp
->_flags
& _IO_TIED_PUT_GET
))
223 mode
= (fp
->_flags
& _IO_CURRENTLY_PUTTING
? _IOS_OUTPUT
: _IOS_INPUT
);
227 /* Don't move any pointers. But there is no clear indication what
228 mode FP is in. Let's guess. */
229 if (fp
->_IO_file_flags
& _IO_NO_WRITES
)
230 new_pos
= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
232 new_pos
= fp
->_IO_write_ptr
- fp
->_IO_write_base
;
236 _IO_ssize_t cur_size
= _IO_str_count(fp
);
239 /* Move the get pointer, if requested. */
240 if (mode
& _IOS_INPUT
)
248 offset
+= fp
->_IO_read_ptr
- fp
->_IO_read_base
;
250 default: /* case _IO_seek_set: */
253 if (offset
< 0 || (_IO_ssize_t
) offset
> cur_size
)
255 fp
->_IO_read_ptr
= fp
->_IO_read_base
+ offset
;
256 fp
->_IO_read_end
= fp
->_IO_read_base
+ cur_size
;
260 /* Move the put pointer, if requested. */
261 if (mode
& _IOS_OUTPUT
)
269 offset
+= fp
->_IO_write_ptr
- fp
->_IO_write_base
;
271 default: /* case _IO_seek_set: */
274 if (offset
< 0 || (_IO_ssize_t
) offset
> cur_size
)
276 fp
->_IO_write_ptr
= fp
->_IO_write_base
+ offset
;
284 _IO_str_pbackfail (fp
, c
)
288 if ((fp
->_flags
& _IO_NO_WRITES
) && c
!= EOF
)
290 return _IO_default_pbackfail (fp
, c
);
294 _IO_str_finish (fp
, dummy
)
298 if (fp
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
299 (((_IO_strfile
*) fp
)->_s
._free_buffer
) (fp
->_IO_buf_base
);
300 fp
->_IO_buf_base
= NULL
;
302 _IO_default_finish (fp
, 0);
305 struct _IO_jump_t _IO_str_jumps
=
308 JUMP_INIT(finish
, _IO_str_finish
),
309 JUMP_INIT(overflow
, _IO_str_overflow
),
310 JUMP_INIT(underflow
, _IO_str_underflow
),
311 JUMP_INIT(uflow
, _IO_default_uflow
),
312 JUMP_INIT(pbackfail
, _IO_str_pbackfail
),
313 JUMP_INIT(xsputn
, _IO_default_xsputn
),
314 JUMP_INIT(xsgetn
, _IO_default_xsgetn
),
315 JUMP_INIT(seekoff
, _IO_str_seekoff
),
316 JUMP_INIT(seekpos
, _IO_default_seekpos
),
317 JUMP_INIT(setbuf
, _IO_default_setbuf
),
318 JUMP_INIT(sync
, _IO_default_sync
),
319 JUMP_INIT(doallocate
, _IO_default_doallocate
),
320 JUMP_INIT(read
, _IO_default_read
),
321 JUMP_INIT(write
, _IO_default_write
),
322 JUMP_INIT(seek
, _IO_default_seek
),
323 JUMP_INIT(close
, _IO_default_close
),
324 JUMP_INIT(stat
, _IO_default_stat
),
325 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
326 JUMP_INIT(imbue
, _IO_default_imbue
)