blob: 701f65b44452ce99cde70aedf3420a1ad91d64b5 [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>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000189#include <filesystem>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000191#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000193#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Eric Fiselierf4433a32017-05-31 22:07:49 +0000195_LIBCPP_PUSH_MACROS
196#include <__undef_macros>
197
198
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199_LIBCPP_BEGIN_NAMESPACE_STD
200
201template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000202class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 : public basic_streambuf<_CharT, _Traits>
204{
205public:
206 typedef _CharT char_type;
207 typedef _Traits traits_type;
208 typedef typename traits_type::int_type int_type;
209 typedef typename traits_type::pos_type pos_type;
210 typedef typename traits_type::off_type off_type;
211 typedef typename traits_type::state_type state_type;
212
213 // 27.9.1.2 Constructors/destructor:
214 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000215#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216 basic_filebuf(basic_filebuf&& __rhs);
217#endif
218 virtual ~basic_filebuf();
219
220 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000221#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_filebuf& operator=(basic_filebuf&& __rhs);
224#endif
225 void swap(basic_filebuf& __rhs);
226
227 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000230#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000232#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
233 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
234#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000237
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000238#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000239 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000240 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
241 return open(__p.c_str(), __mode);
242 }
243#endif
Louis Dionne8a79be62020-10-21 11:11:45 -0400244 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000245 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_filebuf* close();
248
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000249 _LIBCPP_INLINE_VISIBILITY
250 inline static const char*
251 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
252
253 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 // 27.9.1.5 Overridden virtual functions:
255 virtual int_type underflow();
256 virtual int_type pbackfail(int_type __c = traits_type::eof());
257 virtual int_type overflow (int_type __c = traits_type::eof());
258 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
259 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
260 ios_base::openmode __wch = ios_base::in | ios_base::out);
261 virtual pos_type seekpos(pos_type __sp,
262 ios_base::openmode __wch = ios_base::in | ios_base::out);
263 virtual int sync();
264 virtual void imbue(const locale& __loc);
265
266private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000267 char* __extbuf_;
268 const char* __extbufnext_;
269 const char* __extbufend_;
270 char __extbuf_min_[8];
271 size_t __ebs_;
272 char_type* __intbuf_;
273 size_t __ibs_;
274 FILE* __file_;
275 const codecvt<char_type, char, state_type>* __cv_;
276 state_type __st_;
277 state_type __st_last_;
278 ios_base::openmode __om_;
279 ios_base::openmode __cm_;
280 bool __owns_eb_;
281 bool __owns_ib_;
282 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000284 bool __read_mode();
285 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286};
287
288template <class _CharT, class _Traits>
289basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500290 : __extbuf_(nullptr),
291 __extbufnext_(nullptr),
292 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500294 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500296 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000297 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000299 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __om_(0),
301 __cm_(0),
302 __owns_eb_(false),
303 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000304 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305{
Howard Hinnant0090b122012-08-24 16:52:47 +0000306 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
307 {
308 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
309 __always_noconv_ = __cv_->always_noconv();
310 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500311 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312}
313
Eric Fiselier78ccf772017-04-18 23:38:41 +0000314#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315
316template <class _CharT, class _Traits>
317basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
318 : basic_streambuf<_CharT, _Traits>(__rhs)
319{
320 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
321 {
322 __extbuf_ = __extbuf_min_;
323 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
324 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
325 }
326 else
327 {
328 __extbuf_ = __rhs.__extbuf_;
329 __extbufnext_ = __rhs.__extbufnext_;
330 __extbufend_ = __rhs.__extbufend_;
331 }
332 __ebs_ = __rhs.__ebs_;
333 __intbuf_ = __rhs.__intbuf_;
334 __ibs_ = __rhs.__ibs_;
335 __file_ = __rhs.__file_;
336 __cv_ = __rhs.__cv_;
337 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000338 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 __om_ = __rhs.__om_;
340 __cm_ = __rhs.__cm_;
341 __owns_eb_ = __rhs.__owns_eb_;
342 __owns_ib_ = __rhs.__owns_ib_;
343 __always_noconv_ = __rhs.__always_noconv_;
344 if (__rhs.pbase())
345 {
346 if (__rhs.pbase() == __rhs.__intbuf_)
347 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
348 else
349 this->setp((char_type*)__extbuf_,
350 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000351 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 }
353 else if (__rhs.eback())
354 {
355 if (__rhs.eback() == __rhs.__intbuf_)
356 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
357 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
358 else
359 this->setg((char_type*)__extbuf_,
360 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
361 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
362 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500363 __rhs.__extbuf_ = nullptr;
364 __rhs.__extbufnext_ = nullptr;
365 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 __rhs.__ebs_ = 0;
367 __rhs.__intbuf_ = 0;
368 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500369 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000371 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__om_ = 0;
373 __rhs.__cm_ = 0;
374 __rhs.__owns_eb_ = false;
375 __rhs.__owns_ib_ = false;
376 __rhs.setg(0, 0, 0);
377 __rhs.setp(0, 0);
378}
379
380template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382basic_filebuf<_CharT, _Traits>&
383basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
384{
385 close();
386 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000387 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388}
389
Eric Fiselier78ccf772017-04-18 23:38:41 +0000390#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template <class _CharT, class _Traits>
393basic_filebuf<_CharT, _Traits>::~basic_filebuf()
394{
395#ifndef _LIBCPP_NO_EXCEPTIONS
396 try
397 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000398#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 close();
400#ifndef _LIBCPP_NO_EXCEPTIONS
401 }
402 catch (...)
403 {
404 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000405#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 if (__owns_eb_)
407 delete [] __extbuf_;
408 if (__owns_ib_)
409 delete [] __intbuf_;
410}
411
412template <class _CharT, class _Traits>
413void
414basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
415{
416 basic_streambuf<char_type, traits_type>::swap(__rhs);
417 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
418 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000419 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
420 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
421 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 }
423 else
424 {
425 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
426 ptrdiff_t __le = __extbufend_ - __extbuf_;
427 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
428 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
429 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
430 {
431 __extbuf_ = __rhs.__extbuf_;
432 __rhs.__extbuf_ = __rhs.__extbuf_min_;
433 }
434 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
435 {
436 __rhs.__extbuf_ = __extbuf_;
437 __extbuf_ = __extbuf_min_;
438 }
439 __extbufnext_ = __extbuf_ + __rn;
440 __extbufend_ = __extbuf_ + __re;
441 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
442 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
443 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000444 _VSTD::swap(__ebs_, __rhs.__ebs_);
445 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
446 _VSTD::swap(__ibs_, __rhs.__ibs_);
447 _VSTD::swap(__file_, __rhs.__file_);
448 _VSTD::swap(__cv_, __rhs.__cv_);
449 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000450 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000451 _VSTD::swap(__om_, __rhs.__om_);
452 _VSTD::swap(__cm_, __rhs.__cm_);
453 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
454 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
455 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
457 {
458 ptrdiff_t __n = this->gptr() - this->eback();
459 ptrdiff_t __e = this->egptr() - this->eback();
460 this->setg((char_type*)__extbuf_min_,
461 (char_type*)__extbuf_min_ + __n,
462 (char_type*)__extbuf_min_ + __e);
463 }
464 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
465 {
466 ptrdiff_t __n = this->pptr() - this->pbase();
467 ptrdiff_t __e = this->epptr() - this->pbase();
468 this->setp((char_type*)__extbuf_min_,
469 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000470 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 }
472 if (__rhs.eback() == (char_type*)__extbuf_min_)
473 {
474 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
475 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
476 __rhs.setg((char_type*)__rhs.__extbuf_min_,
477 (char_type*)__rhs.__extbuf_min_ + __n,
478 (char_type*)__rhs.__extbuf_min_ + __e);
479 }
480 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
481 {
482 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
483 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
484 __rhs.setp((char_type*)__rhs.__extbuf_min_,
485 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000486 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 }
488}
489
490template <class _CharT, class _Traits>
491inline _LIBCPP_INLINE_VISIBILITY
492void
493swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
494{
495 __x.swap(__y);
496}
497
498template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000499inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500bool
501basic_filebuf<_CharT, _Traits>::is_open() const
502{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500503 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504}
505
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000506template <class _CharT, class _Traits>
507const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
508 ios_base::openmode __mode) _NOEXCEPT {
509 switch (__mode & ~ios_base::ate) {
510 case ios_base::out:
511 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000512 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000513 case ios_base::out | ios_base::app:
514 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000515 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000516 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000517 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000518 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000519 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000520 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000521 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000522 case ios_base::in | ios_base::out | ios_base::app:
523 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000524 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000525 case ios_base::out | ios_base::binary:
526 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::out | ios_base::app | ios_base::binary:
529 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000532 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000533 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000534 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000535 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000536 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000537 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
538 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000539 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000540 default:
541 return nullptr;
542 }
543 _LIBCPP_UNREACHABLE();
544}
545
Ed Schouten7009f4e2015-03-12 15:44:39 +0000546#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547template <class _CharT, class _Traits>
548basic_filebuf<_CharT, _Traits>*
549basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
550{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500551 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
552 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000554 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000556 __file_ = fopen(__s, __mdstr);
557 if (__file_) {
558 __om_ = __mode;
559 if (__mode & ios_base::ate) {
560 if (fseek(__file_, 0, SEEK_END)) {
561 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500562 __file_ = nullptr;
563 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000565 }
566 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500567 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000568 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 }
570 return __rt;
571}
572
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000573template <class _CharT, class _Traits>
Louis Dionne8a79be62020-10-21 11:11:45 -0400574inline _LIBCPP_INLINE_VISIBILITY
575basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000576basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500577 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
578 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000579 if (const char* __mdstr = __make_mdstring(__mode)) {
580 __rt = this;
581 __file_ = fdopen(__fd, __mdstr);
582 if (__file_) {
583 __om_ = __mode;
584 if (__mode & ios_base::ate) {
585 if (fseek(__file_, 0, SEEK_END)) {
586 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500587 __file_ = nullptr;
588 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000589 }
590 }
591 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500592 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000593 }
594 }
595 return __rt;
596}
597
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000598#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
599// This is basically the same as the char* overload except that it uses _wfopen
600// and long mode strings.
601template <class _CharT, class _Traits>
602basic_filebuf<_CharT, _Traits>*
603basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
604{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500605 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
606 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000607 {
608 __rt = this;
609 const wchar_t* __mdstr;
610 switch (__mode & ~ios_base::ate)
611 {
612 case ios_base::out:
613 case ios_base::out | ios_base::trunc:
614 __mdstr = L"w";
615 break;
616 case ios_base::out | ios_base::app:
617 case ios_base::app:
618 __mdstr = L"a";
619 break;
620 case ios_base::in:
621 __mdstr = L"r";
622 break;
623 case ios_base::in | ios_base::out:
624 __mdstr = L"r+";
625 break;
626 case ios_base::in | ios_base::out | ios_base::trunc:
627 __mdstr = L"w+";
628 break;
629 case ios_base::in | ios_base::out | ios_base::app:
630 case ios_base::in | ios_base::app:
631 __mdstr = L"a+";
632 break;
633 case ios_base::out | ios_base::binary:
634 case ios_base::out | ios_base::trunc | ios_base::binary:
635 __mdstr = L"wb";
636 break;
637 case ios_base::out | ios_base::app | ios_base::binary:
638 case ios_base::app | ios_base::binary:
639 __mdstr = L"ab";
640 break;
641 case ios_base::in | ios_base::binary:
642 __mdstr = L"rb";
643 break;
644 case ios_base::in | ios_base::out | ios_base::binary:
645 __mdstr = L"r+b";
646 break;
647 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
648 __mdstr = L"w+b";
649 break;
650 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
651 case ios_base::in | ios_base::app | ios_base::binary:
652 __mdstr = L"a+b";
653 break;
654 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500655 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000656 break;
657 }
658 if (__rt)
659 {
660 __file_ = _wfopen(__s, __mdstr);
661 if (__file_)
662 {
663 __om_ = __mode;
664 if (__mode & ios_base::ate)
665 {
666 if (fseek(__file_, 0, SEEK_END))
667 {
668 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500669 __file_ = nullptr;
670 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000671 }
672 }
673 }
674 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500675 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000676 }
677 }
678 return __rt;
679}
680#endif
681
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000683inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684basic_filebuf<_CharT, _Traits>*
685basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
686{
687 return open(__s.c_str(), __mode);
688}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000689#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
691template <class _CharT, class _Traits>
692basic_filebuf<_CharT, _Traits>*
693basic_filebuf<_CharT, _Traits>::close()
694{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500695 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 if (__file_)
697 {
698 __rt = this;
699 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000700 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500701 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000702 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500703 __rt = nullptr;
704 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000705 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 }
707 return __rt;
708}
709
710template <class _CharT, class _Traits>
711typename basic_filebuf<_CharT, _Traits>::int_type
712basic_filebuf<_CharT, _Traits>::underflow()
713{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500714 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 return traits_type::eof();
716 bool __initial = __read_mode();
717 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500718 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 this->setg(&__1buf, &__1buf+1, &__1buf+1);
720 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
721 int_type __c = traits_type::eof();
722 if (this->gptr() == this->egptr())
723 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500724 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 if (__always_noconv_)
726 {
727 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
728 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
729 if (__nmemb != 0)
730 {
731 this->setg(this->eback(),
732 this->eback() + __unget_sz,
733 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000734 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 }
736 }
737 else
738 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000739 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
740 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500741 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
743 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000744 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 static_cast<size_t>(__extbufend_ - __extbufnext_));
746 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000747 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000748 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 if (__nr != 0)
750 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000751 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000752 __throw_bad_cast();
753
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 __extbufend_ = __extbufnext_ + __nr;
755 char_type* __inext;
756 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
757 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000758 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 if (__r == codecvt_base::noconv)
760 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000761 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000762 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000763 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 }
765 else if (__inext != this->eback() + __unget_sz)
766 {
767 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000768 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 }
770 }
771 }
772 }
773 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000774 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500776 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 return __c;
778}
779
780template <class _CharT, class _Traits>
781typename basic_filebuf<_CharT, _Traits>::int_type
782basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
783{
784 if (__file_ && this->eback() < this->gptr())
785 {
786 if (traits_type::eq_int_type(__c, traits_type::eof()))
787 {
788 this->gbump(-1);
789 return traits_type::not_eof(__c);
790 }
791 if ((__om_ & ios_base::out) ||
792 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
793 {
794 this->gbump(-1);
795 *this->gptr() = traits_type::to_char_type(__c);
796 return __c;
797 }
798 }
799 return traits_type::eof();
800}
801
802template <class _CharT, class _Traits>
803typename basic_filebuf<_CharT, _Traits>::int_type
804basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
805{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500806 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 return traits_type::eof();
808 __write_mode();
809 char_type __1buf;
810 char_type* __pb_save = this->pbase();
811 char_type* __epb_save = this->epptr();
812 if (!traits_type::eq_int_type(__c, traits_type::eof()))
813 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500814 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 this->setp(&__1buf, &__1buf+1);
816 *this->pptr() = traits_type::to_char_type(__c);
817 this->pbump(1);
818 }
819 if (this->pptr() != this->pbase())
820 {
821 if (__always_noconv_)
822 {
823 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
824 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
825 return traits_type::eof();
826 }
827 else
828 {
829 char* __extbe = __extbuf_;
830 codecvt_base::result __r;
831 do
832 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000833 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000834 __throw_bad_cast();
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 const char_type* __e;
837 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
838 __extbuf_, __extbuf_ + __ebs_, __extbe);
839 if (__e == this->pbase())
840 return traits_type::eof();
841 if (__r == codecvt_base::noconv)
842 {
843 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
844 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
845 return traits_type::eof();
846 }
847 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
848 {
849 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
850 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
851 return traits_type::eof();
852 if (__r == codecvt_base::partial)
853 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000854 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000855 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 }
857 }
858 else
859 return traits_type::eof();
860 } while (__r == codecvt_base::partial);
861 }
862 this->setp(__pb_save, __epb_save);
863 }
864 return traits_type::not_eof(__c);
865}
866
867template <class _CharT, class _Traits>
868basic_streambuf<_CharT, _Traits>*
869basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
870{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500871 this->setg(nullptr, nullptr, nullptr);
872 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 if (__owns_eb_)
874 delete [] __extbuf_;
875 if (__owns_ib_)
876 delete [] __intbuf_;
877 __ebs_ = __n;
878 if (__ebs_ > sizeof(__extbuf_min_))
879 {
880 if (__always_noconv_ && __s)
881 {
882 __extbuf_ = (char*)__s;
883 __owns_eb_ = false;
884 }
885 else
886 {
887 __extbuf_ = new char[__ebs_];
888 __owns_eb_ = true;
889 }
890 }
891 else
892 {
893 __extbuf_ = __extbuf_min_;
894 __ebs_ = sizeof(__extbuf_min_);
895 __owns_eb_ = false;
896 }
897 if (!__always_noconv_)
898 {
899 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
900 if (__s && __ibs_ >= sizeof(__extbuf_min_))
901 {
902 __intbuf_ = __s;
903 __owns_ib_ = false;
904 }
905 else
906 {
907 __intbuf_ = new char_type[__ibs_];
908 __owns_ib_ = true;
909 }
910 }
911 else
912 {
913 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500914 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 __owns_ib_ = false;
916 }
917 return this;
918}
919
920template <class _CharT, class _Traits>
921typename basic_filebuf<_CharT, _Traits>::pos_type
922basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
923 ios_base::openmode)
924{
Howard Hinnant0090b122012-08-24 16:52:47 +0000925 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000926 __throw_bad_cast();
927
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500929 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930 return pos_type(off_type(-1));
931 // __width > 0 || __off == 0
932 int __whence;
933 switch (__way)
934 {
935 case ios_base::beg:
936 __whence = SEEK_SET;
937 break;
938 case ios_base::cur:
939 __whence = SEEK_CUR;
940 break;
941 case ios_base::end:
942 __whence = SEEK_END;
943 break;
944 default:
945 return pos_type(off_type(-1));
946 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000947#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000948 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
949 return pos_type(off_type(-1));
950 pos_type __r = ftell(__file_);
951#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
953 return pos_type(off_type(-1));
954 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 __r.state(__st_);
957 return __r;
958}
959
960template <class _CharT, class _Traits>
961typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000962basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500964 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000966#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000967 if (fseek(__file_, __sp, SEEK_SET))
968 return pos_type(off_type(-1));
969#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 if (fseeko(__file_, __sp, SEEK_SET))
971 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000972#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000973 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 return __sp;
975}
976
977template <class _CharT, class _Traits>
978int
979basic_filebuf<_CharT, _Traits>::sync()
980{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500981 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000983 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000984 __throw_bad_cast();
985
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 if (__cm_ & ios_base::out)
987 {
988 if (this->pptr() != this->pbase())
989 if (overflow() == traits_type::eof())
990 return -1;
991 codecvt_base::result __r;
992 do
993 {
994 char* __extbe;
995 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
996 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
997 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
998 return -1;
999 } while (__r == codecvt_base::partial);
1000 if (__r == codecvt_base::error)
1001 return -1;
1002 if (fflush(__file_))
1003 return -1;
1004 }
1005 else if (__cm_ & ios_base::in)
1006 {
1007 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001008 state_type __state = __st_last_;
1009 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 if (__always_noconv_)
1011 __c = this->egptr() - this->gptr();
1012 else
1013 {
1014 int __width = __cv_->encoding();
1015 __c = __extbufend_ - __extbufnext_;
1016 if (__width > 0)
1017 __c += __width * (this->egptr() - this->gptr());
1018 else
1019 {
1020 if (this->gptr() != this->egptr())
1021 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001022 const int __off = __cv_->length(__state, __extbuf_,
1023 __extbufnext_,
1024 this->gptr() - this->eback());
1025 __c += __extbufnext_ - __extbuf_ - __off;
1026 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 }
1028 }
1029 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001030#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001031 if (fseek(__file_, -__c, SEEK_CUR))
1032 return -1;
1033#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 if (fseeko(__file_, -__c, SEEK_CUR))
1035 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001036#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001037 if (__update_st)
1038 __st_ = __state;
1039 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001040 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 __cm_ = 0;
1042 }
1043 return 0;
1044}
1045
1046template <class _CharT, class _Traits>
1047void
1048basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1049{
1050 sync();
1051 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1052 bool __old_anc = __always_noconv_;
1053 __always_noconv_ = __cv_->always_noconv();
1054 if (__old_anc != __always_noconv_)
1055 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001056 this->setg(nullptr, nullptr, nullptr);
1057 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 // invariant, char_type is char, else we couldn't get here
1059 if (__always_noconv_) // need to dump __intbuf_
1060 {
1061 if (__owns_eb_)
1062 delete [] __extbuf_;
1063 __owns_eb_ = __owns_ib_;
1064 __ebs_ = __ibs_;
1065 __extbuf_ = (char*)__intbuf_;
1066 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001067 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 __owns_ib_ = false;
1069 }
1070 else // need to obtain an __intbuf_.
1071 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1072 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1073 {
1074 __ibs_ = __ebs_;
1075 __intbuf_ = (char_type*)__extbuf_;
1076 __owns_ib_ = false;
1077 __extbuf_ = new char[__ebs_];
1078 __owns_eb_ = true;
1079 }
1080 else
1081 {
1082 __ibs_ = __ebs_;
1083 __intbuf_ = new char_type[__ibs_];
1084 __owns_ib_ = true;
1085 }
1086 }
1087 }
1088}
1089
1090template <class _CharT, class _Traits>
1091bool
1092basic_filebuf<_CharT, _Traits>::__read_mode()
1093{
1094 if (!(__cm_ & ios_base::in))
1095 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001096 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 if (__always_noconv_)
1098 this->setg((char_type*)__extbuf_,
1099 (char_type*)__extbuf_ + __ebs_,
1100 (char_type*)__extbuf_ + __ebs_);
1101 else
1102 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1103 __cm_ = ios_base::in;
1104 return true;
1105 }
1106 return false;
1107}
1108
1109template <class _CharT, class _Traits>
1110void
1111basic_filebuf<_CharT, _Traits>::__write_mode()
1112{
1113 if (!(__cm_ & ios_base::out))
1114 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001115 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 if (__ebs_ > sizeof(__extbuf_min_))
1117 {
1118 if (__always_noconv_)
1119 this->setp((char_type*)__extbuf_,
1120 (char_type*)__extbuf_ + (__ebs_ - 1));
1121 else
1122 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1123 }
1124 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001125 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 __cm_ = ios_base::out;
1127 }
1128}
1129
1130// basic_ifstream
1131
1132template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001133class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 : public basic_istream<_CharT, _Traits>
1135{
1136public:
1137 typedef _CharT char_type;
1138 typedef _Traits traits_type;
1139 typedef typename traits_type::int_type int_type;
1140 typedef typename traits_type::pos_type pos_type;
1141 typedef typename traits_type::off_type off_type;
1142
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001145#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001148#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 _LIBCPP_INLINE_VISIBILITY
1150 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1151#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001154#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001155 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001156 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1157 : basic_ifstream(__p.c_str(), __mode) {}
1158#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001159#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001160#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 basic_ifstream& operator=(basic_ifstream&& __rhs);
1166#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 void swap(basic_ifstream& __rhs);
1169
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001174#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001176#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1177 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1178#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001180#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001181 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001182 void open(const filesystem::path& __p,
1183 ios_base::openmode __mode = ios_base::in) {
1184 return open(__p.c_str(), __mode);
1185 }
1186#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001187
1188 _LIBCPP_INLINE_VISIBILITY
1189 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001190#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 void close();
1193
1194private:
1195 basic_filebuf<char_type, traits_type> __sb_;
1196};
1197
1198template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001199inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200basic_ifstream<_CharT, _Traits>::basic_ifstream()
1201 : basic_istream<char_type, traits_type>(&__sb_)
1202{
1203}
1204
Ed Schouten7009f4e2015-03-12 15:44:39 +00001205#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1209 : basic_istream<char_type, traits_type>(&__sb_)
1210{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001211 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 this->setstate(ios_base::failbit);
1213}
1214
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001215#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1216template <class _CharT, class _Traits>
1217inline
1218basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1219 : basic_istream<char_type, traits_type>(&__sb_)
1220{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001221 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001222 this->setstate(ios_base::failbit);
1223}
1224#endif
1225
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001227inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1229 : basic_istream<char_type, traits_type>(&__sb_)
1230{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001231 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 this->setstate(ios_base::failbit);
1233}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Eric Fiselier78ccf772017-04-18 23:38:41 +00001236#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
1238template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001239inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001241 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1242 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243{
1244 this->set_rdbuf(&__sb_);
1245}
1246
1247template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249basic_ifstream<_CharT, _Traits>&
1250basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001252 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1253 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 return *this;
1255}
1256
Eric Fiselier78ccf772017-04-18 23:38:41 +00001257#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
1259template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001260inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261void
1262basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1263{
1264 basic_istream<char_type, traits_type>::swap(__rhs);
1265 __sb_.swap(__rhs.__sb_);
1266}
1267
1268template <class _CharT, class _Traits>
1269inline _LIBCPP_INLINE_VISIBILITY
1270void
1271swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1272{
1273 __x.swap(__y);
1274}
1275
1276template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001277inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278basic_filebuf<_CharT, _Traits>*
1279basic_ifstream<_CharT, _Traits>::rdbuf() const
1280{
1281 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1282}
1283
1284template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001285inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286bool
1287basic_ifstream<_CharT, _Traits>::is_open() const
1288{
1289 return __sb_.is_open();
1290}
1291
Ed Schouten7009f4e2015-03-12 15:44:39 +00001292#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293template <class _CharT, class _Traits>
1294void
1295basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1296{
1297 if (__sb_.open(__s, __mode | ios_base::in))
1298 this->clear();
1299 else
1300 this->setstate(ios_base::failbit);
1301}
1302
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001303#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1304template <class _CharT, class _Traits>
1305void
1306basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1307{
1308 if (__sb_.open(__s, __mode | ios_base::in))
1309 this->clear();
1310 else
1311 this->setstate(ios_base::failbit);
1312}
1313#endif
1314
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315template <class _CharT, class _Traits>
1316void
1317basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1318{
1319 if (__sb_.open(__s, __mode | ios_base::in))
1320 this->clear();
1321 else
1322 this->setstate(ios_base::failbit);
1323}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001324
1325template <class _CharT, class _Traits>
1326void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1327 ios_base::openmode __mode) {
1328 if (__sb_.__open(__fd, __mode | ios_base::in))
1329 this->clear();
1330 else
1331 this->setstate(ios_base::failbit);
1332}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001333#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
1335template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337void
1338basic_ifstream<_CharT, _Traits>::close()
1339{
1340 if (__sb_.close() == 0)
1341 this->setstate(ios_base::failbit);
1342}
1343
1344// basic_ofstream
1345
1346template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001347class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 : public basic_ostream<_CharT, _Traits>
1349{
1350public:
1351 typedef _CharT char_type;
1352 typedef _Traits traits_type;
1353 typedef typename traits_type::int_type int_type;
1354 typedef typename traits_type::pos_type pos_type;
1355 typedef typename traits_type::off_type off_type;
1356
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001361#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1362 _LIBCPP_INLINE_VISIBILITY
1363 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1364#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001367
1368#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001369 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001370 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1371 : basic_ofstream(__p.c_str(), __mode) {}
1372#endif // _LIBCPP_STD_VER >= 17
1373
Eric Fiselier78ccf772017-04-18 23:38:41 +00001374#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 basic_ofstream& operator=(basic_ofstream&& __rhs);
1380#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 void swap(basic_ofstream& __rhs);
1383
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001388#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001390#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1391 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001394
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001395#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001396 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001397 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1398 { return open(__p.c_str(), __mode); }
1399#endif // _LIBCPP_STD_VER >= 17
1400
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001401 _LIBCPP_INLINE_VISIBILITY
1402 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001403#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 void close();
1406
1407private:
1408 basic_filebuf<char_type, traits_type> __sb_;
1409};
1410
1411template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001412inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413basic_ofstream<_CharT, _Traits>::basic_ofstream()
1414 : basic_ostream<char_type, traits_type>(&__sb_)
1415{
1416}
1417
Ed Schouten7009f4e2015-03-12 15:44:39 +00001418#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001420inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __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)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 this->setstate(ios_base::failbit);
1426}
1427
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001428#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1429template <class _CharT, class _Traits>
1430inline
1431basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __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)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001435 this->setstate(ios_base::failbit);
1436}
1437#endif
1438
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(const string& __s, ios_base::openmode __mode)
1442 : basic_ostream<char_type, traits_type>(&__sb_)
1443{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001444 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 this->setstate(ios_base::failbit);
1446}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001447#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448
Eric Fiselier78ccf772017-04-18 23:38:41 +00001449#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
1451template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001452inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001454 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1455 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456{
1457 this->set_rdbuf(&__sb_);
1458}
1459
1460template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001461inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462basic_ofstream<_CharT, _Traits>&
1463basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1464{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001465 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1466 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467 return *this;
1468}
1469
Eric Fiselier78ccf772017-04-18 23:38:41 +00001470#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
1472template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001473inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474void
1475basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1476{
1477 basic_ostream<char_type, traits_type>::swap(__rhs);
1478 __sb_.swap(__rhs.__sb_);
1479}
1480
1481template <class _CharT, class _Traits>
1482inline _LIBCPP_INLINE_VISIBILITY
1483void
1484swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1485{
1486 __x.swap(__y);
1487}
1488
1489template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001490inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491basic_filebuf<_CharT, _Traits>*
1492basic_ofstream<_CharT, _Traits>::rdbuf() const
1493{
1494 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1495}
1496
1497template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001498inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499bool
1500basic_ofstream<_CharT, _Traits>::is_open() const
1501{
1502 return __sb_.is_open();
1503}
1504
Ed Schouten7009f4e2015-03-12 15:44:39 +00001505#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506template <class _CharT, class _Traits>
1507void
1508basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1509{
1510 if (__sb_.open(__s, __mode | ios_base::out))
1511 this->clear();
1512 else
1513 this->setstate(ios_base::failbit);
1514}
1515
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001516#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1517template <class _CharT, class _Traits>
1518void
1519basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1520{
1521 if (__sb_.open(__s, __mode | ios_base::out))
1522 this->clear();
1523 else
1524 this->setstate(ios_base::failbit);
1525}
1526#endif
1527
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528template <class _CharT, class _Traits>
1529void
1530basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1531{
1532 if (__sb_.open(__s, __mode | ios_base::out))
1533 this->clear();
1534 else
1535 this->setstate(ios_base::failbit);
1536}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001537
1538template <class _CharT, class _Traits>
1539void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1540 ios_base::openmode __mode) {
1541 if (__sb_.__open(__fd, __mode | ios_base::out))
1542 this->clear();
1543 else
1544 this->setstate(ios_base::failbit);
1545}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001546#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547
1548template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001549inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550void
1551basic_ofstream<_CharT, _Traits>::close()
1552{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001553 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 this->setstate(ios_base::failbit);
1555}
1556
1557// basic_fstream
1558
1559template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001560class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 : public basic_iostream<_CharT, _Traits>
1562{
1563public:
1564 typedef _CharT char_type;
1565 typedef _Traits traits_type;
1566 typedef typename traits_type::int_type int_type;
1567 typedef typename traits_type::pos_type pos_type;
1568 typedef typename traits_type::off_type off_type;
1569
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001572#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001575#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1576 _LIBCPP_INLINE_VISIBILITY
1577 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1578#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001581
1582#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001583 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001584 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1585 : basic_fstream(__p.c_str(), __mode) {}
1586#endif // _LIBCPP_STD_VER >= 17
1587
Ed Schouten7009f4e2015-03-12 15:44:39 +00001588#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001589#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 basic_fstream& operator=(basic_fstream&& __rhs);
1595#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 void swap(basic_fstream& __rhs);
1598
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001603#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001605#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1607#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001609
1610#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001611 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001612 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1613 { return open(__p.c_str(), __mode); }
1614#endif // _LIBCPP_STD_VER >= 17
1615
Ed Schouten7009f4e2015-03-12 15:44:39 +00001616#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 void close();
1619
1620private:
1621 basic_filebuf<char_type, traits_type> __sb_;
1622};
1623
1624template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001625inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626basic_fstream<_CharT, _Traits>::basic_fstream()
1627 : basic_iostream<char_type, traits_type>(&__sb_)
1628{
1629}
1630
Ed Schouten7009f4e2015-03-12 15:44:39 +00001631#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001633inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1635 : basic_iostream<char_type, traits_type>(&__sb_)
1636{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001637 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 this->setstate(ios_base::failbit);
1639}
1640
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001641#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1642template <class _CharT, class _Traits>
1643inline
1644basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1645 : basic_iostream<char_type, traits_type>(&__sb_)
1646{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001647 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001648 this->setstate(ios_base::failbit);
1649}
1650#endif
1651
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1655 : basic_iostream<char_type, traits_type>(&__sb_)
1656{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001657 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 this->setstate(ios_base::failbit);
1659}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001660#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661
Eric Fiselier78ccf772017-04-18 23:38:41 +00001662#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663
1664template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001665inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001667 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1668 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669{
1670 this->set_rdbuf(&__sb_);
1671}
1672
1673template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001674inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675basic_fstream<_CharT, _Traits>&
1676basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1677{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001678 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1679 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 return *this;
1681}
1682
Eric Fiselier78ccf772017-04-18 23:38:41 +00001683#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684
1685template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001686inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687void
1688basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1689{
1690 basic_iostream<char_type, traits_type>::swap(__rhs);
1691 __sb_.swap(__rhs.__sb_);
1692}
1693
1694template <class _CharT, class _Traits>
1695inline _LIBCPP_INLINE_VISIBILITY
1696void
1697swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1698{
1699 __x.swap(__y);
1700}
1701
1702template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704basic_filebuf<_CharT, _Traits>*
1705basic_fstream<_CharT, _Traits>::rdbuf() const
1706{
1707 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1708}
1709
1710template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001711inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712bool
1713basic_fstream<_CharT, _Traits>::is_open() const
1714{
1715 return __sb_.is_open();
1716}
1717
Ed Schouten7009f4e2015-03-12 15:44:39 +00001718#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719template <class _CharT, class _Traits>
1720void
1721basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1722{
1723 if (__sb_.open(__s, __mode))
1724 this->clear();
1725 else
1726 this->setstate(ios_base::failbit);
1727}
1728
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001729#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1730template <class _CharT, class _Traits>
1731void
1732basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1733{
1734 if (__sb_.open(__s, __mode))
1735 this->clear();
1736 else
1737 this->setstate(ios_base::failbit);
1738}
1739#endif
1740
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741template <class _CharT, class _Traits>
1742void
1743basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1744{
1745 if (__sb_.open(__s, __mode))
1746 this->clear();
1747 else
1748 this->setstate(ios_base::failbit);
1749}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001750#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751
1752template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001753inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754void
1755basic_fstream<_CharT, _Traits>::close()
1756{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001757 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 this->setstate(ios_base::failbit);
1759}
1760
Louis Dionne8a79be62020-10-21 11:11:45 -04001761#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1762_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1763_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1764_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1765#endif
1766
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767_LIBCPP_END_NAMESPACE_STD
1768
Eric Fiselierf4433a32017-05-31 22:07:49 +00001769_LIBCPP_POP_MACROS
1770
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771#endif // _LIBCPP_FSTREAM