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