blob: d7d6b46c32d9039efa7a3dec9f574c6a7462c1a4 [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
182#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500183#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184#include <ostream>
185#include <istream>
186#include <__locale>
187#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000188#include <cstdlib>
Louis Dionnedb84e122021-01-18 12:18:18 -0500189
190#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
191# include <filesystem>
192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000194#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
Eric Fiselierf4433a32017-05-31 22:07:49 +0000198_LIBCPP_PUSH_MACROS
199#include <__undef_macros>
200
201
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202_LIBCPP_BEGIN_NAMESPACE_STD
203
204template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000205class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 : public basic_streambuf<_CharT, _Traits>
207{
208public:
209 typedef _CharT char_type;
210 typedef _Traits traits_type;
211 typedef typename traits_type::int_type int_type;
212 typedef typename traits_type::pos_type pos_type;
213 typedef typename traits_type::off_type off_type;
214 typedef typename traits_type::state_type state_type;
215
216 // 27.9.1.2 Constructors/destructor:
217 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000218#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 basic_filebuf(basic_filebuf&& __rhs);
220#endif
221 virtual ~basic_filebuf();
222
223 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000224#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_filebuf& operator=(basic_filebuf&& __rhs);
227#endif
228 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
Louis Dionne8a79be62020-10-21 11:11:45 -0400247 inline _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
Eric Fiselier78ccf772017-04-18 23:38:41 +0000317#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318
319template <class _CharT, class _Traits>
320basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
321 : basic_streambuf<_CharT, _Traits>(__rhs)
322{
323 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
324 {
325 __extbuf_ = __extbuf_min_;
326 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
327 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
328 }
329 else
330 {
331 __extbuf_ = __rhs.__extbuf_;
332 __extbufnext_ = __rhs.__extbufnext_;
333 __extbufend_ = __rhs.__extbufend_;
334 }
335 __ebs_ = __rhs.__ebs_;
336 __intbuf_ = __rhs.__intbuf_;
337 __ibs_ = __rhs.__ibs_;
338 __file_ = __rhs.__file_;
339 __cv_ = __rhs.__cv_;
340 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000341 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342 __om_ = __rhs.__om_;
343 __cm_ = __rhs.__cm_;
344 __owns_eb_ = __rhs.__owns_eb_;
345 __owns_ib_ = __rhs.__owns_ib_;
346 __always_noconv_ = __rhs.__always_noconv_;
347 if (__rhs.pbase())
348 {
349 if (__rhs.pbase() == __rhs.__intbuf_)
350 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
351 else
352 this->setp((char_type*)__extbuf_,
353 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000354 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355 }
356 else if (__rhs.eback())
357 {
358 if (__rhs.eback() == __rhs.__intbuf_)
359 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
360 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
361 else
362 this->setg((char_type*)__extbuf_,
363 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
364 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
365 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500366 __rhs.__extbuf_ = nullptr;
367 __rhs.__extbufnext_ = nullptr;
368 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369 __rhs.__ebs_ = 0;
370 __rhs.__intbuf_ = 0;
371 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500372 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000374 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000375 __rhs.__om_ = 0;
376 __rhs.__cm_ = 0;
377 __rhs.__owns_eb_ = false;
378 __rhs.__owns_ib_ = false;
379 __rhs.setg(0, 0, 0);
380 __rhs.setp(0, 0);
381}
382
383template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385basic_filebuf<_CharT, _Traits>&
386basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
387{
388 close();
389 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000390 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391}
392
Eric Fiselier78ccf772017-04-18 23:38:41 +0000393#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template <class _CharT, class _Traits>
396basic_filebuf<_CharT, _Traits>::~basic_filebuf()
397{
398#ifndef _LIBCPP_NO_EXCEPTIONS
399 try
400 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000401#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402 close();
403#ifndef _LIBCPP_NO_EXCEPTIONS
404 }
405 catch (...)
406 {
407 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000408#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 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000422 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
423 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
424 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 }
426 else
427 {
428 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
429 ptrdiff_t __le = __extbufend_ - __extbuf_;
430 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
431 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
432 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
433 {
434 __extbuf_ = __rhs.__extbuf_;
435 __rhs.__extbuf_ = __rhs.__extbuf_min_;
436 }
437 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
438 {
439 __rhs.__extbuf_ = __extbuf_;
440 __extbuf_ = __extbuf_min_;
441 }
442 __extbufnext_ = __extbuf_ + __rn;
443 __extbufend_ = __extbuf_ + __re;
444 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
445 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
446 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000447 _VSTD::swap(__ebs_, __rhs.__ebs_);
448 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
449 _VSTD::swap(__ibs_, __rhs.__ibs_);
450 _VSTD::swap(__file_, __rhs.__file_);
451 _VSTD::swap(__cv_, __rhs.__cv_);
452 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000453 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000454 _VSTD::swap(__om_, __rhs.__om_);
455 _VSTD::swap(__cm_, __rhs.__cm_);
456 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
457 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
458 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
460 {
461 ptrdiff_t __n = this->gptr() - this->eback();
462 ptrdiff_t __e = this->egptr() - this->eback();
463 this->setg((char_type*)__extbuf_min_,
464 (char_type*)__extbuf_min_ + __n,
465 (char_type*)__extbuf_min_ + __e);
466 }
467 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
468 {
469 ptrdiff_t __n = this->pptr() - this->pbase();
470 ptrdiff_t __e = this->epptr() - this->pbase();
471 this->setp((char_type*)__extbuf_min_,
472 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000473 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 }
475 if (__rhs.eback() == (char_type*)__extbuf_min_)
476 {
477 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
478 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
479 __rhs.setg((char_type*)__rhs.__extbuf_min_,
480 (char_type*)__rhs.__extbuf_min_ + __n,
481 (char_type*)__rhs.__extbuf_min_ + __e);
482 }
483 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
484 {
485 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
486 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
487 __rhs.setp((char_type*)__rhs.__extbuf_min_,
488 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000489 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490 }
491}
492
493template <class _CharT, class _Traits>
494inline _LIBCPP_INLINE_VISIBILITY
495void
496swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
497{
498 __x.swap(__y);
499}
500
501template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000502inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503bool
504basic_filebuf<_CharT, _Traits>::is_open() const
505{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500506 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507}
508
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000509template <class _CharT, class _Traits>
510const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
511 ios_base::openmode __mode) _NOEXCEPT {
512 switch (__mode & ~ios_base::ate) {
513 case ios_base::out:
514 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000515 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000516 case ios_base::out | ios_base::app:
517 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000518 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000519 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000520 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000521 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000522 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000523 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000524 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000525 case ios_base::in | ios_base::out | ios_base::app:
526 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::out | ios_base::binary:
529 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::out | ios_base::app | ios_base::binary:
532 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000535 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000536 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000537 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000538 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000539 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000540 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
541 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000542 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000543 default:
544 return nullptr;
545 }
546 _LIBCPP_UNREACHABLE();
547}
548
Ed Schouten7009f4e2015-03-12 15:44:39 +0000549#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550template <class _CharT, class _Traits>
551basic_filebuf<_CharT, _Traits>*
552basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
553{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500554 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
555 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000557 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000559 __file_ = fopen(__s, __mdstr);
560 if (__file_) {
561 __om_ = __mode;
562 if (__mode & ios_base::ate) {
563 if (fseek(__file_, 0, SEEK_END)) {
564 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500565 __file_ = nullptr;
566 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000568 }
569 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500570 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000571 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572 }
573 return __rt;
574}
575
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000576template <class _CharT, class _Traits>
Louis Dionne8a79be62020-10-21 11:11:45 -0400577inline _LIBCPP_INLINE_VISIBILITY
578basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000579basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500580 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
581 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000582 if (const char* __mdstr = __make_mdstring(__mode)) {
583 __rt = this;
584 __file_ = fdopen(__fd, __mdstr);
585 if (__file_) {
586 __om_ = __mode;
587 if (__mode & ios_base::ate) {
588 if (fseek(__file_, 0, SEEK_END)) {
589 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500590 __file_ = nullptr;
591 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000592 }
593 }
594 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500595 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000596 }
597 }
598 return __rt;
599}
600
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000601#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
602// This is basically the same as the char* overload except that it uses _wfopen
603// and long mode strings.
604template <class _CharT, class _Traits>
605basic_filebuf<_CharT, _Traits>*
606basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
607{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500608 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
609 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000610 {
611 __rt = this;
612 const wchar_t* __mdstr;
613 switch (__mode & ~ios_base::ate)
614 {
615 case ios_base::out:
616 case ios_base::out | ios_base::trunc:
617 __mdstr = L"w";
618 break;
619 case ios_base::out | ios_base::app:
620 case ios_base::app:
621 __mdstr = L"a";
622 break;
623 case ios_base::in:
624 __mdstr = L"r";
625 break;
626 case ios_base::in | ios_base::out:
627 __mdstr = L"r+";
628 break;
629 case ios_base::in | ios_base::out | ios_base::trunc:
630 __mdstr = L"w+";
631 break;
632 case ios_base::in | ios_base::out | ios_base::app:
633 case ios_base::in | ios_base::app:
634 __mdstr = L"a+";
635 break;
636 case ios_base::out | ios_base::binary:
637 case ios_base::out | ios_base::trunc | ios_base::binary:
638 __mdstr = L"wb";
639 break;
640 case ios_base::out | ios_base::app | ios_base::binary:
641 case ios_base::app | ios_base::binary:
642 __mdstr = L"ab";
643 break;
644 case ios_base::in | ios_base::binary:
645 __mdstr = L"rb";
646 break;
647 case ios_base::in | ios_base::out | ios_base::binary:
648 __mdstr = L"r+b";
649 break;
650 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
651 __mdstr = L"w+b";
652 break;
653 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
654 case ios_base::in | ios_base::app | ios_base::binary:
655 __mdstr = L"a+b";
656 break;
657 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500658 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000659 break;
660 }
661 if (__rt)
662 {
663 __file_ = _wfopen(__s, __mdstr);
664 if (__file_)
665 {
666 __om_ = __mode;
667 if (__mode & ios_base::ate)
668 {
669 if (fseek(__file_, 0, SEEK_END))
670 {
671 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500672 __file_ = nullptr;
673 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000674 }
675 }
676 }
677 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500678 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000679 }
680 }
681 return __rt;
682}
683#endif
684
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000686inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687basic_filebuf<_CharT, _Traits>*
688basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
689{
690 return open(__s.c_str(), __mode);
691}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000692#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693
694template <class _CharT, class _Traits>
695basic_filebuf<_CharT, _Traits>*
696basic_filebuf<_CharT, _Traits>::close()
697{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500698 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 if (__file_)
700 {
701 __rt = this;
702 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000703 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500704 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000705 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500706 __rt = nullptr;
707 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000708 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709 }
710 return __rt;
711}
712
713template <class _CharT, class _Traits>
714typename basic_filebuf<_CharT, _Traits>::int_type
715basic_filebuf<_CharT, _Traits>::underflow()
716{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500717 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718 return traits_type::eof();
719 bool __initial = __read_mode();
720 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500721 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 this->setg(&__1buf, &__1buf+1, &__1buf+1);
723 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
724 int_type __c = traits_type::eof();
725 if (this->gptr() == this->egptr())
726 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500727 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 if (__always_noconv_)
729 {
730 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
731 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
732 if (__nmemb != 0)
733 {
734 this->setg(this->eback(),
735 this->eback() + __unget_sz,
736 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000737 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 }
739 }
740 else
741 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000742 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
743 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500744 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
746 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000747 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 static_cast<size_t>(__extbufend_ - __extbufnext_));
749 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000750 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000751 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 if (__nr != 0)
753 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000754 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000755 __throw_bad_cast();
756
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 __extbufend_ = __extbufnext_ + __nr;
758 char_type* __inext;
759 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
760 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000761 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 if (__r == codecvt_base::noconv)
763 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000764 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000765 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000766 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 }
768 else if (__inext != this->eback() + __unget_sz)
769 {
770 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000771 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 }
773 }
774 }
775 }
776 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000777 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500779 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 return __c;
781}
782
783template <class _CharT, class _Traits>
784typename basic_filebuf<_CharT, _Traits>::int_type
785basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
786{
787 if (__file_ && this->eback() < this->gptr())
788 {
789 if (traits_type::eq_int_type(__c, traits_type::eof()))
790 {
791 this->gbump(-1);
792 return traits_type::not_eof(__c);
793 }
794 if ((__om_ & ios_base::out) ||
795 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
796 {
797 this->gbump(-1);
798 *this->gptr() = traits_type::to_char_type(__c);
799 return __c;
800 }
801 }
802 return traits_type::eof();
803}
804
805template <class _CharT, class _Traits>
806typename basic_filebuf<_CharT, _Traits>::int_type
807basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
808{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500809 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 return traits_type::eof();
811 __write_mode();
812 char_type __1buf;
813 char_type* __pb_save = this->pbase();
814 char_type* __epb_save = this->epptr();
815 if (!traits_type::eq_int_type(__c, traits_type::eof()))
816 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500817 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818 this->setp(&__1buf, &__1buf+1);
819 *this->pptr() = traits_type::to_char_type(__c);
820 this->pbump(1);
821 }
822 if (this->pptr() != this->pbase())
823 {
824 if (__always_noconv_)
825 {
826 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
827 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
828 return traits_type::eof();
829 }
830 else
831 {
832 char* __extbe = __extbuf_;
833 codecvt_base::result __r;
834 do
835 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000836 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000837 __throw_bad_cast();
838
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 const char_type* __e;
840 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
841 __extbuf_, __extbuf_ + __ebs_, __extbe);
842 if (__e == this->pbase())
843 return traits_type::eof();
844 if (__r == codecvt_base::noconv)
845 {
846 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
847 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
848 return traits_type::eof();
849 }
850 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
851 {
852 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
853 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
854 return traits_type::eof();
855 if (__r == codecvt_base::partial)
856 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000857 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000858 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 }
860 }
861 else
862 return traits_type::eof();
863 } while (__r == codecvt_base::partial);
864 }
865 this->setp(__pb_save, __epb_save);
866 }
867 return traits_type::not_eof(__c);
868}
869
870template <class _CharT, class _Traits>
871basic_streambuf<_CharT, _Traits>*
872basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
873{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500874 this->setg(nullptr, nullptr, nullptr);
875 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 if (__owns_eb_)
877 delete [] __extbuf_;
878 if (__owns_ib_)
879 delete [] __intbuf_;
880 __ebs_ = __n;
881 if (__ebs_ > sizeof(__extbuf_min_))
882 {
883 if (__always_noconv_ && __s)
884 {
885 __extbuf_ = (char*)__s;
886 __owns_eb_ = false;
887 }
888 else
889 {
890 __extbuf_ = new char[__ebs_];
891 __owns_eb_ = true;
892 }
893 }
894 else
895 {
896 __extbuf_ = __extbuf_min_;
897 __ebs_ = sizeof(__extbuf_min_);
898 __owns_eb_ = false;
899 }
900 if (!__always_noconv_)
901 {
902 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
903 if (__s && __ibs_ >= sizeof(__extbuf_min_))
904 {
905 __intbuf_ = __s;
906 __owns_ib_ = false;
907 }
908 else
909 {
910 __intbuf_ = new char_type[__ibs_];
911 __owns_ib_ = true;
912 }
913 }
914 else
915 {
916 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500917 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 __owns_ib_ = false;
919 }
920 return this;
921}
922
923template <class _CharT, class _Traits>
924typename basic_filebuf<_CharT, _Traits>::pos_type
925basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
926 ios_base::openmode)
927{
Howard Hinnant0090b122012-08-24 16:52:47 +0000928 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000929 __throw_bad_cast();
930
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500932 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 return pos_type(off_type(-1));
934 // __width > 0 || __off == 0
935 int __whence;
936 switch (__way)
937 {
938 case ios_base::beg:
939 __whence = SEEK_SET;
940 break;
941 case ios_base::cur:
942 __whence = SEEK_CUR;
943 break;
944 case ios_base::end:
945 __whence = SEEK_END;
946 break;
947 default:
948 return pos_type(off_type(-1));
949 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000950#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000951 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
952 return pos_type(off_type(-1));
953 pos_type __r = ftell(__file_);
954#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
956 return pos_type(off_type(-1));
957 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000958#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 __r.state(__st_);
960 return __r;
961}
962
963template <class _CharT, class _Traits>
964typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000965basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500967 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000969#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000970 if (fseek(__file_, __sp, SEEK_SET))
971 return pos_type(off_type(-1));
972#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 if (fseeko(__file_, __sp, SEEK_SET))
974 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000975#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000976 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 return __sp;
978}
979
980template <class _CharT, class _Traits>
981int
982basic_filebuf<_CharT, _Traits>::sync()
983{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500984 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000986 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000987 __throw_bad_cast();
988
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 if (__cm_ & ios_base::out)
990 {
991 if (this->pptr() != this->pbase())
992 if (overflow() == traits_type::eof())
993 return -1;
994 codecvt_base::result __r;
995 do
996 {
997 char* __extbe;
998 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
999 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1000 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1001 return -1;
1002 } while (__r == codecvt_base::partial);
1003 if (__r == codecvt_base::error)
1004 return -1;
1005 if (fflush(__file_))
1006 return -1;
1007 }
1008 else if (__cm_ & ios_base::in)
1009 {
1010 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001011 state_type __state = __st_last_;
1012 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 if (__always_noconv_)
1014 __c = this->egptr() - this->gptr();
1015 else
1016 {
1017 int __width = __cv_->encoding();
1018 __c = __extbufend_ - __extbufnext_;
1019 if (__width > 0)
1020 __c += __width * (this->egptr() - this->gptr());
1021 else
1022 {
1023 if (this->gptr() != this->egptr())
1024 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001025 const int __off = __cv_->length(__state, __extbuf_,
1026 __extbufnext_,
1027 this->gptr() - this->eback());
1028 __c += __extbufnext_ - __extbuf_ - __off;
1029 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 }
1031 }
1032 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001033#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001034 if (fseek(__file_, -__c, SEEK_CUR))
1035 return -1;
1036#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 if (fseeko(__file_, -__c, SEEK_CUR))
1038 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001039#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001040 if (__update_st)
1041 __st_ = __state;
1042 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001043 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 __cm_ = 0;
1045 }
1046 return 0;
1047}
1048
1049template <class _CharT, class _Traits>
1050void
1051basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1052{
1053 sync();
1054 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1055 bool __old_anc = __always_noconv_;
1056 __always_noconv_ = __cv_->always_noconv();
1057 if (__old_anc != __always_noconv_)
1058 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001059 this->setg(nullptr, nullptr, nullptr);
1060 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 // invariant, char_type is char, else we couldn't get here
1062 if (__always_noconv_) // need to dump __intbuf_
1063 {
1064 if (__owns_eb_)
1065 delete [] __extbuf_;
1066 __owns_eb_ = __owns_ib_;
1067 __ebs_ = __ibs_;
1068 __extbuf_ = (char*)__intbuf_;
1069 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001070 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 __owns_ib_ = false;
1072 }
1073 else // need to obtain an __intbuf_.
1074 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1075 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1076 {
1077 __ibs_ = __ebs_;
1078 __intbuf_ = (char_type*)__extbuf_;
1079 __owns_ib_ = false;
1080 __extbuf_ = new char[__ebs_];
1081 __owns_eb_ = true;
1082 }
1083 else
1084 {
1085 __ibs_ = __ebs_;
1086 __intbuf_ = new char_type[__ibs_];
1087 __owns_ib_ = true;
1088 }
1089 }
1090 }
1091}
1092
1093template <class _CharT, class _Traits>
1094bool
1095basic_filebuf<_CharT, _Traits>::__read_mode()
1096{
1097 if (!(__cm_ & ios_base::in))
1098 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001099 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100 if (__always_noconv_)
1101 this->setg((char_type*)__extbuf_,
1102 (char_type*)__extbuf_ + __ebs_,
1103 (char_type*)__extbuf_ + __ebs_);
1104 else
1105 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1106 __cm_ = ios_base::in;
1107 return true;
1108 }
1109 return false;
1110}
1111
1112template <class _CharT, class _Traits>
1113void
1114basic_filebuf<_CharT, _Traits>::__write_mode()
1115{
1116 if (!(__cm_ & ios_base::out))
1117 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001118 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 if (__ebs_ > sizeof(__extbuf_min_))
1120 {
1121 if (__always_noconv_)
1122 this->setp((char_type*)__extbuf_,
1123 (char_type*)__extbuf_ + (__ebs_ - 1));
1124 else
1125 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1126 }
1127 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001128 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 __cm_ = ios_base::out;
1130 }
1131}
1132
1133// basic_ifstream
1134
1135template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001136class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 : public basic_istream<_CharT, _Traits>
1138{
1139public:
1140 typedef _CharT char_type;
1141 typedef _Traits traits_type;
1142 typedef typename traits_type::int_type int_type;
1143 typedef typename traits_type::pos_type pos_type;
1144 typedef typename traits_type::off_type off_type;
1145
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001148#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001151#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1152 _LIBCPP_INLINE_VISIBILITY
1153 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1154#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001157#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001158 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001159 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1160 : basic_ifstream(__p.c_str(), __mode) {}
1161#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001162#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001163#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 basic_ifstream& operator=(basic_ifstream&& __rhs);
1169#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 void swap(basic_ifstream& __rhs);
1172
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001177#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001179#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1180 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001183#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001184 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001185 void open(const filesystem::path& __p,
1186 ios_base::openmode __mode = ios_base::in) {
1187 return open(__p.c_str(), __mode);
1188 }
1189#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001190
1191 _LIBCPP_INLINE_VISIBILITY
1192 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001193#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 void close();
1196
1197private:
1198 basic_filebuf<char_type, traits_type> __sb_;
1199};
1200
1201template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001202inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203basic_ifstream<_CharT, _Traits>::basic_ifstream()
1204 : basic_istream<char_type, traits_type>(&__sb_)
1205{
1206}
1207
Ed Schouten7009f4e2015-03-12 15:44:39 +00001208#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001210inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1212 : basic_istream<char_type, traits_type>(&__sb_)
1213{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001214 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 this->setstate(ios_base::failbit);
1216}
1217
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001218#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1219template <class _CharT, class _Traits>
1220inline
1221basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1222 : basic_istream<char_type, traits_type>(&__sb_)
1223{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001224 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001225 this->setstate(ios_base::failbit);
1226}
1227#endif
1228
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001230inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1232 : basic_istream<char_type, traits_type>(&__sb_)
1233{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001234 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 this->setstate(ios_base::failbit);
1236}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
Eric Fiselier78ccf772017-04-18 23:38:41 +00001239#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240
1241template <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
Eric Fiselier78ccf772017-04-18 23:38:41 +00001260#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261
1262template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001263inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264void
1265basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1266{
1267 basic_istream<char_type, traits_type>::swap(__rhs);
1268 __sb_.swap(__rhs.__sb_);
1269}
1270
1271template <class _CharT, class _Traits>
1272inline _LIBCPP_INLINE_VISIBILITY
1273void
1274swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1275{
1276 __x.swap(__y);
1277}
1278
1279template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001280inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281basic_filebuf<_CharT, _Traits>*
1282basic_ifstream<_CharT, _Traits>::rdbuf() const
1283{
1284 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1285}
1286
1287template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001288inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289bool
1290basic_ifstream<_CharT, _Traits>::is_open() const
1291{
1292 return __sb_.is_open();
1293}
1294
Ed Schouten7009f4e2015-03-12 15:44:39 +00001295#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296template <class _CharT, class _Traits>
1297void
1298basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1299{
1300 if (__sb_.open(__s, __mode | ios_base::in))
1301 this->clear();
1302 else
1303 this->setstate(ios_base::failbit);
1304}
1305
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001306#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1307template <class _CharT, class _Traits>
1308void
1309basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1310{
1311 if (__sb_.open(__s, __mode | ios_base::in))
1312 this->clear();
1313 else
1314 this->setstate(ios_base::failbit);
1315}
1316#endif
1317
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318template <class _CharT, class _Traits>
1319void
1320basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1321{
1322 if (__sb_.open(__s, __mode | ios_base::in))
1323 this->clear();
1324 else
1325 this->setstate(ios_base::failbit);
1326}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001327
1328template <class _CharT, class _Traits>
1329void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1330 ios_base::openmode __mode) {
1331 if (__sb_.__open(__fd, __mode | ios_base::in))
1332 this->clear();
1333 else
1334 this->setstate(ios_base::failbit);
1335}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001336#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337
1338template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001339inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340void
1341basic_ifstream<_CharT, _Traits>::close()
1342{
1343 if (__sb_.close() == 0)
1344 this->setstate(ios_base::failbit);
1345}
1346
1347// basic_ofstream
1348
1349template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001350class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 : public basic_ostream<_CharT, _Traits>
1352{
1353public:
1354 typedef _CharT char_type;
1355 typedef _Traits traits_type;
1356 typedef typename traits_type::int_type int_type;
1357 typedef typename traits_type::pos_type pos_type;
1358 typedef typename traits_type::off_type off_type;
1359
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001364#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1365 _LIBCPP_INLINE_VISIBILITY
1366 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1367#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001370
Louis Dionnedb84e122021-01-18 12:18:18 -05001371#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001372 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001373 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1374 : basic_ofstream(__p.c_str(), __mode) {}
1375#endif // _LIBCPP_STD_VER >= 17
1376
Eric Fiselier78ccf772017-04-18 23:38:41 +00001377#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 basic_ofstream& operator=(basic_ofstream&& __rhs);
1383#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 void swap(basic_ofstream& __rhs);
1386
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001391#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001393#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1394 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1395#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001397
Louis Dionnedb84e122021-01-18 12:18:18 -05001398#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001399 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001400 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1401 { return open(__p.c_str(), __mode); }
1402#endif // _LIBCPP_STD_VER >= 17
1403
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001404 _LIBCPP_INLINE_VISIBILITY
1405 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001406#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 void close();
1409
1410private:
1411 basic_filebuf<char_type, traits_type> __sb_;
1412};
1413
1414template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001415inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416basic_ofstream<_CharT, _Traits>::basic_ofstream()
1417 : basic_ostream<char_type, traits_type>(&__sb_)
1418{
1419}
1420
Ed Schouten7009f4e2015-03-12 15:44:39 +00001421#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001423inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1425 : basic_ostream<char_type, traits_type>(&__sb_)
1426{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001427 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 this->setstate(ios_base::failbit);
1429}
1430
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001431#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1432template <class _CharT, class _Traits>
1433inline
1434basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1435 : basic_ostream<char_type, traits_type>(&__sb_)
1436{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001437 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001438 this->setstate(ios_base::failbit);
1439}
1440#endif
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(const string& __s, ios_base::openmode __mode)
1445 : basic_ostream<char_type, traits_type>(&__sb_)
1446{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001447 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 this->setstate(ios_base::failbit);
1449}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001450#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451
Eric Fiselier78ccf772017-04-18 23:38:41 +00001452#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
1454template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001457 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1458 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459{
1460 this->set_rdbuf(&__sb_);
1461}
1462
1463template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001464inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465basic_ofstream<_CharT, _Traits>&
1466basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1467{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001468 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1469 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470 return *this;
1471}
1472
Eric Fiselier78ccf772017-04-18 23:38:41 +00001473#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474
1475template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001476inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477void
1478basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1479{
1480 basic_ostream<char_type, traits_type>::swap(__rhs);
1481 __sb_.swap(__rhs.__sb_);
1482}
1483
1484template <class _CharT, class _Traits>
1485inline _LIBCPP_INLINE_VISIBILITY
1486void
1487swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1488{
1489 __x.swap(__y);
1490}
1491
1492template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001493inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494basic_filebuf<_CharT, _Traits>*
1495basic_ofstream<_CharT, _Traits>::rdbuf() const
1496{
1497 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1498}
1499
1500template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001501inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502bool
1503basic_ofstream<_CharT, _Traits>::is_open() const
1504{
1505 return __sb_.is_open();
1506}
1507
Ed Schouten7009f4e2015-03-12 15:44:39 +00001508#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509template <class _CharT, class _Traits>
1510void
1511basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1512{
1513 if (__sb_.open(__s, __mode | ios_base::out))
1514 this->clear();
1515 else
1516 this->setstate(ios_base::failbit);
1517}
1518
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001519#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1520template <class _CharT, class _Traits>
1521void
1522basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1523{
1524 if (__sb_.open(__s, __mode | ios_base::out))
1525 this->clear();
1526 else
1527 this->setstate(ios_base::failbit);
1528}
1529#endif
1530
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531template <class _CharT, class _Traits>
1532void
1533basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1534{
1535 if (__sb_.open(__s, __mode | ios_base::out))
1536 this->clear();
1537 else
1538 this->setstate(ios_base::failbit);
1539}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001540
1541template <class _CharT, class _Traits>
1542void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1543 ios_base::openmode __mode) {
1544 if (__sb_.__open(__fd, __mode | ios_base::out))
1545 this->clear();
1546 else
1547 this->setstate(ios_base::failbit);
1548}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001549#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550
1551template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001552inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553void
1554basic_ofstream<_CharT, _Traits>::close()
1555{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001556 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 this->setstate(ios_base::failbit);
1558}
1559
1560// basic_fstream
1561
1562template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001563class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 : public basic_iostream<_CharT, _Traits>
1565{
1566public:
1567 typedef _CharT char_type;
1568 typedef _Traits traits_type;
1569 typedef typename traits_type::int_type int_type;
1570 typedef typename traits_type::pos_type pos_type;
1571 typedef typename traits_type::off_type off_type;
1572
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001575#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001578#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1579 _LIBCPP_INLINE_VISIBILITY
1580 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1581#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001584
Louis Dionnedb84e122021-01-18 12:18:18 -05001585#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001586 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001587 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1588 : basic_fstream(__p.c_str(), __mode) {}
1589#endif // _LIBCPP_STD_VER >= 17
1590
Ed Schouten7009f4e2015-03-12 15:44:39 +00001591#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001592#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 basic_fstream& operator=(basic_fstream&& __rhs);
1598#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 void swap(basic_fstream& __rhs);
1601
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001606#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001608#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1609 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1610#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001612
Louis Dionnedb84e122021-01-18 12:18:18 -05001613#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001614 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001615 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1616 { return open(__p.c_str(), __mode); }
1617#endif // _LIBCPP_STD_VER >= 17
1618
Ed Schouten7009f4e2015-03-12 15:44:39 +00001619#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 void close();
1622
1623private:
1624 basic_filebuf<char_type, traits_type> __sb_;
1625};
1626
1627template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001628inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629basic_fstream<_CharT, _Traits>::basic_fstream()
1630 : basic_iostream<char_type, traits_type>(&__sb_)
1631{
1632}
1633
Ed Schouten7009f4e2015-03-12 15:44:39 +00001634#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001636inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1638 : basic_iostream<char_type, traits_type>(&__sb_)
1639{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001640 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 this->setstate(ios_base::failbit);
1642}
1643
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001644#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1645template <class _CharT, class _Traits>
1646inline
1647basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1648 : basic_iostream<char_type, traits_type>(&__sb_)
1649{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001650 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001651 this->setstate(ios_base::failbit);
1652}
1653#endif
1654
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001656inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1658 : basic_iostream<char_type, traits_type>(&__sb_)
1659{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001660 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 this->setstate(ios_base::failbit);
1662}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001663#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664
Eric Fiselier78ccf772017-04-18 23:38:41 +00001665#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666
1667template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001668inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001670 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1671 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001672{
1673 this->set_rdbuf(&__sb_);
1674}
1675
1676template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001677inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678basic_fstream<_CharT, _Traits>&
1679basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1680{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001681 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1682 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 return *this;
1684}
1685
Eric Fiselier78ccf772017-04-18 23:38:41 +00001686#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687
1688template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690void
1691basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1692{
1693 basic_iostream<char_type, traits_type>::swap(__rhs);
1694 __sb_.swap(__rhs.__sb_);
1695}
1696
1697template <class _CharT, class _Traits>
1698inline _LIBCPP_INLINE_VISIBILITY
1699void
1700swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1701{
1702 __x.swap(__y);
1703}
1704
1705template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001706inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707basic_filebuf<_CharT, _Traits>*
1708basic_fstream<_CharT, _Traits>::rdbuf() const
1709{
1710 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1711}
1712
1713template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001714inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715bool
1716basic_fstream<_CharT, _Traits>::is_open() const
1717{
1718 return __sb_.is_open();
1719}
1720
Ed Schouten7009f4e2015-03-12 15:44:39 +00001721#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722template <class _CharT, class _Traits>
1723void
1724basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1725{
1726 if (__sb_.open(__s, __mode))
1727 this->clear();
1728 else
1729 this->setstate(ios_base::failbit);
1730}
1731
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001732#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1733template <class _CharT, class _Traits>
1734void
1735basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1736{
1737 if (__sb_.open(__s, __mode))
1738 this->clear();
1739 else
1740 this->setstate(ios_base::failbit);
1741}
1742#endif
1743
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744template <class _CharT, class _Traits>
1745void
1746basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1747{
1748 if (__sb_.open(__s, __mode))
1749 this->clear();
1750 else
1751 this->setstate(ios_base::failbit);
1752}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001753#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
1755template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001756inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757void
1758basic_fstream<_CharT, _Traits>::close()
1759{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001760 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 this->setstate(ios_base::failbit);
1762}
1763
Louis Dionne8a79be62020-10-21 11:11:45 -04001764#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1765_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1766_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1767_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1768#endif
1769
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770_LIBCPP_END_NAMESPACE_STD
1771
Eric Fiselierf4433a32017-05-31 22:07:49 +00001772_LIBCPP_POP_MACROS
1773
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774#endif // _LIBCPP_FSTREAM