1 /* Copyright (C) 1993,1997-1999,2001-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
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. */
32 #include <stdio_ext.h>
35 _IO_wstr_init_static (fp
, ptr
, size
, pstart
)
44 end
= ptr
+ __wcslen (ptr
);
45 else if ((_IO_size_t
) ptr
+ size
* sizeof (wchar_t) > (_IO_size_t
) ptr
)
48 /* Even for misaligned ptr make sure there is integral number of wide
50 end
= ptr
+ (-1 - (_IO_size_t
) ptr
) / sizeof (wchar_t);
51 INTUSE(_IO_wsetb
) (fp
, ptr
, end
, 0);
53 fp
->_wide_data
->_IO_write_base
= ptr
;
54 fp
->_wide_data
->_IO_read_base
= ptr
;
55 fp
->_wide_data
->_IO_read_ptr
= ptr
;
58 fp
->_wide_data
->_IO_write_ptr
= pstart
;
59 fp
->_wide_data
->_IO_write_end
= end
;
60 fp
->_wide_data
->_IO_read_end
= pstart
;
64 fp
->_wide_data
->_IO_write_ptr
= ptr
;
65 fp
->_wide_data
->_IO_write_end
= ptr
;
66 fp
->_wide_data
->_IO_read_end
= end
;
68 /* A null _allocate_buffer function flags the strfile as being static. */
69 (((_IO_strfile
*) fp
)->_s
._allocate_buffer
) = (_IO_alloc_type
)0;
73 _IO_wstr_overflow (fp
, c
)
77 int flush_only
= c
== WEOF
;
79 if (fp
->_flags
& _IO_NO_WRITES
)
80 return flush_only
? 0 : WEOF
;
81 if ((fp
->_flags
& _IO_TIED_PUT_GET
) && !(fp
->_flags
& _IO_CURRENTLY_PUTTING
))
83 fp
->_flags
|= _IO_CURRENTLY_PUTTING
;
84 fp
->_wide_data
->_IO_write_ptr
= fp
->_wide_data
->_IO_read_ptr
;
85 fp
->_wide_data
->_IO_read_ptr
= fp
->_wide_data
->_IO_read_end
;
87 pos
= fp
->_wide_data
->_IO_write_ptr
- fp
->_wide_data
->_IO_write_base
;
88 if (pos
>= (_IO_size_t
) (_IO_wblen (fp
) + flush_only
))
90 if (fp
->_flags
& _IO_USER_BUF
) /* not allowed to enlarge */
95 wchar_t *old_buf
= fp
->_wide_data
->_IO_buf_base
;
96 _IO_size_t new_size
= 2 * _IO_wblen (fp
) + 100;
98 = (wchar_t *) (*((_IO_strfile
*) fp
)->_s
._allocate_buffer
) (new_size
102 /* __ferror(fp) = 1; */
107 __wmemcpy (new_buf
, old_buf
, _IO_wblen (fp
));
108 (*((_IO_strfile
*) fp
)->_s
._free_buffer
) (old_buf
);
109 /* Make sure _IO_setb won't try to delete _IO_buf_base. */
110 fp
->_wide_data
->_IO_buf_base
= NULL
;
112 INTUSE(_IO_wsetb
) (fp
, new_buf
, new_buf
+ new_size
, 1);
113 fp
->_wide_data
->_IO_read_base
=
114 new_buf
+ (fp
->_wide_data
->_IO_read_base
- old_buf
);
115 fp
->_wide_data
->_IO_read_ptr
=
116 new_buf
+ (fp
->_wide_data
->_IO_read_ptr
- old_buf
);
117 fp
->_wide_data
->_IO_read_end
=
118 new_buf
+ (fp
->_wide_data
->_IO_read_end
- old_buf
);
119 fp
->_wide_data
->_IO_write_ptr
=
120 new_buf
+ (fp
->_wide_data
->_IO_write_ptr
- old_buf
);
122 fp
->_wide_data
->_IO_write_base
= new_buf
;
123 fp
->_wide_data
->_IO_write_end
= fp
->_wide_data
->_IO_buf_end
;
128 *fp
->_wide_data
->_IO_write_ptr
++ = c
;
129 if (fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_read_end
)
130 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_write_ptr
;
135 _IO_wstr_underflow (fp
)
138 if (fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_read_end
)
139 fp
->_wide_data
->_IO_read_end
= fp
->_wide_data
->_IO_write_ptr
;
140 if ((fp
->_flags
& _IO_TIED_PUT_GET
) && (fp
->_flags
& _IO_CURRENTLY_PUTTING
))
142 fp
->_flags
&= ~_IO_CURRENTLY_PUTTING
;
143 fp
->_wide_data
->_IO_read_ptr
= fp
->_wide_data
->_IO_write_ptr
;
144 fp
->_wide_data
->_IO_write_ptr
= fp
->_wide_data
->_IO_write_end
;
146 if (fp
->_wide_data
->_IO_read_ptr
< fp
->_wide_data
->_IO_read_end
)
147 return *fp
->_wide_data
->_IO_read_ptr
;
152 /* The size of the valid part of the buffer. */
158 return ((fp
->_wide_data
->_IO_write_ptr
> fp
->_wide_data
->_IO_read_end
159 ? fp
->_wide_data
->_IO_write_ptr
: fp
->_wide_data
->_IO_read_end
)
160 - fp
->_wide_data
->_IO_read_base
);
164 _IO_wstr_seekoff (fp
, offset
, dir
, mode
)
172 if (mode
== 0 && (fp
->_flags
& _IO_TIED_PUT_GET
))
173 mode
= (fp
->_flags
& _IO_CURRENTLY_PUTTING
? _IOS_OUTPUT
: _IOS_INPUT
);
177 /* Don't move any pointers. But there is no clear indication what
178 mode FP is in. Let's guess. */
179 if (fp
->_IO_file_flags
& _IO_NO_WRITES
)
180 new_pos
= fp
->_wide_data
->_IO_read_ptr
- fp
->_wide_data
->_IO_read_base
;
182 new_pos
= (fp
->_wide_data
->_IO_write_ptr
183 - fp
->_wide_data
->_IO_write_base
);
187 _IO_ssize_t cur_size
= _IO_wstr_count (fp
);
190 /* Move the get pointer, if requested. */
191 if (mode
& _IOS_INPUT
)
199 offset
+= (fp
->_wide_data
->_IO_read_ptr
200 - fp
->_wide_data
->_IO_read_base
);
202 default: /* case _IO_seek_set: */
205 if (offset
< 0 || (_IO_ssize_t
) offset
> cur_size
)
207 fp
->_wide_data
->_IO_read_ptr
= (fp
->_wide_data
->_IO_read_base
209 fp
->_wide_data
->_IO_read_end
= (fp
->_wide_data
->_IO_read_base
214 /* Move the put pointer, if requested. */
215 if (mode
& _IOS_OUTPUT
)
223 offset
+= (fp
->_wide_data
->_IO_write_ptr
224 - fp
->_wide_data
->_IO_write_base
);
226 default: /* case _IO_seek_set: */
229 if (offset
< 0 || (_IO_ssize_t
) offset
> cur_size
)
231 fp
->_wide_data
->_IO_write_ptr
= (fp
->_wide_data
->_IO_write_base
240 _IO_wstr_pbackfail (fp
, c
)
244 if ((fp
->_flags
& _IO_NO_WRITES
) && c
!= WEOF
)
246 return INTUSE(_IO_wdefault_pbackfail
) (fp
, c
);
250 _IO_wstr_finish (fp
, dummy
)
254 if (fp
->_wide_data
->_IO_buf_base
&& !(fp
->_flags
& _IO_USER_BUF
))
255 (((_IO_strfile
*) fp
)->_s
._free_buffer
) (fp
->_wide_data
->_IO_buf_base
);
256 fp
->_wide_data
->_IO_buf_base
= NULL
;
258 INTUSE(_IO_wdefault_finish
) (fp
, 0);
261 const struct _IO_jump_t _IO_wstr_jumps
=
264 JUMP_INIT(finish
, _IO_wstr_finish
),
265 JUMP_INIT(overflow
, (_IO_overflow_t
) _IO_wstr_overflow
),
266 JUMP_INIT(underflow
, (_IO_underflow_t
) _IO_wstr_underflow
),
267 JUMP_INIT(uflow
, (_IO_underflow_t
) INTUSE(_IO_wdefault_uflow
)),
268 JUMP_INIT(pbackfail
, (_IO_pbackfail_t
) _IO_wstr_pbackfail
),
269 JUMP_INIT(xsputn
, INTUSE(_IO_wdefault_xsputn
)),
270 JUMP_INIT(xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
271 JUMP_INIT(seekoff
, _IO_wstr_seekoff
),
272 JUMP_INIT(seekpos
, _IO_default_seekpos
),
273 JUMP_INIT(setbuf
, _IO_default_setbuf
),
274 JUMP_INIT(sync
, _IO_default_sync
),
275 JUMP_INIT(doallocate
, INTUSE(_IO_wdefault_doallocate
)),
276 JUMP_INIT(read
, _IO_default_read
),
277 JUMP_INIT(write
, _IO_default_write
),
278 JUMP_INIT(seek
, _IO_default_seek
),
279 JUMP_INIT(close
, _IO_default_close
),
280 JUMP_INIT(stat
, _IO_default_stat
),
281 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
282 JUMP_INIT(imbue
, _IO_default_imbue
)