blob: c522b8ab110d7dce085996abd40adc68e8cda7f1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- fstream ------------------------------------===//
3//
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
Louis Dionne73912b22020-11-04 15:01:25 -0500182#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400183#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400184#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185#include <__locale>
186#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000187#include <cstdlib>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400188#include <istream>
189#include <ostream>
Louis Dionnedb84e122021-01-18 12:18:18 -0500190
191#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
192# include <filesystem>
193#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000195#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198
Eric Fiselierf4433a32017-05-31 22:07:49 +0000199_LIBCPP_PUSH_MACROS
200#include <__undef_macros>
201
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400202#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
203# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
204#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000205
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206_LIBCPP_BEGIN_NAMESPACE_STD
207
208template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000209class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 : public basic_streambuf<_CharT, _Traits>
211{
212public:
213 typedef _CharT char_type;
214 typedef _Traits traits_type;
215 typedef typename traits_type::int_type int_type;
216 typedef typename traits_type::pos_type pos_type;
217 typedef typename traits_type::off_type off_type;
218 typedef typename traits_type::state_type state_type;
219
220 // 27.9.1.2 Constructors/destructor:
221 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 basic_filebuf(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 virtual ~basic_filebuf();
224
225 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 void swap(basic_filebuf& __rhs);
229
230 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000233#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000235#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
236 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
237#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000240
Louis Dionnedb84e122021-01-18 12:18:18 -0500241#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000242 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000243 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
244 return open(__p.c_str(), __mode);
245 }
246#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000248 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250 basic_filebuf* close();
251
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000252 _LIBCPP_INLINE_VISIBILITY
253 inline static const char*
254 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
255
256 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257 // 27.9.1.5 Overridden virtual functions:
258 virtual int_type underflow();
259 virtual int_type pbackfail(int_type __c = traits_type::eof());
260 virtual int_type overflow (int_type __c = traits_type::eof());
261 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
262 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
263 ios_base::openmode __wch = ios_base::in | ios_base::out);
264 virtual pos_type seekpos(pos_type __sp,
265 ios_base::openmode __wch = ios_base::in | ios_base::out);
266 virtual int sync();
267 virtual void imbue(const locale& __loc);
268
269private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000270 char* __extbuf_;
271 const char* __extbufnext_;
272 const char* __extbufend_;
273 char __extbuf_min_[8];
274 size_t __ebs_;
275 char_type* __intbuf_;
276 size_t __ibs_;
277 FILE* __file_;
278 const codecvt<char_type, char, state_type>* __cv_;
279 state_type __st_;
280 state_type __st_last_;
281 ios_base::openmode __om_;
282 ios_base::openmode __cm_;
283 bool __owns_eb_;
284 bool __owns_ib_;
285 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000287 bool __read_mode();
288 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289};
290
291template <class _CharT, class _Traits>
292basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500293 : __extbuf_(nullptr),
294 __extbufnext_(nullptr),
295 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500297 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500299 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000300 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000302 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 __om_(0),
304 __cm_(0),
305 __owns_eb_(false),
306 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000307 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308{
Howard Hinnant0090b122012-08-24 16:52:47 +0000309 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
310 {
311 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
312 __always_noconv_ = __cv_->always_noconv();
313 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500314 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315}
316
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317template <class _CharT, class _Traits>
318basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
319 : basic_streambuf<_CharT, _Traits>(__rhs)
320{
321 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
322 {
323 __extbuf_ = __extbuf_min_;
324 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
325 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
326 }
327 else
328 {
329 __extbuf_ = __rhs.__extbuf_;
330 __extbufnext_ = __rhs.__extbufnext_;
331 __extbufend_ = __rhs.__extbufend_;
332 }
333 __ebs_ = __rhs.__ebs_;
334 __intbuf_ = __rhs.__intbuf_;
335 __ibs_ = __rhs.__ibs_;
336 __file_ = __rhs.__file_;
337 __cv_ = __rhs.__cv_;
338 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000339 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340 __om_ = __rhs.__om_;
341 __cm_ = __rhs.__cm_;
342 __owns_eb_ = __rhs.__owns_eb_;
343 __owns_ib_ = __rhs.__owns_ib_;
344 __always_noconv_ = __rhs.__always_noconv_;
345 if (__rhs.pbase())
346 {
347 if (__rhs.pbase() == __rhs.__intbuf_)
348 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
349 else
350 this->setp((char_type*)__extbuf_,
351 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000352 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353 }
354 else if (__rhs.eback())
355 {
356 if (__rhs.eback() == __rhs.__intbuf_)
357 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
358 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
359 else
360 this->setg((char_type*)__extbuf_,
361 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
362 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
363 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500364 __rhs.__extbuf_ = nullptr;
365 __rhs.__extbufnext_ = nullptr;
366 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367 __rhs.__ebs_ = 0;
368 __rhs.__intbuf_ = 0;
369 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500370 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000372 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 __rhs.__om_ = 0;
374 __rhs.__cm_ = 0;
375 __rhs.__owns_eb_ = false;
376 __rhs.__owns_ib_ = false;
377 __rhs.setg(0, 0, 0);
378 __rhs.setp(0, 0);
379}
380
381template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000382inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383basic_filebuf<_CharT, _Traits>&
384basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
385{
386 close();
387 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000388 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389}
390
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391template <class _CharT, class _Traits>
392basic_filebuf<_CharT, _Traits>::~basic_filebuf()
393{
394#ifndef _LIBCPP_NO_EXCEPTIONS
395 try
396 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400397#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 close();
399#ifndef _LIBCPP_NO_EXCEPTIONS
400 }
401 catch (...)
402 {
403 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 if (__owns_eb_)
406 delete [] __extbuf_;
407 if (__owns_ib_)
408 delete [] __intbuf_;
409}
410
411template <class _CharT, class _Traits>
412void
413basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
414{
415 basic_streambuf<char_type, traits_type>::swap(__rhs);
416 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
417 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000418 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
419 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
420 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 }
422 else
423 {
424 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
425 ptrdiff_t __le = __extbufend_ - __extbuf_;
426 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
427 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
428 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
429 {
430 __extbuf_ = __rhs.__extbuf_;
431 __rhs.__extbuf_ = __rhs.__extbuf_min_;
432 }
433 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
434 {
435 __rhs.__extbuf_ = __extbuf_;
436 __extbuf_ = __extbuf_min_;
437 }
438 __extbufnext_ = __extbuf_ + __rn;
439 __extbufend_ = __extbuf_ + __re;
440 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
441 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
442 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000443 _VSTD::swap(__ebs_, __rhs.__ebs_);
444 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
445 _VSTD::swap(__ibs_, __rhs.__ibs_);
446 _VSTD::swap(__file_, __rhs.__file_);
447 _VSTD::swap(__cv_, __rhs.__cv_);
448 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000449 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000450 _VSTD::swap(__om_, __rhs.__om_);
451 _VSTD::swap(__cm_, __rhs.__cm_);
452 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
453 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
454 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
456 {
457 ptrdiff_t __n = this->gptr() - this->eback();
458 ptrdiff_t __e = this->egptr() - this->eback();
459 this->setg((char_type*)__extbuf_min_,
460 (char_type*)__extbuf_min_ + __n,
461 (char_type*)__extbuf_min_ + __e);
462 }
463 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
464 {
465 ptrdiff_t __n = this->pptr() - this->pbase();
466 ptrdiff_t __e = this->epptr() - this->pbase();
467 this->setp((char_type*)__extbuf_min_,
468 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000469 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470 }
471 if (__rhs.eback() == (char_type*)__extbuf_min_)
472 {
473 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
474 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
475 __rhs.setg((char_type*)__rhs.__extbuf_min_,
476 (char_type*)__rhs.__extbuf_min_ + __n,
477 (char_type*)__rhs.__extbuf_min_ + __e);
478 }
479 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
480 {
481 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
482 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
483 __rhs.setp((char_type*)__rhs.__extbuf_min_,
484 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000485 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000486 }
487}
488
489template <class _CharT, class _Traits>
490inline _LIBCPP_INLINE_VISIBILITY
491void
492swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
493{
494 __x.swap(__y);
495}
496
497template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000498inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499bool
500basic_filebuf<_CharT, _Traits>::is_open() const
501{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500502 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503}
504
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000505template <class _CharT, class _Traits>
506const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
507 ios_base::openmode __mode) _NOEXCEPT {
508 switch (__mode & ~ios_base::ate) {
509 case ios_base::out:
510 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000511 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000512 case ios_base::out | ios_base::app:
513 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000514 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000515 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000516 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000517 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000518 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000519 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000520 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000521 case ios_base::in | ios_base::out | ios_base::app:
522 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000523 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000524 case ios_base::out | ios_base::binary:
525 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000526 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000527 case ios_base::out | ios_base::app | ios_base::binary:
528 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000529 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000530 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000531 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000532 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000535 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000536 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
537 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000538 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000539 default:
540 return nullptr;
541 }
542 _LIBCPP_UNREACHABLE();
543}
544
Ed Schouten7009f4e2015-03-12 15:44:39 +0000545#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546template <class _CharT, class _Traits>
547basic_filebuf<_CharT, _Traits>*
548basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
549{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500550 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
551 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000553 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000555 __file_ = fopen(__s, __mdstr);
556 if (__file_) {
557 __om_ = __mode;
558 if (__mode & ios_base::ate) {
559 if (fseek(__file_, 0, SEEK_END)) {
560 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500561 __file_ = nullptr;
562 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000564 }
565 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500566 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000567 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 }
569 return __rt;
570}
571
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000572template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000573inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400574basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000575basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500576 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
577 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000578 if (const char* __mdstr = __make_mdstring(__mode)) {
579 __rt = this;
580 __file_ = fdopen(__fd, __mdstr);
581 if (__file_) {
582 __om_ = __mode;
583 if (__mode & ios_base::ate) {
584 if (fseek(__file_, 0, SEEK_END)) {
585 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500586 __file_ = nullptr;
587 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000588 }
589 }
590 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500591 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000592 }
593 }
594 return __rt;
595}
596
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000597#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
598// This is basically the same as the char* overload except that it uses _wfopen
599// and long mode strings.
600template <class _CharT, class _Traits>
601basic_filebuf<_CharT, _Traits>*
602basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
603{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500604 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
605 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000606 {
607 __rt = this;
608 const wchar_t* __mdstr;
609 switch (__mode & ~ios_base::ate)
610 {
611 case ios_base::out:
612 case ios_base::out | ios_base::trunc:
613 __mdstr = L"w";
614 break;
615 case ios_base::out | ios_base::app:
616 case ios_base::app:
617 __mdstr = L"a";
618 break;
619 case ios_base::in:
620 __mdstr = L"r";
621 break;
622 case ios_base::in | ios_base::out:
623 __mdstr = L"r+";
624 break;
625 case ios_base::in | ios_base::out | ios_base::trunc:
626 __mdstr = L"w+";
627 break;
628 case ios_base::in | ios_base::out | ios_base::app:
629 case ios_base::in | ios_base::app:
630 __mdstr = L"a+";
631 break;
632 case ios_base::out | ios_base::binary:
633 case ios_base::out | ios_base::trunc | ios_base::binary:
634 __mdstr = L"wb";
635 break;
636 case ios_base::out | ios_base::app | ios_base::binary:
637 case ios_base::app | ios_base::binary:
638 __mdstr = L"ab";
639 break;
640 case ios_base::in | ios_base::binary:
641 __mdstr = L"rb";
642 break;
643 case ios_base::in | ios_base::out | ios_base::binary:
644 __mdstr = L"r+b";
645 break;
646 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
647 __mdstr = L"w+b";
648 break;
649 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
650 case ios_base::in | ios_base::app | ios_base::binary:
651 __mdstr = L"a+b";
652 break;
653 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500654 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000655 break;
656 }
657 if (__rt)
658 {
659 __file_ = _wfopen(__s, __mdstr);
660 if (__file_)
661 {
662 __om_ = __mode;
663 if (__mode & ios_base::ate)
664 {
665 if (fseek(__file_, 0, SEEK_END))
666 {
667 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500668 __file_ = nullptr;
669 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000670 }
671 }
672 }
673 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500674 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000675 }
676 }
677 return __rt;
678}
679#endif
680
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683basic_filebuf<_CharT, _Traits>*
684basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
685{
686 return open(__s.c_str(), __mode);
687}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
690template <class _CharT, class _Traits>
691basic_filebuf<_CharT, _Traits>*
692basic_filebuf<_CharT, _Traits>::close()
693{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500694 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 if (__file_)
696 {
697 __rt = this;
698 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000699 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500700 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000701 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500702 __rt = nullptr;
703 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000704 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705 }
706 return __rt;
707}
708
709template <class _CharT, class _Traits>
710typename basic_filebuf<_CharT, _Traits>::int_type
711basic_filebuf<_CharT, _Traits>::underflow()
712{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500713 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 return traits_type::eof();
715 bool __initial = __read_mode();
716 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500717 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 this->setg(&__1buf, &__1buf+1, &__1buf+1);
719 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
720 int_type __c = traits_type::eof();
721 if (this->gptr() == this->egptr())
722 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500723 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 if (__always_noconv_)
725 {
726 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
727 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
728 if (__nmemb != 0)
729 {
730 this->setg(this->eback(),
731 this->eback() + __unget_sz,
732 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000733 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 }
735 }
736 else
737 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000738 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
739 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500740 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
742 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000743 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744 static_cast<size_t>(__extbufend_ - __extbufnext_));
745 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000746 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000747 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 if (__nr != 0)
749 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000750 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000751 __throw_bad_cast();
752
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753 __extbufend_ = __extbufnext_ + __nr;
754 char_type* __inext;
755 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
756 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000757 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 if (__r == codecvt_base::noconv)
759 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000760 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000761 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000762 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 }
764 else if (__inext != this->eback() + __unget_sz)
765 {
766 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000767 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 }
769 }
770 }
771 }
772 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000773 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500775 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 return __c;
777}
778
779template <class _CharT, class _Traits>
780typename basic_filebuf<_CharT, _Traits>::int_type
781basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
782{
783 if (__file_ && this->eback() < this->gptr())
784 {
785 if (traits_type::eq_int_type(__c, traits_type::eof()))
786 {
787 this->gbump(-1);
788 return traits_type::not_eof(__c);
789 }
790 if ((__om_ & ios_base::out) ||
791 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
792 {
793 this->gbump(-1);
794 *this->gptr() = traits_type::to_char_type(__c);
795 return __c;
796 }
797 }
798 return traits_type::eof();
799}
800
801template <class _CharT, class _Traits>
802typename basic_filebuf<_CharT, _Traits>::int_type
803basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
804{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500805 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806 return traits_type::eof();
807 __write_mode();
808 char_type __1buf;
809 char_type* __pb_save = this->pbase();
810 char_type* __epb_save = this->epptr();
811 if (!traits_type::eq_int_type(__c, traits_type::eof()))
812 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500813 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 this->setp(&__1buf, &__1buf+1);
815 *this->pptr() = traits_type::to_char_type(__c);
816 this->pbump(1);
817 }
818 if (this->pptr() != this->pbase())
819 {
820 if (__always_noconv_)
821 {
822 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
823 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
824 return traits_type::eof();
825 }
826 else
827 {
828 char* __extbe = __extbuf_;
829 codecvt_base::result __r;
830 do
831 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000832 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000833 __throw_bad_cast();
834
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 const char_type* __e;
836 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
837 __extbuf_, __extbuf_ + __ebs_, __extbe);
838 if (__e == this->pbase())
839 return traits_type::eof();
840 if (__r == codecvt_base::noconv)
841 {
842 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
843 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
844 return traits_type::eof();
845 }
846 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
847 {
848 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
849 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
850 return traits_type::eof();
851 if (__r == codecvt_base::partial)
852 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000853 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000854 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855 }
856 }
857 else
858 return traits_type::eof();
859 } while (__r == codecvt_base::partial);
860 }
861 this->setp(__pb_save, __epb_save);
862 }
863 return traits_type::not_eof(__c);
864}
865
866template <class _CharT, class _Traits>
867basic_streambuf<_CharT, _Traits>*
868basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
869{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500870 this->setg(nullptr, nullptr, nullptr);
871 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 if (__owns_eb_)
873 delete [] __extbuf_;
874 if (__owns_ib_)
875 delete [] __intbuf_;
876 __ebs_ = __n;
877 if (__ebs_ > sizeof(__extbuf_min_))
878 {
879 if (__always_noconv_ && __s)
880 {
881 __extbuf_ = (char*)__s;
882 __owns_eb_ = false;
883 }
884 else
885 {
886 __extbuf_ = new char[__ebs_];
887 __owns_eb_ = true;
888 }
889 }
890 else
891 {
892 __extbuf_ = __extbuf_min_;
893 __ebs_ = sizeof(__extbuf_min_);
894 __owns_eb_ = false;
895 }
896 if (!__always_noconv_)
897 {
898 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
899 if (__s && __ibs_ >= sizeof(__extbuf_min_))
900 {
901 __intbuf_ = __s;
902 __owns_ib_ = false;
903 }
904 else
905 {
906 __intbuf_ = new char_type[__ibs_];
907 __owns_ib_ = true;
908 }
909 }
910 else
911 {
912 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500913 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 __owns_ib_ = false;
915 }
916 return this;
917}
918
919template <class _CharT, class _Traits>
920typename basic_filebuf<_CharT, _Traits>::pos_type
921basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
922 ios_base::openmode)
923{
Howard Hinnant0090b122012-08-24 16:52:47 +0000924 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000925 __throw_bad_cast();
926
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500928 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 return pos_type(off_type(-1));
930 // __width > 0 || __off == 0
931 int __whence;
932 switch (__way)
933 {
934 case ios_base::beg:
935 __whence = SEEK_SET;
936 break;
937 case ios_base::cur:
938 __whence = SEEK_CUR;
939 break;
940 case ios_base::end:
941 __whence = SEEK_END;
942 break;
943 default:
944 return pos_type(off_type(-1));
945 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000946#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000947 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
948 return pos_type(off_type(-1));
949 pos_type __r = ftell(__file_);
950#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
952 return pos_type(off_type(-1));
953 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000954#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 __r.state(__st_);
956 return __r;
957}
958
959template <class _CharT, class _Traits>
960typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000961basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500963 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000965#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000966 if (fseek(__file_, __sp, SEEK_SET))
967 return pos_type(off_type(-1));
968#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 if (fseeko(__file_, __sp, SEEK_SET))
970 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000971#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000972 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 return __sp;
974}
975
976template <class _CharT, class _Traits>
977int
978basic_filebuf<_CharT, _Traits>::sync()
979{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500980 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000982 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000983 __throw_bad_cast();
984
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 if (__cm_ & ios_base::out)
986 {
987 if (this->pptr() != this->pbase())
988 if (overflow() == traits_type::eof())
989 return -1;
990 codecvt_base::result __r;
991 do
992 {
993 char* __extbe;
994 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
995 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
996 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
997 return -1;
998 } while (__r == codecvt_base::partial);
999 if (__r == codecvt_base::error)
1000 return -1;
1001 if (fflush(__file_))
1002 return -1;
1003 }
1004 else if (__cm_ & ios_base::in)
1005 {
1006 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001007 state_type __state = __st_last_;
1008 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 if (__always_noconv_)
1010 __c = this->egptr() - this->gptr();
1011 else
1012 {
1013 int __width = __cv_->encoding();
1014 __c = __extbufend_ - __extbufnext_;
1015 if (__width > 0)
1016 __c += __width * (this->egptr() - this->gptr());
1017 else
1018 {
1019 if (this->gptr() != this->egptr())
1020 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001021 const int __off = __cv_->length(__state, __extbuf_,
1022 __extbufnext_,
1023 this->gptr() - this->eback());
1024 __c += __extbufnext_ - __extbuf_ - __off;
1025 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 }
1027 }
1028 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001029#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001030 if (fseek(__file_, -__c, SEEK_CUR))
1031 return -1;
1032#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 if (fseeko(__file_, -__c, SEEK_CUR))
1034 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001035#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001036 if (__update_st)
1037 __st_ = __state;
1038 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001039 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 __cm_ = 0;
1041 }
1042 return 0;
1043}
1044
1045template <class _CharT, class _Traits>
1046void
1047basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1048{
1049 sync();
1050 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1051 bool __old_anc = __always_noconv_;
1052 __always_noconv_ = __cv_->always_noconv();
1053 if (__old_anc != __always_noconv_)
1054 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001055 this->setg(nullptr, nullptr, nullptr);
1056 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 // invariant, char_type is char, else we couldn't get here
1058 if (__always_noconv_) // need to dump __intbuf_
1059 {
1060 if (__owns_eb_)
1061 delete [] __extbuf_;
1062 __owns_eb_ = __owns_ib_;
1063 __ebs_ = __ibs_;
1064 __extbuf_ = (char*)__intbuf_;
1065 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001066 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 __owns_ib_ = false;
1068 }
1069 else // need to obtain an __intbuf_.
1070 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1071 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1072 {
1073 __ibs_ = __ebs_;
1074 __intbuf_ = (char_type*)__extbuf_;
1075 __owns_ib_ = false;
1076 __extbuf_ = new char[__ebs_];
1077 __owns_eb_ = true;
1078 }
1079 else
1080 {
1081 __ibs_ = __ebs_;
1082 __intbuf_ = new char_type[__ibs_];
1083 __owns_ib_ = true;
1084 }
1085 }
1086 }
1087}
1088
1089template <class _CharT, class _Traits>
1090bool
1091basic_filebuf<_CharT, _Traits>::__read_mode()
1092{
1093 if (!(__cm_ & ios_base::in))
1094 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001095 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 if (__always_noconv_)
1097 this->setg((char_type*)__extbuf_,
1098 (char_type*)__extbuf_ + __ebs_,
1099 (char_type*)__extbuf_ + __ebs_);
1100 else
1101 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1102 __cm_ = ios_base::in;
1103 return true;
1104 }
1105 return false;
1106}
1107
1108template <class _CharT, class _Traits>
1109void
1110basic_filebuf<_CharT, _Traits>::__write_mode()
1111{
1112 if (!(__cm_ & ios_base::out))
1113 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001114 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 if (__ebs_ > sizeof(__extbuf_min_))
1116 {
1117 if (__always_noconv_)
1118 this->setp((char_type*)__extbuf_,
1119 (char_type*)__extbuf_ + (__ebs_ - 1));
1120 else
1121 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1122 }
1123 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001124 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 __cm_ = ios_base::out;
1126 }
1127}
1128
1129// basic_ifstream
1130
1131template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001132class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 : public basic_istream<_CharT, _Traits>
1134{
1135public:
1136 typedef _CharT char_type;
1137 typedef _Traits traits_type;
1138 typedef typename traits_type::int_type int_type;
1139 typedef typename traits_type::pos_type pos_type;
1140 typedef typename traits_type::off_type off_type;
1141
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001144#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001147#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1148 _LIBCPP_INLINE_VISIBILITY
1149 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1150#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001153#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001154 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001155 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1156 : basic_ifstream(__p.c_str(), __mode) {}
1157#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001158#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 void swap(basic_ifstream& __rhs);
1165
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001170#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001172#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1173 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1174#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001176#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001177 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001178 void open(const filesystem::path& __p,
1179 ios_base::openmode __mode = ios_base::in) {
1180 return open(__p.c_str(), __mode);
1181 }
1182#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001183
1184 _LIBCPP_INLINE_VISIBILITY
1185 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001186#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 void close();
1189
1190private:
1191 basic_filebuf<char_type, traits_type> __sb_;
1192};
1193
1194template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196basic_ifstream<_CharT, _Traits>::basic_ifstream()
1197 : basic_istream<char_type, traits_type>(&__sb_)
1198{
1199}
1200
Ed Schouten7009f4e2015-03-12 15:44:39 +00001201#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001203inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1205 : basic_istream<char_type, traits_type>(&__sb_)
1206{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001207 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 this->setstate(ios_base::failbit);
1209}
1210
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001211#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1212template <class _CharT, class _Traits>
1213inline
1214basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __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)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001218 this->setstate(ios_base::failbit);
1219}
1220#endif
1221
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001223inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __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)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 this->setstate(ios_base::failbit);
1229}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
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(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001235 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1236 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237{
1238 this->set_rdbuf(&__sb_);
1239}
1240
1241template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243basic_ifstream<_CharT, _Traits>&
1244basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1245{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001246 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1247 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 return *this;
1249}
1250
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001252inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253void
1254basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1255{
1256 basic_istream<char_type, traits_type>::swap(__rhs);
1257 __sb_.swap(__rhs.__sb_);
1258}
1259
1260template <class _CharT, class _Traits>
1261inline _LIBCPP_INLINE_VISIBILITY
1262void
1263swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1264{
1265 __x.swap(__y);
1266}
1267
1268template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001269inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270basic_filebuf<_CharT, _Traits>*
1271basic_ifstream<_CharT, _Traits>::rdbuf() const
1272{
1273 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1274}
1275
1276template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001277inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278bool
1279basic_ifstream<_CharT, _Traits>::is_open() const
1280{
1281 return __sb_.is_open();
1282}
1283
Ed Schouten7009f4e2015-03-12 15:44:39 +00001284#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285template <class _CharT, class _Traits>
1286void
1287basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1288{
1289 if (__sb_.open(__s, __mode | ios_base::in))
1290 this->clear();
1291 else
1292 this->setstate(ios_base::failbit);
1293}
1294
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001295#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1296template <class _CharT, class _Traits>
1297void
1298basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1299{
1300 if (__sb_.open(__s, __mode | ios_base::in))
1301 this->clear();
1302 else
1303 this->setstate(ios_base::failbit);
1304}
1305#endif
1306
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307template <class _CharT, class _Traits>
1308void
1309basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1310{
1311 if (__sb_.open(__s, __mode | ios_base::in))
1312 this->clear();
1313 else
1314 this->setstate(ios_base::failbit);
1315}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001316
1317template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001318inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001319void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1320 ios_base::openmode __mode) {
1321 if (__sb_.__open(__fd, __mode | ios_base::in))
1322 this->clear();
1323 else
1324 this->setstate(ios_base::failbit);
1325}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001326#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327
1328template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001329inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330void
1331basic_ifstream<_CharT, _Traits>::close()
1332{
1333 if (__sb_.close() == 0)
1334 this->setstate(ios_base::failbit);
1335}
1336
1337// basic_ofstream
1338
1339template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001340class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 : public basic_ostream<_CharT, _Traits>
1342{
1343public:
1344 typedef _CharT char_type;
1345 typedef _Traits traits_type;
1346 typedef typename traits_type::int_type int_type;
1347 typedef typename traits_type::pos_type pos_type;
1348 typedef typename traits_type::off_type off_type;
1349
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001354#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1355 _LIBCPP_INLINE_VISIBILITY
1356 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1357#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001360
Louis Dionnedb84e122021-01-18 12:18:18 -05001361#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001362 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001363 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1364 : basic_ofstream(__p.c_str(), __mode) {}
1365#endif // _LIBCPP_STD_VER >= 17
1366
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 void swap(basic_ofstream& __rhs);
1373
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001378#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001380#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1381 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1382#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001384
Louis Dionnedb84e122021-01-18 12:18:18 -05001385#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001386 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001387 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1388 { return open(__p.c_str(), __mode); }
1389#endif // _LIBCPP_STD_VER >= 17
1390
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001391 _LIBCPP_INLINE_VISIBILITY
1392 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001393#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 void close();
1396
1397private:
1398 basic_filebuf<char_type, traits_type> __sb_;
1399};
1400
1401template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001402inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403basic_ofstream<_CharT, _Traits>::basic_ofstream()
1404 : basic_ostream<char_type, traits_type>(&__sb_)
1405{
1406}
1407
Ed Schouten7009f4e2015-03-12 15:44:39 +00001408#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1412 : basic_ostream<char_type, traits_type>(&__sb_)
1413{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001414 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 this->setstate(ios_base::failbit);
1416}
1417
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001418#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1419template <class _CharT, class _Traits>
1420inline
1421basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1422 : basic_ostream<char_type, traits_type>(&__sb_)
1423{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001424 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001425 this->setstate(ios_base::failbit);
1426}
1427#endif
1428
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001430inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1432 : basic_ostream<char_type, traits_type>(&__sb_)
1433{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001434 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435 this->setstate(ios_base::failbit);
1436}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001437#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001440inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001442 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1443 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444{
1445 this->set_rdbuf(&__sb_);
1446}
1447
1448template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450basic_ofstream<_CharT, _Traits>&
1451basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1452{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001453 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1454 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 return *this;
1456}
1457
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460void
1461basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1462{
1463 basic_ostream<char_type, traits_type>::swap(__rhs);
1464 __sb_.swap(__rhs.__sb_);
1465}
1466
1467template <class _CharT, class _Traits>
1468inline _LIBCPP_INLINE_VISIBILITY
1469void
1470swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1471{
1472 __x.swap(__y);
1473}
1474
1475template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001476inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477basic_filebuf<_CharT, _Traits>*
1478basic_ofstream<_CharT, _Traits>::rdbuf() const
1479{
1480 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1481}
1482
1483template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001484inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485bool
1486basic_ofstream<_CharT, _Traits>::is_open() const
1487{
1488 return __sb_.is_open();
1489}
1490
Ed Schouten7009f4e2015-03-12 15:44:39 +00001491#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492template <class _CharT, class _Traits>
1493void
1494basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1495{
1496 if (__sb_.open(__s, __mode | ios_base::out))
1497 this->clear();
1498 else
1499 this->setstate(ios_base::failbit);
1500}
1501
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001502#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1503template <class _CharT, class _Traits>
1504void
1505basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1506{
1507 if (__sb_.open(__s, __mode | ios_base::out))
1508 this->clear();
1509 else
1510 this->setstate(ios_base::failbit);
1511}
1512#endif
1513
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514template <class _CharT, class _Traits>
1515void
1516basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1517{
1518 if (__sb_.open(__s, __mode | ios_base::out))
1519 this->clear();
1520 else
1521 this->setstate(ios_base::failbit);
1522}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001523
1524template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001525inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001526void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1527 ios_base::openmode __mode) {
1528 if (__sb_.__open(__fd, __mode | ios_base::out))
1529 this->clear();
1530 else
1531 this->setstate(ios_base::failbit);
1532}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001533#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534
1535template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001536inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537void
1538basic_ofstream<_CharT, _Traits>::close()
1539{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001540 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 this->setstate(ios_base::failbit);
1542}
1543
1544// basic_fstream
1545
1546template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001547class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 : public basic_iostream<_CharT, _Traits>
1549{
1550public:
1551 typedef _CharT char_type;
1552 typedef _Traits traits_type;
1553 typedef typename traits_type::int_type int_type;
1554 typedef typename traits_type::pos_type pos_type;
1555 typedef typename traits_type::off_type off_type;
1556
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001559#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
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
Ed Schouten7009f4e2015-03-12 15:44:39 +00001575#endif
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;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001589#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001591#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1592 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1593#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001595
Louis Dionnedb84e122021-01-18 12:18:18 -05001596#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001597 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001598 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1599 { return open(__p.c_str(), __mode); }
1600#endif // _LIBCPP_STD_VER >= 17
1601
Ed Schouten7009f4e2015-03-12 15:44:39 +00001602#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 void close();
1605
1606private:
1607 basic_filebuf<char_type, traits_type> __sb_;
1608};
1609
1610template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001611inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612basic_fstream<_CharT, _Traits>::basic_fstream()
1613 : basic_iostream<char_type, traits_type>(&__sb_)
1614{
1615}
1616
Ed Schouten7009f4e2015-03-12 15:44:39 +00001617#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001619inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1621 : basic_iostream<char_type, traits_type>(&__sb_)
1622{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001623 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 this->setstate(ios_base::failbit);
1625}
1626
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001627#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1628template <class _CharT, class _Traits>
1629inline
1630basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1631 : basic_iostream<char_type, traits_type>(&__sb_)
1632{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001633 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001634 this->setstate(ios_base::failbit);
1635}
1636#endif
1637
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001639inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1641 : basic_iostream<char_type, traits_type>(&__sb_)
1642{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001643 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 this->setstate(ios_base::failbit);
1645}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001646#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001651 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1652 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653{
1654 this->set_rdbuf(&__sb_);
1655}
1656
1657template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001658inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659basic_fstream<_CharT, _Traits>&
1660basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1661{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001662 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1663 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 return *this;
1665}
1666
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001668inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669void
1670basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1671{
1672 basic_iostream<char_type, traits_type>::swap(__rhs);
1673 __sb_.swap(__rhs.__sb_);
1674}
1675
1676template <class _CharT, class _Traits>
1677inline _LIBCPP_INLINE_VISIBILITY
1678void
1679swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1680{
1681 __x.swap(__y);
1682}
1683
1684template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001685inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686basic_filebuf<_CharT, _Traits>*
1687basic_fstream<_CharT, _Traits>::rdbuf() const
1688{
1689 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1690}
1691
1692template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001693inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694bool
1695basic_fstream<_CharT, _Traits>::is_open() const
1696{
1697 return __sb_.is_open();
1698}
1699
Ed Schouten7009f4e2015-03-12 15:44:39 +00001700#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701template <class _CharT, class _Traits>
1702void
1703basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1704{
1705 if (__sb_.open(__s, __mode))
1706 this->clear();
1707 else
1708 this->setstate(ios_base::failbit);
1709}
1710
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001711#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1712template <class _CharT, class _Traits>
1713void
1714basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1715{
1716 if (__sb_.open(__s, __mode))
1717 this->clear();
1718 else
1719 this->setstate(ios_base::failbit);
1720}
1721#endif
1722
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723template <class _CharT, class _Traits>
1724void
1725basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1726{
1727 if (__sb_.open(__s, __mode))
1728 this->clear();
1729 else
1730 this->setstate(ios_base::failbit);
1731}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001732#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733
1734template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001735inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736void
1737basic_fstream<_CharT, _Traits>::close()
1738{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001739 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 this->setstate(ios_base::failbit);
1741}
1742
Louis Dionne8a79be62020-10-21 11:11:45 -04001743#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1744_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1745_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1746_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1747#endif
1748
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749_LIBCPP_END_NAMESPACE_STD
1750
Eric Fiselierf4433a32017-05-31 22:07:49 +00001751_LIBCPP_POP_MACROS
1752
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001753#endif // _LIBCPP_FSTREAM