[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / libcxx / include / fstream
blobfc0a9204ed605814c926031c1133658682fa7865
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_FSTREAM
11 #define _LIBCPP_FSTREAM
14     fstream synopsis
16 template <class charT, class traits = char_traits<charT> >
17 class basic_filebuf
18     : public basic_streambuf<charT, traits>
20 public:
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;
27     // 27.9.1.2 Constructors/destructor:
28     basic_filebuf();
29     basic_filebuf(basic_filebuf&& rhs);
30     virtual ~basic_filebuf();
32     // 27.9.1.3 Assign/swap:
33     basic_filebuf& operator=(basic_filebuf&& rhs);
34     void swap(basic_filebuf& rhs);
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();
43 protected:
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);
59 template <class charT, class traits>
60   void
61   swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
63 typedef basic_filebuf<char>    filebuf;
64 typedef basic_filebuf<wchar_t> wfilebuf;
66 template <class charT, class traits = char_traits<charT> >
67 class basic_ifstream
68     : public basic_istream<charT,traits>
70 public:
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;
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);
84     basic_ifstream& operator=(basic_ifstream&& rhs);
85     void swap(basic_ifstream& rhs);
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
93     void close();
96 template <class charT, class traits>
97   void
98   swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
100 typedef basic_ifstream<char>    ifstream;
101 typedef basic_ifstream<wchar_t> wifstream;
103 template <class charT, class traits = char_traits<charT> >
104 class basic_ofstream
105     : public basic_ostream<charT,traits>
107 public:
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;
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);
121     basic_ofstream& operator=(basic_ofstream&& rhs);
122     void swap(basic_ofstream& rhs);
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
131     void close();
134 template <class charT, class traits>
135   void
136   swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
138 typedef basic_ofstream<char>    ofstream;
139 typedef basic_ofstream<wchar_t> wofstream;
141 template <class charT, class traits=char_traits<charT> >
142 class basic_fstream
143     : public basic_iostream<charT,traits>
145 public:
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;
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);
159     basic_fstream& operator=(basic_fstream&& rhs);
160     void swap(basic_fstream& rhs);
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
169     void close();
172 template <class charT, class traits>
173   void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
175 typedef basic_fstream<char>    fstream;
176 typedef basic_fstream<wchar_t> wfstream;
178 }  // std
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>
192 #if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
193 #   include <filesystem>
194 #endif
196 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
197 #pragma GCC system_header
198 #endif
200 _LIBCPP_PUSH_MACROS
201 #include <__undef_macros>
203 #if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
204 #  define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
205 #endif
207 _LIBCPP_BEGIN_NAMESPACE_STD
209 template <class _CharT, class _Traits>
210 class _LIBCPP_TEMPLATE_VIS basic_filebuf
211     : public basic_streambuf<_CharT, _Traits>
213 public:
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;
221     // 27.9.1.2 Constructors/destructor:
222     basic_filebuf();
223     basic_filebuf(basic_filebuf&& __rhs);
224     virtual ~basic_filebuf();
226     // 27.9.1.3 Assign/swap:
227     _LIBCPP_INLINE_VISIBILITY
228     basic_filebuf& operator=(basic_filebuf&& __rhs);
229     void swap(basic_filebuf& __rhs);
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);
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();
251     _LIBCPP_INLINE_VISIBILITY
252     inline static const char*
253     __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
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);
268 private:
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_;
286   bool __read_mode();
287   void __write_mode();
290 template <class _CharT, class _Traits>
291 basic_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)
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);
316 template <class _CharT, class _Traits>
317 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
318     : basic_streambuf<_CharT, _Traits>(__rhs)
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);
380 template <class _CharT, class _Traits>
381 inline
382 basic_filebuf<_CharT, _Traits>&
383 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
385     close();
386     swap(__rhs);
387     return *this;
390 template <class _CharT, class _Traits>
391 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
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_;
410 template <class _CharT, class _Traits>
411 void
412 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
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     }
488 template <class _CharT, class _Traits>
489 inline _LIBCPP_INLINE_VISIBILITY
490 void
491 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
493     __x.swap(__y);
496 template <class _CharT, class _Traits>
497 inline
498 bool
499 basic_filebuf<_CharT, _Traits>::is_open() const
501     return __file_ != nullptr;
504 template <class _CharT, class _Traits>
505 const 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();
544 template <class _CharT, class _Traits>
545 basic_filebuf<_CharT, _Traits>*
546 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
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;
570 template <class _CharT, class _Traits>
571 inline
572 basic_filebuf<_CharT, _Traits>*
573 basic_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;
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.
598 template <class _CharT, class _Traits>
599 basic_filebuf<_CharT, _Traits>*
600 basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
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;
677 #endif
679 template <class _CharT, class _Traits>
680 inline
681 basic_filebuf<_CharT, _Traits>*
682 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
684     return open(__s.c_str(), __mode);
687 template <class _CharT, class _Traits>
688 basic_filebuf<_CharT, _Traits>*
689 basic_filebuf<_CharT, _Traits>::close()
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;
706 template <class _CharT, class _Traits>
707 typename basic_filebuf<_CharT, _Traits>::int_type
708 basic_filebuf<_CharT, _Traits>::underflow()
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();
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;
776 template <class _CharT, class _Traits>
777 typename basic_filebuf<_CharT, _Traits>::int_type
778 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
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();
798 template <class _CharT, class _Traits>
799 typename basic_filebuf<_CharT, _Traits>::int_type
800 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
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();
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);
863 template <class _CharT, class _Traits>
864 basic_streambuf<_CharT, _Traits>*
865 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
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;
916 template <class _CharT, class _Traits>
917 typename basic_filebuf<_CharT, _Traits>::pos_type
918 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
919                                         ios_base::openmode)
921     if (!__cv_)
922         __throw_bad_cast();
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;
956 template <class _CharT, class _Traits>
957 typename basic_filebuf<_CharT, _Traits>::pos_type
958 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
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;
973 template <class _CharT, class _Traits>
975 basic_filebuf<_CharT, _Traits>::sync()
977     if (__file_ == nullptr)
978         return 0;
979     if (!__cv_)
980         __throw_bad_cast();
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;
1042 template <class _CharT, class _Traits>
1043 void
1044 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
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     }
1086 template <class _CharT, class _Traits>
1087 bool
1088 basic_filebuf<_CharT, _Traits>::__read_mode()
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;
1105 template <class _CharT, class _Traits>
1106 void
1107 basic_filebuf<_CharT, _Traits>::__write_mode()
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     }
1126 // basic_ifstream
1128 template <class _CharT, class _Traits>
1129 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1130     : public basic_istream<_CharT, _Traits>
1132 public:
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;
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);
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
1178     _LIBCPP_INLINE_VISIBILITY
1179     void __open(int __fd, ios_base::openmode __mode);
1180     _LIBCPP_INLINE_VISIBILITY
1181     void close();
1183 private:
1184     basic_filebuf<char_type, traits_type> __sb_;
1187 template <class _CharT, class _Traits>
1188 inline
1189 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1190     : basic_istream<char_type, traits_type>(&__sb_)
1194 template <class _CharT, class _Traits>
1195 inline
1196 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1197     : basic_istream<char_type, traits_type>(&__sb_)
1199     if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1200         this->setstate(ios_base::failbit);
1203 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1204 template <class _CharT, class _Traits>
1205 inline
1206 basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1207     : basic_istream<char_type, traits_type>(&__sb_)
1209     if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1210         this->setstate(ios_base::failbit);
1212 #endif
1214 template <class _CharT, class _Traits>
1215 inline
1216 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1217     : basic_istream<char_type, traits_type>(&__sb_)
1219     if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1220         this->setstate(ios_base::failbit);
1223 template <class _CharT, class _Traits>
1224 inline
1225 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1226     : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1227       __sb_(_VSTD::move(__rhs.__sb_))
1229     this->set_rdbuf(&__sb_);
1232 template <class _CharT, class _Traits>
1233 inline
1234 basic_ifstream<_CharT, _Traits>&
1235 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1237     basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1238     __sb_ = _VSTD::move(__rhs.__sb_);
1239     return *this;
1242 template <class _CharT, class _Traits>
1243 inline
1244 void
1245 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1247     basic_istream<char_type, traits_type>::swap(__rhs);
1248     __sb_.swap(__rhs.__sb_);
1251 template <class _CharT, class _Traits>
1252 inline _LIBCPP_INLINE_VISIBILITY
1253 void
1254 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1256     __x.swap(__y);
1259 template <class _CharT, class _Traits>
1260 inline
1261 basic_filebuf<_CharT, _Traits>*
1262 basic_ifstream<_CharT, _Traits>::rdbuf() const
1264     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1267 template <class _CharT, class _Traits>
1268 inline
1269 bool
1270 basic_ifstream<_CharT, _Traits>::is_open() const
1272     return __sb_.is_open();
1275 template <class _CharT, class _Traits>
1276 void
1277 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1279     if (__sb_.open(__s, __mode | ios_base::in))
1280         this->clear();
1281     else
1282         this->setstate(ios_base::failbit);
1285 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1286 template <class _CharT, class _Traits>
1287 void
1288 basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1290     if (__sb_.open(__s, __mode | ios_base::in))
1291         this->clear();
1292     else
1293         this->setstate(ios_base::failbit);
1295 #endif
1297 template <class _CharT, class _Traits>
1298 void
1299 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1301     if (__sb_.open(__s, __mode | ios_base::in))
1302         this->clear();
1303     else
1304         this->setstate(ios_base::failbit);
1307 template <class _CharT, class _Traits>
1308 inline
1309 void 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);
1317 template <class _CharT, class _Traits>
1318 inline
1319 void
1320 basic_ifstream<_CharT, _Traits>::close()
1322     if (__sb_.close() == 0)
1323         this->setstate(ios_base::failbit);
1326 // basic_ofstream
1328 template <class _CharT, class _Traits>
1329 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1330     : public basic_ostream<_CharT, _Traits>
1332 public:
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;
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);
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
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);
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);
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
1379     _LIBCPP_INLINE_VISIBILITY
1380     void __open(int __fd, ios_base::openmode __mode);
1381     _LIBCPP_INLINE_VISIBILITY
1382     void close();
1384 private:
1385     basic_filebuf<char_type, traits_type> __sb_;
1388 template <class _CharT, class _Traits>
1389 inline
1390 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1391     : basic_ostream<char_type, traits_type>(&__sb_)
1395 template <class _CharT, class _Traits>
1396 inline
1397 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1398     : basic_ostream<char_type, traits_type>(&__sb_)
1400     if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1401         this->setstate(ios_base::failbit);
1404 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1405 template <class _CharT, class _Traits>
1406 inline
1407 basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1408     : basic_ostream<char_type, traits_type>(&__sb_)
1410     if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1411         this->setstate(ios_base::failbit);
1413 #endif
1415 template <class _CharT, class _Traits>
1416 inline
1417 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1418     : basic_ostream<char_type, traits_type>(&__sb_)
1420     if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1421         this->setstate(ios_base::failbit);
1424 template <class _CharT, class _Traits>
1425 inline
1426 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1427     : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1428       __sb_(_VSTD::move(__rhs.__sb_))
1430     this->set_rdbuf(&__sb_);
1433 template <class _CharT, class _Traits>
1434 inline
1435 basic_ofstream<_CharT, _Traits>&
1436 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1438     basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1439     __sb_ = _VSTD::move(__rhs.__sb_);
1440     return *this;
1443 template <class _CharT, class _Traits>
1444 inline
1445 void
1446 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1448     basic_ostream<char_type, traits_type>::swap(__rhs);
1449     __sb_.swap(__rhs.__sb_);
1452 template <class _CharT, class _Traits>
1453 inline _LIBCPP_INLINE_VISIBILITY
1454 void
1455 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1457     __x.swap(__y);
1460 template <class _CharT, class _Traits>
1461 inline
1462 basic_filebuf<_CharT, _Traits>*
1463 basic_ofstream<_CharT, _Traits>::rdbuf() const
1465     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1468 template <class _CharT, class _Traits>
1469 inline
1470 bool
1471 basic_ofstream<_CharT, _Traits>::is_open() const
1473     return __sb_.is_open();
1476 template <class _CharT, class _Traits>
1477 void
1478 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1480     if (__sb_.open(__s, __mode | ios_base::out))
1481         this->clear();
1482     else
1483         this->setstate(ios_base::failbit);
1486 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1487 template <class _CharT, class _Traits>
1488 void
1489 basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1491     if (__sb_.open(__s, __mode | ios_base::out))
1492         this->clear();
1493     else
1494         this->setstate(ios_base::failbit);
1496 #endif
1498 template <class _CharT, class _Traits>
1499 void
1500 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1502     if (__sb_.open(__s, __mode | ios_base::out))
1503         this->clear();
1504     else
1505         this->setstate(ios_base::failbit);
1508 template <class _CharT, class _Traits>
1509 inline
1510 void 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);
1518 template <class _CharT, class _Traits>
1519 inline
1520 void
1521 basic_ofstream<_CharT, _Traits>::close()
1523     if (__sb_.close() == nullptr)
1524         this->setstate(ios_base::failbit);
1527 // basic_fstream
1529 template <class _CharT, class _Traits>
1530 class _LIBCPP_TEMPLATE_VIS basic_fstream
1531     : public basic_iostream<_CharT, _Traits>
1533 public:
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;
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);
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
1557     _LIBCPP_INLINE_VISIBILITY
1558     basic_fstream(basic_fstream&& __rhs);
1560     _LIBCPP_INLINE_VISIBILITY
1561     basic_fstream& operator=(basic_fstream&& __rhs);
1563     _LIBCPP_INLINE_VISIBILITY
1564     void swap(basic_fstream& __rhs);
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);
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
1582     _LIBCPP_INLINE_VISIBILITY
1583     void close();
1585 private:
1586     basic_filebuf<char_type, traits_type> __sb_;
1589 template <class _CharT, class _Traits>
1590 inline
1591 basic_fstream<_CharT, _Traits>::basic_fstream()
1592     : basic_iostream<char_type, traits_type>(&__sb_)
1596 template <class _CharT, class _Traits>
1597 inline
1598 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1599     : basic_iostream<char_type, traits_type>(&__sb_)
1601     if (__sb_.open(__s, __mode) == nullptr)
1602         this->setstate(ios_base::failbit);
1605 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606 template <class _CharT, class _Traits>
1607 inline
1608 basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1609     : basic_iostream<char_type, traits_type>(&__sb_)
1611     if (__sb_.open(__s, __mode) == nullptr)
1612         this->setstate(ios_base::failbit);
1614 #endif
1616 template <class _CharT, class _Traits>
1617 inline
1618 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1619     : basic_iostream<char_type, traits_type>(&__sb_)
1621     if (__sb_.open(__s, __mode) == nullptr)
1622         this->setstate(ios_base::failbit);
1625 template <class _CharT, class _Traits>
1626 inline
1627 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1628     : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1629       __sb_(_VSTD::move(__rhs.__sb_))
1631     this->set_rdbuf(&__sb_);
1634 template <class _CharT, class _Traits>
1635 inline
1636 basic_fstream<_CharT, _Traits>&
1637 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1639     basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1640     __sb_ = _VSTD::move(__rhs.__sb_);
1641     return *this;
1644 template <class _CharT, class _Traits>
1645 inline
1646 void
1647 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1649     basic_iostream<char_type, traits_type>::swap(__rhs);
1650     __sb_.swap(__rhs.__sb_);
1653 template <class _CharT, class _Traits>
1654 inline _LIBCPP_INLINE_VISIBILITY
1655 void
1656 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1658     __x.swap(__y);
1661 template <class _CharT, class _Traits>
1662 inline
1663 basic_filebuf<_CharT, _Traits>*
1664 basic_fstream<_CharT, _Traits>::rdbuf() const
1666     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1669 template <class _CharT, class _Traits>
1670 inline
1671 bool
1672 basic_fstream<_CharT, _Traits>::is_open() const
1674     return __sb_.is_open();
1677 template <class _CharT, class _Traits>
1678 void
1679 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1681     if (__sb_.open(__s, __mode))
1682         this->clear();
1683     else
1684         this->setstate(ios_base::failbit);
1687 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1688 template <class _CharT, class _Traits>
1689 void
1690 basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1692     if (__sb_.open(__s, __mode))
1693         this->clear();
1694     else
1695         this->setstate(ios_base::failbit);
1697 #endif
1699 template <class _CharT, class _Traits>
1700 void
1701 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1703     if (__sb_.open(__s, __mode))
1704         this->clear();
1705     else
1706         this->setstate(ios_base::failbit);
1709 template <class _CharT, class _Traits>
1710 inline
1711 void
1712 basic_fstream<_CharT, _Traits>::close()
1714     if (__sb_.close() == nullptr)
1715         this->setstate(ios_base::failbit);
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
1724 _LIBCPP_END_NAMESPACE_STD
1726 _LIBCPP_POP_MACROS
1728 #endif // _LIBCPP_FSTREAM