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>
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>
75 _M_buf_allocated =
false;
84 template<
typename _CharT,
typename _Traits>
86 basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock),
87 _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
94 _M_codecvt = std::__try_use_facet<__codecvt_type>(this->
_M_buf_locale);
97#if __cplusplus >= 201103L
98 template<
typename _CharT,
typename _Traits>
101 : __streambuf_type(__rhs),
102 _M_lock(), _M_file(
std::
move(__rhs._M_file), &_M_lock),
103 _M_mode(
std::__exchange(__rhs._M_mode,
ios_base::openmode(0))),
104 _M_state_beg(
std::
move(__rhs._M_state_beg)),
105 _M_state_cur(
std::
move(__rhs._M_state_cur)),
106 _M_state_last(
std::
move(__rhs._M_state_last)),
107 _M_buf(
std::__exchange(__rhs._M_buf, nullptr)),
108 _M_buf_size(
std::__exchange(__rhs._M_buf_size, 1)),
109 _M_buf_allocated(
std::__exchange(__rhs._M_buf_allocated, false)),
110 _M_reading(
std::__exchange(__rhs._M_reading, false)),
111 _M_writing(
std::__exchange(__rhs._M_writing, false)),
112 _M_pback(__rhs._M_pback),
113 _M_pback_cur_save(
std::__exchange(__rhs._M_pback_cur_save, nullptr)),
114 _M_pback_end_save(
std::__exchange(__rhs._M_pback_end_save, nullptr)),
115 _M_pback_init(
std::__exchange(__rhs._M_pback_init, false)),
116 _M_codecvt(__rhs._M_codecvt),
117 _M_ext_buf(
std::__exchange(__rhs._M_ext_buf, nullptr)),
118 _M_ext_buf_size(
std::__exchange(__rhs._M_ext_buf_size, 0)),
119 _M_ext_next(
std::__exchange(__rhs._M_ext_next, nullptr)),
120 _M_ext_end(
std::__exchange(__rhs._M_ext_end, nullptr))
122 __rhs._M_set_buffer(-1);
123 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
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>
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*
186 __filebuf_type *__ret = 0;
189 _M_file.open(__s, __mode);
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())
224 _M_file.open(__s, __mode);
227 _M_allocate_internal_buffer();
236 _M_state_last = _M_state_cur = _M_state_beg;
251 template<
typename _CharT,
typename _Traits>
252 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
259 bool __testfail =
false;
262 struct __close_sentry
270 __fb->_M_destroy_internal_buffer();
272 __fb->_M_writing =
false;
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>
306 if (__testin && this->
is_open())
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())
347 return traits_type::to_int_type(*this->
gptr());
353 bool __got_eof =
false;
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();
372 __blen = __rlen = __buflen * __enc;
375 __blen = __buflen + _M_codecvt->max_length() - 1;
378 const streamsize __remainder = _M_ext_end - _M_ext_next;
379 __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
383 if (_M_reading && this->egptr() == this->eback() && __remainder)
388 if (_M_ext_buf_size < __blen)
390 char* __buf =
new char[__blen];
392 __builtin_memcpy(__buf, _M_ext_next, __remainder);
394 delete [] _M_ext_buf;
396 _M_ext_buf_size = __blen;
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() "
418 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
421 else if (__elen == -1)
423 _M_ext_end += __elen;
431 this->
eback() + __buflen, __iend);
432 if (__r == codecvt_base::noconv)
435 __ilen =
std::min(__avail, __buflen);
437 reinterpret_cast<char_type*
>
442 __ilen = __iend - this->eback();
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())
503 const bool __testeof = traits_type::eq_int_type(__i, __ret);
508 __tmp = traits_type::to_int_type(*this->
gptr());
513 if (traits_type::eq_int_type(__tmp, __ret))
528 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
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();
549 const bool __testeof = traits_type::eq_int_type(__c, __ret);
557 const int __gptr_off = _M_get_ext_pos(_M_state_last);
559 == pos_type(off_type(-1)))
565 ~_Guard() {
if (_M_p) _M_p->pbump(-1); }
573 *this->
pptr() = traits_type::to_char_type(__c);
580 if (_M_convert_to_external(this->
pbase(),
585 __ret = traits_type::not_eof(__c);
597 *this->
pptr() = traits_type::to_char_type(__c);
600 __ret = traits_type::not_eof(__c);
605 char_type __conv = traits_type::to_char_type(__c);
606 if (__testeof || _M_convert_to_external(&__conv, 1))
609 __ret = traits_type::not_eof(__c);
616 template<
typename _CharT,
typename _Traits>
624 if (__check_facet(_M_codecvt).always_noconv())
626 __elen = _M_file.xsputn(
reinterpret_cast<char*
>(__ibuf), __ilen);
633 streamsize __blen = __ilen * _M_codecvt->max_length();
634 char* __buf =
static_cast<char*
>(__builtin_alloca(__blen));
637 const char_type* __iend;
638 codecvt_base::result __r;
639 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
640 __iend, __buf, __buf + __blen, __bend);
642 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
643 __blen = __bend - __buf;
644 else if (__r == codecvt_base::noconv)
647 __buf =
reinterpret_cast<char*
>(__ibuf);
651 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
652 "conversion error"));
654 __elen = _M_file.xsputn(__buf, __blen);
658 if (__r == codecvt_base::partial && __elen == __plen)
660 const char_type* __iresume = __iend;
662 __r = _M_codecvt->out(_M_state_cur, __iresume,
663 __iresume + __rlen, __iend, __buf,
664 __buf + __blen, __bend);
665 if (__r != codecvt_base::error)
667 __rlen = __bend - __buf;
668 __elen = _M_file.xsputn(__buf, __rlen);
672 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
673 "conversion error"));
676 return __elen == __plen;
679 template<
typename _CharT,
typename _Traits>
688 if (__n > 0 && this->
gptr() == this->
eback())
690 *__s++ = *this->
gptr();
699 if (
overflow() == traits_type::eof())
711 if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
718 traits_type::copy(__s, this->
gptr(), __avail);
730 __len = _M_file.xsgetn(
reinterpret_cast<char*
>(__s), __n);
732 __throw_ios_failure(__N(
"basic_filebuf::xsgetn "
733 "error reading the file"), errno);
765 template<
typename _CharT,
typename _Traits>
776 if (__check_facet(_M_codecvt).always_noconv()
785 if (__n >= __bufavail)
788 const char* __buf =
reinterpret_cast<const char*
>(this->
pbase());
789 __ret = _M_file.xsputn_2(__buf, __buffill,
790 reinterpret_cast<const char*
>(__s),
792 if (__ret == __buffill + __n)
797 if (__ret > __buffill)
810 template<
typename _CharT,
typename _Traits>
811 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
817 if (__s == 0 && __n == 0)
819 else if (__s && __n > 0)
839 template<
typename _CharT,
typename _Traits>
840 typename basic_filebuf<_CharT, _Traits>::pos_type
846 __width = _M_codecvt->encoding();
850 pos_type __ret = pos_type(off_type(-1));
851 const bool __testfail = __off != 0 && __width <= 0;
852 if (this->
is_open() && !__testfail)
859 && (!_M_writing || _M_codecvt->always_noconv());
870 __state_type __state = _M_state_beg;
871 off_type __computed_off = __off * __width;
874 __state = _M_state_last;
875 __computed_off += _M_get_ext_pos(__state);
878 __ret = _M_seek(__computed_off, __way, __state);
882 __computed_off = this->
pptr() - this->
pbase();
885 if (__file_off != off_type(-1))
887 __ret = __file_off + __computed_off;
888 __ret.state(__state);
899 template<
typename _CharT,
typename _Traits>
900 typename basic_filebuf<_CharT, _Traits>::pos_type
904 pos_type __ret = pos_type(off_type(-1));
909 __ret = _M_seek(off_type(__pos),
ios_base::beg, __pos.state());
914 template<
typename _CharT,
typename _Traits>
915 typename basic_filebuf<_CharT, _Traits>::pos_type
920 if (_M_terminate_output())
922 off_type __file_off = _M_file.seekoff(__off, __way);
927 _M_ext_next = _M_ext_end = _M_ext_buf;
929 _M_state_cur = __state;
931 __ret.state(_M_state_cur);
940 template<
typename _CharT,
typename _Traits>
944 if (_M_codecvt->always_noconv())
945 return this->gptr() - this->egptr();
951 const int __gptr_off =
952 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
953 this->gptr() - this->eback());
954 return _M_ext_buf + __gptr_off - _M_ext_end;
958 template<
typename _CharT,
typename _Traits>
964 bool __testvalid =
true;
965 if (this->pbase() < this->pptr())
967 const int_type __tmp = this->overflow();
968 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
973 if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
979 const size_t __blen = 128;
981 codecvt_base::result __r;
987 __r = _M_codecvt->unshift(_M_state_cur, __buf,
988 __buf + __blen, __next);
989 if (__r == codecvt_base::error)
991 else if (__r == codecvt_base::ok ||
992 __r == codecvt_base::partial)
994 __ilen = __next - __buf;
997 const streamsize __elen = _M_file.xsputn(__buf, __ilen);
998 if (__elen != __ilen)
1003 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
1011 const int_type __tmp = this->overflow();
1012 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1013 __testvalid =
false;
1019 template<
typename _CharT,
typename _Traits>
1029 const int_type __tmp = this->
overflow();
1030 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1036 template<
typename _CharT,
typename _Traits>
1041 bool __testvalid =
true;
1043 const __codecvt_type*
const _M_codecvt_tmp
1044 = __try_use_facet<__codecvt_type>(__loc);
1050 && __check_facet(_M_codecvt).encoding() == -1)
1051 __testvalid =
false;
1056 if (__check_facet(_M_codecvt).always_noconv())
1059 && !__check_facet(_M_codecvt_tmp).always_noconv())
1061 != pos_type(off_type(-1));
1067 + _M_codecvt->length(_M_state_last,
_M_ext_buf,
1077 _M_state_last = _M_state_cur = _M_state_beg;
1080 else if (_M_writing && (__testvalid = _M_terminate_output()))
1086 _M_codecvt = _M_codecvt_tmp;
1093#if _GLIBCXX_EXTERN_TEMPLATE
1099#ifdef _GLIBCXX_USE_WCHAR_T
1107_GLIBCXX_END_NAMESPACE_VERSION
1110#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.
ios_base::openmode _M_mode
Place to stash in || out || in | out settings for current filebuf.
streamsize _M_ext_buf_size
char_type * _M_pback_cur_save
virtual void imbue(const locale &__loc)
Changes translations.
virtual streamsize xsgetn(char_type *__s, streamsize __n)
Multiple character extraction.
bool is_open() const
Returns true if the external file is open.
__filebuf_type * close()
Closes the currently associated file.
char_type * _M_pback_end_save
virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode=ios_base::in|ios_base::out)
Alters the stream positions.
char_type * _M_buf
Pointer to the beginning of internal buffer.
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.
void _M_set_buffer(streamsize __off)
virtual int sync()
Synchronizes the buffer arrays with the controlled sequences.
virtual __streambuf_type * setbuf(char_type *__s, streamsize __n)
Manipulates the buffer.
Controlling input for files.
Controlling output for files.
Controlling input and output for files.
char_type * epptr() const
Access to the put area.
virtual streamsize xsputn(const char_type *__s, streamsize __n)
char_type * pptr() const
Access to the put area.
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
char_type * eback() const
Access to the get area.
char_type * egptr() const
Access to the get area.
char_type * gptr() const
Access to the get area.
virtual streamsize xsgetn(char_type *__s, streamsize __n)
void gbump(int __n)
Moving the read position.
void pbump(int __n)
Moving the write position.
char_type * pbase() const
Access to the put area.
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.
_Ios_Seekdir seekdir
This is an enumerated type.
static const openmode ate
Open and seek to end immediately after opening.
Container class for localization functionality.