blob: 907c59e888fb0ddf23d9a5e228ea4d2d117450bf [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000040 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000080 explicit basic_ifstream(const filesystem::path& p,
81 ios_base::openmode mode = ios_base::in); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000091 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000117 explicit basic_ofstream(const filesystem::path& p,
118 ios_base::openmode mode = ios_base::out); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000128 void open(const filesystem::path& p,
129 ios_base::openmode mode = ios_base::out); // C++17
130
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 void close();
132};
133
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000134template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000155 explicit basic_fstream(const filesystem::path& p,
156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 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);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000166 void open(const filesystem::path& s,
167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
168
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 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
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100182#include <__algorithm/max.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400183#include <__assert> // all public C++ headers provide the assertion handler
Louis Dionne73912b22020-11-04 15:01:25 -0500184#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400185#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186#include <__locale>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100187#include <__utility/move.h>
188#include <__utility/swap.h>
Nikolas Klausercfe21472022-02-14 18:26:02 +0100189#include <__utility/unreachable.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000191#include <cstdlib>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100192#include <cstring>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400193#include <istream>
194#include <ostream>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100195#include <version>
Louis Dionnedb84e122021-01-18 12:18:18 -0500196
197#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
198# include <filesystem>
199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500202# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
Eric Fiselierf4433a32017-05-31 22:07:49 +0000205_LIBCPP_PUSH_MACROS
206#include <__undef_macros>
207
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400208#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
209# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
210#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000211
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212_LIBCPP_BEGIN_NAMESPACE_STD
213
214template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000215class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216 : public basic_streambuf<_CharT, _Traits>
217{
218public:
219 typedef _CharT char_type;
220 typedef _Traits traits_type;
221 typedef typename traits_type::int_type int_type;
222 typedef typename traits_type::pos_type pos_type;
223 typedef typename traits_type::off_type off_type;
224 typedef typename traits_type::state_type state_type;
225
226 // 27.9.1.2 Constructors/destructor:
227 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 basic_filebuf(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 virtual ~basic_filebuf();
230
231 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 void swap(basic_filebuf& __rhs);
235
236 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 bool is_open() const;
239 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000240#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
241 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
242#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000245
Louis Dionnedb84e122021-01-18 12:18:18 -0500246#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000247 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000248 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
249 return open(__p.c_str(), __mode);
250 }
251#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000252 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000253 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 basic_filebuf* close();
255
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000256 _LIBCPP_INLINE_VISIBILITY
257 inline static const char*
258 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
259
260 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 // 27.9.1.5 Overridden virtual functions:
262 virtual int_type underflow();
263 virtual int_type pbackfail(int_type __c = traits_type::eof());
264 virtual int_type overflow (int_type __c = traits_type::eof());
265 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
266 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
267 ios_base::openmode __wch = ios_base::in | ios_base::out);
268 virtual pos_type seekpos(pos_type __sp,
269 ios_base::openmode __wch = ios_base::in | ios_base::out);
270 virtual int sync();
271 virtual void imbue(const locale& __loc);
272
273private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000274 char* __extbuf_;
275 const char* __extbufnext_;
276 const char* __extbufend_;
277 char __extbuf_min_[8];
278 size_t __ebs_;
279 char_type* __intbuf_;
280 size_t __ibs_;
281 FILE* __file_;
282 const codecvt<char_type, char, state_type>* __cv_;
283 state_type __st_;
284 state_type __st_last_;
285 ios_base::openmode __om_;
286 ios_base::openmode __cm_;
287 bool __owns_eb_;
288 bool __owns_ib_;
289 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000291 bool __read_mode();
292 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293};
294
295template <class _CharT, class _Traits>
296basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500297 : __extbuf_(nullptr),
298 __extbufnext_(nullptr),
299 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500301 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500303 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000304 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000306 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 __om_(0),
308 __cm_(0),
309 __owns_eb_(false),
310 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000311 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312{
Howard Hinnant0090b122012-08-24 16:52:47 +0000313 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
314 {
315 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
316 __always_noconv_ = __cv_->always_noconv();
317 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500318 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319}
320
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321template <class _CharT, class _Traits>
322basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
323 : basic_streambuf<_CharT, _Traits>(__rhs)
324{
325 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
326 {
327 __extbuf_ = __extbuf_min_;
328 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
329 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
330 }
331 else
332 {
333 __extbuf_ = __rhs.__extbuf_;
334 __extbufnext_ = __rhs.__extbufnext_;
335 __extbufend_ = __rhs.__extbufend_;
336 }
337 __ebs_ = __rhs.__ebs_;
338 __intbuf_ = __rhs.__intbuf_;
339 __ibs_ = __rhs.__ibs_;
340 __file_ = __rhs.__file_;
341 __cv_ = __rhs.__cv_;
342 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000343 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344 __om_ = __rhs.__om_;
345 __cm_ = __rhs.__cm_;
346 __owns_eb_ = __rhs.__owns_eb_;
347 __owns_ib_ = __rhs.__owns_ib_;
348 __always_noconv_ = __rhs.__always_noconv_;
349 if (__rhs.pbase())
350 {
351 if (__rhs.pbase() == __rhs.__intbuf_)
352 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
353 else
354 this->setp((char_type*)__extbuf_,
355 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000356 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 }
358 else if (__rhs.eback())
359 {
360 if (__rhs.eback() == __rhs.__intbuf_)
361 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
362 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
363 else
364 this->setg((char_type*)__extbuf_,
365 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
366 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
367 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500368 __rhs.__extbuf_ = nullptr;
369 __rhs.__extbufnext_ = nullptr;
370 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 __rhs.__ebs_ = 0;
372 __rhs.__intbuf_ = 0;
373 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500374 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000376 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 __rhs.__om_ = 0;
378 __rhs.__cm_ = 0;
379 __rhs.__owns_eb_ = false;
380 __rhs.__owns_ib_ = false;
381 __rhs.setg(0, 0, 0);
382 __rhs.setp(0, 0);
383}
384
385template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000386inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387basic_filebuf<_CharT, _Traits>&
388basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
389{
390 close();
391 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000392 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393}
394
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395template <class _CharT, class _Traits>
396basic_filebuf<_CharT, _Traits>::~basic_filebuf()
397{
398#ifndef _LIBCPP_NO_EXCEPTIONS
399 try
400 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400401#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402 close();
403#ifndef _LIBCPP_NO_EXCEPTIONS
404 }
405 catch (...)
406 {
407 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400408#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 if (__owns_eb_)
410 delete [] __extbuf_;
411 if (__owns_ib_)
412 delete [] __intbuf_;
413}
414
415template <class _CharT, class _Traits>
416void
417basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
418{
419 basic_streambuf<char_type, traits_type>::swap(__rhs);
420 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
421 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000422 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
423 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
424 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 }
426 else
427 {
428 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
429 ptrdiff_t __le = __extbufend_ - __extbuf_;
430 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
431 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
432 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
433 {
434 __extbuf_ = __rhs.__extbuf_;
435 __rhs.__extbuf_ = __rhs.__extbuf_min_;
436 }
437 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
438 {
439 __rhs.__extbuf_ = __extbuf_;
440 __extbuf_ = __extbuf_min_;
441 }
442 __extbufnext_ = __extbuf_ + __rn;
443 __extbufend_ = __extbuf_ + __re;
444 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
445 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
446 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000447 _VSTD::swap(__ebs_, __rhs.__ebs_);
448 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
449 _VSTD::swap(__ibs_, __rhs.__ibs_);
450 _VSTD::swap(__file_, __rhs.__file_);
451 _VSTD::swap(__cv_, __rhs.__cv_);
452 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000453 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000454 _VSTD::swap(__om_, __rhs.__om_);
455 _VSTD::swap(__cm_, __rhs.__cm_);
456 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
457 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
458 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
460 {
461 ptrdiff_t __n = this->gptr() - this->eback();
462 ptrdiff_t __e = this->egptr() - this->eback();
463 this->setg((char_type*)__extbuf_min_,
464 (char_type*)__extbuf_min_ + __n,
465 (char_type*)__extbuf_min_ + __e);
466 }
467 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
468 {
469 ptrdiff_t __n = this->pptr() - this->pbase();
470 ptrdiff_t __e = this->epptr() - this->pbase();
471 this->setp((char_type*)__extbuf_min_,
472 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000473 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 }
475 if (__rhs.eback() == (char_type*)__extbuf_min_)
476 {
477 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
478 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
479 __rhs.setg((char_type*)__rhs.__extbuf_min_,
480 (char_type*)__rhs.__extbuf_min_ + __n,
481 (char_type*)__rhs.__extbuf_min_ + __e);
482 }
483 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
484 {
485 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
486 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
487 __rhs.setp((char_type*)__rhs.__extbuf_min_,
488 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000489 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490 }
491}
492
493template <class _CharT, class _Traits>
494inline _LIBCPP_INLINE_VISIBILITY
495void
496swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
497{
498 __x.swap(__y);
499}
500
501template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000502inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503bool
504basic_filebuf<_CharT, _Traits>::is_open() const
505{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500506 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507}
508
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000509template <class _CharT, class _Traits>
510const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
511 ios_base::openmode __mode) _NOEXCEPT {
512 switch (__mode & ~ios_base::ate) {
513 case ios_base::out:
514 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000515 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000516 case ios_base::out | ios_base::app:
517 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000518 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000519 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000520 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000521 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000522 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000523 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000524 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000525 case ios_base::in | ios_base::out | ios_base::app:
526 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::out | ios_base::binary:
529 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::out | ios_base::app | ios_base::binary:
532 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000535 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000536 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000537 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000538 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000539 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000540 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
541 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000542 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000543 default:
544 return nullptr;
545 }
Nikolas Klausercfe21472022-02-14 18:26:02 +0100546 __libcpp_unreachable();
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000547}
548
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549template <class _CharT, class _Traits>
550basic_filebuf<_CharT, _Traits>*
551basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
552{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500553 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
554 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000556 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000558 __file_ = fopen(__s, __mdstr);
559 if (__file_) {
560 __om_ = __mode;
561 if (__mode & ios_base::ate) {
562 if (fseek(__file_, 0, SEEK_END)) {
563 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500564 __file_ = nullptr;
565 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000567 }
568 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500569 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000570 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571 }
572 return __rt;
573}
574
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000575template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000576inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400577basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000578basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500579 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
580 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000581 if (const char* __mdstr = __make_mdstring(__mode)) {
582 __rt = this;
583 __file_ = fdopen(__fd, __mdstr);
584 if (__file_) {
585 __om_ = __mode;
586 if (__mode & ios_base::ate) {
587 if (fseek(__file_, 0, SEEK_END)) {
588 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500589 __file_ = nullptr;
590 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000591 }
592 }
593 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500594 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000595 }
596 }
597 return __rt;
598}
599
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000600#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
601// This is basically the same as the char* overload except that it uses _wfopen
602// and long mode strings.
603template <class _CharT, class _Traits>
604basic_filebuf<_CharT, _Traits>*
605basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
606{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500607 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
608 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000609 {
610 __rt = this;
611 const wchar_t* __mdstr;
612 switch (__mode & ~ios_base::ate)
613 {
614 case ios_base::out:
615 case ios_base::out | ios_base::trunc:
616 __mdstr = L"w";
617 break;
618 case ios_base::out | ios_base::app:
619 case ios_base::app:
620 __mdstr = L"a";
621 break;
622 case ios_base::in:
623 __mdstr = L"r";
624 break;
625 case ios_base::in | ios_base::out:
626 __mdstr = L"r+";
627 break;
628 case ios_base::in | ios_base::out | ios_base::trunc:
629 __mdstr = L"w+";
630 break;
631 case ios_base::in | ios_base::out | ios_base::app:
632 case ios_base::in | ios_base::app:
633 __mdstr = L"a+";
634 break;
635 case ios_base::out | ios_base::binary:
636 case ios_base::out | ios_base::trunc | ios_base::binary:
637 __mdstr = L"wb";
638 break;
639 case ios_base::out | ios_base::app | ios_base::binary:
640 case ios_base::app | ios_base::binary:
641 __mdstr = L"ab";
642 break;
643 case ios_base::in | ios_base::binary:
644 __mdstr = L"rb";
645 break;
646 case ios_base::in | ios_base::out | ios_base::binary:
647 __mdstr = L"r+b";
648 break;
649 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
650 __mdstr = L"w+b";
651 break;
652 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
653 case ios_base::in | ios_base::app | ios_base::binary:
654 __mdstr = L"a+b";
655 break;
656 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500657 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000658 break;
659 }
660 if (__rt)
661 {
662 __file_ = _wfopen(__s, __mdstr);
663 if (__file_)
664 {
665 __om_ = __mode;
666 if (__mode & ios_base::ate)
667 {
668 if (fseek(__file_, 0, SEEK_END))
669 {
670 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500671 __file_ = nullptr;
672 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000673 }
674 }
675 }
676 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500677 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000678 }
679 }
680 return __rt;
681}
682#endif
683
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000685inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686basic_filebuf<_CharT, _Traits>*
687basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
688{
689 return open(__s.c_str(), __mode);
690}
691
692template <class _CharT, class _Traits>
693basic_filebuf<_CharT, _Traits>*
694basic_filebuf<_CharT, _Traits>::close()
695{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500696 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 if (__file_)
698 {
699 __rt = this;
700 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000701 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500702 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000703 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500704 __rt = nullptr;
705 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000706 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 }
708 return __rt;
709}
710
711template <class _CharT, class _Traits>
712typename basic_filebuf<_CharT, _Traits>::int_type
713basic_filebuf<_CharT, _Traits>::underflow()
714{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500715 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716 return traits_type::eof();
717 bool __initial = __read_mode();
718 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500719 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 this->setg(&__1buf, &__1buf+1, &__1buf+1);
721 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
722 int_type __c = traits_type::eof();
723 if (this->gptr() == this->egptr())
724 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500725 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 if (__always_noconv_)
727 {
728 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
729 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
730 if (__nmemb != 0)
731 {
732 this->setg(this->eback(),
733 this->eback() + __unget_sz,
734 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000735 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 }
737 }
738 else
739 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000740 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
741 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500742 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
744 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000745 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 static_cast<size_t>(__extbufend_ - __extbufnext_));
747 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000748 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000749 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 if (__nr != 0)
751 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000752 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000753 __throw_bad_cast();
754
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 __extbufend_ = __extbufnext_ + __nr;
756 char_type* __inext;
757 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
758 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000759 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 if (__r == codecvt_base::noconv)
761 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000762 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000763 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000764 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 }
766 else if (__inext != this->eback() + __unget_sz)
767 {
768 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000769 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 }
771 }
772 }
773 }
774 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000775 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500777 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 return __c;
779}
780
781template <class _CharT, class _Traits>
782typename basic_filebuf<_CharT, _Traits>::int_type
783basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
784{
785 if (__file_ && this->eback() < this->gptr())
786 {
787 if (traits_type::eq_int_type(__c, traits_type::eof()))
788 {
789 this->gbump(-1);
790 return traits_type::not_eof(__c);
791 }
792 if ((__om_ & ios_base::out) ||
793 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
794 {
795 this->gbump(-1);
796 *this->gptr() = traits_type::to_char_type(__c);
797 return __c;
798 }
799 }
800 return traits_type::eof();
801}
802
803template <class _CharT, class _Traits>
804typename basic_filebuf<_CharT, _Traits>::int_type
805basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
806{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500807 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 return traits_type::eof();
809 __write_mode();
810 char_type __1buf;
811 char_type* __pb_save = this->pbase();
812 char_type* __epb_save = this->epptr();
813 if (!traits_type::eq_int_type(__c, traits_type::eof()))
814 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500815 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 this->setp(&__1buf, &__1buf+1);
817 *this->pptr() = traits_type::to_char_type(__c);
818 this->pbump(1);
819 }
820 if (this->pptr() != this->pbase())
821 {
822 if (__always_noconv_)
823 {
824 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
825 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
826 return traits_type::eof();
827 }
828 else
829 {
830 char* __extbe = __extbuf_;
831 codecvt_base::result __r;
832 do
833 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000834 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000835 __throw_bad_cast();
836
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 const char_type* __e;
838 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
839 __extbuf_, __extbuf_ + __ebs_, __extbe);
840 if (__e == this->pbase())
841 return traits_type::eof();
842 if (__r == codecvt_base::noconv)
843 {
844 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
845 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
846 return traits_type::eof();
847 }
848 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
849 {
850 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
851 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
852 return traits_type::eof();
853 if (__r == codecvt_base::partial)
854 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000855 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000856 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857 }
858 }
859 else
860 return traits_type::eof();
861 } while (__r == codecvt_base::partial);
862 }
863 this->setp(__pb_save, __epb_save);
864 }
865 return traits_type::not_eof(__c);
866}
867
868template <class _CharT, class _Traits>
869basic_streambuf<_CharT, _Traits>*
870basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
871{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500872 this->setg(nullptr, nullptr, nullptr);
873 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 if (__owns_eb_)
875 delete [] __extbuf_;
876 if (__owns_ib_)
877 delete [] __intbuf_;
878 __ebs_ = __n;
879 if (__ebs_ > sizeof(__extbuf_min_))
880 {
881 if (__always_noconv_ && __s)
882 {
883 __extbuf_ = (char*)__s;
884 __owns_eb_ = false;
885 }
886 else
887 {
888 __extbuf_ = new char[__ebs_];
889 __owns_eb_ = true;
890 }
891 }
892 else
893 {
894 __extbuf_ = __extbuf_min_;
895 __ebs_ = sizeof(__extbuf_min_);
896 __owns_eb_ = false;
897 }
898 if (!__always_noconv_)
899 {
900 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
901 if (__s && __ibs_ >= sizeof(__extbuf_min_))
902 {
903 __intbuf_ = __s;
904 __owns_ib_ = false;
905 }
906 else
907 {
908 __intbuf_ = new char_type[__ibs_];
909 __owns_ib_ = true;
910 }
911 }
912 else
913 {
914 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500915 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 __owns_ib_ = false;
917 }
918 return this;
919}
920
921template <class _CharT, class _Traits>
922typename basic_filebuf<_CharT, _Traits>::pos_type
923basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
924 ios_base::openmode)
925{
Howard Hinnant0090b122012-08-24 16:52:47 +0000926 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000927 __throw_bad_cast();
928
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500930 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 return pos_type(off_type(-1));
932 // __width > 0 || __off == 0
933 int __whence;
934 switch (__way)
935 {
936 case ios_base::beg:
937 __whence = SEEK_SET;
938 break;
939 case ios_base::cur:
940 __whence = SEEK_CUR;
941 break;
942 case ios_base::end:
943 __whence = SEEK_END;
944 break;
945 default:
946 return pos_type(off_type(-1));
947 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000948#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000949 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
950 return pos_type(off_type(-1));
951 pos_type __r = ftell(__file_);
952#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
954 return pos_type(off_type(-1));
955 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000956#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957 __r.state(__st_);
958 return __r;
959}
960
961template <class _CharT, class _Traits>
962typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000963basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500965 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000967#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000968 if (fseek(__file_, __sp, SEEK_SET))
969 return pos_type(off_type(-1));
970#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 if (fseeko(__file_, __sp, SEEK_SET))
972 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000973#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000974 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 return __sp;
976}
977
978template <class _CharT, class _Traits>
979int
980basic_filebuf<_CharT, _Traits>::sync()
981{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500982 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000984 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000985 __throw_bad_cast();
986
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 if (__cm_ & ios_base::out)
988 {
989 if (this->pptr() != this->pbase())
990 if (overflow() == traits_type::eof())
991 return -1;
992 codecvt_base::result __r;
993 do
994 {
995 char* __extbe;
996 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
997 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
998 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
999 return -1;
1000 } while (__r == codecvt_base::partial);
1001 if (__r == codecvt_base::error)
1002 return -1;
1003 if (fflush(__file_))
1004 return -1;
1005 }
1006 else if (__cm_ & ios_base::in)
1007 {
1008 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001009 state_type __state = __st_last_;
1010 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 if (__always_noconv_)
1012 __c = this->egptr() - this->gptr();
1013 else
1014 {
1015 int __width = __cv_->encoding();
1016 __c = __extbufend_ - __extbufnext_;
1017 if (__width > 0)
1018 __c += __width * (this->egptr() - this->gptr());
1019 else
1020 {
1021 if (this->gptr() != this->egptr())
1022 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001023 const int __off = __cv_->length(__state, __extbuf_,
1024 __extbufnext_,
1025 this->gptr() - this->eback());
1026 __c += __extbufnext_ - __extbuf_ - __off;
1027 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 }
1029 }
1030 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001031#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001032 if (fseek(__file_, -__c, SEEK_CUR))
1033 return -1;
1034#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 if (fseeko(__file_, -__c, SEEK_CUR))
1036 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001037#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001038 if (__update_st)
1039 __st_ = __state;
1040 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001041 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 __cm_ = 0;
1043 }
1044 return 0;
1045}
1046
1047template <class _CharT, class _Traits>
1048void
1049basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1050{
1051 sync();
1052 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1053 bool __old_anc = __always_noconv_;
1054 __always_noconv_ = __cv_->always_noconv();
1055 if (__old_anc != __always_noconv_)
1056 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001057 this->setg(nullptr, nullptr, nullptr);
1058 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 // invariant, char_type is char, else we couldn't get here
1060 if (__always_noconv_) // need to dump __intbuf_
1061 {
1062 if (__owns_eb_)
1063 delete [] __extbuf_;
1064 __owns_eb_ = __owns_ib_;
1065 __ebs_ = __ibs_;
1066 __extbuf_ = (char*)__intbuf_;
1067 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001068 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 __owns_ib_ = false;
1070 }
1071 else // need to obtain an __intbuf_.
1072 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1073 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1074 {
1075 __ibs_ = __ebs_;
1076 __intbuf_ = (char_type*)__extbuf_;
1077 __owns_ib_ = false;
1078 __extbuf_ = new char[__ebs_];
1079 __owns_eb_ = true;
1080 }
1081 else
1082 {
1083 __ibs_ = __ebs_;
1084 __intbuf_ = new char_type[__ibs_];
1085 __owns_ib_ = true;
1086 }
1087 }
1088 }
1089}
1090
1091template <class _CharT, class _Traits>
1092bool
1093basic_filebuf<_CharT, _Traits>::__read_mode()
1094{
1095 if (!(__cm_ & ios_base::in))
1096 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001097 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 if (__always_noconv_)
1099 this->setg((char_type*)__extbuf_,
1100 (char_type*)__extbuf_ + __ebs_,
1101 (char_type*)__extbuf_ + __ebs_);
1102 else
1103 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1104 __cm_ = ios_base::in;
1105 return true;
1106 }
1107 return false;
1108}
1109
1110template <class _CharT, class _Traits>
1111void
1112basic_filebuf<_CharT, _Traits>::__write_mode()
1113{
1114 if (!(__cm_ & ios_base::out))
1115 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001116 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 if (__ebs_ > sizeof(__extbuf_min_))
1118 {
1119 if (__always_noconv_)
1120 this->setp((char_type*)__extbuf_,
1121 (char_type*)__extbuf_ + (__ebs_ - 1));
1122 else
1123 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1124 }
1125 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001126 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 __cm_ = ios_base::out;
1128 }
1129}
1130
1131// basic_ifstream
1132
1133template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001134class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 : public basic_istream<_CharT, _Traits>
1136{
1137public:
1138 typedef _CharT char_type;
1139 typedef _Traits traits_type;
1140 typedef typename traits_type::int_type int_type;
1141 typedef typename traits_type::pos_type pos_type;
1142 typedef typename traits_type::off_type off_type;
1143
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001148#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 _LIBCPP_INLINE_VISIBILITY
1150 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1151#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001154#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001155 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001156 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1157 : basic_ifstream(__p.c_str(), __mode) {}
1158#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 void swap(basic_ifstream& __rhs);
1165
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 bool is_open() const;
1170 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001171#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1172 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001175#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001176 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001177 void open(const filesystem::path& __p,
1178 ios_base::openmode __mode = ios_base::in) {
1179 return open(__p.c_str(), __mode);
1180 }
1181#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001182
1183 _LIBCPP_INLINE_VISIBILITY
1184 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 void close();
1187
1188private:
1189 basic_filebuf<char_type, traits_type> __sb_;
1190};
1191
1192template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194basic_ifstream<_CharT, _Traits>::basic_ifstream()
1195 : basic_istream<char_type, traits_type>(&__sb_)
1196{
1197}
1198
1199template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001200inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1202 : basic_istream<char_type, traits_type>(&__sb_)
1203{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001204 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 this->setstate(ios_base::failbit);
1206}
1207
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001208#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1209template <class _CharT, class _Traits>
1210inline
1211basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1212 : basic_istream<char_type, traits_type>(&__sb_)
1213{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001214 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001215 this->setstate(ios_base::failbit);
1216}
1217#endif
1218
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001220inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1222 : basic_istream<char_type, traits_type>(&__sb_)
1223{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001224 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 this->setstate(ios_base::failbit);
1226}
1227
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001229inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001231 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1232 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233{
1234 this->set_rdbuf(&__sb_);
1235}
1236
1237template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001238inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239basic_ifstream<_CharT, _Traits>&
1240basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1241{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001242 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1243 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 return *this;
1245}
1246
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249void
1250basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1251{
1252 basic_istream<char_type, traits_type>::swap(__rhs);
1253 __sb_.swap(__rhs.__sb_);
1254}
1255
1256template <class _CharT, class _Traits>
1257inline _LIBCPP_INLINE_VISIBILITY
1258void
1259swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1260{
1261 __x.swap(__y);
1262}
1263
1264template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001265inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266basic_filebuf<_CharT, _Traits>*
1267basic_ifstream<_CharT, _Traits>::rdbuf() const
1268{
1269 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1270}
1271
1272template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001273inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274bool
1275basic_ifstream<_CharT, _Traits>::is_open() const
1276{
1277 return __sb_.is_open();
1278}
1279
1280template <class _CharT, class _Traits>
1281void
1282basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1283{
1284 if (__sb_.open(__s, __mode | ios_base::in))
1285 this->clear();
1286 else
1287 this->setstate(ios_base::failbit);
1288}
1289
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001290#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1291template <class _CharT, class _Traits>
1292void
1293basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1294{
1295 if (__sb_.open(__s, __mode | ios_base::in))
1296 this->clear();
1297 else
1298 this->setstate(ios_base::failbit);
1299}
1300#endif
1301
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302template <class _CharT, class _Traits>
1303void
1304basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1305{
1306 if (__sb_.open(__s, __mode | ios_base::in))
1307 this->clear();
1308 else
1309 this->setstate(ios_base::failbit);
1310}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001311
1312template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001313inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001314void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1315 ios_base::openmode __mode) {
1316 if (__sb_.__open(__fd, __mode | ios_base::in))
1317 this->clear();
1318 else
1319 this->setstate(ios_base::failbit);
1320}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321
1322template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001323inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324void
1325basic_ifstream<_CharT, _Traits>::close()
1326{
1327 if (__sb_.close() == 0)
1328 this->setstate(ios_base::failbit);
1329}
1330
1331// basic_ofstream
1332
1333template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001334class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335 : public basic_ostream<_CharT, _Traits>
1336{
1337public:
1338 typedef _CharT char_type;
1339 typedef _Traits traits_type;
1340 typedef typename traits_type::int_type int_type;
1341 typedef typename traits_type::pos_type pos_type;
1342 typedef typename traits_type::off_type off_type;
1343
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001348#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1349 _LIBCPP_INLINE_VISIBILITY
1350 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1351#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001354
Louis Dionnedb84e122021-01-18 12:18:18 -05001355#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001356 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001357 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1358 : basic_ofstream(__p.c_str(), __mode) {}
1359#endif // _LIBCPP_STD_VER >= 17
1360
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 void swap(basic_ofstream& __rhs);
1367
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 bool is_open() const;
1372 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001373#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1374 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1375#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001377
Louis Dionnedb84e122021-01-18 12:18:18 -05001378#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001379 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001380 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1381 { return open(__p.c_str(), __mode); }
1382#endif // _LIBCPP_STD_VER >= 17
1383
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001384 _LIBCPP_INLINE_VISIBILITY
1385 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 void close();
1388
1389private:
1390 basic_filebuf<char_type, traits_type> __sb_;
1391};
1392
1393template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001394inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395basic_ofstream<_CharT, _Traits>::basic_ofstream()
1396 : basic_ostream<char_type, traits_type>(&__sb_)
1397{
1398}
1399
1400template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001401inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1403 : basic_ostream<char_type, traits_type>(&__sb_)
1404{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001405 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 this->setstate(ios_base::failbit);
1407}
1408
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001409#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1410template <class _CharT, class _Traits>
1411inline
1412basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1413 : basic_ostream<char_type, traits_type>(&__sb_)
1414{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001415 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001416 this->setstate(ios_base::failbit);
1417}
1418#endif
1419
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001421inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1423 : basic_ostream<char_type, traits_type>(&__sb_)
1424{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001425 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 this->setstate(ios_base::failbit);
1427}
1428
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001430inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001432 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1433 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434{
1435 this->set_rdbuf(&__sb_);
1436}
1437
1438template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001439inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440basic_ofstream<_CharT, _Traits>&
1441basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1442{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001443 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1444 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 return *this;
1446}
1447
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450void
1451basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1452{
1453 basic_ostream<char_type, traits_type>::swap(__rhs);
1454 __sb_.swap(__rhs.__sb_);
1455}
1456
1457template <class _CharT, class _Traits>
1458inline _LIBCPP_INLINE_VISIBILITY
1459void
1460swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1461{
1462 __x.swap(__y);
1463}
1464
1465template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001466inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467basic_filebuf<_CharT, _Traits>*
1468basic_ofstream<_CharT, _Traits>::rdbuf() const
1469{
1470 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1471}
1472
1473template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001474inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475bool
1476basic_ofstream<_CharT, _Traits>::is_open() const
1477{
1478 return __sb_.is_open();
1479}
1480
1481template <class _CharT, class _Traits>
1482void
1483basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1484{
1485 if (__sb_.open(__s, __mode | ios_base::out))
1486 this->clear();
1487 else
1488 this->setstate(ios_base::failbit);
1489}
1490
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001491#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1492template <class _CharT, class _Traits>
1493void
1494basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1495{
1496 if (__sb_.open(__s, __mode | ios_base::out))
1497 this->clear();
1498 else
1499 this->setstate(ios_base::failbit);
1500}
1501#endif
1502
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503template <class _CharT, class _Traits>
1504void
1505basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1506{
1507 if (__sb_.open(__s, __mode | ios_base::out))
1508 this->clear();
1509 else
1510 this->setstate(ios_base::failbit);
1511}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001512
1513template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001514inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001515void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1516 ios_base::openmode __mode) {
1517 if (__sb_.__open(__fd, __mode | ios_base::out))
1518 this->clear();
1519 else
1520 this->setstate(ios_base::failbit);
1521}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522
1523template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001524inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525void
1526basic_ofstream<_CharT, _Traits>::close()
1527{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001528 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 this->setstate(ios_base::failbit);
1530}
1531
1532// basic_fstream
1533
1534template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001535class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 : public basic_iostream<_CharT, _Traits>
1537{
1538public:
1539 typedef _CharT char_type;
1540 typedef _Traits traits_type;
1541 typedef typename traits_type::int_type int_type;
1542 typedef typename traits_type::pos_type pos_type;
1543 typedef typename traits_type::off_type off_type;
1544
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001549#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1550 _LIBCPP_INLINE_VISIBILITY
1551 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1552#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001555
Louis Dionnedb84e122021-01-18 12:18:18 -05001556#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001557 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001558 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1559 : basic_fstream(__p.c_str(), __mode) {}
1560#endif // _LIBCPP_STD_VER >= 17
1561
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001567
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 void swap(basic_fstream& __rhs);
1570
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 bool is_open() const;
1575 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001576#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1577 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1578#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001580
Louis Dionnedb84e122021-01-18 12:18:18 -05001581#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001582 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001583 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1584 { return open(__p.c_str(), __mode); }
1585#endif // _LIBCPP_STD_VER >= 17
1586
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void close();
1589
1590private:
1591 basic_filebuf<char_type, traits_type> __sb_;
1592};
1593
1594template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001595inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596basic_fstream<_CharT, _Traits>::basic_fstream()
1597 : basic_iostream<char_type, traits_type>(&__sb_)
1598{
1599}
1600
1601template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001602inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1604 : basic_iostream<char_type, traits_type>(&__sb_)
1605{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001606 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 this->setstate(ios_base::failbit);
1608}
1609
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001610#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1611template <class _CharT, class _Traits>
1612inline
1613basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1614 : basic_iostream<char_type, traits_type>(&__sb_)
1615{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001616 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001617 this->setstate(ios_base::failbit);
1618}
1619#endif
1620
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001622inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1624 : basic_iostream<char_type, traits_type>(&__sb_)
1625{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001626 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 this->setstate(ios_base::failbit);
1628}
1629
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001631inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001633 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1634 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635{
1636 this->set_rdbuf(&__sb_);
1637}
1638
1639template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001640inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641basic_fstream<_CharT, _Traits>&
1642basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1643{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001644 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1645 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 return *this;
1647}
1648
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001650inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651void
1652basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1653{
1654 basic_iostream<char_type, traits_type>::swap(__rhs);
1655 __sb_.swap(__rhs.__sb_);
1656}
1657
1658template <class _CharT, class _Traits>
1659inline _LIBCPP_INLINE_VISIBILITY
1660void
1661swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1662{
1663 __x.swap(__y);
1664}
1665
1666template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668basic_filebuf<_CharT, _Traits>*
1669basic_fstream<_CharT, _Traits>::rdbuf() const
1670{
1671 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1672}
1673
1674template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001675inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676bool
1677basic_fstream<_CharT, _Traits>::is_open() const
1678{
1679 return __sb_.is_open();
1680}
1681
1682template <class _CharT, class _Traits>
1683void
1684basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1685{
1686 if (__sb_.open(__s, __mode))
1687 this->clear();
1688 else
1689 this->setstate(ios_base::failbit);
1690}
1691
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001692#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1693template <class _CharT, class _Traits>
1694void
1695basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1696{
1697 if (__sb_.open(__s, __mode))
1698 this->clear();
1699 else
1700 this->setstate(ios_base::failbit);
1701}
1702#endif
1703
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704template <class _CharT, class _Traits>
1705void
1706basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1707{
1708 if (__sb_.open(__s, __mode))
1709 this->clear();
1710 else
1711 this->setstate(ios_base::failbit);
1712}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713
1714template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001715inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716void
1717basic_fstream<_CharT, _Traits>::close()
1718{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001719 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 this->setstate(ios_base::failbit);
1721}
1722
Louis Dionne8a79be62020-10-21 11:11:45 -04001723#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1724_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1725_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1726_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1727#endif
1728
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729_LIBCPP_END_NAMESPACE_STD
1730
Eric Fiselierf4433a32017-05-31 22:07:49 +00001731_LIBCPP_POP_MACROS
1732
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001733#endif // _LIBCPP_FSTREAM