* libio/wstrops.c: Remove dead macro definitions and comments.
[glibc.git] / libio / strops.c
blobda60e9319f15e05d6a722f017c96d9b1f0f4dc60
1 /* Copyright (C) 1993, 1997-2003, 2004, 2006 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
17 02111-1307 USA.
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. */
28 #include "strfile.h"
29 #include "libioP.h"
30 #include <string.h>
31 #include <stdio_ext.h>
33 void
34 _IO_str_init_static_internal (sf, ptr, size, pstart)
35 _IO_strfile *sf;
36 char *ptr;
37 _IO_size_t size;
38 char *pstart;
40 _IO_FILE *fp = &sf->_sbf._f;
41 char *end;
43 if (size == 0)
44 end = __rawmemchr (ptr, '\0');
45 else if ((_IO_size_t) ptr + size > (_IO_size_t) ptr)
46 end = ptr + size;
47 else
48 end = (char *) -1;
49 INTUSE(_IO_setb) (fp, ptr, end, 0);
51 fp->_IO_write_base = ptr;
52 fp->_IO_read_base = ptr;
53 fp->_IO_read_ptr = ptr;
54 if (pstart)
56 fp->_IO_write_ptr = pstart;
57 fp->_IO_write_end = end;
58 fp->_IO_read_end = pstart;
60 else
62 fp->_IO_write_ptr = ptr;
63 fp->_IO_write_end = ptr;
64 fp->_IO_read_end = end;
66 /* A null _allocate_buffer function flags the strfile as being static. */
67 sf->_s._allocate_buffer = (_IO_alloc_type) 0;
70 void
71 _IO_str_init_static (sf, ptr, size, pstart)
72 _IO_strfile *sf;
73 char *ptr;
74 int size;
75 char *pstart;
77 return _IO_str_init_static_internal (sf, ptr, size < 0 ? -1 : size, pstart);
80 void
81 _IO_str_init_readonly (sf, ptr, size)
82 _IO_strfile *sf;
83 const char *ptr;
84 int size;
86 _IO_str_init_static_internal (sf, (char *) ptr, size < 0 ? -1 : size, NULL);
87 sf->_sbf._f._IO_file_flags |= _IO_NO_WRITES;
90 int
91 _IO_str_overflow (fp, c)
92 _IO_FILE *fp;
93 int c;
95 int flush_only = c == EOF;
96 _IO_size_t pos;
97 if (fp->_flags & _IO_NO_WRITES)
98 return flush_only ? 0 : EOF;
99 if ((fp->_flags & _IO_TIED_PUT_GET) && !(fp->_flags & _IO_CURRENTLY_PUTTING))
101 fp->_flags |= _IO_CURRENTLY_PUTTING;
102 fp->_IO_write_ptr = fp->_IO_read_ptr;
103 fp->_IO_read_ptr = fp->_IO_read_end;
105 pos = fp->_IO_write_ptr - fp->_IO_write_base;
106 if (pos >= (_IO_size_t) (_IO_blen (fp) + flush_only))
108 if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */
109 return EOF;
110 else
112 char *new_buf;
113 char *old_buf = fp->_IO_buf_base;
114 _IO_size_t new_size = 2 * _IO_blen (fp) + 100;
115 new_buf
116 = (char *) (*((_IO_strfile *) fp)->_s._allocate_buffer) (new_size);
117 if (new_buf == NULL)
119 /* __ferror(fp) = 1; */
120 return EOF;
122 if (old_buf)
124 memcpy (new_buf, old_buf, _IO_blen (fp));
125 (*((_IO_strfile *) fp)->_s._free_buffer) (old_buf);
126 /* Make sure _IO_setb won't try to delete _IO_buf_base. */
127 fp->_IO_buf_base = NULL;
129 #if 0
130 if (lenp == &LEN(fp)) /* use '\0'-filling */
131 memset (new_buf + pos, 0, blen() - pos);
132 #endif
133 INTUSE(_IO_setb) (fp, new_buf, new_buf + new_size, 1);
134 fp->_IO_read_base = new_buf + (fp->_IO_read_base - old_buf);
135 fp->_IO_read_ptr = new_buf + (fp->_IO_read_ptr - old_buf);
136 fp->_IO_read_end = new_buf + (fp->_IO_read_end - old_buf);
137 fp->_IO_write_ptr = new_buf + (fp->_IO_write_ptr - old_buf);
139 fp->_IO_write_base = new_buf;
140 fp->_IO_write_end = fp->_IO_buf_end;
144 if (!flush_only)
145 *fp->_IO_write_ptr++ = (unsigned char) c;
146 if (fp->_IO_write_ptr > fp->_IO_read_end)
147 fp->_IO_read_end = fp->_IO_write_ptr;
148 return c;
150 INTDEF(_IO_str_overflow)
153 _IO_str_underflow (fp)
154 _IO_FILE *fp;
156 if (fp->_IO_write_ptr > fp->_IO_read_end)
157 fp->_IO_read_end = fp->_IO_write_ptr;
158 if ((fp->_flags & _IO_TIED_PUT_GET) && (fp->_flags & _IO_CURRENTLY_PUTTING))
160 fp->_flags &= ~_IO_CURRENTLY_PUTTING;
161 fp->_IO_read_ptr = fp->_IO_write_ptr;
162 fp->_IO_write_ptr = fp->_IO_write_end;
164 if (fp->_IO_read_ptr < fp->_IO_read_end)
165 return *((unsigned char *) fp->_IO_read_ptr);
166 else
167 return EOF;
169 INTDEF(_IO_str_underflow)
171 /* The size of the valid part of the buffer. */
173 _IO_ssize_t
174 _IO_str_count (fp)
175 _IO_FILE *fp;
177 return ((fp->_IO_write_ptr > fp->_IO_read_end
178 ? fp->_IO_write_ptr : fp->_IO_read_end)
179 - fp->_IO_read_base);
182 _IO_off64_t
183 _IO_str_seekoff (fp, offset, dir, mode)
184 _IO_FILE *fp;
185 _IO_off64_t offset;
186 int dir;
187 int mode;
189 _IO_off64_t new_pos;
191 if (mode == 0 && (fp->_flags & _IO_TIED_PUT_GET))
192 mode = (fp->_flags & _IO_CURRENTLY_PUTTING ? _IOS_OUTPUT : _IOS_INPUT);
194 if (mode == 0)
196 /* Don't move any pointers. But there is no clear indication what
197 mode FP is in. Let's guess. */
198 if (fp->_IO_file_flags & _IO_NO_WRITES)
199 new_pos = fp->_IO_read_ptr - fp->_IO_read_base;
200 else
201 new_pos = fp->_IO_write_ptr - fp->_IO_write_base;
203 else
205 _IO_ssize_t cur_size = _IO_str_count(fp);
206 new_pos = EOF;
208 /* Move the get pointer, if requested. */
209 if (mode & _IOS_INPUT)
211 switch (dir)
213 case _IO_seek_end:
214 offset += cur_size;
215 break;
216 case _IO_seek_cur:
217 offset += fp->_IO_read_ptr - fp->_IO_read_base;
218 break;
219 default: /* case _IO_seek_set: */
220 break;
222 if (offset < 0 || (_IO_ssize_t) offset > cur_size)
223 return EOF;
224 fp->_IO_read_ptr = fp->_IO_read_base + offset;
225 fp->_IO_read_end = fp->_IO_read_base + cur_size;
226 new_pos = offset;
229 /* Move the put pointer, if requested. */
230 if (mode & _IOS_OUTPUT)
232 switch (dir)
234 case _IO_seek_end:
235 offset += cur_size;
236 break;
237 case _IO_seek_cur:
238 offset += fp->_IO_write_ptr - fp->_IO_write_base;
239 break;
240 default: /* case _IO_seek_set: */
241 break;
243 if (offset < 0 || (_IO_ssize_t) offset > cur_size)
244 return EOF;
245 fp->_IO_write_ptr = fp->_IO_write_base + offset;
246 new_pos = offset;
249 return new_pos;
251 INTDEF(_IO_str_seekoff)
254 _IO_str_pbackfail (fp, c)
255 _IO_FILE *fp;
256 int c;
258 if ((fp->_flags & _IO_NO_WRITES) && c != EOF)
259 return EOF;
260 return INTUSE(_IO_default_pbackfail) (fp, c);
262 INTDEF(_IO_str_pbackfail)
264 void
265 _IO_str_finish (fp, dummy)
266 _IO_FILE *fp;
267 int dummy;
269 if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
270 (((_IO_strfile *) fp)->_s._free_buffer) (fp->_IO_buf_base);
271 fp->_IO_buf_base = NULL;
273 INTUSE(_IO_default_finish) (fp, 0);
276 const struct _IO_jump_t _IO_str_jumps =
278 JUMP_INIT_DUMMY,
279 JUMP_INIT(finish, _IO_str_finish),
280 JUMP_INIT(overflow, INTUSE(_IO_str_overflow)),
281 JUMP_INIT(underflow, INTUSE(_IO_str_underflow)),
282 JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
283 JUMP_INIT(pbackfail, INTUSE(_IO_str_pbackfail)),
284 JUMP_INIT(xsputn, INTUSE(_IO_default_xsputn)),
285 JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
286 JUMP_INIT(seekoff, INTUSE(_IO_str_seekoff)),
287 JUMP_INIT(seekpos, _IO_default_seekpos),
288 JUMP_INIT(setbuf, _IO_default_setbuf),
289 JUMP_INIT(sync, _IO_default_sync),
290 JUMP_INIT(doallocate, INTUSE(_IO_default_doallocate)),
291 JUMP_INIT(read, _IO_default_read),
292 JUMP_INIT(write, _IO_default_write),
293 JUMP_INIT(seek, _IO_default_seek),
294 JUMP_INIT(close, _IO_default_close),
295 JUMP_INIT(stat, _IO_default_stat),
296 JUMP_INIT(showmanyc, _IO_default_showmanyc),
297 JUMP_INIT(imbue, _IO_default_imbue)