2 //===------------------------- fstream ------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_FSTREAM
11 #define _LIBCPP_FSTREAM
16 template <class charT, class traits = char_traits<charT> >
18 : public basic_streambuf<charT, traits>
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
27 // 27.9.1.2 Constructors/destructor:
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
38 basic_filebuf* open(const char* s, ios_base::openmode mode);
39 basic_filebuf* open(const string& s, ios_base::openmode mode);
40 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
41 basic_filebuf* close();
44 // 27.9.1.5 Overridden virtual functions:
45 virtual streamsize showmanyc();
46 virtual int_type underflow();
47 virtual int_type uflow();
48 virtual int_type pbackfail(int_type c = traits_type::eof());
49 virtual int_type overflow (int_type c = traits_type::eof());
50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52 ios_base::openmode which = ios_base::in | ios_base::out);
53 virtual pos_type seekpos(pos_type sp,
54 ios_base::openmode which = ios_base::in | ios_base::out);
56 virtual void imbue(const locale& loc);
59 template <class charT, class traits>
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
63 typedef basic_filebuf<char> filebuf;
64 typedef basic_filebuf<wchar_t> wfilebuf;
66 template <class charT, class traits = char_traits<charT> >
68 : public basic_istream<charT,traits>
71 typedef charT char_type;
72 typedef traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
78 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
79 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
80 explicit basic_ifstream(const filesystem::path& p,
81 ios_base::openmode mode = ios_base::in); // C++17
82 basic_ifstream(basic_ifstream&& rhs);
84 basic_ifstream& operator=(basic_ifstream&& rhs);
85 void swap(basic_ifstream& rhs);
87 basic_filebuf<char_type, traits_type>* rdbuf() const;
89 void open(const char* s, ios_base::openmode mode = ios_base::in);
90 void open(const string& s, ios_base::openmode mode = ios_base::in);
91 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
96 template <class charT, class traits>
98 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
100 typedef basic_ifstream<char> ifstream;
101 typedef basic_ifstream<wchar_t> wifstream;
103 template <class charT, class traits = char_traits<charT> >
105 : public basic_ostream<charT,traits>
108 typedef charT char_type;
109 typedef traits traits_type;
110 typedef typename traits_type::int_type int_type;
111 typedef typename traits_type::pos_type pos_type;
112 typedef typename traits_type::off_type off_type;
115 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
116 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
117 explicit basic_ofstream(const filesystem::path& p,
118 ios_base::openmode mode = ios_base::out); // C++17
119 basic_ofstream(basic_ofstream&& rhs);
121 basic_ofstream& operator=(basic_ofstream&& rhs);
122 void swap(basic_ofstream& rhs);
124 basic_filebuf<char_type, traits_type>* rdbuf() const;
125 bool is_open() const;
126 void open(const char* s, ios_base::openmode mode = ios_base::out);
127 void open(const string& s, ios_base::openmode mode = ios_base::out);
128 void open(const filesystem::path& p,
129 ios_base::openmode mode = ios_base::out); // C++17
134 template <class charT, class traits>
136 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
138 typedef basic_ofstream<char> ofstream;
139 typedef basic_ofstream<wchar_t> wofstream;
141 template <class charT, class traits=char_traits<charT> >
143 : public basic_iostream<charT,traits>
146 typedef charT char_type;
147 typedef traits traits_type;
148 typedef typename traits_type::int_type int_type;
149 typedef typename traits_type::pos_type pos_type;
150 typedef typename traits_type::off_type off_type;
153 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
155 explicit basic_fstream(const filesystem::path& p,
156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
157 basic_fstream(basic_fstream&& rhs);
159 basic_fstream& operator=(basic_fstream&& rhs);
160 void swap(basic_fstream& rhs);
162 basic_filebuf<char_type, traits_type>* rdbuf() const;
163 bool is_open() const;
164 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
165 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
166 void open(const filesystem::path& s,
167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
172 template <class charT, class traits>
173 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
175 typedef basic_fstream<char> fstream;
176 typedef basic_fstream<wchar_t> wfstream;
188 #include <filesystem>
190 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
191 #pragma GCC system_header
195 #include <__undef_macros>
198 _LIBCPP_BEGIN_NAMESPACE_STD
200 template <class _CharT, class _Traits>
201 class _LIBCPP_TEMPLATE_VIS basic_filebuf
202 : public basic_streambuf<_CharT, _Traits>
205 typedef _CharT char_type;
206 typedef _Traits traits_type;
207 typedef typename traits_type::int_type int_type;
208 typedef typename traits_type::pos_type pos_type;
209 typedef typename traits_type::off_type off_type;
210 typedef typename traits_type::state_type state_type;
212 // 27.9.1.2 Constructors/destructor:
214 #ifndef _LIBCPP_CXX03_LANG
215 basic_filebuf(basic_filebuf&& __rhs);
217 virtual ~basic_filebuf();
219 // 27.9.1.3 Assign/swap:
220 #ifndef _LIBCPP_CXX03_LANG
221 _LIBCPP_INLINE_VISIBILITY
222 basic_filebuf& operator=(basic_filebuf&& __rhs);
224 void swap(basic_filebuf& __rhs);
227 _LIBCPP_INLINE_VISIBILITY
228 bool is_open() const;
229 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
230 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
231 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
232 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
234 _LIBCPP_INLINE_VISIBILITY
235 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
237 #if _LIBCPP_STD_VER >= 17
238 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
239 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
240 return open(__p.c_str(), __mode);
243 _LIBCPP_INLINE_VISIBILITY
244 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
246 basic_filebuf* close();
248 _LIBCPP_INLINE_VISIBILITY
249 inline static const char*
250 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
253 // 27.9.1.5 Overridden virtual functions:
254 virtual int_type underflow();
255 virtual int_type pbackfail(int_type __c = traits_type::eof());
256 virtual int_type overflow (int_type __c = traits_type::eof());
257 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
258 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
259 ios_base::openmode __wch = ios_base::in | ios_base::out);
260 virtual pos_type seekpos(pos_type __sp,
261 ios_base::openmode __wch = ios_base::in | ios_base::out);
263 virtual void imbue(const locale& __loc);
267 const char* __extbufnext_;
268 const char* __extbufend_;
269 char __extbuf_min_[8];
271 char_type* __intbuf_;
274 const codecvt<char_type, char, state_type>* __cv_;
276 state_type __st_last_;
277 ios_base::openmode __om_;
278 ios_base::openmode __cm_;
281 bool __always_noconv_;
287 template <class _CharT, class _Traits>
288 basic_filebuf<_CharT, _Traits>::basic_filebuf()
303 __always_noconv_(false)
305 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
307 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
308 __always_noconv_ = __cv_->always_noconv();
313 #ifndef _LIBCPP_CXX03_LANG
315 template <class _CharT, class _Traits>
316 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
317 : basic_streambuf<_CharT, _Traits>(__rhs)
319 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
321 __extbuf_ = __extbuf_min_;
322 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
323 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
327 __extbuf_ = __rhs.__extbuf_;
328 __extbufnext_ = __rhs.__extbufnext_;
329 __extbufend_ = __rhs.__extbufend_;
331 __ebs_ = __rhs.__ebs_;
332 __intbuf_ = __rhs.__intbuf_;
333 __ibs_ = __rhs.__ibs_;
334 __file_ = __rhs.__file_;
337 __st_last_ = __rhs.__st_last_;
340 __owns_eb_ = __rhs.__owns_eb_;
341 __owns_ib_ = __rhs.__owns_ib_;
342 __always_noconv_ = __rhs.__always_noconv_;
345 if (__rhs.pbase() == __rhs.__intbuf_)
346 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
348 this->setp((char_type*)__extbuf_,
349 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
350 this->__pbump(__rhs. pptr() - __rhs.pbase());
352 else if (__rhs.eback())
354 if (__rhs.eback() == __rhs.__intbuf_)
355 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
356 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
358 this->setg((char_type*)__extbuf_,
359 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
360 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
363 __rhs.__extbufnext_ = 0;
364 __rhs.__extbufend_ = 0;
369 __rhs.__st_ = state_type();
370 __rhs.__st_last_ = state_type();
373 __rhs.__owns_eb_ = false;
374 __rhs.__owns_ib_ = false;
379 template <class _CharT, class _Traits>
381 basic_filebuf<_CharT, _Traits>&
382 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
389 #endif // _LIBCPP_CXX03_LANG
391 template <class _CharT, class _Traits>
392 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
394 #ifndef _LIBCPP_NO_EXCEPTIONS
397 #endif // _LIBCPP_NO_EXCEPTIONS
399 #ifndef _LIBCPP_NO_EXCEPTIONS
404 #endif // _LIBCPP_NO_EXCEPTIONS
411 template <class _CharT, class _Traits>
413 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
415 basic_streambuf<char_type, traits_type>::swap(__rhs);
416 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
418 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
419 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
420 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
424 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
425 ptrdiff_t __le = __extbufend_ - __extbuf_;
426 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
427 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
428 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
430 __extbuf_ = __rhs.__extbuf_;
431 __rhs.__extbuf_ = __rhs.__extbuf_min_;
433 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
435 __rhs.__extbuf_ = __extbuf_;
436 __extbuf_ = __extbuf_min_;
438 __extbufnext_ = __extbuf_ + __rn;
439 __extbufend_ = __extbuf_ + __re;
440 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
441 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
443 _VSTD::swap(__ebs_, __rhs.__ebs_);
444 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
445 _VSTD::swap(__ibs_, __rhs.__ibs_);
446 _VSTD::swap(__file_, __rhs.__file_);
447 _VSTD::swap(__cv_, __rhs.__cv_);
448 _VSTD::swap(__st_, __rhs.__st_);
449 _VSTD::swap(__st_last_, __rhs.__st_last_);
450 _VSTD::swap(__om_, __rhs.__om_);
451 _VSTD::swap(__cm_, __rhs.__cm_);
452 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
453 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
454 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
455 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
457 ptrdiff_t __n = this->gptr() - this->eback();
458 ptrdiff_t __e = this->egptr() - this->eback();
459 this->setg((char_type*)__extbuf_min_,
460 (char_type*)__extbuf_min_ + __n,
461 (char_type*)__extbuf_min_ + __e);
463 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
465 ptrdiff_t __n = this->pptr() - this->pbase();
466 ptrdiff_t __e = this->epptr() - this->pbase();
467 this->setp((char_type*)__extbuf_min_,
468 (char_type*)__extbuf_min_ + __e);
471 if (__rhs.eback() == (char_type*)__extbuf_min_)
473 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
474 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
475 __rhs.setg((char_type*)__rhs.__extbuf_min_,
476 (char_type*)__rhs.__extbuf_min_ + __n,
477 (char_type*)__rhs.__extbuf_min_ + __e);
479 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
481 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
482 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
483 __rhs.setp((char_type*)__rhs.__extbuf_min_,
484 (char_type*)__rhs.__extbuf_min_ + __e);
489 template <class _CharT, class _Traits>
490 inline _LIBCPP_INLINE_VISIBILITY
492 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
497 template <class _CharT, class _Traits>
500 basic_filebuf<_CharT, _Traits>::is_open() const
505 template <class _CharT, class _Traits>
506 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
507 ios_base::openmode __mode) _NOEXCEPT {
508 switch (__mode & ~ios_base::ate) {
510 case ios_base::out | ios_base::trunc:
511 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
512 case ios_base::out | ios_base::app:
514 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
516 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
517 case ios_base::in | ios_base::out:
518 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
519 case ios_base::in | ios_base::out | ios_base::trunc:
520 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
521 case ios_base::in | ios_base::out | ios_base::app:
522 case ios_base::in | ios_base::app:
523 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
524 case ios_base::out | ios_base::binary:
525 case ios_base::out | ios_base::trunc | ios_base::binary:
526 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
527 case ios_base::out | ios_base::app | ios_base::binary:
528 case ios_base::app | ios_base::binary:
529 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
530 case ios_base::in | ios_base::binary:
531 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
532 case ios_base::in | ios_base::out | ios_base::binary:
533 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
534 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
535 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
536 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
537 case ios_base::in | ios_base::app | ios_base::binary:
538 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
542 _LIBCPP_UNREACHABLE();
545 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
546 template <class _CharT, class _Traits>
547 basic_filebuf<_CharT, _Traits>*
548 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
550 basic_filebuf<_CharT, _Traits>* __rt = 0;
553 if (const char* __mdstr = __make_mdstring(__mode)) {
555 __file_ = fopen(__s, __mdstr);
558 if (__mode & ios_base::ate) {
559 if (fseek(__file_, 0, SEEK_END)) {
572 template <class _CharT, class _Traits>
573 _LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>*
574 basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
575 basic_filebuf<_CharT, _Traits>* __rt = 0;
577 if (const char* __mdstr = __make_mdstring(__mode)) {
579 __file_ = fdopen(__fd, __mdstr);
582 if (__mode & ios_base::ate) {
583 if (fseek(__file_, 0, SEEK_END)) {
596 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
597 // This is basically the same as the char* overload except that it uses _wfopen
598 // and long mode strings.
599 template <class _CharT, class _Traits>
600 basic_filebuf<_CharT, _Traits>*
601 basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
603 basic_filebuf<_CharT, _Traits>* __rt = 0;
607 const wchar_t* __mdstr;
608 switch (__mode & ~ios_base::ate)
611 case ios_base::out | ios_base::trunc:
614 case ios_base::out | ios_base::app:
621 case ios_base::in | ios_base::out:
624 case ios_base::in | ios_base::out | ios_base::trunc:
627 case ios_base::in | ios_base::out | ios_base::app:
628 case ios_base::in | ios_base::app:
631 case ios_base::out | ios_base::binary:
632 case ios_base::out | ios_base::trunc | ios_base::binary:
635 case ios_base::out | ios_base::app | ios_base::binary:
636 case ios_base::app | ios_base::binary:
639 case ios_base::in | ios_base::binary:
642 case ios_base::in | ios_base::out | ios_base::binary:
645 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
648 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
649 case ios_base::in | ios_base::app | ios_base::binary:
658 __file_ = _wfopen(__s, __mdstr);
662 if (__mode & ios_base::ate)
664 if (fseek(__file_, 0, SEEK_END))
680 template <class _CharT, class _Traits>
682 basic_filebuf<_CharT, _Traits>*
683 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
685 return open(__s.c_str(), __mode);
689 template <class _CharT, class _Traits>
690 basic_filebuf<_CharT, _Traits>*
691 basic_filebuf<_CharT, _Traits>::close()
693 basic_filebuf<_CharT, _Traits>* __rt = 0;
697 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
700 if (fclose(__h.release()))
708 template <class _CharT, class _Traits>
709 typename basic_filebuf<_CharT, _Traits>::int_type
710 basic_filebuf<_CharT, _Traits>::underflow()
713 return traits_type::eof();
714 bool __initial = __read_mode();
716 if (this->gptr() == 0)
717 this->setg(&__1buf, &__1buf+1, &__1buf+1);
718 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
719 int_type __c = traits_type::eof();
720 if (this->gptr() == this->egptr())
722 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
723 if (__always_noconv_)
725 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
726 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
729 this->setg(this->eback(),
730 this->eback() + __unget_sz,
731 this->eback() + __unget_sz + __nmemb);
732 __c = traits_type::to_int_type(*this->gptr());
737 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
738 if (__extbufend_ != __extbufnext_)
739 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
740 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
741 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
742 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
743 static_cast<size_t>(__extbufend_ - __extbufnext_));
744 codecvt_base::result __r;
746 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
752 __extbufend_ = __extbufnext_ + __nr;
754 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
755 this->eback() + __unget_sz,
756 this->eback() + __ibs_, __inext);
757 if (__r == codecvt_base::noconv)
759 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
760 (char_type*)const_cast<char *>(__extbufend_));
761 __c = traits_type::to_int_type(*this->gptr());
763 else if (__inext != this->eback() + __unget_sz)
765 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
766 __c = traits_type::to_int_type(*this->gptr());
772 __c = traits_type::to_int_type(*this->gptr());
773 if (this->eback() == &__1buf)
778 template <class _CharT, class _Traits>
779 typename basic_filebuf<_CharT, _Traits>::int_type
780 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
782 if (__file_ && this->eback() < this->gptr())
784 if (traits_type::eq_int_type(__c, traits_type::eof()))
787 return traits_type::not_eof(__c);
789 if ((__om_ & ios_base::out) ||
790 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
793 *this->gptr() = traits_type::to_char_type(__c);
797 return traits_type::eof();
800 template <class _CharT, class _Traits>
801 typename basic_filebuf<_CharT, _Traits>::int_type
802 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
805 return traits_type::eof();
808 char_type* __pb_save = this->pbase();
809 char_type* __epb_save = this->epptr();
810 if (!traits_type::eq_int_type(__c, traits_type::eof()))
812 if (this->pptr() == 0)
813 this->setp(&__1buf, &__1buf+1);
814 *this->pptr() = traits_type::to_char_type(__c);
817 if (this->pptr() != this->pbase())
819 if (__always_noconv_)
821 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
822 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
823 return traits_type::eof();
827 char* __extbe = __extbuf_;
828 codecvt_base::result __r;
834 const char_type* __e;
835 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
836 __extbuf_, __extbuf_ + __ebs_, __extbe);
837 if (__e == this->pbase())
838 return traits_type::eof();
839 if (__r == codecvt_base::noconv)
841 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
842 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
843 return traits_type::eof();
845 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
847 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
848 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
849 return traits_type::eof();
850 if (__r == codecvt_base::partial)
852 this->setp(const_cast<char_type*>(__e), this->pptr());
853 this->__pbump(this->epptr() - this->pbase());
857 return traits_type::eof();
858 } while (__r == codecvt_base::partial);
860 this->setp(__pb_save, __epb_save);
862 return traits_type::not_eof(__c);
865 template <class _CharT, class _Traits>
866 basic_streambuf<_CharT, _Traits>*
867 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
876 if (__ebs_ > sizeof(__extbuf_min_))
878 if (__always_noconv_ && __s)
880 __extbuf_ = (char*)__s;
885 __extbuf_ = new char[__ebs_];
891 __extbuf_ = __extbuf_min_;
892 __ebs_ = sizeof(__extbuf_min_);
895 if (!__always_noconv_)
897 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
898 if (__s && __ibs_ >= sizeof(__extbuf_min_))
905 __intbuf_ = new char_type[__ibs_];
918 template <class _CharT, class _Traits>
919 typename basic_filebuf<_CharT, _Traits>::pos_type
920 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
926 int __width = __cv_->encoding();
927 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
928 return pos_type(off_type(-1));
929 // __width > 0 || __off == 0
943 return pos_type(off_type(-1));
945 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
946 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
947 return pos_type(off_type(-1));
948 pos_type __r = ftell(__file_);
950 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
951 return pos_type(off_type(-1));
952 pos_type __r = ftello(__file_);
958 template <class _CharT, class _Traits>
959 typename basic_filebuf<_CharT, _Traits>::pos_type
960 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
962 if (__file_ == 0 || sync())
963 return pos_type(off_type(-1));
964 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
965 if (fseek(__file_, __sp, SEEK_SET))
966 return pos_type(off_type(-1));
968 if (fseeko(__file_, __sp, SEEK_SET))
969 return pos_type(off_type(-1));
971 __st_ = __sp.state();
975 template <class _CharT, class _Traits>
977 basic_filebuf<_CharT, _Traits>::sync()
984 if (__cm_ & ios_base::out)
986 if (this->pptr() != this->pbase())
987 if (overflow() == traits_type::eof())
989 codecvt_base::result __r;
993 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
994 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
995 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
997 } while (__r == codecvt_base::partial);
998 if (__r == codecvt_base::error)
1000 if (fflush(__file_))
1003 else if (__cm_ & ios_base::in)
1006 state_type __state = __st_last_;
1007 bool __update_st = false;
1008 if (__always_noconv_)
1009 __c = this->egptr() - this->gptr();
1012 int __width = __cv_->encoding();
1013 __c = __extbufend_ - __extbufnext_;
1015 __c += __width * (this->egptr() - this->gptr());
1018 if (this->gptr() != this->egptr())
1020 const int __off = __cv_->length(__state, __extbuf_,
1022 this->gptr() - this->eback());
1023 __c += __extbufnext_ - __extbuf_ - __off;
1028 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1029 if (fseek(__file_, -__c, SEEK_CUR))
1032 if (fseeko(__file_, -__c, SEEK_CUR))
1037 __extbufnext_ = __extbufend_ = __extbuf_;
1038 this->setg(0, 0, 0);
1044 template <class _CharT, class _Traits>
1046 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1049 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1050 bool __old_anc = __always_noconv_;
1051 __always_noconv_ = __cv_->always_noconv();
1052 if (__old_anc != __always_noconv_)
1054 this->setg(0, 0, 0);
1056 // invariant, char_type is char, else we couldn't get here
1057 if (__always_noconv_) // need to dump __intbuf_
1060 delete [] __extbuf_;
1061 __owns_eb_ = __owns_ib_;
1063 __extbuf_ = (char*)__intbuf_;
1068 else // need to obtain an __intbuf_.
1069 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1070 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1073 __intbuf_ = (char_type*)__extbuf_;
1075 __extbuf_ = new char[__ebs_];
1081 __intbuf_ = new char_type[__ibs_];
1088 template <class _CharT, class _Traits>
1090 basic_filebuf<_CharT, _Traits>::__read_mode()
1092 if (!(__cm_ & ios_base::in))
1095 if (__always_noconv_)
1096 this->setg((char_type*)__extbuf_,
1097 (char_type*)__extbuf_ + __ebs_,
1098 (char_type*)__extbuf_ + __ebs_);
1100 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1101 __cm_ = ios_base::in;
1107 template <class _CharT, class _Traits>
1109 basic_filebuf<_CharT, _Traits>::__write_mode()
1111 if (!(__cm_ & ios_base::out))
1113 this->setg(0, 0, 0);
1114 if (__ebs_ > sizeof(__extbuf_min_))
1116 if (__always_noconv_)
1117 this->setp((char_type*)__extbuf_,
1118 (char_type*)__extbuf_ + (__ebs_ - 1));
1120 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1124 __cm_ = ios_base::out;
1130 template <class _CharT, class _Traits>
1131 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1132 : public basic_istream<_CharT, _Traits>
1135 typedef _CharT char_type;
1136 typedef _Traits traits_type;
1137 typedef typename traits_type::int_type int_type;
1138 typedef typename traits_type::pos_type pos_type;
1139 typedef typename traits_type::off_type off_type;
1141 _LIBCPP_INLINE_VISIBILITY
1143 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1144 _LIBCPP_INLINE_VISIBILITY
1145 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1146 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1147 _LIBCPP_INLINE_VISIBILITY
1148 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1150 _LIBCPP_INLINE_VISIBILITY
1151 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1152 #if _LIBCPP_STD_VER >= 17
1153 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1154 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1155 : basic_ifstream(__p.c_str(), __mode) {}
1156 #endif // _LIBCPP_STD_VER >= 17
1158 #ifndef _LIBCPP_CXX03_LANG
1159 _LIBCPP_INLINE_VISIBILITY
1160 basic_ifstream(basic_ifstream&& __rhs);
1162 _LIBCPP_INLINE_VISIBILITY
1163 basic_ifstream& operator=(basic_ifstream&& __rhs);
1165 _LIBCPP_INLINE_VISIBILITY
1166 void swap(basic_ifstream& __rhs);
1168 _LIBCPP_INLINE_VISIBILITY
1169 basic_filebuf<char_type, traits_type>* rdbuf() const;
1170 _LIBCPP_INLINE_VISIBILITY
1171 bool is_open() const;
1172 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1173 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1174 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1175 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1177 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1178 #if _LIBCPP_STD_VER >= 17
1179 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1180 void open(const filesystem::path& __p,
1181 ios_base::openmode __mode = ios_base::in) {
1182 return open(__p.c_str(), __mode);
1184 #endif // _LIBCPP_STD_VER >= 17
1186 _LIBCPP_INLINE_VISIBILITY
1187 void __open(int __fd, ios_base::openmode __mode);
1189 _LIBCPP_INLINE_VISIBILITY
1193 basic_filebuf<char_type, traits_type> __sb_;
1196 template <class _CharT, class _Traits>
1198 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1199 : basic_istream<char_type, traits_type>(&__sb_)
1203 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1204 template <class _CharT, class _Traits>
1206 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1207 : basic_istream<char_type, traits_type>(&__sb_)
1209 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1210 this->setstate(ios_base::failbit);
1213 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1214 template <class _CharT, class _Traits>
1216 basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1217 : basic_istream<char_type, traits_type>(&__sb_)
1219 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1220 this->setstate(ios_base::failbit);
1224 template <class _CharT, class _Traits>
1226 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1227 : basic_istream<char_type, traits_type>(&__sb_)
1229 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1230 this->setstate(ios_base::failbit);
1234 #ifndef _LIBCPP_CXX03_LANG
1236 template <class _CharT, class _Traits>
1238 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1239 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1240 __sb_(_VSTD::move(__rhs.__sb_))
1242 this->set_rdbuf(&__sb_);
1245 template <class _CharT, class _Traits>
1247 basic_ifstream<_CharT, _Traits>&
1248 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1250 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1251 __sb_ = _VSTD::move(__rhs.__sb_);
1255 #endif // _LIBCPP_CXX03_LANG
1257 template <class _CharT, class _Traits>
1260 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1262 basic_istream<char_type, traits_type>::swap(__rhs);
1263 __sb_.swap(__rhs.__sb_);
1266 template <class _CharT, class _Traits>
1267 inline _LIBCPP_INLINE_VISIBILITY
1269 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1274 template <class _CharT, class _Traits>
1276 basic_filebuf<_CharT, _Traits>*
1277 basic_ifstream<_CharT, _Traits>::rdbuf() const
1279 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1282 template <class _CharT, class _Traits>
1285 basic_ifstream<_CharT, _Traits>::is_open() const
1287 return __sb_.is_open();
1290 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1291 template <class _CharT, class _Traits>
1293 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1295 if (__sb_.open(__s, __mode | ios_base::in))
1298 this->setstate(ios_base::failbit);
1301 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1302 template <class _CharT, class _Traits>
1304 basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1306 if (__sb_.open(__s, __mode | ios_base::in))
1309 this->setstate(ios_base::failbit);
1313 template <class _CharT, class _Traits>
1315 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1317 if (__sb_.open(__s, __mode | ios_base::in))
1320 this->setstate(ios_base::failbit);
1323 template <class _CharT, class _Traits>
1324 void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1325 ios_base::openmode __mode) {
1326 if (__sb_.__open(__fd, __mode | ios_base::in))
1329 this->setstate(ios_base::failbit);
1333 template <class _CharT, class _Traits>
1336 basic_ifstream<_CharT, _Traits>::close()
1338 if (__sb_.close() == 0)
1339 this->setstate(ios_base::failbit);
1344 template <class _CharT, class _Traits>
1345 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1346 : public basic_ostream<_CharT, _Traits>
1349 typedef _CharT char_type;
1350 typedef _Traits traits_type;
1351 typedef typename traits_type::int_type int_type;
1352 typedef typename traits_type::pos_type pos_type;
1353 typedef typename traits_type::off_type off_type;
1355 _LIBCPP_INLINE_VISIBILITY
1357 _LIBCPP_INLINE_VISIBILITY
1358 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1359 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1360 _LIBCPP_INLINE_VISIBILITY
1361 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1363 _LIBCPP_INLINE_VISIBILITY
1364 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1366 #if _LIBCPP_STD_VER >= 17
1367 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1368 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1369 : basic_ofstream(__p.c_str(), __mode) {}
1370 #endif // _LIBCPP_STD_VER >= 17
1372 #ifndef _LIBCPP_CXX03_LANG
1373 _LIBCPP_INLINE_VISIBILITY
1374 basic_ofstream(basic_ofstream&& __rhs);
1376 _LIBCPP_INLINE_VISIBILITY
1377 basic_ofstream& operator=(basic_ofstream&& __rhs);
1379 _LIBCPP_INLINE_VISIBILITY
1380 void swap(basic_ofstream& __rhs);
1382 _LIBCPP_INLINE_VISIBILITY
1383 basic_filebuf<char_type, traits_type>* rdbuf() const;
1384 _LIBCPP_INLINE_VISIBILITY
1385 bool is_open() const;
1386 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1387 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1388 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1389 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1391 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1393 #if _LIBCPP_STD_VER >= 17
1394 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1395 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1396 { return open(__p.c_str(), __mode); }
1397 #endif // _LIBCPP_STD_VER >= 17
1399 _LIBCPP_INLINE_VISIBILITY
1400 void __open(int __fd, ios_base::openmode __mode);
1402 _LIBCPP_INLINE_VISIBILITY
1406 basic_filebuf<char_type, traits_type> __sb_;
1409 template <class _CharT, class _Traits>
1411 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1412 : basic_ostream<char_type, traits_type>(&__sb_)
1416 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1417 template <class _CharT, class _Traits>
1419 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1420 : basic_ostream<char_type, traits_type>(&__sb_)
1422 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1423 this->setstate(ios_base::failbit);
1426 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1427 template <class _CharT, class _Traits>
1429 basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1430 : basic_ostream<char_type, traits_type>(&__sb_)
1432 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1433 this->setstate(ios_base::failbit);
1437 template <class _CharT, class _Traits>
1439 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1440 : basic_ostream<char_type, traits_type>(&__sb_)
1442 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1443 this->setstate(ios_base::failbit);
1447 #ifndef _LIBCPP_CXX03_LANG
1449 template <class _CharT, class _Traits>
1451 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1452 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1453 __sb_(_VSTD::move(__rhs.__sb_))
1455 this->set_rdbuf(&__sb_);
1458 template <class _CharT, class _Traits>
1460 basic_ofstream<_CharT, _Traits>&
1461 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1463 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1464 __sb_ = _VSTD::move(__rhs.__sb_);
1468 #endif // _LIBCPP_CXX03_LANG
1470 template <class _CharT, class _Traits>
1473 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1475 basic_ostream<char_type, traits_type>::swap(__rhs);
1476 __sb_.swap(__rhs.__sb_);
1479 template <class _CharT, class _Traits>
1480 inline _LIBCPP_INLINE_VISIBILITY
1482 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1487 template <class _CharT, class _Traits>
1489 basic_filebuf<_CharT, _Traits>*
1490 basic_ofstream<_CharT, _Traits>::rdbuf() const
1492 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1495 template <class _CharT, class _Traits>
1498 basic_ofstream<_CharT, _Traits>::is_open() const
1500 return __sb_.is_open();
1503 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1504 template <class _CharT, class _Traits>
1506 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1508 if (__sb_.open(__s, __mode | ios_base::out))
1511 this->setstate(ios_base::failbit);
1514 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1515 template <class _CharT, class _Traits>
1517 basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1519 if (__sb_.open(__s, __mode | ios_base::out))
1522 this->setstate(ios_base::failbit);
1526 template <class _CharT, class _Traits>
1528 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1530 if (__sb_.open(__s, __mode | ios_base::out))
1533 this->setstate(ios_base::failbit);
1536 template <class _CharT, class _Traits>
1537 void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1538 ios_base::openmode __mode) {
1539 if (__sb_.__open(__fd, __mode | ios_base::out))
1542 this->setstate(ios_base::failbit);
1546 template <class _CharT, class _Traits>
1549 basic_ofstream<_CharT, _Traits>::close()
1551 if (__sb_.close() == 0)
1552 this->setstate(ios_base::failbit);
1557 template <class _CharT, class _Traits>
1558 class _LIBCPP_TEMPLATE_VIS basic_fstream
1559 : public basic_iostream<_CharT, _Traits>
1562 typedef _CharT char_type;
1563 typedef _Traits traits_type;
1564 typedef typename traits_type::int_type int_type;
1565 typedef typename traits_type::pos_type pos_type;
1566 typedef typename traits_type::off_type off_type;
1568 _LIBCPP_INLINE_VISIBILITY
1570 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1571 _LIBCPP_INLINE_VISIBILITY
1572 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1573 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1574 _LIBCPP_INLINE_VISIBILITY
1575 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1577 _LIBCPP_INLINE_VISIBILITY
1578 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1580 #if _LIBCPP_STD_VER >= 17
1581 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1582 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1583 : basic_fstream(__p.c_str(), __mode) {}
1584 #endif // _LIBCPP_STD_VER >= 17
1587 #ifndef _LIBCPP_CXX03_LANG
1588 _LIBCPP_INLINE_VISIBILITY
1589 basic_fstream(basic_fstream&& __rhs);
1591 _LIBCPP_INLINE_VISIBILITY
1592 basic_fstream& operator=(basic_fstream&& __rhs);
1594 _LIBCPP_INLINE_VISIBILITY
1595 void swap(basic_fstream& __rhs);
1597 _LIBCPP_INLINE_VISIBILITY
1598 basic_filebuf<char_type, traits_type>* rdbuf() const;
1599 _LIBCPP_INLINE_VISIBILITY
1600 bool is_open() const;
1601 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1602 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1603 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1604 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1606 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1608 #if _LIBCPP_STD_VER >= 17
1609 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1610 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1611 { return open(__p.c_str(), __mode); }
1612 #endif // _LIBCPP_STD_VER >= 17
1615 _LIBCPP_INLINE_VISIBILITY
1619 basic_filebuf<char_type, traits_type> __sb_;
1622 template <class _CharT, class _Traits>
1624 basic_fstream<_CharT, _Traits>::basic_fstream()
1625 : basic_iostream<char_type, traits_type>(&__sb_)
1629 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1630 template <class _CharT, class _Traits>
1632 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1633 : basic_iostream<char_type, traits_type>(&__sb_)
1635 if (__sb_.open(__s, __mode) == 0)
1636 this->setstate(ios_base::failbit);
1639 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1640 template <class _CharT, class _Traits>
1642 basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1643 : basic_iostream<char_type, traits_type>(&__sb_)
1645 if (__sb_.open(__s, __mode) == 0)
1646 this->setstate(ios_base::failbit);
1650 template <class _CharT, class _Traits>
1652 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1653 : basic_iostream<char_type, traits_type>(&__sb_)
1655 if (__sb_.open(__s, __mode) == 0)
1656 this->setstate(ios_base::failbit);
1660 #ifndef _LIBCPP_CXX03_LANG
1662 template <class _CharT, class _Traits>
1664 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1665 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1666 __sb_(_VSTD::move(__rhs.__sb_))
1668 this->set_rdbuf(&__sb_);
1671 template <class _CharT, class _Traits>
1673 basic_fstream<_CharT, _Traits>&
1674 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1676 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1677 __sb_ = _VSTD::move(__rhs.__sb_);
1681 #endif // _LIBCPP_CXX03_LANG
1683 template <class _CharT, class _Traits>
1686 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1688 basic_iostream<char_type, traits_type>::swap(__rhs);
1689 __sb_.swap(__rhs.__sb_);
1692 template <class _CharT, class _Traits>
1693 inline _LIBCPP_INLINE_VISIBILITY
1695 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1700 template <class _CharT, class _Traits>
1702 basic_filebuf<_CharT, _Traits>*
1703 basic_fstream<_CharT, _Traits>::rdbuf() const
1705 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1708 template <class _CharT, class _Traits>
1711 basic_fstream<_CharT, _Traits>::is_open() const
1713 return __sb_.is_open();
1716 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1717 template <class _CharT, class _Traits>
1719 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1721 if (__sb_.open(__s, __mode))
1724 this->setstate(ios_base::failbit);
1727 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1728 template <class _CharT, class _Traits>
1730 basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1732 if (__sb_.open(__s, __mode))
1735 this->setstate(ios_base::failbit);
1739 template <class _CharT, class _Traits>
1741 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1743 if (__sb_.open(__s, __mode))
1746 this->setstate(ios_base::failbit);
1750 template <class _CharT, class _Traits>
1753 basic_fstream<_CharT, _Traits>::close()
1755 if (__sb_.close() == 0)
1756 this->setstate(ios_base::failbit);
1759 _LIBCPP_END_NAMESPACE_STD
1763 #endif // _LIBCPP_FSTREAM