blob: b8417f3e6c02d6cb6c71b3824b440e76eb9a04ae [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 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000756 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
757 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500758 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
760 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000761 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 static_cast<size_t>(__extbufend_ - __extbufnext_));
763 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000764 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000765 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 if (__nr != 0)
767 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000768 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000769 __throw_bad_cast();
770
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 __extbufend_ = __extbufnext_ + __nr;
772 char_type* __inext;
773 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
774 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000775 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 if (__r == codecvt_base::noconv)
777 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000778 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000779 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000780 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 }
782 else if (__inext != this->eback() + __unget_sz)
783 {
784 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000785 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 }
787 }
788 }
789 }
790 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000791 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500793 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794 return __c;
795}
796
797template <class _CharT, class _Traits>
798typename basic_filebuf<_CharT, _Traits>::int_type
799basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
800{
801 if (__file_ && this->eback() < this->gptr())
802 {
803 if (traits_type::eq_int_type(__c, traits_type::eof()))
804 {
805 this->gbump(-1);
806 return traits_type::not_eof(__c);
807 }
808 if ((__om_ & ios_base::out) ||
809 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
810 {
811 this->gbump(-1);
812 *this->gptr() = traits_type::to_char_type(__c);
813 return __c;
814 }
815 }
816 return traits_type::eof();
817}
818
819template <class _CharT, class _Traits>
820typename basic_filebuf<_CharT, _Traits>::int_type
821basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
822{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500823 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 return traits_type::eof();
825 __write_mode();
826 char_type __1buf;
827 char_type* __pb_save = this->pbase();
828 char_type* __epb_save = this->epptr();
829 if (!traits_type::eq_int_type(__c, traits_type::eof()))
830 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500831 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 this->setp(&__1buf, &__1buf+1);
833 *this->pptr() = traits_type::to_char_type(__c);
834 this->pbump(1);
835 }
836 if (this->pptr() != this->pbase())
837 {
838 if (__always_noconv_)
839 {
840 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
841 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
842 return traits_type::eof();
843 }
844 else
845 {
846 char* __extbe = __extbuf_;
847 codecvt_base::result __r;
848 do
849 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000850 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000851 __throw_bad_cast();
852
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 const char_type* __e;
854 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
855 __extbuf_, __extbuf_ + __ebs_, __extbe);
856 if (__e == this->pbase())
857 return traits_type::eof();
858 if (__r == codecvt_base::noconv)
859 {
860 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
861 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
862 return traits_type::eof();
863 }
864 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
865 {
866 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
867 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
868 return traits_type::eof();
869 if (__r == codecvt_base::partial)
870 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000871 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000872 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 }
874 }
875 else
876 return traits_type::eof();
877 } while (__r == codecvt_base::partial);
878 }
879 this->setp(__pb_save, __epb_save);
880 }
881 return traits_type::not_eof(__c);
882}
883
884template <class _CharT, class _Traits>
885basic_streambuf<_CharT, _Traits>*
886basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
887{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500888 this->setg(nullptr, nullptr, nullptr);
889 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 if (__owns_eb_)
891 delete [] __extbuf_;
892 if (__owns_ib_)
893 delete [] __intbuf_;
894 __ebs_ = __n;
895 if (__ebs_ > sizeof(__extbuf_min_))
896 {
897 if (__always_noconv_ && __s)
898 {
899 __extbuf_ = (char*)__s;
900 __owns_eb_ = false;
901 }
902 else
903 {
904 __extbuf_ = new char[__ebs_];
905 __owns_eb_ = true;
906 }
907 }
908 else
909 {
910 __extbuf_ = __extbuf_min_;
911 __ebs_ = sizeof(__extbuf_min_);
912 __owns_eb_ = false;
913 }
914 if (!__always_noconv_)
915 {
916 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
917 if (__s && __ibs_ >= sizeof(__extbuf_min_))
918 {
919 __intbuf_ = __s;
920 __owns_ib_ = false;
921 }
922 else
923 {
924 __intbuf_ = new char_type[__ibs_];
925 __owns_ib_ = true;
926 }
927 }
928 else
929 {
930 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500931 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 __owns_ib_ = false;
933 }
934 return this;
935}
936
937template <class _CharT, class _Traits>
938typename basic_filebuf<_CharT, _Traits>::pos_type
939basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
940 ios_base::openmode)
941{
Howard Hinnant0090b122012-08-24 16:52:47 +0000942 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000943 __throw_bad_cast();
944
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500946 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 return pos_type(off_type(-1));
948 // __width > 0 || __off == 0
949 int __whence;
950 switch (__way)
951 {
952 case ios_base::beg:
953 __whence = SEEK_SET;
954 break;
955 case ios_base::cur:
956 __whence = SEEK_CUR;
957 break;
958 case ios_base::end:
959 __whence = SEEK_END;
960 break;
961 default:
962 return pos_type(off_type(-1));
963 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000964#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000965 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
966 return pos_type(off_type(-1));
967 pos_type __r = ftell(__file_);
968#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
970 return pos_type(off_type(-1));
971 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000972#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 __r.state(__st_);
974 return __r;
975}
976
977template <class _CharT, class _Traits>
978typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000979basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500981 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000983#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000984 if (fseek(__file_, __sp, SEEK_SET))
985 return pos_type(off_type(-1));
986#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 if (fseeko(__file_, __sp, SEEK_SET))
988 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000989#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000990 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 return __sp;
992}
993
994template <class _CharT, class _Traits>
995int
996basic_filebuf<_CharT, _Traits>::sync()
997{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500998 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +00001000 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +00001001 __throw_bad_cast();
1002
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 if (__cm_ & ios_base::out)
1004 {
1005 if (this->pptr() != this->pbase())
1006 if (overflow() == traits_type::eof())
1007 return -1;
1008 codecvt_base::result __r;
1009 do
1010 {
1011 char* __extbe;
1012 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1013 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1014 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1015 return -1;
1016 } while (__r == codecvt_base::partial);
1017 if (__r == codecvt_base::error)
1018 return -1;
1019 if (fflush(__file_))
1020 return -1;
1021 }
1022 else if (__cm_ & ios_base::in)
1023 {
1024 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001025 state_type __state = __st_last_;
1026 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 if (__always_noconv_)
1028 __c = this->egptr() - this->gptr();
1029 else
1030 {
1031 int __width = __cv_->encoding();
1032 __c = __extbufend_ - __extbufnext_;
1033 if (__width > 0)
1034 __c += __width * (this->egptr() - this->gptr());
1035 else
1036 {
1037 if (this->gptr() != this->egptr())
1038 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001039 const int __off = __cv_->length(__state, __extbuf_,
1040 __extbufnext_,
1041 this->gptr() - this->eback());
1042 __c += __extbufnext_ - __extbuf_ - __off;
1043 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 }
1045 }
1046 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001047#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001048 if (fseek(__file_, -__c, SEEK_CUR))
1049 return -1;
1050#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 if (fseeko(__file_, -__c, SEEK_CUR))
1052 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001053#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001054 if (__update_st)
1055 __st_ = __state;
1056 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001057 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 __cm_ = 0;
1059 }
1060 return 0;
1061}
1062
1063template <class _CharT, class _Traits>
1064void
1065basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1066{
1067 sync();
1068 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1069 bool __old_anc = __always_noconv_;
1070 __always_noconv_ = __cv_->always_noconv();
1071 if (__old_anc != __always_noconv_)
1072 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001073 this->setg(nullptr, nullptr, nullptr);
1074 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 // invariant, char_type is char, else we couldn't get here
1076 if (__always_noconv_) // need to dump __intbuf_
1077 {
1078 if (__owns_eb_)
1079 delete [] __extbuf_;
1080 __owns_eb_ = __owns_ib_;
1081 __ebs_ = __ibs_;
1082 __extbuf_ = (char*)__intbuf_;
1083 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001084 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 __owns_ib_ = false;
1086 }
1087 else // need to obtain an __intbuf_.
1088 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1089 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1090 {
1091 __ibs_ = __ebs_;
1092 __intbuf_ = (char_type*)__extbuf_;
1093 __owns_ib_ = false;
1094 __extbuf_ = new char[__ebs_];
1095 __owns_eb_ = true;
1096 }
1097 else
1098 {
1099 __ibs_ = __ebs_;
1100 __intbuf_ = new char_type[__ibs_];
1101 __owns_ib_ = true;
1102 }
1103 }
1104 }
1105}
1106
1107template <class _CharT, class _Traits>
1108bool
1109basic_filebuf<_CharT, _Traits>::__read_mode()
1110{
1111 if (!(__cm_ & ios_base::in))
1112 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001113 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 if (__always_noconv_)
1115 this->setg((char_type*)__extbuf_,
1116 (char_type*)__extbuf_ + __ebs_,
1117 (char_type*)__extbuf_ + __ebs_);
1118 else
1119 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1120 __cm_ = ios_base::in;
1121 return true;
1122 }
1123 return false;
1124}
1125
1126template <class _CharT, class _Traits>
1127void
1128basic_filebuf<_CharT, _Traits>::__write_mode()
1129{
1130 if (!(__cm_ & ios_base::out))
1131 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001132 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 if (__ebs_ > sizeof(__extbuf_min_))
1134 {
1135 if (__always_noconv_)
1136 this->setp((char_type*)__extbuf_,
1137 (char_type*)__extbuf_ + (__ebs_ - 1));
1138 else
1139 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1140 }
1141 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001142 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 __cm_ = ios_base::out;
1144 }
1145}
1146
1147// basic_ifstream
1148
1149template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001150class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 : public basic_istream<_CharT, _Traits>
1152{
1153public:
1154 typedef _CharT char_type;
1155 typedef _Traits traits_type;
1156 typedef typename traits_type::int_type int_type;
1157 typedef typename traits_type::pos_type pos_type;
1158 typedef typename traits_type::off_type off_type;
1159
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001164#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1165 _LIBCPP_INLINE_VISIBILITY
1166 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1167#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001170#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001171 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001172 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1173 : basic_ifstream(__p.c_str(), __mode) {}
1174#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 void swap(basic_ifstream& __rhs);
1181
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 bool is_open() const;
1186 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001187#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1188 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1189#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001191#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001192 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001193 void open(const filesystem::path& __p,
1194 ios_base::openmode __mode = ios_base::in) {
1195 return open(__p.c_str(), __mode);
1196 }
1197#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001198
1199 _LIBCPP_INLINE_VISIBILITY
1200 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 void close();
1203
1204private:
1205 basic_filebuf<char_type, traits_type> __sb_;
1206};
1207
1208template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210basic_ifstream<_CharT, _Traits>::basic_ifstream()
1211 : basic_istream<char_type, traits_type>(&__sb_)
1212{
1213}
1214
1215template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1218 : basic_istream<char_type, traits_type>(&__sb_)
1219{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001220 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 this->setstate(ios_base::failbit);
1222}
1223
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001224#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1225template <class _CharT, class _Traits>
1226inline
1227basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1228 : basic_istream<char_type, traits_type>(&__sb_)
1229{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001230 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001231 this->setstate(ios_base::failbit);
1232}
1233#endif
1234
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001236inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1238 : basic_istream<char_type, traits_type>(&__sb_)
1239{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001240 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 this->setstate(ios_base::failbit);
1242}
1243
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001247 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1248 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249{
1250 this->set_rdbuf(&__sb_);
1251}
1252
1253template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001254inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255basic_ifstream<_CharT, _Traits>&
1256basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001258 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1259 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 return *this;
1261}
1262
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001264inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265void
1266basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1267{
1268 basic_istream<char_type, traits_type>::swap(__rhs);
1269 __sb_.swap(__rhs.__sb_);
1270}
1271
1272template <class _CharT, class _Traits>
1273inline _LIBCPP_INLINE_VISIBILITY
1274void
1275swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1276{
1277 __x.swap(__y);
1278}
1279
1280template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001281inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282basic_filebuf<_CharT, _Traits>*
1283basic_ifstream<_CharT, _Traits>::rdbuf() const
1284{
1285 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1286}
1287
1288template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001289inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290bool
1291basic_ifstream<_CharT, _Traits>::is_open() const
1292{
1293 return __sb_.is_open();
1294}
1295
1296template <class _CharT, class _Traits>
1297void
1298basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1299{
1300 if (__sb_.open(__s, __mode | ios_base::in))
1301 this->clear();
1302 else
1303 this->setstate(ios_base::failbit);
1304}
1305
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001306#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1307template <class _CharT, class _Traits>
1308void
1309basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1310{
1311 if (__sb_.open(__s, __mode | ios_base::in))
1312 this->clear();
1313 else
1314 this->setstate(ios_base::failbit);
1315}
1316#endif
1317
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318template <class _CharT, class _Traits>
1319void
1320basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1321{
1322 if (__sb_.open(__s, __mode | ios_base::in))
1323 this->clear();
1324 else
1325 this->setstate(ios_base::failbit);
1326}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001327
1328template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001329inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001330void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1331 ios_base::openmode __mode) {
1332 if (__sb_.__open(__fd, __mode | ios_base::in))
1333 this->clear();
1334 else
1335 this->setstate(ios_base::failbit);
1336}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337
1338template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001339inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340void
1341basic_ifstream<_CharT, _Traits>::close()
1342{
1343 if (__sb_.close() == 0)
1344 this->setstate(ios_base::failbit);
1345}
1346
1347// basic_ofstream
1348
1349template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001350class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 : public basic_ostream<_CharT, _Traits>
1352{
1353public:
1354 typedef _CharT char_type;
1355 typedef _Traits traits_type;
1356 typedef typename traits_type::int_type int_type;
1357 typedef typename traits_type::pos_type pos_type;
1358 typedef typename traits_type::off_type off_type;
1359
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001364#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1365 _LIBCPP_INLINE_VISIBILITY
1366 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1367#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001370
Louis Dionnedb84e122021-01-18 12:18:18 -05001371#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001372 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001373 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1374 : basic_ofstream(__p.c_str(), __mode) {}
1375#endif // _LIBCPP_STD_VER >= 17
1376
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 void swap(basic_ofstream& __rhs);
1383
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 bool is_open() const;
1388 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001389#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1390 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1391#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001393
Louis Dionnedb84e122021-01-18 12:18:18 -05001394#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001395 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001396 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1397 { return open(__p.c_str(), __mode); }
1398#endif // _LIBCPP_STD_VER >= 17
1399
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001400 _LIBCPP_INLINE_VISIBILITY
1401 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 void close();
1404
1405private:
1406 basic_filebuf<char_type, traits_type> __sb_;
1407};
1408
1409template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411basic_ofstream<_CharT, _Traits>::basic_ofstream()
1412 : basic_ostream<char_type, traits_type>(&__sb_)
1413{
1414}
1415
1416template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001417inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1419 : basic_ostream<char_type, traits_type>(&__sb_)
1420{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001421 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 this->setstate(ios_base::failbit);
1423}
1424
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001425#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1426template <class _CharT, class _Traits>
1427inline
1428basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1429 : basic_ostream<char_type, traits_type>(&__sb_)
1430{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001431 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001432 this->setstate(ios_base::failbit);
1433}
1434#endif
1435
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001437inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1439 : basic_ostream<char_type, traits_type>(&__sb_)
1440{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001441 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 this->setstate(ios_base::failbit);
1443}
1444
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001446inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001448 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1449 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450{
1451 this->set_rdbuf(&__sb_);
1452}
1453
1454template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456basic_ofstream<_CharT, _Traits>&
1457basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1458{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001459 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1460 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 return *this;
1462}
1463
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001465inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466void
1467basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1468{
1469 basic_ostream<char_type, traits_type>::swap(__rhs);
1470 __sb_.swap(__rhs.__sb_);
1471}
1472
1473template <class _CharT, class _Traits>
1474inline _LIBCPP_INLINE_VISIBILITY
1475void
1476swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1477{
1478 __x.swap(__y);
1479}
1480
1481template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001482inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483basic_filebuf<_CharT, _Traits>*
1484basic_ofstream<_CharT, _Traits>::rdbuf() const
1485{
1486 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1487}
1488
1489template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001490inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491bool
1492basic_ofstream<_CharT, _Traits>::is_open() const
1493{
1494 return __sb_.is_open();
1495}
1496
1497template <class _CharT, class _Traits>
1498void
1499basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1500{
1501 if (__sb_.open(__s, __mode | ios_base::out))
1502 this->clear();
1503 else
1504 this->setstate(ios_base::failbit);
1505}
1506
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001507#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1508template <class _CharT, class _Traits>
1509void
1510basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1511{
1512 if (__sb_.open(__s, __mode | ios_base::out))
1513 this->clear();
1514 else
1515 this->setstate(ios_base::failbit);
1516}
1517#endif
1518
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519template <class _CharT, class _Traits>
1520void
1521basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1522{
1523 if (__sb_.open(__s, __mode | ios_base::out))
1524 this->clear();
1525 else
1526 this->setstate(ios_base::failbit);
1527}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001528
1529template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001530inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001531void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1532 ios_base::openmode __mode) {
1533 if (__sb_.__open(__fd, __mode | ios_base::out))
1534 this->clear();
1535 else
1536 this->setstate(ios_base::failbit);
1537}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
1539template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001540inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541void
1542basic_ofstream<_CharT, _Traits>::close()
1543{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001544 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 this->setstate(ios_base::failbit);
1546}
1547
1548// basic_fstream
1549
1550template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001551class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 : public basic_iostream<_CharT, _Traits>
1553{
1554public:
1555 typedef _CharT char_type;
1556 typedef _Traits traits_type;
1557 typedef typename traits_type::int_type int_type;
1558 typedef typename traits_type::pos_type pos_type;
1559 typedef typename traits_type::off_type off_type;
1560
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001565#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1566 _LIBCPP_INLINE_VISIBILITY
1567 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1568#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001571
Louis Dionnedb84e122021-01-18 12:18:18 -05001572#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001573 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001574 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1575 : basic_fstream(__p.c_str(), __mode) {}
1576#endif // _LIBCPP_STD_VER >= 17
1577
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001583
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 void swap(basic_fstream& __rhs);
1586
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 bool is_open() const;
1591 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001592#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1593 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1594#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001596
Louis Dionnedb84e122021-01-18 12:18:18 -05001597#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001598 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001599 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1600 { return open(__p.c_str(), __mode); }
1601#endif // _LIBCPP_STD_VER >= 17
1602
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 void close();
1605
1606private:
1607 basic_filebuf<char_type, traits_type> __sb_;
1608};
1609
1610template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001611inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612basic_fstream<_CharT, _Traits>::basic_fstream()
1613 : basic_iostream<char_type, traits_type>(&__sb_)
1614{
1615}
1616
1617template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001618inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1620 : basic_iostream<char_type, traits_type>(&__sb_)
1621{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001622 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623 this->setstate(ios_base::failbit);
1624}
1625
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001626#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1627template <class _CharT, class _Traits>
1628inline
1629basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1630 : basic_iostream<char_type, traits_type>(&__sb_)
1631{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001632 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001633 this->setstate(ios_base::failbit);
1634}
1635#endif
1636
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001638inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1640 : basic_iostream<char_type, traits_type>(&__sb_)
1641{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001642 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643 this->setstate(ios_base::failbit);
1644}
1645
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001647inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001649 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1650 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651{
1652 this->set_rdbuf(&__sb_);
1653}
1654
1655template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001656inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657basic_fstream<_CharT, _Traits>&
1658basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1659{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001660 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1661 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 return *this;
1663}
1664
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001666inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667void
1668basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1669{
1670 basic_iostream<char_type, traits_type>::swap(__rhs);
1671 __sb_.swap(__rhs.__sb_);
1672}
1673
1674template <class _CharT, class _Traits>
1675inline _LIBCPP_INLINE_VISIBILITY
1676void
1677swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1678{
1679 __x.swap(__y);
1680}
1681
1682template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001683inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684basic_filebuf<_CharT, _Traits>*
1685basic_fstream<_CharT, _Traits>::rdbuf() const
1686{
1687 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1688}
1689
1690template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001691inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692bool
1693basic_fstream<_CharT, _Traits>::is_open() const
1694{
1695 return __sb_.is_open();
1696}
1697
1698template <class _CharT, class _Traits>
1699void
1700basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1701{
1702 if (__sb_.open(__s, __mode))
1703 this->clear();
1704 else
1705 this->setstate(ios_base::failbit);
1706}
1707
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001708#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1709template <class _CharT, class _Traits>
1710void
1711basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1712{
1713 if (__sb_.open(__s, __mode))
1714 this->clear();
1715 else
1716 this->setstate(ios_base::failbit);
1717}
1718#endif
1719
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720template <class _CharT, class _Traits>
1721void
1722basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1723{
1724 if (__sb_.open(__s, __mode))
1725 this->clear();
1726 else
1727 this->setstate(ios_base::failbit);
1728}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729
1730template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001731inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732void
1733basic_fstream<_CharT, _Traits>::close()
1734{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001735 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 this->setstate(ios_base::failbit);
1737}
1738
Louis Dionne8a79be62020-10-21 11:11:45 -04001739#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
Louis Dionnedc496ec2021-06-08 17:25:08 -04001740extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1741extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1742extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
Louis Dionne8a79be62020-10-21 11:11:45 -04001743#endif
1744
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745_LIBCPP_END_NAMESPACE_STD
1746
Louis Dionne3f3f7532022-11-18 15:01:33 -05001747#endif // _LIBCPP_HAS_NO_FSTREAM
1748
Eric Fiselierf4433a32017-05-31 22:07:49 +00001749_LIBCPP_POP_MACROS
1750
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001751#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
Nikolas Klauserd858e752022-09-22 21:53:13 +02001752# include <atomic>
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001753# include <concepts>
Nikolas Klauserd858e752022-09-22 21:53:13 +02001754# include <iosfwd>
1755# include <limits>
1756# include <new>
1757# include <stdexcept>
Nikolas Klauser1e4ae5d2022-11-02 20:27:42 +01001758#endif
1759
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001760#endif // _LIBCPP_FSTREAM