blob: ffa75b14eb15f0baa27a1c77cba585deec034bb7 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FSTREAM
11#define _LIBCPP_FSTREAM
12
13/*
14 fstream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_filebuf
18 : public basic_streambuf<charT, traits>
19{
20public:
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
26
27 // 27.9.1.2 Constructors/destructor:
28 basic_filebuf();
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
31
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
35
36 // 27.9.1.4 Members:
37 bool is_open() const;
38 basic_filebuf* open(const char* s, ios_base::openmode mode);
39 basic_filebuf* open(const string& s, ios_base::openmode mode);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000040 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 basic_filebuf* close();
42
43protected:
44 // 27.9.1.5 Overridden virtual functions:
45 virtual streamsize showmanyc();
46 virtual int_type underflow();
47 virtual int_type uflow();
48 virtual int_type pbackfail(int_type c = traits_type::eof());
49 virtual int_type overflow (int_type c = traits_type::eof());
50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52 ios_base::openmode which = ios_base::in | ios_base::out);
53 virtual pos_type seekpos(pos_type sp,
54 ios_base::openmode which = ios_base::in | ios_base::out);
55 virtual int sync();
56 virtual void imbue(const locale& loc);
57};
58
59template <class charT, class traits>
60 void
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63typedef basic_filebuf<char> filebuf;
64typedef basic_filebuf<wchar_t> wfilebuf;
65
66template <class charT, class traits = char_traits<charT> >
67class basic_ifstream
68 : public basic_istream<charT,traits>
69{
70public:
71 typedef charT char_type;
72 typedef traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
76
77 basic_ifstream();
78 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
79 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000080 explicit basic_ifstream(const filesystem::path& p,
81 ios_base::openmode mode = ios_base::in); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 basic_ifstream(basic_ifstream&& rhs);
83
84 basic_ifstream& operator=(basic_ifstream&& rhs);
85 void swap(basic_ifstream& rhs);
86
87 basic_filebuf<char_type, traits_type>* rdbuf() const;
88 bool is_open() const;
89 void open(const char* s, ios_base::openmode mode = ios_base::in);
90 void open(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000091 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 void close();
94};
95
96template <class charT, class traits>
97 void
98 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
99
100typedef basic_ifstream<char> ifstream;
101typedef basic_ifstream<wchar_t> wifstream;
102
103template <class charT, class traits = char_traits<charT> >
104class basic_ofstream
105 : public basic_ostream<charT,traits>
106{
107public:
108 typedef charT char_type;
109 typedef traits traits_type;
110 typedef typename traits_type::int_type int_type;
111 typedef typename traits_type::pos_type pos_type;
112 typedef typename traits_type::off_type off_type;
113
114 basic_ofstream();
115 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
116 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000117 explicit basic_ofstream(const filesystem::path& p,
118 ios_base::openmode mode = ios_base::out); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 basic_ofstream(basic_ofstream&& rhs);
120
121 basic_ofstream& operator=(basic_ofstream&& rhs);
122 void swap(basic_ofstream& rhs);
123
124 basic_filebuf<char_type, traits_type>* rdbuf() const;
125 bool is_open() const;
126 void open(const char* s, ios_base::openmode mode = ios_base::out);
127 void open(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000128 void open(const filesystem::path& p,
129 ios_base::openmode mode = ios_base::out); // C++17
130
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 void close();
132};
133
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000134template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 void
136 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
137
138typedef basic_ofstream<char> ofstream;
139typedef basic_ofstream<wchar_t> wofstream;
140
141template <class charT, class traits=char_traits<charT> >
142class basic_fstream
143 : public basic_iostream<charT,traits>
144{
145public:
146 typedef charT char_type;
147 typedef traits traits_type;
148 typedef typename traits_type::int_type int_type;
149 typedef typename traits_type::pos_type pos_type;
150 typedef typename traits_type::off_type off_type;
151
152 basic_fstream();
153 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000155 explicit basic_fstream(const filesystem::path& p,
156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 basic_fstream(basic_fstream&& rhs);
158
159 basic_fstream& operator=(basic_fstream&& rhs);
160 void swap(basic_fstream& rhs);
161
162 basic_filebuf<char_type, traits_type>* rdbuf() const;
163 bool is_open() const;
164 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
165 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000166 void open(const filesystem::path& s,
167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
168
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 void close();
170};
171
172template <class charT, class traits>
173 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
174
175typedef basic_fstream<char> fstream;
176typedef basic_fstream<wchar_t> wfstream;
177
178} // std
179
180*/
181
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100182#include <__algorithm/max.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400183#include <__assert> // all public C++ headers provide the assertion handler
Louis Dionne73912b22020-11-04 15:01:25 -0500184#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400185#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186#include <__locale>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100187#include <__utility/move.h>
188#include <__utility/swap.h>
Nikolas Klausercfe21472022-02-14 18:26:02 +0100189#include <__utility/unreachable.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000191#include <cstdlib>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100192#include <cstring>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400193#include <istream>
194#include <ostream>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100195#include <version>
Louis Dionnedb84e122021-01-18 12:18:18 -0500196
197#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
198# include <filesystem>
199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500202# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
Eric Fiselierf4433a32017-05-31 22:07:49 +0000205_LIBCPP_PUSH_MACROS
206#include <__undef_macros>
207
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400208#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
209# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
210#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000211
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212_LIBCPP_BEGIN_NAMESPACE_STD
213
214template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000215class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216 : public basic_streambuf<_CharT, _Traits>
217{
218public:
219 typedef _CharT char_type;
220 typedef _Traits traits_type;
221 typedef typename traits_type::int_type int_type;
222 typedef typename traits_type::pos_type pos_type;
223 typedef typename traits_type::off_type off_type;
224 typedef typename traits_type::state_type state_type;
225
226 // 27.9.1.2 Constructors/destructor:
227 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 basic_filebuf(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 virtual ~basic_filebuf();
230
231 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 void swap(basic_filebuf& __rhs);
235
236 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 bool is_open() const;
239 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000240#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
241 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
242#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000245
Louis Dionnedb84e122021-01-18 12:18:18 -0500246#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000247 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000248 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
249 return open(__p.c_str(), __mode);
250 }
251#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000252 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000253 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 basic_filebuf* close();
255
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000256 _LIBCPP_INLINE_VISIBILITY
257 inline static const char*
258 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
259
260 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 // 27.9.1.5 Overridden virtual functions:
262 virtual int_type underflow();
263 virtual int_type pbackfail(int_type __c = traits_type::eof());
264 virtual int_type overflow (int_type __c = traits_type::eof());
265 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
266 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
267 ios_base::openmode __wch = ios_base::in | ios_base::out);
268 virtual pos_type seekpos(pos_type __sp,
269 ios_base::openmode __wch = ios_base::in | ios_base::out);
270 virtual int sync();
271 virtual void imbue(const locale& __loc);
272
273private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000274 char* __extbuf_;
275 const char* __extbufnext_;
276 const char* __extbufend_;
277 char __extbuf_min_[8];
278 size_t __ebs_;
279 char_type* __intbuf_;
280 size_t __ibs_;
281 FILE* __file_;
282 const codecvt<char_type, char, state_type>* __cv_;
283 state_type __st_;
284 state_type __st_last_;
285 ios_base::openmode __om_;
286 ios_base::openmode __cm_;
287 bool __owns_eb_;
288 bool __owns_ib_;
289 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000291 bool __read_mode();
292 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293};
294
295template <class _CharT, class _Traits>
296basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500297 : __extbuf_(nullptr),
298 __extbufnext_(nullptr),
299 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500301 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500303 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000304 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000306 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 __om_(0),
308 __cm_(0),
309 __owns_eb_(false),
310 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000311 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312{
Howard Hinnant0090b122012-08-24 16:52:47 +0000313 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
314 {
315 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
316 __always_noconv_ = __cv_->always_noconv();
317 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500318 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319}
320
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321template <class _CharT, class _Traits>
322basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
323 : basic_streambuf<_CharT, _Traits>(__rhs)
324{
325 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
326 {
327 __extbuf_ = __extbuf_min_;
328 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
329 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
330 }
331 else
332 {
333 __extbuf_ = __rhs.__extbuf_;
334 __extbufnext_ = __rhs.__extbufnext_;
335 __extbufend_ = __rhs.__extbufend_;
336 }
337 __ebs_ = __rhs.__ebs_;
338 __intbuf_ = __rhs.__intbuf_;
339 __ibs_ = __rhs.__ibs_;
340 __file_ = __rhs.__file_;
341 __cv_ = __rhs.__cv_;
342 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000343 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344 __om_ = __rhs.__om_;
345 __cm_ = __rhs.__cm_;
346 __owns_eb_ = __rhs.__owns_eb_;
347 __owns_ib_ = __rhs.__owns_ib_;
348 __always_noconv_ = __rhs.__always_noconv_;
349 if (__rhs.pbase())
350 {
351 if (__rhs.pbase() == __rhs.__intbuf_)
352 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
353 else
354 this->setp((char_type*)__extbuf_,
355 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000356 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 }
358 else if (__rhs.eback())
359 {
360 if (__rhs.eback() == __rhs.__intbuf_)
361 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
362 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
363 else
364 this->setg((char_type*)__extbuf_,
365 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
366 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
367 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500368 __rhs.__extbuf_ = nullptr;
369 __rhs.__extbufnext_ = nullptr;
370 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 __rhs.__ebs_ = 0;
372 __rhs.__intbuf_ = 0;
373 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500374 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000376 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 __rhs.__om_ = 0;
378 __rhs.__cm_ = 0;
379 __rhs.__owns_eb_ = false;
380 __rhs.__owns_ib_ = false;
381 __rhs.setg(0, 0, 0);
382 __rhs.setp(0, 0);
383}
384
385template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000386inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387basic_filebuf<_CharT, _Traits>&
388basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
389{
390 close();
391 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000392 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393}
394
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395template <class _CharT, class _Traits>
396basic_filebuf<_CharT, _Traits>::~basic_filebuf()
397{
398#ifndef _LIBCPP_NO_EXCEPTIONS
399 try
400 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400401#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402 close();
403#ifndef _LIBCPP_NO_EXCEPTIONS
404 }
405 catch (...)
406 {
407 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400408#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 if (__owns_eb_)
410 delete [] __extbuf_;
411 if (__owns_ib_)
412 delete [] __intbuf_;
413}
414
415template <class _CharT, class _Traits>
416void
417basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
418{
419 basic_streambuf<char_type, traits_type>::swap(__rhs);
420 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
421 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200422 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
423 std::swap(__extbuf_, __rhs.__extbuf_);
424 std::swap(__extbufnext_, __rhs.__extbufnext_);
425 std::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426 }
427 else
428 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200429 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
430 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
431 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
432 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
434 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200435 // *this uses the small buffer, but __rhs doesn't.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 __extbuf_ = __rhs.__extbuf_;
437 __rhs.__extbuf_ = __rhs.__extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200438 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 }
440 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
441 {
Fabian Wolffec9098e2022-04-14 16:17:24 +0200442 // *this doesn't use the small buffer, but __rhs does.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 __rhs.__extbuf_ = __extbuf_;
444 __extbuf_ = __extbuf_min_;
Fabian Wolffec9098e2022-04-14 16:17:24 +0200445 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
446 }
447 else
448 {
449 // Both *this and __rhs use the small buffer.
450 char __tmp[sizeof(__extbuf_min_)];
451 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
452 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
453 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 }
455 __extbufnext_ = __extbuf_ + __rn;
456 __extbufend_ = __extbuf_ + __re;
457 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
458 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
459 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000460 _VSTD::swap(__ebs_, __rhs.__ebs_);
461 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
462 _VSTD::swap(__ibs_, __rhs.__ibs_);
463 _VSTD::swap(__file_, __rhs.__file_);
464 _VSTD::swap(__cv_, __rhs.__cv_);
465 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000466 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000467 _VSTD::swap(__om_, __rhs.__om_);
468 _VSTD::swap(__cm_, __rhs.__cm_);
469 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
470 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
471 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
473 {
474 ptrdiff_t __n = this->gptr() - this->eback();
475 ptrdiff_t __e = this->egptr() - this->eback();
476 this->setg((char_type*)__extbuf_min_,
477 (char_type*)__extbuf_min_ + __n,
478 (char_type*)__extbuf_min_ + __e);
479 }
480 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
481 {
482 ptrdiff_t __n = this->pptr() - this->pbase();
483 ptrdiff_t __e = this->epptr() - this->pbase();
484 this->setp((char_type*)__extbuf_min_,
485 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000486 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 }
488 if (__rhs.eback() == (char_type*)__extbuf_min_)
489 {
490 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
491 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
492 __rhs.setg((char_type*)__rhs.__extbuf_min_,
493 (char_type*)__rhs.__extbuf_min_ + __n,
494 (char_type*)__rhs.__extbuf_min_ + __e);
495 }
496 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
497 {
498 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
499 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
500 __rhs.setp((char_type*)__rhs.__extbuf_min_,
501 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000502 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503 }
504}
505
506template <class _CharT, class _Traits>
507inline _LIBCPP_INLINE_VISIBILITY
508void
509swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
510{
511 __x.swap(__y);
512}
513
514template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000515inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516bool
517basic_filebuf<_CharT, _Traits>::is_open() const
518{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500519 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520}
521
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000522template <class _CharT, class _Traits>
523const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
524 ios_base::openmode __mode) _NOEXCEPT {
525 switch (__mode & ~ios_base::ate) {
526 case ios_base::out:
527 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000528 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000529 case ios_base::out | ios_base::app:
530 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000531 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000532 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000535 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000536 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000537 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000538 case ios_base::in | ios_base::out | ios_base::app:
539 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000540 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000541 case ios_base::out | ios_base::binary:
542 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000543 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000544 case ios_base::out | ios_base::app | ios_base::binary:
545 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000546 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000547 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000548 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000549 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000550 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000551 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000552 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000553 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
554 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000555 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000556 default:
557 return nullptr;
558 }
Nikolas Klausercfe21472022-02-14 18:26:02 +0100559 __libcpp_unreachable();
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000560}
561
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562template <class _CharT, class _Traits>
563basic_filebuf<_CharT, _Traits>*
564basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
565{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500566 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
567 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000569 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000571 __file_ = fopen(__s, __mdstr);
572 if (__file_) {
573 __om_ = __mode;
574 if (__mode & ios_base::ate) {
575 if (fseek(__file_, 0, SEEK_END)) {
576 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500577 __file_ = nullptr;
578 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000580 }
581 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500582 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000583 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 }
585 return __rt;
586}
587
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000588template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000589inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400590basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000591basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500592 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
593 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000594 if (const char* __mdstr = __make_mdstring(__mode)) {
595 __rt = this;
596 __file_ = fdopen(__fd, __mdstr);
597 if (__file_) {
598 __om_ = __mode;
599 if (__mode & ios_base::ate) {
600 if (fseek(__file_, 0, SEEK_END)) {
601 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500602 __file_ = nullptr;
603 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000604 }
605 }
606 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500607 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000608 }
609 }
610 return __rt;
611}
612
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000613#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
614// This is basically the same as the char* overload except that it uses _wfopen
615// and long mode strings.
616template <class _CharT, class _Traits>
617basic_filebuf<_CharT, _Traits>*
618basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
619{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500620 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
621 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000622 {
623 __rt = this;
624 const wchar_t* __mdstr;
625 switch (__mode & ~ios_base::ate)
626 {
627 case ios_base::out:
628 case ios_base::out | ios_base::trunc:
629 __mdstr = L"w";
630 break;
631 case ios_base::out | ios_base::app:
632 case ios_base::app:
633 __mdstr = L"a";
634 break;
635 case ios_base::in:
636 __mdstr = L"r";
637 break;
638 case ios_base::in | ios_base::out:
639 __mdstr = L"r+";
640 break;
641 case ios_base::in | ios_base::out | ios_base::trunc:
642 __mdstr = L"w+";
643 break;
644 case ios_base::in | ios_base::out | ios_base::app:
645 case ios_base::in | ios_base::app:
646 __mdstr = L"a+";
647 break;
648 case ios_base::out | ios_base::binary:
649 case ios_base::out | ios_base::trunc | ios_base::binary:
650 __mdstr = L"wb";
651 break;
652 case ios_base::out | ios_base::app | ios_base::binary:
653 case ios_base::app | ios_base::binary:
654 __mdstr = L"ab";
655 break;
656 case ios_base::in | ios_base::binary:
657 __mdstr = L"rb";
658 break;
659 case ios_base::in | ios_base::out | ios_base::binary:
660 __mdstr = L"r+b";
661 break;
662 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
663 __mdstr = L"w+b";
664 break;
665 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
666 case ios_base::in | ios_base::app | ios_base::binary:
667 __mdstr = L"a+b";
668 break;
669 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500670 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000671 break;
672 }
673 if (__rt)
674 {
675 __file_ = _wfopen(__s, __mdstr);
676 if (__file_)
677 {
678 __om_ = __mode;
679 if (__mode & ios_base::ate)
680 {
681 if (fseek(__file_, 0, SEEK_END))
682 {
683 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500684 __file_ = nullptr;
685 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000686 }
687 }
688 }
689 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500690 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000691 }
692 }
693 return __rt;
694}
695#endif
696
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000698inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699basic_filebuf<_CharT, _Traits>*
700basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
701{
702 return open(__s.c_str(), __mode);
703}
704
705template <class _CharT, class _Traits>
706basic_filebuf<_CharT, _Traits>*
707basic_filebuf<_CharT, _Traits>::close()
708{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500709 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710 if (__file_)
711 {
712 __rt = this;
713 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000714 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500715 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000716 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500717 __rt = nullptr;
718 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000719 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 }
721 return __rt;
722}
723
724template <class _CharT, class _Traits>
725typename basic_filebuf<_CharT, _Traits>::int_type
726basic_filebuf<_CharT, _Traits>::underflow()
727{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500728 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729 return traits_type::eof();
730 bool __initial = __read_mode();
731 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500732 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 this->setg(&__1buf, &__1buf+1, &__1buf+1);
734 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
735 int_type __c = traits_type::eof();
736 if (this->gptr() == this->egptr())
737 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500738 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739 if (__always_noconv_)
740 {
741 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
742 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
743 if (__nmemb != 0)
744 {
745 this->setg(this->eback(),
746 this->eback() + __unget_sz,
747 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000748 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 }
750 }
751 else
752 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000753 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
754 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500755 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
757 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000758 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 static_cast<size_t>(__extbufend_ - __extbufnext_));
760 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000761 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000762 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 if (__nr != 0)
764 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000765 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000766 __throw_bad_cast();
767
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 __extbufend_ = __extbufnext_ + __nr;
769 char_type* __inext;
770 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
771 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000772 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 if (__r == codecvt_base::noconv)
774 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000775 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000776 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000777 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 }
779 else if (__inext != this->eback() + __unget_sz)
780 {
781 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000782 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 }
784 }
785 }
786 }
787 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000788 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500790 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791 return __c;
792}
793
794template <class _CharT, class _Traits>
795typename basic_filebuf<_CharT, _Traits>::int_type
796basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
797{
798 if (__file_ && this->eback() < this->gptr())
799 {
800 if (traits_type::eq_int_type(__c, traits_type::eof()))
801 {
802 this->gbump(-1);
803 return traits_type::not_eof(__c);
804 }
805 if ((__om_ & ios_base::out) ||
806 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
807 {
808 this->gbump(-1);
809 *this->gptr() = traits_type::to_char_type(__c);
810 return __c;
811 }
812 }
813 return traits_type::eof();
814}
815
816template <class _CharT, class _Traits>
817typename basic_filebuf<_CharT, _Traits>::int_type
818basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
819{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500820 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 return traits_type::eof();
822 __write_mode();
823 char_type __1buf;
824 char_type* __pb_save = this->pbase();
825 char_type* __epb_save = this->epptr();
826 if (!traits_type::eq_int_type(__c, traits_type::eof()))
827 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500828 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 this->setp(&__1buf, &__1buf+1);
830 *this->pptr() = traits_type::to_char_type(__c);
831 this->pbump(1);
832 }
833 if (this->pptr() != this->pbase())
834 {
835 if (__always_noconv_)
836 {
837 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
838 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
839 return traits_type::eof();
840 }
841 else
842 {
843 char* __extbe = __extbuf_;
844 codecvt_base::result __r;
845 do
846 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000847 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000848 __throw_bad_cast();
849
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 const char_type* __e;
851 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
852 __extbuf_, __extbuf_ + __ebs_, __extbe);
853 if (__e == this->pbase())
854 return traits_type::eof();
855 if (__r == codecvt_base::noconv)
856 {
857 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
858 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
859 return traits_type::eof();
860 }
861 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
862 {
863 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
864 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
865 return traits_type::eof();
866 if (__r == codecvt_base::partial)
867 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000868 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000869 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870 }
871 }
872 else
873 return traits_type::eof();
874 } while (__r == codecvt_base::partial);
875 }
876 this->setp(__pb_save, __epb_save);
877 }
878 return traits_type::not_eof(__c);
879}
880
881template <class _CharT, class _Traits>
882basic_streambuf<_CharT, _Traits>*
883basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
884{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500885 this->setg(nullptr, nullptr, nullptr);
886 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887 if (__owns_eb_)
888 delete [] __extbuf_;
889 if (__owns_ib_)
890 delete [] __intbuf_;
891 __ebs_ = __n;
892 if (__ebs_ > sizeof(__extbuf_min_))
893 {
894 if (__always_noconv_ && __s)
895 {
896 __extbuf_ = (char*)__s;
897 __owns_eb_ = false;
898 }
899 else
900 {
901 __extbuf_ = new char[__ebs_];
902 __owns_eb_ = true;
903 }
904 }
905 else
906 {
907 __extbuf_ = __extbuf_min_;
908 __ebs_ = sizeof(__extbuf_min_);
909 __owns_eb_ = false;
910 }
911 if (!__always_noconv_)
912 {
913 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
914 if (__s && __ibs_ >= sizeof(__extbuf_min_))
915 {
916 __intbuf_ = __s;
917 __owns_ib_ = false;
918 }
919 else
920 {
921 __intbuf_ = new char_type[__ibs_];
922 __owns_ib_ = true;
923 }
924 }
925 else
926 {
927 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500928 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 __owns_ib_ = false;
930 }
931 return this;
932}
933
934template <class _CharT, class _Traits>
935typename basic_filebuf<_CharT, _Traits>::pos_type
936basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
937 ios_base::openmode)
938{
Howard Hinnant0090b122012-08-24 16:52:47 +0000939 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000940 __throw_bad_cast();
941
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500943 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 return pos_type(off_type(-1));
945 // __width > 0 || __off == 0
946 int __whence;
947 switch (__way)
948 {
949 case ios_base::beg:
950 __whence = SEEK_SET;
951 break;
952 case ios_base::cur:
953 __whence = SEEK_CUR;
954 break;
955 case ios_base::end:
956 __whence = SEEK_END;
957 break;
958 default:
959 return pos_type(off_type(-1));
960 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000961#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000962 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
963 return pos_type(off_type(-1));
964 pos_type __r = ftell(__file_);
965#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
967 return pos_type(off_type(-1));
968 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 __r.state(__st_);
971 return __r;
972}
973
974template <class _CharT, class _Traits>
975typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000976basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500978 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000980#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000981 if (fseek(__file_, __sp, SEEK_SET))
982 return pos_type(off_type(-1));
983#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 if (fseeko(__file_, __sp, SEEK_SET))
985 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000986#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000987 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 return __sp;
989}
990
991template <class _CharT, class _Traits>
992int
993basic_filebuf<_CharT, _Traits>::sync()
994{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500995 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000997 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000998 __throw_bad_cast();
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 if (__cm_ & ios_base::out)
1001 {
1002 if (this->pptr() != this->pbase())
1003 if (overflow() == traits_type::eof())
1004 return -1;
1005 codecvt_base::result __r;
1006 do
1007 {
1008 char* __extbe;
1009 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1010 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1011 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1012 return -1;
1013 } while (__r == codecvt_base::partial);
1014 if (__r == codecvt_base::error)
1015 return -1;
1016 if (fflush(__file_))
1017 return -1;
1018 }
1019 else if (__cm_ & ios_base::in)
1020 {
1021 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001022 state_type __state = __st_last_;
1023 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 if (__always_noconv_)
1025 __c = this->egptr() - this->gptr();
1026 else
1027 {
1028 int __width = __cv_->encoding();
1029 __c = __extbufend_ - __extbufnext_;
1030 if (__width > 0)
1031 __c += __width * (this->egptr() - this->gptr());
1032 else
1033 {
1034 if (this->gptr() != this->egptr())
1035 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001036 const int __off = __cv_->length(__state, __extbuf_,
1037 __extbufnext_,
1038 this->gptr() - this->eback());
1039 __c += __extbufnext_ - __extbuf_ - __off;
1040 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 }
1042 }
1043 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001044#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001045 if (fseek(__file_, -__c, SEEK_CUR))
1046 return -1;
1047#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 if (fseeko(__file_, -__c, SEEK_CUR))
1049 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001050#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001051 if (__update_st)
1052 __st_ = __state;
1053 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001054 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 __cm_ = 0;
1056 }
1057 return 0;
1058}
1059
1060template <class _CharT, class _Traits>
1061void
1062basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1063{
1064 sync();
1065 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1066 bool __old_anc = __always_noconv_;
1067 __always_noconv_ = __cv_->always_noconv();
1068 if (__old_anc != __always_noconv_)
1069 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001070 this->setg(nullptr, nullptr, nullptr);
1071 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 // invariant, char_type is char, else we couldn't get here
1073 if (__always_noconv_) // need to dump __intbuf_
1074 {
1075 if (__owns_eb_)
1076 delete [] __extbuf_;
1077 __owns_eb_ = __owns_ib_;
1078 __ebs_ = __ibs_;
1079 __extbuf_ = (char*)__intbuf_;
1080 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001081 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 __owns_ib_ = false;
1083 }
1084 else // need to obtain an __intbuf_.
1085 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1086 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1087 {
1088 __ibs_ = __ebs_;
1089 __intbuf_ = (char_type*)__extbuf_;
1090 __owns_ib_ = false;
1091 __extbuf_ = new char[__ebs_];
1092 __owns_eb_ = true;
1093 }
1094 else
1095 {
1096 __ibs_ = __ebs_;
1097 __intbuf_ = new char_type[__ibs_];
1098 __owns_ib_ = true;
1099 }
1100 }
1101 }
1102}
1103
1104template <class _CharT, class _Traits>
1105bool
1106basic_filebuf<_CharT, _Traits>::__read_mode()
1107{
1108 if (!(__cm_ & ios_base::in))
1109 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001110 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 if (__always_noconv_)
1112 this->setg((char_type*)__extbuf_,
1113 (char_type*)__extbuf_ + __ebs_,
1114 (char_type*)__extbuf_ + __ebs_);
1115 else
1116 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1117 __cm_ = ios_base::in;
1118 return true;
1119 }
1120 return false;
1121}
1122
1123template <class _CharT, class _Traits>
1124void
1125basic_filebuf<_CharT, _Traits>::__write_mode()
1126{
1127 if (!(__cm_ & ios_base::out))
1128 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001129 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 if (__ebs_ > sizeof(__extbuf_min_))
1131 {
1132 if (__always_noconv_)
1133 this->setp((char_type*)__extbuf_,
1134 (char_type*)__extbuf_ + (__ebs_ - 1));
1135 else
1136 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1137 }
1138 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001139 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 __cm_ = ios_base::out;
1141 }
1142}
1143
1144// basic_ifstream
1145
1146template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001147class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 : public basic_istream<_CharT, _Traits>
1149{
1150public:
1151 typedef _CharT char_type;
1152 typedef _Traits traits_type;
1153 typedef typename traits_type::int_type int_type;
1154 typedef typename traits_type::pos_type pos_type;
1155 typedef typename traits_type::off_type off_type;
1156
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001161#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1162 _LIBCPP_INLINE_VISIBILITY
1163 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1164#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001167#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001168 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001169 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1170 : basic_ifstream(__p.c_str(), __mode) {}
1171#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 void swap(basic_ifstream& __rhs);
1178
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 bool is_open() const;
1183 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001184#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1185 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001188#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001189 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001190 void open(const filesystem::path& __p,
1191 ios_base::openmode __mode = ios_base::in) {
1192 return open(__p.c_str(), __mode);
1193 }
1194#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001195
1196 _LIBCPP_INLINE_VISIBILITY
1197 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 void close();
1200
1201private:
1202 basic_filebuf<char_type, traits_type> __sb_;
1203};
1204
1205template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001206inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207basic_ifstream<_CharT, _Traits>::basic_ifstream()
1208 : basic_istream<char_type, traits_type>(&__sb_)
1209{
1210}
1211
1212template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1215 : basic_istream<char_type, traits_type>(&__sb_)
1216{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001217 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 this->setstate(ios_base::failbit);
1219}
1220
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001221#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1222template <class _CharT, class _Traits>
1223inline
1224basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1225 : basic_istream<char_type, traits_type>(&__sb_)
1226{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001227 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001228 this->setstate(ios_base::failbit);
1229}
1230#endif
1231
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1235 : basic_istream<char_type, traits_type>(&__sb_)
1236{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001237 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 this->setstate(ios_base::failbit);
1239}
1240
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001244 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1245 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246{
1247 this->set_rdbuf(&__sb_);
1248}
1249
1250template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252basic_ifstream<_CharT, _Traits>&
1253basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1254{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001255 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1256 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257 return *this;
1258}
1259
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001261inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262void
1263basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1264{
1265 basic_istream<char_type, traits_type>::swap(__rhs);
1266 __sb_.swap(__rhs.__sb_);
1267}
1268
1269template <class _CharT, class _Traits>
1270inline _LIBCPP_INLINE_VISIBILITY
1271void
1272swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1273{
1274 __x.swap(__y);
1275}
1276
1277template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001278inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279basic_filebuf<_CharT, _Traits>*
1280basic_ifstream<_CharT, _Traits>::rdbuf() const
1281{
1282 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1283}
1284
1285template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001286inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287bool
1288basic_ifstream<_CharT, _Traits>::is_open() const
1289{
1290 return __sb_.is_open();
1291}
1292
1293template <class _CharT, class _Traits>
1294void
1295basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1296{
1297 if (__sb_.open(__s, __mode | ios_base::in))
1298 this->clear();
1299 else
1300 this->setstate(ios_base::failbit);
1301}
1302
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001303#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1304template <class _CharT, class _Traits>
1305void
1306basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1307{
1308 if (__sb_.open(__s, __mode | ios_base::in))
1309 this->clear();
1310 else
1311 this->setstate(ios_base::failbit);
1312}
1313#endif
1314
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315template <class _CharT, class _Traits>
1316void
1317basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1318{
1319 if (__sb_.open(__s, __mode | ios_base::in))
1320 this->clear();
1321 else
1322 this->setstate(ios_base::failbit);
1323}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001324
1325template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001326inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001327void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1328 ios_base::openmode __mode) {
1329 if (__sb_.__open(__fd, __mode | ios_base::in))
1330 this->clear();
1331 else
1332 this->setstate(ios_base::failbit);
1333}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
1335template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337void
1338basic_ifstream<_CharT, _Traits>::close()
1339{
1340 if (__sb_.close() == 0)
1341 this->setstate(ios_base::failbit);
1342}
1343
1344// basic_ofstream
1345
1346template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001347class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 : public basic_ostream<_CharT, _Traits>
1349{
1350public:
1351 typedef _CharT char_type;
1352 typedef _Traits traits_type;
1353 typedef typename traits_type::int_type int_type;
1354 typedef typename traits_type::pos_type pos_type;
1355 typedef typename traits_type::off_type off_type;
1356
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001361#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1362 _LIBCPP_INLINE_VISIBILITY
1363 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1364#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001367
Louis Dionnedb84e122021-01-18 12:18:18 -05001368#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001369 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001370 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1371 : basic_ofstream(__p.c_str(), __mode) {}
1372#endif // _LIBCPP_STD_VER >= 17
1373
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 void swap(basic_ofstream& __rhs);
1380
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 bool is_open() const;
1385 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001386#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1387 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1388#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001390
Louis Dionnedb84e122021-01-18 12:18:18 -05001391#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001392 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001393 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1394 { return open(__p.c_str(), __mode); }
1395#endif // _LIBCPP_STD_VER >= 17
1396
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001397 _LIBCPP_INLINE_VISIBILITY
1398 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 void close();
1401
1402private:
1403 basic_filebuf<char_type, traits_type> __sb_;
1404};
1405
1406template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001407inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408basic_ofstream<_CharT, _Traits>::basic_ofstream()
1409 : basic_ostream<char_type, traits_type>(&__sb_)
1410{
1411}
1412
1413template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001414inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1416 : basic_ostream<char_type, traits_type>(&__sb_)
1417{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001418 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419 this->setstate(ios_base::failbit);
1420}
1421
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001422#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1423template <class _CharT, class _Traits>
1424inline
1425basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1426 : basic_ostream<char_type, traits_type>(&__sb_)
1427{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001428 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001429 this->setstate(ios_base::failbit);
1430}
1431#endif
1432
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001434inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1436 : basic_ostream<char_type, traits_type>(&__sb_)
1437{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001438 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 this->setstate(ios_base::failbit);
1440}
1441
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001443inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001445 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1446 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447{
1448 this->set_rdbuf(&__sb_);
1449}
1450
1451template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001452inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453basic_ofstream<_CharT, _Traits>&
1454basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1455{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001456 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1457 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 return *this;
1459}
1460
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001462inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463void
1464basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1465{
1466 basic_ostream<char_type, traits_type>::swap(__rhs);
1467 __sb_.swap(__rhs.__sb_);
1468}
1469
1470template <class _CharT, class _Traits>
1471inline _LIBCPP_INLINE_VISIBILITY
1472void
1473swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1474{
1475 __x.swap(__y);
1476}
1477
1478template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001479inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480basic_filebuf<_CharT, _Traits>*
1481basic_ofstream<_CharT, _Traits>::rdbuf() const
1482{
1483 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1484}
1485
1486template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001487inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488bool
1489basic_ofstream<_CharT, _Traits>::is_open() const
1490{
1491 return __sb_.is_open();
1492}
1493
1494template <class _CharT, class _Traits>
1495void
1496basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1497{
1498 if (__sb_.open(__s, __mode | ios_base::out))
1499 this->clear();
1500 else
1501 this->setstate(ios_base::failbit);
1502}
1503
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001504#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1505template <class _CharT, class _Traits>
1506void
1507basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1508{
1509 if (__sb_.open(__s, __mode | ios_base::out))
1510 this->clear();
1511 else
1512 this->setstate(ios_base::failbit);
1513}
1514#endif
1515
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516template <class _CharT, class _Traits>
1517void
1518basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1519{
1520 if (__sb_.open(__s, __mode | ios_base::out))
1521 this->clear();
1522 else
1523 this->setstate(ios_base::failbit);
1524}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001525
1526template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001527inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001528void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1529 ios_base::openmode __mode) {
1530 if (__sb_.__open(__fd, __mode | ios_base::out))
1531 this->clear();
1532 else
1533 this->setstate(ios_base::failbit);
1534}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535
1536template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001537inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538void
1539basic_ofstream<_CharT, _Traits>::close()
1540{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001541 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 this->setstate(ios_base::failbit);
1543}
1544
1545// basic_fstream
1546
1547template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001548class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 : public basic_iostream<_CharT, _Traits>
1550{
1551public:
1552 typedef _CharT char_type;
1553 typedef _Traits traits_type;
1554 typedef typename traits_type::int_type int_type;
1555 typedef typename traits_type::pos_type pos_type;
1556 typedef typename traits_type::off_type off_type;
1557
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001562#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1563 _LIBCPP_INLINE_VISIBILITY
1564 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1565#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001568
Louis Dionnedb84e122021-01-18 12:18:18 -05001569#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001570 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001571 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1572 : basic_fstream(__p.c_str(), __mode) {}
1573#endif // _LIBCPP_STD_VER >= 17
1574
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001580
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 void swap(basic_fstream& __rhs);
1583
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587 bool is_open() const;
1588 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001589#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1590 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1591#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001593
Louis Dionnedb84e122021-01-18 12:18:18 -05001594#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001595 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001596 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1597 { return open(__p.c_str(), __mode); }
1598#endif // _LIBCPP_STD_VER >= 17
1599
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 void close();
1602
1603private:
1604 basic_filebuf<char_type, traits_type> __sb_;
1605};
1606
1607template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001608inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609basic_fstream<_CharT, _Traits>::basic_fstream()
1610 : basic_iostream<char_type, traits_type>(&__sb_)
1611{
1612}
1613
1614template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001615inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1617 : basic_iostream<char_type, traits_type>(&__sb_)
1618{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001619 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 this->setstate(ios_base::failbit);
1621}
1622
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001623#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1624template <class _CharT, class _Traits>
1625inline
1626basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1627 : basic_iostream<char_type, traits_type>(&__sb_)
1628{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001629 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001630 this->setstate(ios_base::failbit);
1631}
1632#endif
1633
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001635inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1637 : basic_iostream<char_type, traits_type>(&__sb_)
1638{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001639 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 this->setstate(ios_base::failbit);
1641}
1642
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001644inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001646 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1647 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648{
1649 this->set_rdbuf(&__sb_);
1650}
1651
1652template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654basic_fstream<_CharT, _Traits>&
1655basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1656{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001657 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1658 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 return *this;
1660}
1661
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664void
1665basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1666{
1667 basic_iostream<char_type, traits_type>::swap(__rhs);
1668 __sb_.swap(__rhs.__sb_);
1669}
1670
1671template <class _CharT, class _Traits>
1672inline _LIBCPP_INLINE_VISIBILITY
1673void
1674swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1675{
1676 __x.swap(__y);
1677}
1678
1679template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681basic_filebuf<_CharT, _Traits>*
1682basic_fstream<_CharT, _Traits>::rdbuf() const
1683{
1684 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1685}
1686
1687template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001688inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689bool
1690basic_fstream<_CharT, _Traits>::is_open() const
1691{
1692 return __sb_.is_open();
1693}
1694
1695template <class _CharT, class _Traits>
1696void
1697basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1698{
1699 if (__sb_.open(__s, __mode))
1700 this->clear();
1701 else
1702 this->setstate(ios_base::failbit);
1703}
1704
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001705#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1706template <class _CharT, class _Traits>
1707void
1708basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1709{
1710 if (__sb_.open(__s, __mode))
1711 this->clear();
1712 else
1713 this->setstate(ios_base::failbit);
1714}
1715#endif
1716
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717template <class _CharT, class _Traits>
1718void
1719basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1720{
1721 if (__sb_.open(__s, __mode))
1722 this->clear();
1723 else
1724 this->setstate(ios_base::failbit);
1725}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726
1727template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001728inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729void
1730basic_fstream<_CharT, _Traits>::close()
1731{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001732 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 this->setstate(ios_base::failbit);
1734}
1735
Louis Dionne8a79be62020-10-21 11:11:45 -04001736#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
Louis Dionnedc496ec2021-06-08 17:25:08 -04001737extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1738extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1739extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
Louis Dionne8a79be62020-10-21 11:11:45 -04001740#endif
1741
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742_LIBCPP_END_NAMESPACE_STD
1743
Eric Fiselierf4433a32017-05-31 22:07:49 +00001744_LIBCPP_POP_MACROS
1745
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001746#endif // _LIBCPP_FSTREAM