]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // File based streams -*- C++ -*- |
2 | ||
fba10f59 | 3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
bc2631e0 | 4 | // 2007, 2008, 2009 |
e6705174 | 5 | // Free Software Foundation, Inc. |
725dc051 BK |
6 | // |
7 | // This file is part of the GNU ISO C++ Library. This library is free | |
8 | // software; you can redistribute it and/or modify it under the | |
9 | // terms of the GNU General Public License as published by the | |
748086b7 | 10 | // Free Software Foundation; either version 3, or (at your option) |
725dc051 BK |
11 | // any later version. |
12 | ||
13 | // This library is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
748086b7 JJ |
18 | // Under Section 7 of GPL version 3, you are granted additional |
19 | // permissions described in the GCC Runtime Library Exception, version | |
20 | // 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | // You should have received a copy of the GNU General Public License and | |
23 | // a copy of the GCC Runtime Library Exception along with this program; | |
24 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | // <http://www.gnu.org/licenses/>. | |
725dc051 | 26 | |
0aa06b18 BK |
27 | /** @file fstream.tcc |
28 | * This is an internal header file, included by other library headers. | |
29 | * You should not attempt to use it directly. | |
30 | */ | |
31 | ||
725dc051 BK |
32 | // |
33 | // ISO C++ 14882: 27.8 File-based streams | |
34 | // | |
35 | ||
3d7c150e BK |
36 | #ifndef _FSTREAM_TCC |
37 | #define _FSTREAM_TCC 1 | |
725dc051 | 38 | |
3b794528 BK |
39 | #pragma GCC system_header |
40 | ||
fba10f59 | 41 | #include <cxxabi-forced.h> |
d05f74f1 | 42 | |
3cbc7af0 BK |
43 | _GLIBCXX_BEGIN_NAMESPACE(std) |
44 | ||
725dc051 BK |
45 | template<typename _CharT, typename _Traits> |
46 | void | |
47 | basic_filebuf<_CharT, _Traits>:: | |
990101f9 | 48 | _M_allocate_internal_buffer() |
725dc051 | 49 | { |
dfad48c6 PC |
50 | // Allocate internal buffer only if one doesn't already exist |
51 | // (either allocated or provided by the user via setbuf). | |
a8e3a00f | 52 | if (!_M_buf_allocated && !_M_buf) |
13187a45 | 53 | { |
a8e3a00f | 54 | _M_buf = new char_type[_M_buf_size]; |
5cdd50a5 | 55 | _M_buf_allocated = true; |
990101f9 BK |
56 | } |
57 | } | |
58 | ||
990101f9 BK |
59 | template<typename _CharT, typename _Traits> |
60 | void | |
61 | basic_filebuf<_CharT, _Traits>:: | |
a1796d12 | 62 | _M_destroy_internal_buffer() throw() |
990101f9 BK |
63 | { |
64 | if (_M_buf_allocated) | |
65 | { | |
a8e3a00f BK |
66 | delete [] _M_buf; |
67 | _M_buf = NULL; | |
990101f9 | 68 | _M_buf_allocated = false; |
990101f9 | 69 | } |
f1813b69 PR |
70 | delete [] _M_ext_buf; |
71 | _M_ext_buf = NULL; | |
72 | _M_ext_buf_size = 0; | |
73 | _M_ext_next = NULL; | |
74 | _M_ext_end = NULL; | |
725dc051 BK |
75 | } |
76 | ||
77 | template<typename _CharT, typename _Traits> | |
78 | basic_filebuf<_CharT, _Traits>:: | |
26c691a8 | 79 | basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock), |
5e93f39f PR |
80 | _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(), |
81 | _M_state_last(), _M_buf(NULL), _M_buf_size(BUFSIZ), | |
26c691a8 | 82 | _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(), |
5e93f39f PR |
83 | _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false), |
84 | _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0), | |
85 | _M_ext_end(0) | |
ed6814f7 | 86 | { |
46c4e5d6 | 87 | if (has_facet<__codecvt_type>(this->_M_buf_locale)) |
0cd1de6f BK |
88 | _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale); |
89 | } | |
725dc051 | 90 | |
725dc051 | 91 | template<typename _CharT, typename _Traits> |
ed6814f7 | 92 | typename basic_filebuf<_CharT, _Traits>::__filebuf_type* |
725dc051 BK |
93 | basic_filebuf<_CharT, _Traits>:: |
94 | open(const char* __s, ios_base::openmode __mode) | |
95 | { | |
96 | __filebuf_type *__ret = NULL; | |
97 | if (!this->is_open()) | |
98 | { | |
d3a193e3 | 99 | _M_file.open(__s, __mode); |
13187a45 | 100 | if (this->is_open()) |
725dc051 | 101 | { |
990101f9 | 102 | _M_allocate_internal_buffer(); |
a8e3a00f | 103 | _M_mode = __mode; |
6eeb7d7a | 104 | |
71b46021 PC |
105 | // Setup initial buffer to 'uncommitted' mode. |
106 | _M_reading = false; | |
107 | _M_writing = false; | |
108 | _M_set_buffer(-1); | |
6e52332e | 109 | |
5e93f39f PR |
110 | // Reset to initial state. |
111 | _M_state_last = _M_state_cur = _M_state_beg; | |
112 | ||
cc5112c9 | 113 | // 27.8.1.3,4 |
ed6814f7 BI |
114 | if ((__mode & ios_base::ate) |
115 | && this->seekoff(0, ios_base::end, __mode) | |
4c4809c1 | 116 | == pos_type(off_type(-1))) |
cc9d1c78 PC |
117 | this->close(); |
118 | else | |
119 | __ret = this; | |
725dc051 BK |
120 | } |
121 | } | |
122 | return __ret; | |
123 | } | |
124 | ||
125 | template<typename _CharT, typename _Traits> | |
ed6814f7 | 126 | typename basic_filebuf<_CharT, _Traits>::__filebuf_type* |
725dc051 | 127 | basic_filebuf<_CharT, _Traits>:: |
d05f74f1 | 128 | close() |
725dc051 | 129 | { |
d05f74f1 JM |
130 | if (!this->is_open()) |
131 | return NULL; | |
132 | ||
133 | bool __testfail = false; | |
134 | { | |
135 | // NB: Do this here so that re-opened filebufs will be cool... | |
136 | struct __close_sentry | |
725dc051 | 137 | { |
d05f74f1 JM |
138 | basic_filebuf *__fb; |
139 | __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { } | |
140 | ~__close_sentry () | |
141 | { | |
142 | __fb->_M_mode = ios_base::openmode(0); | |
143 | __fb->_M_pback_init = false; | |
144 | __fb->_M_destroy_internal_buffer(); | |
145 | __fb->_M_reading = false; | |
146 | __fb->_M_writing = false; | |
147 | __fb->_M_set_buffer(-1); | |
148 | __fb->_M_state_last = __fb->_M_state_cur = __fb->_M_state_beg; | |
149 | } | |
150 | } __cs (this); | |
151 | ||
bc2631e0 | 152 | __try |
d05f74f1 JM |
153 | { |
154 | if (!_M_terminate_output()) | |
155 | __testfail = true; | |
156 | } | |
bc2631e0 | 157 | __catch(__cxxabiv1::__forced_unwind&) |
d05f74f1 JM |
158 | { |
159 | _M_file.close(); | |
160 | __throw_exception_again; | |
161 | } | |
bc2631e0 | 162 | __catch(...) |
d05f74f1 JM |
163 | { __testfail = true; } |
164 | } | |
165 | ||
166 | if (!_M_file.close()) | |
167 | __testfail = true; | |
168 | ||
169 | if (__testfail) | |
170 | return NULL; | |
171 | else | |
172 | return this; | |
725dc051 BK |
173 | } |
174 | ||
175 | template<typename _CharT, typename _Traits> | |
ed6814f7 | 176 | streamsize |
725dc051 BK |
177 | basic_filebuf<_CharT, _Traits>:: |
178 | showmanyc() | |
179 | { | |
180 | streamsize __ret = -1; | |
a8e3a00f | 181 | const bool __testin = _M_mode & ios_base::in; |
9fbcb61a | 182 | if (__testin && this->is_open()) |
bbacb998 | 183 | { |
bbacb998 PC |
184 | // For a stateful encoding (-1) the pending sequence might be just |
185 | // shift and unshift prefixes with no actual character. | |
71b46021 | 186 | __ret = this->egptr() - this->gptr(); |
1ae48358 DS |
187 | |
188 | #if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM | |
189 | // About this workaround, see libstdc++/20806. | |
190 | const bool __testbinary = _M_mode & ios_base::binary; | |
191 | if (__check_facet(_M_codecvt).encoding() >= 0 | |
192 | && __testbinary) | |
193 | #else | |
0cd1de6f | 194 | if (__check_facet(_M_codecvt).encoding() >= 0) |
1ae48358 | 195 | #endif |
0cd1de6f | 196 | __ret += _M_file.showmanyc() / _M_codecvt->max_length(); |
bbacb998 | 197 | } |
725dc051 BK |
198 | return __ret; |
199 | } | |
09625c16 | 200 | |
51ff8149 | 201 | template<typename _CharT, typename _Traits> |
ed6814f7 | 202 | typename basic_filebuf<_CharT, _Traits>::int_type |
6e81c6f4 | 203 | basic_filebuf<_CharT, _Traits>:: |
cd16e04b | 204 | underflow() |
51ff8149 BK |
205 | { |
206 | int_type __ret = traits_type::eof(); | |
a8e3a00f | 207 | const bool __testin = _M_mode & ios_base::in; |
71b46021 | 208 | if (__testin && !_M_writing) |
51ff8149 | 209 | { |
ee5ca789 | 210 | // Check for pback madness, and if so switch back to the |
51ff8149 BK |
211 | // normal buffers and jet outta here before expensive |
212 | // fileops happen... | |
0aef8de2 | 213 | _M_destroy_pback(); |
51ff8149 | 214 | |
71b46021 | 215 | if (this->gptr() < this->egptr()) |
cd16e04b | 216 | return traits_type::to_int_type(*this->gptr()); |
51ff8149 | 217 | |
f10eea7b | 218 | // Get and convert input sequence. |
fdeeef7f | 219 | const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1; |
ed6814f7 | 220 | |
f1813b69 PR |
221 | // Will be set to true if ::read() returns 0 indicating EOF. |
222 | bool __got_eof = false; | |
223 | // Number of internal characters produced. | |
f10eea7b | 224 | streamsize __ilen = 0; |
ed6814f7 | 225 | codecvt_base::result __r = codecvt_base::ok; |
f10eea7b PC |
226 | if (__check_facet(_M_codecvt).always_noconv()) |
227 | { | |
ed6814f7 | 228 | __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()), |
cc5112c9 | 229 | __buflen); |
f1813b69 PR |
230 | if (__ilen == 0) |
231 | __got_eof = true; | |
f10eea7b PC |
232 | } |
233 | else | |
234 | { | |
f1813b69 | 235 | // Worst-case number of external bytes. |
dfad48c6 PC |
236 | // XXX Not done encoding() == -1. |
237 | const int __enc = _M_codecvt->encoding(); | |
f1813b69 PR |
238 | streamsize __blen; // Minimum buffer size. |
239 | streamsize __rlen; // Number of chars to read. | |
240 | if (__enc > 0) | |
241 | __blen = __rlen = __buflen * __enc; | |
242 | else | |
51ff8149 | 243 | { |
f1813b69 PR |
244 | __blen = __buflen + _M_codecvt->max_length() - 1; |
245 | __rlen = __buflen; | |
51ff8149 | 246 | } |
f1813b69 PR |
247 | const streamsize __remainder = _M_ext_end - _M_ext_next; |
248 | __rlen = __rlen > __remainder ? __rlen - __remainder : 0; | |
09625c16 PC |
249 | |
250 | // An imbue in 'read' mode implies first converting the external | |
251 | // chars already present. | |
252 | if (_M_reading && this->egptr() == this->eback() && __remainder) | |
253 | __rlen = 0; | |
ed6814f7 | 254 | |
f1813b69 PR |
255 | // Allocate buffer if necessary and move unconverted |
256 | // bytes to front. | |
257 | if (_M_ext_buf_size < __blen) | |
258 | { | |
259 | char* __buf = new char[__blen]; | |
09625c16 | 260 | if (__remainder) |
538075fe | 261 | __builtin_memcpy(__buf, _M_ext_next, __remainder); |
f1813b69 PR |
262 | |
263 | delete [] _M_ext_buf; | |
264 | _M_ext_buf = __buf; | |
265 | _M_ext_buf_size = __blen; | |
266 | } | |
09625c16 | 267 | else if (__remainder) |
538075fe | 268 | __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder); |
f1813b69 PR |
269 | |
270 | _M_ext_next = _M_ext_buf; | |
271 | _M_ext_end = _M_ext_buf + __remainder; | |
5e93f39f | 272 | _M_state_last = _M_state_cur; |
f1813b69 PR |
273 | |
274 | do | |
51ff8149 | 275 | { |
f1813b69 PR |
276 | if (__rlen > 0) |
277 | { | |
278 | // Sanity check! | |
279 | // This may fail if the return value of | |
280 | // codecvt::max_length() is bogus. | |
281 | if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size) | |
5e93f39f | 282 | { |
ba9119ec | 283 | __throw_ios_failure(__N("basic_filebuf::underflow " |
d78e147a | 284 | "codecvt::max_length() " |
ba9119ec | 285 | "is not valid")); |
5e93f39f | 286 | } |
f1813b69 PR |
287 | streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen); |
288 | if (__elen == 0) | |
289 | __got_eof = true; | |
3090572c PC |
290 | else if (__elen == -1) |
291 | break; | |
f1813b69 PR |
292 | _M_ext_end += __elen; |
293 | } | |
5536b61c | 294 | |
7558d810 PC |
295 | char_type* __iend = this->eback(); |
296 | if (_M_ext_next < _M_ext_end) | |
297 | __r = _M_codecvt->in(_M_state_cur, _M_ext_next, | |
298 | _M_ext_end, _M_ext_next, | |
299 | this->eback(), | |
300 | this->eback() + __buflen, __iend); | |
29342463 | 301 | if (__r == codecvt_base::noconv) |
f1813b69 PR |
302 | { |
303 | size_t __avail = _M_ext_end - _M_ext_buf; | |
304 | __ilen = std::min(__avail, __buflen); | |
305 | traits_type::copy(this->eback(), | |
7558d810 PC |
306 | reinterpret_cast<char_type*> |
307 | (_M_ext_buf), __ilen); | |
f1813b69 PR |
308 | _M_ext_next = _M_ext_buf + __ilen; |
309 | } | |
29342463 PC |
310 | else |
311 | __ilen = __iend - this->eback(); | |
5536b61c | 312 | |
29342463 PC |
313 | // _M_codecvt->in may return error while __ilen > 0: this is |
314 | // ok, and actually occurs in case of mixed encodings (e.g., | |
315 | // XML files). | |
316 | if (__r == codecvt_base::error) | |
317 | break; | |
318 | ||
f1813b69 | 319 | __rlen = 1; |
51ff8149 | 320 | } |
4da2b960 | 321 | while (__ilen == 0 && !__got_eof); |
46c4e5d6 | 322 | } |
f10eea7b PC |
323 | |
324 | if (__ilen > 0) | |
46c4e5d6 | 325 | { |
f10eea7b | 326 | _M_set_buffer(__ilen); |
71b46021 PC |
327 | _M_reading = true; |
328 | __ret = traits_type::to_int_type(*this->gptr()); | |
2cfe4e68 | 329 | } |
f1813b69 | 330 | else if (__got_eof) |
2cfe4e68 PC |
331 | { |
332 | // If the actual end of file is reached, set 'uncommitted' | |
ed6814f7 | 333 | // mode, thus allowing an immediate write without an |
2cfe4e68 PC |
334 | // intervening seek. |
335 | _M_set_buffer(-1); | |
336 | _M_reading = false; | |
5536b61c PC |
337 | // However, reaching it while looping on partial means that |
338 | // the file has got an incomplete character. | |
339 | if (__r == codecvt_base::partial) | |
ba9119ec PC |
340 | __throw_ios_failure(__N("basic_filebuf::underflow " |
341 | "incomplete character in file")); | |
2cfe4e68 | 342 | } |
3090572c | 343 | else if (__r == codecvt_base::error) |
ba9119ec PC |
344 | __throw_ios_failure(__N("basic_filebuf::underflow " |
345 | "invalid byte sequence in file")); | |
3090572c | 346 | else |
ba9119ec | 347 | __throw_ios_failure(__N("basic_filebuf::underflow " |
ed6814f7 | 348 | "error reading the file")); |
51ff8149 | 349 | } |
51ff8149 BK |
350 | return __ret; |
351 | } | |
352 | ||
725dc051 | 353 | template<typename _CharT, typename _Traits> |
ed6814f7 | 354 | typename basic_filebuf<_CharT, _Traits>::int_type |
725dc051 BK |
355 | basic_filebuf<_CharT, _Traits>:: |
356 | pbackfail(int_type __i) | |
357 | { | |
358 | int_type __ret = traits_type::eof(); | |
a8e3a00f | 359 | const bool __testin = _M_mode & ios_base::in; |
71b46021 | 360 | if (__testin && !_M_writing) |
725dc051 | 361 | { |
b166bded PC |
362 | // Remember whether the pback buffer is active, otherwise below |
363 | // we may try to store in it a second char (libstdc++/9761). | |
a8e3a00f | 364 | const bool __testpb = _M_pback_init; |
0b176c1a | 365 | const bool __testeof = traits_type::eq_int_type(__i, __ret); |
b166bded | 366 | int_type __tmp; |
71b46021 | 367 | if (this->eback() < this->gptr()) |
725dc051 | 368 | { |
71b46021 PC |
369 | this->gbump(-1); |
370 | __tmp = traits_type::to_int_type(*this->gptr()); | |
b166bded | 371 | } |
4c4809c1 | 372 | else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1))) |
f24ce7c1 BK |
373 | { |
374 | __tmp = this->underflow(); | |
375 | if (traits_type::eq_int_type(__tmp, __ret)) | |
376 | return __ret; | |
377 | } | |
378 | else | |
46c4e5d6 BK |
379 | { |
380 | // At the beginning of the buffer, need to make a | |
381 | // putback position available. But the seek may fail | |
382 | // (f.i., at the beginning of a file, see | |
383 | // libstdc++/9439) and in that case we return | |
384 | // traits_type::eof(). | |
385 | return __ret; | |
386 | } | |
725dc051 | 387 | |
b166bded PC |
388 | // Try to put back __i into input sequence in one of three ways. |
389 | // Order these tests done in is unspecified by the standard. | |
390 | if (!__testeof && traits_type::eq_int_type(__i, __tmp)) | |
391 | __ret = __i; | |
392 | else if (__testeof) | |
393 | __ret = traits_type::not_eof(__i); | |
394 | else if (!__testpb) | |
395 | { | |
396 | _M_create_pback(); | |
71b46021 | 397 | _M_reading = true; |
ed6814f7 | 398 | *this->gptr() = traits_type::to_char_type(__i); |
b166bded | 399 | __ret = __i; |
725dc051 | 400 | } |
725dc051 | 401 | } |
725dc051 BK |
402 | return __ret; |
403 | } | |
404 | ||
405 | template<typename _CharT, typename _Traits> | |
ed6814f7 | 406 | typename basic_filebuf<_CharT, _Traits>::int_type |
725dc051 BK |
407 | basic_filebuf<_CharT, _Traits>:: |
408 | overflow(int_type __c) | |
409 | { | |
410 | int_type __ret = traits_type::eof(); | |
002bd606 | 411 | const bool __testeof = traits_type::eq_int_type(__c, __ret); |
a8e3a00f | 412 | const bool __testout = _M_mode & ios_base::out; |
71b46021 | 413 | if (__testout && !_M_reading) |
725dc051 | 414 | { |
71b46021 | 415 | if (this->pbase() < this->pptr()) |
002bd606 | 416 | { |
002bd606 BK |
417 | // If appropriate, append the overflow char. |
418 | if (!__testeof) | |
71b46021 PC |
419 | { |
420 | *this->pptr() = traits_type::to_char_type(__c); | |
421 | this->pbump(1); | |
422 | } | |
ed6814f7 | 423 | |
002bd606 | 424 | // Convert pending sequence to external representation, |
42134429 | 425 | // and output. |
71b46021 | 426 | if (_M_convert_to_external(this->pbase(), |
ee19761d | 427 | this->pptr() - this->pbase())) |
002bd606 BK |
428 | { |
429 | _M_set_buffer(0); | |
430 | __ret = traits_type::not_eof(__c); | |
431 | } | |
432 | } | |
a8e3a00f | 433 | else if (_M_buf_size > 1) |
71b46021 PC |
434 | { |
435 | // Overflow in 'uncommitted' mode: set _M_writing, set | |
436 | // the buffer to the initial 'write' mode, and put __c | |
437 | // into the buffer. | |
438 | _M_set_buffer(0); | |
439 | _M_writing = true; | |
440 | if (!__testeof) | |
441 | { | |
442 | *this->pptr() = traits_type::to_char_type(__c); | |
443 | this->pbump(1); | |
444 | } | |
445 | __ret = traits_type::not_eof(__c); | |
446 | } | |
002bd606 | 447 | else |
725dc051 | 448 | { |
002bd606 BK |
449 | // Unbuffered. |
450 | char_type __conv = traits_type::to_char_type(__c); | |
9335d80a | 451 | if (__testeof || _M_convert_to_external(&__conv, 1)) |
ed6814f7 | 452 | { |
71b46021 PC |
453 | _M_writing = true; |
454 | __ret = traits_type::not_eof(__c); | |
455 | } | |
725dc051 | 456 | } |
725dc051 | 457 | } |
725dc051 BK |
458 | return __ret; |
459 | } | |
ed6814f7 | 460 | |
07814743 | 461 | template<typename _CharT, typename _Traits> |
6e81c6f4 | 462 | bool |
07814743 | 463 | basic_filebuf<_CharT, _Traits>:: |
6e81c6f4 | 464 | _M_convert_to_external(_CharT* __ibuf, streamsize __ilen) |
07814743 | 465 | { |
6e81c6f4 | 466 | // Sizes of external and pending output. |
ac3cadf0 PC |
467 | streamsize __elen; |
468 | streamsize __plen; | |
c5b6351b | 469 | if (__check_facet(_M_codecvt).always_noconv()) |
07814743 | 470 | { |
ac3cadf0 PC |
471 | __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen); |
472 | __plen = __ilen; | |
07814743 BK |
473 | } |
474 | else | |
475 | { | |
476 | // Worst-case number of external bytes needed. | |
3461133d PC |
477 | // XXX Not done encoding() == -1. |
478 | streamsize __blen = __ilen * _M_codecvt->max_length(); | |
07814743 | 479 | char* __buf = static_cast<char*>(__builtin_alloca(__blen)); |
3461133d | 480 | |
07814743 BK |
481 | char* __bend; |
482 | const char_type* __iend; | |
a1796d12 | 483 | codecvt_base::result __r; |
0cd1de6f BK |
484 | __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen, |
485 | __iend, __buf, __buf + __blen, __bend); | |
ed6814f7 | 486 | |
130cd3e1 | 487 | if (__r == codecvt_base::ok || __r == codecvt_base::partial) |
07814743 | 488 | __blen = __bend - __buf; |
130cd3e1 PC |
489 | else if (__r == codecvt_base::noconv) |
490 | { | |
a1796d12 | 491 | // Same as the always_noconv case above. |
130cd3e1 PC |
492 | __buf = reinterpret_cast<char*>(__ibuf); |
493 | __blen = __ilen; | |
494 | } | |
a1796d12 | 495 | else |
ac3cadf0 PC |
496 | __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external " |
497 | "conversion error")); | |
498 | ||
499 | __elen = _M_file.xsputn(__buf, __blen); | |
500 | __plen = __blen; | |
ed6814f7 | 501 | |
07814743 | 502 | // Try once more for partial conversions. |
ac3cadf0 | 503 | if (__r == codecvt_base::partial && __elen == __plen) |
07814743 BK |
504 | { |
505 | const char_type* __iresume = __iend; | |
71b46021 | 506 | streamsize __rlen = this->pptr() - __iend; |
0cd1de6f | 507 | __r = _M_codecvt->out(_M_state_cur, __iresume, |
ed6814f7 | 508 | __iresume + __rlen, __iend, __buf, |
0cd1de6f | 509 | __buf + __blen, __bend); |
07814743 | 510 | if (__r != codecvt_base::error) |
07814743 | 511 | { |
a1796d12 | 512 | __rlen = __bend - __buf; |
ac3cadf0 PC |
513 | __elen = _M_file.xsputn(__buf, __rlen); |
514 | __plen = __rlen; | |
07814743 | 515 | } |
ac3cadf0 PC |
516 | else |
517 | __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external " | |
518 | "conversion error")); | |
07814743 BK |
519 | } |
520 | } | |
ac3cadf0 | 521 | return __elen == __plen; |
725dc051 BK |
522 | } |
523 | ||
c56e3d82 PC |
524 | template<typename _CharT, typename _Traits> |
525 | streamsize | |
526 | basic_filebuf<_CharT, _Traits>:: | |
527 | xsgetn(_CharT* __s, streamsize __n) | |
528 | { | |
529 | // Clear out pback buffer before going on to the real deal... | |
530 | streamsize __ret = 0; | |
a8e3a00f | 531 | if (_M_pback_init) |
c56e3d82 PC |
532 | { |
533 | if (__n > 0 && this->gptr() == this->eback()) | |
534 | { | |
535 | *__s++ = *this->gptr(); | |
536 | this->gbump(1); | |
537 | __ret = 1; | |
538 | --__n; | |
539 | } | |
540 | _M_destroy_pback(); | |
541 | } | |
542 | ||
543 | // Optimization in the always_noconv() case, to be generalized in the | |
544 | // future: when __n > __buflen we read directly instead of using the | |
545 | // buffer repeatedly. | |
a8e3a00f | 546 | const bool __testin = _M_mode & ios_base::in; |
1ae48358 DS |
547 | const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1; |
548 | ||
c56e3d82 PC |
549 | if (__n > __buflen && __check_facet(_M_codecvt).always_noconv() |
550 | && __testin && !_M_writing) | |
551 | { | |
552 | // First, copy the chars already present in the buffer. | |
553 | const streamsize __avail = this->egptr() - this->gptr(); | |
d1768069 NM |
554 | if (__avail != 0) |
555 | { | |
556 | if (__avail == 1) | |
557 | *__s = *this->gptr(); | |
c93af6f2 | 558 | else |
d1768069 NM |
559 | traits_type::copy(__s, this->gptr(), __avail); |
560 | __s += __avail; | |
561 | this->gbump(__avail); | |
562 | __ret += __avail; | |
563 | __n -= __avail; | |
564 | } | |
c56e3d82 | 565 | |
544f9440 PC |
566 | // Need to loop in case of short reads (relatively common |
567 | // with pipes). | |
568 | streamsize __len; | |
569 | for (;;) | |
570 | { | |
571 | __len = _M_file.xsgetn(reinterpret_cast<char*>(__s), | |
572 | __n); | |
573 | if (__len == -1) | |
574 | __throw_ios_failure(__N("basic_filebuf::xsgetn " | |
575 | "error reading the file")); | |
576 | if (__len == 0) | |
577 | break; | |
578 | ||
579 | __n -= __len; | |
580 | __ret += __len; | |
581 | if (__n == 0) | |
582 | break; | |
583 | ||
584 | __s += __len; | |
585 | } | |
586 | ||
587 | if (__n == 0) | |
c56e3d82 PC |
588 | { |
589 | _M_set_buffer(0); | |
590 | _M_reading = true; | |
591 | } | |
592 | else if (__len == 0) | |
593 | { | |
594 | // If end of file is reached, set 'uncommitted' | |
595 | // mode, thus allowing an immediate write without | |
596 | // an intervening seek. | |
597 | _M_set_buffer(-1); | |
598 | _M_reading = false; | |
599 | } | |
600 | } | |
601 | else | |
602 | __ret += __streambuf_type::xsgetn(__s, __n); | |
603 | ||
604 | return __ret; | |
605 | } | |
606 | ||
bda243ec PC |
607 | template<typename _CharT, typename _Traits> |
608 | streamsize | |
609 | basic_filebuf<_CharT, _Traits>:: | |
610 | xsputn(const _CharT* __s, streamsize __n) | |
ed6814f7 | 611 | { |
bda243ec PC |
612 | // Optimization in the always_noconv() case, to be generalized in the |
613 | // future: when __n is sufficiently large we write directly instead of | |
614 | // using the buffer. | |
42134429 | 615 | streamsize __ret = 0; |
a8e3a00f | 616 | const bool __testout = _M_mode & ios_base::out; |
c56e3d82 PC |
617 | if (__check_facet(_M_codecvt).always_noconv() |
618 | && __testout && !_M_reading) | |
bda243ec PC |
619 | { |
620 | // Measurement would reveal the best choice. | |
621 | const streamsize __chunk = 1ul << 10; | |
622 | streamsize __bufavail = this->epptr() - this->pptr(); | |
623 | ||
624 | // Don't mistake 'uncommitted' mode buffered with unbuffered. | |
a8e3a00f BK |
625 | if (!_M_writing && _M_buf_size > 1) |
626 | __bufavail = _M_buf_size - 1; | |
bda243ec PC |
627 | |
628 | const streamsize __limit = std::min(__chunk, __bufavail); | |
629 | if (__n >= __limit) | |
630 | { | |
631 | const streamsize __buffill = this->pptr() - this->pbase(); | |
632 | const char* __buf = reinterpret_cast<const char*>(this->pbase()); | |
633 | __ret = _M_file.xsputn_2(__buf, __buffill, | |
ed6814f7 | 634 | reinterpret_cast<const char*>(__s), |
5e93f39f | 635 | __n); |
bda243ec PC |
636 | if (__ret == __buffill + __n) |
637 | { | |
638 | _M_set_buffer(0); | |
639 | _M_writing = true; | |
640 | } | |
641 | if (__ret > __buffill) | |
642 | __ret -= __buffill; | |
643 | else | |
644 | __ret = 0; | |
645 | } | |
646 | else | |
647 | __ret = __streambuf_type::xsputn(__s, __n); | |
648 | } | |
649 | else | |
ed6814f7 | 650 | __ret = __streambuf_type::xsputn(__s, __n); |
bda243ec PC |
651 | return __ret; |
652 | } | |
653 | ||
990101f9 | 654 | template<typename _CharT, typename _Traits> |
ed6814f7 | 655 | typename basic_filebuf<_CharT, _Traits>::__streambuf_type* |
990101f9 BK |
656 | basic_filebuf<_CharT, _Traits>:: |
657 | setbuf(char_type* __s, streamsize __n) | |
658 | { | |
dfad48c6 | 659 | if (!this->is_open()) |
d9cdfe6a BK |
660 | { |
661 | if (__s == 0 && __n == 0) | |
662 | _M_buf_size = 1; | |
663 | else if (__s && __n > 0) | |
664 | { | |
665 | // This is implementation-defined behavior, and assumes that | |
666 | // an external char_type array of length __n exists and has | |
667 | // been pre-allocated. If this is not the case, things will | |
668 | // quickly blow up. When __n > 1, __n - 1 positions will be | |
669 | // used for the get area, __n - 1 for the put area and 1 | |
670 | // position to host the overflow char of a full put area. | |
671 | // When __n == 1, 1 position will be used for the get area | |
672 | // and 0 for the put area, as in the unbuffered case above. | |
673 | _M_buf = __s; | |
674 | _M_buf_size = __n; | |
675 | } | |
676 | } | |
ed6814f7 | 677 | return this; |
990101f9 | 678 | } |
ed6814f7 | 679 | |
8dcaff28 | 680 | |
1a139c59 PR |
681 | // According to 27.8.1.4 p11 - 13, seekoff should ignore the last |
682 | // argument (of type openmode). | |
725dc051 | 683 | template<typename _CharT, typename _Traits> |
31bfa177 | 684 | typename basic_filebuf<_CharT, _Traits>::pos_type |
725dc051 | 685 | basic_filebuf<_CharT, _Traits>:: |
8dcaff28 | 686 | seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) |
725dc051 | 687 | { |
a1796d12 | 688 | int __width = 0; |
0cd1de6f | 689 | if (_M_codecvt) |
b417ae14 | 690 | __width = _M_codecvt->encoding(); |
725dc051 BK |
691 | if (__width < 0) |
692 | __width = 0; | |
a1796d12 | 693 | |
ed6814f7 | 694 | pos_type __ret = pos_type(off_type(-1)); |
5e93f39f | 695 | const bool __testfail = __off != 0 && __width <= 0; |
ed6814f7 | 696 | if (this->is_open() && !__testfail) |
725dc051 BK |
697 | { |
698 | // Ditch any pback buffers to avoid confusion. | |
aa438e8f | 699 | _M_destroy_pback(); |
725dc051 | 700 | |
5e93f39f PR |
701 | // Correct state at destination. Note that this is the correct |
702 | // state for the current position during output, because | |
703 | // codecvt::unshift() returns the state to the initial state. | |
704 | // This is also the correct state at the end of the file because | |
705 | // an unshift sequence should have been written at the end. | |
706 | __state_type __state = _M_state_beg; | |
8c8dec01 | 707 | off_type __computed_off = __off * __width; |
1a139c59 | 708 | if (_M_reading && __way == ios_base::cur) |
8c8dec01 PR |
709 | { |
710 | if (_M_codecvt->always_noconv()) | |
711 | __computed_off += this->gptr() - this->egptr(); | |
712 | else | |
713 | { | |
714 | // Calculate offset from _M_ext_buf that corresponds | |
5e93f39f PR |
715 | // to gptr(). Note: uses _M_state_last, which |
716 | // corresponds to eback(). | |
8c8dec01 | 717 | const int __gptr_off = |
5e93f39f | 718 | _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next, |
8c8dec01 PR |
719 | this->gptr() - this->eback()); |
720 | __computed_off += _M_ext_buf + __gptr_off - _M_ext_end; | |
ed6814f7 | 721 | |
5e93f39f | 722 | // _M_state_last is modified by codecvt::length() so |
ed6814f7 BI |
723 | // it now corresponds to gptr(). |
724 | __state = _M_state_last; | |
8c8dec01 PR |
725 | } |
726 | } | |
5e93f39f | 727 | __ret = _M_seek(__computed_off, __way, __state); |
725dc051 | 728 | } |
725dc051 BK |
729 | return __ret; |
730 | } | |
731 | ||
1a139c59 PR |
732 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
733 | // 171. Strange seekpos() semantics due to joint position | |
734 | // According to the resolution of DR 171, seekpos should ignore the last | |
735 | // argument (of type openmode). | |
725dc051 | 736 | template<typename _CharT, typename _Traits> |
31bfa177 | 737 | typename basic_filebuf<_CharT, _Traits>::pos_type |
725dc051 | 738 | basic_filebuf<_CharT, _Traits>:: |
1a139c59 | 739 | seekpos(pos_type __pos, ios_base::openmode) |
725dc051 | 740 | { |
ed6814f7 BI |
741 | pos_type __ret = pos_type(off_type(-1)); |
742 | if (this->is_open()) | |
1a139c59 PR |
743 | { |
744 | // Ditch any pback buffers to avoid confusion. | |
745 | _M_destroy_pback(); | |
5e93f39f | 746 | __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state()); |
1a139c59 | 747 | } |
1a139c59 PR |
748 | return __ret; |
749 | } | |
750 | ||
751 | template<typename _CharT, typename _Traits> | |
752 | typename basic_filebuf<_CharT, _Traits>::pos_type | |
753 | basic_filebuf<_CharT, _Traits>:: | |
5e93f39f PR |
754 | _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state) |
755 | { | |
756 | pos_type __ret = pos_type(off_type(-1)); | |
757 | if (_M_terminate_output()) | |
ed6814f7 | 758 | { |
5e93f39f | 759 | // Returns pos_type(off_type(-1)) in case of failure. |
ed6814f7 | 760 | __ret = pos_type(_M_file.seekoff(__off, __way)); |
d4d21a01 PC |
761 | if (__ret != pos_type(off_type(-1))) |
762 | { | |
763 | _M_reading = false; | |
764 | _M_writing = false; | |
765 | _M_ext_next = _M_ext_end = _M_ext_buf; | |
766 | _M_set_buffer(-1); | |
767 | _M_state_cur = __state; | |
768 | __ret.state(_M_state_cur); | |
769 | } | |
5e93f39f PR |
770 | } |
771 | return __ret; | |
772 | } | |
773 | ||
774 | template<typename _CharT, typename _Traits> | |
775 | bool | |
776 | basic_filebuf<_CharT, _Traits>:: | |
777 | _M_terminate_output() | |
1a139c59 | 778 | { |
5e93f39f | 779 | // Part one: update the output sequence. |
42134429 | 780 | bool __testvalid = true; |
1a139c59 PR |
781 | if (this->pbase() < this->pptr()) |
782 | { | |
5e93f39f PR |
783 | const int_type __tmp = this->overflow(); |
784 | if (traits_type::eq_int_type(__tmp, traits_type::eof())) | |
785 | __testvalid = false; | |
786 | } | |
ed6814f7 | 787 | |
5e93f39f | 788 | // Part two: output unshift sequence. |
ed6814f7 | 789 | if (_M_writing && !__check_facet(_M_codecvt).always_noconv() |
5e93f39f PR |
790 | && __testvalid) |
791 | { | |
792 | // Note: this value is arbitrary, since there is no way to | |
793 | // get the length of the unshift sequence from codecvt, | |
794 | // without calling unshift. | |
795 | const size_t __blen = 128; | |
5e93f39f PR |
796 | char __buf[__blen]; |
797 | codecvt_base::result __r; | |
798 | streamsize __ilen = 0; | |
799 | ||
800 | do | |
801 | { | |
802 | char* __next; | |
803 | __r = _M_codecvt->unshift(_M_state_cur, __buf, | |
804 | __buf + __blen, __next); | |
805 | if (__r == codecvt_base::error) | |
806 | __testvalid = false; | |
807 | else if (__r == codecvt_base::ok || | |
808 | __r == codecvt_base::partial) | |
809 | { | |
ed6814f7 | 810 | __ilen = __next - __buf; |
5e93f39f PR |
811 | if (__ilen > 0) |
812 | { | |
813 | const streamsize __elen = _M_file.xsputn(__buf, __ilen); | |
814 | if (__elen != __ilen) | |
815 | __testvalid = false; | |
816 | } | |
817 | } | |
818 | } | |
819 | while (__r == codecvt_base::partial && __ilen > 0 && __testvalid); | |
820 | ||
821 | if (__testvalid) | |
822 | { | |
823 | // This second call to overflow() is required by the standard, | |
824 | // but it's not clear why it's needed, since the output buffer | |
825 | // should be empty by this point (it should have been emptied | |
826 | // in the first call to overflow()). | |
827 | const int_type __tmp = this->overflow(); | |
828 | if (traits_type::eq_int_type(__tmp, traits_type::eof())) | |
829 | __testvalid = false; | |
830 | } | |
1a139c59 | 831 | } |
5e93f39f | 832 | return __testvalid; |
725dc051 BK |
833 | } |
834 | ||
835 | template<typename _CharT, typename _Traits> | |
5e93f39f | 836 | int |
725dc051 | 837 | basic_filebuf<_CharT, _Traits>:: |
5e93f39f PR |
838 | sync() |
839 | { | |
5e93f39f PR |
840 | // Make sure that the internal buffer resyncs its idea of |
841 | // the file position with the external file. | |
42134429 | 842 | int __ret = 0; |
5e93f39f PR |
843 | if (this->pbase() < this->pptr()) |
844 | { | |
845 | const int_type __tmp = this->overflow(); | |
846 | if (traits_type::eq_int_type(__tmp, traits_type::eof())) | |
847 | __ret = -1; | |
ed6814f7 | 848 | } |
5e93f39f PR |
849 | return __ret; |
850 | } | |
725dc051 BK |
851 | |
852 | template<typename _CharT, typename _Traits> | |
853 | void | |
854 | basic_filebuf<_CharT, _Traits>:: | |
855 | imbue(const locale& __loc) | |
856 | { | |
09625c16 PC |
857 | bool __testvalid = true; |
858 | ||
859 | const __codecvt_type* _M_codecvt_tmp = 0; | |
ed6814f7 BI |
860 | if (__builtin_expect(has_facet<__codecvt_type>(__loc), true)) |
861 | _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc); | |
4c4809c1 | 862 | |
09625c16 | 863 | if (this->is_open()) |
c03d83d4 | 864 | { |
09625c16 PC |
865 | // encoding() == -1 is ok only at the beginning. |
866 | if ((_M_reading || _M_writing) | |
867 | && __check_facet(_M_codecvt).encoding() == -1) | |
868 | __testvalid = false; | |
c03d83d4 | 869 | else |
09625c16 PC |
870 | { |
871 | if (_M_reading) | |
872 | { | |
873 | if (__check_facet(_M_codecvt).always_noconv()) | |
874 | { | |
875 | if (_M_codecvt_tmp | |
876 | && !__check_facet(_M_codecvt_tmp).always_noconv()) | |
a8e3a00f | 877 | __testvalid = this->seekoff(0, ios_base::cur, _M_mode) |
09625c16 PC |
878 | != pos_type(off_type(-1)); |
879 | } | |
880 | else | |
881 | { | |
882 | // External position corresponding to gptr(). | |
ed6814f7 | 883 | _M_ext_next = _M_ext_buf |
09625c16 PC |
884 | + _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next, |
885 | this->gptr() - this->eback()); | |
886 | const streamsize __remainder = _M_ext_end - _M_ext_next; | |
887 | if (__remainder) | |
538075fe | 888 | __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder); |
09625c16 PC |
889 | |
890 | _M_ext_next = _M_ext_buf; | |
891 | _M_ext_end = _M_ext_buf + __remainder; | |
892 | _M_set_buffer(-1); | |
893 | _M_state_last = _M_state_cur = _M_state_beg; | |
894 | } | |
895 | } | |
896 | else if (_M_writing && (__testvalid = _M_terminate_output())) | |
897 | _M_set_buffer(-1); | |
898 | } | |
0cd1de6f | 899 | } |
09625c16 PC |
900 | |
901 | if (__testvalid) | |
902 | _M_codecvt = _M_codecvt_tmp; | |
2f228199 PC |
903 | else |
904 | _M_codecvt = 0; | |
725dc051 | 905 | } |
a32e3c09 BK |
906 | |
907 | // Inhibit implicit instantiations for required instantiations, | |
ed6814f7 | 908 | // which are defined via explicit instantiations elsewhere. |
a32e3c09 | 909 | // NB: This syntax is a GNU extension. |
3d7c150e | 910 | #if _GLIBCXX_EXTERN_TEMPLATE |
a32e3c09 | 911 | extern template class basic_filebuf<char>; |
a32e3c09 | 912 | extern template class basic_ifstream<char>; |
a32e3c09 | 913 | extern template class basic_ofstream<char>; |
a32e3c09 | 914 | extern template class basic_fstream<char>; |
5112ae3a | 915 | |
3d7c150e | 916 | #ifdef _GLIBCXX_USE_WCHAR_T |
5112ae3a BK |
917 | extern template class basic_filebuf<wchar_t>; |
918 | extern template class basic_ifstream<wchar_t>; | |
919 | extern template class basic_ofstream<wchar_t>; | |
a32e3c09 | 920 | extern template class basic_fstream<wchar_t>; |
5112ae3a | 921 | #endif |
1bc8b0ad | 922 | #endif |
3cbc7af0 BK |
923 | |
924 | _GLIBCXX_END_NAMESPACE | |
725dc051 | 925 | |
ed6814f7 | 926 | #endif |