37#pragma GCC system_header
43namespace std _GLIBCXX_VISIBILITY(default)
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<
typename _CharT,
typename _Traits>
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>
70 _M_buf_allocated =
false;
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(),
89 _M_codecvt = std::__try_use_facet<__codecvt_type>(this->
_M_buf_locale);
92#if __cplusplus >= 201103L
93 template<
typename _CharT,
typename _Traits>
96 : __streambuf_type(__rhs),
97 _M_lock(), _M_file(
std::
move(__rhs._M_file), &_M_lock),
98 _M_mode(
std::__exchange(__rhs._M_mode,
ios_base::openmode(0))),
99 _M_state_beg(
std::
move(__rhs._M_state_beg)),
100 _M_state_cur(
std::
move(__rhs._M_state_cur)),
101 _M_state_last(
std::
move(__rhs._M_state_last)),
102 _M_buf(
std::__exchange(__rhs._M_buf, nullptr)),
103 _M_buf_size(
std::__exchange(__rhs._M_buf_size, 1)),
104 _M_buf_allocated(
std::__exchange(__rhs._M_buf_allocated, false)),
105 _M_reading(
std::__exchange(__rhs._M_reading, false)),
106 _M_writing(
std::__exchange(__rhs._M_writing, false)),
107 _M_pback(__rhs._M_pback),
108 _M_pback_cur_save(
std::__exchange(__rhs._M_pback_cur_save, nullptr)),
109 _M_pback_end_save(
std::__exchange(__rhs._M_pback_end_save, nullptr)),
110 _M_pback_init(
std::__exchange(__rhs._M_pback_init, false)),
111 _M_codecvt(__rhs._M_codecvt),
112 _M_ext_buf(
std::__exchange(__rhs._M_ext_buf, nullptr)),
113 _M_ext_buf_size(
std::__exchange(__rhs._M_ext_buf_size, 0)),
114 _M_ext_next(
std::__exchange(__rhs._M_ext_next, nullptr)),
115 _M_ext_end(
std::__exchange(__rhs._M_ext_end, nullptr))
117 __rhs._M_set_buffer(-1);
118 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
121 template<
typename _CharT,
typename _Traits>
122 basic_filebuf<_CharT, _Traits>&
123 basic_filebuf<_CharT, _Traits>::
124 operator=(basic_filebuf&& __rhs)
127 __streambuf_type::operator=(__rhs);
128 _M_file.swap(__rhs._M_file);
130 _M_state_beg =
std::move(__rhs._M_state_beg);
131 _M_state_cur =
std::move(__rhs._M_state_cur);
132 _M_state_last =
std::move(__rhs._M_state_last);
133 _M_buf = std::__exchange(__rhs._M_buf,
nullptr);
134 _M_buf_size = std::__exchange(__rhs._M_buf_size, 1);
135 _M_buf_allocated = std::__exchange(__rhs._M_buf_allocated,
false);
136 _M_ext_buf = std::__exchange(__rhs._M_ext_buf,
nullptr);
137 _M_ext_buf_size = std::__exchange(__rhs._M_ext_buf_size, 0);
138 _M_ext_next = std::__exchange(__rhs._M_ext_next,
nullptr);
139 _M_ext_end = std::__exchange(__rhs._M_ext_end,
nullptr);
140 _M_reading = std::__exchange(__rhs._M_reading,
false);
141 _M_writing = std::__exchange(__rhs._M_writing,
false);
142 _M_pback_cur_save = std::__exchange(__rhs._M_pback_cur_save,
nullptr);
143 _M_pback_end_save = std::__exchange(__rhs._M_pback_end_save,
nullptr);
144 _M_pback_init = std::__exchange(__rhs._M_pback_init,
false);
145 __rhs._M_set_buffer(-1);
146 __rhs._M_state_last = __rhs._M_state_cur = __rhs._M_state_beg;
150 template<
typename _CharT,
typename _Traits>
155 __streambuf_type::swap(__rhs);
156 _M_file.swap(__rhs._M_file);
157 std::swap(_M_mode, __rhs._M_mode);
158 std::swap(_M_state_beg, __rhs._M_state_beg);
159 std::swap(_M_state_cur, __rhs._M_state_cur);
160 std::swap(_M_state_last, __rhs._M_state_last);
161 std::swap(_M_buf, __rhs._M_buf);
162 std::swap(_M_buf_size, __rhs._M_buf_size);
163 std::swap(_M_buf_allocated, __rhs._M_buf_allocated);
164 std::swap(_M_ext_buf, __rhs._M_ext_buf);
165 std::swap(_M_ext_buf_size, __rhs._M_ext_buf_size);
166 std::swap(_M_ext_next, __rhs._M_ext_next);
167 std::swap(_M_ext_end, __rhs._M_ext_end);
168 std::swap(_M_reading, __rhs._M_reading);
169 std::swap(_M_writing, __rhs._M_writing);
170 std::swap(_M_pback_cur_save, __rhs._M_pback_cur_save);
171 std::swap(_M_pback_end_save, __rhs._M_pback_end_save);
172 std::swap(_M_pback_init, __rhs._M_pback_init);
176 template<
typename _CharT,
typename _Traits>
177 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
181 __filebuf_type *__ret = 0;
184 _M_file.open(__s, __mode);
187 _M_allocate_internal_buffer();
196 _M_state_last = _M_state_cur = _M_state_beg;
201 == pos_type(off_type(-1)))
210#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
211 template<
typename _CharT,
typename _Traits>
216 __filebuf_type *__ret = 0;
217 if (!this->is_open())
219 _M_file.open(__s, __mode);
222 _M_allocate_internal_buffer();
231 _M_state_last = _M_state_cur = _M_state_beg;
246 template<
typename _CharT,
typename _Traits>
247 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
254 bool __testfail =
false;
257 struct __close_sentry
265 __fb->_M_destroy_internal_buffer();
267 __fb->_M_writing =
false;
269 __fb->_M_state_last = __fb->_M_state_cur = __fb->_M_state_beg;
275 if (!_M_terminate_output())
281 __throw_exception_again;
285 if (!_M_file.close())
294 template<
typename _CharT,
typename _Traits>
301 if (__testin && this->
is_open())
307#if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM
310 if (__check_facet(_M_codecvt).encoding() >= 0
313 if (__check_facet(_M_codecvt).encoding() >= 0)
315 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
320 template<
typename _CharT,
typename _Traits>
321 typename basic_filebuf<_CharT, _Traits>::int_type
325 int_type __ret = traits_type::eof();
331 if (
overflow() == traits_type::eof())
342 return traits_type::to_int_type(*this->
gptr());
348 bool __got_eof =
false;
351 codecvt_base::result __r = codecvt_base::ok;
352 if (__check_facet(_M_codecvt).always_noconv())
354 __ilen = _M_file.xsgetn(
reinterpret_cast<char*
>(this->
eback()),
363 const int __enc = _M_codecvt->encoding();
367 __blen = __rlen = __buflen * __enc;
370 __blen = __buflen + _M_codecvt->max_length() - 1;
373 const streamsize __remainder = _M_ext_end - _M_ext_next;
374 __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
378 if (_M_reading && this->egptr() == this->eback() && __remainder)
383 if (_M_ext_buf_size < __blen)
385 char* __buf =
new char[__blen];
387 __builtin_memcpy(__buf, _M_ext_next, __remainder);
389 delete [] _M_ext_buf;
391 _M_ext_buf_size = __blen;
393 else if (__remainder)
394 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
396 _M_ext_next = _M_ext_buf;
397 _M_ext_end = _M_ext_buf + __remainder;
398 _M_state_last = _M_state_cur;
407 if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
409 __throw_ios_failure(__N(
"basic_filebuf::underflow "
410 "codecvt::max_length() "
413 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
416 else if (__elen == -1)
418 _M_ext_end += __elen;
421 char_type* __iend = this->
eback();
426 this->
eback() + __buflen, __iend);
427 if (__r == codecvt_base::noconv)
430 __ilen =
std::min(__avail, __buflen);
431 traits_type::copy(this->
eback(),
432 reinterpret_cast<char_type*
>
437 __ilen = __iend - this->eback();
442 if (__r == codecvt_base::error)
447 while (__ilen == 0 && !__got_eof);
452 _M_set_buffer(__ilen);
454 __ret = traits_type::to_int_type(*this->
gptr());
465 if (__r == codecvt_base::partial)
466 __throw_ios_failure(__N(
"basic_filebuf::underflow "
467 "incomplete character in file"));
469 else if (__r == codecvt_base::error)
470 __throw_ios_failure(__N(
"basic_filebuf::underflow "
471 "invalid byte sequence in file"));
473 __throw_ios_failure(__N(
"basic_filebuf::underflow "
474 "error reading the file"), errno);
479 template<
typename _CharT,
typename _Traits>
480 typename basic_filebuf<_CharT, _Traits>::int_type
484 int_type __ret = traits_type::eof();
490 if (
overflow() == traits_type::eof())
498 const bool __testeof = traits_type::eq_int_type(__i, __ret);
503 __tmp = traits_type::to_int_type(*this->
gptr());
508 if (traits_type::eq_int_type(__tmp, __ret))
523 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
526 __ret = traits_type::not_eof(__i);
531 *this->
gptr() = traits_type::to_char_type(__i);
538 template<
typename _CharT,
typename _Traits>
539 typename basic_filebuf<_CharT, _Traits>::int_type
543 int_type __ret = traits_type::eof();
544 const bool __testeof = traits_type::eq_int_type(__c, __ret);
552 const int __gptr_off = _M_get_ext_pos(_M_state_last);
554 == pos_type(off_type(-1)))
560 ~_Guard() {
if (_M_p) _M_p->pbump(-1); }
568 *this->
pptr() = traits_type::to_char_type(__c);
575 if (_M_convert_to_external(this->
pbase(),
580 __ret = traits_type::not_eof(__c);
592 *this->
pptr() = traits_type::to_char_type(__c);
595 __ret = traits_type::not_eof(__c);
600 char_type __conv = traits_type::to_char_type(__c);
601 if (__testeof || _M_convert_to_external(&__conv, 1))
604 __ret = traits_type::not_eof(__c);
611 template<
typename _CharT,
typename _Traits>
619 if (__check_facet(_M_codecvt).always_noconv())
621 __elen = _M_file.xsputn(
reinterpret_cast<char*
>(__ibuf), __ilen);
628 streamsize __blen = __ilen * _M_codecvt->max_length();
629 char* __buf =
static_cast<char*
>(__builtin_alloca(__blen));
632 const char_type* __iend;
633 codecvt_base::result __r;
634 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
635 __iend, __buf, __buf + __blen, __bend);
637 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
638 __blen = __bend - __buf;
639 else if (__r == codecvt_base::noconv)
642 __buf =
reinterpret_cast<char*
>(__ibuf);
646 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
647 "conversion error"));
649 __elen = _M_file.xsputn(__buf, __blen);
653 if (__r == codecvt_base::partial && __elen == __plen)
655 const char_type* __iresume = __iend;
657 __r = _M_codecvt->out(_M_state_cur, __iresume,
658 __iresume + __rlen, __iend, __buf,
659 __buf + __blen, __bend);
660 if (__r != codecvt_base::error)
662 __rlen = __bend - __buf;
663 __elen = _M_file.xsputn(__buf, __rlen);
667 __throw_ios_failure(__N(
"basic_filebuf::_M_convert_to_external "
668 "conversion error"));
671 return __elen == __plen;
674 template<
typename _CharT,
typename _Traits>
683 if (__n > 0 && this->
gptr() == this->
eback())
685 *__s++ = *this->
gptr();
694 if (
overflow() == traits_type::eof())
706 if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
713 traits_type::copy(__s, this->
gptr(), __avail);
725 __len = _M_file.xsgetn(
reinterpret_cast<char*
>(__s), __n);
727 __throw_ios_failure(__N(
"basic_filebuf::xsgetn "
728 "error reading the file"), errno);
760 template<
typename _CharT,
typename _Traits>
771 if (__check_facet(_M_codecvt).always_noconv()
780 if (__n >= __bufavail)
783 const char* __buf =
reinterpret_cast<const char*
>(this->
pbase());
784 __ret = _M_file.xsputn_2(__buf, __buffill,
785 reinterpret_cast<const char*
>(__s),
787 if (__ret == __buffill + __n)
792 if (__ret > __buffill)
805 template<
typename _CharT,
typename _Traits>
806 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
812 if (__s == 0 && __n == 0)
814 else if (__s && __n > 0)
834 template<
typename _CharT,
typename _Traits>
835 typename basic_filebuf<_CharT, _Traits>::pos_type
841 __width = _M_codecvt->encoding();
845 pos_type __ret = pos_type(off_type(-1));
846 const bool __testfail = __off != 0 && __width <= 0;
847 if (this->
is_open() && !__testfail)
854 && (!_M_writing || _M_codecvt->always_noconv());
865 __state_type __state = _M_state_beg;
866 off_type __computed_off = __off * __width;
869 __state = _M_state_last;
870 __computed_off += _M_get_ext_pos(__state);
873 __ret = _M_seek(__computed_off, __way, __state);
877 __computed_off = this->
pptr() - this->
pbase();
880 if (__file_off != off_type(-1))
882 __ret = __file_off + __computed_off;
883 __ret.state(__state);
894 template<
typename _CharT,
typename _Traits>
895 typename basic_filebuf<_CharT, _Traits>::pos_type
899 pos_type __ret = pos_type(off_type(-1));
904 __ret = _M_seek(off_type(__pos),
ios_base::beg, __pos.state());
909 template<
typename _CharT,
typename _Traits>
910 typename basic_filebuf<_CharT, _Traits>::pos_type
915 if (_M_terminate_output())
917 off_type __file_off = _M_file.seekoff(__off, __way);
922 _M_ext_next = _M_ext_end = _M_ext_buf;
924 _M_state_cur = __state;
926 __ret.state(_M_state_cur);
935 template<
typename _CharT,
typename _Traits>
939 if (_M_codecvt->always_noconv())
940 return this->gptr() - this->egptr();
946 const int __gptr_off =
947 _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
948 this->gptr() - this->eback());
949 return _M_ext_buf + __gptr_off - _M_ext_end;
953 template<
typename _CharT,
typename _Traits>
959 bool __testvalid =
true;
960 if (this->pbase() < this->pptr())
962 const int_type __tmp = this->overflow();
963 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
968 if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
974 const size_t __blen = 128;
976 codecvt_base::result __r;
982 __r = _M_codecvt->unshift(_M_state_cur, __buf,
983 __buf + __blen, __next);
984 if (__r == codecvt_base::error)
986 else if (__r == codecvt_base::ok ||
987 __r == codecvt_base::partial)
989 __ilen = __next - __buf;
992 const streamsize __elen = _M_file.xsputn(__buf, __ilen);
993 if (__elen != __ilen)
998 while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
1006 const int_type __tmp = this->overflow();
1007 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1008 __testvalid =
false;
1014 template<
typename _CharT,
typename _Traits>
1024 const int_type __tmp = this->
overflow();
1025 if (traits_type::eq_int_type(__tmp, traits_type::eof()))
1031 template<
typename _CharT,
typename _Traits>
1036 bool __testvalid =
true;
1038 const __codecvt_type*
const _M_codecvt_tmp
1039 = __try_use_facet<__codecvt_type>(__loc);
1045 && __check_facet(_M_codecvt).encoding() == -1)
1046 __testvalid =
false;
1051 if (__check_facet(_M_codecvt).always_noconv())
1054 && !__check_facet(_M_codecvt_tmp).always_noconv())
1056 != pos_type(off_type(-1));
1062 + _M_codecvt->length(_M_state_last,
_M_ext_buf,
1072 _M_state_last = _M_state_cur = _M_state_beg;
1075 else if (_M_writing && (__testvalid = _M_terminate_output()))
1081 _M_codecvt = _M_codecvt_tmp;
1088#if _GLIBCXX_EXTERN_TEMPLATE
1094#ifdef _GLIBCXX_USE_WCHAR_T
1102_GLIBCXX_END_NAMESPACE_VERSION
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.