38#pragma GCC system_header
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wc++11-extensions"
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<
typename _CharT,
typename _Traits>
54 basic_filebuf<_CharT, _Traits>::
55 _M_allocate_internal_buffer()
59 if (!_M_buf_allocated && !_M_buf)
61 _M_buf =
new char_type[_M_buf_size];
62 _M_buf_allocated =
true;
66 template<
typename _CharT,
typename _Traits>
68 basic_filebuf<_CharT, _Traits>::
69 _M_destroy_internal_buffer() throw()
75 _M_buf_allocated =
false;
84 template<
typename _CharT,
typename _Traits>
87 _M_mode(
ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
88 _M_state_last(), _M_buf(0), _M_buf_size(_GLIBCXX_BUFSIZ),
89 _M_buf_allocated(
false), _M_reading(
false), _M_writing(
false), _M_pback(),
90 _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(
false),
91 _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
97#if __cplusplus >= 201103L
98 template<
typename _CharT,
typename _Traits>
101 : __streambuf_type(
__rhs),
108 _M_buf_size(
std::__exchange(
__rhs._M_buf_size, 1)),
109 _M_buf_allocated(
std::__exchange(
__rhs._M_buf_allocated,
false)),
112 _M_pback(
__rhs._M_pback),
116 _M_codecvt(
__rhs._M_codecvt),
118 _M_ext_buf_size(
std::__exchange(
__rhs._M_ext_buf_size, 0)),
122 __rhs._M_set_buffer(-1);
126 template<
typename _CharT,
typename _Traits>
127 basic_filebuf<_CharT, _Traits>&
128 basic_filebuf<_CharT, _Traits>::
129 operator=(basic_filebuf&& __rhs)
132 __streambuf_type::operator=(__rhs);
133 _M_file.swap(__rhs._M_file);
135 _M_state_beg =
std::move(__rhs._M_state_beg);
136 _M_state_cur =
std::move(__rhs._M_state_cur);
137 _M_state_last =
std::move(__rhs._M_state_last);
138 _M_buf = std::__exchange(__rhs._M_buf,
nullptr);
139 _M_buf_size = std::__exchange(__rhs._M_buf_size, 1);
140 _M_buf_allocated = std::__exchange(__rhs._M_buf_allocated,
false);
141 _M_ext_buf = std::__exchange(__rhs._M_ext_buf,
nullptr);
142 _M_ext_buf_size = std::__exchange(__rhs._M_ext_buf_size, 0);
143 _M_ext_next = std::__exchange(__rhs._M_ext_next,
nullptr);
144 _M_ext_end = std::__exchange(__rhs._M_ext_end,
nullptr);
145 _M_reading = std::__exchange(__rhs._M_reading,
false);
146 _M_writing = std::__exchange(__rhs._M_writing,
false);
147 _M_pback_cur_save = std::__exchange(__rhs._M_pback_cur_save,
nullptr);
148 _M_pback_end_save = std::__exchange(__rhs._M_pback_end_save,
nullptr);
149 _M_pback_init = std::__exchange(__rhs._M_pback_init,
false);
150 __rhs._M_set_buffer(-1);
151 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
155 template<
typename _CharT,
typename _Traits>
157 basic_filebuf<_CharT, _Traits>::
158 swap(basic_filebuf& __rhs)
160 __streambuf_type::swap(__rhs);
161 _M_file.swap(__rhs._M_file);
162 std::swap(_M_mode, __rhs._M_mode);
163 std::swap(_M_state_beg, __rhs._M_state_beg);
164 std::swap(_M_state_cur, __rhs._M_state_cur);
165 std::swap(_M_state_last, __rhs._M_state_last);
166 std::swap(_M_buf, __rhs._M_buf);
167 std::swap(_M_buf_size, __rhs._M_buf_size);
168 std::swap(_M_buf_allocated, __rhs._M_buf_allocated);
169 std::swap(_M_ext_buf, __rhs._M_ext_buf);
170 std::swap(_M_ext_buf_size, __rhs._M_ext_buf_size);
171 std::swap(_M_ext_next, __rhs._M_ext_next);
172 std::swap(_M_ext_end, __rhs._M_ext_end);
173 std::swap(_M_reading, __rhs._M_reading);
174 std::swap(_M_writing, __rhs._M_writing);
175 std::swap(_M_pback_cur_save, __rhs._M_pback_cur_save);
176 std::swap(_M_pback_end_save, __rhs._M_pback_end_save);
177 std::swap(_M_pback_init, __rhs._M_pback_init);
181 template<
typename _CharT,
typename _Traits>
182 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
187 if (!this->is_open())
192 _M_allocate_internal_buffer();
201 _M_state_last = _M_state_cur = _M_state_beg;
206 == pos_type(off_type(-1)))
215#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
216 template<
typename _CharT,
typename _Traits>
221 __filebuf_type *
__ret = 0;
222 if (!this->is_open())
227 _M_allocate_internal_buffer();
236 _M_state_last = _M_state_cur = _M_state_beg;
241 == pos_type(off_type(-1)))
251 template<
typename _CharT,
typename _Traits>
256 if (!this->is_open())
269 __fb->_M_pback_init =
false;
270 __fb->_M_destroy_internal_buffer();
271 __fb->_M_reading =
false;
272 __fb->_M_writing =
false;
273 __fb->_M_set_buffer(-1);
274 __fb->_M_state_last =
__fb->_M_state_cur =
__fb->_M_state_beg;
280 if (!_M_terminate_output())
286 __throw_exception_again;
290 if (!_M_file.close())
299 template<
typename _CharT,
typename _Traits>
310 __ret = this->egptr() - this->gptr();
312#if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM
315 if (__check_facet(_M_codecvt).encoding() >= 0
318 if (__check_facet(_M_codecvt).encoding() >= 0)
320 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
325 template<
typename _CharT,
typename _Traits>
326 typename basic_filebuf<_CharT, _Traits>::int_type
330 int_type
__ret = traits_type::eof();
336 if (overflow() == traits_type::eof())
346 if (this->gptr() < this->egptr())
347 return traits_type::to_int_type(*this->gptr());
350 const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
356 codecvt_base::result __r = codecvt_base::ok;
357 if (__check_facet(_M_codecvt).always_noconv())
359 __ilen = _M_file.xsgetn(
reinterpret_cast<char*
>(this->eback()),
368 const int __enc = _M_codecvt->encoding();
383 if (_M_reading && this->egptr() == this->eback() &&
__remainder)
388 if (_M_ext_buf_size <
__blen)
394 delete [] _M_ext_buf;
398 else if (__remainder)
399 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
401 _M_ext_next = _M_ext_buf;
402 _M_ext_end = _M_ext_buf + __remainder;
403 _M_state_last = _M_state_cur;
412 if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
414 __throw_ios_failure(__N(
"basic_filebuf::underflow "
415 "codecvt::max_length() "
427 if (_M_ext_next < _M_ext_end)
428 __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
429 _M_ext_end, _M_ext_next,
432 if (__r == codecvt_base::noconv)
434 size_t __avail = _M_ext_end - _M_ext_buf;
436 traits_type::copy(this->eback(),
437 reinterpret_cast<char_type*
>
439 _M_ext_next = _M_ext_buf +
__ilen;
447 if (__r == codecvt_base::error)
452 while (__ilen == 0 && !__got_eof);
459 __ret = traits_type::to_int_type(*this->gptr());
470 if (__r == codecvt_base::partial)
471 __throw_ios_failure(__N(
"basic_filebuf::underflow "
472 "incomplete character in file"));
474 else if (__r == codecvt_base::error)
475 __throw_ios_failure(__N(
"basic_filebuf::underflow "
476 "invalid byte sequence in file"));
478 __throw_ios_failure(__N(
"basic_filebuf::underflow "
479 "error reading the file"), errno);
484 template<
typename _CharT,
typename _Traits>
485 typename basic_filebuf<_CharT, _Traits>::int_type
489 int_type
__ret = traits_type::eof();
495 if (overflow() == traits_type::eof())
502 const bool __testpb = _M_pback_init;
505 if (this->eback() < this->gptr())
508 __tmp = traits_type::to_int_type(*this->gptr());
510 else if (this->seekoff(-1,
ios_base::cur) != pos_type(off_type(-1)))
512 __tmp = this->underflow();
531 __ret = traits_type::not_eof(__i);
536 *this->gptr() = traits_type::to_char_type(__i);
543 template<
typename _CharT,
typename _Traits>
544 typename basic_filebuf<_CharT, _Traits>::int_type
548 int_type
__ret = traits_type::eof();
557 const int __gptr_off = _M_get_ext_pos(_M_state_last);
559 == pos_type(off_type(-1)))
562 if (this->pbase() < this->pptr())
567 *this->pptr() = traits_type::to_char_type(__c);
573 if (_M_convert_to_external(this->pbase(),
574 this->pptr() - this->pbase()))
577 __ret = traits_type::not_eof(__c);
580 else if (_M_buf_size > 1)
589 *this->pptr() = traits_type::to_char_type(__c);
592 __ret = traits_type::not_eof(__c);
597 char_type
__conv = traits_type::to_char_type(__c);
601 __ret = traits_type::not_eof(__c);
608 template<
typename _CharT,
typename _Traits>
616 if (__check_facet(_M_codecvt).always_noconv())
625 streamsize __blen = __ilen * _M_codecvt->max_length();
626 char* __buf =
static_cast<char*
>(__builtin_alloca(__blen));
629 const char_type* __iend;
630 codecvt_base::result __r;
631 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
632 __iend, __buf, __buf + __blen, __bend);
634 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
635 __blen = __bend - __buf;
636 else if (__r == codecvt_base::noconv)
639 __buf =
reinterpret_cast<char*
>(__ibuf);
643 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
644 "conversion error"));
646 __elen = _M_file.xsputn(__buf, __blen);
650 if (__r == codecvt_base::partial && __elen == __plen)
652 const char_type* __iresume = __iend;
654 __r = _M_codecvt->out(_M_state_cur, __iresume,
655 __iresume + __rlen, __iend, __buf,
656 __buf + __blen, __bend);
657 if (__r != codecvt_base::error)
659 __rlen = __bend - __buf;
660 __elen = _M_file.xsputn(__buf, __rlen);
664 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
665 "conversion error"));
668 return __elen == __plen;
671 template<
typename _CharT,
typename _Traits>
680 if (__n > 0 && this->gptr() == this->eback())
682 *
__s++ = *this->gptr();
691 if (overflow() == traits_type::eof())
703 if (__n >
__buflen && __check_facet(_M_codecvt).always_noconv()
710 traits_type::copy(
__s, this->gptr(),
__avail);
712 this->setg(this->eback(), this->gptr() +
__avail, this->egptr());
722 __len = _M_file.xsgetn(
reinterpret_cast<char*
>(
__s), __n);
724 __throw_ios_failure(__N(
"basic_filebuf::xsgetn "
725 "error reading the file"), errno);
752 __ret += __streambuf_type::xsgetn(
__s, __n);
757 template<
typename _CharT,
typename _Traits>
768 if (__check_facet(_M_codecvt).always_noconv()
774 if (!_M_writing && _M_buf_size > 1)
780 const char*
__buf =
reinterpret_cast<const char*
>(this->pbase());
782 reinterpret_cast<const char*
>(
__s),
795 __ret = __streambuf_type::xsputn(
__s, __n);
798 __ret = __streambuf_type::xsputn(
__s, __n);
802 template<
typename _CharT,
typename _Traits>
807 if (!this->is_open())
809 if (
__s == 0 && __n == 0)
811 else if (
__s && __n > 0)
831 template<
typename _CharT,
typename _Traits>
832 typename basic_filebuf<_CharT, _Traits>::pos_type
838 __width = _M_codecvt->encoding();
842 pos_type
__ret = pos_type(off_type(-1));
851 && (!_M_writing || _M_codecvt->always_noconv());
862 __state_type
__state = _M_state_beg;
891 template<
typename _CharT,
typename _Traits>
892 typename basic_filebuf<_CharT, _Traits>::pos_type
896 pos_type
__ret = pos_type(off_type(-1));
906 template<
typename _CharT,
typename _Traits>
907 typename basic_filebuf<_CharT, _Traits>::pos_type
911 pos_type
__ret = pos_type(off_type(-1));
912 if (_M_terminate_output())
919 _M_ext_next = _M_ext_end = _M_ext_buf;
923 __ret.state(_M_state_cur);
932 template<
typename _CharT,
typename _Traits>
933 int basic_filebuf<_CharT, _Traits>::
934 _M_get_ext_pos(__state_type& __state)
936 if (_M_codecvt->always_noconv())
937 return this->gptr() - this->egptr();
943 const int __gptr_off =
944 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
945 this->gptr() - this->eback());
946 return _M_ext_buf + __gptr_off - _M_ext_end;
950 template<
typename _CharT,
typename _Traits>
952 basic_filebuf<_CharT, _Traits>::
953 _M_terminate_output()
956 bool __testvalid =
true;
957 if (this->pbase() < this->pptr())
959 const int_type __tmp = this->overflow();
960 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
965 if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
971 const size_t __blen = 128;
973 codecvt_base::result __r;
979 __r = _M_codecvt->unshift(_M_state_cur, __buf,
980 __buf + __blen, __next);
981 if (__r == codecvt_base::error)
983 else if (__r == codecvt_base::ok ||
984 __r == codecvt_base::partial)
986 __ilen = __next - __buf;
989 const streamsize __elen = _M_file.xsputn(__buf, __ilen);
990 if (__elen != __ilen)
995 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
1003 const int_type __tmp = this->overflow();
1004 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1005 __testvalid =
false;
1011 template<
typename _CharT,
typename _Traits>
1019 if (this->pbase() < this->pptr())
1021 const int_type
__tmp = this->overflow();
1022 if (traits_type::eq_int_type(
__tmp, traits_type::eof()))
1028 template<
typename _CharT,
typename _Traits>
1038 if (this->is_open())
1041 if ((_M_reading || _M_writing)
1042 && __check_facet(_M_codecvt).encoding() == -1)
1048 if (__check_facet(_M_codecvt).always_noconv())
1053 != pos_type(off_type(-1));
1058 _M_ext_next = _M_ext_buf
1059 + _M_codecvt->length(_M_state_last, _M_ext_buf,
1061 this->gptr() - this->eback());
1066 _M_ext_next = _M_ext_buf;
1069 _M_state_last = _M_state_cur = _M_state_beg;
1072 else if (_M_writing && (
__testvalid = _M_terminate_output()))
1085#if _GLIBCXX_EXTERN_TEMPLATE
1091#ifdef _GLIBCXX_USE_WCHAR_T
1099_GLIBCXX_END_NAMESPACE_VERSION
1102#pragma GCC diagnostic pop
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
The actual work of input and output (for files).
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.
virtual int_type underflow()
Fetches more data from the controlled sequence.
virtual streamsize showmanyc()
Investigating the data available.
virtual void imbue(const locale &__loc)
Changes translations.
virtual streamsize xsgetn(char_type *__s, streamsize __n)
Multiple character extraction.
__filebuf_type * close()
Closes the currently associated file.
virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
virtual int_type pbackfail(int_type __c=_Traits::eof())
Tries to back up the input sequence.
virtual streamsize xsputn(const char_type *__s, streamsize __n)
Multiple character insertion.
virtual int_type overflow(int_type __c=_Traits::eof())
Consumes data from the buffer; writes to the controlled sequence.
__filebuf_type * open(const char *__s, ios_base::openmode __mode)
Opens an external file.
basic_filebuf()
Does not open any files.
virtual int sync()
Synchronizes the buffer arrays with the controlled sequences.
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
locale _M_buf_locale
Current locale setting.
The base of the I/O class hierarchy.
static const seekdir cur
Request a seek relative to the current position within the sequence.
static const seekdir beg
Request a seek relative to the beginning of the stream.
static const seekdir end
Request a seek relative to the current end of the sequence.
static const openmode in
Open for input. Default for ifstream and fstream.
static const openmode out
Open for output. Default for ofstream and fstream.
static const openmode binary
Perform input and output in binary mode (as opposed to text mode). This is probably not what you thin...
_Ios_Openmode openmode
This is a bitmask type.
static const openmode app
Seek to end before each write.
static const openmode ate
Open and seek to end immediately after opening.
Container class for localization functionality.