blob: 95f345fae4d78c91a5aa79b140a09cec6ac05a97 [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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213_LIBCPP_BEGIN_NAMESPACE_STD
214
215template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000216class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 : public basic_streambuf<_CharT, _Traits>
218{
219public:
220 typedef _CharT char_type;
221 typedef _Traits traits_type;
222 typedef typename traits_type::int_type int_type;
223 typedef typename traits_type::pos_type pos_type;
224 typedef typename traits_type::off_type off_type;
225 typedef typename traits_type::state_type state_type;
226
227 // 27.9.1.2 Constructors/destructor:
228 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 basic_filebuf(basic_filebuf&& __rhs);
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200230 ~basic_filebuf() override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231
232 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 void swap(basic_filebuf& __rhs);
236
237 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 bool is_open() const;
240 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000241#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
242 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
243#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000246
Louis Dionnedb84e122021-01-18 12:18:18 -0500247#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000248 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000249 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
250 return open(__p.c_str(), __mode);
251 }
252#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000254 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255 basic_filebuf* close();
256
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000257 _LIBCPP_INLINE_VISIBILITY
258 inline static const char*
259 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
260
261 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 // 27.9.1.5 Overridden virtual functions:
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200263 int_type underflow() override;
264 int_type pbackfail(int_type __c = traits_type::eof()) override;
265 int_type overflow (int_type __c = traits_type::eof()) override;
266 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
267 pos_type seekoff(off_type __off, ios_base::seekdir __way,
268 ios_base::openmode __wch = ios_base::in | ios_base::out) override;
269 pos_type seekpos(pos_type __sp,
270 ios_base::openmode __wch = ios_base::in | ios_base::out) override;
271 int sync() override;
272 void imbue(const locale& __loc) override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
274private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000275 char* __extbuf_;
276 const char* __extbufnext_;
277 const char* __extbufend_;
278 char __extbuf_min_[8];
279 size_t __ebs_;
280 char_type* __intbuf_;
281 size_t __ibs_;
282 FILE* __file_;
283 const codecvt<char_type, char, state_type>* __cv_;
284 state_type __st_;
285 state_type __st_last_;
286 ios_base::openmode __om_;
287 ios_base::openmode __cm_;
288 bool __owns_eb_;
289 bool __owns_ib_;
290 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000292 bool __read_mode();
293 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294};
295
296template <class _CharT, class _Traits>
297basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500298 : __extbuf_(nullptr),
299 __extbufnext_(nullptr),
300 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500302 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500304 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000305 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000307 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308 __om_(0),
309 __cm_(0),
310 __owns_eb_(false),
311 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000312 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313{
Howard Hinnant0090b122012-08-24 16:52:47 +0000314 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
315 {
316 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
317 __always_noconv_ = __cv_->always_noconv();
318 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500319 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320}
321
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322template <class _CharT, class _Traits>
323basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
324 : basic_streambuf<_CharT, _Traits>(__rhs)
325{
326 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
327 {
328 __extbuf_ = __extbuf_min_;
329 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
330 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
331 }
332 else
333 {
334 __extbuf_ = __rhs.__extbuf_;
335 __extbufnext_ = __rhs.__extbufnext_;
336 __extbufend_ = __rhs.__extbufend_;
337 }
338 __ebs_ = __rhs.__ebs_;
339 __intbuf_ = __rhs.__intbuf_;
340 __ibs_ = __rhs.__ibs_;
341 __file_ = __rhs.__file_;
342 __cv_ = __rhs.__cv_;
343 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000344 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 __om_ = __rhs.__om_;
346 __cm_ = __rhs.__cm_;
347 __owns_eb_ = __rhs.__owns_eb_;
348 __owns_ib_ = __rhs.__owns_ib_;
349 __always_noconv_ = __rhs.__always_noconv_;
350 if (__rhs.pbase())
351 {
352 if (__rhs.pbase() == __rhs.__intbuf_)
353 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
354 else
355 this->setp((char_type*)__extbuf_,
356 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000357 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 }
359 else if (__rhs.eback())
360 {
361 if (__rhs.eback() == __rhs.__intbuf_)
362 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
363 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
364 else
365 this->setg((char_type*)__extbuf_,
366 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
367 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
368 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500369 __rhs.__extbuf_ = nullptr;
370 __rhs.__extbufnext_ = nullptr;
371 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__ebs_ = 0;
373 __rhs.__intbuf_ = 0;
374 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500375 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000377 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 __rhs.__om_ = 0;
379 __rhs.__cm_ = 0;
380 __rhs.__owns_eb_ = false;
381 __rhs.__owns_ib_ = false;
382 __rhs.setg(0, 0, 0);
383 __rhs.setp(0, 0);
384}
385
386template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388basic_filebuf<_CharT, _Traits>&
389basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
390{
391 close();
392 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000393 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394}
395
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396template <class _CharT, class _Traits>
397basic_filebuf<_CharT, _Traits>::~basic_filebuf()
398{
399#ifndef _LIBCPP_NO_EXCEPTIONS
400 try
401 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400402#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 close();
404#ifndef _LIBCPP_NO_EXCEPTIONS
405 }
406 catch (...)
407 {
408 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400409#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 if (__owns_eb_)
411 delete [] __extbuf_;
412 if (__owns_ib_)
413 delete [] __intbuf_;
414}
415
416template <class _CharT, class _Traits>
417void
418basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
419{
420 basic_streambuf<char_type, traits_type>::swap(__rhs);
421 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
422 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200423 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
424 std::swap(__extbuf_, __rhs.__extbuf_);
425 std::swap(__extbufnext_, __rhs.__extbufnext_);
426 std::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 }
428 else
429 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200430 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
431 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
432 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
433 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
435 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200436 // *this uses the small buffer, but __rhs doesn't.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437 __extbuf_ = __rhs.__extbuf_;
438 __rhs.__extbuf_ = __rhs.__extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200439 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440 }
441 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
442 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200443 // *this doesn't use the small buffer, but __rhs does.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 __rhs.__extbuf_ = __extbuf_;
445 __extbuf_ = __extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200446 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
447 }
448 else
449 {
450 // Both *this and __rhs use the small buffer.
451 char __tmp[sizeof(__extbuf_min_)];
452 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
453 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
454 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455 }
456 __extbufnext_ = __extbuf_ + __rn;
457 __extbufend_ = __extbuf_ + __re;
458 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
459 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
460 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000461 _VSTD::swap(__ebs_, __rhs.__ebs_);
462 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
463 _VSTD::swap(__ibs_, __rhs.__ibs_);
464 _VSTD::swap(__file_, __rhs.__file_);
465 _VSTD::swap(__cv_, __rhs.__cv_);
466 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000467 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000468 _VSTD::swap(__om_, __rhs.__om_);
469 _VSTD::swap(__cm_, __rhs.__cm_);
470 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
471 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
472 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
474 {
475 ptrdiff_t __n = this->gptr() - this->eback();
476 ptrdiff_t __e = this->egptr() - this->eback();
477 this->setg((char_type*)__extbuf_min_,
478 (char_type*)__extbuf_min_ + __n,
479 (char_type*)__extbuf_min_ + __e);
480 }
481 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
482 {
483 ptrdiff_t __n = this->pptr() - this->pbase();
484 ptrdiff_t __e = this->epptr() - this->pbase();
485 this->setp((char_type*)__extbuf_min_,
486 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000487 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488 }
489 if (__rhs.eback() == (char_type*)__extbuf_min_)
490 {
491 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
492 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
493 __rhs.setg((char_type*)__rhs.__extbuf_min_,
494 (char_type*)__rhs.__extbuf_min_ + __n,
495 (char_type*)__rhs.__extbuf_min_ + __e);
496 }
497 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
498 {
499 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
500 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
501 __rhs.setp((char_type*)__rhs.__extbuf_min_,
502 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000503 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 }
505}
506
507template <class _CharT, class _Traits>
508inline _LIBCPP_INLINE_VISIBILITY
509void
510swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
511{
512 __x.swap(__y);
513}
514
515template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000516inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517bool
518basic_filebuf<_CharT, _Traits>::is_open() const
519{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500520 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521}
522
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000523template <class _CharT, class _Traits>
524const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
525 ios_base::openmode __mode) _NOEXCEPT {
526 switch (__mode & ~ios_base::ate) {
527 case ios_base::out:
528 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000529 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000530 case ios_base::out | ios_base::app:
531 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000532 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000533 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000534 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000535 case ios_base::in | ios_base::out:
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 | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000538 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000539 case ios_base::in | ios_base::out | ios_base::app:
540 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000541 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000542 case ios_base::out | ios_base::binary:
543 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000544 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000545 case ios_base::out | ios_base::app | ios_base::binary:
546 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000547 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000548 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000549 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000550 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000551 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000552 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000553 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000554 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
555 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000556 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000557 default:
558 return nullptr;
559 }
Nikolas Klausercfe21472022-02-14 18:26:02 +0100560 __libcpp_unreachable();
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000561}
562
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563template <class _CharT, class _Traits>
564basic_filebuf<_CharT, _Traits>*
565basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
566{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500567 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
568 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000570 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000572 __file_ = fopen(__s, __mdstr);
573 if (__file_) {
574 __om_ = __mode;
575 if (__mode & ios_base::ate) {
576 if (fseek(__file_, 0, SEEK_END)) {
577 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500578 __file_ = nullptr;
579 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000581 }
582 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500583 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000584 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585 }
586 return __rt;
587}
588
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000589template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000590inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400591basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000592basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500593 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
594 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000595 if (const char* __mdstr = __make_mdstring(__mode)) {
596 __rt = this;
597 __file_ = fdopen(__fd, __mdstr);
598 if (__file_) {
599 __om_ = __mode;
600 if (__mode & ios_base::ate) {
601 if (fseek(__file_, 0, SEEK_END)) {
602 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500603 __file_ = nullptr;
604 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000605 }
606 }
607 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500608 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000609 }
610 }
611 return __rt;
612}
613
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000614#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
615// This is basically the same as the char* overload except that it uses _wfopen
616// and long mode strings.
617template <class _CharT, class _Traits>
618basic_filebuf<_CharT, _Traits>*
619basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
620{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500621 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
622 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000623 {
624 __rt = this;
625 const wchar_t* __mdstr;
626 switch (__mode & ~ios_base::ate)
627 {
628 case ios_base::out:
629 case ios_base::out | ios_base::trunc:
630 __mdstr = L"w";
631 break;
632 case ios_base::out | ios_base::app:
633 case ios_base::app:
634 __mdstr = L"a";
635 break;
636 case ios_base::in:
637 __mdstr = L"r";
638 break;
639 case ios_base::in | ios_base::out:
640 __mdstr = L"r+";
641 break;
642 case ios_base::in | ios_base::out | ios_base::trunc:
643 __mdstr = L"w+";
644 break;
645 case ios_base::in | ios_base::out | ios_base::app:
646 case ios_base::in | ios_base::app:
647 __mdstr = L"a+";
648 break;
649 case ios_base::out | ios_base::binary:
650 case ios_base::out | ios_base::trunc | ios_base::binary:
651 __mdstr = L"wb";
652 break;
653 case ios_base::out | ios_base::app | ios_base::binary:
654 case ios_base::app | ios_base::binary:
655 __mdstr = L"ab";
656 break;
657 case ios_base::in | ios_base::binary:
658 __mdstr = L"rb";
659 break;
660 case ios_base::in | ios_base::out | ios_base::binary:
661 __mdstr = L"r+b";
662 break;
663 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
664 __mdstr = L"w+b";
665 break;
666 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
667 case ios_base::in | ios_base::app | ios_base::binary:
668 __mdstr = L"a+b";
669 break;
670 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500671 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000672 break;
673 }
674 if (__rt)
675 {
676 __file_ = _wfopen(__s, __mdstr);
677 if (__file_)
678 {
679 __om_ = __mode;
680 if (__mode & ios_base::ate)
681 {
682 if (fseek(__file_, 0, SEEK_END))
683 {
684 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500685 __file_ = nullptr;
686 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000687 }
688 }
689 }
690 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500691 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000692 }
693 }
694 return __rt;
695}
696#endif
697
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700basic_filebuf<_CharT, _Traits>*
701basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
702{
703 return open(__s.c_str(), __mode);
704}
705
706template <class _CharT, class _Traits>
707basic_filebuf<_CharT, _Traits>*
708basic_filebuf<_CharT, _Traits>::close()
709{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500710 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 if (__file_)
712 {
713 __rt = this;
714 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000715 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500716 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000717 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500718 __rt = nullptr;
719 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000720 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 }
722 return __rt;
723}
724
725template <class _CharT, class _Traits>
726typename basic_filebuf<_CharT, _Traits>::int_type
727basic_filebuf<_CharT, _Traits>::underflow()
728{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500729 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730 return traits_type::eof();
731 bool __initial = __read_mode();
732 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500733 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 this->setg(&__1buf, &__1buf+1, &__1buf+1);
735 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
736 int_type __c = traits_type::eof();
737 if (this->gptr() == this->egptr())
738 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500739 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 if (__always_noconv_)
741 {
742 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
743 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
744 if (__nmemb != 0)
745 {
746 this->setg(this->eback(),
747 this->eback() + __unget_sz,
748 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000749 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 }
751 }
752 else
753 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000754 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
755 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500756 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
758 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000759 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 static_cast<size_t>(__extbufend_ - __extbufnext_));
761 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000762 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000763 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 if (__nr != 0)
765 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000766 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000767 __throw_bad_cast();
768
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 __extbufend_ = __extbufnext_ + __nr;
770 char_type* __inext;
771 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
772 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000773 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 if (__r == codecvt_base::noconv)
775 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000776 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000777 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000778 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 }
780 else if (__inext != this->eback() + __unget_sz)
781 {
782 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000783 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784 }
785 }
786 }
787 }
788 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000789 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500791 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 return __c;
793}
794
795template <class _CharT, class _Traits>
796typename basic_filebuf<_CharT, _Traits>::int_type
797basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
798{
799 if (__file_ && this->eback() < this->gptr())
800 {
801 if (traits_type::eq_int_type(__c, traits_type::eof()))
802 {
803 this->gbump(-1);
804 return traits_type::not_eof(__c);
805 }
806 if ((__om_ & ios_base::out) ||
807 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
808 {
809 this->gbump(-1);
810 *this->gptr() = traits_type::to_char_type(__c);
811 return __c;
812 }
813 }
814 return traits_type::eof();
815}
816
817template <class _CharT, class _Traits>
818typename basic_filebuf<_CharT, _Traits>::int_type
819basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
820{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500821 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 return traits_type::eof();
823 __write_mode();
824 char_type __1buf;
825 char_type* __pb_save = this->pbase();
826 char_type* __epb_save = this->epptr();
827 if (!traits_type::eq_int_type(__c, traits_type::eof()))
828 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500829 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 this->setp(&__1buf, &__1buf+1);
831 *this->pptr() = traits_type::to_char_type(__c);
832 this->pbump(1);
833 }
834 if (this->pptr() != this->pbase())
835 {
836 if (__always_noconv_)
837 {
838 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
839 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
840 return traits_type::eof();
841 }
842 else
843 {
844 char* __extbe = __extbuf_;
845 codecvt_base::result __r;
846 do
847 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000848 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000849 __throw_bad_cast();
850
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851 const char_type* __e;
852 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
853 __extbuf_, __extbuf_ + __ebs_, __extbe);
854 if (__e == this->pbase())
855 return traits_type::eof();
856 if (__r == codecvt_base::noconv)
857 {
858 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
859 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
860 return traits_type::eof();
861 }
862 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
863 {
864 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
865 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
866 return traits_type::eof();
867 if (__r == codecvt_base::partial)
868 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000869 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000870 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 }
872 }
873 else
874 return traits_type::eof();
875 } while (__r == codecvt_base::partial);
876 }
877 this->setp(__pb_save, __epb_save);
878 }
879 return traits_type::not_eof(__c);
880}
881
882template <class _CharT, class _Traits>
883basic_streambuf<_CharT, _Traits>*
884basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
885{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500886 this->setg(nullptr, nullptr, nullptr);
887 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 if (__owns_eb_)
889 delete [] __extbuf_;
890 if (__owns_ib_)
891 delete [] __intbuf_;
892 __ebs_ = __n;
893 if (__ebs_ > sizeof(__extbuf_min_))
894 {
895 if (__always_noconv_ && __s)
896 {
897 __extbuf_ = (char*)__s;
898 __owns_eb_ = false;
899 }
900 else
901 {
902 __extbuf_ = new char[__ebs_];
903 __owns_eb_ = true;
904 }
905 }
906 else
907 {
908 __extbuf_ = __extbuf_min_;
909 __ebs_ = sizeof(__extbuf_min_);
910 __owns_eb_ = false;
911 }
912 if (!__always_noconv_)
913 {
914 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
915 if (__s && __ibs_ >= sizeof(__extbuf_min_))
916 {
917 __intbuf_ = __s;
918 __owns_ib_ = false;
919 }
920 else
921 {
922 __intbuf_ = new char_type[__ibs_];
923 __owns_ib_ = true;
924 }
925 }
926 else
927 {
928 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500929 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930 __owns_ib_ = false;
931 }
932 return this;
933}
934
935template <class _CharT, class _Traits>
936typename basic_filebuf<_CharT, _Traits>::pos_type
937basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
938 ios_base::openmode)
939{
Howard Hinnant0090b122012-08-24 16:52:47 +0000940 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000941 __throw_bad_cast();
942
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500944 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 return pos_type(off_type(-1));
946 // __width > 0 || __off == 0
947 int __whence;
948 switch (__way)
949 {
950 case ios_base::beg:
951 __whence = SEEK_SET;
952 break;
953 case ios_base::cur:
954 __whence = SEEK_CUR;
955 break;
956 case ios_base::end:
957 __whence = SEEK_END;
958 break;
959 default:
960 return pos_type(off_type(-1));
961 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000962#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000963 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
964 return pos_type(off_type(-1));
965 pos_type __r = ftell(__file_);
966#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
968 return pos_type(off_type(-1));
969 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000970#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 __r.state(__st_);
972 return __r;
973}
974
975template <class _CharT, class _Traits>
976typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000977basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500979 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000981#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000982 if (fseek(__file_, __sp, SEEK_SET))
983 return pos_type(off_type(-1));
984#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 if (fseeko(__file_, __sp, SEEK_SET))
986 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000987#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000988 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 return __sp;
990}
991
992template <class _CharT, class _Traits>
993int
994basic_filebuf<_CharT, _Traits>::sync()
995{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500996 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000998 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000999 __throw_bad_cast();
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 if (__cm_ & ios_base::out)
1002 {
1003 if (this->pptr() != this->pbase())
1004 if (overflow() == traits_type::eof())
1005 return -1;
1006 codecvt_base::result __r;
1007 do
1008 {
1009 char* __extbe;
1010 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1011 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1012 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1013 return -1;
1014 } while (__r == codecvt_base::partial);
1015 if (__r == codecvt_base::error)
1016 return -1;
1017 if (fflush(__file_))
1018 return -1;
1019 }
1020 else if (__cm_ & ios_base::in)
1021 {
1022 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001023 state_type __state = __st_last_;
1024 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 if (__always_noconv_)
1026 __c = this->egptr() - this->gptr();
1027 else
1028 {
1029 int __width = __cv_->encoding();
1030 __c = __extbufend_ - __extbufnext_;
1031 if (__width > 0)
1032 __c += __width * (this->egptr() - this->gptr());
1033 else
1034 {
1035 if (this->gptr() != this->egptr())
1036 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001037 const int __off = __cv_->length(__state, __extbuf_,
1038 __extbufnext_,
1039 this->gptr() - this->eback());
1040 __c += __extbufnext_ - __extbuf_ - __off;
1041 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 }
1043 }
1044 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001045#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001046 if (fseek(__file_, -__c, SEEK_CUR))
1047 return -1;
1048#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 if (fseeko(__file_, -__c, SEEK_CUR))
1050 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001051#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001052 if (__update_st)
1053 __st_ = __state;
1054 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001055 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 __cm_ = 0;
1057 }
1058 return 0;
1059}
1060
1061template <class _CharT, class _Traits>
1062void
1063basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1064{
1065 sync();
1066 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1067 bool __old_anc = __always_noconv_;
1068 __always_noconv_ = __cv_->always_noconv();
1069 if (__old_anc != __always_noconv_)
1070 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001071 this->setg(nullptr, nullptr, nullptr);
1072 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 // invariant, char_type is char, else we couldn't get here
1074 if (__always_noconv_) // need to dump __intbuf_
1075 {
1076 if (__owns_eb_)
1077 delete [] __extbuf_;
1078 __owns_eb_ = __owns_ib_;
1079 __ebs_ = __ibs_;
1080 __extbuf_ = (char*)__intbuf_;
1081 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001082 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 __owns_ib_ = false;
1084 }
1085 else // need to obtain an __intbuf_.
1086 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1087 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1088 {
1089 __ibs_ = __ebs_;
1090 __intbuf_ = (char_type*)__extbuf_;
1091 __owns_ib_ = false;
1092 __extbuf_ = new char[__ebs_];
1093 __owns_eb_ = true;
1094 }
1095 else
1096 {
1097 __ibs_ = __ebs_;
1098 __intbuf_ = new char_type[__ibs_];
1099 __owns_ib_ = true;
1100 }
1101 }
1102 }
1103}
1104
1105template <class _CharT, class _Traits>
1106bool
1107basic_filebuf<_CharT, _Traits>::__read_mode()
1108{
1109 if (!(__cm_ & ios_base::in))
1110 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001111 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 if (__always_noconv_)
1113 this->setg((char_type*)__extbuf_,
1114 (char_type*)__extbuf_ + __ebs_,
1115 (char_type*)__extbuf_ + __ebs_);
1116 else
1117 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1118 __cm_ = ios_base::in;
1119 return true;
1120 }
1121 return false;
1122}
1123
1124template <class _CharT, class _Traits>
1125void
1126basic_filebuf<_CharT, _Traits>::__write_mode()
1127{
1128 if (!(__cm_ & ios_base::out))
1129 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001130 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 if (__ebs_ > sizeof(__extbuf_min_))
1132 {
1133 if (__always_noconv_)
1134 this->setp((char_type*)__extbuf_,
1135 (char_type*)__extbuf_ + (__ebs_ - 1));
1136 else
1137 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1138 }
1139 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001140 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 __cm_ = ios_base::out;
1142 }
1143}
1144
1145// basic_ifstream
1146
1147template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001148class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 : public basic_istream<_CharT, _Traits>
1150{
1151public:
1152 typedef _CharT char_type;
1153 typedef _Traits traits_type;
1154 typedef typename traits_type::int_type int_type;
1155 typedef typename traits_type::pos_type pos_type;
1156 typedef typename traits_type::off_type off_type;
1157
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001162#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1163 _LIBCPP_INLINE_VISIBILITY
1164 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1165#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001168#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001169 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001170 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1171 : basic_ifstream(__p.c_str(), __mode) {}
1172#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 void swap(basic_ifstream& __rhs);
1179
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 bool is_open() const;
1184 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001185#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1186 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001189#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001190 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001191 void open(const filesystem::path& __p,
1192 ios_base::openmode __mode = ios_base::in) {
1193 return open(__p.c_str(), __mode);
1194 }
1195#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001196
1197 _LIBCPP_INLINE_VISIBILITY
1198 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 void close();
1201
1202private:
1203 basic_filebuf<char_type, traits_type> __sb_;
1204};
1205
1206template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208basic_ifstream<_CharT, _Traits>::basic_ifstream()
1209 : basic_istream<char_type, traits_type>(&__sb_)
1210{
1211}
1212
1213template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1216 : basic_istream<char_type, traits_type>(&__sb_)
1217{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001218 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 this->setstate(ios_base::failbit);
1220}
1221
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001222#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1223template <class _CharT, class _Traits>
1224inline
1225basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1226 : basic_istream<char_type, traits_type>(&__sb_)
1227{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001228 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001229 this->setstate(ios_base::failbit);
1230}
1231#endif
1232
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001234inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1236 : basic_istream<char_type, traits_type>(&__sb_)
1237{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001238 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 this->setstate(ios_base::failbit);
1240}
1241
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001243inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001245 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1246 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247{
1248 this->set_rdbuf(&__sb_);
1249}
1250
1251template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001252inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253basic_ifstream<_CharT, _Traits>&
1254basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1255{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001256 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1257 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 return *this;
1259}
1260
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001262inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263void
1264basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1265{
1266 basic_istream<char_type, traits_type>::swap(__rhs);
1267 __sb_.swap(__rhs.__sb_);
1268}
1269
1270template <class _CharT, class _Traits>
1271inline _LIBCPP_INLINE_VISIBILITY
1272void
1273swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1274{
1275 __x.swap(__y);
1276}
1277
1278template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001279inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280basic_filebuf<_CharT, _Traits>*
1281basic_ifstream<_CharT, _Traits>::rdbuf() const
1282{
1283 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1284}
1285
1286template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001287inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288bool
1289basic_ifstream<_CharT, _Traits>::is_open() const
1290{
1291 return __sb_.is_open();
1292}
1293
1294template <class _CharT, class _Traits>
1295void
1296basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1297{
1298 if (__sb_.open(__s, __mode | ios_base::in))
1299 this->clear();
1300 else
1301 this->setstate(ios_base::failbit);
1302}
1303
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001304#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1305template <class _CharT, class _Traits>
1306void
1307basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1308{
1309 if (__sb_.open(__s, __mode | ios_base::in))
1310 this->clear();
1311 else
1312 this->setstate(ios_base::failbit);
1313}
1314#endif
1315
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316template <class _CharT, class _Traits>
1317void
1318basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1319{
1320 if (__sb_.open(__s, __mode | ios_base::in))
1321 this->clear();
1322 else
1323 this->setstate(ios_base::failbit);
1324}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001325
1326template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001327inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001328void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1329 ios_base::openmode __mode) {
1330 if (__sb_.__open(__fd, __mode | ios_base::in))
1331 this->clear();
1332 else
1333 this->setstate(ios_base::failbit);
1334}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
1336template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001337inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338void
1339basic_ifstream<_CharT, _Traits>::close()
1340{
1341 if (__sb_.close() == 0)
1342 this->setstate(ios_base::failbit);
1343}
1344
1345// basic_ofstream
1346
1347template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001348class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 : public basic_ostream<_CharT, _Traits>
1350{
1351public:
1352 typedef _CharT char_type;
1353 typedef _Traits traits_type;
1354 typedef typename traits_type::int_type int_type;
1355 typedef typename traits_type::pos_type pos_type;
1356 typedef typename traits_type::off_type off_type;
1357
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001362#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1363 _LIBCPP_INLINE_VISIBILITY
1364 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1365#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001368
Louis Dionnedb84e122021-01-18 12:18:18 -05001369#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001370 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001371 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1372 : basic_ofstream(__p.c_str(), __mode) {}
1373#endif // _LIBCPP_STD_VER >= 17
1374
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 void swap(basic_ofstream& __rhs);
1381
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 bool is_open() const;
1386 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001387#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1388 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1389#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001391
Louis Dionnedb84e122021-01-18 12:18:18 -05001392#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001393 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001394 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1395 { return open(__p.c_str(), __mode); }
1396#endif // _LIBCPP_STD_VER >= 17
1397
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001398 _LIBCPP_INLINE_VISIBILITY
1399 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 void close();
1402
1403private:
1404 basic_filebuf<char_type, traits_type> __sb_;
1405};
1406
1407template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001408inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409basic_ofstream<_CharT, _Traits>::basic_ofstream()
1410 : basic_ostream<char_type, traits_type>(&__sb_)
1411{
1412}
1413
1414template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001415inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1417 : basic_ostream<char_type, traits_type>(&__sb_)
1418{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001419 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 this->setstate(ios_base::failbit);
1421}
1422
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001423#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1424template <class _CharT, class _Traits>
1425inline
1426basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1427 : basic_ostream<char_type, traits_type>(&__sb_)
1428{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001429 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001430 this->setstate(ios_base::failbit);
1431}
1432#endif
1433
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1437 : basic_ostream<char_type, traits_type>(&__sb_)
1438{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001439 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 this->setstate(ios_base::failbit);
1441}
1442
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001446 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1447 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448{
1449 this->set_rdbuf(&__sb_);
1450}
1451
1452template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001453inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454basic_ofstream<_CharT, _Traits>&
1455basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1456{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001457 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1458 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 return *this;
1460}
1461
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001463inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464void
1465basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1466{
1467 basic_ostream<char_type, traits_type>::swap(__rhs);
1468 __sb_.swap(__rhs.__sb_);
1469}
1470
1471template <class _CharT, class _Traits>
1472inline _LIBCPP_INLINE_VISIBILITY
1473void
1474swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1475{
1476 __x.swap(__y);
1477}
1478
1479template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001480inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481basic_filebuf<_CharT, _Traits>*
1482basic_ofstream<_CharT, _Traits>::rdbuf() const
1483{
1484 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1485}
1486
1487template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001488inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489bool
1490basic_ofstream<_CharT, _Traits>::is_open() const
1491{
1492 return __sb_.is_open();
1493}
1494
1495template <class _CharT, class _Traits>
1496void
1497basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1498{
1499 if (__sb_.open(__s, __mode | ios_base::out))
1500 this->clear();
1501 else
1502 this->setstate(ios_base::failbit);
1503}
1504
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001505#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1506template <class _CharT, class _Traits>
1507void
1508basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1509{
1510 if (__sb_.open(__s, __mode | ios_base::out))
1511 this->clear();
1512 else
1513 this->setstate(ios_base::failbit);
1514}
1515#endif
1516
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517template <class _CharT, class _Traits>
1518void
1519basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1520{
1521 if (__sb_.open(__s, __mode | ios_base::out))
1522 this->clear();
1523 else
1524 this->setstate(ios_base::failbit);
1525}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001526
1527template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001528inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001529void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1530 ios_base::openmode __mode) {
1531 if (__sb_.__open(__fd, __mode | ios_base::out))
1532 this->clear();
1533 else
1534 this->setstate(ios_base::failbit);
1535}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536
1537template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001538inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539void
1540basic_ofstream<_CharT, _Traits>::close()
1541{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001542 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 this->setstate(ios_base::failbit);
1544}
1545
1546// basic_fstream
1547
1548template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001549class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 : public basic_iostream<_CharT, _Traits>
1551{
1552public:
1553 typedef _CharT char_type;
1554 typedef _Traits traits_type;
1555 typedef typename traits_type::int_type int_type;
1556 typedef typename traits_type::pos_type pos_type;
1557 typedef typename traits_type::off_type off_type;
1558
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001563#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1564 _LIBCPP_INLINE_VISIBILITY
1565 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1566#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001569
Louis Dionnedb84e122021-01-18 12:18:18 -05001570#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001571 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001572 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1573 : basic_fstream(__p.c_str(), __mode) {}
1574#endif // _LIBCPP_STD_VER >= 17
1575
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001581
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 void swap(basic_fstream& __rhs);
1584
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 bool is_open() const;
1589 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001590#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1591 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1592#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001594
Louis Dionnedb84e122021-01-18 12:18:18 -05001595#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001596 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001597 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1598 { return open(__p.c_str(), __mode); }
1599#endif // _LIBCPP_STD_VER >= 17
1600
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 void close();
1603
1604private:
1605 basic_filebuf<char_type, traits_type> __sb_;
1606};
1607
1608template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001609inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610basic_fstream<_CharT, _Traits>::basic_fstream()
1611 : basic_iostream<char_type, traits_type>(&__sb_)
1612{
1613}
1614
1615template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001616inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1618 : basic_iostream<char_type, traits_type>(&__sb_)
1619{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001620 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 this->setstate(ios_base::failbit);
1622}
1623
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001624#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1625template <class _CharT, class _Traits>
1626inline
1627basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1628 : basic_iostream<char_type, traits_type>(&__sb_)
1629{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001630 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001631 this->setstate(ios_base::failbit);
1632}
1633#endif
1634
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001636inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1638 : basic_iostream<char_type, traits_type>(&__sb_)
1639{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001640 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 this->setstate(ios_base::failbit);
1642}
1643
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001647 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1648 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649{
1650 this->set_rdbuf(&__sb_);
1651}
1652
1653template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001654inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655basic_fstream<_CharT, _Traits>&
1656basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1657{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001658 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1659 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660 return *this;
1661}
1662
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665void
1666basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1667{
1668 basic_iostream<char_type, traits_type>::swap(__rhs);
1669 __sb_.swap(__rhs.__sb_);
1670}
1671
1672template <class _CharT, class _Traits>
1673inline _LIBCPP_INLINE_VISIBILITY
1674void
1675swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1676{
1677 __x.swap(__y);
1678}
1679
1680template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001681inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682basic_filebuf<_CharT, _Traits>*
1683basic_fstream<_CharT, _Traits>::rdbuf() const
1684{
1685 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1686}
1687
1688template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690bool
1691basic_fstream<_CharT, _Traits>::is_open() const
1692{
1693 return __sb_.is_open();
1694}
1695
1696template <class _CharT, class _Traits>
1697void
1698basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1699{
1700 if (__sb_.open(__s, __mode))
1701 this->clear();
1702 else
1703 this->setstate(ios_base::failbit);
1704}
1705
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001706#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1707template <class _CharT, class _Traits>
1708void
1709basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1710{
1711 if (__sb_.open(__s, __mode))
1712 this->clear();
1713 else
1714 this->setstate(ios_base::failbit);
1715}
1716#endif
1717
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718template <class _CharT, class _Traits>
1719void
1720basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1721{
1722 if (__sb_.open(__s, __mode))
1723 this->clear();
1724 else
1725 this->setstate(ios_base::failbit);
1726}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727
1728template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001729inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730void
1731basic_fstream<_CharT, _Traits>::close()
1732{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001733 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 this->setstate(ios_base::failbit);
1735}
1736
Louis Dionne8a79be62020-10-21 11:11:45 -04001737#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
Louis Dionnedc496ec2021-06-08 17:25:08 -04001738extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1739extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1740extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
Louis Dionne8a79be62020-10-21 11:11:45 -04001741#endif
1742
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743_LIBCPP_END_NAMESPACE_STD
1744
Eric Fiselierf4433a32017-05-31 22:07:49 +00001745_LIBCPP_POP_MACROS
1746
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001747#endif // _LIBCPP_FSTREAM