libstdc++
fstream.tcc
Go to the documentation of this file.
1// File based streams -*- C++ -*-
2
3// Copyright (C) 1997-2021 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/fstream.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{fstream}
28 */
29
30//
31// ISO C++ 14882: 27.8 File-based streams
32//
33
34#ifndef _FSTREAM_TCC
35#define _FSTREAM_TCC 1
36
37#pragma GCC system_header
38
39#include <bits/cxxabi_forced.h>
40#include <bits/move.h> // for swap
41#include <cerrno>
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 template<typename _CharT, typename _Traits>
48 void
51 {
52 // Allocate internal buffer only if one doesn't already exist
53 // (either allocated or provided by the user via setbuf).
54 if (!_M_buf_allocated && !_M_buf)
55 {
56 _M_buf = new char_type[_M_buf_size];
57 _M_buf_allocated = true;
58 }
59 }
60
61 template<typename _CharT, typename _Traits>
62 void
65 {
66 if (_M_buf_allocated)
67 {
68 delete [] _M_buf;
69 _M_buf = 0;
70 _M_buf_allocated = false;
71 }
72 delete [] _M_ext_buf;
73 _M_ext_buf = 0;
74 _M_ext_buf_size = 0;
75 _M_ext_next = 0;
76 _M_ext_end = 0;
77 }
78
79 template<typename _CharT, typename _Traits>
81 basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock),
82 _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
83 _M_state_last(), _M_buf(0), _M_buf_size(_GLIBCXX_BUFSIZ),
84 _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(),
86 _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
87 _M_ext_end(0)
88 {
90 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
91 }
92
93#if __cplusplus >= 201103L
94 template<typename _CharT, typename _Traits>
97 : __streambuf_type(__rhs),
98 _M_lock(), _M_file(std::move(__rhs._M_file), &_M_lock),
99 _M_mode(std::__exchange(__rhs._M_mode, ios_base::openmode(0))),
100 _M_state_beg(std::move(__rhs._M_state_beg)),
101 _M_state_cur(std::move(__rhs._M_state_cur)),
102 _M_state_last(std::move(__rhs._M_state_last)),
103 _M_buf(std::__exchange(__rhs._M_buf, nullptr)),
104 _M_buf_size(std::__exchange(__rhs._M_buf_size, 1)),
105 _M_buf_allocated(std::__exchange(__rhs._M_buf_allocated, false)),
106 _M_reading(std::__exchange(__rhs._M_reading, false)),
107 _M_writing(std::__exchange(__rhs._M_writing, false)),
108 _M_pback(__rhs._M_pback),
109 _M_pback_cur_save(std::__exchange(__rhs._M_pback_cur_save, nullptr)),
110 _M_pback_end_save(std::__exchange(__rhs._M_pback_end_save, nullptr)),
111 _M_pback_init(std::__exchange(__rhs._M_pback_init, false)),
112 _M_codecvt(__rhs._M_codecvt),
113 _M_ext_buf(std::__exchange(__rhs._M_ext_buf, nullptr)),
114 _M_ext_buf_size(std::__exchange(__rhs._M_ext_buf_size, 0)),
115 _M_ext_next(std::__exchange(__rhs._M_ext_next, nullptr)),
116 _M_ext_end(std::__exchange(__rhs._M_ext_end, nullptr))
117 {
118 __rhs._M_set_buffer(-1);
119 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
120 }
121
122 template<typename _CharT, typename _Traits>
123 basic_filebuf<_CharT, _Traits>&
124 basic_filebuf<_CharT, _Traits>::
125 operator=(basic_filebuf&& __rhs)
126 {
127 this->close();
128 __streambuf_type::operator=(__rhs);
129 _M_file.swap(__rhs._M_file);
130 _M_mode = std::__exchange(__rhs._M_mode, ios_base::openmode(0));
131 _M_state_beg = std::move(__rhs._M_state_beg);
132 _M_state_cur = std::move(__rhs._M_state_cur);
133 _M_state_last = std::move(__rhs._M_state_last);
134 _M_buf = std::__exchange(__rhs._M_buf, nullptr);
135 _M_buf_size = std::__exchange(__rhs._M_buf_size, 1);
136 _M_buf_allocated = std::__exchange(__rhs._M_buf_allocated, false);
137 _M_ext_buf = std::__exchange(__rhs._M_ext_buf, nullptr);
138 _M_ext_buf_size = std::__exchange(__rhs._M_ext_buf_size, 0);
139 _M_ext_next = std::__exchange(__rhs._M_ext_next, nullptr);
140 _M_ext_end = std::__exchange(__rhs._M_ext_end, nullptr);
141 _M_reading = std::__exchange(__rhs._M_reading, false);
142 _M_writing = std::__exchange(__rhs._M_writing, false);
143 _M_pback_cur_save = std::__exchange(__rhs._M_pback_cur_save, nullptr);
144 _M_pback_end_save = std::__exchange(__rhs._M_pback_end_save, nullptr);
145 _M_pback_init = std::__exchange(__rhs._M_pback_init, false);
146 __rhs._M_set_buffer(-1);
147 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
148 return *this;
149 }
150
151 template<typename _CharT, typename _Traits>
152 void
154 swap(basic_filebuf& __rhs)
155 {
156 __streambuf_type::swap(__rhs);
157 _M_file.swap(__rhs._M_file);
158 std::swap(_M_mode, __rhs._M_mode);
159 std::swap(_M_state_beg, __rhs._M_state_beg);
160 std::swap(_M_state_cur, __rhs._M_state_cur);
161 std::swap(_M_state_last, __rhs._M_state_last);
162 std::swap(_M_buf, __rhs._M_buf);
163 std::swap(_M_buf_size, __rhs._M_buf_size);
164 std::swap(_M_buf_allocated, __rhs._M_buf_allocated);
165 std::swap(_M_ext_buf, __rhs._M_ext_buf);
166 std::swap(_M_ext_buf_size, __rhs._M_ext_buf_size);
167 std::swap(_M_ext_next, __rhs._M_ext_next);
168 std::swap(_M_ext_end, __rhs._M_ext_end);
169 std::swap(_M_reading, __rhs._M_reading);
170 std::swap(_M_writing, __rhs._M_writing);
171 std::swap(_M_pback_cur_save, __rhs._M_pback_cur_save);
172 std::swap(_M_pback_end_save, __rhs._M_pback_end_save);
173 std::swap(_M_pback_init, __rhs._M_pback_init);
174 }
175#endif
176
177 template<typename _CharT, typename _Traits>
178 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
180 open(const char* __s, ios_base::openmode __mode)
181 {
182 __filebuf_type *__ret = 0;
183 if (!this->is_open())
184 {
185 _M_file.open(__s, __mode);
186 if (this->is_open())
187 {
188 _M_allocate_internal_buffer();
189 _M_mode = __mode;
190
191 // Setup initial buffer to 'uncommitted' mode.
192 _M_reading = false;
193 _M_writing = false;
194 _M_set_buffer(-1);
195
196 // Reset to initial state.
197 _M_state_last = _M_state_cur = _M_state_beg;
198
199 // 27.8.1.3,4
200 if ((__mode & ios_base::ate)
201 && this->seekoff(0, ios_base::end, __mode)
202 == pos_type(off_type(-1)))
203 this->close();
204 else
205 __ret = this;
206 }
207 }
208 return __ret;
209 }
210
211#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
212 template<typename _CharT, typename _Traits>
215 open(const wchar_t* __s, ios_base::openmode __mode)
216 {
217 __filebuf_type *__ret = 0;
218 if (!this->is_open())
219 {
220 _M_file.open(__s, __mode);
221 if (this->is_open())
222 {
223 _M_allocate_internal_buffer();
224 _M_mode = __mode;
225
226 // Setup initial buffer to 'uncommitted' mode.
227 _M_reading = false;
228 _M_writing = false;
229 _M_set_buffer(-1);
230
231 // Reset to initial state.
232 _M_state_last = _M_state_cur = _M_state_beg;
233
234 // 27.8.1.3,4
235 if ((__mode & ios_base::ate)
236 && this->seekoff(0, ios_base::end, __mode)
237 == pos_type(off_type(-1)))
238 this->close();
239 else
240 __ret = this;
241 }
242 }
243 return __ret;
244 }
245#endif // HAVE__WFOPEN && USE_WCHAR_T
246
247 template<typename _CharT, typename _Traits>
248 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
250 close()
251 {
252 if (!this->is_open())
253 return 0;
254
255 bool __testfail = false;
256 {
257 // NB: Do this here so that re-opened filebufs will be cool...
258 struct __close_sentry
259 {
260 basic_filebuf *__fb;
261 __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { }
262 ~__close_sentry ()
263 {
264 __fb->_M_mode = ios_base::openmode(0);
265 __fb->_M_pback_init = false;
266 __fb->_M_destroy_internal_buffer();
267 __fb->_M_reading = false;
268 __fb->_M_writing = false;
269 __fb->_M_set_buffer(-1);
270 __fb->_M_state_last = __fb->_M_state_cur = __fb->_M_state_beg;
271 }
272 } __cs (this);
273
274 __try
275 {
276 if (!_M_terminate_output())
277 __testfail = true;
278 }
279 __catch(...)
280 {
281 _M_file.close();
282 __throw_exception_again;
283 }
284 }
285
286 if (!_M_file.close())
287 __testfail = true;
288
289 if (__testfail)
290 return 0;
291 else
292 return this;
293 }
294
295 template<typename _CharT, typename _Traits>
299 {
300 streamsize __ret = -1;
301 const bool __testin = _M_mode & ios_base::in;
302 if (__testin && this->is_open())
303 {
304 // For a stateful encoding (-1) the pending sequence might be just
305 // shift and unshift prefixes with no actual character.
306 __ret = this->egptr() - this->gptr();
307
308#if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM
309 // About this workaround, see libstdc++/20806.
310 const bool __testbinary = _M_mode & ios_base::binary;
311 if (__check_facet(_M_codecvt).encoding() >= 0
312 && __testbinary)
313#else
314 if (__check_facet(_M_codecvt).encoding() >= 0)
315#endif
316 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
317 }
318 return __ret;
319 }
320
321 template<typename _CharT, typename _Traits>
325 {
326 int_type __ret = traits_type::eof();
327 const bool __testin = _M_mode & ios_base::in;
328 if (__testin)
329 {
330 if (_M_writing)
331 {
332 if (overflow() == traits_type::eof())
333 return __ret;
334 _M_set_buffer(-1);
335 _M_writing = false;
336 }
337 // Check for pback madness, and if so switch back to the
338 // normal buffers and jet outta here before expensive
339 // fileops happen...
341
342 if (this->gptr() < this->egptr())
343 return traits_type::to_int_type(*this->gptr());
344
345 // Get and convert input sequence.
346 const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
347
348 // Will be set to true if ::read() returns 0 indicating EOF.
349 bool __got_eof = false;
350 // Number of internal characters produced.
351 streamsize __ilen = 0;
352 codecvt_base::result __r = codecvt_base::ok;
353 if (__check_facet(_M_codecvt).always_noconv())
354 {
355 __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
356 __buflen);
357 if (__ilen == 0)
358 __got_eof = true;
359 }
360 else
361 {
362 // Worst-case number of external bytes.
363 // XXX Not done encoding() == -1.
364 const int __enc = _M_codecvt->encoding();
365 streamsize __blen; // Minimum buffer size.
366 streamsize __rlen; // Number of chars to read.
367 if (__enc > 0)
368 __blen = __rlen = __buflen * __enc;
369 else
370 {
371 __blen = __buflen + _M_codecvt->max_length() - 1;
372 __rlen = __buflen;
373 }
374 const streamsize __remainder = _M_ext_end - _M_ext_next;
375 __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
376
377 // An imbue in 'read' mode implies first converting the external
378 // chars already present.
379 if (_M_reading && this->egptr() == this->eback() && __remainder)
380 __rlen = 0;
381
382 // Allocate buffer if necessary and move unconverted
383 // bytes to front.
384 if (_M_ext_buf_size < __blen)
386 char* __buf = new char[__blen];
387 if (__remainder)
388 __builtin_memcpy(__buf, _M_ext_next, __remainder);
389
390 delete [] _M_ext_buf;
391 _M_ext_buf = __buf;
392 _M_ext_buf_size = __blen;
393 }
394 else if (__remainder)
395 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
396
397 _M_ext_next = _M_ext_buf;
398 _M_ext_end = _M_ext_buf + __remainder;
399 _M_state_last = _M_state_cur;
400
401 do
402 {
403 if (__rlen > 0)
404 {
405 // Sanity check!
406 // This may fail if the return value of
407 // codecvt::max_length() is bogus.
408 if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
409 {
410 __throw_ios_failure(__N("basic_filebuf::underflow "
411 "codecvt::max_length() "
412 "is not valid"));
413 }
414 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
415 if (__elen == 0)
416 __got_eof = true;
417 else if (__elen == -1)
418 break;
419 _M_ext_end += __elen;
420 }
422 char_type* __iend = this->eback();
423 if (_M_ext_next < _M_ext_end)
424 __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
425 _M_ext_end, _M_ext_next,
426 this->eback(),
427 this->eback() + __buflen, __iend);
428 if (__r == codecvt_base::noconv)
429 {
430 size_t __avail = _M_ext_end - _M_ext_buf;
431 __ilen = std::min(__avail, __buflen);
432 traits_type::copy(this->eback(),
433 reinterpret_cast<char_type*>
434 (_M_ext_buf), __ilen);
435 _M_ext_next = _M_ext_buf + __ilen;
437 else
438 __ilen = __iend - this->eback();
440 // _M_codecvt->in may return error while __ilen > 0: this is
441 // ok, and actually occurs in case of mixed encodings (e.g.,
442 // XML files).
443 if (__r == codecvt_base::error)
444 break;
446 __rlen = 1;
447 }
448 while (__ilen == 0 && !__got_eof);
449 }
450
451 if (__ilen > 0)
452 {
453 _M_set_buffer(__ilen);
454 _M_reading = true;
455 __ret = traits_type::to_int_type(*this->gptr());
456 }
457 else if (__got_eof)
458 {
459 // If the actual end of file is reached, set 'uncommitted'
460 // mode, thus allowing an immediate write without an
461 // intervening seek.
462 _M_set_buffer(-1);
463 _M_reading = false;
464 // However, reaching it while looping on partial means that
465 // the file has got an incomplete character.
466 if (__r == codecvt_base::partial)
467 __throw_ios_failure(__N("basic_filebuf::underflow "
468 "incomplete character in file"));
469 }
470 else if (__r == codecvt_base::error)
471 __throw_ios_failure(__N("basic_filebuf::underflow "
472 "invalid byte sequence in file"));
473 else
474 __throw_ios_failure(__N("basic_filebuf::underflow "
475 "error reading the file"), errno);
476 }
477 return __ret;
478 }
479
480 template<typename _CharT, typename _Traits>
481 typename basic_filebuf<_CharT, _Traits>::int_type
483 pbackfail(int_type __i)
484 {
485 int_type __ret = traits_type::eof();
486 const bool __testin = _M_mode & ios_base::in;
487 if (__testin)
488 {
489 if (_M_writing)
490 {
491 if (overflow() == traits_type::eof())
492 return __ret;
493 _M_set_buffer(-1);
494 _M_writing = false;
495 }
496 // Remember whether the pback buffer is active, otherwise below
497 // we may try to store in it a second char (libstdc++/9761).
498 const bool __testpb = _M_pback_init;
499 const bool __testeof = traits_type::eq_int_type(__i, __ret);
500 int_type __tmp;
501 if (this->eback() < this->gptr())
502 {
503 this->gbump(-1);
504 __tmp = traits_type::to_int_type(*this->gptr());
505 }
506 else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
507 {
508 __tmp = this->underflow();
509 if (traits_type::eq_int_type(__tmp, __ret))
510 return __ret;
511 }
512 else
513 {
514 // At the beginning of the buffer, need to make a
515 // putback position available. But the seek may fail
516 // (f.i., at the beginning of a file, see
517 // libstdc++/9439) and in that case we return
518 // traits_type::eof().
519 return __ret;
520 }
521
522 // Try to put back __i into input sequence in one of three ways.
523 // Order these tests done in is unspecified by the standard.
524 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
525 __ret = __i;
526 else if (__testeof)
527 __ret = traits_type::not_eof(__i);
528 else if (!__testpb)
529 {
531 _M_reading = true;
532 *this->gptr() = traits_type::to_char_type(__i);
533 __ret = __i;
534 }
535 }
536 return __ret;
537 }
538
539 template<typename _CharT, typename _Traits>
540 typename basic_filebuf<_CharT, _Traits>::int_type
542 overflow(int_type __c)
543 {
544 int_type __ret = traits_type::eof();
545 const bool __testeof = traits_type::eq_int_type(__c, __ret);
546 const bool __testout = (_M_mode & ios_base::out
548 if (__testout)
549 {
550 if (_M_reading)
551 {
553 const int __gptr_off = _M_get_ext_pos(_M_state_last);
554 if (_M_seek(__gptr_off, ios_base::cur, _M_state_last)
555 == pos_type(off_type(-1)))
556 return __ret;
557 }
558 if (this->pbase() < this->pptr())
559 {
560 struct _Guard {
561 ~_Guard() { if (_M_p) _M_p->pbump(-1); }
562 basic_filebuf* _M_p;
563 };
564 _Guard __g = { };
565
566 // If appropriate, append the overflow char.
567 if (!__testeof)
568 {
569 *this->pptr() = traits_type::to_char_type(__c);
570 this->pbump(1);
571 __g._M_p = this;
572 }
573
574 // Convert pending sequence to external representation,
575 // and output.
576 if (_M_convert_to_external(this->pbase(),
577 this->pptr() - this->pbase()))
578 {
579 __g._M_p = 0;
580 _M_set_buffer(0);
581 __ret = traits_type::not_eof(__c);
582 }
583 }
584 else if (_M_buf_size > 1)
585 {
586 // Overflow in 'uncommitted' mode: set _M_writing, set
587 // the buffer to the initial 'write' mode, and put __c
588 // into the buffer.
589 _M_set_buffer(0);
590 _M_writing = true;
591 if (!__testeof)
592 {
593 *this->pptr() = traits_type::to_char_type(__c);
594 this->pbump(1);
595 }
596 __ret = traits_type::not_eof(__c);
597 }
598 else
599 {
600 // Unbuffered.
601 char_type __conv = traits_type::to_char_type(__c);
602 if (__testeof || _M_convert_to_external(&__conv, 1))
603 {
604 _M_writing = true;
605 __ret = traits_type::not_eof(__c);
606 }
607 }
608 }
609 return __ret;
610 }
611
612 template<typename _CharT, typename _Traits>
613 bool
615 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
616 {
617 // Sizes of external and pending output.
618 streamsize __elen;
619 streamsize __plen;
620 if (__check_facet(_M_codecvt).always_noconv())
621 {
622 __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
623 __plen = __ilen;
624 }
625 else
626 {
627 // Worst-case number of external bytes needed.
628 // XXX Not done encoding() == -1.
629 streamsize __blen = __ilen * _M_codecvt->max_length();
630 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
631
632 char* __bend;
633 const char_type* __iend;
634 codecvt_base::result __r;
635 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
636 __iend, __buf, __buf + __blen, __bend);
637
638 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
639 __blen = __bend - __buf;
640 else if (__r == codecvt_base::noconv)
641 {
642 // Same as the always_noconv case above.
643 __buf = reinterpret_cast<char*>(__ibuf);
644 __blen = __ilen;
645 }
646 else
647 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
648 "conversion error"));
649
650 __elen = _M_file.xsputn(__buf, __blen);
651 __plen = __blen;
652
653 // Try once more for partial conversions.
654 if (__r == codecvt_base::partial && __elen == __plen)
655 {
656 const char_type* __iresume = __iend;
657 streamsize __rlen = this->pptr() - __iend;
658 __r = _M_codecvt->out(_M_state_cur, __iresume,
659 __iresume + __rlen, __iend, __buf,
660 __buf + __blen, __bend);
661 if (__r != codecvt_base::error)
662 {
663 __rlen = __bend - __buf;
664 __elen = _M_file.xsputn(__buf, __rlen);
665 __plen = __rlen;
666 }
667 else
668 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
669 "conversion error"));
670 }
671 }
672 return __elen == __plen;
673 }
674
675 template<typename _CharT, typename _Traits>
678 xsgetn(_CharT* __s, streamsize __n)
679 {
680 // Clear out pback buffer before going on to the real deal...
681 streamsize __ret = 0;
682 if (_M_pback_init)
683 {
684 if (__n > 0 && this->gptr() == this->eback())
685 {
686 *__s++ = *this->gptr(); // emulate non-underflowing sbumpc
687 this->gbump(1);
688 __ret = 1;
689 --__n;
690 }
692 }
693 else if (_M_writing)
694 {
695 if (overflow() == traits_type::eof())
696 return __ret;
697 _M_set_buffer(-1);
698 _M_writing = false;
699 }
700
701 // Optimization in the always_noconv() case, to be generalized in the
702 // future: when __n > __buflen we read directly instead of using the
703 // buffer repeatedly.
704 const bool __testin = _M_mode & ios_base::in;
705 const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
706
707 if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
708 && __testin)
709 {
710 // First, copy the chars already present in the buffer.
711 const streamsize __avail = this->egptr() - this->gptr();
712 if (__avail != 0)
713 {
714 traits_type::copy(__s, this->gptr(), __avail);
715 __s += __avail;
716 this->setg(this->eback(), this->gptr() + __avail, this->egptr());
717 __ret += __avail;
718 __n -= __avail;
719 }
720
721 // Need to loop in case of short reads (relatively common
722 // with pipes).
723 streamsize __len;
724 for (;;)
725 {
726 __len = _M_file.xsgetn(reinterpret_cast<char*>(__s), __n);
727 if (__len == -1)
728 __throw_ios_failure(__N("basic_filebuf::xsgetn "
729 "error reading the file"), errno);
730 if (__len == 0)
731 break;
732
733 __n -= __len;
734 __ret += __len;
735 if (__n == 0)
736 break;
737
738 __s += __len;
739 }
740
741 if (__n == 0)
742 {
743 // Set _M_reading. Buffer is already in initial 'read' mode.
744 _M_reading = true;
745 }
746 else if (__len == 0)
747 {
748 // If end of file is reached, set 'uncommitted'
749 // mode, thus allowing an immediate write without
750 // an intervening seek.
751 _M_set_buffer(-1);
752 _M_reading = false;
753 }
754 }
755 else
756 __ret += __streambuf_type::xsgetn(__s, __n);
757
758 return __ret;
759 }
760
761 template<typename _CharT, typename _Traits>
764 xsputn(const _CharT* __s, streamsize __n)
765 {
766 streamsize __ret = 0;
767 // Optimization in the always_noconv() case, to be generalized in the
768 // future: when __n is sufficiently large we write directly instead of
769 // using the buffer.
770 const bool __testout = (_M_mode & ios_base::out
772 if (__check_facet(_M_codecvt).always_noconv()
773 && __testout && !_M_reading)
774 {
775 // Measurement would reveal the best choice.
776 const streamsize __chunk = 1ul << 10;
777 streamsize __bufavail = this->epptr() - this->pptr();
778
779 // Don't mistake 'uncommitted' mode buffered with unbuffered.
780 if (!_M_writing && _M_buf_size > 1)
781 __bufavail = _M_buf_size - 1;
782
783 const streamsize __limit = std::min(__chunk, __bufavail);
784 if (__n >= __limit)
785 {
786 const streamsize __buffill = this->pptr() - this->pbase();
787 const char* __buf = reinterpret_cast<const char*>(this->pbase());
788 __ret = _M_file.xsputn_2(__buf, __buffill,
789 reinterpret_cast<const char*>(__s),
790 __n);
791 if (__ret == __buffill + __n)
792 {
793 _M_set_buffer(0);
794 _M_writing = true;
795 }
796 if (__ret > __buffill)
797 __ret -= __buffill;
798 else
799 __ret = 0;
800 }
801 else
802 __ret = __streambuf_type::xsputn(__s, __n);
803 }
804 else
805 __ret = __streambuf_type::xsputn(__s, __n);
806 return __ret;
807 }
808
809 template<typename _CharT, typename _Traits>
810 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
812 setbuf(char_type* __s, streamsize __n)
813 {
814 if (!this->is_open())
815 {
816 if (__s == 0 && __n == 0)
817 _M_buf_size = 1;
818 else if (__s && __n > 0)
819 {
820 // This is implementation-defined behavior, and assumes that
821 // an external char_type array of length __n exists and has
822 // been pre-allocated. If this is not the case, things will
823 // quickly blow up. When __n > 1, __n - 1 positions will be
824 // used for the get area, __n - 1 for the put area and 1
825 // position to host the overflow char of a full put area.
826 // When __n == 1, 1 position will be used for the get area
827 // and 0 for the put area, as in the unbuffered case above.
828 _M_buf = __s;
829 _M_buf_size = __n;
830 }
831 }
832 return this;
833 }
834
835
836 // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
837 // argument (of type openmode).
838 template<typename _CharT, typename _Traits>
839 typename basic_filebuf<_CharT, _Traits>::pos_type
841 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
842 {
843 int __width = 0;
844 if (_M_codecvt)
845 __width = _M_codecvt->encoding();
846 if (__width < 0)
847 __width = 0;
848
849 pos_type __ret = pos_type(off_type(-1));
850 const bool __testfail = __off != 0 && __width <= 0;
851 if (this->is_open() && !__testfail)
852 {
853 // tellg and tellp queries do not affect any state, unless
854 // ! always_noconv and the put sequence is not empty.
855 // In that case, determining the position requires converting the
856 // put sequence. That doesn't use ext_buf, so requires a flush.
857 bool __no_movement = __way == ios_base::cur && __off == 0
858 && (!_M_writing || _M_codecvt->always_noconv());
859
860 // Ditch any pback buffers to avoid confusion.
861 if (!__no_movement)
863
864 // Correct state at destination. Note that this is the correct
865 // state for the current position during output, because
866 // codecvt::unshift() returns the state to the initial state.
867 // This is also the correct state at the end of the file because
868 // an unshift sequence should have been written at the end.
869 __state_type __state = _M_state_beg;
870 off_type __computed_off = __off * __width;
871 if (_M_reading && __way == ios_base::cur)
872 {
873 __state = _M_state_last;
874 __computed_off += _M_get_ext_pos(__state);
875 }
876 if (!__no_movement)
877 __ret = _M_seek(__computed_off, __way, __state);
878 else
879 {
880 if (_M_writing)
881 __computed_off = this->pptr() - this->pbase();
882
883 off_type __file_off = _M_file.seekoff(0, ios_base::cur);
884 if (__file_off != off_type(-1))
885 {
886 __ret = __file_off + __computed_off;
887 __ret.state(__state);
888 }
889 }
890 }
891 return __ret;
892 }
893
894 // _GLIBCXX_RESOLVE_LIB_DEFECTS
895 // 171. Strange seekpos() semantics due to joint position
896 // According to the resolution of DR 171, seekpos should ignore the last
897 // argument (of type openmode).
898 template<typename _CharT, typename _Traits>
899 typename basic_filebuf<_CharT, _Traits>::pos_type
901 seekpos(pos_type __pos, ios_base::openmode)
902 {
903 pos_type __ret = pos_type(off_type(-1));
904 if (this->is_open())
905 {
906 // Ditch any pback buffers to avoid confusion.
908 __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
909 }
910 return __ret;
911 }
912
913 template<typename _CharT, typename _Traits>
914 typename basic_filebuf<_CharT, _Traits>::pos_type
916 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
917 {
918 pos_type __ret = pos_type(off_type(-1));
919 if (_M_terminate_output())
920 {
921 off_type __file_off = _M_file.seekoff(__off, __way);
922 if (__file_off != off_type(-1))
923 {
924 _M_reading = false;
925 _M_writing = false;
926 _M_ext_next = _M_ext_end = _M_ext_buf;
927 _M_set_buffer(-1);
928 _M_state_cur = __state;
929 __ret = __file_off;
930 __ret.state(_M_state_cur);
931 }
932 }
933 return __ret;
934 }
935
936 // Returns the distance from the end of the ext buffer to the point
937 // corresponding to gptr(). This is a negative value. Updates __state
938 // from eback() correspondence to gptr().
939 template<typename _CharT, typename _Traits>
941 _M_get_ext_pos(__state_type& __state)
942 {
943 if (_M_codecvt->always_noconv())
944 return this->gptr() - this->egptr();
945 else
946 {
947 // Calculate offset from _M_ext_buf that corresponds to
948 // gptr(). Precondition: __state == _M_state_last, which
949 // corresponds to eback().
950 const int __gptr_off =
951 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
952 this->gptr() - this->eback());
953 return _M_ext_buf + __gptr_off - _M_ext_end;
954 }
955 }
956
957 template<typename _CharT, typename _Traits>
958 bool
961 {
962 // Part one: update the output sequence.
963 bool __testvalid = true;
964 if (this->pbase() < this->pptr())
965 {
966 const int_type __tmp = this->overflow();
967 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
968 __testvalid = false;
969 }
970
971 // Part two: output unshift sequence.
972 if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
973 && __testvalid)
974 {
975 // Note: this value is arbitrary, since there is no way to
976 // get the length of the unshift sequence from codecvt,
977 // without calling unshift.
978 const size_t __blen = 128;
979 char __buf[__blen];
980 codecvt_base::result __r;
981 streamsize __ilen = 0;
982
983 do
984 {
985 char* __next;
986 __r = _M_codecvt->unshift(_M_state_cur, __buf,
987 __buf + __blen, __next);
988 if (__r == codecvt_base::error)
989 __testvalid = false;
990 else if (__r == codecvt_base::ok ||
991 __r == codecvt_base::partial)
992 {
993 __ilen = __next - __buf;
994 if (__ilen > 0)
995 {
996 const streamsize __elen = _M_file.xsputn(__buf, __ilen);
997 if (__elen != __ilen)
998 __testvalid = false;
999 }
1000 }
1001 }
1002 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
1003
1004 if (__testvalid)
1005 {
1006 // This second call to overflow() is required by the standard,
1007 // but it's not clear why it's needed, since the output buffer
1008 // should be empty by this point (it should have been emptied
1009 // in the first call to overflow()).
1010 const int_type __tmp = this->overflow();
1011 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1012 __testvalid = false;
1013 }
1014 }
1015 return __testvalid;
1016 }
1017
1018 template<typename _CharT, typename _Traits>
1019 int
1021 sync()
1022 {
1023 // Make sure that the internal buffer resyncs its idea of
1024 // the file position with the external file.
1025 int __ret = 0;
1026 if (this->pbase() < this->pptr())
1027 {
1028 const int_type __tmp = this->overflow();
1029 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1030 __ret = -1;
1031 }
1032 return __ret;
1033 }
1034
1035 template<typename _CharT, typename _Traits>
1036 void
1038 imbue(const locale& __loc)
1039 {
1040 bool __testvalid = true;
1041
1042 const __codecvt_type* _M_codecvt_tmp = 0;
1043 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
1044 _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc);
1045
1046 if (this->is_open())
1047 {
1048 // encoding() == -1 is ok only at the beginning.
1049 if ((_M_reading || _M_writing)
1050 && __check_facet(_M_codecvt).encoding() == -1)
1051 __testvalid = false;
1052 else
1053 {
1054 if (_M_reading)
1055 {
1056 if (__check_facet(_M_codecvt).always_noconv())
1057 {
1058 if (_M_codecvt_tmp
1059 && !__check_facet(_M_codecvt_tmp).always_noconv())
1060 __testvalid = this->seekoff(0, ios_base::cur, _M_mode)
1061 != pos_type(off_type(-1));
1062 }
1063 else
1064 {
1065 // External position corresponding to gptr().
1067 + _M_codecvt->length(_M_state_last, _M_ext_buf,
1069 this->gptr() - this->eback());
1070 const streamsize __remainder = _M_ext_end - _M_ext_next;
1071 if (__remainder)
1072 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
1073
1075 _M_ext_end = _M_ext_buf + __remainder;
1076 _M_set_buffer(-1);
1077 _M_state_last = _M_state_cur = _M_state_beg;
1078 }
1079 }
1080 else if (_M_writing && (__testvalid = _M_terminate_output()))
1081 _M_set_buffer(-1);
1082 }
1083 }
1084
1085 if (__testvalid)
1086 _M_codecvt = _M_codecvt_tmp;
1087 else
1088 _M_codecvt = 0;
1089 }
1090
1091 // Inhibit implicit instantiations for required instantiations,
1092 // which are defined via explicit instantiations elsewhere.
1093#if _GLIBCXX_EXTERN_TEMPLATE
1094 extern template class basic_filebuf<char>;
1095 extern template class basic_ifstream<char>;
1096 extern template class basic_ofstream<char>;
1097 extern template class basic_fstream<char>;
1098
1099#ifdef _GLIBCXX_USE_WCHAR_T
1100 extern template class basic_filebuf<wchar_t>;
1101 extern template class basic_ifstream<wchar_t>;
1102 extern template class basic_ofstream<wchar_t>;
1103 extern template class basic_fstream<wchar_t>;
1104#endif
1105#endif
1106
1107_GLIBCXX_END_NAMESPACE_VERSION
1108} // namespace std
1109
1110#endif
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:104
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition any:428
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
bool has_facet(const locale &__loc)
Test for the presence of a facet.
const _Facet & use_facet(const locale &__loc)
Return a facet.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:98
The actual work of input and output (for files).
Definition fstream:86
virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition fstream.tcc:841
virtual int_type underflow()
Fetches more data from the controlled sequence.
Definition fstream.tcc:324
char_type _M_pback
Definition fstream:169
virtual streamsize showmanyc()
Investigating the data available.
Definition fstream.tcc:298
ios_base::openmode _M_mode
Place to stash in || out || in | out settings for current filebuf.
Definition fstream:126
streamsize _M_ext_buf_size
Definition fstream:188
char_type * _M_pback_cur_save
Definition fstream:170
virtual void imbue(const locale &__loc)
Changes translations.
Definition fstream.tcc:1038
virtual streamsize xsgetn(char_type *__s, streamsize __n)
Multiple character extraction.
Definition fstream.tcc:678
bool is_open() const
Returns true if the external file is open.
Definition fstream:270
size_t _M_buf_size
Definition fstream:148
__filebuf_type * close()
Closes the currently associated file.
Definition fstream.tcc:250
char_type * _M_pback_end_save
Definition fstream:171
char * _M_ext_buf
Definition fstream:183
void _M_destroy_pback()
Definition fstream:221
virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
Definition fstream.tcc:901
char_type * _M_buf
Pointer to the beginning of internal buffer.
Definition fstream:141
virtual int_type pbackfail(int_type __c=_Traits::eof())
Tries to back up the input sequence.
Definition fstream.tcc:483
virtual streamsize xsputn(const char_type *__s, streamsize __n)
Multiple character insertion.
Definition fstream.tcc:764
virtual int_type overflow(int_type __c=_Traits::eof())
Consumes data from the buffer; writes to the controlled sequence.
Definition fstream.tcc:542
void _M_create_pback()
Definition fstream:204
__filebuf_type * open(const char *__s, ios_base::openmode __mode)
Opens an external file.
Definition fstream.tcc:180
basic_filebuf()
Does not open any files.
Definition fstream.tcc:81
void _M_set_buffer(streamsize __off)
Definition fstream:464
virtual int sync()
Synchronizes the buffer arrays with the controlled sequences.
Definition fstream.tcc:1021
const char * _M_ext_next
Definition fstream:195
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
Definition fstream.tcc:812
Controlling input for files.
Definition fstream:498
Controlling output for files.
Definition fstream:759
Controlling input and output for files.
Definition fstream:1022
traits_type::int_type int_type
Definition streambuf:133
char_type * epptr() const
Access to the put area.
Definition streambuf:540
virtual streamsize xsputn(const char_type *__s, streamsize __n)
char_type * pptr() const
Access to the put area.
Definition streambuf:537
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
Definition streambuf:514
char_type * eback() const
Access to the get area.
Definition streambuf:487
char_type * egptr() const
Access to the get area.
Definition streambuf:493
char_type * gptr() const
Access to the get area.
Definition streambuf:490
virtual streamsize xsgetn(char_type *__s, streamsize __n)
void gbump(int __n)
Moving the read position.
Definition streambuf:503
void pbump(int __n)
Moving the write position.
Definition streambuf:550
char_type * pbase() const
Access to the put area.
Definition streambuf:534
locale _M_buf_locale
Current locale setting.
Definition streambuf:197
The base of the I/O class hierarchy.
Definition ios_base.h:229
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition ios_base.h:485
static const seekdir beg
Request a seek relative to the beginning of the stream.
Definition ios_base.h:482
static const seekdir end
Request a seek relative to the current end of the sequence.
Definition ios_base.h:488
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:461
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:464
static const openmode binary
Perform input and output in binary mode (as opposed to text mode). This is probably not what you thin...
Definition ios_base.h:458
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:447
static const openmode app
Seek to end before each write.
Definition ios_base.h:450
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:479
static const openmode ate
Open and seek to end immediately after opening.
Definition ios_base.h:453
Container class for localization functionality.