Update.
[glibc.git] / libio / strops.c
blob7c49bd5d2c0a90488396c4121322120b9686c550
1 /* Copyright (C) 1993, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
26 #include "strfile.h"
27 #include "libioP.h"
28 #include <string.h>
30 #if 0
31 /* The following definitions are for exposition only.
32 They map the terminology used in the ANSI/ISO C++ draft standard
33 to the implementation. */
35 /* allocated: set when a dynamic array object has been allocated, and
36 hence should be freed by the destructor for the strstreambuf object. */
37 #define ALLOCATED(FP) ((FP)->_f._IO_buf_base && DYNAMIC(FP))
39 /* constant: set when the array object has const elements,
40 so the output sequence cannot be written. */
41 #define CONSTANT(FP) ((FP)->_f._IO_file_flags & _IO_NO_WRITES)
43 /* alsize: the suggested minimum size for a dynamic array object. */
44 #define ALSIZE(FP) ??? /* not stored */
46 /* palloc: points to the function to call to allocate a dynamic array object.*/
47 #define PALLOC(FP) \
48 ((FP)->_s._allocate_buffer == default_alloc ? 0 : (FP)->_s._allocate_buffer)
50 /* pfree: points to the function to call to free a dynamic array object. */
51 #define PFREE(FP) \
52 ((FP)->_s._free_buffer == default_free ? 0 : (FP)->_s._free_buffer)
54 #endif
56 #ifdef TODO
57 /* An "unbounded buffer" is when a buffer is supplied, but with no
58 specified length. An example is the buffer argument to sprintf.
60 #endif
62 void
63 _IO_str_init_static (sf, ptr, size, pstart)
64 _IO_strfile *sf;
65 char *ptr;
66 int size;
67 char *pstart;
69 _IO_FILE *fp = &sf->_sbf._f;
71 if (size == 0)
72 size = strlen (ptr);
73 else if (size < 0)
75 /* If size is negative 'the characters are assumed to
76 continue indefinitely.' This is kind of messy ... */
77 int s;
78 size = 512;
79 /* Try increasing powers of 2, as long as we don't wrap around. */
80 for (; s = 2*size, s > 0 && ptr + s > ptr && s < 0x4000000L; )
81 size = s;
82 /* Try increasing size as much as we can without wrapping around. */
83 for (s = size >> 1; s > 0; s >>= 1)
85 if (ptr + size + s > ptr)
86 size += s;
89 _IO_setb (fp, ptr, ptr + size, 0);
91 fp->_IO_write_base = ptr;
92 fp->_IO_read_base = ptr;
93 fp->_IO_read_ptr = ptr;
94 if (pstart)
96 fp->_IO_write_ptr = pstart;
97 fp->_IO_write_end = ptr + size;
98 fp->_IO_read_end = pstart;
100 else
102 fp->_IO_write_ptr = ptr;
103 fp->_IO_write_end = ptr;
104 fp->_IO_read_end = ptr+size;
106 /* A null _allocate_buffer function flags the strfile as being static. */
107 sf->_s._allocate_buffer = (_IO_alloc_type) 0;
110 void
111 _IO_str_init_readonly (sf, ptr, size)
112 _IO_strfile *sf;
113 const char *ptr;
114 int size;
116 _IO_str_init_static (sf, (char *) ptr, size, NULL);
117 sf->_sbf._f._IO_file_flags |= _IO_NO_WRITES;
121 _IO_str_overflow (fp, c)
122 _IO_FILE *fp;
123 int c;
125 int flush_only = c == EOF;
126 _IO_size_t pos;
127 if (fp->_flags & _IO_NO_WRITES)
128 return flush_only ? 0 : EOF;
129 if ((fp->_flags & _IO_TIED_PUT_GET) && !(fp->_flags & _IO_CURRENTLY_PUTTING))
131 fp->_flags |= _IO_CURRENTLY_PUTTING;
132 fp->_IO_write_ptr = fp->_IO_read_ptr;
133 fp->_IO_read_ptr = fp->_IO_read_end;
135 pos = fp->_IO_write_ptr - fp->_IO_write_base;
136 if (pos >= (_IO_size_t) (_IO_blen (fp) + flush_only))
138 if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */
139 return EOF;
140 else
142 char *new_buf;
143 char *old_buf = fp->_IO_buf_base;
144 _IO_size_t new_size = 2 * _IO_blen (fp) + 100;
145 new_buf
146 = (char *) (*((_IO_strfile *) fp)->_s._allocate_buffer) (new_size);
147 if (new_buf == NULL)
149 /* __ferror(fp) = 1; */
150 return EOF;
152 if (old_buf)
154 memcpy (new_buf, old_buf, _IO_blen (fp));
155 (*((_IO_strfile *) fp)->_s._free_buffer) (old_buf);
156 /* Make sure _IO_setb won't try to delete _IO_buf_base. */
157 fp->_IO_buf_base = NULL;
159 #if 0
160 if (lenp == &LEN(fp)) /* use '\0'-filling */
161 memset (new_buf + pos, 0, blen() - pos);
162 #endif
163 _IO_setb (fp, new_buf, new_buf + new_size, 1);
164 fp->_IO_read_base = new_buf + (fp->_IO_read_base - old_buf);
165 fp->_IO_read_ptr = new_buf + (fp->_IO_read_ptr - old_buf);
166 fp->_IO_read_end = new_buf + (fp->_IO_read_end - old_buf);
167 fp->_IO_write_ptr = new_buf + (fp->_IO_write_ptr - old_buf);
169 fp->_IO_write_base = new_buf;
170 fp->_IO_write_end = fp->_IO_buf_end;
174 if (!flush_only)
175 *fp->_IO_write_ptr++ = (unsigned char) c;
176 if (fp->_IO_write_ptr > fp->_IO_read_end)
177 fp->_IO_read_end = fp->_IO_write_ptr;
178 return c;
182 _IO_str_underflow (fp)
183 _IO_FILE *fp;
185 if (fp->_IO_write_ptr > fp->_IO_read_end)
186 fp->_IO_read_end = fp->_IO_write_ptr;
187 if ((fp->_flags & _IO_TIED_PUT_GET) && (fp->_flags & _IO_CURRENTLY_PUTTING))
189 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
190 fp->_IO_read_ptr = fp->_IO_write_ptr;
191 fp->_IO_write_ptr = fp->_IO_write_end;
193 if (fp->_IO_read_ptr < fp->_IO_read_end)
194 return *((unsigned char *) fp->_IO_read_ptr);
195 else
196 return EOF;
199 /* The size of the valid part of the buffer. */
201 _IO_ssize_t
202 _IO_str_count (fp)
203 _IO_FILE *fp;
205 return ((fp->_IO_write_ptr > fp->_IO_read_end
206 ? fp->_IO_write_ptr : fp->_IO_read_end)
207 - fp->_IO_read_base);
210 _IO_off64_t
211 _IO_str_seekoff (fp, offset, dir, mode)
212 _IO_FILE *fp;
213 _IO_off64_t offset;
214 int dir;
215 int mode;
217 _IO_off64_t new_pos;
219 if (mode == 0 && (fp->_flags & _IO_TIED_PUT_GET))
220 mode = (fp->_flags & _IO_CURRENTLY_PUTTING ? _IOS_OUTPUT : _IOS_INPUT);
222 if (mode == 0)
224 /* Don't move any pointers. But there is no clear indication what
225 mode FP is in. Let's guess. */
226 if (fp->_IO_file_flags & _IO_NO_WRITES)
227 new_pos = fp->_IO_read_ptr - fp->_IO_read_base;
228 else
229 new_pos = fp->_IO_write_ptr - fp->_IO_write_base;
231 else
233 _IO_ssize_t cur_size = _IO_str_count(fp);
234 new_pos = EOF;
236 /* Move the get pointer, if requested. */
237 if (mode & _IOS_INPUT)
239 switch (dir)
241 case _IO_seek_end:
242 offset += cur_size;
243 break;
244 case _IO_seek_cur:
245 offset += fp->_IO_read_ptr - fp->_IO_read_base;
246 break;
247 default: /* case _IO_seek_set: */
248 break;
250 if (offset < 0 || (_IO_ssize_t) offset > cur_size)
251 return EOF;
252 fp->_IO_read_ptr = fp->_IO_read_base + offset;
253 fp->_IO_read_end = fp->_IO_read_base + cur_size;
254 new_pos = offset;
257 /* Move the put pointer, if requested. */
258 if (mode & _IOS_OUTPUT)
260 switch (dir)
262 case _IO_seek_end:
263 offset += cur_size;
264 break;
265 case _IO_seek_cur:
266 offset += fp->_IO_write_ptr - fp->_IO_write_base;
267 break;
268 default: /* case _IO_seek_set: */
269 break;
271 if (offset < 0 || (_IO_ssize_t) offset > cur_size)
272 return EOF;
273 fp->_IO_write_ptr = fp->_IO_write_base + offset;
274 new_pos = offset;
277 return new_pos;
281 _IO_str_pbackfail (fp, c)
282 _IO_FILE *fp;
283 int c;
285 if ((fp->_flags & _IO_NO_WRITES) && c != EOF)
286 return EOF;
287 return _IO_default_pbackfail (fp, c);
290 void
291 _IO_str_finish (fp, dummy)
292 _IO_FILE *fp;
293 int dummy;
295 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
296 (((_IO_strfile *) fp)->_s._free_buffer) (fp->_IO_buf_base);
297 fp->_IO_buf_base = NULL;
299 _IO_default_finish (fp, 0);
302 struct _IO_jump_t _IO_str_jumps =
304 JUMP_INIT_DUMMY,
305 JUMP_INIT(finish, _IO_str_finish),
306 JUMP_INIT(overflow, _IO_str_overflow),
307 JUMP_INIT(underflow, _IO_str_underflow),
308 JUMP_INIT(uflow, _IO_default_uflow),
309 JUMP_INIT(pbackfail, _IO_str_pbackfail),
310 JUMP_INIT(xsputn, _IO_default_xsputn),
311 JUMP_INIT(xsgetn, _IO_default_xsgetn),
312 JUMP_INIT(seekoff, _IO_str_seekoff),
313 JUMP_INIT(seekpos, _IO_default_seekpos),
314 JUMP_INIT(setbuf, _IO_default_setbuf),
315 JUMP_INIT(sync, _IO_default_sync),
316 JUMP_INIT(doallocate, _IO_default_doallocate),
317 JUMP_INIT(read, _IO_default_read),
318 JUMP_INIT(write, _IO_default_write),
319 JUMP_INIT(seek, _IO_default_seek),
320 JUMP_INIT(close, _IO_default_close),
321 JUMP_INIT(stat, _IO_default_stat),
322 JUMP_INIT(showmanyc, _IO_default_showmanyc),
323 JUMP_INIT(imbue, _IO_default_imbue)