1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FSTREAM
11#define _LIBCPP_FSTREAM
12
13/*
14    fstream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_filebuf
18    : public basic_streambuf<charT, traits>
19{
20public:
21    typedef charT                          char_type;
22    typedef traits                         traits_type;
23    typedef typename traits_type::int_type int_type;
24    typedef typename traits_type::pos_type pos_type;
25    typedef typename traits_type::off_type off_type;
26
27    // 27.9.1.2 Constructors/destructor:
28    basic_filebuf();
29    basic_filebuf(basic_filebuf&& rhs);
30    virtual ~basic_filebuf();
31
32    // 27.9.1.3 Assign/swap:
33    basic_filebuf& operator=(basic_filebuf&& rhs);
34    void swap(basic_filebuf& rhs);
35
36    // 27.9.1.4 Members:
37    bool is_open() const;
38    basic_filebuf* open(const char* s, ios_base::openmode mode);
39    basic_filebuf* open(const string& s, ios_base::openmode mode);
40    basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
41    basic_filebuf* close();
42
43protected:
44    // 27.9.1.5 Overridden virtual functions:
45    virtual streamsize showmanyc();
46    virtual int_type underflow();
47    virtual int_type uflow();
48    virtual int_type pbackfail(int_type c = traits_type::eof());
49    virtual int_type overflow (int_type c = traits_type::eof());
50    virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52                             ios_base::openmode which = ios_base::in | ios_base::out);
53    virtual pos_type seekpos(pos_type sp,
54                             ios_base::openmode which = ios_base::in | ios_base::out);
55    virtual int sync();
56    virtual void imbue(const locale& loc);
57};
58
59template <class charT, class traits>
60  void
61  swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63typedef basic_filebuf<char>    filebuf;
64typedef basic_filebuf<wchar_t> wfilebuf;
65
66template <class charT, class traits = char_traits<charT> >
67class basic_ifstream
68    : public basic_istream<charT,traits>
69{
70public:
71    typedef charT                          char_type;
72    typedef traits                         traits_type;
73    typedef typename traits_type::int_type int_type;
74    typedef typename traits_type::pos_type pos_type;
75    typedef typename traits_type::off_type off_type;
76    using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
77
78    basic_ifstream();
79    explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
80    explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
81    template<class T>
82    explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17
83    basic_ifstream(basic_ifstream&& rhs);
84
85    basic_ifstream& operator=(basic_ifstream&& rhs);
86    void swap(basic_ifstream& rhs);
87
88    basic_filebuf<char_type, traits_type>* rdbuf() const;
89    native_handle_type native_handle() const noexcept; // Since C++26
90    bool is_open() const;
91    void open(const char* s, ios_base::openmode mode = ios_base::in);
92    void open(const string& s, ios_base::openmode mode = ios_base::in);
93    void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
94
95    void close();
96};
97
98template <class charT, class traits>
99  void
100  swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
101
102typedef basic_ifstream<char>    ifstream;
103typedef basic_ifstream<wchar_t> wifstream;
104
105template <class charT, class traits = char_traits<charT> >
106class basic_ofstream
107    : public basic_ostream<charT,traits>
108{
109public:
110    typedef charT                          char_type;
111    typedef traits                         traits_type;
112    typedef typename traits_type::int_type int_type;
113    typedef typename traits_type::pos_type pos_type;
114    typedef typename traits_type::off_type off_type;
115    using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
116
117    basic_ofstream();
118    explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
119    explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
120    template<class T>
121    explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
122    basic_ofstream(basic_ofstream&& rhs);
123
124    basic_ofstream& operator=(basic_ofstream&& rhs);
125    void swap(basic_ofstream& rhs);
126
127    basic_filebuf<char_type, traits_type>* rdbuf() const;
128    native_handle_type native_handle() const noexcept; // Since C++26
129
130    bool is_open() const;
131    void open(const char* s, ios_base::openmode mode = ios_base::out);
132    void open(const string& s, ios_base::openmode mode = ios_base::out);
133    void open(const filesystem::path& p,
134              ios_base::openmode mode = ios_base::out); // C++17
135
136    void close();
137};
138
139template <class charT, class traits>
140  void
141  swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
142
143typedef basic_ofstream<char>    ofstream;
144typedef basic_ofstream<wchar_t> wofstream;
145
146template <class charT, class traits=char_traits<charT> >
147class basic_fstream
148    : public basic_iostream<charT,traits>
149{
150public:
151    typedef charT                          char_type;
152    typedef traits                         traits_type;
153    typedef typename traits_type::int_type int_type;
154    typedef typename traits_type::pos_type pos_type;
155    typedef typename traits_type::off_type off_type;
156    using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
157
158    basic_fstream();
159    explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
160    explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
161    template<class T>
162    explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); // Since C++17
163    basic_fstream(basic_fstream&& rhs);
164
165    basic_fstream& operator=(basic_fstream&& rhs);
166    void swap(basic_fstream& rhs);
167
168    basic_filebuf<char_type, traits_type>* rdbuf() const;
169    native_handle_type native_handle() const noexcept; // Since C++26
170    bool is_open() const;
171    void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
172    void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
173    void open(const filesystem::path& s,
174              ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
175
176    void close();
177};
178
179template <class charT, class traits>
180  void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
181
182typedef basic_fstream<char>    fstream;
183typedef basic_fstream<wchar_t> wfstream;
184
185}  // std
186
187*/
188
189#include <__algorithm/max.h>
190#include <__assert>
191#include <__availability>
192#include <__config>
193#include <__fwd/fstream.h>
194#include <__locale>
195#include <__type_traits/enable_if.h>
196#include <__type_traits/is_same.h>
197#include <__utility/move.h>
198#include <__utility/swap.h>
199#include <__utility/unreachable.h>
200#include <cstdio>
201#include <filesystem>
202#include <istream>
203#include <ostream>
204#include <typeinfo>
205#include <version>
206
207#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
208#  pragma GCC system_header
209#endif
210
211_LIBCPP_PUSH_MACROS
212#include <__undef_macros>
213
214#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
215#  define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
216#endif
217
218#if !defined(_LIBCPP_HAS_NO_FILESYSTEM)
219
220_LIBCPP_BEGIN_NAMESPACE_STD
221
222#  if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
223_LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
224#  endif
225
226template <class _CharT, class _Traits>
227class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
228public:
229  typedef _CharT char_type;
230  typedef _Traits traits_type;
231  typedef typename traits_type::int_type int_type;
232  typedef typename traits_type::pos_type pos_type;
233  typedef typename traits_type::off_type off_type;
234  typedef typename traits_type::state_type state_type;
235#  if _LIBCPP_STD_VER >= 26
236#    if defined(_LIBCPP_WIN32API)
237  using native_handle_type = void*; // HANDLE
238#    elif __has_include(<unistd.h>)
239  using native_handle_type = int; // POSIX file descriptor
240#    else
241#      error "Provide a native file handle!"
242#    endif
243#  endif
244
245  // 27.9.1.2 Constructors/destructor:
246  basic_filebuf();
247  basic_filebuf(basic_filebuf&& __rhs);
248  ~basic_filebuf() override;
249
250  // 27.9.1.3 Assign/swap:
251  _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
252  void swap(basic_filebuf& __rhs);
253
254  // 27.9.1.4 Members:
255  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
256  basic_filebuf* open(const char* __s, ios_base::openmode __mode);
257#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
258  basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
259#  endif
260  _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
261
262#  if _LIBCPP_STD_VER >= 17
263  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
264  open(const filesystem::path& __p, ios_base::openmode __mode) {
265    return open(__p.c_str(), __mode);
266  }
267#  endif
268  _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
269  basic_filebuf* close();
270#  if _LIBCPP_STD_VER >= 26
271  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
272    _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
273#    if defined(_LIBCPP_WIN32API)
274    return std::__filebuf_windows_native_handle(__file_);
275#    elif __has_include(<unistd.h>)
276    return fileno(__file_);
277#    else
278#      error "Provide a way to determine the file native handle!"
279#    endif
280  }
281#  endif //  _LIBCPP_STD_VER >= 26
282
283  _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
284#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
285  _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
286#  endif
287
288protected:
289  // 27.9.1.5 Overridden virtual functions:
290  int_type underflow() override;
291  int_type pbackfail(int_type __c = traits_type::eof()) override;
292  int_type overflow(int_type __c = traits_type::eof()) override;
293  basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
294  pos_type
295  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
296  pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
297  int sync() override;
298  void imbue(const locale& __loc) override;
299
300private:
301  char* __extbuf_;
302  const char* __extbufnext_;
303  const char* __extbufend_;
304  char __extbuf_min_[8];
305  size_t __ebs_;
306  char_type* __intbuf_;
307  size_t __ibs_;
308  FILE* __file_;
309  const codecvt<char_type, char, state_type>* __cv_;
310  state_type __st_;
311  state_type __st_last_;
312  ios_base::openmode __om_;
313  // There have been no file operations yet, which allows setting unbuffered
314  // I/O mode.
315  static const ios_base::openmode __no_io_operations = ios_base::trunc;
316  // Unbuffered I/O mode has been requested.
317  static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
318  // Used to track the currently used mode and track whether the output should
319  // be unbuffered.
320  // [filebuf.virtuals]/12
321  //   If setbuf(0, 0) is called on a stream before any I/O has occurred on
322  //   that stream, the stream becomes unbuffered. Otherwise the results are
323  //   implementation-defined.
324  // This allows calling setbuf(0, 0)
325  // - before opening a file,
326  // - after opening a file, before
327  //   - a read
328  //   - a write
329  //   - a seek.
330  // Note that opening a file with ios_base::ate does a seek operation.
331  // Normally underflow, overflow, and sync change this flag to ios_base::in,
332  // ios_base_out, or 0.
333  //
334  // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
335  // are used to track the state of the unbuffered request. For readability
336  // they have the aliases __no_io_operations and __use_unbuffered_io
337  // respectively.
338  //
339  // The __no_io_operations and __use_unbuffered_io flags are used in the
340  // following way:
341  // - __no_io_operations is set upon construction to indicate the unbuffered
342  //   state can be set.
343  // - When requesting unbuffered output:
344  //   - If the file is open it sets the mode.
345  //   - Else places a request by adding the __use_unbuffered_io flag.
346  // - When a file is opened it checks whether both __no_io_operations and
347  //   __use_unbuffered_io are set. If so switches to unbuffered mode.
348  // - All file I/O operations change the mode effectively clearing the
349  //   __no_io_operations and __use_unbuffered_io flags.
350  ios_base::openmode __cm_;
351  bool __owns_eb_;
352  bool __owns_ib_;
353  bool __always_noconv_;
354
355  bool __read_mode();
356  void __write_mode();
357
358  _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
359
360  // There are multiple (__)open function, they use different C-API open
361  // function. After that call these functions behave the same. This function
362  // does that part and determines the final return value.
363  _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {
364    __file_ = __file;
365    if (!__file_)
366      return nullptr;
367
368    __om_ = __mode;
369    if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
370      std::setbuf(__file_, nullptr);
371      __cm_ = 0;
372    }
373
374    if (__mode & ios_base::ate) {
375      __cm_ = 0;
376      if (fseek(__file_, 0, SEEK_END)) {
377        fclose(__file_);
378        __file_ = nullptr;
379        return nullptr;
380      }
381    }
382
383    return this;
384  }
385
386  // If the file is already open, switch to unbuffered mode. Otherwise, record
387  // the request to use unbuffered mode so that we use that mode when we
388  // eventually open the file.
389  _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
390    if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
391      if (__file_) {
392        std::setbuf(__file_, nullptr);
393        __cm_ = 0;
394      } else {
395        __cm_ = __no_io_operations | __use_unbuffered_io;
396      }
397    }
398  }
399};
400
401template <class _CharT, class _Traits>
402basic_filebuf<_CharT, _Traits>::basic_filebuf()
403    : __extbuf_(nullptr),
404      __extbufnext_(nullptr),
405      __extbufend_(nullptr),
406      __ebs_(0),
407      __intbuf_(nullptr),
408      __ibs_(0),
409      __file_(nullptr),
410      __cv_(nullptr),
411      __st_(),
412      __st_last_(),
413      __om_(0),
414      __cm_(__no_io_operations),
415      __owns_eb_(false),
416      __owns_ib_(false),
417      __always_noconv_(false) {
418  if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
419    __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
420    __always_noconv_ = __cv_->always_noconv();
421  }
422  setbuf(nullptr, 4096);
423}
424
425template <class _CharT, class _Traits>
426basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
427  if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
428    __extbuf_     = __extbuf_min_;
429    __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
430    __extbufend_  = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
431  } else {
432    __extbuf_     = __rhs.__extbuf_;
433    __extbufnext_ = __rhs.__extbufnext_;
434    __extbufend_  = __rhs.__extbufend_;
435  }
436  __ebs_           = __rhs.__ebs_;
437  __intbuf_        = __rhs.__intbuf_;
438  __ibs_           = __rhs.__ibs_;
439  __file_          = __rhs.__file_;
440  __cv_            = __rhs.__cv_;
441  __st_            = __rhs.__st_;
442  __st_last_       = __rhs.__st_last_;
443  __om_            = __rhs.__om_;
444  __cm_            = __rhs.__cm_;
445  __owns_eb_       = __rhs.__owns_eb_;
446  __owns_ib_       = __rhs.__owns_ib_;
447  __always_noconv_ = __rhs.__always_noconv_;
448  if (__rhs.pbase()) {
449    if (__rhs.pbase() == __rhs.__intbuf_)
450      this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
451    else
452      this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
453    this->__pbump(__rhs.pptr() - __rhs.pbase());
454  } else if (__rhs.eback()) {
455    if (__rhs.eback() == __rhs.__intbuf_)
456      this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
457    else
458      this->setg((char_type*)__extbuf_,
459                 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
460                 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
461  }
462  __rhs.__extbuf_     = nullptr;
463  __rhs.__extbufnext_ = nullptr;
464  __rhs.__extbufend_  = nullptr;
465  __rhs.__ebs_        = 0;
466  __rhs.__intbuf_     = 0;
467  __rhs.__ibs_        = 0;
468  __rhs.__file_       = nullptr;
469  __rhs.__st_         = state_type();
470  __rhs.__st_last_    = state_type();
471  __rhs.__om_         = 0;
472  __rhs.__cm_         = 0;
473  __rhs.__owns_eb_    = false;
474  __rhs.__owns_ib_    = false;
475  __rhs.setg(0, 0, 0);
476  __rhs.setp(0, 0);
477}
478
479template <class _CharT, class _Traits>
480inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
481  close();
482  swap(__rhs);
483  return *this;
484}
485
486template <class _CharT, class _Traits>
487basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
488#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
489  try {
490#  endif // _LIBCPP_HAS_NO_EXCEPTIONS
491    close();
492#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
493  } catch (...) {
494  }
495#  endif // _LIBCPP_HAS_NO_EXCEPTIONS
496  if (__owns_eb_)
497    delete[] __extbuf_;
498  if (__owns_ib_)
499    delete[] __intbuf_;
500}
501
502template <class _CharT, class _Traits>
503void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
504  basic_streambuf<char_type, traits_type>::swap(__rhs);
505  if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
506    // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
507    std::swap(__extbuf_, __rhs.__extbuf_);
508    std::swap(__extbufnext_, __rhs.__extbufnext_);
509    std::swap(__extbufend_, __rhs.__extbufend_);
510  } else {
511    ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
512    ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
513    ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
514    ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
515    if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
516      // *this uses the small buffer, but __rhs doesn't.
517      __extbuf_       = __rhs.__extbuf_;
518      __rhs.__extbuf_ = __rhs.__extbuf_min_;
519      std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
520    } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
521      // *this doesn't use the small buffer, but __rhs does.
522      __rhs.__extbuf_ = __extbuf_;
523      __extbuf_       = __extbuf_min_;
524      std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
525    } else {
526      // Both *this and __rhs use the small buffer.
527      char __tmp[sizeof(__extbuf_min_)];
528      std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
529      std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
530      std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
531    }
532    __extbufnext_       = __extbuf_ + __rn;
533    __extbufend_        = __extbuf_ + __re;
534    __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
535    __rhs.__extbufend_  = __rhs.__extbuf_ + __le;
536  }
537  std::swap(__ebs_, __rhs.__ebs_);
538  std::swap(__intbuf_, __rhs.__intbuf_);
539  std::swap(__ibs_, __rhs.__ibs_);
540  std::swap(__file_, __rhs.__file_);
541  std::swap(__cv_, __rhs.__cv_);
542  std::swap(__st_, __rhs.__st_);
543  std::swap(__st_last_, __rhs.__st_last_);
544  std::swap(__om_, __rhs.__om_);
545  std::swap(__cm_, __rhs.__cm_);
546  std::swap(__owns_eb_, __rhs.__owns_eb_);
547  std::swap(__owns_ib_, __rhs.__owns_ib_);
548  std::swap(__always_noconv_, __rhs.__always_noconv_);
549  if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
550    ptrdiff_t __n = this->gptr() - this->eback();
551    ptrdiff_t __e = this->egptr() - this->eback();
552    this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
553  } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
554    ptrdiff_t __n = this->pptr() - this->pbase();
555    ptrdiff_t __e = this->epptr() - this->pbase();
556    this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
557    this->__pbump(__n);
558  }
559  if (__rhs.eback() == (char_type*)__extbuf_min_) {
560    ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
561    ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
562    __rhs.setg(
563        (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
564  } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
565    ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
566    ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
567    __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
568    __rhs.__pbump(__n);
569  }
570}
571
572template <class _CharT, class _Traits>
573inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
574  __x.swap(__y);
575}
576
577template <class _CharT, class _Traits>
578inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
579  return __file_ != nullptr;
580}
581
582template <class _CharT, class _Traits>
583const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
584  switch (__mode & ~ios_base::ate) {
585  case ios_base::out:
586  case ios_base::out | ios_base::trunc:
587    return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
588  case ios_base::out | ios_base::app:
589  case ios_base::app:
590    return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
591  case ios_base::in:
592    return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
593  case ios_base::in | ios_base::out:
594    return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
595  case ios_base::in | ios_base::out | ios_base::trunc:
596    return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
597  case ios_base::in | ios_base::out | ios_base::app:
598  case ios_base::in | ios_base::app:
599    return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
600  case ios_base::out | ios_base::binary:
601  case ios_base::out | ios_base::trunc | ios_base::binary:
602    return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
603  case ios_base::out | ios_base::app | ios_base::binary:
604  case ios_base::app | ios_base::binary:
605    return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
606  case ios_base::in | ios_base::binary:
607    return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
608  case ios_base::in | ios_base::out | ios_base::binary:
609    return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
610  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
611    return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
612  case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
613  case ios_base::in | ios_base::app | ios_base::binary:
614    return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
615#  if _LIBCPP_STD_VER >= 23
616  case ios_base::out | ios_base::noreplace:
617  case ios_base::out | ios_base::trunc | ios_base::noreplace:
618    return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
619  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
620    return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
621  case ios_base::out | ios_base::binary | ios_base::noreplace:
622  case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
623    return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
624  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
625    return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
626#  endif // _LIBCPP_STD_VER >= 23
627  default:
628    return nullptr;
629  }
630  __libcpp_unreachable();
631}
632
633#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
634template <class _CharT, class _Traits>
635const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
636  switch (__mode & ~ios_base::ate) {
637  case ios_base::out:
638  case ios_base::out | ios_base::trunc:
639    return L"w";
640  case ios_base::out | ios_base::app:
641  case ios_base::app:
642    return L"a";
643  case ios_base::in:
644    return L"r";
645  case ios_base::in | ios_base::out:
646    return L"r+";
647  case ios_base::in | ios_base::out | ios_base::trunc:
648    return L"w+";
649  case ios_base::in | ios_base::out | ios_base::app:
650  case ios_base::in | ios_base::app:
651    return L"a+";
652  case ios_base::out | ios_base::binary:
653  case ios_base::out | ios_base::trunc | ios_base::binary:
654    return L"wb";
655  case ios_base::out | ios_base::app | ios_base::binary:
656  case ios_base::app | ios_base::binary:
657    return L"ab";
658  case ios_base::in | ios_base::binary:
659    return L"rb";
660  case ios_base::in | ios_base::out | ios_base::binary:
661    return L"r+b";
662  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
663    return L"w+b";
664  case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
665  case ios_base::in | ios_base::app | ios_base::binary:
666    return L"a+b";
667#    if _LIBCPP_STD_VER >= 23
668  case ios_base::out | ios_base::noreplace:
669  case ios_base::out | ios_base::trunc | ios_base::noreplace:
670    return L"wx";
671  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
672    return L"w+x";
673  case ios_base::out | ios_base::binary | ios_base::noreplace:
674  case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
675    return L"wbx";
676  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
677    return L"w+bx";
678#    endif // _LIBCPP_STD_VER >= 23
679  default:
680    return nullptr;
681  }
682  __libcpp_unreachable();
683}
684#  endif
685
686template <class _CharT, class _Traits>
687basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
688  if (__file_)
689    return nullptr;
690  const char* __mdstr = __make_mdstring(__mode);
691  if (!__mdstr)
692    return nullptr;
693
694  return __do_open(fopen(__s, __mdstr), __mode);
695}
696
697template <class _CharT, class _Traits>
698inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
699  if (__file_)
700    return nullptr;
701  const char* __mdstr = __make_mdstring(__mode);
702  if (!__mdstr)
703    return nullptr;
704
705  return __do_open(fdopen(__fd, __mdstr), __mode);
706}
707
708#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
709// This is basically the same as the char* overload except that it uses _wfopen
710// and long mode strings.
711template <class _CharT, class _Traits>
712basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
713  if (__file_)
714    return nullptr;
715  const wchar_t* __mdstr = __make_mdwstring(__mode);
716  if (!__mdstr)
717    return nullptr;
718
719  return __do_open(_wfopen(__s, __mdstr), __mode);
720}
721#  endif
722
723template <class _CharT, class _Traits>
724inline basic_filebuf<_CharT, _Traits>*
725basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
726  return open(__s.c_str(), __mode);
727}
728
729template <class _CharT, class _Traits>
730basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
731  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
732  if (__file_) {
733    __rt = this;
734    unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
735    if (sync())
736      __rt = nullptr;
737    if (fclose(__h.release()))
738      __rt = nullptr;
739    __file_ = nullptr;
740    setbuf(0, 0);
741  }
742  return __rt;
743}
744
745template <class _CharT, class _Traits>
746typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
747  if (__file_ == nullptr)
748    return traits_type::eof();
749  bool __initial = __read_mode();
750  char_type __1buf;
751  if (this->gptr() == nullptr)
752    this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
753  const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
754  int_type __c            = traits_type::eof();
755  if (this->gptr() == this->egptr()) {
756    std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
757    if (__always_noconv_) {
758      size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
759      __nmemb        = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
760      if (__nmemb != 0) {
761        this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
762        __c = traits_type::to_int_type(*this->gptr());
763      }
764    } else {
765      if (__extbufend_ != __extbufnext_) {
766        _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
767        _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
768        std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
769      }
770      __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
771      __extbufend_  = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
772      size_t __nmemb =
773          std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
774      codecvt_base::result __r;
775      __st_last_  = __st_;
776      size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
777      if (__nr != 0) {
778        if (!__cv_)
779          __throw_bad_cast();
780
781        __extbufend_ = __extbufnext_ + __nr;
782        char_type* __inext;
783        __r = __cv_->in(
784            __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
785        if (__r == codecvt_base::noconv) {
786          this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
787          __c = traits_type::to_int_type(*this->gptr());
788        } else if (__inext != this->eback() + __unget_sz) {
789          this->setg(this->eback(), this->eback() + __unget_sz, __inext);
790          __c = traits_type::to_int_type(*this->gptr());
791        }
792      }
793    }
794  } else
795    __c = traits_type::to_int_type(*this->gptr());
796  if (this->eback() == &__1buf)
797    this->setg(nullptr, nullptr, nullptr);
798  return __c;
799}
800
801template <class _CharT, class _Traits>
802typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
803  if (__file_ && this->eback() < this->gptr()) {
804    if (traits_type::eq_int_type(__c, traits_type::eof())) {
805      this->gbump(-1);
806      return traits_type::not_eof(__c);
807    }
808    if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
809      this->gbump(-1);
810      *this->gptr() = traits_type::to_char_type(__c);
811      return __c;
812    }
813  }
814  return traits_type::eof();
815}
816
817template <class _CharT, class _Traits>
818typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
819  if (__file_ == nullptr)
820    return traits_type::eof();
821  __write_mode();
822  char_type __1buf;
823  char_type* __pb_save  = this->pbase();
824  char_type* __epb_save = this->epptr();
825  if (!traits_type::eq_int_type(__c, traits_type::eof())) {
826    if (this->pptr() == nullptr)
827      this->setp(&__1buf, &__1buf + 1);
828    *this->pptr() = traits_type::to_char_type(__c);
829    this->pbump(1);
830  }
831  if (this->pptr() != this->pbase()) {
832    if (__always_noconv_) {
833      size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
834      if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
835        return traits_type::eof();
836    } else {
837      char* __extbe = __extbuf_;
838      codecvt_base::result __r;
839      do {
840        if (!__cv_)
841          __throw_bad_cast();
842
843        const char_type* __e;
844        __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
845        if (__e == this->pbase())
846          return traits_type::eof();
847        if (__r == codecvt_base::noconv) {
848          size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
849          if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
850            return traits_type::eof();
851        } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
852          size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
853          if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
854            return traits_type::eof();
855          if (__r == codecvt_base::partial) {
856            this->setp(const_cast<char_type*>(__e), this->pptr());
857            this->__pbump(this->epptr() - this->pbase());
858          }
859        } else
860          return traits_type::eof();
861      } while (__r == codecvt_base::partial);
862    }
863    this->setp(__pb_save, __epb_save);
864  }
865  return traits_type::not_eof(__c);
866}
867
868template <class _CharT, class _Traits>
869basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
870  this->setg(nullptr, nullptr, nullptr);
871  this->setp(nullptr, nullptr);
872  __request_unbuffered_mode(__s, __n);
873  if (__owns_eb_)
874    delete[] __extbuf_;
875  if (__owns_ib_)
876    delete[] __intbuf_;
877  __ebs_ = __n;
878  if (__ebs_ > sizeof(__extbuf_min_)) {
879    if (__always_noconv_ && __s) {
880      __extbuf_  = (char*)__s;
881      __owns_eb_ = false;
882    } else {
883      __extbuf_  = new char[__ebs_];
884      __owns_eb_ = true;
885    }
886  } else {
887    __extbuf_  = __extbuf_min_;
888    __ebs_     = sizeof(__extbuf_min_);
889    __owns_eb_ = false;
890  }
891  if (!__always_noconv_) {
892    __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
893    if (__s && __ibs_ > sizeof(__extbuf_min_)) {
894      __intbuf_  = __s;
895      __owns_ib_ = false;
896    } else {
897      __intbuf_  = new char_type[__ibs_];
898      __owns_ib_ = true;
899    }
900  } else {
901    __ibs_     = 0;
902    __intbuf_  = nullptr;
903    __owns_ib_ = false;
904  }
905  return this;
906}
907
908template <class _CharT, class _Traits>
909typename basic_filebuf<_CharT, _Traits>::pos_type
910basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
911  if (!__cv_)
912    __throw_bad_cast();
913
914  int __width = __cv_->encoding();
915  if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
916    return pos_type(off_type(-1));
917  // __width > 0 || __off == 0
918  int __whence;
919  switch (__way) {
920  case ios_base::beg:
921    __whence = SEEK_SET;
922    break;
923  case ios_base::cur:
924    __whence = SEEK_CUR;
925    break;
926  case ios_base::end:
927    __whence = SEEK_END;
928    break;
929  default:
930    return pos_type(off_type(-1));
931  }
932#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
933  if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
934    return pos_type(off_type(-1));
935  pos_type __r = ftell(__file_);
936#  else
937  if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
938    return pos_type(off_type(-1));
939  pos_type __r = ftello(__file_);
940#  endif
941  __r.state(__st_);
942  return __r;
943}
944
945template <class _CharT, class _Traits>
946typename basic_filebuf<_CharT, _Traits>::pos_type
947basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
948  if (__file_ == nullptr || sync())
949    return pos_type(off_type(-1));
950#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
951  if (fseek(__file_, __sp, SEEK_SET))
952    return pos_type(off_type(-1));
953#  else
954  if (::fseeko(__file_, __sp, SEEK_SET))
955    return pos_type(off_type(-1));
956#  endif
957  __st_ = __sp.state();
958  return __sp;
959}
960
961template <class _CharT, class _Traits>
962int basic_filebuf<_CharT, _Traits>::sync() {
963  if (__file_ == nullptr)
964    return 0;
965  if (!__cv_)
966    __throw_bad_cast();
967
968  if (__cm_ & ios_base::out) {
969    if (this->pptr() != this->pbase())
970      if (overflow() == traits_type::eof())
971        return -1;
972    codecvt_base::result __r;
973    do {
974      char* __extbe;
975      __r            = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
976      size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
977      if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
978        return -1;
979    } while (__r == codecvt_base::partial);
980    if (__r == codecvt_base::error)
981      return -1;
982    if (fflush(__file_))
983      return -1;
984  } else if (__cm_ & ios_base::in) {
985    off_type __c;
986    state_type __state = __st_last_;
987    bool __update_st   = false;
988    if (__always_noconv_)
989      __c = this->egptr() - this->gptr();
990    else {
991      int __width = __cv_->encoding();
992      __c         = __extbufend_ - __extbufnext_;
993      if (__width > 0)
994        __c += __width * (this->egptr() - this->gptr());
995      else {
996        if (this->gptr() != this->egptr()) {
997          const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
998          __c += __extbufnext_ - __extbuf_ - __off;
999          __update_st = true;
1000        }
1001      }
1002    }
1003#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1004    if (fseek(__file_, -__c, SEEK_CUR))
1005      return -1;
1006#  else
1007    if (::fseeko(__file_, -__c, SEEK_CUR))
1008      return -1;
1009#  endif
1010    if (__update_st)
1011      __st_ = __state;
1012    __extbufnext_ = __extbufend_ = __extbuf_;
1013    this->setg(nullptr, nullptr, nullptr);
1014    __cm_ = 0;
1015  }
1016  return 0;
1017}
1018
1019template <class _CharT, class _Traits>
1020void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1021  sync();
1022  __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1023  bool __old_anc   = __always_noconv_;
1024  __always_noconv_ = __cv_->always_noconv();
1025  if (__old_anc != __always_noconv_) {
1026    this->setg(nullptr, nullptr, nullptr);
1027    this->setp(nullptr, nullptr);
1028    // invariant, char_type is char, else we couldn't get here
1029    if (__always_noconv_) // need to dump __intbuf_
1030    {
1031      if (__owns_eb_)
1032        delete[] __extbuf_;
1033      __owns_eb_ = __owns_ib_;
1034      __ebs_     = __ibs_;
1035      __extbuf_  = (char*)__intbuf_;
1036      __ibs_     = 0;
1037      __intbuf_  = nullptr;
1038      __owns_ib_ = false;
1039    } else // need to obtain an __intbuf_.
1040    {      // If __extbuf_ is user-supplied, use it, else new __intbuf_
1041      if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1042        __ibs_     = __ebs_;
1043        __intbuf_  = (char_type*)__extbuf_;
1044        __owns_ib_ = false;
1045        __extbuf_  = new char[__ebs_];
1046        __owns_eb_ = true;
1047      } else {
1048        __ibs_     = __ebs_;
1049        __intbuf_  = new char_type[__ibs_];
1050        __owns_ib_ = true;
1051      }
1052    }
1053  }
1054}
1055
1056template <class _CharT, class _Traits>
1057bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1058  if (!(__cm_ & ios_base::in)) {
1059    this->setp(nullptr, nullptr);
1060    if (__always_noconv_)
1061      this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1062    else
1063      this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1064    __cm_ = ios_base::in;
1065    return true;
1066  }
1067  return false;
1068}
1069
1070template <class _CharT, class _Traits>
1071void basic_filebuf<_CharT, _Traits>::__write_mode() {
1072  if (!(__cm_ & ios_base::out)) {
1073    this->setg(nullptr, nullptr, nullptr);
1074    if (__ebs_ > sizeof(__extbuf_min_)) {
1075      if (__always_noconv_)
1076        this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1077      else
1078        this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1079    } else
1080      this->setp(nullptr, nullptr);
1081    __cm_ = ios_base::out;
1082  }
1083}
1084
1085// basic_ifstream
1086
1087template <class _CharT, class _Traits>
1088class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1089public:
1090  typedef _CharT char_type;
1091  typedef _Traits traits_type;
1092  typedef typename traits_type::int_type int_type;
1093  typedef typename traits_type::pos_type pos_type;
1094  typedef typename traits_type::off_type off_type;
1095#  if _LIBCPP_STD_VER >= 26
1096  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1097#  endif
1098
1099  _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1100  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1101#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1102  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1103#  endif
1104  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1105#  if _LIBCPP_STD_VER >= 17
1106  template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1107  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(
1108      const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1109      : basic_ifstream(__p.c_str(), __mode) {}
1110#  endif // _LIBCPP_STD_VER >= 17
1111  _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1112  _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1113  _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1114
1115  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1116#  if _LIBCPP_STD_VER >= 26
1117  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1118#  endif
1119  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1120  void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1121#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1122  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1123#  endif
1124  void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1125#  if _LIBCPP_STD_VER >= 17
1126  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1127  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1128    return open(__p.c_str(), __mode);
1129  }
1130#  endif // _LIBCPP_STD_VER >= 17
1131
1132  _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1133  _LIBCPP_HIDE_FROM_ABI void close();
1134
1135private:
1136  basic_filebuf<char_type, traits_type> __sb_;
1137};
1138
1139template <class _CharT, class _Traits>
1140inline basic_ifstream<_CharT, _Traits>::basic_ifstream() : basic_istream<char_type, traits_type>(&__sb_) {}
1141
1142template <class _CharT, class _Traits>
1143inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1144    : basic_istream<char_type, traits_type>(&__sb_) {
1145  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1146    this->setstate(ios_base::failbit);
1147}
1148
1149#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1150template <class _CharT, class _Traits>
1151inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1152    : basic_istream<char_type, traits_type>(&__sb_) {
1153  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1154    this->setstate(ios_base::failbit);
1155}
1156#  endif
1157
1158template <class _CharT, class _Traits>
1159inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1160    : basic_istream<char_type, traits_type>(&__sb_) {
1161  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1162    this->setstate(ios_base::failbit);
1163}
1164
1165template <class _CharT, class _Traits>
1166inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1167    : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1168  this->set_rdbuf(&__sb_);
1169}
1170
1171template <class _CharT, class _Traits>
1172inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1173  basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1174  __sb_ = std::move(__rhs.__sb_);
1175  return *this;
1176}
1177
1178template <class _CharT, class _Traits>
1179inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1180  basic_istream<char_type, traits_type>::swap(__rhs);
1181  __sb_.swap(__rhs.__sb_);
1182}
1183
1184template <class _CharT, class _Traits>
1185inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1186  __x.swap(__y);
1187}
1188
1189template <class _CharT, class _Traits>
1190inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1191  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1192}
1193
1194template <class _CharT, class _Traits>
1195inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1196  return __sb_.is_open();
1197}
1198
1199template <class _CharT, class _Traits>
1200void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1201  if (__sb_.open(__s, __mode | ios_base::in))
1202    this->clear();
1203  else
1204    this->setstate(ios_base::failbit);
1205}
1206
1207#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1208template <class _CharT, class _Traits>
1209void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1210  if (__sb_.open(__s, __mode | ios_base::in))
1211    this->clear();
1212  else
1213    this->setstate(ios_base::failbit);
1214}
1215#  endif
1216
1217template <class _CharT, class _Traits>
1218void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1219  if (__sb_.open(__s, __mode | ios_base::in))
1220    this->clear();
1221  else
1222    this->setstate(ios_base::failbit);
1223}
1224
1225template <class _CharT, class _Traits>
1226inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1227  if (__sb_.__open(__fd, __mode | ios_base::in))
1228    this->clear();
1229  else
1230    this->setstate(ios_base::failbit);
1231}
1232
1233template <class _CharT, class _Traits>
1234inline void basic_ifstream<_CharT, _Traits>::close() {
1235  if (__sb_.close() == 0)
1236    this->setstate(ios_base::failbit);
1237}
1238
1239// basic_ofstream
1240
1241template <class _CharT, class _Traits>
1242class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1243public:
1244  typedef _CharT char_type;
1245  typedef _Traits traits_type;
1246  typedef typename traits_type::int_type int_type;
1247  typedef typename traits_type::pos_type pos_type;
1248  typedef typename traits_type::off_type off_type;
1249#  if _LIBCPP_STD_VER >= 26
1250  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1251#  endif
1252
1253  _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1254  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1255#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1256  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1257#  endif
1258  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1259
1260#  if _LIBCPP_STD_VER >= 17
1261  template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1262  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(
1263      const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1264      : basic_ofstream(__p.c_str(), __mode) {}
1265#  endif // _LIBCPP_STD_VER >= 17
1266
1267  _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1268  _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1269  _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1270
1271  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1272#  if _LIBCPP_STD_VER >= 26
1273  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1274#  endif
1275  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1276  void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1277#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1278  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1279#  endif
1280  void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1281
1282#  if _LIBCPP_STD_VER >= 17
1283  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1284  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1285    return open(__p.c_str(), __mode);
1286  }
1287#  endif // _LIBCPP_STD_VER >= 17
1288
1289  _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1290  _LIBCPP_HIDE_FROM_ABI void close();
1291
1292private:
1293  basic_filebuf<char_type, traits_type> __sb_;
1294};
1295
1296template <class _CharT, class _Traits>
1297inline basic_ofstream<_CharT, _Traits>::basic_ofstream() : basic_ostream<char_type, traits_type>(&__sb_) {}
1298
1299template <class _CharT, class _Traits>
1300inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1301    : basic_ostream<char_type, traits_type>(&__sb_) {
1302  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1303    this->setstate(ios_base::failbit);
1304}
1305
1306#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1307template <class _CharT, class _Traits>
1308inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1309    : basic_ostream<char_type, traits_type>(&__sb_) {
1310  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1311    this->setstate(ios_base::failbit);
1312}
1313#  endif
1314
1315template <class _CharT, class _Traits>
1316inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1317    : basic_ostream<char_type, traits_type>(&__sb_) {
1318  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1319    this->setstate(ios_base::failbit);
1320}
1321
1322template <class _CharT, class _Traits>
1323inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1324    : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1325  this->set_rdbuf(&__sb_);
1326}
1327
1328template <class _CharT, class _Traits>
1329inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1330  basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1331  __sb_ = std::move(__rhs.__sb_);
1332  return *this;
1333}
1334
1335template <class _CharT, class _Traits>
1336inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1337  basic_ostream<char_type, traits_type>::swap(__rhs);
1338  __sb_.swap(__rhs.__sb_);
1339}
1340
1341template <class _CharT, class _Traits>
1342inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1343  __x.swap(__y);
1344}
1345
1346template <class _CharT, class _Traits>
1347inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1348  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1349}
1350
1351template <class _CharT, class _Traits>
1352inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1353  return __sb_.is_open();
1354}
1355
1356template <class _CharT, class _Traits>
1357void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1358  if (__sb_.open(__s, __mode | ios_base::out))
1359    this->clear();
1360  else
1361    this->setstate(ios_base::failbit);
1362}
1363
1364#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1365template <class _CharT, class _Traits>
1366void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1367  if (__sb_.open(__s, __mode | ios_base::out))
1368    this->clear();
1369  else
1370    this->setstate(ios_base::failbit);
1371}
1372#  endif
1373
1374template <class _CharT, class _Traits>
1375void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1376  if (__sb_.open(__s, __mode | ios_base::out))
1377    this->clear();
1378  else
1379    this->setstate(ios_base::failbit);
1380}
1381
1382template <class _CharT, class _Traits>
1383inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1384  if (__sb_.__open(__fd, __mode | ios_base::out))
1385    this->clear();
1386  else
1387    this->setstate(ios_base::failbit);
1388}
1389
1390template <class _CharT, class _Traits>
1391inline void basic_ofstream<_CharT, _Traits>::close() {
1392  if (__sb_.close() == nullptr)
1393    this->setstate(ios_base::failbit);
1394}
1395
1396// basic_fstream
1397
1398template <class _CharT, class _Traits>
1399class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1400public:
1401  typedef _CharT char_type;
1402  typedef _Traits traits_type;
1403  typedef typename traits_type::int_type int_type;
1404  typedef typename traits_type::pos_type pos_type;
1405  typedef typename traits_type::off_type off_type;
1406#  if _LIBCPP_STD_VER >= 26
1407  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1408#  endif
1409
1410  _LIBCPP_HIDE_FROM_ABI basic_fstream();
1411  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1412                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1413#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1414  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1415                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1416#  endif
1417  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1418                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1419
1420#  if _LIBCPP_STD_VER >= 17
1421  template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1422  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1423      const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1424      : basic_fstream(__p.c_str(), __mode) {}
1425#  endif // _LIBCPP_STD_VER >= 17
1426
1427  _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1428
1429  _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1430
1431  _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1432
1433  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1434#  if _LIBCPP_STD_VER >= 26
1435  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1436#  endif
1437  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1438  _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1439#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1440  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1441#  endif
1442  _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1443
1444#  if _LIBCPP_STD_VER >= 17
1445  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1446  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1447    return open(__p.c_str(), __mode);
1448  }
1449#  endif // _LIBCPP_STD_VER >= 17
1450
1451  _LIBCPP_HIDE_FROM_ABI void close();
1452
1453private:
1454  basic_filebuf<char_type, traits_type> __sb_;
1455};
1456
1457template <class _CharT, class _Traits>
1458inline basic_fstream<_CharT, _Traits>::basic_fstream() : basic_iostream<char_type, traits_type>(&__sb_) {}
1459
1460template <class _CharT, class _Traits>
1461inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1462    : basic_iostream<char_type, traits_type>(&__sb_) {
1463  if (__sb_.open(__s, __mode) == nullptr)
1464    this->setstate(ios_base::failbit);
1465}
1466
1467#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1468template <class _CharT, class _Traits>
1469inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1470    : basic_iostream<char_type, traits_type>(&__sb_) {
1471  if (__sb_.open(__s, __mode) == nullptr)
1472    this->setstate(ios_base::failbit);
1473}
1474#  endif
1475
1476template <class _CharT, class _Traits>
1477inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1478    : basic_iostream<char_type, traits_type>(&__sb_) {
1479  if (__sb_.open(__s, __mode) == nullptr)
1480    this->setstate(ios_base::failbit);
1481}
1482
1483template <class _CharT, class _Traits>
1484inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1485    : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1486  this->set_rdbuf(&__sb_);
1487}
1488
1489template <class _CharT, class _Traits>
1490inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1491  basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1492  __sb_ = std::move(__rhs.__sb_);
1493  return *this;
1494}
1495
1496template <class _CharT, class _Traits>
1497inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1498  basic_iostream<char_type, traits_type>::swap(__rhs);
1499  __sb_.swap(__rhs.__sb_);
1500}
1501
1502template <class _CharT, class _Traits>
1503inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1504  __x.swap(__y);
1505}
1506
1507template <class _CharT, class _Traits>
1508inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1509  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1510}
1511
1512template <class _CharT, class _Traits>
1513inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1514  return __sb_.is_open();
1515}
1516
1517template <class _CharT, class _Traits>
1518void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1519  if (__sb_.open(__s, __mode))
1520    this->clear();
1521  else
1522    this->setstate(ios_base::failbit);
1523}
1524
1525#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1526template <class _CharT, class _Traits>
1527void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1528  if (__sb_.open(__s, __mode))
1529    this->clear();
1530  else
1531    this->setstate(ios_base::failbit);
1532}
1533#  endif
1534
1535template <class _CharT, class _Traits>
1536void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1537  if (__sb_.open(__s, __mode))
1538    this->clear();
1539  else
1540    this->setstate(ios_base::failbit);
1541}
1542
1543template <class _CharT, class _Traits>
1544inline void basic_fstream<_CharT, _Traits>::close() {
1545  if (__sb_.close() == nullptr)
1546    this->setstate(ios_base::failbit);
1547}
1548
1549#  if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1550extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1551extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1552extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1553#  endif
1554
1555_LIBCPP_END_NAMESPACE_STD
1556
1557#endif // _LIBCPP_HAS_NO_FILESYSTEM
1558
1559_LIBCPP_POP_MACROS
1560
1561#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1562#  include <atomic>
1563#  include <concepts>
1564#  include <cstdlib>
1565#  include <iosfwd>
1566#  include <limits>
1567#  include <mutex>
1568#  include <new>
1569#  include <stdexcept>
1570#  include <type_traits>
1571#endif
1572
1573#endif // _LIBCPP_FSTREAM
1574