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