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