Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/__cxx03/fstream is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03_FSTREAM
0011 #define _LIBCPP___CXX03_FSTREAM
0012 
0013 /*
0014     fstream synopsis
0015 
0016 template <class charT, class traits = char_traits<charT> >
0017 class basic_filebuf
0018     : public basic_streambuf<charT, traits>
0019 {
0020 public:
0021     typedef charT                          char_type;
0022     typedef traits                         traits_type;
0023     typedef typename traits_type::int_type int_type;
0024     typedef typename traits_type::pos_type pos_type;
0025     typedef typename traits_type::off_type off_type;
0026 
0027     // 27.9.1.2 Constructors/destructor:
0028     basic_filebuf();
0029     basic_filebuf(basic_filebuf&& rhs);
0030     virtual ~basic_filebuf();
0031 
0032     // 27.9.1.3 Assign/swap:
0033     basic_filebuf& operator=(basic_filebuf&& rhs);
0034     void swap(basic_filebuf& rhs);
0035 
0036     // 27.9.1.4 Members:
0037     bool is_open() const;
0038     basic_filebuf* open(const char* s, ios_base::openmode mode);
0039     basic_filebuf* open(const string& s, ios_base::openmode mode);
0040     basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
0041     basic_filebuf* close();
0042 
0043 protected:
0044     // 27.9.1.5 Overridden virtual functions:
0045     virtual streamsize showmanyc();
0046     virtual int_type underflow();
0047     virtual int_type uflow();
0048     virtual int_type pbackfail(int_type c = traits_type::eof());
0049     virtual int_type overflow (int_type c = traits_type::eof());
0050     virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
0051     virtual pos_type seekoff(off_type off, ios_base::seekdir way,
0052                              ios_base::openmode which = ios_base::in | ios_base::out);
0053     virtual pos_type seekpos(pos_type sp,
0054                              ios_base::openmode which = ios_base::in | ios_base::out);
0055     virtual int sync();
0056     virtual void imbue(const locale& loc);
0057 };
0058 
0059 template <class charT, class traits>
0060   void
0061   swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
0062 
0063 typedef basic_filebuf<char>    filebuf;
0064 typedef basic_filebuf<wchar_t> wfilebuf;
0065 
0066 template <class charT, class traits = char_traits<charT> >
0067 class basic_ifstream
0068     : public basic_istream<charT,traits>
0069 {
0070 public:
0071     typedef charT                          char_type;
0072     typedef traits                         traits_type;
0073     typedef typename traits_type::int_type int_type;
0074     typedef typename traits_type::pos_type pos_type;
0075     typedef typename traits_type::off_type off_type;
0076     using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
0077 
0078     basic_ifstream();
0079     explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
0080     explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
0081     template<class T>
0082     explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17
0083     basic_ifstream(basic_ifstream&& rhs);
0084 
0085     basic_ifstream& operator=(basic_ifstream&& rhs);
0086     void swap(basic_ifstream& rhs);
0087 
0088     basic_filebuf<char_type, traits_type>* rdbuf() const;
0089     native_handle_type native_handle() const noexcept; // Since C++26
0090     bool is_open() const;
0091     void open(const char* s, ios_base::openmode mode = ios_base::in);
0092     void open(const string& s, ios_base::openmode mode = ios_base::in);
0093     void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
0094 
0095     void close();
0096 };
0097 
0098 template <class charT, class traits>
0099   void
0100   swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
0101 
0102 typedef basic_ifstream<char>    ifstream;
0103 typedef basic_ifstream<wchar_t> wifstream;
0104 
0105 template <class charT, class traits = char_traits<charT> >
0106 class basic_ofstream
0107     : public basic_ostream<charT,traits>
0108 {
0109 public:
0110     typedef charT                          char_type;
0111     typedef traits                         traits_type;
0112     typedef typename traits_type::int_type int_type;
0113     typedef typename traits_type::pos_type pos_type;
0114     typedef typename traits_type::off_type off_type;
0115     using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
0116 
0117     basic_ofstream();
0118     explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
0119     explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
0120     template<class T>
0121     explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
0122     basic_ofstream(basic_ofstream&& rhs);
0123 
0124     basic_ofstream& operator=(basic_ofstream&& rhs);
0125     void swap(basic_ofstream& rhs);
0126 
0127     basic_filebuf<char_type, traits_type>* rdbuf() const;
0128     native_handle_type native_handle() const noexcept; // Since C++26
0129 
0130     bool is_open() const;
0131     void open(const char* s, ios_base::openmode mode = ios_base::out);
0132     void open(const string& s, ios_base::openmode mode = ios_base::out);
0133     void open(const filesystem::path& p,
0134               ios_base::openmode mode = ios_base::out); // C++17
0135 
0136     void close();
0137 };
0138 
0139 template <class charT, class traits>
0140   void
0141   swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
0142 
0143 typedef basic_ofstream<char>    ofstream;
0144 typedef basic_ofstream<wchar_t> wofstream;
0145 
0146 template <class charT, class traits=char_traits<charT> >
0147 class basic_fstream
0148     : public basic_iostream<charT,traits>
0149 {
0150 public:
0151     typedef charT                          char_type;
0152     typedef traits                         traits_type;
0153     typedef typename traits_type::int_type int_type;
0154     typedef typename traits_type::pos_type pos_type;
0155     typedef typename traits_type::off_type off_type;
0156     using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
0157 
0158     basic_fstream();
0159     explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
0160     explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
0161     template<class T>
0162     explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); // Since C++17
0163     basic_fstream(basic_fstream&& rhs);
0164 
0165     basic_fstream& operator=(basic_fstream&& rhs);
0166     void swap(basic_fstream& rhs);
0167 
0168     basic_filebuf<char_type, traits_type>* rdbuf() const;
0169     native_handle_type native_handle() const noexcept; // Since C++26
0170     bool is_open() const;
0171     void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
0172     void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
0173     void open(const filesystem::path& s,
0174               ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
0175 
0176     void close();
0177 };
0178 
0179 template <class charT, class traits>
0180   void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
0181 
0182 typedef basic_fstream<char>    fstream;
0183 typedef basic_fstream<wchar_t> wfstream;
0184 
0185 }  // std
0186 
0187 */
0188 
0189 #include <__cxx03/__algorithm/max.h>
0190 #include <__cxx03/__assert>
0191 #include <__cxx03/__config>
0192 #include <__cxx03/__fwd/fstream.h>
0193 #include <__cxx03/__locale>
0194 #include <__cxx03/__type_traits/enable_if.h>
0195 #include <__cxx03/__type_traits/is_same.h>
0196 #include <__cxx03/__utility/move.h>
0197 #include <__cxx03/__utility/swap.h>
0198 #include <__cxx03/__utility/unreachable.h>
0199 #include <__cxx03/cstdio>
0200 #include <__cxx03/filesystem>
0201 #include <__cxx03/istream>
0202 #include <__cxx03/ostream>
0203 #include <__cxx03/typeinfo>
0204 #include <__cxx03/version>
0205 
0206 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0207 #  pragma GCC system_header
0208 #endif
0209 
0210 _LIBCPP_PUSH_MACROS
0211 #include <__cxx03/__undef_macros>
0212 
0213 #if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
0214 #  define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
0215 #endif
0216 
0217 #if !defined(_LIBCPP_HAS_NO_FILESYSTEM)
0218 
0219 _LIBCPP_BEGIN_NAMESPACE_STD
0220 
0221 #  if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
0222 _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
0223 #  endif
0224 
0225 template <class _CharT, class _Traits>
0226 class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
0227 public:
0228   typedef _CharT char_type;
0229   typedef _Traits traits_type;
0230   typedef typename traits_type::int_type int_type;
0231   typedef typename traits_type::pos_type pos_type;
0232   typedef typename traits_type::off_type off_type;
0233   typedef typename traits_type::state_type state_type;
0234 #  if _LIBCPP_STD_VER >= 26
0235 #    if defined(_LIBCPP_WIN32API)
0236   using native_handle_type = void*; // HANDLE
0237 #    elif __has_include(<unistd.h>)
0238   using native_handle_type = int; // POSIX file descriptor
0239 #    else
0240 #      error "Provide a native file handle!"
0241 #    endif
0242 #  endif
0243 
0244   // 27.9.1.2 Constructors/destructor:
0245   basic_filebuf();
0246   basic_filebuf(basic_filebuf&& __rhs);
0247   ~basic_filebuf() override;
0248 
0249   // 27.9.1.3 Assign/swap:
0250   _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
0251   void swap(basic_filebuf& __rhs);
0252 
0253   // 27.9.1.4 Members:
0254   _LIBCPP_HIDE_FROM_ABI bool is_open() const;
0255   basic_filebuf* open(const char* __s, ios_base::openmode __mode);
0256 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
0257   basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
0258 #  endif
0259   _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
0260 
0261 #  if _LIBCPP_STD_VER >= 17
0262   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
0263   open(const filesystem::path& __p, ios_base::openmode __mode) {
0264     return open(__p.c_str(), __mode);
0265   }
0266 #  endif
0267   _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
0268   basic_filebuf* close();
0269 #  if _LIBCPP_STD_VER >= 26
0270   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
0271     _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
0272 #    if defined(_LIBCPP_WIN32API)
0273     return std::__filebuf_windows_native_handle(__file_);
0274 #    elif __has_include(<unistd.h>)
0275     return fileno(__file_);
0276 #    else
0277 #      error "Provide a way to determine the file native handle!"
0278 #    endif
0279   }
0280 #  endif //  _LIBCPP_STD_VER >= 26
0281 
0282   _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
0283 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
0284   _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
0285 #  endif
0286 
0287 protected:
0288   // 27.9.1.5 Overridden virtual functions:
0289   int_type underflow() override;
0290   int_type pbackfail(int_type __c = traits_type::eof()) override;
0291   int_type overflow(int_type __c = traits_type::eof()) override;
0292   basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
0293   pos_type
0294   seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
0295   pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
0296   int sync() override;
0297   void imbue(const locale& __loc) override;
0298 
0299 private:
0300   char* __extbuf_;
0301   const char* __extbufnext_;
0302   const char* __extbufend_;
0303   char __extbuf_min_[8];
0304   size_t __ebs_;
0305   char_type* __intbuf_;
0306   size_t __ibs_;
0307   FILE* __file_;
0308   const codecvt<char_type, char, state_type>* __cv_;
0309   state_type __st_;
0310   state_type __st_last_;
0311   ios_base::openmode __om_;
0312   // There have been no file operations yet, which allows setting unbuffered
0313   // I/O mode.
0314   static const ios_base::openmode __no_io_operations = ios_base::trunc;
0315   // Unbuffered I/O mode has been requested.
0316   static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
0317   // Used to track the currently used mode and track whether the output should
0318   // be unbuffered.
0319   // [filebuf.virtuals]/12
0320   //   If setbuf(0, 0) is called on a stream before any I/O has occurred on
0321   //   that stream, the stream becomes unbuffered. Otherwise the results are
0322   //   implementation-defined.
0323   // This allows calling setbuf(0, 0)
0324   // - before opening a file,
0325   // - after opening a file, before
0326   //   - a read
0327   //   - a write
0328   //   - a seek.
0329   // Note that opening a file with ios_base::ate does a seek operation.
0330   // Normally underflow, overflow, and sync change this flag to ios_base::in,
0331   // ios_base_out, or 0.
0332   //
0333   // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
0334   // are used to track the state of the unbuffered request. For readability
0335   // they have the aliases __no_io_operations and __use_unbuffered_io
0336   // respectively.
0337   //
0338   // The __no_io_operations and __use_unbuffered_io flags are used in the
0339   // following way:
0340   // - __no_io_operations is set upon construction to indicate the unbuffered
0341   //   state can be set.
0342   // - When requesting unbuffered output:
0343   //   - If the file is open it sets the mode.
0344   //   - Else places a request by adding the __use_unbuffered_io flag.
0345   // - When a file is opened it checks whether both __no_io_operations and
0346   //   __use_unbuffered_io are set. If so switches to unbuffered mode.
0347   // - All file I/O operations change the mode effectively clearing the
0348   //   __no_io_operations and __use_unbuffered_io flags.
0349   ios_base::openmode __cm_;
0350   bool __owns_eb_;
0351   bool __owns_ib_;
0352   bool __always_noconv_;
0353 
0354   bool __read_mode();
0355   void __write_mode();
0356 
0357   _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
0358 
0359   // There are multiple (__)open function, they use different C-API open
0360   // function. After that call these functions behave the same. This function
0361   // does that part and determines the final return value.
0362   _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {
0363     __file_ = __file;
0364     if (!__file_)
0365       return nullptr;
0366 
0367     __om_ = __mode;
0368     if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
0369       std::setbuf(__file_, nullptr);
0370       __cm_ = 0;
0371     }
0372 
0373     if (__mode & ios_base::ate) {
0374       __cm_ = 0;
0375       if (fseek(__file_, 0, SEEK_END)) {
0376         fclose(__file_);
0377         __file_ = nullptr;
0378         return nullptr;
0379       }
0380     }
0381 
0382     return this;
0383   }
0384 
0385   // If the file is already open, switch to unbuffered mode. Otherwise, record
0386   // the request to use unbuffered mode so that we use that mode when we
0387   // eventually open the file.
0388   _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
0389     if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
0390       if (__file_) {
0391         std::setbuf(__file_, nullptr);
0392         __cm_ = 0;
0393       } else {
0394         __cm_ = __no_io_operations | __use_unbuffered_io;
0395       }
0396     }
0397   }
0398 };
0399 
0400 template <class _CharT, class _Traits>
0401 basic_filebuf<_CharT, _Traits>::basic_filebuf()
0402     : __extbuf_(nullptr),
0403       __extbufnext_(nullptr),
0404       __extbufend_(nullptr),
0405       __ebs_(0),
0406       __intbuf_(nullptr),
0407       __ibs_(0),
0408       __file_(nullptr),
0409       __cv_(nullptr),
0410       __st_(),
0411       __st_last_(),
0412       __om_(0),
0413       __cm_(__no_io_operations),
0414       __owns_eb_(false),
0415       __owns_ib_(false),
0416       __always_noconv_(false) {
0417   if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
0418     __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
0419     __always_noconv_ = __cv_->always_noconv();
0420   }
0421   setbuf(nullptr, 4096);
0422 }
0423 
0424 template <class _CharT, class _Traits>
0425 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
0426   if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
0427     __extbuf_     = __extbuf_min_;
0428     __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
0429     __extbufend_  = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
0430   } else {
0431     __extbuf_     = __rhs.__extbuf_;
0432     __extbufnext_ = __rhs.__extbufnext_;
0433     __extbufend_  = __rhs.__extbufend_;
0434   }
0435   __ebs_           = __rhs.__ebs_;
0436   __intbuf_        = __rhs.__intbuf_;
0437   __ibs_           = __rhs.__ibs_;
0438   __file_          = __rhs.__file_;
0439   __cv_            = __rhs.__cv_;
0440   __st_            = __rhs.__st_;
0441   __st_last_       = __rhs.__st_last_;
0442   __om_            = __rhs.__om_;
0443   __cm_            = __rhs.__cm_;
0444   __owns_eb_       = __rhs.__owns_eb_;
0445   __owns_ib_       = __rhs.__owns_ib_;
0446   __always_noconv_ = __rhs.__always_noconv_;
0447   if (__rhs.pbase()) {
0448     if (__rhs.pbase() == __rhs.__intbuf_)
0449       this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
0450     else
0451       this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
0452     this->__pbump(__rhs.pptr() - __rhs.pbase());
0453   } else if (__rhs.eback()) {
0454     if (__rhs.eback() == __rhs.__intbuf_)
0455       this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
0456     else
0457       this->setg((char_type*)__extbuf_,
0458                  (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
0459                  (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
0460   }
0461   __rhs.__extbuf_     = nullptr;
0462   __rhs.__extbufnext_ = nullptr;
0463   __rhs.__extbufend_  = nullptr;
0464   __rhs.__ebs_        = 0;
0465   __rhs.__intbuf_     = 0;
0466   __rhs.__ibs_        = 0;
0467   __rhs.__file_       = nullptr;
0468   __rhs.__st_         = state_type();
0469   __rhs.__st_last_    = state_type();
0470   __rhs.__om_         = 0;
0471   __rhs.__cm_         = 0;
0472   __rhs.__owns_eb_    = false;
0473   __rhs.__owns_ib_    = false;
0474   __rhs.setg(0, 0, 0);
0475   __rhs.setp(0, 0);
0476 }
0477 
0478 template <class _CharT, class _Traits>
0479 inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
0480   close();
0481   swap(__rhs);
0482   return *this;
0483 }
0484 
0485 template <class _CharT, class _Traits>
0486 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
0487 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0488   try {
0489 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0490     close();
0491 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0492   } catch (...) {
0493   }
0494 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0495   if (__owns_eb_)
0496     delete[] __extbuf_;
0497   if (__owns_ib_)
0498     delete[] __intbuf_;
0499 }
0500 
0501 template <class _CharT, class _Traits>
0502 void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
0503   basic_streambuf<char_type, traits_type>::swap(__rhs);
0504   if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
0505     // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
0506     std::swap(__extbuf_, __rhs.__extbuf_);
0507     std::swap(__extbufnext_, __rhs.__extbufnext_);
0508     std::swap(__extbufend_, __rhs.__extbufend_);
0509   } else {
0510     ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
0511     ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
0512     ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
0513     ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
0514     if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
0515       // *this uses the small buffer, but __rhs doesn't.
0516       __extbuf_       = __rhs.__extbuf_;
0517       __rhs.__extbuf_ = __rhs.__extbuf_min_;
0518       std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
0519     } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
0520       // *this doesn't use the small buffer, but __rhs does.
0521       __rhs.__extbuf_ = __extbuf_;
0522       __extbuf_       = __extbuf_min_;
0523       std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
0524     } else {
0525       // Both *this and __rhs use the small buffer.
0526       char __tmp[sizeof(__extbuf_min_)];
0527       std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
0528       std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
0529       std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
0530     }
0531     __extbufnext_       = __extbuf_ + __rn;
0532     __extbufend_        = __extbuf_ + __re;
0533     __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
0534     __rhs.__extbufend_  = __rhs.__extbuf_ + __le;
0535   }
0536   std::swap(__ebs_, __rhs.__ebs_);
0537   std::swap(__intbuf_, __rhs.__intbuf_);
0538   std::swap(__ibs_, __rhs.__ibs_);
0539   std::swap(__file_, __rhs.__file_);
0540   std::swap(__cv_, __rhs.__cv_);
0541   std::swap(__st_, __rhs.__st_);
0542   std::swap(__st_last_, __rhs.__st_last_);
0543   std::swap(__om_, __rhs.__om_);
0544   std::swap(__cm_, __rhs.__cm_);
0545   std::swap(__owns_eb_, __rhs.__owns_eb_);
0546   std::swap(__owns_ib_, __rhs.__owns_ib_);
0547   std::swap(__always_noconv_, __rhs.__always_noconv_);
0548   if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
0549     ptrdiff_t __n = this->gptr() - this->eback();
0550     ptrdiff_t __e = this->egptr() - this->eback();
0551     this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
0552   } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
0553     ptrdiff_t __n = this->pptr() - this->pbase();
0554     ptrdiff_t __e = this->epptr() - this->pbase();
0555     this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
0556     this->__pbump(__n);
0557   }
0558   if (__rhs.eback() == (char_type*)__extbuf_min_) {
0559     ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
0560     ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
0561     __rhs.setg(
0562         (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
0563   } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
0564     ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
0565     ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
0566     __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
0567     __rhs.__pbump(__n);
0568   }
0569 }
0570 
0571 template <class _CharT, class _Traits>
0572 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
0573   __x.swap(__y);
0574 }
0575 
0576 template <class _CharT, class _Traits>
0577 inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
0578   return __file_ != nullptr;
0579 }
0580 
0581 template <class _CharT, class _Traits>
0582 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
0583   switch (__mode & ~ios_base::ate) {
0584   case ios_base::out:
0585   case ios_base::out | ios_base::trunc:
0586     return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
0587   case ios_base::out | ios_base::app:
0588   case ios_base::app:
0589     return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
0590   case ios_base::in:
0591     return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
0592   case ios_base::in | ios_base::out:
0593     return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
0594   case ios_base::in | ios_base::out | ios_base::trunc:
0595     return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
0596   case ios_base::in | ios_base::out | ios_base::app:
0597   case ios_base::in | ios_base::app:
0598     return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
0599   case ios_base::out | ios_base::binary:
0600   case ios_base::out | ios_base::trunc | ios_base::binary:
0601     return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
0602   case ios_base::out | ios_base::app | ios_base::binary:
0603   case ios_base::app | ios_base::binary:
0604     return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
0605   case ios_base::in | ios_base::binary:
0606     return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
0607   case ios_base::in | ios_base::out | ios_base::binary:
0608     return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
0609   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
0610     return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
0611   case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
0612   case ios_base::in | ios_base::app | ios_base::binary:
0613     return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
0614 #  if _LIBCPP_STD_VER >= 23
0615   case ios_base::out | ios_base::noreplace:
0616   case ios_base::out | ios_base::trunc | ios_base::noreplace:
0617     return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
0618   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
0619     return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
0620   case ios_base::out | ios_base::binary | ios_base::noreplace:
0621   case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
0622     return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
0623   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
0624     return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
0625 #  endif // _LIBCPP_STD_VER >= 23
0626   default:
0627     return nullptr;
0628   }
0629   __libcpp_unreachable();
0630 }
0631 
0632 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
0633 template <class _CharT, class _Traits>
0634 const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
0635   switch (__mode & ~ios_base::ate) {
0636   case ios_base::out:
0637   case ios_base::out | ios_base::trunc:
0638     return L"w";
0639   case ios_base::out | ios_base::app:
0640   case ios_base::app:
0641     return L"a";
0642   case ios_base::in:
0643     return L"r";
0644   case ios_base::in | ios_base::out:
0645     return L"r+";
0646   case ios_base::in | ios_base::out | ios_base::trunc:
0647     return L"w+";
0648   case ios_base::in | ios_base::out | ios_base::app:
0649   case ios_base::in | ios_base::app:
0650     return L"a+";
0651   case ios_base::out | ios_base::binary:
0652   case ios_base::out | ios_base::trunc | ios_base::binary:
0653     return L"wb";
0654   case ios_base::out | ios_base::app | ios_base::binary:
0655   case ios_base::app | ios_base::binary:
0656     return L"ab";
0657   case ios_base::in | ios_base::binary:
0658     return L"rb";
0659   case ios_base::in | ios_base::out | ios_base::binary:
0660     return L"r+b";
0661   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
0662     return L"w+b";
0663   case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
0664   case ios_base::in | ios_base::app | ios_base::binary:
0665     return L"a+b";
0666 #    if _LIBCPP_STD_VER >= 23
0667   case ios_base::out | ios_base::noreplace:
0668   case ios_base::out | ios_base::trunc | ios_base::noreplace:
0669     return L"wx";
0670   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
0671     return L"w+x";
0672   case ios_base::out | ios_base::binary | ios_base::noreplace:
0673   case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
0674     return L"wbx";
0675   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
0676     return L"w+bx";
0677 #    endif // _LIBCPP_STD_VER >= 23
0678   default:
0679     return nullptr;
0680   }
0681   __libcpp_unreachable();
0682 }
0683 #  endif
0684 
0685 template <class _CharT, class _Traits>
0686 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
0687   if (__file_)
0688     return nullptr;
0689   const char* __mdstr = __make_mdstring(__mode);
0690   if (!__mdstr)
0691     return nullptr;
0692 
0693   return __do_open(fopen(__s, __mdstr), __mode);
0694 }
0695 
0696 template <class _CharT, class _Traits>
0697 inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
0698   if (__file_)
0699     return nullptr;
0700   const char* __mdstr = __make_mdstring(__mode);
0701   if (!__mdstr)
0702     return nullptr;
0703 
0704   return __do_open(fdopen(__fd, __mdstr), __mode);
0705 }
0706 
0707 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
0708 // This is basically the same as the char* overload except that it uses _wfopen
0709 // and long mode strings.
0710 template <class _CharT, class _Traits>
0711 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
0712   if (__file_)
0713     return nullptr;
0714   const wchar_t* __mdstr = __make_mdwstring(__mode);
0715   if (!__mdstr)
0716     return nullptr;
0717 
0718   return __do_open(_wfopen(__s, __mdstr), __mode);
0719 }
0720 #  endif
0721 
0722 template <class _CharT, class _Traits>
0723 inline basic_filebuf<_CharT, _Traits>*
0724 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
0725   return open(__s.c_str(), __mode);
0726 }
0727 
0728 template <class _CharT, class _Traits>
0729 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
0730   basic_filebuf<_CharT, _Traits>* __rt = nullptr;
0731   if (__file_) {
0732     __rt = this;
0733     unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
0734     if (sync())
0735       __rt = nullptr;
0736     if (fclose(__h.release()))
0737       __rt = nullptr;
0738     __file_ = nullptr;
0739     setbuf(0, 0);
0740   }
0741   return __rt;
0742 }
0743 
0744 template <class _CharT, class _Traits>
0745 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
0746   if (__file_ == nullptr)
0747     return traits_type::eof();
0748   bool __initial = __read_mode();
0749   char_type __1buf;
0750   if (this->gptr() == nullptr)
0751     this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
0752   const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
0753   int_type __c            = traits_type::eof();
0754   if (this->gptr() == this->egptr()) {
0755     std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
0756     if (__always_noconv_) {
0757       size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
0758       __nmemb        = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
0759       if (__nmemb != 0) {
0760         this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
0761         __c = traits_type::to_int_type(*this->gptr());
0762       }
0763     } else {
0764       if (__extbufend_ != __extbufnext_) {
0765         _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
0766         _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
0767         std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
0768       }
0769       __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
0770       __extbufend_  = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
0771       size_t __nmemb =
0772           std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
0773       codecvt_base::result __r;
0774       __st_last_  = __st_;
0775       size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
0776       if (__nr != 0) {
0777         if (!__cv_)
0778           __throw_bad_cast();
0779 
0780         __extbufend_ = __extbufnext_ + __nr;
0781         char_type* __inext;
0782         __r = __cv_->in(
0783             __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
0784         if (__r == codecvt_base::noconv) {
0785           this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
0786           __c = traits_type::to_int_type(*this->gptr());
0787         } else if (__inext != this->eback() + __unget_sz) {
0788           this->setg(this->eback(), this->eback() + __unget_sz, __inext);
0789           __c = traits_type::to_int_type(*this->gptr());
0790         }
0791       }
0792     }
0793   } else
0794     __c = traits_type::to_int_type(*this->gptr());
0795   if (this->eback() == &__1buf)
0796     this->setg(nullptr, nullptr, nullptr);
0797   return __c;
0798 }
0799 
0800 template <class _CharT, class _Traits>
0801 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
0802   if (__file_ && this->eback() < this->gptr()) {
0803     if (traits_type::eq_int_type(__c, traits_type::eof())) {
0804       this->gbump(-1);
0805       return traits_type::not_eof(__c);
0806     }
0807     if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
0808       this->gbump(-1);
0809       *this->gptr() = traits_type::to_char_type(__c);
0810       return __c;
0811     }
0812   }
0813   return traits_type::eof();
0814 }
0815 
0816 template <class _CharT, class _Traits>
0817 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
0818   if (__file_ == nullptr)
0819     return traits_type::eof();
0820   __write_mode();
0821   char_type __1buf;
0822   char_type* __pb_save  = this->pbase();
0823   char_type* __epb_save = this->epptr();
0824   if (!traits_type::eq_int_type(__c, traits_type::eof())) {
0825     if (this->pptr() == nullptr)
0826       this->setp(&__1buf, &__1buf + 1);
0827     *this->pptr() = traits_type::to_char_type(__c);
0828     this->pbump(1);
0829   }
0830   if (this->pptr() != this->pbase()) {
0831     if (__always_noconv_) {
0832       size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
0833       if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
0834         return traits_type::eof();
0835     } else {
0836       char* __extbe = __extbuf_;
0837       codecvt_base::result __r;
0838       do {
0839         if (!__cv_)
0840           __throw_bad_cast();
0841 
0842         const char_type* __e;
0843         __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
0844         if (__e == this->pbase())
0845           return traits_type::eof();
0846         if (__r == codecvt_base::noconv) {
0847           size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
0848           if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
0849             return traits_type::eof();
0850         } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
0851           size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
0852           if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
0853             return traits_type::eof();
0854           if (__r == codecvt_base::partial) {
0855             this->setp(const_cast<char_type*>(__e), this->pptr());
0856             this->__pbump(this->epptr() - this->pbase());
0857           }
0858         } else
0859           return traits_type::eof();
0860       } while (__r == codecvt_base::partial);
0861     }
0862     this->setp(__pb_save, __epb_save);
0863   }
0864   return traits_type::not_eof(__c);
0865 }
0866 
0867 template <class _CharT, class _Traits>
0868 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
0869   this->setg(nullptr, nullptr, nullptr);
0870   this->setp(nullptr, nullptr);
0871   __request_unbuffered_mode(__s, __n);
0872   if (__owns_eb_)
0873     delete[] __extbuf_;
0874   if (__owns_ib_)
0875     delete[] __intbuf_;
0876   __ebs_ = __n;
0877   if (__ebs_ > sizeof(__extbuf_min_)) {
0878     if (__always_noconv_ && __s) {
0879       __extbuf_  = (char*)__s;
0880       __owns_eb_ = false;
0881     } else {
0882       __extbuf_  = new char[__ebs_];
0883       __owns_eb_ = true;
0884     }
0885   } else {
0886     __extbuf_  = __extbuf_min_;
0887     __ebs_     = sizeof(__extbuf_min_);
0888     __owns_eb_ = false;
0889   }
0890   if (!__always_noconv_) {
0891     __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
0892     if (__s && __ibs_ > sizeof(__extbuf_min_)) {
0893       __intbuf_  = __s;
0894       __owns_ib_ = false;
0895     } else {
0896       __intbuf_  = new char_type[__ibs_];
0897       __owns_ib_ = true;
0898     }
0899   } else {
0900     __ibs_     = 0;
0901     __intbuf_  = nullptr;
0902     __owns_ib_ = false;
0903   }
0904   return this;
0905 }
0906 
0907 template <class _CharT, class _Traits>
0908 typename basic_filebuf<_CharT, _Traits>::pos_type
0909 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
0910   if (!__cv_)
0911     __throw_bad_cast();
0912 
0913   int __width = __cv_->encoding();
0914   if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
0915     return pos_type(off_type(-1));
0916   // __width > 0 || __off == 0
0917   int __whence;
0918   switch (__way) {
0919   case ios_base::beg:
0920     __whence = SEEK_SET;
0921     break;
0922   case ios_base::cur:
0923     __whence = SEEK_CUR;
0924     break;
0925   case ios_base::end:
0926     __whence = SEEK_END;
0927     break;
0928   default:
0929     return pos_type(off_type(-1));
0930   }
0931 #  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
0932   if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
0933     return pos_type(off_type(-1));
0934   pos_type __r = ftell(__file_);
0935 #  else
0936   if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
0937     return pos_type(off_type(-1));
0938   pos_type __r = ftello(__file_);
0939 #  endif
0940   __r.state(__st_);
0941   return __r;
0942 }
0943 
0944 template <class _CharT, class _Traits>
0945 typename basic_filebuf<_CharT, _Traits>::pos_type
0946 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
0947   if (__file_ == nullptr || sync())
0948     return pos_type(off_type(-1));
0949 #  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
0950   if (fseek(__file_, __sp, SEEK_SET))
0951     return pos_type(off_type(-1));
0952 #  else
0953   if (::fseeko(__file_, __sp, SEEK_SET))
0954     return pos_type(off_type(-1));
0955 #  endif
0956   __st_ = __sp.state();
0957   return __sp;
0958 }
0959 
0960 template <class _CharT, class _Traits>
0961 int basic_filebuf<_CharT, _Traits>::sync() {
0962   if (__file_ == nullptr)
0963     return 0;
0964   if (!__cv_)
0965     __throw_bad_cast();
0966 
0967   if (__cm_ & ios_base::out) {
0968     if (this->pptr() != this->pbase())
0969       if (overflow() == traits_type::eof())
0970         return -1;
0971     codecvt_base::result __r;
0972     do {
0973       char* __extbe;
0974       __r            = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
0975       size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
0976       if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
0977         return -1;
0978     } while (__r == codecvt_base::partial);
0979     if (__r == codecvt_base::error)
0980       return -1;
0981     if (fflush(__file_))
0982       return -1;
0983   } else if (__cm_ & ios_base::in) {
0984     off_type __c;
0985     state_type __state = __st_last_;
0986     bool __update_st   = false;
0987     if (__always_noconv_)
0988       __c = this->egptr() - this->gptr();
0989     else {
0990       int __width = __cv_->encoding();
0991       __c         = __extbufend_ - __extbufnext_;
0992       if (__width > 0)
0993         __c += __width * (this->egptr() - this->gptr());
0994       else {
0995         if (this->gptr() != this->egptr()) {
0996           const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
0997           __c += __extbufnext_ - __extbuf_ - __off;
0998           __update_st = true;
0999         }
1000       }
1001     }
1002 #  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1003     if (fseek(__file_, -__c, SEEK_CUR))
1004       return -1;
1005 #  else
1006     if (::fseeko(__file_, -__c, SEEK_CUR))
1007       return -1;
1008 #  endif
1009     if (__update_st)
1010       __st_ = __state;
1011     __extbufnext_ = __extbufend_ = __extbuf_;
1012     this->setg(nullptr, nullptr, nullptr);
1013     __cm_ = 0;
1014   }
1015   return 0;
1016 }
1017 
1018 template <class _CharT, class _Traits>
1019 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1020   sync();
1021   __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1022   bool __old_anc   = __always_noconv_;
1023   __always_noconv_ = __cv_->always_noconv();
1024   if (__old_anc != __always_noconv_) {
1025     this->setg(nullptr, nullptr, nullptr);
1026     this->setp(nullptr, nullptr);
1027     // invariant, char_type is char, else we couldn't get here
1028     if (__always_noconv_) // need to dump __intbuf_
1029     {
1030       if (__owns_eb_)
1031         delete[] __extbuf_;
1032       __owns_eb_ = __owns_ib_;
1033       __ebs_     = __ibs_;
1034       __extbuf_  = (char*)__intbuf_;
1035       __ibs_     = 0;
1036       __intbuf_  = nullptr;
1037       __owns_ib_ = false;
1038     } else // need to obtain an __intbuf_.
1039     {      // If __extbuf_ is user-supplied, use it, else new __intbuf_
1040       if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1041         __ibs_     = __ebs_;
1042         __intbuf_  = (char_type*)__extbuf_;
1043         __owns_ib_ = false;
1044         __extbuf_  = new char[__ebs_];
1045         __owns_eb_ = true;
1046       } else {
1047         __ibs_     = __ebs_;
1048         __intbuf_  = new char_type[__ibs_];
1049         __owns_ib_ = true;
1050       }
1051     }
1052   }
1053 }
1054 
1055 template <class _CharT, class _Traits>
1056 bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1057   if (!(__cm_ & ios_base::in)) {
1058     this->setp(nullptr, nullptr);
1059     if (__always_noconv_)
1060       this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1061     else
1062       this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1063     __cm_ = ios_base::in;
1064     return true;
1065   }
1066   return false;
1067 }
1068 
1069 template <class _CharT, class _Traits>
1070 void basic_filebuf<_CharT, _Traits>::__write_mode() {
1071   if (!(__cm_ & ios_base::out)) {
1072     this->setg(nullptr, nullptr, nullptr);
1073     if (__ebs_ > sizeof(__extbuf_min_)) {
1074       if (__always_noconv_)
1075         this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1076       else
1077         this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1078     } else
1079       this->setp(nullptr, nullptr);
1080     __cm_ = ios_base::out;
1081   }
1082 }
1083 
1084 // basic_ifstream
1085 
1086 template <class _CharT, class _Traits>
1087 class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1088 public:
1089   typedef _CharT char_type;
1090   typedef _Traits traits_type;
1091   typedef typename traits_type::int_type int_type;
1092   typedef typename traits_type::pos_type pos_type;
1093   typedef typename traits_type::off_type off_type;
1094 #  if _LIBCPP_STD_VER >= 26
1095   using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1096 #  endif
1097 
1098   _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1099   _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1100 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1101   _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1102 #  endif
1103   _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1104 #  if _LIBCPP_STD_VER >= 17
1105   template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1106   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1107       _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1108       : basic_ifstream(__p.c_str(), __mode) {}
1109 #  endif // _LIBCPP_STD_VER >= 17
1110   _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1111   _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1112   _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1113 
1114   _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1115 #  if _LIBCPP_STD_VER >= 26
1116   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1117 #  endif
1118   _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1119   void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1120 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1121   void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1122 #  endif
1123   void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1124 #  if _LIBCPP_STD_VER >= 17
1125   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1126   open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1127     return open(__p.c_str(), __mode);
1128   }
1129 #  endif // _LIBCPP_STD_VER >= 17
1130 
1131   _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1132   _LIBCPP_HIDE_FROM_ABI void close();
1133 
1134 private:
1135   basic_filebuf<char_type, traits_type> __sb_;
1136 };
1137 
1138 template <class _CharT, class _Traits>
1139 inline basic_ifstream<_CharT, _Traits>::basic_ifstream() : basic_istream<char_type, traits_type>(&__sb_) {}
1140 
1141 template <class _CharT, class _Traits>
1142 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1143     : basic_istream<char_type, traits_type>(&__sb_) {
1144   if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1145     this->setstate(ios_base::failbit);
1146 }
1147 
1148 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 template <class _CharT, class _Traits>
1150 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1151     : basic_istream<char_type, traits_type>(&__sb_) {
1152   if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1153     this->setstate(ios_base::failbit);
1154 }
1155 #  endif
1156 
1157 template <class _CharT, class _Traits>
1158 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1159     : basic_istream<char_type, traits_type>(&__sb_) {
1160   if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1161     this->setstate(ios_base::failbit);
1162 }
1163 
1164 template <class _CharT, class _Traits>
1165 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1166     : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1167   this->set_rdbuf(&__sb_);
1168 }
1169 
1170 template <class _CharT, class _Traits>
1171 inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1172   basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1173   __sb_ = std::move(__rhs.__sb_);
1174   return *this;
1175 }
1176 
1177 template <class _CharT, class _Traits>
1178 inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1179   basic_istream<char_type, traits_type>::swap(__rhs);
1180   __sb_.swap(__rhs.__sb_);
1181 }
1182 
1183 template <class _CharT, class _Traits>
1184 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1185   __x.swap(__y);
1186 }
1187 
1188 template <class _CharT, class _Traits>
1189 inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1190   return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1191 }
1192 
1193 template <class _CharT, class _Traits>
1194 inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1195   return __sb_.is_open();
1196 }
1197 
1198 template <class _CharT, class _Traits>
1199 void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1200   if (__sb_.open(__s, __mode | ios_base::in))
1201     this->clear();
1202   else
1203     this->setstate(ios_base::failbit);
1204 }
1205 
1206 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1207 template <class _CharT, class _Traits>
1208 void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1209   if (__sb_.open(__s, __mode | ios_base::in))
1210     this->clear();
1211   else
1212     this->setstate(ios_base::failbit);
1213 }
1214 #  endif
1215 
1216 template <class _CharT, class _Traits>
1217 void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1218   if (__sb_.open(__s, __mode | ios_base::in))
1219     this->clear();
1220   else
1221     this->setstate(ios_base::failbit);
1222 }
1223 
1224 template <class _CharT, class _Traits>
1225 inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1226   if (__sb_.__open(__fd, __mode | ios_base::in))
1227     this->clear();
1228   else
1229     this->setstate(ios_base::failbit);
1230 }
1231 
1232 template <class _CharT, class _Traits>
1233 inline void basic_ifstream<_CharT, _Traits>::close() {
1234   if (__sb_.close() == 0)
1235     this->setstate(ios_base::failbit);
1236 }
1237 
1238 // basic_ofstream
1239 
1240 template <class _CharT, class _Traits>
1241 class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1242 public:
1243   typedef _CharT char_type;
1244   typedef _Traits traits_type;
1245   typedef typename traits_type::int_type int_type;
1246   typedef typename traits_type::pos_type pos_type;
1247   typedef typename traits_type::off_type off_type;
1248 #  if _LIBCPP_STD_VER >= 26
1249   using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1250 #  endif
1251 
1252   _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1253   _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1254 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1255   _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1256 #  endif
1257   _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1258 
1259 #  if _LIBCPP_STD_VER >= 17
1260   template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1261   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1262       _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1263       : basic_ofstream(__p.c_str(), __mode) {}
1264 #  endif // _LIBCPP_STD_VER >= 17
1265 
1266   _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1267   _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1268   _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1269 
1270   _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1271 #  if _LIBCPP_STD_VER >= 26
1272   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1273 #  endif
1274   _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1275   void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1276 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1277   void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1278 #  endif
1279   void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1280 
1281 #  if _LIBCPP_STD_VER >= 17
1282   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1283   open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1284     return open(__p.c_str(), __mode);
1285   }
1286 #  endif // _LIBCPP_STD_VER >= 17
1287 
1288   _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1289   _LIBCPP_HIDE_FROM_ABI void close();
1290 
1291 private:
1292   basic_filebuf<char_type, traits_type> __sb_;
1293 };
1294 
1295 template <class _CharT, class _Traits>
1296 inline basic_ofstream<_CharT, _Traits>::basic_ofstream() : basic_ostream<char_type, traits_type>(&__sb_) {}
1297 
1298 template <class _CharT, class _Traits>
1299 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1300     : basic_ostream<char_type, traits_type>(&__sb_) {
1301   if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1302     this->setstate(ios_base::failbit);
1303 }
1304 
1305 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1306 template <class _CharT, class _Traits>
1307 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1308     : basic_ostream<char_type, traits_type>(&__sb_) {
1309   if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1310     this->setstate(ios_base::failbit);
1311 }
1312 #  endif
1313 
1314 template <class _CharT, class _Traits>
1315 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1316     : basic_ostream<char_type, traits_type>(&__sb_) {
1317   if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1318     this->setstate(ios_base::failbit);
1319 }
1320 
1321 template <class _CharT, class _Traits>
1322 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1323     : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1324   this->set_rdbuf(&__sb_);
1325 }
1326 
1327 template <class _CharT, class _Traits>
1328 inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1329   basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1330   __sb_ = std::move(__rhs.__sb_);
1331   return *this;
1332 }
1333 
1334 template <class _CharT, class _Traits>
1335 inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1336   basic_ostream<char_type, traits_type>::swap(__rhs);
1337   __sb_.swap(__rhs.__sb_);
1338 }
1339 
1340 template <class _CharT, class _Traits>
1341 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1342   __x.swap(__y);
1343 }
1344 
1345 template <class _CharT, class _Traits>
1346 inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1347   return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1348 }
1349 
1350 template <class _CharT, class _Traits>
1351 inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1352   return __sb_.is_open();
1353 }
1354 
1355 template <class _CharT, class _Traits>
1356 void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1357   if (__sb_.open(__s, __mode | ios_base::out))
1358     this->clear();
1359   else
1360     this->setstate(ios_base::failbit);
1361 }
1362 
1363 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1364 template <class _CharT, class _Traits>
1365 void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1366   if (__sb_.open(__s, __mode | ios_base::out))
1367     this->clear();
1368   else
1369     this->setstate(ios_base::failbit);
1370 }
1371 #  endif
1372 
1373 template <class _CharT, class _Traits>
1374 void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1375   if (__sb_.open(__s, __mode | ios_base::out))
1376     this->clear();
1377   else
1378     this->setstate(ios_base::failbit);
1379 }
1380 
1381 template <class _CharT, class _Traits>
1382 inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1383   if (__sb_.__open(__fd, __mode | ios_base::out))
1384     this->clear();
1385   else
1386     this->setstate(ios_base::failbit);
1387 }
1388 
1389 template <class _CharT, class _Traits>
1390 inline void basic_ofstream<_CharT, _Traits>::close() {
1391   if (__sb_.close() == nullptr)
1392     this->setstate(ios_base::failbit);
1393 }
1394 
1395 // basic_fstream
1396 
1397 template <class _CharT, class _Traits>
1398 class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1399 public:
1400   typedef _CharT char_type;
1401   typedef _Traits traits_type;
1402   typedef typename traits_type::int_type int_type;
1403   typedef typename traits_type::pos_type pos_type;
1404   typedef typename traits_type::off_type off_type;
1405 #  if _LIBCPP_STD_VER >= 26
1406   using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1407 #  endif
1408 
1409   _LIBCPP_HIDE_FROM_ABI basic_fstream();
1410   _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1411                                                ios_base::openmode __mode = ios_base::in | ios_base::out);
1412 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1413   _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1414                                                ios_base::openmode __mode = ios_base::in | ios_base::out);
1415 #  endif
1416   _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1417                                                ios_base::openmode __mode = ios_base::in | ios_base::out);
1418 
1419 #  if _LIBCPP_STD_VER >= 17
1420   template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1421   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1422       const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1423       : basic_fstream(__p.c_str(), __mode) {}
1424 #  endif // _LIBCPP_STD_VER >= 17
1425 
1426   _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1427 
1428   _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1429 
1430   _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1431 
1432   _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1433 #  if _LIBCPP_STD_VER >= 26
1434   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1435 #  endif
1436   _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1437   _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1438 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1439   void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1440 #  endif
1441   _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1442 
1443 #  if _LIBCPP_STD_VER >= 17
1444   _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1445   open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1446     return open(__p.c_str(), __mode);
1447   }
1448 #  endif // _LIBCPP_STD_VER >= 17
1449 
1450   _LIBCPP_HIDE_FROM_ABI void close();
1451 
1452 private:
1453   basic_filebuf<char_type, traits_type> __sb_;
1454 };
1455 
1456 template <class _CharT, class _Traits>
1457 inline basic_fstream<_CharT, _Traits>::basic_fstream() : basic_iostream<char_type, traits_type>(&__sb_) {}
1458 
1459 template <class _CharT, class _Traits>
1460 inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1461     : basic_iostream<char_type, traits_type>(&__sb_) {
1462   if (__sb_.open(__s, __mode) == nullptr)
1463     this->setstate(ios_base::failbit);
1464 }
1465 
1466 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1467 template <class _CharT, class _Traits>
1468 inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1469     : basic_iostream<char_type, traits_type>(&__sb_) {
1470   if (__sb_.open(__s, __mode) == nullptr)
1471     this->setstate(ios_base::failbit);
1472 }
1473 #  endif
1474 
1475 template <class _CharT, class _Traits>
1476 inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1477     : basic_iostream<char_type, traits_type>(&__sb_) {
1478   if (__sb_.open(__s, __mode) == nullptr)
1479     this->setstate(ios_base::failbit);
1480 }
1481 
1482 template <class _CharT, class _Traits>
1483 inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1484     : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1485   this->set_rdbuf(&__sb_);
1486 }
1487 
1488 template <class _CharT, class _Traits>
1489 inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1490   basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1491   __sb_ = std::move(__rhs.__sb_);
1492   return *this;
1493 }
1494 
1495 template <class _CharT, class _Traits>
1496 inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1497   basic_iostream<char_type, traits_type>::swap(__rhs);
1498   __sb_.swap(__rhs.__sb_);
1499 }
1500 
1501 template <class _CharT, class _Traits>
1502 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1503   __x.swap(__y);
1504 }
1505 
1506 template <class _CharT, class _Traits>
1507 inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1508   return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1509 }
1510 
1511 template <class _CharT, class _Traits>
1512 inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1513   return __sb_.is_open();
1514 }
1515 
1516 template <class _CharT, class _Traits>
1517 void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1518   if (__sb_.open(__s, __mode))
1519     this->clear();
1520   else
1521     this->setstate(ios_base::failbit);
1522 }
1523 
1524 #  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1525 template <class _CharT, class _Traits>
1526 void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1527   if (__sb_.open(__s, __mode))
1528     this->clear();
1529   else
1530     this->setstate(ios_base::failbit);
1531 }
1532 #  endif
1533 
1534 template <class _CharT, class _Traits>
1535 void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1536   if (__sb_.open(__s, __mode))
1537     this->clear();
1538   else
1539     this->setstate(ios_base::failbit);
1540 }
1541 
1542 template <class _CharT, class _Traits>
1543 inline void basic_fstream<_CharT, _Traits>::close() {
1544   if (__sb_.close() == nullptr)
1545     this->setstate(ios_base::failbit);
1546 }
1547 
1548 #  if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1549 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1550 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1551 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1552 #  endif
1553 
1554 _LIBCPP_END_NAMESPACE_STD
1555 
1556 #endif // _LIBCPP_HAS_NO_FILESYSTEM
1557 
1558 _LIBCPP_POP_MACROS
1559 
1560 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1561 #  include <__cxx03/atomic>
1562 #  include <__cxx03/concepts>
1563 #  include <__cxx03/cstdlib>
1564 #  include <__cxx03/iosfwd>
1565 #  include <__cxx03/limits>
1566 #  include <__cxx03/mutex>
1567 #  include <__cxx03/new>
1568 #  include <__cxx03/stdexcept>
1569 #  include <__cxx03/type_traits>
1570 #endif
1571 
1572 #endif // _LIBCPP___CXX03_FSTREAM