• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
77    basic_ifstream();
78    explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
79    explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
80    explicit basic_ifstream(const filesystem::path& p,
81                            ios_base::openmode mode = ios_base::in); // C++17
82    basic_ifstream(basic_ifstream&& rhs);
83
84    basic_ifstream& operator=(basic_ifstream&& rhs);
85    void swap(basic_ifstream& rhs);
86
87    basic_filebuf<char_type, traits_type>* rdbuf() const;
88    bool is_open() const;
89    void open(const char* s, ios_base::openmode mode = ios_base::in);
90    void open(const string& s, ios_base::openmode mode = ios_base::in);
91    void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
92
93    void close();
94};
95
96template <class charT, class traits>
97  void
98  swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
99
100typedef basic_ifstream<char>    ifstream;
101typedef basic_ifstream<wchar_t> wifstream;
102
103template <class charT, class traits = char_traits<charT> >
104class basic_ofstream
105    : public basic_ostream<charT,traits>
106{
107public:
108    typedef charT                          char_type;
109    typedef traits                         traits_type;
110    typedef typename traits_type::int_type int_type;
111    typedef typename traits_type::pos_type pos_type;
112    typedef typename traits_type::off_type off_type;
113
114    basic_ofstream();
115    explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
116    explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
117    explicit basic_ofstream(const filesystem::path& p,
118                            ios_base::openmode mode = ios_base::out); // C++17
119    basic_ofstream(basic_ofstream&& rhs);
120
121    basic_ofstream& operator=(basic_ofstream&& rhs);
122    void swap(basic_ofstream& rhs);
123
124    basic_filebuf<char_type, traits_type>* rdbuf() const;
125    bool is_open() const;
126    void open(const char* s, ios_base::openmode mode = ios_base::out);
127    void open(const string& s, ios_base::openmode mode = ios_base::out);
128    void open(const filesystem::path& p,
129              ios_base::openmode mode = ios_base::out); // C++17
130
131    void close();
132};
133
134template <class charT, class traits>
135  void
136  swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
137
138typedef basic_ofstream<char>    ofstream;
139typedef basic_ofstream<wchar_t> wofstream;
140
141template <class charT, class traits=char_traits<charT> >
142class basic_fstream
143    : public basic_iostream<charT,traits>
144{
145public:
146    typedef charT                          char_type;
147    typedef traits                         traits_type;
148    typedef typename traits_type::int_type int_type;
149    typedef typename traits_type::pos_type pos_type;
150    typedef typename traits_type::off_type off_type;
151
152    basic_fstream();
153    explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154    explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
155    explicit basic_fstream(const filesystem::path& p,
156                           ios_base::openmode mode = ios_base::in|ios_base::out); C++17
157    basic_fstream(basic_fstream&& rhs);
158
159    basic_fstream& operator=(basic_fstream&& rhs);
160    void swap(basic_fstream& rhs);
161
162    basic_filebuf<char_type, traits_type>* rdbuf() const;
163    bool is_open() const;
164    void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
165    void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
166    void open(const filesystem::path& s,
167              ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
168
169    void close();
170};
171
172template <class charT, class traits>
173  void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
174
175typedef basic_fstream<char>    fstream;
176typedef basic_fstream<wchar_t> wfstream;
177
178}  // std
179
180*/
181
182#include <__algorithm/max.h>
183#include <__assert> // all public C++ headers provide the assertion handler
184#include <__availability>
185#include <__config>
186#include <__locale>
187#include <__utility/move.h>
188#include <__utility/swap.h>
189#include <__utility/unreachable.h>
190#include <cstdio>
191#include <istream>
192#include <ostream>
193#include <typeinfo>
194#include <version>
195
196#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
197#   include <filesystem>
198#endif
199
200#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
201#  pragma GCC system_header
202#endif
203
204_LIBCPP_PUSH_MACROS
205#include <__undef_macros>
206
207#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
208#  define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
209#endif
210
211#if !defined(_LIBCPP_HAS_NO_FSTREAM)
212
213_LIBCPP_BEGIN_NAMESPACE_STD
214
215template <class _CharT, class _Traits>
216class _LIBCPP_TEMPLATE_VIS basic_filebuf
217    : public basic_streambuf<_CharT, _Traits>
218{
219public:
220    typedef _CharT                           char_type;
221    typedef _Traits                          traits_type;
222    typedef typename traits_type::int_type   int_type;
223    typedef typename traits_type::pos_type   pos_type;
224    typedef typename traits_type::off_type   off_type;
225    typedef typename traits_type::state_type state_type;
226
227    // 27.9.1.2 Constructors/destructor:
228    basic_filebuf();
229    basic_filebuf(basic_filebuf&& __rhs);
230    ~basic_filebuf() override;
231
232    // 27.9.1.3 Assign/swap:
233    _LIBCPP_INLINE_VISIBILITY
234    basic_filebuf& operator=(basic_filebuf&& __rhs);
235    void swap(basic_filebuf& __rhs);
236
237    // 27.9.1.4 Members:
238    _LIBCPP_INLINE_VISIBILITY
239    bool is_open() const;
240    basic_filebuf* open(const char* __s, ios_base::openmode __mode);
241#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
242    basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
243#endif
244    _LIBCPP_INLINE_VISIBILITY
245    basic_filebuf* open(const string& __s, ios_base::openmode __mode);
246
247#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
248    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
249    basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
250      return open(__p.c_str(), __mode);
251    }
252#endif
253    _LIBCPP_INLINE_VISIBILITY
254    basic_filebuf* __open(int __fd, ios_base::openmode __mode);
255    basic_filebuf* close();
256
257    _LIBCPP_INLINE_VISIBILITY
258    inline static const char*
259    __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
260
261  protected:
262    // 27.9.1.5 Overridden virtual functions:
263    int_type underflow() override;
264    int_type pbackfail(int_type __c = traits_type::eof()) override;
265    int_type overflow (int_type __c = traits_type::eof()) override;
266    basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
267    pos_type seekoff(off_type __off, ios_base::seekdir __way,
268                     ios_base::openmode __wch = ios_base::in | ios_base::out) override;
269    pos_type seekpos(pos_type __sp,
270                     ios_base::openmode __wch = ios_base::in | ios_base::out) override;
271    int sync() override;
272    void imbue(const locale& __loc) override;
273
274private:
275  char* __extbuf_;
276  const char* __extbufnext_;
277  const char* __extbufend_;
278  char __extbuf_min_[8];
279  size_t __ebs_;
280  char_type* __intbuf_;
281  size_t __ibs_;
282  FILE* __file_;
283  const codecvt<char_type, char, state_type>* __cv_;
284  state_type __st_;
285  state_type __st_last_;
286  ios_base::openmode __om_;
287  ios_base::openmode __cm_;
288  bool __owns_eb_;
289  bool __owns_ib_;
290  bool __always_noconv_;
291
292  bool __read_mode();
293  void __write_mode();
294};
295
296template <class _CharT, class _Traits>
297basic_filebuf<_CharT, _Traits>::basic_filebuf()
298    : __extbuf_(nullptr),
299      __extbufnext_(nullptr),
300      __extbufend_(nullptr),
301      __ebs_(0),
302      __intbuf_(nullptr),
303      __ibs_(0),
304      __file_(nullptr),
305      __cv_(nullptr),
306      __st_(),
307      __st_last_(),
308      __om_(0),
309      __cm_(0),
310      __owns_eb_(false),
311      __owns_ib_(false),
312      __always_noconv_(false)
313{
314    if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
315    {
316        __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
317        __always_noconv_ = __cv_->always_noconv();
318    }
319    setbuf(nullptr, 4096);
320}
321
322template <class _CharT, class _Traits>
323basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
324    : basic_streambuf<_CharT, _Traits>(__rhs)
325{
326    if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
327    {
328        __extbuf_ = __extbuf_min_;
329        __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
330        __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
331    }
332    else
333    {
334        __extbuf_ = __rhs.__extbuf_;
335        __extbufnext_ = __rhs.__extbufnext_;
336        __extbufend_ = __rhs.__extbufend_;
337    }
338    __ebs_ = __rhs.__ebs_;
339    __intbuf_ = __rhs.__intbuf_;
340    __ibs_ = __rhs.__ibs_;
341    __file_ = __rhs.__file_;
342    __cv_ = __rhs.__cv_;
343    __st_ = __rhs.__st_;
344    __st_last_ = __rhs.__st_last_;
345    __om_ = __rhs.__om_;
346    __cm_ = __rhs.__cm_;
347    __owns_eb_ = __rhs.__owns_eb_;
348    __owns_ib_ = __rhs.__owns_ib_;
349    __always_noconv_ = __rhs.__always_noconv_;
350    if (__rhs.pbase())
351    {
352        if (__rhs.pbase() == __rhs.__intbuf_)
353            this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
354        else
355            this->setp((char_type*)__extbuf_,
356                       (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
357        this->__pbump(__rhs. pptr() - __rhs.pbase());
358    }
359    else if (__rhs.eback())
360    {
361        if (__rhs.eback() == __rhs.__intbuf_)
362            this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
363                                  __intbuf_ + (__rhs.egptr() - __rhs.eback()));
364        else
365            this->setg((char_type*)__extbuf_,
366                       (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
367                       (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
368    }
369    __rhs.__extbuf_ = nullptr;
370    __rhs.__extbufnext_ = nullptr;
371    __rhs.__extbufend_ = nullptr;
372    __rhs.__ebs_ = 0;
373    __rhs.__intbuf_ = 0;
374    __rhs.__ibs_ = 0;
375    __rhs.__file_ = nullptr;
376    __rhs.__st_ = state_type();
377    __rhs.__st_last_ = state_type();
378    __rhs.__om_ = 0;
379    __rhs.__cm_ = 0;
380    __rhs.__owns_eb_ = false;
381    __rhs.__owns_ib_ = false;
382    __rhs.setg(0, 0, 0);
383    __rhs.setp(0, 0);
384}
385
386template <class _CharT, class _Traits>
387inline
388basic_filebuf<_CharT, _Traits>&
389basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
390{
391    close();
392    swap(__rhs);
393    return *this;
394}
395
396template <class _CharT, class _Traits>
397basic_filebuf<_CharT, _Traits>::~basic_filebuf()
398{
399#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
400    try
401    {
402#endif // _LIBCPP_HAS_NO_EXCEPTIONS
403        close();
404#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
405    }
406    catch (...)
407    {
408    }
409#endif // _LIBCPP_HAS_NO_EXCEPTIONS
410    if (__owns_eb_)
411        delete [] __extbuf_;
412    if (__owns_ib_)
413        delete [] __intbuf_;
414}
415
416template <class _CharT, class _Traits>
417void
418basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
419{
420    basic_streambuf<char_type, traits_type>::swap(__rhs);
421    if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
422    {
423        // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
424        std::swap(__extbuf_, __rhs.__extbuf_);
425        std::swap(__extbufnext_, __rhs.__extbufnext_);
426        std::swap(__extbufend_, __rhs.__extbufend_);
427    }
428    else
429    {
430        ptrdiff_t __ln = __extbufnext_       ? __extbufnext_ - __extbuf_             : 0;
431        ptrdiff_t __le = __extbufend_        ? __extbufend_ - __extbuf_              : 0;
432        ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
433        ptrdiff_t __re = __rhs.__extbufend_  ? __rhs.__extbufend_ - __rhs.__extbuf_  : 0;
434        if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
435        {
436            // *this uses the small buffer, but __rhs doesn't.
437            __extbuf_ = __rhs.__extbuf_;
438            __rhs.__extbuf_ = __rhs.__extbuf_min_;
439            std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
440        }
441        else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
442        {
443            // *this doesn't use the small buffer, but __rhs does.
444            __rhs.__extbuf_ = __extbuf_;
445            __extbuf_ = __extbuf_min_;
446            std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
447        }
448        else
449        {
450            // Both *this and __rhs use the small buffer.
451            char __tmp[sizeof(__extbuf_min_)];
452            std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
453            std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
454            std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
455        }
456        __extbufnext_ = __extbuf_ + __rn;
457        __extbufend_ = __extbuf_ + __re;
458        __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
459        __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
460    }
461    _VSTD::swap(__ebs_, __rhs.__ebs_);
462    _VSTD::swap(__intbuf_, __rhs.__intbuf_);
463    _VSTD::swap(__ibs_, __rhs.__ibs_);
464    _VSTD::swap(__file_, __rhs.__file_);
465    _VSTD::swap(__cv_, __rhs.__cv_);
466    _VSTD::swap(__st_, __rhs.__st_);
467    _VSTD::swap(__st_last_, __rhs.__st_last_);
468    _VSTD::swap(__om_, __rhs.__om_);
469    _VSTD::swap(__cm_, __rhs.__cm_);
470    _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
471    _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
472    _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
473    if (this->eback() == (char_type*)__rhs.__extbuf_min_)
474    {
475        ptrdiff_t __n = this->gptr() - this->eback();
476        ptrdiff_t __e = this->egptr() - this->eback();
477        this->setg((char_type*)__extbuf_min_,
478                   (char_type*)__extbuf_min_ + __n,
479                   (char_type*)__extbuf_min_ + __e);
480    }
481    else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
482    {
483        ptrdiff_t __n = this->pptr() - this->pbase();
484        ptrdiff_t __e = this->epptr() - this->pbase();
485        this->setp((char_type*)__extbuf_min_,
486                   (char_type*)__extbuf_min_ + __e);
487        this->__pbump(__n);
488    }
489    if (__rhs.eback() == (char_type*)__extbuf_min_)
490    {
491        ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
492        ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
493        __rhs.setg((char_type*)__rhs.__extbuf_min_,
494                   (char_type*)__rhs.__extbuf_min_ + __n,
495                   (char_type*)__rhs.__extbuf_min_ + __e);
496    }
497    else if (__rhs.pbase() == (char_type*)__extbuf_min_)
498    {
499        ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
500        ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
501        __rhs.setp((char_type*)__rhs.__extbuf_min_,
502                   (char_type*)__rhs.__extbuf_min_ + __e);
503        __rhs.__pbump(__n);
504    }
505}
506
507template <class _CharT, class _Traits>
508inline _LIBCPP_INLINE_VISIBILITY
509void
510swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
511{
512    __x.swap(__y);
513}
514
515template <class _CharT, class _Traits>
516inline
517bool
518basic_filebuf<_CharT, _Traits>::is_open() const
519{
520    return __file_ != nullptr;
521}
522
523template <class _CharT, class _Traits>
524const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
525    ios_base::openmode __mode) _NOEXCEPT {
526  switch (__mode & ~ios_base::ate) {
527  case ios_base::out:
528  case ios_base::out | ios_base::trunc:
529    return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
530  case ios_base::out | ios_base::app:
531  case ios_base::app:
532    return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
533  case ios_base::in:
534    return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
535  case ios_base::in | ios_base::out:
536    return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
537  case ios_base::in | ios_base::out | ios_base::trunc:
538    return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
539  case ios_base::in | ios_base::out | ios_base::app:
540  case ios_base::in | ios_base::app:
541    return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
542  case ios_base::out | ios_base::binary:
543  case ios_base::out | ios_base::trunc | ios_base::binary:
544    return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
545  case ios_base::out | ios_base::app | ios_base::binary:
546  case ios_base::app | ios_base::binary:
547    return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
548  case ios_base::in | ios_base::binary:
549    return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
550  case ios_base::in | ios_base::out | ios_base::binary:
551    return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
552  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
553    return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
554  case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
555  case ios_base::in | ios_base::app | ios_base::binary:
556    return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
557  default:
558    return nullptr;
559  }
560  __libcpp_unreachable();
561}
562
563template <class _CharT, class _Traits>
564basic_filebuf<_CharT, _Traits>*
565basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
566{
567    basic_filebuf<_CharT, _Traits>* __rt = nullptr;
568    if (__file_ == nullptr)
569    {
570      if (const char* __mdstr = __make_mdstring(__mode)) {
571        __rt = this;
572        __file_ = fopen(__s, __mdstr);
573        if (__file_) {
574          __om_ = __mode;
575          if (__mode & ios_base::ate) {
576            if (fseek(__file_, 0, SEEK_END)) {
577              fclose(__file_);
578              __file_ = nullptr;
579              __rt = nullptr;
580            }
581          }
582        } else
583          __rt = nullptr;
584      }
585    }
586    return __rt;
587}
588
589template <class _CharT, class _Traits>
590inline
591basic_filebuf<_CharT, _Traits>*
592basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
593  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
594  if (__file_ == nullptr) {
595    if (const char* __mdstr = __make_mdstring(__mode)) {
596      __rt = this;
597      __file_ = fdopen(__fd, __mdstr);
598      if (__file_) {
599        __om_ = __mode;
600        if (__mode & ios_base::ate) {
601          if (fseek(__file_, 0, SEEK_END)) {
602            fclose(__file_);
603            __file_ = nullptr;
604            __rt = nullptr;
605          }
606        }
607      } else
608        __rt = nullptr;
609    }
610  }
611  return __rt;
612}
613
614#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
615// This is basically the same as the char* overload except that it uses _wfopen
616// and long mode strings.
617template <class _CharT, class _Traits>
618basic_filebuf<_CharT, _Traits>*
619basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
620{
621    basic_filebuf<_CharT, _Traits>* __rt = nullptr;
622    if (__file_ == nullptr)
623    {
624        __rt = this;
625        const wchar_t* __mdstr;
626        switch (__mode & ~ios_base::ate)
627        {
628        case ios_base::out:
629        case ios_base::out | ios_base::trunc:
630            __mdstr = L"w";
631            break;
632        case ios_base::out | ios_base::app:
633        case ios_base::app:
634            __mdstr = L"a";
635            break;
636        case ios_base::in:
637            __mdstr = L"r";
638            break;
639        case ios_base::in | ios_base::out:
640            __mdstr = L"r+";
641            break;
642        case ios_base::in | ios_base::out | ios_base::trunc:
643            __mdstr = L"w+";
644            break;
645        case ios_base::in | ios_base::out | ios_base::app:
646        case ios_base::in | ios_base::app:
647            __mdstr = L"a+";
648            break;
649        case ios_base::out | ios_base::binary:
650        case ios_base::out | ios_base::trunc | ios_base::binary:
651            __mdstr = L"wb";
652            break;
653        case ios_base::out | ios_base::app | ios_base::binary:
654        case ios_base::app | ios_base::binary:
655            __mdstr = L"ab";
656            break;
657        case ios_base::in | ios_base::binary:
658            __mdstr = L"rb";
659            break;
660        case ios_base::in | ios_base::out | ios_base::binary:
661            __mdstr = L"r+b";
662            break;
663        case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
664            __mdstr = L"w+b";
665            break;
666        case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
667        case ios_base::in | ios_base::app | ios_base::binary:
668            __mdstr = L"a+b";
669            break;
670        default:
671            __rt = nullptr;
672            break;
673        }
674        if (__rt)
675        {
676            __file_ = _wfopen(__s, __mdstr);
677            if (__file_)
678            {
679                __om_ = __mode;
680                if (__mode & ios_base::ate)
681                {
682                    if (fseek(__file_, 0, SEEK_END))
683                    {
684                        fclose(__file_);
685                        __file_ = nullptr;
686                        __rt = nullptr;
687                    }
688                }
689            }
690            else
691                __rt = nullptr;
692        }
693    }
694    return __rt;
695}
696#endif
697
698template <class _CharT, class _Traits>
699inline
700basic_filebuf<_CharT, _Traits>*
701basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
702{
703    return open(__s.c_str(), __mode);
704}
705
706template <class _CharT, class _Traits>
707basic_filebuf<_CharT, _Traits>*
708basic_filebuf<_CharT, _Traits>::close()
709{
710    basic_filebuf<_CharT, _Traits>* __rt = nullptr;
711    if (__file_)
712    {
713        __rt = this;
714        unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
715        if (sync())
716            __rt = nullptr;
717        if (fclose(__h.release()))
718            __rt = nullptr;
719        __file_ = nullptr;
720        setbuf(0, 0);
721    }
722    return __rt;
723}
724
725template <class _CharT, class _Traits>
726typename basic_filebuf<_CharT, _Traits>::int_type
727basic_filebuf<_CharT, _Traits>::underflow()
728{
729    if (__file_ == nullptr)
730        return traits_type::eof();
731    bool __initial = __read_mode();
732    char_type __1buf;
733    if (this->gptr() == nullptr)
734        this->setg(&__1buf, &__1buf+1, &__1buf+1);
735    const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
736    int_type __c = traits_type::eof();
737    if (this->gptr() == this->egptr())
738    {
739        _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
740        if (__always_noconv_)
741        {
742            size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
743            __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
744            if (__nmemb != 0)
745            {
746                this->setg(this->eback(),
747                           this->eback() + __unget_sz,
748                           this->eback() + __unget_sz + __nmemb);
749                __c = traits_type::to_int_type(*this->gptr());
750            }
751        }
752        else
753        {
754            if (__extbufend_ != __extbufnext_) {
755                _LIBCPP_ASSERT(__extbufnext_ != nullptr, "underflow moving from nullptr");
756                _LIBCPP_ASSERT(__extbuf_ != nullptr, "underflow moving into nullptr");
757                _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
758            }
759            __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
760            __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
761            size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
762                                 static_cast<size_t>(__extbufend_ - __extbufnext_));
763            codecvt_base::result __r;
764            __st_last_ = __st_;
765            size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
766            if (__nr != 0)
767            {
768                if (!__cv_)
769                    __throw_bad_cast();
770
771                __extbufend_ = __extbufnext_ + __nr;
772                char_type*  __inext;
773                __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
774                                       this->eback() + __unget_sz,
775                                       this->eback() + __ibs_, __inext);
776                if (__r == codecvt_base::noconv)
777                {
778                    this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
779                                          (char_type*)const_cast<char *>(__extbufend_));
780                    __c = traits_type::to_int_type(*this->gptr());
781                }
782                else if (__inext != this->eback() + __unget_sz)
783                {
784                    this->setg(this->eback(), this->eback() + __unget_sz, __inext);
785                    __c = traits_type::to_int_type(*this->gptr());
786                }
787            }
788        }
789    }
790    else
791        __c = traits_type::to_int_type(*this->gptr());
792    if (this->eback() == &__1buf)
793        this->setg(nullptr, nullptr, nullptr);
794    return __c;
795}
796
797template <class _CharT, class _Traits>
798typename basic_filebuf<_CharT, _Traits>::int_type
799basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
800{
801    if (__file_ && this->eback() < this->gptr())
802    {
803        if (traits_type::eq_int_type(__c, traits_type::eof()))
804        {
805            this->gbump(-1);
806            return traits_type::not_eof(__c);
807        }
808        if ((__om_ & ios_base::out) ||
809            traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
810        {
811            this->gbump(-1);
812            *this->gptr() = traits_type::to_char_type(__c);
813            return __c;
814        }
815    }
816    return traits_type::eof();
817}
818
819template <class _CharT, class _Traits>
820typename basic_filebuf<_CharT, _Traits>::int_type
821basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
822{
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    {
831        if (this->pptr() == nullptr)
832            this->setp(&__1buf, &__1buf+1);
833        *this->pptr() = traits_type::to_char_type(__c);
834        this->pbump(1);
835    }
836    if (this->pptr() != this->pbase())
837    {
838        if (__always_noconv_)
839        {
840            size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
841            if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
842                return traits_type::eof();
843        }
844        else
845        {
846            char* __extbe = __extbuf_;
847            codecvt_base::result __r;
848            do
849            {
850                if (!__cv_)
851                    __throw_bad_cast();
852
853                const char_type* __e;
854                __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
855                                        __extbuf_, __extbuf_ + __ebs_, __extbe);
856                if (__e == this->pbase())
857                    return traits_type::eof();
858                if (__r == codecvt_base::noconv)
859                {
860                    size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
861                    if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
862                        return traits_type::eof();
863                }
864                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
865                {
866                    size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
867                    if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
868                        return traits_type::eof();
869                    if (__r == codecvt_base::partial)
870                    {
871                        this->setp(const_cast<char_type*>(__e), this->pptr());
872                        this->__pbump(this->epptr() - this->pbase());
873                    }
874                }
875                else
876                    return traits_type::eof();
877            } while (__r == codecvt_base::partial);
878        }
879        this->setp(__pb_save, __epb_save);
880    }
881    return traits_type::not_eof(__c);
882}
883
884template <class _CharT, class _Traits>
885basic_streambuf<_CharT, _Traits>*
886basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
887{
888    this->setg(nullptr, nullptr, nullptr);
889    this->setp(nullptr, nullptr);
890    if (__owns_eb_)
891        delete [] __extbuf_;
892    if (__owns_ib_)
893        delete [] __intbuf_;
894    __ebs_ = __n;
895    if (__ebs_ > sizeof(__extbuf_min_))
896    {
897        if (__always_noconv_ && __s)
898        {
899            __extbuf_ = (char*)__s;
900            __owns_eb_ = false;
901        }
902        else
903        {
904            __extbuf_ = new char[__ebs_];
905            __owns_eb_ = true;
906        }
907    }
908    else
909    {
910        __extbuf_ = __extbuf_min_;
911        __ebs_ = sizeof(__extbuf_min_);
912        __owns_eb_ = false;
913    }
914    if (!__always_noconv_)
915    {
916        __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
917        if (__s && __ibs_ >= sizeof(__extbuf_min_))
918        {
919            __intbuf_ = __s;
920            __owns_ib_ = false;
921        }
922        else
923        {
924            __intbuf_ = new char_type[__ibs_];
925            __owns_ib_ = true;
926        }
927    }
928    else
929    {
930        __ibs_ = 0;
931        __intbuf_ = nullptr;
932        __owns_ib_ = false;
933    }
934    return this;
935}
936
937template <class _CharT, class _Traits>
938typename basic_filebuf<_CharT, _Traits>::pos_type
939basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
940                                        ios_base::openmode)
941{
942    if (!__cv_)
943        __throw_bad_cast();
944
945    int __width = __cv_->encoding();
946    if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
947        return pos_type(off_type(-1));
948    // __width > 0 || __off == 0
949    int __whence;
950    switch (__way)
951    {
952    case ios_base::beg:
953        __whence = SEEK_SET;
954        break;
955    case ios_base::cur:
956        __whence = SEEK_CUR;
957        break;
958    case ios_base::end:
959        __whence = SEEK_END;
960        break;
961    default:
962        return pos_type(off_type(-1));
963    }
964#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
965    if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
966        return pos_type(off_type(-1));
967    pos_type __r = ftell(__file_);
968#else
969    if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
970        return pos_type(off_type(-1));
971    pos_type __r = ftello(__file_);
972#endif
973    __r.state(__st_);
974    return __r;
975}
976
977template <class _CharT, class _Traits>
978typename basic_filebuf<_CharT, _Traits>::pos_type
979basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
980{
981    if (__file_ == nullptr || sync())
982        return pos_type(off_type(-1));
983#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
984    if (fseek(__file_, __sp, SEEK_SET))
985        return pos_type(off_type(-1));
986#else
987    if (::fseeko(__file_, __sp, SEEK_SET))
988        return pos_type(off_type(-1));
989#endif
990    __st_ = __sp.state();
991    return __sp;
992}
993
994template <class _CharT, class _Traits>
995int
996basic_filebuf<_CharT, _Traits>::sync()
997{
998    if (__file_ == nullptr)
999        return 0;
1000    if (!__cv_)
1001        __throw_bad_cast();
1002
1003    if (__cm_ & ios_base::out)
1004    {
1005        if (this->pptr() != this->pbase())
1006            if (overflow() == traits_type::eof())
1007                return -1;
1008        codecvt_base::result __r;
1009        do
1010        {
1011            char* __extbe;
1012            __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1013            size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1014            if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1015                return -1;
1016        } while (__r == codecvt_base::partial);
1017        if (__r == codecvt_base::error)
1018            return -1;
1019        if (fflush(__file_))
1020            return -1;
1021    }
1022    else if (__cm_ & ios_base::in)
1023    {
1024        off_type __c;
1025        state_type __state = __st_last_;
1026        bool __update_st = false;
1027        if (__always_noconv_)
1028            __c = this->egptr() - this->gptr();
1029        else
1030        {
1031            int __width = __cv_->encoding();
1032            __c = __extbufend_ - __extbufnext_;
1033            if (__width > 0)
1034                __c += __width * (this->egptr() - this->gptr());
1035            else
1036            {
1037                if (this->gptr() != this->egptr())
1038                {
1039                    const int __off =  __cv_->length(__state, __extbuf_,
1040                                                     __extbufnext_,
1041                                                     this->gptr() - this->eback());
1042                    __c += __extbufnext_ - __extbuf_ - __off;
1043                    __update_st = true;
1044                }
1045            }
1046        }
1047#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1048        if (fseek(__file_, -__c, SEEK_CUR))
1049            return -1;
1050#else
1051        if (::fseeko(__file_, -__c, SEEK_CUR))
1052            return -1;
1053#endif
1054        if (__update_st)
1055            __st_ = __state;
1056        __extbufnext_ = __extbufend_ = __extbuf_;
1057        this->setg(nullptr, nullptr, nullptr);
1058        __cm_ = 0;
1059    }
1060    return 0;
1061}
1062
1063template <class _CharT, class _Traits>
1064void
1065basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1066{
1067    sync();
1068    __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1069    bool __old_anc = __always_noconv_;
1070    __always_noconv_ = __cv_->always_noconv();
1071    if (__old_anc != __always_noconv_)
1072    {
1073        this->setg(nullptr, nullptr, nullptr);
1074        this->setp(nullptr, nullptr);
1075        // invariant, char_type is char, else we couldn't get here
1076        if (__always_noconv_)  // need to dump __intbuf_
1077        {
1078            if (__owns_eb_)
1079                delete [] __extbuf_;
1080            __owns_eb_ = __owns_ib_;
1081            __ebs_ = __ibs_;
1082            __extbuf_ = (char*)__intbuf_;
1083            __ibs_ = 0;
1084            __intbuf_ = nullptr;
1085            __owns_ib_ = false;
1086        }
1087        else  // need to obtain an __intbuf_.
1088        {     // If __extbuf_ is user-supplied, use it, else new __intbuf_
1089            if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1090            {
1091                __ibs_ = __ebs_;
1092                __intbuf_ = (char_type*)__extbuf_;
1093                __owns_ib_ = false;
1094                __extbuf_ = new char[__ebs_];
1095                __owns_eb_ = true;
1096            }
1097            else
1098            {
1099                __ibs_ = __ebs_;
1100                __intbuf_ = new char_type[__ibs_];
1101                __owns_ib_ = true;
1102            }
1103        }
1104    }
1105}
1106
1107template <class _CharT, class _Traits>
1108bool
1109basic_filebuf<_CharT, _Traits>::__read_mode()
1110{
1111    if (!(__cm_ & ios_base::in))
1112    {
1113        this->setp(nullptr, nullptr);
1114        if (__always_noconv_)
1115            this->setg((char_type*)__extbuf_,
1116                       (char_type*)__extbuf_ + __ebs_,
1117                       (char_type*)__extbuf_ + __ebs_);
1118        else
1119            this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1120        __cm_ = ios_base::in;
1121        return true;
1122    }
1123    return false;
1124}
1125
1126template <class _CharT, class _Traits>
1127void
1128basic_filebuf<_CharT, _Traits>::__write_mode()
1129{
1130    if (!(__cm_ & ios_base::out))
1131    {
1132        this->setg(nullptr, nullptr, nullptr);
1133        if (__ebs_ > sizeof(__extbuf_min_))
1134        {
1135            if (__always_noconv_)
1136                this->setp((char_type*)__extbuf_,
1137                           (char_type*)__extbuf_ + (__ebs_ - 1));
1138            else
1139                this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1140        }
1141        else
1142            this->setp(nullptr, nullptr);
1143        __cm_ = ios_base::out;
1144    }
1145}
1146
1147// basic_ifstream
1148
1149template <class _CharT, class _Traits>
1150class _LIBCPP_TEMPLATE_VIS basic_ifstream
1151    : public basic_istream<_CharT, _Traits>
1152{
1153public:
1154    typedef _CharT                         char_type;
1155    typedef _Traits                        traits_type;
1156    typedef typename traits_type::int_type int_type;
1157    typedef typename traits_type::pos_type pos_type;
1158    typedef typename traits_type::off_type off_type;
1159
1160    _LIBCPP_INLINE_VISIBILITY
1161    basic_ifstream();
1162    _LIBCPP_INLINE_VISIBILITY
1163    explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1164#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1165    _LIBCPP_INLINE_VISIBILITY
1166    explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1167#endif
1168    _LIBCPP_INLINE_VISIBILITY
1169    explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1170#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1171    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1172    explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1173      : basic_ifstream(__p.c_str(), __mode) {}
1174#endif // _LIBCPP_STD_VER >= 17
1175    _LIBCPP_INLINE_VISIBILITY
1176    basic_ifstream(basic_ifstream&& __rhs);
1177    _LIBCPP_INLINE_VISIBILITY
1178    basic_ifstream& operator=(basic_ifstream&& __rhs);
1179    _LIBCPP_INLINE_VISIBILITY
1180    void swap(basic_ifstream& __rhs);
1181
1182    _LIBCPP_INLINE_VISIBILITY
1183    basic_filebuf<char_type, traits_type>* rdbuf() const;
1184    _LIBCPP_INLINE_VISIBILITY
1185    bool is_open() const;
1186    void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1187#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1188    void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1189#endif
1190    void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1191#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1192    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1193    void open(const filesystem::path& __p,
1194              ios_base::openmode __mode = ios_base::in) {
1195      return open(__p.c_str(), __mode);
1196    }
1197#endif // _LIBCPP_STD_VER >= 17
1198
1199    _LIBCPP_INLINE_VISIBILITY
1200    void __open(int __fd, ios_base::openmode __mode);
1201    _LIBCPP_INLINE_VISIBILITY
1202    void close();
1203
1204private:
1205    basic_filebuf<char_type, traits_type> __sb_;
1206};
1207
1208template <class _CharT, class _Traits>
1209inline
1210basic_ifstream<_CharT, _Traits>::basic_ifstream()
1211    : basic_istream<char_type, traits_type>(&__sb_)
1212{
1213}
1214
1215template <class _CharT, class _Traits>
1216inline
1217basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1218    : basic_istream<char_type, traits_type>(&__sb_)
1219{
1220    if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1221        this->setstate(ios_base::failbit);
1222}
1223
1224#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1225template <class _CharT, class _Traits>
1226inline
1227basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1228    : basic_istream<char_type, traits_type>(&__sb_)
1229{
1230    if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1231        this->setstate(ios_base::failbit);
1232}
1233#endif
1234
1235template <class _CharT, class _Traits>
1236inline
1237basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1238    : basic_istream<char_type, traits_type>(&__sb_)
1239{
1240    if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1241        this->setstate(ios_base::failbit);
1242}
1243
1244template <class _CharT, class _Traits>
1245inline
1246basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1247    : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1248      __sb_(_VSTD::move(__rhs.__sb_))
1249{
1250    this->set_rdbuf(&__sb_);
1251}
1252
1253template <class _CharT, class _Traits>
1254inline
1255basic_ifstream<_CharT, _Traits>&
1256basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1257{
1258    basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1259    __sb_ = _VSTD::move(__rhs.__sb_);
1260    return *this;
1261}
1262
1263template <class _CharT, class _Traits>
1264inline
1265void
1266basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1267{
1268    basic_istream<char_type, traits_type>::swap(__rhs);
1269    __sb_.swap(__rhs.__sb_);
1270}
1271
1272template <class _CharT, class _Traits>
1273inline _LIBCPP_INLINE_VISIBILITY
1274void
1275swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1276{
1277    __x.swap(__y);
1278}
1279
1280template <class _CharT, class _Traits>
1281inline
1282basic_filebuf<_CharT, _Traits>*
1283basic_ifstream<_CharT, _Traits>::rdbuf() const
1284{
1285    return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1286}
1287
1288template <class _CharT, class _Traits>
1289inline
1290bool
1291basic_ifstream<_CharT, _Traits>::is_open() const
1292{
1293    return __sb_.is_open();
1294}
1295
1296template <class _CharT, class _Traits>
1297void
1298basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1299{
1300    if (__sb_.open(__s, __mode | ios_base::in))
1301        this->clear();
1302    else
1303        this->setstate(ios_base::failbit);
1304}
1305
1306#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1307template <class _CharT, class _Traits>
1308void
1309basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1310{
1311    if (__sb_.open(__s, __mode | ios_base::in))
1312        this->clear();
1313    else
1314        this->setstate(ios_base::failbit);
1315}
1316#endif
1317
1318template <class _CharT, class _Traits>
1319void
1320basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1321{
1322    if (__sb_.open(__s, __mode | ios_base::in))
1323        this->clear();
1324    else
1325        this->setstate(ios_base::failbit);
1326}
1327
1328template <class _CharT, class _Traits>
1329inline
1330void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1331                                             ios_base::openmode __mode) {
1332  if (__sb_.__open(__fd, __mode | ios_base::in))
1333    this->clear();
1334  else
1335    this->setstate(ios_base::failbit);
1336}
1337
1338template <class _CharT, class _Traits>
1339inline
1340void
1341basic_ifstream<_CharT, _Traits>::close()
1342{
1343    if (__sb_.close() == 0)
1344        this->setstate(ios_base::failbit);
1345}
1346
1347// basic_ofstream
1348
1349template <class _CharT, class _Traits>
1350class _LIBCPP_TEMPLATE_VIS basic_ofstream
1351    : public basic_ostream<_CharT, _Traits>
1352{
1353public:
1354    typedef _CharT                         char_type;
1355    typedef _Traits                        traits_type;
1356    typedef typename traits_type::int_type int_type;
1357    typedef typename traits_type::pos_type pos_type;
1358    typedef typename traits_type::off_type off_type;
1359
1360    _LIBCPP_INLINE_VISIBILITY
1361    basic_ofstream();
1362    _LIBCPP_INLINE_VISIBILITY
1363    explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1364#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1365    _LIBCPP_INLINE_VISIBILITY
1366    explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1367#endif
1368    _LIBCPP_INLINE_VISIBILITY
1369    explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1370
1371#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1372    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1373    explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1374      : basic_ofstream(__p.c_str(), __mode) {}
1375#endif // _LIBCPP_STD_VER >= 17
1376
1377    _LIBCPP_INLINE_VISIBILITY
1378    basic_ofstream(basic_ofstream&& __rhs);
1379    _LIBCPP_INLINE_VISIBILITY
1380    basic_ofstream& operator=(basic_ofstream&& __rhs);
1381    _LIBCPP_INLINE_VISIBILITY
1382    void swap(basic_ofstream& __rhs);
1383
1384    _LIBCPP_INLINE_VISIBILITY
1385    basic_filebuf<char_type, traits_type>* rdbuf() const;
1386    _LIBCPP_INLINE_VISIBILITY
1387    bool is_open() const;
1388    void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1389#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1390    void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1391#endif
1392    void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1393
1394#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1395    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1396    void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1397    { return open(__p.c_str(), __mode); }
1398#endif // _LIBCPP_STD_VER >= 17
1399
1400    _LIBCPP_INLINE_VISIBILITY
1401    void __open(int __fd, ios_base::openmode __mode);
1402    _LIBCPP_INLINE_VISIBILITY
1403    void close();
1404
1405private:
1406    basic_filebuf<char_type, traits_type> __sb_;
1407};
1408
1409template <class _CharT, class _Traits>
1410inline
1411basic_ofstream<_CharT, _Traits>::basic_ofstream()
1412    : basic_ostream<char_type, traits_type>(&__sb_)
1413{
1414}
1415
1416template <class _CharT, class _Traits>
1417inline
1418basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1419    : basic_ostream<char_type, traits_type>(&__sb_)
1420{
1421    if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1422        this->setstate(ios_base::failbit);
1423}
1424
1425#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1426template <class _CharT, class _Traits>
1427inline
1428basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1429    : basic_ostream<char_type, traits_type>(&__sb_)
1430{
1431    if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1432        this->setstate(ios_base::failbit);
1433}
1434#endif
1435
1436template <class _CharT, class _Traits>
1437inline
1438basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1439    : basic_ostream<char_type, traits_type>(&__sb_)
1440{
1441    if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1442        this->setstate(ios_base::failbit);
1443}
1444
1445template <class _CharT, class _Traits>
1446inline
1447basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1448    : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1449      __sb_(_VSTD::move(__rhs.__sb_))
1450{
1451    this->set_rdbuf(&__sb_);
1452}
1453
1454template <class _CharT, class _Traits>
1455inline
1456basic_ofstream<_CharT, _Traits>&
1457basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1458{
1459    basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1460    __sb_ = _VSTD::move(__rhs.__sb_);
1461    return *this;
1462}
1463
1464template <class _CharT, class _Traits>
1465inline
1466void
1467basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1468{
1469    basic_ostream<char_type, traits_type>::swap(__rhs);
1470    __sb_.swap(__rhs.__sb_);
1471}
1472
1473template <class _CharT, class _Traits>
1474inline _LIBCPP_INLINE_VISIBILITY
1475void
1476swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1477{
1478    __x.swap(__y);
1479}
1480
1481template <class _CharT, class _Traits>
1482inline
1483basic_filebuf<_CharT, _Traits>*
1484basic_ofstream<_CharT, _Traits>::rdbuf() const
1485{
1486    return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1487}
1488
1489template <class _CharT, class _Traits>
1490inline
1491bool
1492basic_ofstream<_CharT, _Traits>::is_open() const
1493{
1494    return __sb_.is_open();
1495}
1496
1497template <class _CharT, class _Traits>
1498void
1499basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1500{
1501    if (__sb_.open(__s, __mode | ios_base::out))
1502        this->clear();
1503    else
1504        this->setstate(ios_base::failbit);
1505}
1506
1507#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1508template <class _CharT, class _Traits>
1509void
1510basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1511{
1512    if (__sb_.open(__s, __mode | ios_base::out))
1513        this->clear();
1514    else
1515        this->setstate(ios_base::failbit);
1516}
1517#endif
1518
1519template <class _CharT, class _Traits>
1520void
1521basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1522{
1523    if (__sb_.open(__s, __mode | ios_base::out))
1524        this->clear();
1525    else
1526        this->setstate(ios_base::failbit);
1527}
1528
1529template <class _CharT, class _Traits>
1530inline
1531void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1532                                             ios_base::openmode __mode) {
1533  if (__sb_.__open(__fd, __mode | ios_base::out))
1534    this->clear();
1535  else
1536    this->setstate(ios_base::failbit);
1537}
1538
1539template <class _CharT, class _Traits>
1540inline
1541void
1542basic_ofstream<_CharT, _Traits>::close()
1543{
1544    if (__sb_.close() == nullptr)
1545        this->setstate(ios_base::failbit);
1546}
1547
1548// basic_fstream
1549
1550template <class _CharT, class _Traits>
1551class _LIBCPP_TEMPLATE_VIS basic_fstream
1552    : public basic_iostream<_CharT, _Traits>
1553{
1554public:
1555    typedef _CharT                         char_type;
1556    typedef _Traits                        traits_type;
1557    typedef typename traits_type::int_type int_type;
1558    typedef typename traits_type::pos_type pos_type;
1559    typedef typename traits_type::off_type off_type;
1560
1561    _LIBCPP_INLINE_VISIBILITY
1562    basic_fstream();
1563    _LIBCPP_INLINE_VISIBILITY
1564    explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1565#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1566    _LIBCPP_INLINE_VISIBILITY
1567    explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1568#endif
1569    _LIBCPP_INLINE_VISIBILITY
1570    explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1571
1572#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1573    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1574    explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1575      : basic_fstream(__p.c_str(), __mode) {}
1576#endif // _LIBCPP_STD_VER >= 17
1577
1578    _LIBCPP_INLINE_VISIBILITY
1579    basic_fstream(basic_fstream&& __rhs);
1580
1581    _LIBCPP_INLINE_VISIBILITY
1582    basic_fstream& operator=(basic_fstream&& __rhs);
1583
1584    _LIBCPP_INLINE_VISIBILITY
1585    void swap(basic_fstream& __rhs);
1586
1587    _LIBCPP_INLINE_VISIBILITY
1588    basic_filebuf<char_type, traits_type>* rdbuf() const;
1589    _LIBCPP_INLINE_VISIBILITY
1590    bool is_open() const;
1591    _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1592#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1593    void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1594#endif
1595    _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1596
1597#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
1598    _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
1599    void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1600    { return open(__p.c_str(), __mode); }
1601#endif // _LIBCPP_STD_VER >= 17
1602
1603    _LIBCPP_INLINE_VISIBILITY
1604    void close();
1605
1606private:
1607    basic_filebuf<char_type, traits_type> __sb_;
1608};
1609
1610template <class _CharT, class _Traits>
1611inline
1612basic_fstream<_CharT, _Traits>::basic_fstream()
1613    : basic_iostream<char_type, traits_type>(&__sb_)
1614{
1615}
1616
1617template <class _CharT, class _Traits>
1618inline
1619basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1620    : basic_iostream<char_type, traits_type>(&__sb_)
1621{
1622    if (__sb_.open(__s, __mode) == nullptr)
1623        this->setstate(ios_base::failbit);
1624}
1625
1626#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1627template <class _CharT, class _Traits>
1628inline
1629basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1630    : basic_iostream<char_type, traits_type>(&__sb_)
1631{
1632    if (__sb_.open(__s, __mode) == nullptr)
1633        this->setstate(ios_base::failbit);
1634}
1635#endif
1636
1637template <class _CharT, class _Traits>
1638inline
1639basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1640    : basic_iostream<char_type, traits_type>(&__sb_)
1641{
1642    if (__sb_.open(__s, __mode) == nullptr)
1643        this->setstate(ios_base::failbit);
1644}
1645
1646template <class _CharT, class _Traits>
1647inline
1648basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1649    : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1650      __sb_(_VSTD::move(__rhs.__sb_))
1651{
1652    this->set_rdbuf(&__sb_);
1653}
1654
1655template <class _CharT, class _Traits>
1656inline
1657basic_fstream<_CharT, _Traits>&
1658basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1659{
1660    basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1661    __sb_ = _VSTD::move(__rhs.__sb_);
1662    return *this;
1663}
1664
1665template <class _CharT, class _Traits>
1666inline
1667void
1668basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1669{
1670    basic_iostream<char_type, traits_type>::swap(__rhs);
1671    __sb_.swap(__rhs.__sb_);
1672}
1673
1674template <class _CharT, class _Traits>
1675inline _LIBCPP_INLINE_VISIBILITY
1676void
1677swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1678{
1679    __x.swap(__y);
1680}
1681
1682template <class _CharT, class _Traits>
1683inline
1684basic_filebuf<_CharT, _Traits>*
1685basic_fstream<_CharT, _Traits>::rdbuf() const
1686{
1687    return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1688}
1689
1690template <class _CharT, class _Traits>
1691inline
1692bool
1693basic_fstream<_CharT, _Traits>::is_open() const
1694{
1695    return __sb_.is_open();
1696}
1697
1698template <class _CharT, class _Traits>
1699void
1700basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1701{
1702    if (__sb_.open(__s, __mode))
1703        this->clear();
1704    else
1705        this->setstate(ios_base::failbit);
1706}
1707
1708#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1709template <class _CharT, class _Traits>
1710void
1711basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1712{
1713    if (__sb_.open(__s, __mode))
1714        this->clear();
1715    else
1716        this->setstate(ios_base::failbit);
1717}
1718#endif
1719
1720template <class _CharT, class _Traits>
1721void
1722basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1723{
1724    if (__sb_.open(__s, __mode))
1725        this->clear();
1726    else
1727        this->setstate(ios_base::failbit);
1728}
1729
1730template <class _CharT, class _Traits>
1731inline
1732void
1733basic_fstream<_CharT, _Traits>::close()
1734{
1735    if (__sb_.close() == nullptr)
1736        this->setstate(ios_base::failbit);
1737}
1738
1739#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1740extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1741extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1742extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1743#endif
1744
1745_LIBCPP_END_NAMESPACE_STD
1746
1747#endif // _LIBCPP_HAS_NO_FSTREAM
1748
1749_LIBCPP_POP_MACROS
1750
1751#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1752#  include <atomic>
1753#  include <concepts>
1754#  include <cstdlib>
1755#  include <iosfwd>
1756#  include <limits>
1757#  include <new>
1758#  include <stdexcept>
1759#  include <type_traits>
1760#endif
1761
1762#endif // _LIBCPP_FSTREAM
1763