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