2 //===----------------------------------------------------------------------===//
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;
182 #include <__availability>
192 #if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
193 # include <filesystem>
196 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
197 #pragma GCC system_header
201 #include <__undef_macros>
203 #if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
204 # define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
207 _LIBCPP_BEGIN_NAMESPACE_STD
209 template <class _CharT, class _Traits>
210 class _LIBCPP_TEMPLATE_VIS basic_filebuf
211 : public basic_streambuf<_CharT, _Traits>
214 typedef _CharT char_type;
215 typedef _Traits traits_type;
216 typedef typename traits_type::int_type int_type;
217 typedef typename traits_type::pos_type pos_type;
218 typedef typename traits_type::off_type off_type;
219 typedef typename traits_type::state_type state_type;
221 // 27.9.1.2 Constructors/destructor:
223 basic_filebuf(basic_filebuf&& __rhs);
224 virtual ~basic_filebuf();
226 // 27.9.1.3 Assign/swap:
227 _LIBCPP_INLINE_VISIBILITY
228 basic_filebuf& operator=(basic_filebuf&& __rhs);
229 void swap(basic_filebuf& __rhs);
232 _LIBCPP_INLINE_VISIBILITY
233 bool is_open() const;
234 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
235 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
236 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
238 _LIBCPP_INLINE_VISIBILITY
239 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
241 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
242 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
243 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
244 return open(__p.c_str(), __mode);
247 _LIBCPP_INLINE_VISIBILITY
248 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
249 basic_filebuf* close();
251 _LIBCPP_INLINE_VISIBILITY
252 inline static const char*
253 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
256 // 27.9.1.5 Overridden virtual functions:
257 virtual int_type underflow();
258 virtual int_type pbackfail(int_type __c = traits_type::eof());
259 virtual int_type overflow (int_type __c = traits_type::eof());
260 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
261 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
262 ios_base::openmode __wch = ios_base::in | ios_base::out);
263 virtual pos_type seekpos(pos_type __sp,
264 ios_base::openmode __wch = ios_base::in | ios_base::out);
266 virtual void imbue(const locale& __loc);
270 const char* __extbufnext_;
271 const char* __extbufend_;
272 char __extbuf_min_[8];
274 char_type* __intbuf_;
277 const codecvt<char_type, char, state_type>* __cv_;
279 state_type __st_last_;
280 ios_base::openmode __om_;
281 ios_base::openmode __cm_;
284 bool __always_noconv_;
290 template <class _CharT, class _Traits>
291 basic_filebuf<_CharT, _Traits>::basic_filebuf()
292 : __extbuf_(nullptr),
293 __extbufnext_(nullptr),
294 __extbufend_(nullptr),
306 __always_noconv_(false)
308 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
310 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
311 __always_noconv_ = __cv_->always_noconv();
313 setbuf(nullptr, 4096);
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()));
363 __rhs.__extbuf_ = nullptr;
364 __rhs.__extbufnext_ = nullptr;
365 __rhs.__extbufend_ = nullptr;
369 __rhs.__file_ = nullptr;
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 template <class _CharT, class _Traits>
391 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
393 #ifndef _LIBCPP_NO_EXCEPTIONS
396 #endif // _LIBCPP_NO_EXCEPTIONS
398 #ifndef _LIBCPP_NO_EXCEPTIONS
403 #endif // _LIBCPP_NO_EXCEPTIONS
410 template <class _CharT, class _Traits>
412 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
414 basic_streambuf<char_type, traits_type>::swap(__rhs);
415 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
417 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
418 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
419 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
423 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
424 ptrdiff_t __le = __extbufend_ - __extbuf_;
425 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
426 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
427 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
429 __extbuf_ = __rhs.__extbuf_;
430 __rhs.__extbuf_ = __rhs.__extbuf_min_;
432 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
434 __rhs.__extbuf_ = __extbuf_;
435 __extbuf_ = __extbuf_min_;
437 __extbufnext_ = __extbuf_ + __rn;
438 __extbufend_ = __extbuf_ + __re;
439 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
440 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
442 _VSTD::swap(__ebs_, __rhs.__ebs_);
443 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
444 _VSTD::swap(__ibs_, __rhs.__ibs_);
445 _VSTD::swap(__file_, __rhs.__file_);
446 _VSTD::swap(__cv_, __rhs.__cv_);
447 _VSTD::swap(__st_, __rhs.__st_);
448 _VSTD::swap(__st_last_, __rhs.__st_last_);
449 _VSTD::swap(__om_, __rhs.__om_);
450 _VSTD::swap(__cm_, __rhs.__cm_);
451 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
452 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
453 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
454 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
456 ptrdiff_t __n = this->gptr() - this->eback();
457 ptrdiff_t __e = this->egptr() - this->eback();
458 this->setg((char_type*)__extbuf_min_,
459 (char_type*)__extbuf_min_ + __n,
460 (char_type*)__extbuf_min_ + __e);
462 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
464 ptrdiff_t __n = this->pptr() - this->pbase();
465 ptrdiff_t __e = this->epptr() - this->pbase();
466 this->setp((char_type*)__extbuf_min_,
467 (char_type*)__extbuf_min_ + __e);
470 if (__rhs.eback() == (char_type*)__extbuf_min_)
472 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
473 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
474 __rhs.setg((char_type*)__rhs.__extbuf_min_,
475 (char_type*)__rhs.__extbuf_min_ + __n,
476 (char_type*)__rhs.__extbuf_min_ + __e);
478 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
480 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
481 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
482 __rhs.setp((char_type*)__rhs.__extbuf_min_,
483 (char_type*)__rhs.__extbuf_min_ + __e);
488 template <class _CharT, class _Traits>
489 inline _LIBCPP_INLINE_VISIBILITY
491 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
496 template <class _CharT, class _Traits>
499 basic_filebuf<_CharT, _Traits>::is_open() const
501 return __file_ != nullptr;
504 template <class _CharT, class _Traits>
505 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
506 ios_base::openmode __mode) _NOEXCEPT {
507 switch (__mode & ~ios_base::ate) {
509 case ios_base::out | ios_base::trunc:
510 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
511 case ios_base::out | ios_base::app:
513 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
515 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
516 case ios_base::in | ios_base::out:
517 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
518 case ios_base::in | ios_base::out | ios_base::trunc:
519 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
520 case ios_base::in | ios_base::out | ios_base::app:
521 case ios_base::in | ios_base::app:
522 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
523 case ios_base::out | ios_base::binary:
524 case ios_base::out | ios_base::trunc | ios_base::binary:
525 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
526 case ios_base::out | ios_base::app | ios_base::binary:
527 case ios_base::app | ios_base::binary:
528 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
529 case ios_base::in | ios_base::binary:
530 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
531 case ios_base::in | ios_base::out | ios_base::binary:
532 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
533 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
534 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
535 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
536 case ios_base::in | ios_base::app | ios_base::binary:
537 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
541 _LIBCPP_UNREACHABLE();
544 template <class _CharT, class _Traits>
545 basic_filebuf<_CharT, _Traits>*
546 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
548 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
549 if (__file_ == nullptr)
551 if (const char* __mdstr = __make_mdstring(__mode)) {
553 __file_ = fopen(__s, __mdstr);
556 if (__mode & ios_base::ate) {
557 if (fseek(__file_, 0, SEEK_END)) {
570 template <class _CharT, class _Traits>
572 basic_filebuf<_CharT, _Traits>*
573 basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
574 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
575 if (__file_ == nullptr) {
576 if (const char* __mdstr = __make_mdstring(__mode)) {
578 __file_ = fdopen(__fd, __mdstr);
581 if (__mode & ios_base::ate) {
582 if (fseek(__file_, 0, SEEK_END)) {
595 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
596 // This is basically the same as the char* overload except that it uses _wfopen
597 // and long mode strings.
598 template <class _CharT, class _Traits>
599 basic_filebuf<_CharT, _Traits>*
600 basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
602 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
603 if (__file_ == nullptr)
606 const wchar_t* __mdstr;
607 switch (__mode & ~ios_base::ate)
610 case ios_base::out | ios_base::trunc:
613 case ios_base::out | ios_base::app:
620 case ios_base::in | ios_base::out:
623 case ios_base::in | ios_base::out | ios_base::trunc:
626 case ios_base::in | ios_base::out | ios_base::app:
627 case ios_base::in | ios_base::app:
630 case ios_base::out | ios_base::binary:
631 case ios_base::out | ios_base::trunc | ios_base::binary:
634 case ios_base::out | ios_base::app | ios_base::binary:
635 case ios_base::app | ios_base::binary:
638 case ios_base::in | ios_base::binary:
641 case ios_base::in | ios_base::out | ios_base::binary:
644 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
647 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
648 case ios_base::in | ios_base::app | ios_base::binary:
657 __file_ = _wfopen(__s, __mdstr);
661 if (__mode & ios_base::ate)
663 if (fseek(__file_, 0, SEEK_END))
679 template <class _CharT, class _Traits>
681 basic_filebuf<_CharT, _Traits>*
682 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
684 return open(__s.c_str(), __mode);
687 template <class _CharT, class _Traits>
688 basic_filebuf<_CharT, _Traits>*
689 basic_filebuf<_CharT, _Traits>::close()
691 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
695 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
698 if (fclose(__h.release()))
706 template <class _CharT, class _Traits>
707 typename basic_filebuf<_CharT, _Traits>::int_type
708 basic_filebuf<_CharT, _Traits>::underflow()
710 if (__file_ == nullptr)
711 return traits_type::eof();
712 bool __initial = __read_mode();
714 if (this->gptr() == nullptr)
715 this->setg(&__1buf, &__1buf+1, &__1buf+1);
716 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
717 int_type __c = traits_type::eof();
718 if (this->gptr() == this->egptr())
720 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
721 if (__always_noconv_)
723 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
724 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
727 this->setg(this->eback(),
728 this->eback() + __unget_sz,
729 this->eback() + __unget_sz + __nmemb);
730 __c = traits_type::to_int_type(*this->gptr());
735 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
736 if (__extbufend_ != __extbufnext_)
737 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
738 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
739 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
740 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
741 static_cast<size_t>(__extbufend_ - __extbufnext_));
742 codecvt_base::result __r;
744 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
750 __extbufend_ = __extbufnext_ + __nr;
752 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
753 this->eback() + __unget_sz,
754 this->eback() + __ibs_, __inext);
755 if (__r == codecvt_base::noconv)
757 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
758 (char_type*)const_cast<char *>(__extbufend_));
759 __c = traits_type::to_int_type(*this->gptr());
761 else if (__inext != this->eback() + __unget_sz)
763 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
764 __c = traits_type::to_int_type(*this->gptr());
770 __c = traits_type::to_int_type(*this->gptr());
771 if (this->eback() == &__1buf)
772 this->setg(nullptr, nullptr, nullptr);
776 template <class _CharT, class _Traits>
777 typename basic_filebuf<_CharT, _Traits>::int_type
778 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
780 if (__file_ && this->eback() < this->gptr())
782 if (traits_type::eq_int_type(__c, traits_type::eof()))
785 return traits_type::not_eof(__c);
787 if ((__om_ & ios_base::out) ||
788 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
791 *this->gptr() = traits_type::to_char_type(__c);
795 return traits_type::eof();
798 template <class _CharT, class _Traits>
799 typename basic_filebuf<_CharT, _Traits>::int_type
800 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
802 if (__file_ == nullptr)
803 return traits_type::eof();
806 char_type* __pb_save = this->pbase();
807 char_type* __epb_save = this->epptr();
808 if (!traits_type::eq_int_type(__c, traits_type::eof()))
810 if (this->pptr() == nullptr)
811 this->setp(&__1buf, &__1buf+1);
812 *this->pptr() = traits_type::to_char_type(__c);
815 if (this->pptr() != this->pbase())
817 if (__always_noconv_)
819 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
820 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
821 return traits_type::eof();
825 char* __extbe = __extbuf_;
826 codecvt_base::result __r;
832 const char_type* __e;
833 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
834 __extbuf_, __extbuf_ + __ebs_, __extbe);
835 if (__e == this->pbase())
836 return traits_type::eof();
837 if (__r == codecvt_base::noconv)
839 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
840 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
841 return traits_type::eof();
843 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
845 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
846 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
847 return traits_type::eof();
848 if (__r == codecvt_base::partial)
850 this->setp(const_cast<char_type*>(__e), this->pptr());
851 this->__pbump(this->epptr() - this->pbase());
855 return traits_type::eof();
856 } while (__r == codecvt_base::partial);
858 this->setp(__pb_save, __epb_save);
860 return traits_type::not_eof(__c);
863 template <class _CharT, class _Traits>
864 basic_streambuf<_CharT, _Traits>*
865 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
867 this->setg(nullptr, nullptr, nullptr);
868 this->setp(nullptr, nullptr);
874 if (__ebs_ > sizeof(__extbuf_min_))
876 if (__always_noconv_ && __s)
878 __extbuf_ = (char*)__s;
883 __extbuf_ = new char[__ebs_];
889 __extbuf_ = __extbuf_min_;
890 __ebs_ = sizeof(__extbuf_min_);
893 if (!__always_noconv_)
895 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
896 if (__s && __ibs_ >= sizeof(__extbuf_min_))
903 __intbuf_ = new char_type[__ibs_];
916 template <class _CharT, class _Traits>
917 typename basic_filebuf<_CharT, _Traits>::pos_type
918 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
924 int __width = __cv_->encoding();
925 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
926 return pos_type(off_type(-1));
927 // __width > 0 || __off == 0
941 return pos_type(off_type(-1));
943 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
944 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
945 return pos_type(off_type(-1));
946 pos_type __r = ftell(__file_);
948 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
949 return pos_type(off_type(-1));
950 pos_type __r = ftello(__file_);
956 template <class _CharT, class _Traits>
957 typename basic_filebuf<_CharT, _Traits>::pos_type
958 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
960 if (__file_ == nullptr || sync())
961 return pos_type(off_type(-1));
962 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
963 if (fseek(__file_, __sp, SEEK_SET))
964 return pos_type(off_type(-1));
966 if (fseeko(__file_, __sp, SEEK_SET))
967 return pos_type(off_type(-1));
969 __st_ = __sp.state();
973 template <class _CharT, class _Traits>
975 basic_filebuf<_CharT, _Traits>::sync()
977 if (__file_ == nullptr)
982 if (__cm_ & ios_base::out)
984 if (this->pptr() != this->pbase())
985 if (overflow() == traits_type::eof())
987 codecvt_base::result __r;
991 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
992 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
993 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
995 } while (__r == codecvt_base::partial);
996 if (__r == codecvt_base::error)
1001 else if (__cm_ & ios_base::in)
1004 state_type __state = __st_last_;
1005 bool __update_st = false;
1006 if (__always_noconv_)
1007 __c = this->egptr() - this->gptr();
1010 int __width = __cv_->encoding();
1011 __c = __extbufend_ - __extbufnext_;
1013 __c += __width * (this->egptr() - this->gptr());
1016 if (this->gptr() != this->egptr())
1018 const int __off = __cv_->length(__state, __extbuf_,
1020 this->gptr() - this->eback());
1021 __c += __extbufnext_ - __extbuf_ - __off;
1026 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1027 if (fseek(__file_, -__c, SEEK_CUR))
1030 if (fseeko(__file_, -__c, SEEK_CUR))
1035 __extbufnext_ = __extbufend_ = __extbuf_;
1036 this->setg(nullptr, nullptr, nullptr);
1042 template <class _CharT, class _Traits>
1044 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1047 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1048 bool __old_anc = __always_noconv_;
1049 __always_noconv_ = __cv_->always_noconv();
1050 if (__old_anc != __always_noconv_)
1052 this->setg(nullptr, nullptr, nullptr);
1053 this->setp(nullptr, nullptr);
1054 // invariant, char_type is char, else we couldn't get here
1055 if (__always_noconv_) // need to dump __intbuf_
1058 delete [] __extbuf_;
1059 __owns_eb_ = __owns_ib_;
1061 __extbuf_ = (char*)__intbuf_;
1063 __intbuf_ = nullptr;
1066 else // need to obtain an __intbuf_.
1067 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1068 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1071 __intbuf_ = (char_type*)__extbuf_;
1073 __extbuf_ = new char[__ebs_];
1079 __intbuf_ = new char_type[__ibs_];
1086 template <class _CharT, class _Traits>
1088 basic_filebuf<_CharT, _Traits>::__read_mode()
1090 if (!(__cm_ & ios_base::in))
1092 this->setp(nullptr, nullptr);
1093 if (__always_noconv_)
1094 this->setg((char_type*)__extbuf_,
1095 (char_type*)__extbuf_ + __ebs_,
1096 (char_type*)__extbuf_ + __ebs_);
1098 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1099 __cm_ = ios_base::in;
1105 template <class _CharT, class _Traits>
1107 basic_filebuf<_CharT, _Traits>::__write_mode()
1109 if (!(__cm_ & ios_base::out))
1111 this->setg(nullptr, nullptr, nullptr);
1112 if (__ebs_ > sizeof(__extbuf_min_))
1114 if (__always_noconv_)
1115 this->setp((char_type*)__extbuf_,
1116 (char_type*)__extbuf_ + (__ebs_ - 1));
1118 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1121 this->setp(nullptr, nullptr);
1122 __cm_ = ios_base::out;
1128 template <class _CharT, class _Traits>
1129 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1130 : public basic_istream<_CharT, _Traits>
1133 typedef _CharT char_type;
1134 typedef _Traits traits_type;
1135 typedef typename traits_type::int_type int_type;
1136 typedef typename traits_type::pos_type pos_type;
1137 typedef typename traits_type::off_type off_type;
1139 _LIBCPP_INLINE_VISIBILITY
1141 _LIBCPP_INLINE_VISIBILITY
1142 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1143 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1144 _LIBCPP_INLINE_VISIBILITY
1145 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1147 _LIBCPP_INLINE_VISIBILITY
1148 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1149 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1150 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1151 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1152 : basic_ifstream(__p.c_str(), __mode) {}
1153 #endif // _LIBCPP_STD_VER >= 17
1154 _LIBCPP_INLINE_VISIBILITY
1155 basic_ifstream(basic_ifstream&& __rhs);
1156 _LIBCPP_INLINE_VISIBILITY
1157 basic_ifstream& operator=(basic_ifstream&& __rhs);
1158 _LIBCPP_INLINE_VISIBILITY
1159 void swap(basic_ifstream& __rhs);
1161 _LIBCPP_INLINE_VISIBILITY
1162 basic_filebuf<char_type, traits_type>* rdbuf() const;
1163 _LIBCPP_INLINE_VISIBILITY
1164 bool is_open() const;
1165 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1166 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1167 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1169 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1170 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1171 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1172 void open(const filesystem::path& __p,
1173 ios_base::openmode __mode = ios_base::in) {
1174 return open(__p.c_str(), __mode);
1176 #endif // _LIBCPP_STD_VER >= 17
1178 _LIBCPP_INLINE_VISIBILITY
1179 void __open(int __fd, ios_base::openmode __mode);
1180 _LIBCPP_INLINE_VISIBILITY
1184 basic_filebuf<char_type, traits_type> __sb_;
1187 template <class _CharT, class _Traits>
1189 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1190 : basic_istream<char_type, traits_type>(&__sb_)
1194 template <class _CharT, class _Traits>
1196 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1197 : basic_istream<char_type, traits_type>(&__sb_)
1199 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1200 this->setstate(ios_base::failbit);
1203 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1204 template <class _CharT, class _Traits>
1206 basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1207 : basic_istream<char_type, traits_type>(&__sb_)
1209 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1210 this->setstate(ios_base::failbit);
1214 template <class _CharT, class _Traits>
1216 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1217 : basic_istream<char_type, traits_type>(&__sb_)
1219 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1220 this->setstate(ios_base::failbit);
1223 template <class _CharT, class _Traits>
1225 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1226 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1227 __sb_(_VSTD::move(__rhs.__sb_))
1229 this->set_rdbuf(&__sb_);
1232 template <class _CharT, class _Traits>
1234 basic_ifstream<_CharT, _Traits>&
1235 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1237 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1238 __sb_ = _VSTD::move(__rhs.__sb_);
1242 template <class _CharT, class _Traits>
1245 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1247 basic_istream<char_type, traits_type>::swap(__rhs);
1248 __sb_.swap(__rhs.__sb_);
1251 template <class _CharT, class _Traits>
1252 inline _LIBCPP_INLINE_VISIBILITY
1254 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1259 template <class _CharT, class _Traits>
1261 basic_filebuf<_CharT, _Traits>*
1262 basic_ifstream<_CharT, _Traits>::rdbuf() const
1264 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1267 template <class _CharT, class _Traits>
1270 basic_ifstream<_CharT, _Traits>::is_open() const
1272 return __sb_.is_open();
1275 template <class _CharT, class _Traits>
1277 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1279 if (__sb_.open(__s, __mode | ios_base::in))
1282 this->setstate(ios_base::failbit);
1285 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1286 template <class _CharT, class _Traits>
1288 basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1290 if (__sb_.open(__s, __mode | ios_base::in))
1293 this->setstate(ios_base::failbit);
1297 template <class _CharT, class _Traits>
1299 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1301 if (__sb_.open(__s, __mode | ios_base::in))
1304 this->setstate(ios_base::failbit);
1307 template <class _CharT, class _Traits>
1309 void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1310 ios_base::openmode __mode) {
1311 if (__sb_.__open(__fd, __mode | ios_base::in))
1314 this->setstate(ios_base::failbit);
1317 template <class _CharT, class _Traits>
1320 basic_ifstream<_CharT, _Traits>::close()
1322 if (__sb_.close() == 0)
1323 this->setstate(ios_base::failbit);
1328 template <class _CharT, class _Traits>
1329 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1330 : public basic_ostream<_CharT, _Traits>
1333 typedef _CharT char_type;
1334 typedef _Traits traits_type;
1335 typedef typename traits_type::int_type int_type;
1336 typedef typename traits_type::pos_type pos_type;
1337 typedef typename traits_type::off_type off_type;
1339 _LIBCPP_INLINE_VISIBILITY
1341 _LIBCPP_INLINE_VISIBILITY
1342 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1343 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1344 _LIBCPP_INLINE_VISIBILITY
1345 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1347 _LIBCPP_INLINE_VISIBILITY
1348 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1350 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1351 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1352 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1353 : basic_ofstream(__p.c_str(), __mode) {}
1354 #endif // _LIBCPP_STD_VER >= 17
1356 _LIBCPP_INLINE_VISIBILITY
1357 basic_ofstream(basic_ofstream&& __rhs);
1358 _LIBCPP_INLINE_VISIBILITY
1359 basic_ofstream& operator=(basic_ofstream&& __rhs);
1360 _LIBCPP_INLINE_VISIBILITY
1361 void swap(basic_ofstream& __rhs);
1363 _LIBCPP_INLINE_VISIBILITY
1364 basic_filebuf<char_type, traits_type>* rdbuf() const;
1365 _LIBCPP_INLINE_VISIBILITY
1366 bool is_open() const;
1367 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1368 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1369 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1371 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1373 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1374 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1375 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1376 { return open(__p.c_str(), __mode); }
1377 #endif // _LIBCPP_STD_VER >= 17
1379 _LIBCPP_INLINE_VISIBILITY
1380 void __open(int __fd, ios_base::openmode __mode);
1381 _LIBCPP_INLINE_VISIBILITY
1385 basic_filebuf<char_type, traits_type> __sb_;
1388 template <class _CharT, class _Traits>
1390 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1391 : basic_ostream<char_type, traits_type>(&__sb_)
1395 template <class _CharT, class _Traits>
1397 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1398 : basic_ostream<char_type, traits_type>(&__sb_)
1400 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1401 this->setstate(ios_base::failbit);
1404 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1405 template <class _CharT, class _Traits>
1407 basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1408 : basic_ostream<char_type, traits_type>(&__sb_)
1410 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1411 this->setstate(ios_base::failbit);
1415 template <class _CharT, class _Traits>
1417 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1418 : basic_ostream<char_type, traits_type>(&__sb_)
1420 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1421 this->setstate(ios_base::failbit);
1424 template <class _CharT, class _Traits>
1426 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1427 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1428 __sb_(_VSTD::move(__rhs.__sb_))
1430 this->set_rdbuf(&__sb_);
1433 template <class _CharT, class _Traits>
1435 basic_ofstream<_CharT, _Traits>&
1436 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1438 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1439 __sb_ = _VSTD::move(__rhs.__sb_);
1443 template <class _CharT, class _Traits>
1446 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1448 basic_ostream<char_type, traits_type>::swap(__rhs);
1449 __sb_.swap(__rhs.__sb_);
1452 template <class _CharT, class _Traits>
1453 inline _LIBCPP_INLINE_VISIBILITY
1455 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1460 template <class _CharT, class _Traits>
1462 basic_filebuf<_CharT, _Traits>*
1463 basic_ofstream<_CharT, _Traits>::rdbuf() const
1465 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1468 template <class _CharT, class _Traits>
1471 basic_ofstream<_CharT, _Traits>::is_open() const
1473 return __sb_.is_open();
1476 template <class _CharT, class _Traits>
1478 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1480 if (__sb_.open(__s, __mode | ios_base::out))
1483 this->setstate(ios_base::failbit);
1486 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1487 template <class _CharT, class _Traits>
1489 basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1491 if (__sb_.open(__s, __mode | ios_base::out))
1494 this->setstate(ios_base::failbit);
1498 template <class _CharT, class _Traits>
1500 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1502 if (__sb_.open(__s, __mode | ios_base::out))
1505 this->setstate(ios_base::failbit);
1508 template <class _CharT, class _Traits>
1510 void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1511 ios_base::openmode __mode) {
1512 if (__sb_.__open(__fd, __mode | ios_base::out))
1515 this->setstate(ios_base::failbit);
1518 template <class _CharT, class _Traits>
1521 basic_ofstream<_CharT, _Traits>::close()
1523 if (__sb_.close() == nullptr)
1524 this->setstate(ios_base::failbit);
1529 template <class _CharT, class _Traits>
1530 class _LIBCPP_TEMPLATE_VIS basic_fstream
1531 : public basic_iostream<_CharT, _Traits>
1534 typedef _CharT char_type;
1535 typedef _Traits traits_type;
1536 typedef typename traits_type::int_type int_type;
1537 typedef typename traits_type::pos_type pos_type;
1538 typedef typename traits_type::off_type off_type;
1540 _LIBCPP_INLINE_VISIBILITY
1542 _LIBCPP_INLINE_VISIBILITY
1543 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1544 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1545 _LIBCPP_INLINE_VISIBILITY
1546 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1548 _LIBCPP_INLINE_VISIBILITY
1549 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1551 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1552 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1553 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1554 : basic_fstream(__p.c_str(), __mode) {}
1555 #endif // _LIBCPP_STD_VER >= 17
1557 _LIBCPP_INLINE_VISIBILITY
1558 basic_fstream(basic_fstream&& __rhs);
1560 _LIBCPP_INLINE_VISIBILITY
1561 basic_fstream& operator=(basic_fstream&& __rhs);
1563 _LIBCPP_INLINE_VISIBILITY
1564 void swap(basic_fstream& __rhs);
1566 _LIBCPP_INLINE_VISIBILITY
1567 basic_filebuf<char_type, traits_type>* rdbuf() const;
1568 _LIBCPP_INLINE_VISIBILITY
1569 bool is_open() const;
1570 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1571 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1572 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1574 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1576 #if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1577 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1578 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1579 { return open(__p.c_str(), __mode); }
1580 #endif // _LIBCPP_STD_VER >= 17
1582 _LIBCPP_INLINE_VISIBILITY
1586 basic_filebuf<char_type, traits_type> __sb_;
1589 template <class _CharT, class _Traits>
1591 basic_fstream<_CharT, _Traits>::basic_fstream()
1592 : basic_iostream<char_type, traits_type>(&__sb_)
1596 template <class _CharT, class _Traits>
1598 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1599 : basic_iostream<char_type, traits_type>(&__sb_)
1601 if (__sb_.open(__s, __mode) == nullptr)
1602 this->setstate(ios_base::failbit);
1605 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606 template <class _CharT, class _Traits>
1608 basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1609 : basic_iostream<char_type, traits_type>(&__sb_)
1611 if (__sb_.open(__s, __mode) == nullptr)
1612 this->setstate(ios_base::failbit);
1616 template <class _CharT, class _Traits>
1618 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1619 : basic_iostream<char_type, traits_type>(&__sb_)
1621 if (__sb_.open(__s, __mode) == nullptr)
1622 this->setstate(ios_base::failbit);
1625 template <class _CharT, class _Traits>
1627 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1628 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1629 __sb_(_VSTD::move(__rhs.__sb_))
1631 this->set_rdbuf(&__sb_);
1634 template <class _CharT, class _Traits>
1636 basic_fstream<_CharT, _Traits>&
1637 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1639 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1640 __sb_ = _VSTD::move(__rhs.__sb_);
1644 template <class _CharT, class _Traits>
1647 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1649 basic_iostream<char_type, traits_type>::swap(__rhs);
1650 __sb_.swap(__rhs.__sb_);
1653 template <class _CharT, class _Traits>
1654 inline _LIBCPP_INLINE_VISIBILITY
1656 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1661 template <class _CharT, class _Traits>
1663 basic_filebuf<_CharT, _Traits>*
1664 basic_fstream<_CharT, _Traits>::rdbuf() const
1666 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1669 template <class _CharT, class _Traits>
1672 basic_fstream<_CharT, _Traits>::is_open() const
1674 return __sb_.is_open();
1677 template <class _CharT, class _Traits>
1679 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1681 if (__sb_.open(__s, __mode))
1684 this->setstate(ios_base::failbit);
1687 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1688 template <class _CharT, class _Traits>
1690 basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1692 if (__sb_.open(__s, __mode))
1695 this->setstate(ios_base::failbit);
1699 template <class _CharT, class _Traits>
1701 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1703 if (__sb_.open(__s, __mode))
1706 this->setstate(ios_base::failbit);
1709 template <class _CharT, class _Traits>
1712 basic_fstream<_CharT, _Traits>::close()
1714 if (__sb_.close() == nullptr)
1715 this->setstate(ios_base::failbit);
1718 #if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1719 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1720 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1721 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1724 _LIBCPP_END_NAMESPACE_STD
1728 #endif // _LIBCPP_FSTREAM