2 //===------------------------- fstream ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FSTREAM
12 #define _LIBCPP_FSTREAM
17 template <class charT, class traits = char_traits<charT> >
19 : public basic_streambuf<charT, traits>
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
28 // 27.9.1.2 Constructors/destructor:
30 basic_filebuf(basic_filebuf&& rhs);
31 virtual ~basic_filebuf();
33 // 27.9.1.3 Assign/swap:
34 basic_filebuf& operator=(basic_filebuf&& rhs);
35 void swap(basic_filebuf& rhs);
39 basic_filebuf* open(const char* s, ios_base::openmode mode);
40 basic_filebuf* open(const string& s, ios_base::openmode mode);
41 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
42 basic_filebuf* close();
45 // 27.9.1.5 Overridden virtual functions:
46 virtual streamsize showmanyc();
47 virtual int_type underflow();
48 virtual int_type uflow();
49 virtual int_type pbackfail(int_type c = traits_type::eof());
50 virtual int_type overflow (int_type c = traits_type::eof());
51 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
52 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
53 ios_base::openmode which = ios_base::in | ios_base::out);
54 virtual pos_type seekpos(pos_type sp,
55 ios_base::openmode which = ios_base::in | ios_base::out);
57 virtual void imbue(const locale& loc);
60 template <class charT, class traits>
62 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
64 typedef basic_filebuf<char> filebuf;
65 typedef basic_filebuf<wchar_t> wfilebuf;
67 template <class charT, class traits = char_traits<charT> >
69 : public basic_istream<charT,traits>
72 typedef charT char_type;
73 typedef traits traits_type;
74 typedef typename traits_type::int_type int_type;
75 typedef typename traits_type::pos_type pos_type;
76 typedef typename traits_type::off_type off_type;
79 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
80 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
81 explicit basic_ifstream(const filesystem::path& p,
82 ios_base::openmode mode = ios_base::in); // C++17
83 basic_ifstream(basic_ifstream&& rhs);
85 basic_ifstream& operator=(basic_ifstream&& rhs);
86 void swap(basic_ifstream& rhs);
88 basic_filebuf<char_type, traits_type>* rdbuf() const;
90 void open(const char* s, ios_base::openmode mode = ios_base::in);
91 void open(const string& s, ios_base::openmode mode = ios_base::in);
92 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
97 template <class charT, class traits>
99 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
101 typedef basic_ifstream<char> ifstream;
102 typedef basic_ifstream<wchar_t> wifstream;
104 template <class charT, class traits = char_traits<charT> >
106 : public basic_ostream<charT,traits>
109 typedef charT char_type;
110 typedef traits traits_type;
111 typedef typename traits_type::int_type int_type;
112 typedef typename traits_type::pos_type pos_type;
113 typedef typename traits_type::off_type off_type;
116 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
117 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
118 explicit basic_ofstream(const filesystem::path& p,
119 ios_base::openmode mode = ios_base::out); // C++17
120 basic_ofstream(basic_ofstream&& rhs);
122 basic_ofstream& operator=(basic_ofstream&& rhs);
123 void swap(basic_ofstream& rhs);
125 basic_filebuf<char_type, traits_type>* rdbuf() const;
126 bool is_open() const;
127 void open(const char* s, ios_base::openmode mode = ios_base::out);
128 void open(const string& s, ios_base::openmode mode = ios_base::out);
129 void open(const filesystem::path& p,
130 ios_base::openmode mode = ios_base::out); // C++17
135 template <class charT, class traits>
137 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
139 typedef basic_ofstream<char> ofstream;
140 typedef basic_ofstream<wchar_t> wofstream;
142 template <class charT, class traits=char_traits<charT> >
144 : public basic_iostream<charT,traits>
147 typedef charT char_type;
148 typedef traits traits_type;
149 typedef typename traits_type::int_type int_type;
150 typedef typename traits_type::pos_type pos_type;
151 typedef typename traits_type::off_type off_type;
154 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
155 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
156 explicit basic_fstream(const filesystem::path& p,
157 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
158 basic_fstream(basic_fstream&& rhs);
160 basic_fstream& operator=(basic_fstream&& rhs);
161 void swap(basic_fstream& rhs);
163 basic_filebuf<char_type, traits_type>* rdbuf() const;
164 bool is_open() const;
165 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
166 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
167 void open(const filesystem::path& s,
168 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
173 template <class charT, class traits>
174 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
176 typedef basic_fstream<char> fstream;
177 typedef basic_fstream<wchar_t> wfstream;
189 #include <filesystem>
191 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
192 #pragma GCC system_header
196 #include <__undef_macros>
199 _LIBCPP_BEGIN_NAMESPACE_STD
201 template <class _CharT, class _Traits>
202 class _LIBCPP_TEMPLATE_VIS basic_filebuf
203 : public basic_streambuf<_CharT, _Traits>
206 typedef _CharT char_type;
207 typedef _Traits traits_type;
208 typedef typename traits_type::int_type int_type;
209 typedef typename traits_type::pos_type pos_type;
210 typedef typename traits_type::off_type off_type;
211 typedef typename traits_type::state_type state_type;
213 // 27.9.1.2 Constructors/destructor:
215 #ifndef _LIBCPP_CXX03_LANG
216 basic_filebuf(basic_filebuf&& __rhs);
218 virtual ~basic_filebuf();
220 // 27.9.1.3 Assign/swap:
221 #ifndef _LIBCPP_CXX03_LANG
222 _LIBCPP_INLINE_VISIBILITY
223 basic_filebuf& operator=(basic_filebuf&& __rhs);
225 void swap(basic_filebuf& __rhs);
228 _LIBCPP_INLINE_VISIBILITY
229 bool is_open() const;
230 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
231 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
232 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
233 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
235 _LIBCPP_INLINE_VISIBILITY
236 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
238 #if _LIBCPP_STD_VER >= 17
239 _LIBCPP_INLINE_VISIBILITY
240 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
241 return open(__p.c_str(), __mode);
244 _LIBCPP_INLINE_VISIBILITY
245 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
247 basic_filebuf* close();
249 _LIBCPP_INLINE_VISIBILITY
250 inline static const char*
251 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
254 // 27.9.1.5 Overridden virtual functions:
255 virtual int_type underflow();
256 virtual int_type pbackfail(int_type __c = traits_type::eof());
257 virtual int_type overflow (int_type __c = traits_type::eof());
258 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
259 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
260 ios_base::openmode __wch = ios_base::in | ios_base::out);
261 virtual pos_type seekpos(pos_type __sp,
262 ios_base::openmode __wch = ios_base::in | ios_base::out);
264 virtual void imbue(const locale& __loc);
268 const char* __extbufnext_;
269 const char* __extbufend_;
270 char __extbuf_min_[8];
272 char_type* __intbuf_;
275 const codecvt<char_type, char, state_type>* __cv_;
277 state_type __st_last_;
278 ios_base::openmode __om_;
279 ios_base::openmode __cm_;
282 bool __always_noconv_;
288 template <class _CharT, class _Traits>
289 basic_filebuf<_CharT, _Traits>::basic_filebuf()
304 __always_noconv_(false)
306 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
308 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
309 __always_noconv_ = __cv_->always_noconv();
314 #ifndef _LIBCPP_CXX03_LANG
316 template <class _CharT, class _Traits>
317 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
318 : basic_streambuf<_CharT, _Traits>(__rhs)
320 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
322 __extbuf_ = __extbuf_min_;
323 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
324 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
328 __extbuf_ = __rhs.__extbuf_;
329 __extbufnext_ = __rhs.__extbufnext_;
330 __extbufend_ = __rhs.__extbufend_;
332 __ebs_ = __rhs.__ebs_;
333 __intbuf_ = __rhs.__intbuf_;
334 __ibs_ = __rhs.__ibs_;
335 __file_ = __rhs.__file_;
338 __st_last_ = __rhs.__st_last_;
341 __owns_eb_ = __rhs.__owns_eb_;
342 __owns_ib_ = __rhs.__owns_ib_;
343 __always_noconv_ = __rhs.__always_noconv_;
346 if (__rhs.pbase() == __rhs.__intbuf_)
347 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
349 this->setp((char_type*)__extbuf_,
350 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
351 this->__pbump(__rhs. pptr() - __rhs.pbase());
353 else if (__rhs.eback())
355 if (__rhs.eback() == __rhs.__intbuf_)
356 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
357 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
359 this->setg((char_type*)__extbuf_,
360 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
361 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
364 __rhs.__extbufnext_ = 0;
365 __rhs.__extbufend_ = 0;
370 __rhs.__st_ = state_type();
371 __rhs.__st_last_ = state_type();
374 __rhs.__owns_eb_ = false;
375 __rhs.__owns_ib_ = false;
380 template <class _CharT, class _Traits>
382 basic_filebuf<_CharT, _Traits>&
383 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
390 #endif // _LIBCPP_CXX03_LANG
392 template <class _CharT, class _Traits>
393 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
395 #ifndef _LIBCPP_NO_EXCEPTIONS
398 #endif // _LIBCPP_NO_EXCEPTIONS
400 #ifndef _LIBCPP_NO_EXCEPTIONS
405 #endif // _LIBCPP_NO_EXCEPTIONS
412 template <class _CharT, class _Traits>
414 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
416 basic_streambuf<char_type, traits_type>::swap(__rhs);
417 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
419 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
420 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
421 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
425 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
426 ptrdiff_t __le = __extbufend_ - __extbuf_;
427 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
428 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
429 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
431 __extbuf_ = __rhs.__extbuf_;
432 __rhs.__extbuf_ = __rhs.__extbuf_min_;
434 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
436 __rhs.__extbuf_ = __extbuf_;
437 __extbuf_ = __extbuf_min_;
439 __extbufnext_ = __extbuf_ + __rn;
440 __extbufend_ = __extbuf_ + __re;
441 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
442 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
444 _VSTD::swap(__ebs_, __rhs.__ebs_);
445 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
446 _VSTD::swap(__ibs_, __rhs.__ibs_);
447 _VSTD::swap(__file_, __rhs.__file_);
448 _VSTD::swap(__cv_, __rhs.__cv_);
449 _VSTD::swap(__st_, __rhs.__st_);
450 _VSTD::swap(__st_last_, __rhs.__st_last_);
451 _VSTD::swap(__om_, __rhs.__om_);
452 _VSTD::swap(__cm_, __rhs.__cm_);
453 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
454 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
455 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
456 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
458 ptrdiff_t __n = this->gptr() - this->eback();
459 ptrdiff_t __e = this->egptr() - this->eback();
460 this->setg((char_type*)__extbuf_min_,
461 (char_type*)__extbuf_min_ + __n,
462 (char_type*)__extbuf_min_ + __e);
464 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
466 ptrdiff_t __n = this->pptr() - this->pbase();
467 ptrdiff_t __e = this->epptr() - this->pbase();
468 this->setp((char_type*)__extbuf_min_,
469 (char_type*)__extbuf_min_ + __e);
472 if (__rhs.eback() == (char_type*)__extbuf_min_)
474 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
475 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
476 __rhs.setg((char_type*)__rhs.__extbuf_min_,
477 (char_type*)__rhs.__extbuf_min_ + __n,
478 (char_type*)__rhs.__extbuf_min_ + __e);
480 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
482 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
483 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
484 __rhs.setp((char_type*)__rhs.__extbuf_min_,
485 (char_type*)__rhs.__extbuf_min_ + __e);
490 template <class _CharT, class _Traits>
491 inline _LIBCPP_INLINE_VISIBILITY
493 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
498 template <class _CharT, class _Traits>
501 basic_filebuf<_CharT, _Traits>::is_open() const
506 template <class _CharT, class _Traits>
507 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
508 ios_base::openmode __mode) _NOEXCEPT {
509 switch (__mode & ~ios_base::ate) {
511 case ios_base::out | ios_base::trunc:
513 case ios_base::out | ios_base::app:
518 case ios_base::in | ios_base::out:
520 case ios_base::in | ios_base::out | ios_base::trunc:
522 case ios_base::in | ios_base::out | ios_base::app:
523 case ios_base::in | ios_base::app:
525 case ios_base::out | ios_base::binary:
526 case ios_base::out | ios_base::trunc | ios_base::binary:
528 case ios_base::out | ios_base::app | ios_base::binary:
529 case ios_base::app | ios_base::binary:
531 case ios_base::in | ios_base::binary:
533 case ios_base::in | ios_base::out | ios_base::binary:
535 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
537 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
538 case ios_base::in | ios_base::app | ios_base::binary:
543 _LIBCPP_UNREACHABLE();
546 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
547 template <class _CharT, class _Traits>
548 basic_filebuf<_CharT, _Traits>*
549 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
551 basic_filebuf<_CharT, _Traits>* __rt = 0;
554 if (const char* __mdstr = __make_mdstring(__mode)) {
556 __file_ = fopen(__s, __mdstr);
559 if (__mode & ios_base::ate) {
560 if (fseek(__file_, 0, SEEK_END)) {
573 template <class _CharT, class _Traits>
574 _LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>*
575 basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
576 basic_filebuf<_CharT, _Traits>* __rt = 0;
578 if (const char* __mdstr = __make_mdstring(__mode)) {
580 __file_ = fdopen(__fd, __mdstr);
583 if (__mode & ios_base::ate) {
584 if (fseek(__file_, 0, SEEK_END)) {
597 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
598 // This is basically the same as the char* overload except that it uses _wfopen
599 // and long mode strings.
600 template <class _CharT, class _Traits>
601 basic_filebuf<_CharT, _Traits>*
602 basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
604 basic_filebuf<_CharT, _Traits>* __rt = 0;
608 const wchar_t* __mdstr;
609 switch (__mode & ~ios_base::ate)
612 case ios_base::out | ios_base::trunc:
615 case ios_base::out | ios_base::app:
622 case ios_base::in | ios_base::out:
625 case ios_base::in | ios_base::out | ios_base::trunc:
628 case ios_base::in | ios_base::out | ios_base::app:
629 case ios_base::in | ios_base::app:
632 case ios_base::out | ios_base::binary:
633 case ios_base::out | ios_base::trunc | ios_base::binary:
636 case ios_base::out | ios_base::app | ios_base::binary:
637 case ios_base::app | ios_base::binary:
640 case ios_base::in | ios_base::binary:
643 case ios_base::in | ios_base::out | ios_base::binary:
646 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
649 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
650 case ios_base::in | ios_base::app | ios_base::binary:
659 __file_ = _wfopen(__s, __mdstr);
663 if (__mode & ios_base::ate)
665 if (fseek(__file_, 0, SEEK_END))
681 template <class _CharT, class _Traits>
683 basic_filebuf<_CharT, _Traits>*
684 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
686 return open(__s.c_str(), __mode);
690 template <class _CharT, class _Traits>
691 basic_filebuf<_CharT, _Traits>*
692 basic_filebuf<_CharT, _Traits>::close()
694 basic_filebuf<_CharT, _Traits>* __rt = 0;
698 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
701 if (fclose(__h.release()) == 0)
710 template <class _CharT, class _Traits>
711 typename basic_filebuf<_CharT, _Traits>::int_type
712 basic_filebuf<_CharT, _Traits>::underflow()
715 return traits_type::eof();
716 bool __initial = __read_mode();
718 if (this->gptr() == 0)
719 this->setg(&__1buf, &__1buf+1, &__1buf+1);
720 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
721 int_type __c = traits_type::eof();
722 if (this->gptr() == this->egptr())
724 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
725 if (__always_noconv_)
727 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
728 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
731 this->setg(this->eback(),
732 this->eback() + __unget_sz,
733 this->eback() + __unget_sz + __nmemb);
734 __c = traits_type::to_int_type(*this->gptr());
739 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
740 if (__extbufend_ != __extbufnext_)
741 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
742 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
743 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
744 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
745 static_cast<size_t>(__extbufend_ - __extbufnext_));
746 codecvt_base::result __r;
748 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
754 __extbufend_ = __extbufnext_ + __nr;
756 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
757 this->eback() + __unget_sz,
758 this->eback() + __ibs_, __inext);
759 if (__r == codecvt_base::noconv)
761 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
762 (char_type*)const_cast<char *>(__extbufend_));
763 __c = traits_type::to_int_type(*this->gptr());
765 else if (__inext != this->eback() + __unget_sz)
767 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
768 __c = traits_type::to_int_type(*this->gptr());
774 __c = traits_type::to_int_type(*this->gptr());
775 if (this->eback() == &__1buf)
780 template <class _CharT, class _Traits>
781 typename basic_filebuf<_CharT, _Traits>::int_type
782 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
784 if (__file_ && this->eback() < this->gptr())
786 if (traits_type::eq_int_type(__c, traits_type::eof()))
789 return traits_type::not_eof(__c);
791 if ((__om_ & ios_base::out) ||
792 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
795 *this->gptr() = traits_type::to_char_type(__c);
799 return traits_type::eof();
802 template <class _CharT, class _Traits>
803 typename basic_filebuf<_CharT, _Traits>::int_type
804 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
807 return traits_type::eof();
810 char_type* __pb_save = this->pbase();
811 char_type* __epb_save = this->epptr();
812 if (!traits_type::eq_int_type(__c, traits_type::eof()))
814 if (this->pptr() == 0)
815 this->setp(&__1buf, &__1buf+1);
816 *this->pptr() = traits_type::to_char_type(__c);
819 if (this->pptr() != this->pbase())
821 if (__always_noconv_)
823 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
824 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
825 return traits_type::eof();
829 char* __extbe = __extbuf_;
830 codecvt_base::result __r;
836 const char_type* __e;
837 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
838 __extbuf_, __extbuf_ + __ebs_, __extbe);
839 if (__e == this->pbase())
840 return traits_type::eof();
841 if (__r == codecvt_base::noconv)
843 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
844 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
845 return traits_type::eof();
847 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
849 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
850 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
851 return traits_type::eof();
852 if (__r == codecvt_base::partial)
854 this->setp(const_cast<char_type*>(__e), this->pptr());
855 this->__pbump(this->epptr() - this->pbase());
859 return traits_type::eof();
860 } while (__r == codecvt_base::partial);
862 this->setp(__pb_save, __epb_save);
864 return traits_type::not_eof(__c);
867 template <class _CharT, class _Traits>
868 basic_streambuf<_CharT, _Traits>*
869 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
878 if (__ebs_ > sizeof(__extbuf_min_))
880 if (__always_noconv_ && __s)
882 __extbuf_ = (char*)__s;
887 __extbuf_ = new char[__ebs_];
893 __extbuf_ = __extbuf_min_;
894 __ebs_ = sizeof(__extbuf_min_);
897 if (!__always_noconv_)
899 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
900 if (__s && __ibs_ >= sizeof(__extbuf_min_))
907 __intbuf_ = new char_type[__ibs_];
920 template <class _CharT, class _Traits>
921 typename basic_filebuf<_CharT, _Traits>::pos_type
922 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
928 int __width = __cv_->encoding();
929 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
930 return pos_type(off_type(-1));
931 // __width > 0 || __off == 0
945 return pos_type(off_type(-1));
947 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
948 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
949 return pos_type(off_type(-1));
950 pos_type __r = ftell(__file_);
952 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
953 return pos_type(off_type(-1));
954 pos_type __r = ftello(__file_);
960 template <class _CharT, class _Traits>
961 typename basic_filebuf<_CharT, _Traits>::pos_type
962 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
964 if (__file_ == 0 || sync())
965 return pos_type(off_type(-1));
966 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
967 if (fseek(__file_, __sp, SEEK_SET))
968 return pos_type(off_type(-1));
970 if (fseeko(__file_, __sp, SEEK_SET))
971 return pos_type(off_type(-1));
973 __st_ = __sp.state();
977 template <class _CharT, class _Traits>
979 basic_filebuf<_CharT, _Traits>::sync()
986 if (__cm_ & ios_base::out)
988 if (this->pptr() != this->pbase())
989 if (overflow() == traits_type::eof())
991 codecvt_base::result __r;
995 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
996 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
997 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
999 } while (__r == codecvt_base::partial);
1000 if (__r == codecvt_base::error)
1002 if (fflush(__file_))
1005 else if (__cm_ & ios_base::in)
1008 state_type __state = __st_last_;
1009 bool __update_st = false;
1010 if (__always_noconv_)
1011 __c = this->egptr() - this->gptr();
1014 int __width = __cv_->encoding();
1015 __c = __extbufend_ - __extbufnext_;
1017 __c += __width * (this->egptr() - this->gptr());
1020 if (this->gptr() != this->egptr())
1022 const int __off = __cv_->length(__state, __extbuf_,
1024 this->gptr() - this->eback());
1025 __c += __extbufnext_ - __extbuf_ - __off;
1030 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1031 if (fseek(__file_, -__c, SEEK_CUR))
1034 if (fseeko(__file_, -__c, SEEK_CUR))
1039 __extbufnext_ = __extbufend_ = __extbuf_;
1040 this->setg(0, 0, 0);
1046 template <class _CharT, class _Traits>
1048 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1051 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1052 bool __old_anc = __always_noconv_;
1053 __always_noconv_ = __cv_->always_noconv();
1054 if (__old_anc != __always_noconv_)
1056 this->setg(0, 0, 0);
1058 // invariant, char_type is char, else we couldn't get here
1059 if (__always_noconv_) // need to dump __intbuf_
1062 delete [] __extbuf_;
1063 __owns_eb_ = __owns_ib_;
1065 __extbuf_ = (char*)__intbuf_;
1070 else // need to obtain an __intbuf_.
1071 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1072 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1075 __intbuf_ = (char_type*)__extbuf_;
1077 __extbuf_ = new char[__ebs_];
1083 __intbuf_ = new char_type[__ibs_];
1090 template <class _CharT, class _Traits>
1092 basic_filebuf<_CharT, _Traits>::__read_mode()
1094 if (!(__cm_ & ios_base::in))
1097 if (__always_noconv_)
1098 this->setg((char_type*)__extbuf_,
1099 (char_type*)__extbuf_ + __ebs_,
1100 (char_type*)__extbuf_ + __ebs_);
1102 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1103 __cm_ = ios_base::in;
1109 template <class _CharT, class _Traits>
1111 basic_filebuf<_CharT, _Traits>::__write_mode()
1113 if (!(__cm_ & ios_base::out))
1115 this->setg(0, 0, 0);
1116 if (__ebs_ > sizeof(__extbuf_min_))
1118 if (__always_noconv_)
1119 this->setp((char_type*)__extbuf_,
1120 (char_type*)__extbuf_ + (__ebs_ - 1));
1122 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1126 __cm_ = ios_base::out;
1132 template <class _CharT, class _Traits>
1133 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1134 : public basic_istream<_CharT, _Traits>
1137 typedef _CharT char_type;
1138 typedef _Traits traits_type;
1139 typedef typename traits_type::int_type int_type;
1140 typedef typename traits_type::pos_type pos_type;
1141 typedef typename traits_type::off_type off_type;
1143 _LIBCPP_INLINE_VISIBILITY
1145 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1146 _LIBCPP_INLINE_VISIBILITY
1147 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1148 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 _LIBCPP_INLINE_VISIBILITY
1150 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1152 _LIBCPP_INLINE_VISIBILITY
1153 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1154 #if _LIBCPP_STD_VER >= 17
1155 _LIBCPP_INLINE_VISIBILITY
1156 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1157 : basic_ifstream(__p.c_str(), __mode) {}
1158 #endif // _LIBCPP_STD_VER >= 17
1160 #ifndef _LIBCPP_CXX03_LANG
1161 _LIBCPP_INLINE_VISIBILITY
1162 basic_ifstream(basic_ifstream&& __rhs);
1164 _LIBCPP_INLINE_VISIBILITY
1165 basic_ifstream& operator=(basic_ifstream&& __rhs);
1167 _LIBCPP_INLINE_VISIBILITY
1168 void swap(basic_ifstream& __rhs);
1170 _LIBCPP_INLINE_VISIBILITY
1171 basic_filebuf<char_type, traits_type>* rdbuf() const;
1172 _LIBCPP_INLINE_VISIBILITY
1173 bool is_open() const;
1174 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1175 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1176 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1177 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1179 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1180 #if _LIBCPP_STD_VER >= 17
1181 _LIBCPP_INLINE_VISIBILITY
1182 void open(const filesystem::path& __p,
1183 ios_base::openmode __mode = ios_base::in) {
1184 return open(__p.c_str(), __mode);
1186 #endif // _LIBCPP_STD_VER >= 17
1188 _LIBCPP_INLINE_VISIBILITY
1189 void __open(int __fd, ios_base::openmode __mode);
1191 _LIBCPP_INLINE_VISIBILITY
1195 basic_filebuf<char_type, traits_type> __sb_;
1198 template <class _CharT, class _Traits>
1200 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1201 : basic_istream<char_type, traits_type>(&__sb_)
1205 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1206 template <class _CharT, class _Traits>
1208 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1209 : basic_istream<char_type, traits_type>(&__sb_)
1211 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1212 this->setstate(ios_base::failbit);
1215 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1216 template <class _CharT, class _Traits>
1218 basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1219 : basic_istream<char_type, traits_type>(&__sb_)
1221 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1222 this->setstate(ios_base::failbit);
1226 template <class _CharT, class _Traits>
1228 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1229 : basic_istream<char_type, traits_type>(&__sb_)
1231 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1232 this->setstate(ios_base::failbit);
1236 #ifndef _LIBCPP_CXX03_LANG
1238 template <class _CharT, class _Traits>
1240 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1241 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1242 __sb_(_VSTD::move(__rhs.__sb_))
1244 this->set_rdbuf(&__sb_);
1247 template <class _CharT, class _Traits>
1249 basic_ifstream<_CharT, _Traits>&
1250 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1252 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1253 __sb_ = _VSTD::move(__rhs.__sb_);
1257 #endif // _LIBCPP_CXX03_LANG
1259 template <class _CharT, class _Traits>
1262 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1264 basic_istream<char_type, traits_type>::swap(__rhs);
1265 __sb_.swap(__rhs.__sb_);
1268 template <class _CharT, class _Traits>
1269 inline _LIBCPP_INLINE_VISIBILITY
1271 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1276 template <class _CharT, class _Traits>
1278 basic_filebuf<_CharT, _Traits>*
1279 basic_ifstream<_CharT, _Traits>::rdbuf() const
1281 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1284 template <class _CharT, class _Traits>
1287 basic_ifstream<_CharT, _Traits>::is_open() const
1289 return __sb_.is_open();
1292 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1293 template <class _CharT, class _Traits>
1295 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1297 if (__sb_.open(__s, __mode | ios_base::in))
1300 this->setstate(ios_base::failbit);
1303 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1304 template <class _CharT, class _Traits>
1306 basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1308 if (__sb_.open(__s, __mode | ios_base::in))
1311 this->setstate(ios_base::failbit);
1315 template <class _CharT, class _Traits>
1317 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1319 if (__sb_.open(__s, __mode | ios_base::in))
1322 this->setstate(ios_base::failbit);
1325 template <class _CharT, class _Traits>
1326 void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1327 ios_base::openmode __mode) {
1328 if (__sb_.__open(__fd, __mode | ios_base::in))
1331 this->setstate(ios_base::failbit);
1335 template <class _CharT, class _Traits>
1338 basic_ifstream<_CharT, _Traits>::close()
1340 if (__sb_.close() == 0)
1341 this->setstate(ios_base::failbit);
1346 template <class _CharT, class _Traits>
1347 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1348 : public basic_ostream<_CharT, _Traits>
1351 typedef _CharT char_type;
1352 typedef _Traits traits_type;
1353 typedef typename traits_type::int_type int_type;
1354 typedef typename traits_type::pos_type pos_type;
1355 typedef typename traits_type::off_type off_type;
1357 _LIBCPP_INLINE_VISIBILITY
1359 _LIBCPP_INLINE_VISIBILITY
1360 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1361 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1362 _LIBCPP_INLINE_VISIBILITY
1363 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1365 _LIBCPP_INLINE_VISIBILITY
1366 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1368 #if _LIBCPP_STD_VER >= 17
1369 _LIBCPP_INLINE_VISIBILITY
1370 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1371 : basic_ofstream(__p.c_str(), __mode) {}
1372 #endif // _LIBCPP_STD_VER >= 17
1374 #ifndef _LIBCPP_CXX03_LANG
1375 _LIBCPP_INLINE_VISIBILITY
1376 basic_ofstream(basic_ofstream&& __rhs);
1378 _LIBCPP_INLINE_VISIBILITY
1379 basic_ofstream& operator=(basic_ofstream&& __rhs);
1381 _LIBCPP_INLINE_VISIBILITY
1382 void swap(basic_ofstream& __rhs);
1384 _LIBCPP_INLINE_VISIBILITY
1385 basic_filebuf<char_type, traits_type>* rdbuf() const;
1386 _LIBCPP_INLINE_VISIBILITY
1387 bool is_open() const;
1388 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1389 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1390 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1391 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1393 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1395 #if _LIBCPP_STD_VER >= 17
1396 _LIBCPP_INLINE_VISIBILITY
1397 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1398 { return open(__p.c_str(), __mode); }
1399 #endif // _LIBCPP_STD_VER >= 17
1401 _LIBCPP_INLINE_VISIBILITY
1402 void __open(int __fd, ios_base::openmode __mode);
1404 _LIBCPP_INLINE_VISIBILITY
1408 basic_filebuf<char_type, traits_type> __sb_;
1411 template <class _CharT, class _Traits>
1413 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1414 : basic_ostream<char_type, traits_type>(&__sb_)
1418 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1419 template <class _CharT, class _Traits>
1421 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1422 : basic_ostream<char_type, traits_type>(&__sb_)
1424 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1425 this->setstate(ios_base::failbit);
1428 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1429 template <class _CharT, class _Traits>
1431 basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1432 : basic_ostream<char_type, traits_type>(&__sb_)
1434 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1435 this->setstate(ios_base::failbit);
1439 template <class _CharT, class _Traits>
1441 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1442 : basic_ostream<char_type, traits_type>(&__sb_)
1444 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1445 this->setstate(ios_base::failbit);
1449 #ifndef _LIBCPP_CXX03_LANG
1451 template <class _CharT, class _Traits>
1453 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1454 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1455 __sb_(_VSTD::move(__rhs.__sb_))
1457 this->set_rdbuf(&__sb_);
1460 template <class _CharT, class _Traits>
1462 basic_ofstream<_CharT, _Traits>&
1463 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1465 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1466 __sb_ = _VSTD::move(__rhs.__sb_);
1470 #endif // _LIBCPP_CXX03_LANG
1472 template <class _CharT, class _Traits>
1475 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1477 basic_ostream<char_type, traits_type>::swap(__rhs);
1478 __sb_.swap(__rhs.__sb_);
1481 template <class _CharT, class _Traits>
1482 inline _LIBCPP_INLINE_VISIBILITY
1484 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1489 template <class _CharT, class _Traits>
1491 basic_filebuf<_CharT, _Traits>*
1492 basic_ofstream<_CharT, _Traits>::rdbuf() const
1494 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1497 template <class _CharT, class _Traits>
1500 basic_ofstream<_CharT, _Traits>::is_open() const
1502 return __sb_.is_open();
1505 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1506 template <class _CharT, class _Traits>
1508 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1510 if (__sb_.open(__s, __mode | ios_base::out))
1513 this->setstate(ios_base::failbit);
1516 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1517 template <class _CharT, class _Traits>
1519 basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1521 if (__sb_.open(__s, __mode | ios_base::out))
1524 this->setstate(ios_base::failbit);
1528 template <class _CharT, class _Traits>
1530 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1532 if (__sb_.open(__s, __mode | ios_base::out))
1535 this->setstate(ios_base::failbit);
1538 template <class _CharT, class _Traits>
1539 void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1540 ios_base::openmode __mode) {
1541 if (__sb_.__open(__fd, __mode | ios_base::out))
1544 this->setstate(ios_base::failbit);
1548 template <class _CharT, class _Traits>
1551 basic_ofstream<_CharT, _Traits>::close()
1553 if (__sb_.close() == 0)
1554 this->setstate(ios_base::failbit);
1559 template <class _CharT, class _Traits>
1560 class _LIBCPP_TEMPLATE_VIS basic_fstream
1561 : public basic_iostream<_CharT, _Traits>
1564 typedef _CharT char_type;
1565 typedef _Traits traits_type;
1566 typedef typename traits_type::int_type int_type;
1567 typedef typename traits_type::pos_type pos_type;
1568 typedef typename traits_type::off_type off_type;
1570 _LIBCPP_INLINE_VISIBILITY
1572 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1573 _LIBCPP_INLINE_VISIBILITY
1574 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1575 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1576 _LIBCPP_INLINE_VISIBILITY
1577 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1579 _LIBCPP_INLINE_VISIBILITY
1580 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1582 #if _LIBCPP_STD_VER >= 17
1583 _LIBCPP_INLINE_VISIBILITY
1584 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1585 : basic_fstream(__p.c_str(), __mode) {}
1586 #endif // _LIBCPP_STD_VER >= 17
1589 #ifndef _LIBCPP_CXX03_LANG
1590 _LIBCPP_INLINE_VISIBILITY
1591 basic_fstream(basic_fstream&& __rhs);
1593 _LIBCPP_INLINE_VISIBILITY
1594 basic_fstream& operator=(basic_fstream&& __rhs);
1596 _LIBCPP_INLINE_VISIBILITY
1597 void swap(basic_fstream& __rhs);
1599 _LIBCPP_INLINE_VISIBILITY
1600 basic_filebuf<char_type, traits_type>* rdbuf() const;
1601 _LIBCPP_INLINE_VISIBILITY
1602 bool is_open() const;
1603 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1604 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1605 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1608 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1610 #if _LIBCPP_STD_VER >= 17
1611 _LIBCPP_INLINE_VISIBILITY
1612 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1613 { return open(__p.c_str(), __mode); }
1614 #endif // _LIBCPP_STD_VER >= 17
1617 _LIBCPP_INLINE_VISIBILITY
1621 basic_filebuf<char_type, traits_type> __sb_;
1624 template <class _CharT, class _Traits>
1626 basic_fstream<_CharT, _Traits>::basic_fstream()
1627 : basic_iostream<char_type, traits_type>(&__sb_)
1631 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1632 template <class _CharT, class _Traits>
1634 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1635 : basic_iostream<char_type, traits_type>(&__sb_)
1637 if (__sb_.open(__s, __mode) == 0)
1638 this->setstate(ios_base::failbit);
1641 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1642 template <class _CharT, class _Traits>
1644 basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1645 : basic_iostream<char_type, traits_type>(&__sb_)
1647 if (__sb_.open(__s, __mode) == 0)
1648 this->setstate(ios_base::failbit);
1652 template <class _CharT, class _Traits>
1654 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1655 : basic_iostream<char_type, traits_type>(&__sb_)
1657 if (__sb_.open(__s, __mode) == 0)
1658 this->setstate(ios_base::failbit);
1662 #ifndef _LIBCPP_CXX03_LANG
1664 template <class _CharT, class _Traits>
1666 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1667 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1668 __sb_(_VSTD::move(__rhs.__sb_))
1670 this->set_rdbuf(&__sb_);
1673 template <class _CharT, class _Traits>
1675 basic_fstream<_CharT, _Traits>&
1676 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1678 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1679 __sb_ = _VSTD::move(__rhs.__sb_);
1683 #endif // _LIBCPP_CXX03_LANG
1685 template <class _CharT, class _Traits>
1688 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1690 basic_iostream<char_type, traits_type>::swap(__rhs);
1691 __sb_.swap(__rhs.__sb_);
1694 template <class _CharT, class _Traits>
1695 inline _LIBCPP_INLINE_VISIBILITY
1697 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1702 template <class _CharT, class _Traits>
1704 basic_filebuf<_CharT, _Traits>*
1705 basic_fstream<_CharT, _Traits>::rdbuf() const
1707 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1710 template <class _CharT, class _Traits>
1713 basic_fstream<_CharT, _Traits>::is_open() const
1715 return __sb_.is_open();
1718 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1719 template <class _CharT, class _Traits>
1721 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1723 if (__sb_.open(__s, __mode))
1726 this->setstate(ios_base::failbit);
1729 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1730 template <class _CharT, class _Traits>
1732 basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1734 if (__sb_.open(__s, __mode))
1737 this->setstate(ios_base::failbit);
1741 template <class _CharT, class _Traits>
1743 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1745 if (__sb_.open(__s, __mode))
1748 this->setstate(ios_base::failbit);
1752 template <class _CharT, class _Traits>
1755 basic_fstream<_CharT, _Traits>::close()
1757 if (__sb_.close() == 0)
1758 this->setstate(ios_base::failbit);
1761 _LIBCPP_END_NAMESPACE_STD
1765 #endif // _LIBCPP_FSTREAM