1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997-2021 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
31 // ISO C++ 14882: 27.8 File-based streams
35 #define _FSTREAM_TCC 1
37 #pragma GCC system_header
39 #include <bits/cxxabi_forced.h>
40 #include <bits/move.h> // for swap
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<typename _CharT, typename _Traits>
49 basic_filebuf<_CharT, _Traits>::
50 _M_allocate_internal_buffer()
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)
56 _M_buf = new char_type[_M_buf_size];
57 _M_buf_allocated = true;
61 template<typename _CharT, typename _Traits>
63 basic_filebuf<_CharT, _Traits>::
64 _M_destroy_internal_buffer() throw()
70 _M_buf_allocated = false;
79 template<typename _CharT, typename _Traits>
80 basic_filebuf<_CharT, _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(),
85 _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false),
86 _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
89 if (has_facet<__codecvt_type>(this->_M_buf_locale))
90 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
93 #if __cplusplus >= 201103L
94 template<typename _CharT, typename _Traits>
95 basic_filebuf<_CharT, _Traits>::
96 basic_filebuf(basic_filebuf&& __rhs)
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))
118 __rhs._M_set_buffer(-1);
119 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
122 template<typename _CharT, typename _Traits>
123 basic_filebuf<_CharT, _Traits>&
124 basic_filebuf<_CharT, _Traits>::
125 operator=(basic_filebuf&& __rhs)
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;
151 template<typename _CharT, typename _Traits>
153 basic_filebuf<_CharT, _Traits>::
154 swap(basic_filebuf& __rhs)
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);
177 template<typename _CharT, typename _Traits>
178 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
179 basic_filebuf<_CharT, _Traits>::
180 open(const char* __s, ios_base::openmode __mode)
182 __filebuf_type *__ret = 0;
183 if (!this->is_open())
185 _M_file.open(__s, __mode);
188 _M_allocate_internal_buffer();
191 // Setup initial buffer to 'uncommitted' mode.
196 // Reset to initial state.
197 _M_state_last = _M_state_cur = _M_state_beg;
200 if ((__mode & ios_base::ate)
201 && this->seekoff(0, ios_base::end, __mode)
202 == pos_type(off_type(-1)))
211 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
212 template<typename _CharT, typename _Traits>
213 basic_filebuf<_CharT, _Traits>*
214 basic_filebuf<_CharT, _Traits>::
215 open(const wchar_t* __s, ios_base::openmode __mode)
217 __filebuf_type *__ret = 0;
218 if (!this->is_open())
220 _M_file.open(__s, __mode);
223 _M_allocate_internal_buffer();
226 // Setup initial buffer to 'uncommitted' mode.
231 // Reset to initial state.
232 _M_state_last = _M_state_cur = _M_state_beg;
235 if ((__mode & ios_base::ate)
236 && this->seekoff(0, ios_base::end, __mode)
237 == pos_type(off_type(-1)))
245 #endif // HAVE__WFOPEN && USE_WCHAR_T
247 template<typename _CharT, typename _Traits>
248 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
249 basic_filebuf<_CharT, _Traits>::
252 if (!this->is_open())
255 bool __testfail = false;
257 // NB: Do this here so that re-opened filebufs will be cool...
258 struct __close_sentry
261 __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { }
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;
276 if (!_M_terminate_output())
282 __throw_exception_again;
286 if (!_M_file.close())
295 template<typename _CharT, typename _Traits>
297 basic_filebuf<_CharT, _Traits>::
300 streamsize __ret = -1;
301 const bool __testin = _M_mode & ios_base::in;
302 if (__testin && this->is_open())
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();
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
314 if (__check_facet(_M_codecvt).encoding() >= 0)
316 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
321 template<typename _CharT, typename _Traits>
322 typename basic_filebuf<_CharT, _Traits>::int_type
323 basic_filebuf<_CharT, _Traits>::
326 int_type __ret = traits_type::eof();
327 const bool __testin = _M_mode & ios_base::in;
332 if (overflow() == traits_type::eof())
337 // Check for pback madness, and if so switch back to the
338 // normal buffers and jet outta here before expensive
342 if (this->gptr() < this->egptr())
343 return traits_type::to_int_type(*this->gptr());
345 // Get and convert input sequence.
346 const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
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())
355 __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
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.
368 __blen = __rlen = __buflen * __enc;
371 __blen = __buflen + _M_codecvt->max_length() - 1;
374 const streamsize __remainder = _M_ext_end - _M_ext_next;
375 __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
377 // An imbue in 'read' mode implies first converting the external
378 // chars already present.
379 if (_M_reading && this->egptr() == this->eback() && __remainder)
382 // Allocate buffer if necessary and move unconverted
384 if (_M_ext_buf_size < __blen)
386 char* __buf = new char[__blen];
388 __builtin_memcpy(__buf, _M_ext_next, __remainder);
390 delete [] _M_ext_buf;
392 _M_ext_buf_size = __blen;
394 else if (__remainder)
395 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
397 _M_ext_next = _M_ext_buf;
398 _M_ext_end = _M_ext_buf + __remainder;
399 _M_state_last = _M_state_cur;
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)
410 __throw_ios_failure(__N("basic_filebuf::underflow "
411 "codecvt::max_length() "
414 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
417 else if (__elen == -1)
419 _M_ext_end += __elen;
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,
427 this->eback() + __buflen, __iend);
428 if (__r == codecvt_base::noconv)
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;
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.,
443 if (__r == codecvt_base::error)
448 while (__ilen == 0 && !__got_eof);
453 _M_set_buffer(__ilen);
455 __ret = traits_type::to_int_type(*this->gptr());
459 // If the actual end of file is reached, set 'uncommitted'
460 // mode, thus allowing an immediate write without an
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"));
470 else if (__r == codecvt_base::error)
471 __throw_ios_failure(__N("basic_filebuf::underflow "
472 "invalid byte sequence in file"));
474 __throw_ios_failure(__N("basic_filebuf::underflow "
475 "error reading the file"), errno);
480 template<typename _CharT, typename _Traits>
481 typename basic_filebuf<_CharT, _Traits>::int_type
482 basic_filebuf<_CharT, _Traits>::
483 pbackfail(int_type __i)
485 int_type __ret = traits_type::eof();
486 const bool __testin = _M_mode & ios_base::in;
491 if (overflow() == traits_type::eof())
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);
501 if (this->eback() < this->gptr())
504 __tmp = traits_type::to_int_type(*this->gptr());
506 else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
508 __tmp = this->underflow();
509 if (traits_type::eq_int_type(__tmp, __ret))
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().
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))
527 __ret = traits_type::not_eof(__i);
532 *this->gptr() = traits_type::to_char_type(__i);
539 template<typename _CharT, typename _Traits>
540 typename basic_filebuf<_CharT, _Traits>::int_type
541 basic_filebuf<_CharT, _Traits>::
542 overflow(int_type __c)
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
547 || _M_mode & ios_base::app);
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)))
558 if (this->pbase() < this->pptr())
560 // If appropriate, append the overflow char.
563 *this->pptr() = traits_type::to_char_type(__c);
567 // Convert pending sequence to external representation,
569 if (_M_convert_to_external(this->pbase(),
570 this->pptr() - this->pbase()))
573 __ret = traits_type::not_eof(__c);
576 else if (_M_buf_size > 1)
578 // Overflow in 'uncommitted' mode: set _M_writing, set
579 // the buffer to the initial 'write' mode, and put __c
585 *this->pptr() = traits_type::to_char_type(__c);
588 __ret = traits_type::not_eof(__c);
593 char_type __conv = traits_type::to_char_type(__c);
594 if (__testeof || _M_convert_to_external(&__conv, 1))
597 __ret = traits_type::not_eof(__c);
604 template<typename _CharT, typename _Traits>
606 basic_filebuf<_CharT, _Traits>::
607 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
609 // Sizes of external and pending output.
612 if (__check_facet(_M_codecvt).always_noconv())
614 __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
619 // Worst-case number of external bytes needed.
620 // XXX Not done encoding() == -1.
621 streamsize __blen = __ilen * _M_codecvt->max_length();
622 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
625 const char_type* __iend;
626 codecvt_base::result __r;
627 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
628 __iend, __buf, __buf + __blen, __bend);
630 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
631 __blen = __bend - __buf;
632 else if (__r == codecvt_base::noconv)
634 // Same as the always_noconv case above.
635 __buf = reinterpret_cast<char*>(__ibuf);
639 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
640 "conversion error"));
642 __elen = _M_file.xsputn(__buf, __blen);
645 // Try once more for partial conversions.
646 if (__r == codecvt_base::partial && __elen == __plen)
648 const char_type* __iresume = __iend;
649 streamsize __rlen = this->pptr() - __iend;
650 __r = _M_codecvt->out(_M_state_cur, __iresume,
651 __iresume + __rlen, __iend, __buf,
652 __buf + __blen, __bend);
653 if (__r != codecvt_base::error)
655 __rlen = __bend - __buf;
656 __elen = _M_file.xsputn(__buf, __rlen);
660 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
661 "conversion error"));
664 return __elen == __plen;
667 template<typename _CharT, typename _Traits>
669 basic_filebuf<_CharT, _Traits>::
670 xsgetn(_CharT* __s, streamsize __n)
672 // Clear out pback buffer before going on to the real deal...
673 streamsize __ret = 0;
676 if (__n > 0 && this->gptr() == this->eback())
678 *__s++ = *this->gptr(); // emulate non-underflowing sbumpc
687 if (overflow() == traits_type::eof())
693 // Optimization in the always_noconv() case, to be generalized in the
694 // future: when __n > __buflen we read directly instead of using the
695 // buffer repeatedly.
696 const bool __testin = _M_mode & ios_base::in;
697 const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
699 if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
702 // First, copy the chars already present in the buffer.
703 const streamsize __avail = this->egptr() - this->gptr();
706 traits_type::copy(__s, this->gptr(), __avail);
708 this->setg(this->eback(), this->gptr() + __avail, this->egptr());
713 // Need to loop in case of short reads (relatively common
718 __len = _M_file.xsgetn(reinterpret_cast<char*>(__s), __n);
720 __throw_ios_failure(__N("basic_filebuf::xsgetn "
721 "error reading the file"), errno);
735 // Set _M_reading. Buffer is already in initial 'read' mode.
740 // If end of file is reached, set 'uncommitted'
741 // mode, thus allowing an immediate write without
742 // an intervening seek.
748 __ret += __streambuf_type::xsgetn(__s, __n);
753 template<typename _CharT, typename _Traits>
755 basic_filebuf<_CharT, _Traits>::
756 xsputn(const _CharT* __s, streamsize __n)
758 streamsize __ret = 0;
759 // Optimization in the always_noconv() case, to be generalized in the
760 // future: when __n is sufficiently large we write directly instead of
762 const bool __testout = (_M_mode & ios_base::out
763 || _M_mode & ios_base::app);
764 if (__check_facet(_M_codecvt).always_noconv()
765 && __testout && !_M_reading)
767 // Measurement would reveal the best choice.
768 const streamsize __chunk = 1ul << 10;
769 streamsize __bufavail = this->epptr() - this->pptr();
771 // Don't mistake 'uncommitted' mode buffered with unbuffered.
772 if (!_M_writing && _M_buf_size > 1)
773 __bufavail = _M_buf_size - 1;
775 const streamsize __limit = std::min(__chunk, __bufavail);
778 const streamsize __buffill = this->pptr() - this->pbase();
779 const char* __buf = reinterpret_cast<const char*>(this->pbase());
780 __ret = _M_file.xsputn_2(__buf, __buffill,
781 reinterpret_cast<const char*>(__s),
783 if (__ret == __buffill + __n)
788 if (__ret > __buffill)
794 __ret = __streambuf_type::xsputn(__s, __n);
797 __ret = __streambuf_type::xsputn(__s, __n);
801 template<typename _CharT, typename _Traits>
802 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
803 basic_filebuf<_CharT, _Traits>::
804 setbuf(char_type* __s, streamsize __n)
806 if (!this->is_open())
808 if (__s == 0 && __n == 0)
810 else if (__s && __n > 0)
812 // This is implementation-defined behavior, and assumes that
813 // an external char_type array of length __n exists and has
814 // been pre-allocated. If this is not the case, things will
815 // quickly blow up. When __n > 1, __n - 1 positions will be
816 // used for the get area, __n - 1 for the put area and 1
817 // position to host the overflow char of a full put area.
818 // When __n == 1, 1 position will be used for the get area
819 // and 0 for the put area, as in the unbuffered case above.
828 // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
829 // argument (of type openmode).
830 template<typename _CharT, typename _Traits>
831 typename basic_filebuf<_CharT, _Traits>::pos_type
832 basic_filebuf<_CharT, _Traits>::
833 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
837 __width = _M_codecvt->encoding();
841 pos_type __ret = pos_type(off_type(-1));
842 const bool __testfail = __off != 0 && __width <= 0;
843 if (this->is_open() && !__testfail)
845 // tellg and tellp queries do not affect any state, unless
846 // ! always_noconv and the put sequence is not empty.
847 // In that case, determining the position requires converting the
848 // put sequence. That doesn't use ext_buf, so requires a flush.
849 bool __no_movement = __way == ios_base::cur && __off == 0
850 && (!_M_writing || _M_codecvt->always_noconv());
852 // Ditch any pback buffers to avoid confusion.
856 // Correct state at destination. Note that this is the correct
857 // state for the current position during output, because
858 // codecvt::unshift() returns the state to the initial state.
859 // This is also the correct state at the end of the file because
860 // an unshift sequence should have been written at the end.
861 __state_type __state = _M_state_beg;
862 off_type __computed_off = __off * __width;
863 if (_M_reading && __way == ios_base::cur)
865 __state = _M_state_last;
866 __computed_off += _M_get_ext_pos(__state);
869 __ret = _M_seek(__computed_off, __way, __state);
873 __computed_off = this->pptr() - this->pbase();
875 off_type __file_off = _M_file.seekoff(0, ios_base::cur);
876 if (__file_off != off_type(-1))
878 __ret = __file_off + __computed_off;
879 __ret.state(__state);
886 // _GLIBCXX_RESOLVE_LIB_DEFECTS
887 // 171. Strange seekpos() semantics due to joint position
888 // According to the resolution of DR 171, seekpos should ignore the last
889 // argument (of type openmode).
890 template<typename _CharT, typename _Traits>
891 typename basic_filebuf<_CharT, _Traits>::pos_type
892 basic_filebuf<_CharT, _Traits>::
893 seekpos(pos_type __pos, ios_base::openmode)
895 pos_type __ret = pos_type(off_type(-1));
898 // Ditch any pback buffers to avoid confusion.
900 __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
905 template<typename _CharT, typename _Traits>
906 typename basic_filebuf<_CharT, _Traits>::pos_type
907 basic_filebuf<_CharT, _Traits>::
908 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
910 pos_type __ret = pos_type(off_type(-1));
911 if (_M_terminate_output())
913 off_type __file_off = _M_file.seekoff(__off, __way);
914 if (__file_off != off_type(-1))
918 _M_ext_next = _M_ext_end = _M_ext_buf;
920 _M_state_cur = __state;
922 __ret.state(_M_state_cur);
928 // Returns the distance from the end of the ext buffer to the point
929 // corresponding to gptr(). This is a negative value. Updates __state
930 // from eback() correspondence to gptr().
931 template<typename _CharT, typename _Traits>
932 int basic_filebuf<_CharT, _Traits>::
933 _M_get_ext_pos(__state_type& __state)
935 if (_M_codecvt->always_noconv())
936 return this->gptr() - this->egptr();
939 // Calculate offset from _M_ext_buf that corresponds to
940 // gptr(). Precondition: __state == _M_state_last, which
941 // corresponds to eback().
942 const int __gptr_off =
943 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
944 this->gptr() - this->eback());
945 return _M_ext_buf + __gptr_off - _M_ext_end;
949 template<typename _CharT, typename _Traits>
951 basic_filebuf<_CharT, _Traits>::
952 _M_terminate_output()
954 // Part one: update the output sequence.
955 bool __testvalid = true;
956 if (this->pbase() < this->pptr())
958 const int_type __tmp = this->overflow();
959 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
963 // Part two: output unshift sequence.
964 if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
967 // Note: this value is arbitrary, since there is no way to
968 // get the length of the unshift sequence from codecvt,
969 // without calling unshift.
970 const size_t __blen = 128;
972 codecvt_base::result __r;
973 streamsize __ilen = 0;
978 __r = _M_codecvt->unshift(_M_state_cur, __buf,
979 __buf + __blen, __next);
980 if (__r == codecvt_base::error)
982 else if (__r == codecvt_base::ok ||
983 __r == codecvt_base::partial)
985 __ilen = __next - __buf;
988 const streamsize __elen = _M_file.xsputn(__buf, __ilen);
989 if (__elen != __ilen)
994 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
998 // This second call to overflow() is required by the standard,
999 // but it's not clear why it's needed, since the output buffer
1000 // should be empty by this point (it should have been emptied
1001 // in the first call to overflow()).
1002 const int_type __tmp = this->overflow();
1003 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1004 __testvalid = false;
1010 template<typename _CharT, typename _Traits>
1012 basic_filebuf<_CharT, _Traits>::
1015 // Make sure that the internal buffer resyncs its idea of
1016 // the file position with the external file.
1018 if (this->pbase() < this->pptr())
1020 const int_type __tmp = this->overflow();
1021 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1027 template<typename _CharT, typename _Traits>
1029 basic_filebuf<_CharT, _Traits>::
1030 imbue(const locale& __loc)
1032 bool __testvalid = true;
1034 const __codecvt_type* _M_codecvt_tmp = 0;
1035 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
1036 _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc);
1038 if (this->is_open())
1040 // encoding() == -1 is ok only at the beginning.
1041 if ((_M_reading || _M_writing)
1042 && __check_facet(_M_codecvt).encoding() == -1)
1043 __testvalid = false;
1048 if (__check_facet(_M_codecvt).always_noconv())
1051 && !__check_facet(_M_codecvt_tmp).always_noconv())
1052 __testvalid = this->seekoff(0, ios_base::cur, _M_mode)
1053 != pos_type(off_type(-1));
1057 // External position corresponding to gptr().
1058 _M_ext_next = _M_ext_buf
1059 + _M_codecvt->length(_M_state_last, _M_ext_buf,
1061 this->gptr() - this->eback());
1062 const streamsize __remainder = _M_ext_end - _M_ext_next;
1064 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
1066 _M_ext_next = _M_ext_buf;
1067 _M_ext_end = _M_ext_buf + __remainder;
1069 _M_state_last = _M_state_cur = _M_state_beg;
1072 else if (_M_writing && (__testvalid = _M_terminate_output()))
1078 _M_codecvt = _M_codecvt_tmp;
1083 // Inhibit implicit instantiations for required instantiations,
1084 // which are defined via explicit instantiations elsewhere.
1085 #if _GLIBCXX_EXTERN_TEMPLATE
1086 extern template class basic_filebuf<char>;
1087 extern template class basic_ifstream<char>;
1088 extern template class basic_ofstream<char>;
1089 extern template class basic_fstream<char>;
1091 #ifdef _GLIBCXX_USE_WCHAR_T
1092 extern template class basic_filebuf<wchar_t>;
1093 extern template class basic_ifstream<wchar_t>;
1094 extern template class basic_ofstream<wchar_t>;
1095 extern template class basic_fstream<wchar_t>;
1099 _GLIBCXX_END_NAMESPACE_VERSION