blob: 65b22a4db70a990bbdd714a532bd23ef6bc61a64 [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>
Nikolas Klauser0b5df932022-09-05 00:01:15 +0200195#include <typeinfo>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100196#include <version>
Louis Dionnedb84e122021-01-18 12:18:18 -0500197
198#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
199# include <filesystem>
200#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000202#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500203# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000204#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205
Eric Fiselierf4433a32017-05-31 22:07:49 +0000206_LIBCPP_PUSH_MACROS
207#include <__undef_macros>
208
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400209#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
210# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
211#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000212
Louis Dionne3f3f7532022-11-18 15:01:33 -0500213#if !defined(_LIBCPP_HAS_NO_FSTREAM)
214
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215_LIBCPP_BEGIN_NAMESPACE_STD
216
217template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000218class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 : public basic_streambuf<_CharT, _Traits>
220{
221public:
222 typedef _CharT char_type;
223 typedef _Traits traits_type;
224 typedef typename traits_type::int_type int_type;
225 typedef typename traits_type::pos_type pos_type;
226 typedef typename traits_type::off_type off_type;
227 typedef typename traits_type::state_type state_type;
228
229 // 27.9.1.2 Constructors/destructor:
230 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 basic_filebuf(basic_filebuf&& __rhs);
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200232 ~basic_filebuf() override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233
234 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 void swap(basic_filebuf& __rhs);
238
239 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 bool is_open() const;
242 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000243#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
244 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
245#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000248
Louis Dionnedb84e122021-01-18 12:18:18 -0500249#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000250 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000251 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
252 return open(__p.c_str(), __mode);
253 }
254#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000255 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000256 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257 basic_filebuf* close();
258
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000259 _LIBCPP_INLINE_VISIBILITY
260 inline static const char*
261 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
262
263 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 // 27.9.1.5 Overridden virtual functions:
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200265 int_type underflow() override;
266 int_type pbackfail(int_type __c = traits_type::eof()) override;
267 int_type overflow (int_type __c = traits_type::eof()) override;
268 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
269 pos_type seekoff(off_type __off, ios_base::seekdir __way,
270 ios_base::openmode __wch = ios_base::in | ios_base::out) override;
271 pos_type seekpos(pos_type __sp,
272 ios_base::openmode __wch = ios_base::in | ios_base::out) override;
273 int sync() override;
274 void imbue(const locale& __loc) override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
276private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000277 char* __extbuf_;
278 const char* __extbufnext_;
279 const char* __extbufend_;
280 char __extbuf_min_[8];
281 size_t __ebs_;
282 char_type* __intbuf_;
283 size_t __ibs_;
284 FILE* __file_;
285 const codecvt<char_type, char, state_type>* __cv_;
286 state_type __st_;
287 state_type __st_last_;
288 ios_base::openmode __om_;
289 ios_base::openmode __cm_;
290 bool __owns_eb_;
291 bool __owns_ib_;
292 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000294 bool __read_mode();
295 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296};
297
298template <class _CharT, class _Traits>
299basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500300 : __extbuf_(nullptr),
301 __extbufnext_(nullptr),
302 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500304 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500306 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000307 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000309 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310 __om_(0),
311 __cm_(0),
312 __owns_eb_(false),
313 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000314 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315{
Howard Hinnant0090b122012-08-24 16:52:47 +0000316 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
317 {
318 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
319 __always_noconv_ = __cv_->always_noconv();
320 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500321 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322}
323
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324template <class _CharT, class _Traits>
325basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
326 : basic_streambuf<_CharT, _Traits>(__rhs)
327{
328 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
329 {
330 __extbuf_ = __extbuf_min_;
331 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
332 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
333 }
334 else
335 {
336 __extbuf_ = __rhs.__extbuf_;
337 __extbufnext_ = __rhs.__extbufnext_;
338 __extbufend_ = __rhs.__extbufend_;
339 }
340 __ebs_ = __rhs.__ebs_;
341 __intbuf_ = __rhs.__intbuf_;
342 __ibs_ = __rhs.__ibs_;
343 __file_ = __rhs.__file_;
344 __cv_ = __rhs.__cv_;
345 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000346 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347 __om_ = __rhs.__om_;
348 __cm_ = __rhs.__cm_;
349 __owns_eb_ = __rhs.__owns_eb_;
350 __owns_ib_ = __rhs.__owns_ib_;
351 __always_noconv_ = __rhs.__always_noconv_;
352 if (__rhs.pbase())
353 {
354 if (__rhs.pbase() == __rhs.__intbuf_)
355 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
356 else
357 this->setp((char_type*)__extbuf_,
358 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000359 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360 }
361 else if (__rhs.eback())
362 {
363 if (__rhs.eback() == __rhs.__intbuf_)
364 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
365 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
366 else
367 this->setg((char_type*)__extbuf_,
368 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
369 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
370 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500371 __rhs.__extbuf_ = nullptr;
372 __rhs.__extbufnext_ = nullptr;
373 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 __rhs.__ebs_ = 0;
375 __rhs.__intbuf_ = 0;
376 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500377 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000379 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380 __rhs.__om_ = 0;
381 __rhs.__cm_ = 0;
382 __rhs.__owns_eb_ = false;
383 __rhs.__owns_ib_ = false;
384 __rhs.setg(0, 0, 0);
385 __rhs.setp(0, 0);
386}
387
388template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000389inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390basic_filebuf<_CharT, _Traits>&
391basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
392{
393 close();
394 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000395 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396}
397
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398template <class _CharT, class _Traits>
399basic_filebuf<_CharT, _Traits>::~basic_filebuf()
400{
401#ifndef _LIBCPP_NO_EXCEPTIONS
402 try
403 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 close();
406#ifndef _LIBCPP_NO_EXCEPTIONS
407 }
408 catch (...)
409 {
410 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400411#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 if (__owns_eb_)
413 delete [] __extbuf_;
414 if (__owns_ib_)
415 delete [] __intbuf_;
416}
417
418template <class _CharT, class _Traits>
419void
420basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
421{
422 basic_streambuf<char_type, traits_type>::swap(__rhs);
423 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
424 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200425 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
426 std::swap(__extbuf_, __rhs.__extbuf_);
427 std::swap(__extbufnext_, __rhs.__extbufnext_);
428 std::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 }
430 else
431 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200432 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
433 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
434 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
435 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
437 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200438 // *this uses the small buffer, but __rhs doesn't.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 __extbuf_ = __rhs.__extbuf_;
440 __rhs.__extbuf_ = __rhs.__extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200441 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 }
443 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
444 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200445 // *this doesn't use the small buffer, but __rhs does.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446 __rhs.__extbuf_ = __extbuf_;
447 __extbuf_ = __extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200448 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
449 }
450 else
451 {
452 // Both *this and __rhs use the small buffer.
453 char __tmp[sizeof(__extbuf_min_)];
454 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
455 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
456 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457 }
458 __extbufnext_ = __extbuf_ + __rn;
459 __extbufend_ = __extbuf_ + __re;
460 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
461 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
462 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000463 _VSTD::swap(__ebs_, __rhs.__ebs_);
464 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
465 _VSTD::swap(__ibs_, __rhs.__ibs_);
466 _VSTD::swap(__file_, __rhs.__file_);
467 _VSTD::swap(__cv_, __rhs.__cv_);
468 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000469 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000470 _VSTD::swap(__om_, __rhs.__om_);
471 _VSTD::swap(__cm_, __rhs.__cm_);
472 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
473 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
474 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
476 {
477 ptrdiff_t __n = this->gptr() - this->eback();
478 ptrdiff_t __e = this->egptr() - this->eback();
479 this->setg((char_type*)__extbuf_min_,
480 (char_type*)__extbuf_min_ + __n,
481 (char_type*)__extbuf_min_ + __e);
482 }
483 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
484 {
485 ptrdiff_t __n = this->pptr() - this->pbase();
486 ptrdiff_t __e = this->epptr() - this->pbase();
487 this->setp((char_type*)__extbuf_min_,
488 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000489 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490 }
491 if (__rhs.eback() == (char_type*)__extbuf_min_)
492 {
493 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
494 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
495 __rhs.setg((char_type*)__rhs.__extbuf_min_,
496 (char_type*)__rhs.__extbuf_min_ + __n,
497 (char_type*)__rhs.__extbuf_min_ + __e);
498 }
499 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
500 {
501 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
502 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
503 __rhs.setp((char_type*)__rhs.__extbuf_min_,
504 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000505 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506 }
507}
508
509template <class _CharT, class _Traits>
510inline _LIBCPP_INLINE_VISIBILITY
511void
512swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
513{
514 __x.swap(__y);
515}
516
517template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000518inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519bool
520basic_filebuf<_CharT, _Traits>::is_open() const
521{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500522 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523}
524
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000525template <class _CharT, class _Traits>
526const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
527 ios_base::openmode __mode) _NOEXCEPT {
528 switch (__mode & ~ios_base::ate) {
529 case ios_base::out:
530 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000531 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000532 case ios_base::out | ios_base::app:
533 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000534 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000535 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000536 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000537 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000538 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000539 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000540 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000541 case ios_base::in | ios_base::out | ios_base::app:
542 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000543 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000544 case ios_base::out | ios_base::binary:
545 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000546 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000547 case ios_base::out | ios_base::app | ios_base::binary:
548 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000549 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000550 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000551 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000552 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000553 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000554 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000555 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000556 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
557 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000558 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000559 default:
560 return nullptr;
561 }
Nikolas Klausercfe21472022-02-14 18:26:02 +0100562 __libcpp_unreachable();
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000563}
564
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565template <class _CharT, class _Traits>
566basic_filebuf<_CharT, _Traits>*
567basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
568{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500569 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
570 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000572 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000574 __file_ = fopen(__s, __mdstr);
575 if (__file_) {
576 __om_ = __mode;
577 if (__mode & ios_base::ate) {
578 if (fseek(__file_, 0, SEEK_END)) {
579 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500580 __file_ = nullptr;
581 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000583 }
584 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500585 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000586 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 }
588 return __rt;
589}
590
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000591template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000592inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400593basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000594basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500595 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
596 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000597 if (const char* __mdstr = __make_mdstring(__mode)) {
598 __rt = this;
599 __file_ = fdopen(__fd, __mdstr);
600 if (__file_) {
601 __om_ = __mode;
602 if (__mode & ios_base::ate) {
603 if (fseek(__file_, 0, SEEK_END)) {
604 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500605 __file_ = nullptr;
606 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000607 }
608 }
609 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500610 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000611 }
612 }
613 return __rt;
614}
615
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000616#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
617// This is basically the same as the char* overload except that it uses _wfopen
618// and long mode strings.
619template <class _CharT, class _Traits>
620basic_filebuf<_CharT, _Traits>*
621basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
622{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500623 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
624 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000625 {
626 __rt = this;
627 const wchar_t* __mdstr;
628 switch (__mode & ~ios_base::ate)
629 {
630 case ios_base::out:
631 case ios_base::out | ios_base::trunc:
632 __mdstr = L"w";
633 break;
634 case ios_base::out | ios_base::app:
635 case ios_base::app:
636 __mdstr = L"a";
637 break;
638 case ios_base::in:
639 __mdstr = L"r";
640 break;
641 case ios_base::in | ios_base::out:
642 __mdstr = L"r+";
643 break;
644 case ios_base::in | ios_base::out | ios_base::trunc:
645 __mdstr = L"w+";
646 break;
647 case ios_base::in | ios_base::out | ios_base::app:
648 case ios_base::in | ios_base::app:
649 __mdstr = L"a+";
650 break;
651 case ios_base::out | ios_base::binary:
652 case ios_base::out | ios_base::trunc | ios_base::binary:
653 __mdstr = L"wb";
654 break;
655 case ios_base::out | ios_base::app | ios_base::binary:
656 case ios_base::app | ios_base::binary:
657 __mdstr = L"ab";
658 break;
659 case ios_base::in | ios_base::binary:
660 __mdstr = L"rb";
661 break;
662 case ios_base::in | ios_base::out | ios_base::binary:
663 __mdstr = L"r+b";
664 break;
665 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
666 __mdstr = L"w+b";
667 break;
668 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
669 case ios_base::in | ios_base::app | ios_base::binary:
670 __mdstr = L"a+b";
671 break;
672 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500673 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000674 break;
675 }
676 if (__rt)
677 {
678 __file_ = _wfopen(__s, __mdstr);
679 if (__file_)
680 {
681 __om_ = __mode;
682 if (__mode & ios_base::ate)
683 {
684 if (fseek(__file_, 0, SEEK_END))
685 {
686 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500687 __file_ = nullptr;
688 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000689 }
690 }
691 }
692 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500693 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000694 }
695 }
696 return __rt;
697}
698#endif
699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000701inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702basic_filebuf<_CharT, _Traits>*
703basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
704{
705 return open(__s.c_str(), __mode);
706}
707
708template <class _CharT, class _Traits>
709basic_filebuf<_CharT, _Traits>*
710basic_filebuf<_CharT, _Traits>::close()
711{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500712 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 if (__file_)
714 {
715 __rt = this;
716 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000717 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500718 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000719 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500720 __rt = nullptr;
721 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000722 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 }
724 return __rt;
725}
726
727template <class _CharT, class _Traits>
728typename basic_filebuf<_CharT, _Traits>::int_type
729basic_filebuf<_CharT, _Traits>::underflow()
730{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500731 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 return traits_type::eof();
733 bool __initial = __read_mode();
734 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500735 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 this->setg(&__1buf, &__1buf+1, &__1buf+1);
737 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
738 int_type __c = traits_type::eof();
739 if (this->gptr() == this->egptr())
740 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500741 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 if (__always_noconv_)
743 {
744 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
745 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
746 if (__nmemb != 0)
747 {
748 this->setg(this->eback(),
749 this->eback() + __unget_sz,
750 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000751 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 }
753 }
754 else
755 {
Nikolas Klauser5d2d7392022-11-10 23:57:43 +0100756 if (__extbufend_ != __extbufnext_) {
757 _LIBCPP_ASSERT(__extbufnext_ != nullptr, "underflow moving from NULL" );
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500758 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Nikolas Klauser5d2d7392022-11-10 23:57:43 +0100759 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
761 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000762 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 static_cast<size_t>(__extbufend_ - __extbufnext_));
764 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000765 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000766 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 if (__nr != 0)
768 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000769 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000770 __throw_bad_cast();
771
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 __extbufend_ = __extbufnext_ + __nr;
773 char_type* __inext;
774 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
775 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000776 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 if (__r == codecvt_base::noconv)
778 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000779 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000780 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000781 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782 }
783 else if (__inext != this->eback() + __unget_sz)
784 {
785 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000786 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 }
788 }
789 }
790 }
791 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000792 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500794 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795 return __c;
796}
797
798template <class _CharT, class _Traits>
799typename basic_filebuf<_CharT, _Traits>::int_type
800basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
801{
802 if (__file_ && this->eback() < this->gptr())
803 {
804 if (traits_type::eq_int_type(__c, traits_type::eof()))
805 {
806 this->gbump(-1);
807 return traits_type::not_eof(__c);
808 }
809 if ((__om_ & ios_base::out) ||
810 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
811 {
812 this->gbump(-1);
813 *this->gptr() = traits_type::to_char_type(__c);
814 return __c;
815 }
816 }
817 return traits_type::eof();
818}
819
820template <class _CharT, class _Traits>
821typename basic_filebuf<_CharT, _Traits>::int_type
822basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
823{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500824 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 return traits_type::eof();
826 __write_mode();
827 char_type __1buf;
828 char_type* __pb_save = this->pbase();
829 char_type* __epb_save = this->epptr();
830 if (!traits_type::eq_int_type(__c, traits_type::eof()))
831 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500832 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833 this->setp(&__1buf, &__1buf+1);
834 *this->pptr() = traits_type::to_char_type(__c);
835 this->pbump(1);
836 }
837 if (this->pptr() != this->pbase())
838 {
839 if (__always_noconv_)
840 {
841 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
842 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
843 return traits_type::eof();
844 }
845 else
846 {
847 char* __extbe = __extbuf_;
848 codecvt_base::result __r;
849 do
850 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000851 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000852 __throw_bad_cast();
853
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 const char_type* __e;
855 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
856 __extbuf_, __extbuf_ + __ebs_, __extbe);
857 if (__e == this->pbase())
858 return traits_type::eof();
859 if (__r == codecvt_base::noconv)
860 {
861 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
862 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
863 return traits_type::eof();
864 }
865 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
866 {
867 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
868 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
869 return traits_type::eof();
870 if (__r == codecvt_base::partial)
871 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000872 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000873 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 }
875 }
876 else
877 return traits_type::eof();
878 } while (__r == codecvt_base::partial);
879 }
880 this->setp(__pb_save, __epb_save);
881 }
882 return traits_type::not_eof(__c);
883}
884
885template <class _CharT, class _Traits>
886basic_streambuf<_CharT, _Traits>*
887basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
888{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500889 this->setg(nullptr, nullptr, nullptr);
890 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 if (__owns_eb_)
892 delete [] __extbuf_;
893 if (__owns_ib_)
894 delete [] __intbuf_;
895 __ebs_ = __n;
896 if (__ebs_ > sizeof(__extbuf_min_))
897 {
898 if (__always_noconv_ && __s)
899 {
900 __extbuf_ = (char*)__s;
901 __owns_eb_ = false;
902 }
903 else
904 {
905 __extbuf_ = new char[__ebs_];
906 __owns_eb_ = true;
907 }
908 }
909 else
910 {
911 __extbuf_ = __extbuf_min_;
912 __ebs_ = sizeof(__extbuf_min_);
913 __owns_eb_ = false;
914 }
915 if (!__always_noconv_)
916 {
917 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
918 if (__s && __ibs_ >= sizeof(__extbuf_min_))
919 {
920 __intbuf_ = __s;
921 __owns_ib_ = false;
922 }
923 else
924 {
925 __intbuf_ = new char_type[__ibs_];
926 __owns_ib_ = true;
927 }
928 }
929 else
930 {
931 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500932 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 __owns_ib_ = false;
934 }
935 return this;
936}
937
938template <class _CharT, class _Traits>
939typename basic_filebuf<_CharT, _Traits>::pos_type
940basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
941 ios_base::openmode)
942{
Howard Hinnant0090b122012-08-24 16:52:47 +0000943 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000944 __throw_bad_cast();
945
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500947 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 return pos_type(off_type(-1));
949 // __width > 0 || __off == 0
950 int __whence;
951 switch (__way)
952 {
953 case ios_base::beg:
954 __whence = SEEK_SET;
955 break;
956 case ios_base::cur:
957 __whence = SEEK_CUR;
958 break;
959 case ios_base::end:
960 __whence = SEEK_END;
961 break;
962 default:
963 return pos_type(off_type(-1));
964 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000965#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000966 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
967 return pos_type(off_type(-1));
968 pos_type __r = ftell(__file_);
969#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
971 return pos_type(off_type(-1));
972 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000973#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 __r.state(__st_);
975 return __r;
976}
977
978template <class _CharT, class _Traits>
979typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000980basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500982 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000984#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000985 if (fseek(__file_, __sp, SEEK_SET))
986 return pos_type(off_type(-1));
987#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 if (fseeko(__file_, __sp, SEEK_SET))
989 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000990#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000991 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 return __sp;
993}
994
995template <class _CharT, class _Traits>
996int
997basic_filebuf<_CharT, _Traits>::sync()
998{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500999 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +00001001 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +00001002 __throw_bad_cast();
1003
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 if (__cm_ & ios_base::out)
1005 {
1006 if (this->pptr() != this->pbase())
1007 if (overflow() == traits_type::eof())
1008 return -1;
1009 codecvt_base::result __r;
1010 do
1011 {
1012 char* __extbe;
1013 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1014 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1015 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1016 return -1;
1017 } while (__r == codecvt_base::partial);
1018 if (__r == codecvt_base::error)
1019 return -1;
1020 if (fflush(__file_))
1021 return -1;
1022 }
1023 else if (__cm_ & ios_base::in)
1024 {
1025 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001026 state_type __state = __st_last_;
1027 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 if (__always_noconv_)
1029 __c = this->egptr() - this->gptr();
1030 else
1031 {
1032 int __width = __cv_->encoding();
1033 __c = __extbufend_ - __extbufnext_;
1034 if (__width > 0)
1035 __c += __width * (this->egptr() - this->gptr());
1036 else
1037 {
1038 if (this->gptr() != this->egptr())
1039 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001040 const int __off = __cv_->length(__state, __extbuf_,
1041 __extbufnext_,
1042 this->gptr() - this->eback());
1043 __c += __extbufnext_ - __extbuf_ - __off;
1044 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 }
1046 }
1047 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001048#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001049 if (fseek(__file_, -__c, SEEK_CUR))
1050 return -1;
1051#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 if (fseeko(__file_, -__c, SEEK_CUR))
1053 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001054#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001055 if (__update_st)
1056 __st_ = __state;
1057 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001058 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 __cm_ = 0;
1060 }
1061 return 0;
1062}
1063
1064template <class _CharT, class _Traits>
1065void
1066basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1067{
1068 sync();
1069 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1070 bool __old_anc = __always_noconv_;
1071 __always_noconv_ = __cv_->always_noconv();
1072 if (__old_anc != __always_noconv_)
1073 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001074 this->setg(nullptr, nullptr, nullptr);
1075 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 // invariant, char_type is char, else we couldn't get here
1077 if (__always_noconv_) // need to dump __intbuf_
1078 {
1079 if (__owns_eb_)
1080 delete [] __extbuf_;
1081 __owns_eb_ = __owns_ib_;
1082 __ebs_ = __ibs_;
1083 __extbuf_ = (char*)__intbuf_;
1084 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001085 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 __owns_ib_ = false;
1087 }
1088 else // need to obtain an __intbuf_.
1089 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1090 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1091 {
1092 __ibs_ = __ebs_;
1093 __intbuf_ = (char_type*)__extbuf_;
1094 __owns_ib_ = false;
1095 __extbuf_ = new char[__ebs_];
1096 __owns_eb_ = true;
1097 }
1098 else
1099 {
1100 __ibs_ = __ebs_;
1101 __intbuf_ = new char_type[__ibs_];
1102 __owns_ib_ = true;
1103 }
1104 }
1105 }
1106}
1107
1108template <class _CharT, class _Traits>
1109bool
1110basic_filebuf<_CharT, _Traits>::__read_mode()
1111{
1112 if (!(__cm_ & ios_base::in))
1113 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001114 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 if (__always_noconv_)
1116 this->setg((char_type*)__extbuf_,
1117 (char_type*)__extbuf_ + __ebs_,
1118 (char_type*)__extbuf_ + __ebs_);
1119 else
1120 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1121 __cm_ = ios_base::in;
1122 return true;
1123 }
1124 return false;
1125}
1126
1127template <class _CharT, class _Traits>
1128void
1129basic_filebuf<_CharT, _Traits>::__write_mode()
1130{
1131 if (!(__cm_ & ios_base::out))
1132 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001133 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 if (__ebs_ > sizeof(__extbuf_min_))
1135 {
1136 if (__always_noconv_)
1137 this->setp((char_type*)__extbuf_,
1138 (char_type*)__extbuf_ + (__ebs_ - 1));
1139 else
1140 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1141 }
1142 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001143 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 __cm_ = ios_base::out;
1145 }
1146}
1147
1148// basic_ifstream
1149
1150template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001151class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 : public basic_istream<_CharT, _Traits>
1153{
1154public:
1155 typedef _CharT char_type;
1156 typedef _Traits traits_type;
1157 typedef typename traits_type::int_type int_type;
1158 typedef typename traits_type::pos_type pos_type;
1159 typedef typename traits_type::off_type off_type;
1160
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001165#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1166 _LIBCPP_INLINE_VISIBILITY
1167 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1168#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001171#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001172 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001173 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1174 : basic_ifstream(__p.c_str(), __mode) {}
1175#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 void swap(basic_ifstream& __rhs);
1182
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 bool is_open() const;
1187 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001188#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1189 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1190#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001192#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001193 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001194 void open(const filesystem::path& __p,
1195 ios_base::openmode __mode = ios_base::in) {
1196 return open(__p.c_str(), __mode);
1197 }
1198#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001199
1200 _LIBCPP_INLINE_VISIBILITY
1201 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 void close();
1204
1205private:
1206 basic_filebuf<char_type, traits_type> __sb_;
1207};
1208
1209template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001210inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211basic_ifstream<_CharT, _Traits>::basic_ifstream()
1212 : basic_istream<char_type, traits_type>(&__sb_)
1213{
1214}
1215
1216template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1219 : basic_istream<char_type, traits_type>(&__sb_)
1220{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001221 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 this->setstate(ios_base::failbit);
1223}
1224
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001225#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1226template <class _CharT, class _Traits>
1227inline
1228basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1229 : basic_istream<char_type, traits_type>(&__sb_)
1230{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001231 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001232 this->setstate(ios_base::failbit);
1233}
1234#endif
1235
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001237inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1239 : basic_istream<char_type, traits_type>(&__sb_)
1240{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001241 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242 this->setstate(ios_base::failbit);
1243}
1244
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001246inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001248 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1249 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250{
1251 this->set_rdbuf(&__sb_);
1252}
1253
1254template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001255inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256basic_ifstream<_CharT, _Traits>&
1257basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1258{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001259 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1260 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 return *this;
1262}
1263
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001265inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266void
1267basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1268{
1269 basic_istream<char_type, traits_type>::swap(__rhs);
1270 __sb_.swap(__rhs.__sb_);
1271}
1272
1273template <class _CharT, class _Traits>
1274inline _LIBCPP_INLINE_VISIBILITY
1275void
1276swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1277{
1278 __x.swap(__y);
1279}
1280
1281template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001282inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283basic_filebuf<_CharT, _Traits>*
1284basic_ifstream<_CharT, _Traits>::rdbuf() const
1285{
1286 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1287}
1288
1289template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001290inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291bool
1292basic_ifstream<_CharT, _Traits>::is_open() const
1293{
1294 return __sb_.is_open();
1295}
1296
1297template <class _CharT, class _Traits>
1298void
1299basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1300{
1301 if (__sb_.open(__s, __mode | ios_base::in))
1302 this->clear();
1303 else
1304 this->setstate(ios_base::failbit);
1305}
1306
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001307#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1308template <class _CharT, class _Traits>
1309void
1310basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1311{
1312 if (__sb_.open(__s, __mode | ios_base::in))
1313 this->clear();
1314 else
1315 this->setstate(ios_base::failbit);
1316}
1317#endif
1318
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319template <class _CharT, class _Traits>
1320void
1321basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1322{
1323 if (__sb_.open(__s, __mode | ios_base::in))
1324 this->clear();
1325 else
1326 this->setstate(ios_base::failbit);
1327}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001328
1329template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001330inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001331void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1332 ios_base::openmode __mode) {
1333 if (__sb_.__open(__fd, __mode | ios_base::in))
1334 this->clear();
1335 else
1336 this->setstate(ios_base::failbit);
1337}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
1339template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001340inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341void
1342basic_ifstream<_CharT, _Traits>::close()
1343{
1344 if (__sb_.close() == 0)
1345 this->setstate(ios_base::failbit);
1346}
1347
1348// basic_ofstream
1349
1350template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001351class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 : public basic_ostream<_CharT, _Traits>
1353{
1354public:
1355 typedef _CharT char_type;
1356 typedef _Traits traits_type;
1357 typedef typename traits_type::int_type int_type;
1358 typedef typename traits_type::pos_type pos_type;
1359 typedef typename traits_type::off_type off_type;
1360
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001365#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1366 _LIBCPP_INLINE_VISIBILITY
1367 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1368#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001371
Louis Dionnedb84e122021-01-18 12:18:18 -05001372#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001373 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001374 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1375 : basic_ofstream(__p.c_str(), __mode) {}
1376#endif // _LIBCPP_STD_VER >= 17
1377
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 void swap(basic_ofstream& __rhs);
1384
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 bool is_open() const;
1389 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001390#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1391 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001394
Louis Dionnedb84e122021-01-18 12:18:18 -05001395#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001396 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001397 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1398 { return open(__p.c_str(), __mode); }
1399#endif // _LIBCPP_STD_VER >= 17
1400
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001401 _LIBCPP_INLINE_VISIBILITY
1402 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404 void close();
1405
1406private:
1407 basic_filebuf<char_type, traits_type> __sb_;
1408};
1409
1410template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412basic_ofstream<_CharT, _Traits>::basic_ofstream()
1413 : basic_ostream<char_type, traits_type>(&__sb_)
1414{
1415}
1416
1417template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001418inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1420 : basic_ostream<char_type, traits_type>(&__sb_)
1421{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001422 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423 this->setstate(ios_base::failbit);
1424}
1425
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001426#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1427template <class _CharT, class _Traits>
1428inline
1429basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1430 : basic_ostream<char_type, traits_type>(&__sb_)
1431{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001432 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001433 this->setstate(ios_base::failbit);
1434}
1435#endif
1436
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001438inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1440 : basic_ostream<char_type, traits_type>(&__sb_)
1441{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001442 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 this->setstate(ios_base::failbit);
1444}
1445
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001447inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001449 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1450 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451{
1452 this->set_rdbuf(&__sb_);
1453}
1454
1455template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001456inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457basic_ofstream<_CharT, _Traits>&
1458basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1459{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001460 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1461 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462 return *this;
1463}
1464
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001466inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467void
1468basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1469{
1470 basic_ostream<char_type, traits_type>::swap(__rhs);
1471 __sb_.swap(__rhs.__sb_);
1472}
1473
1474template <class _CharT, class _Traits>
1475inline _LIBCPP_INLINE_VISIBILITY
1476void
1477swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1478{
1479 __x.swap(__y);
1480}
1481
1482template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001483inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484basic_filebuf<_CharT, _Traits>*
1485basic_ofstream<_CharT, _Traits>::rdbuf() const
1486{
1487 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1488}
1489
1490template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001491inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492bool
1493basic_ofstream<_CharT, _Traits>::is_open() const
1494{
1495 return __sb_.is_open();
1496}
1497
1498template <class _CharT, class _Traits>
1499void
1500basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1501{
1502 if (__sb_.open(__s, __mode | ios_base::out))
1503 this->clear();
1504 else
1505 this->setstate(ios_base::failbit);
1506}
1507
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001508#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1509template <class _CharT, class _Traits>
1510void
1511basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1512{
1513 if (__sb_.open(__s, __mode | ios_base::out))
1514 this->clear();
1515 else
1516 this->setstate(ios_base::failbit);
1517}
1518#endif
1519
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520template <class _CharT, class _Traits>
1521void
1522basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1523{
1524 if (__sb_.open(__s, __mode | ios_base::out))
1525 this->clear();
1526 else
1527 this->setstate(ios_base::failbit);
1528}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001529
1530template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001531inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001532void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1533 ios_base::openmode __mode) {
1534 if (__sb_.__open(__fd, __mode | ios_base::out))
1535 this->clear();
1536 else
1537 this->setstate(ios_base::failbit);
1538}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539
1540template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001541inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542void
1543basic_ofstream<_CharT, _Traits>::close()
1544{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001545 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 this->setstate(ios_base::failbit);
1547}
1548
1549// basic_fstream
1550
1551template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001552class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 : public basic_iostream<_CharT, _Traits>
1554{
1555public:
1556 typedef _CharT char_type;
1557 typedef _Traits traits_type;
1558 typedef typename traits_type::int_type int_type;
1559 typedef typename traits_type::pos_type pos_type;
1560 typedef typename traits_type::off_type off_type;
1561
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001566#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1567 _LIBCPP_INLINE_VISIBILITY
1568 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1569#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001572
Louis Dionnedb84e122021-01-18 12:18:18 -05001573#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001574 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001575 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1576 : basic_fstream(__p.c_str(), __mode) {}
1577#endif // _LIBCPP_STD_VER >= 17
1578
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001584
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 void swap(basic_fstream& __rhs);
1587
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 bool is_open() const;
1592 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001593#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1594 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1595#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001597
Louis Dionnedb84e122021-01-18 12:18:18 -05001598#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001599 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001600 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1601 { return open(__p.c_str(), __mode); }
1602#endif // _LIBCPP_STD_VER >= 17
1603
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 void close();
1606
1607private:
1608 basic_filebuf<char_type, traits_type> __sb_;
1609};
1610
1611template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001612inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613basic_fstream<_CharT, _Traits>::basic_fstream()
1614 : basic_iostream<char_type, traits_type>(&__sb_)
1615{
1616}
1617
1618template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001619inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1621 : basic_iostream<char_type, traits_type>(&__sb_)
1622{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001623 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 this->setstate(ios_base::failbit);
1625}
1626
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001627#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1628template <class _CharT, class _Traits>
1629inline
1630basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1631 : basic_iostream<char_type, traits_type>(&__sb_)
1632{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001633 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001634 this->setstate(ios_base::failbit);
1635}
1636#endif
1637
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001639inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1641 : basic_iostream<char_type, traits_type>(&__sb_)
1642{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001643 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 this->setstate(ios_base::failbit);
1645}
1646
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001648inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001650 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1651 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652{
1653 this->set_rdbuf(&__sb_);
1654}
1655
1656template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001657inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658basic_fstream<_CharT, _Traits>&
1659basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1660{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001661 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1662 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663 return *this;
1664}
1665
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668void
1669basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1670{
1671 basic_iostream<char_type, traits_type>::swap(__rhs);
1672 __sb_.swap(__rhs.__sb_);
1673}
1674
1675template <class _CharT, class _Traits>
1676inline _LIBCPP_INLINE_VISIBILITY
1677void
1678swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1679{
1680 __x.swap(__y);
1681}
1682
1683template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001684inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685basic_filebuf<_CharT, _Traits>*
1686basic_fstream<_CharT, _Traits>::rdbuf() const
1687{
1688 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1689}
1690
1691template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001692inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693bool
1694basic_fstream<_CharT, _Traits>::is_open() const
1695{
1696 return __sb_.is_open();
1697}
1698
1699template <class _CharT, class _Traits>
1700void
1701basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1702{
1703 if (__sb_.open(__s, __mode))
1704 this->clear();
1705 else
1706 this->setstate(ios_base::failbit);
1707}
1708
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001709#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1710template <class _CharT, class _Traits>
1711void
1712basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1713{
1714 if (__sb_.open(__s, __mode))
1715 this->clear();
1716 else
1717 this->setstate(ios_base::failbit);
1718}
1719#endif
1720
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721template <class _CharT, class _Traits>
1722void
1723basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1724{
1725 if (__sb_.open(__s, __mode))
1726 this->clear();
1727 else
1728 this->setstate(ios_base::failbit);
1729}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730
1731template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001732inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733void
1734basic_fstream<_CharT, _Traits>::close()
1735{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001736 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 this->setstate(ios_base::failbit);
1738}
1739
Louis Dionne8a79be62020-10-21 11:11:45 -04001740#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
Louis Dionnedc496ec2021-06-08 17:25:08 -04001741extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1742extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1743extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
Louis Dionne8a79be62020-10-21 11:11:45 -04001744#endif
1745
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746_LIBCPP_END_NAMESPACE_STD
1747
Louis Dionne3f3f7532022-11-18 15:01:33 -05001748#endif // _LIBCPP_HAS_NO_FSTREAM
1749
Eric Fiselierf4433a32017-05-31 22:07:49 +00001750_LIBCPP_POP_MACROS
1751
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001752#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
Nikolas Klauserd858e752022-09-22 21:53:13 +02001753# include <atomic>
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001754# include <concepts>
Nikolas Klauserd858e752022-09-22 21:53:13 +02001755# include <iosfwd>
1756# include <limits>
1757# include <new>
1758# include <stdexcept>
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001759#endif
1760
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001761#endif // _LIBCPP_FSTREAM