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