blob: 5006923405d51de3c18a63c31fee972c6ec1c660 [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
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400201#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
202# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
203#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000204
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205_LIBCPP_BEGIN_NAMESPACE_STD
206
207template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000208class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209 : public basic_streambuf<_CharT, _Traits>
210{
211public:
212 typedef _CharT char_type;
213 typedef _Traits traits_type;
214 typedef typename traits_type::int_type int_type;
215 typedef typename traits_type::pos_type pos_type;
216 typedef typename traits_type::off_type off_type;
217 typedef typename traits_type::state_type state_type;
218
219 // 27.9.1.2 Constructors/destructor:
220 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 basic_filebuf(basic_filebuf&& __rhs);
223#endif
224 virtual ~basic_filebuf();
225
226 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000227#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 basic_filebuf& operator=(basic_filebuf&& __rhs);
230#endif
231 void swap(basic_filebuf& __rhs);
232
233 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000236#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000238#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
239 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
240#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000243
Louis Dionnedb84e122021-01-18 12:18:18 -0500244#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000245 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000246 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
247 return open(__p.c_str(), __mode);
248 }
249#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000251 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253 basic_filebuf* close();
254
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000255 _LIBCPP_INLINE_VISIBILITY
256 inline static const char*
257 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
258
259 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260 // 27.9.1.5 Overridden virtual functions:
261 virtual int_type underflow();
262 virtual int_type pbackfail(int_type __c = traits_type::eof());
263 virtual int_type overflow (int_type __c = traits_type::eof());
264 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
265 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
266 ios_base::openmode __wch = ios_base::in | ios_base::out);
267 virtual pos_type seekpos(pos_type __sp,
268 ios_base::openmode __wch = ios_base::in | ios_base::out);
269 virtual int sync();
270 virtual void imbue(const locale& __loc);
271
272private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000273 char* __extbuf_;
274 const char* __extbufnext_;
275 const char* __extbufend_;
276 char __extbuf_min_[8];
277 size_t __ebs_;
278 char_type* __intbuf_;
279 size_t __ibs_;
280 FILE* __file_;
281 const codecvt<char_type, char, state_type>* __cv_;
282 state_type __st_;
283 state_type __st_last_;
284 ios_base::openmode __om_;
285 ios_base::openmode __cm_;
286 bool __owns_eb_;
287 bool __owns_ib_;
288 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000290 bool __read_mode();
291 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292};
293
294template <class _CharT, class _Traits>
295basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500296 : __extbuf_(nullptr),
297 __extbufnext_(nullptr),
298 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500300 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500302 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000303 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000305 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 __om_(0),
307 __cm_(0),
308 __owns_eb_(false),
309 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000310 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311{
Howard Hinnant0090b122012-08-24 16:52:47 +0000312 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
313 {
314 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
315 __always_noconv_ = __cv_->always_noconv();
316 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500317 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318}
319
Eric Fiselier78ccf772017-04-18 23:38:41 +0000320#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
322template <class _CharT, class _Traits>
323basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
324 : basic_streambuf<_CharT, _Traits>(__rhs)
325{
326 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
327 {
328 __extbuf_ = __extbuf_min_;
329 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
330 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
331 }
332 else
333 {
334 __extbuf_ = __rhs.__extbuf_;
335 __extbufnext_ = __rhs.__extbufnext_;
336 __extbufend_ = __rhs.__extbufend_;
337 }
338 __ebs_ = __rhs.__ebs_;
339 __intbuf_ = __rhs.__intbuf_;
340 __ibs_ = __rhs.__ibs_;
341 __file_ = __rhs.__file_;
342 __cv_ = __rhs.__cv_;
343 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000344 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 __om_ = __rhs.__om_;
346 __cm_ = __rhs.__cm_;
347 __owns_eb_ = __rhs.__owns_eb_;
348 __owns_ib_ = __rhs.__owns_ib_;
349 __always_noconv_ = __rhs.__always_noconv_;
350 if (__rhs.pbase())
351 {
352 if (__rhs.pbase() == __rhs.__intbuf_)
353 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
354 else
355 this->setp((char_type*)__extbuf_,
356 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000357 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 }
359 else if (__rhs.eback())
360 {
361 if (__rhs.eback() == __rhs.__intbuf_)
362 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
363 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
364 else
365 this->setg((char_type*)__extbuf_,
366 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
367 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
368 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500369 __rhs.__extbuf_ = nullptr;
370 __rhs.__extbufnext_ = nullptr;
371 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__ebs_ = 0;
373 __rhs.__intbuf_ = 0;
374 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500375 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000377 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 __rhs.__om_ = 0;
379 __rhs.__cm_ = 0;
380 __rhs.__owns_eb_ = false;
381 __rhs.__owns_ib_ = false;
382 __rhs.setg(0, 0, 0);
383 __rhs.setp(0, 0);
384}
385
386template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388basic_filebuf<_CharT, _Traits>&
389basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
390{
391 close();
392 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000393 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394}
395
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400396#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template <class _CharT, class _Traits>
399basic_filebuf<_CharT, _Traits>::~basic_filebuf()
400{
401#ifndef _LIBCPP_NO_EXCEPTIONS
402 try
403 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 close();
406#ifndef _LIBCPP_NO_EXCEPTIONS
407 }
408 catch (...)
409 {
410 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400411#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 if (__owns_eb_)
413 delete [] __extbuf_;
414 if (__owns_ib_)
415 delete [] __intbuf_;
416}
417
418template <class _CharT, class _Traits>
419void
420basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
421{
422 basic_streambuf<char_type, traits_type>::swap(__rhs);
423 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
424 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000425 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
426 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
427 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 }
429 else
430 {
431 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
432 ptrdiff_t __le = __extbufend_ - __extbuf_;
433 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
434 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
435 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
436 {
437 __extbuf_ = __rhs.__extbuf_;
438 __rhs.__extbuf_ = __rhs.__extbuf_min_;
439 }
440 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
441 {
442 __rhs.__extbuf_ = __extbuf_;
443 __extbuf_ = __extbuf_min_;
444 }
445 __extbufnext_ = __extbuf_ + __rn;
446 __extbufend_ = __extbuf_ + __re;
447 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
448 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
449 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000450 _VSTD::swap(__ebs_, __rhs.__ebs_);
451 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
452 _VSTD::swap(__ibs_, __rhs.__ibs_);
453 _VSTD::swap(__file_, __rhs.__file_);
454 _VSTD::swap(__cv_, __rhs.__cv_);
455 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000456 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000457 _VSTD::swap(__om_, __rhs.__om_);
458 _VSTD::swap(__cm_, __rhs.__cm_);
459 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
460 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
461 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
463 {
464 ptrdiff_t __n = this->gptr() - this->eback();
465 ptrdiff_t __e = this->egptr() - this->eback();
466 this->setg((char_type*)__extbuf_min_,
467 (char_type*)__extbuf_min_ + __n,
468 (char_type*)__extbuf_min_ + __e);
469 }
470 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
471 {
472 ptrdiff_t __n = this->pptr() - this->pbase();
473 ptrdiff_t __e = this->epptr() - this->pbase();
474 this->setp((char_type*)__extbuf_min_,
475 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000476 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477 }
478 if (__rhs.eback() == (char_type*)__extbuf_min_)
479 {
480 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
481 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
482 __rhs.setg((char_type*)__rhs.__extbuf_min_,
483 (char_type*)__rhs.__extbuf_min_ + __n,
484 (char_type*)__rhs.__extbuf_min_ + __e);
485 }
486 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
487 {
488 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
489 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
490 __rhs.setp((char_type*)__rhs.__extbuf_min_,
491 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000492 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493 }
494}
495
496template <class _CharT, class _Traits>
497inline _LIBCPP_INLINE_VISIBILITY
498void
499swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
500{
501 __x.swap(__y);
502}
503
504template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000505inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506bool
507basic_filebuf<_CharT, _Traits>::is_open() const
508{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500509 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510}
511
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000512template <class _CharT, class _Traits>
513const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
514 ios_base::openmode __mode) _NOEXCEPT {
515 switch (__mode & ~ios_base::ate) {
516 case ios_base::out:
517 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000518 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000519 case ios_base::out | ios_base::app:
520 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000521 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000522 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000523 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000524 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000525 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000526 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::in | ios_base::out | ios_base::app:
529 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::out | ios_base::binary:
532 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::out | ios_base::app | ios_base::binary:
535 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000536 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000537 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000538 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000539 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000540 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000541 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000542 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000543 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
544 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000545 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000546 default:
547 return nullptr;
548 }
549 _LIBCPP_UNREACHABLE();
550}
551
Ed Schouten7009f4e2015-03-12 15:44:39 +0000552#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553template <class _CharT, class _Traits>
554basic_filebuf<_CharT, _Traits>*
555basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
556{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500557 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
558 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000560 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000562 __file_ = fopen(__s, __mdstr);
563 if (__file_) {
564 __om_ = __mode;
565 if (__mode & ios_base::ate) {
566 if (fseek(__file_, 0, SEEK_END)) {
567 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500568 __file_ = nullptr;
569 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000571 }
572 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500573 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000574 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 }
576 return __rt;
577}
578
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000579template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000580inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400581basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000582basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500583 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
584 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000585 if (const char* __mdstr = __make_mdstring(__mode)) {
586 __rt = this;
587 __file_ = fdopen(__fd, __mdstr);
588 if (__file_) {
589 __om_ = __mode;
590 if (__mode & ios_base::ate) {
591 if (fseek(__file_, 0, SEEK_END)) {
592 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500593 __file_ = nullptr;
594 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000595 }
596 }
597 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500598 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000599 }
600 }
601 return __rt;
602}
603
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000604#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
605// This is basically the same as the char* overload except that it uses _wfopen
606// and long mode strings.
607template <class _CharT, class _Traits>
608basic_filebuf<_CharT, _Traits>*
609basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
610{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500611 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
612 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000613 {
614 __rt = this;
615 const wchar_t* __mdstr;
616 switch (__mode & ~ios_base::ate)
617 {
618 case ios_base::out:
619 case ios_base::out | ios_base::trunc:
620 __mdstr = L"w";
621 break;
622 case ios_base::out | ios_base::app:
623 case ios_base::app:
624 __mdstr = L"a";
625 break;
626 case ios_base::in:
627 __mdstr = L"r";
628 break;
629 case ios_base::in | ios_base::out:
630 __mdstr = L"r+";
631 break;
632 case ios_base::in | ios_base::out | ios_base::trunc:
633 __mdstr = L"w+";
634 break;
635 case ios_base::in | ios_base::out | ios_base::app:
636 case ios_base::in | ios_base::app:
637 __mdstr = L"a+";
638 break;
639 case ios_base::out | ios_base::binary:
640 case ios_base::out | ios_base::trunc | ios_base::binary:
641 __mdstr = L"wb";
642 break;
643 case ios_base::out | ios_base::app | ios_base::binary:
644 case ios_base::app | ios_base::binary:
645 __mdstr = L"ab";
646 break;
647 case ios_base::in | ios_base::binary:
648 __mdstr = L"rb";
649 break;
650 case ios_base::in | ios_base::out | ios_base::binary:
651 __mdstr = L"r+b";
652 break;
653 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
654 __mdstr = L"w+b";
655 break;
656 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
657 case ios_base::in | ios_base::app | ios_base::binary:
658 __mdstr = L"a+b";
659 break;
660 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500661 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000662 break;
663 }
664 if (__rt)
665 {
666 __file_ = _wfopen(__s, __mdstr);
667 if (__file_)
668 {
669 __om_ = __mode;
670 if (__mode & ios_base::ate)
671 {
672 if (fseek(__file_, 0, SEEK_END))
673 {
674 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500675 __file_ = nullptr;
676 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000677 }
678 }
679 }
680 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500681 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000682 }
683 }
684 return __rt;
685}
686#endif
687
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690basic_filebuf<_CharT, _Traits>*
691basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
692{
693 return open(__s.c_str(), __mode);
694}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000695#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696
697template <class _CharT, class _Traits>
698basic_filebuf<_CharT, _Traits>*
699basic_filebuf<_CharT, _Traits>::close()
700{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500701 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 if (__file_)
703 {
704 __rt = this;
705 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000706 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500707 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000708 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500709 __rt = nullptr;
710 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000711 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 }
713 return __rt;
714}
715
716template <class _CharT, class _Traits>
717typename basic_filebuf<_CharT, _Traits>::int_type
718basic_filebuf<_CharT, _Traits>::underflow()
719{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500720 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 return traits_type::eof();
722 bool __initial = __read_mode();
723 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500724 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 this->setg(&__1buf, &__1buf+1, &__1buf+1);
726 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
727 int_type __c = traits_type::eof();
728 if (this->gptr() == this->egptr())
729 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500730 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 if (__always_noconv_)
732 {
733 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
734 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
735 if (__nmemb != 0)
736 {
737 this->setg(this->eback(),
738 this->eback() + __unget_sz,
739 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000740 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 }
742 }
743 else
744 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000745 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
746 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500747 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
749 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000750 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 static_cast<size_t>(__extbufend_ - __extbufnext_));
752 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000753 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000754 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 if (__nr != 0)
756 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000757 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000758 __throw_bad_cast();
759
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 __extbufend_ = __extbufnext_ + __nr;
761 char_type* __inext;
762 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
763 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000764 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 if (__r == codecvt_base::noconv)
766 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000767 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000768 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000769 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 }
771 else if (__inext != this->eback() + __unget_sz)
772 {
773 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000774 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 }
776 }
777 }
778 }
779 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000780 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500782 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 return __c;
784}
785
786template <class _CharT, class _Traits>
787typename basic_filebuf<_CharT, _Traits>::int_type
788basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
789{
790 if (__file_ && this->eback() < this->gptr())
791 {
792 if (traits_type::eq_int_type(__c, traits_type::eof()))
793 {
794 this->gbump(-1);
795 return traits_type::not_eof(__c);
796 }
797 if ((__om_ & ios_base::out) ||
798 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
799 {
800 this->gbump(-1);
801 *this->gptr() = traits_type::to_char_type(__c);
802 return __c;
803 }
804 }
805 return traits_type::eof();
806}
807
808template <class _CharT, class _Traits>
809typename basic_filebuf<_CharT, _Traits>::int_type
810basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
811{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500812 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 return traits_type::eof();
814 __write_mode();
815 char_type __1buf;
816 char_type* __pb_save = this->pbase();
817 char_type* __epb_save = this->epptr();
818 if (!traits_type::eq_int_type(__c, traits_type::eof()))
819 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500820 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 this->setp(&__1buf, &__1buf+1);
822 *this->pptr() = traits_type::to_char_type(__c);
823 this->pbump(1);
824 }
825 if (this->pptr() != this->pbase())
826 {
827 if (__always_noconv_)
828 {
829 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
830 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
831 return traits_type::eof();
832 }
833 else
834 {
835 char* __extbe = __extbuf_;
836 codecvt_base::result __r;
837 do
838 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000839 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000840 __throw_bad_cast();
841
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 const char_type* __e;
843 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
844 __extbuf_, __extbuf_ + __ebs_, __extbe);
845 if (__e == this->pbase())
846 return traits_type::eof();
847 if (__r == codecvt_base::noconv)
848 {
849 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
850 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
851 return traits_type::eof();
852 }
853 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
854 {
855 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
856 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
857 return traits_type::eof();
858 if (__r == codecvt_base::partial)
859 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000860 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000861 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 }
863 }
864 else
865 return traits_type::eof();
866 } while (__r == codecvt_base::partial);
867 }
868 this->setp(__pb_save, __epb_save);
869 }
870 return traits_type::not_eof(__c);
871}
872
873template <class _CharT, class _Traits>
874basic_streambuf<_CharT, _Traits>*
875basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
876{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500877 this->setg(nullptr, nullptr, nullptr);
878 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 if (__owns_eb_)
880 delete [] __extbuf_;
881 if (__owns_ib_)
882 delete [] __intbuf_;
883 __ebs_ = __n;
884 if (__ebs_ > sizeof(__extbuf_min_))
885 {
886 if (__always_noconv_ && __s)
887 {
888 __extbuf_ = (char*)__s;
889 __owns_eb_ = false;
890 }
891 else
892 {
893 __extbuf_ = new char[__ebs_];
894 __owns_eb_ = true;
895 }
896 }
897 else
898 {
899 __extbuf_ = __extbuf_min_;
900 __ebs_ = sizeof(__extbuf_min_);
901 __owns_eb_ = false;
902 }
903 if (!__always_noconv_)
904 {
905 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
906 if (__s && __ibs_ >= sizeof(__extbuf_min_))
907 {
908 __intbuf_ = __s;
909 __owns_ib_ = false;
910 }
911 else
912 {
913 __intbuf_ = new char_type[__ibs_];
914 __owns_ib_ = true;
915 }
916 }
917 else
918 {
919 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500920 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921 __owns_ib_ = false;
922 }
923 return this;
924}
925
926template <class _CharT, class _Traits>
927typename basic_filebuf<_CharT, _Traits>::pos_type
928basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
929 ios_base::openmode)
930{
Howard Hinnant0090b122012-08-24 16:52:47 +0000931 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000932 __throw_bad_cast();
933
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500935 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 return pos_type(off_type(-1));
937 // __width > 0 || __off == 0
938 int __whence;
939 switch (__way)
940 {
941 case ios_base::beg:
942 __whence = SEEK_SET;
943 break;
944 case ios_base::cur:
945 __whence = SEEK_CUR;
946 break;
947 case ios_base::end:
948 __whence = SEEK_END;
949 break;
950 default:
951 return pos_type(off_type(-1));
952 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000953#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000954 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
955 return pos_type(off_type(-1));
956 pos_type __r = ftell(__file_);
957#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
959 return pos_type(off_type(-1));
960 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962 __r.state(__st_);
963 return __r;
964}
965
966template <class _CharT, class _Traits>
967typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000968basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500970 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000972#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000973 if (fseek(__file_, __sp, SEEK_SET))
974 return pos_type(off_type(-1));
975#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 if (fseeko(__file_, __sp, SEEK_SET))
977 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000978#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000979 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 return __sp;
981}
982
983template <class _CharT, class _Traits>
984int
985basic_filebuf<_CharT, _Traits>::sync()
986{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500987 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000989 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000990 __throw_bad_cast();
991
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 if (__cm_ & ios_base::out)
993 {
994 if (this->pptr() != this->pbase())
995 if (overflow() == traits_type::eof())
996 return -1;
997 codecvt_base::result __r;
998 do
999 {
1000 char* __extbe;
1001 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1002 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1003 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1004 return -1;
1005 } while (__r == codecvt_base::partial);
1006 if (__r == codecvt_base::error)
1007 return -1;
1008 if (fflush(__file_))
1009 return -1;
1010 }
1011 else if (__cm_ & ios_base::in)
1012 {
1013 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001014 state_type __state = __st_last_;
1015 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 if (__always_noconv_)
1017 __c = this->egptr() - this->gptr();
1018 else
1019 {
1020 int __width = __cv_->encoding();
1021 __c = __extbufend_ - __extbufnext_;
1022 if (__width > 0)
1023 __c += __width * (this->egptr() - this->gptr());
1024 else
1025 {
1026 if (this->gptr() != this->egptr())
1027 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001028 const int __off = __cv_->length(__state, __extbuf_,
1029 __extbufnext_,
1030 this->gptr() - this->eback());
1031 __c += __extbufnext_ - __extbuf_ - __off;
1032 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 }
1034 }
1035 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001036#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001037 if (fseek(__file_, -__c, SEEK_CUR))
1038 return -1;
1039#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 if (fseeko(__file_, -__c, SEEK_CUR))
1041 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001042#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001043 if (__update_st)
1044 __st_ = __state;
1045 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001046 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 __cm_ = 0;
1048 }
1049 return 0;
1050}
1051
1052template <class _CharT, class _Traits>
1053void
1054basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1055{
1056 sync();
1057 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1058 bool __old_anc = __always_noconv_;
1059 __always_noconv_ = __cv_->always_noconv();
1060 if (__old_anc != __always_noconv_)
1061 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001062 this->setg(nullptr, nullptr, nullptr);
1063 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 // invariant, char_type is char, else we couldn't get here
1065 if (__always_noconv_) // need to dump __intbuf_
1066 {
1067 if (__owns_eb_)
1068 delete [] __extbuf_;
1069 __owns_eb_ = __owns_ib_;
1070 __ebs_ = __ibs_;
1071 __extbuf_ = (char*)__intbuf_;
1072 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001073 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 __owns_ib_ = false;
1075 }
1076 else // need to obtain an __intbuf_.
1077 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1078 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1079 {
1080 __ibs_ = __ebs_;
1081 __intbuf_ = (char_type*)__extbuf_;
1082 __owns_ib_ = false;
1083 __extbuf_ = new char[__ebs_];
1084 __owns_eb_ = true;
1085 }
1086 else
1087 {
1088 __ibs_ = __ebs_;
1089 __intbuf_ = new char_type[__ibs_];
1090 __owns_ib_ = true;
1091 }
1092 }
1093 }
1094}
1095
1096template <class _CharT, class _Traits>
1097bool
1098basic_filebuf<_CharT, _Traits>::__read_mode()
1099{
1100 if (!(__cm_ & ios_base::in))
1101 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001102 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 if (__always_noconv_)
1104 this->setg((char_type*)__extbuf_,
1105 (char_type*)__extbuf_ + __ebs_,
1106 (char_type*)__extbuf_ + __ebs_);
1107 else
1108 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1109 __cm_ = ios_base::in;
1110 return true;
1111 }
1112 return false;
1113}
1114
1115template <class _CharT, class _Traits>
1116void
1117basic_filebuf<_CharT, _Traits>::__write_mode()
1118{
1119 if (!(__cm_ & ios_base::out))
1120 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001121 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 if (__ebs_ > sizeof(__extbuf_min_))
1123 {
1124 if (__always_noconv_)
1125 this->setp((char_type*)__extbuf_,
1126 (char_type*)__extbuf_ + (__ebs_ - 1));
1127 else
1128 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1129 }
1130 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001131 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 __cm_ = ios_base::out;
1133 }
1134}
1135
1136// basic_ifstream
1137
1138template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001139class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 : public basic_istream<_CharT, _Traits>
1141{
1142public:
1143 typedef _CharT char_type;
1144 typedef _Traits traits_type;
1145 typedef typename traits_type::int_type int_type;
1146 typedef typename traits_type::pos_type pos_type;
1147 typedef typename traits_type::off_type off_type;
1148
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001151#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001154#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1155 _LIBCPP_INLINE_VISIBILITY
1156 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1157#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001160#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001161 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001162 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1163 : basic_ifstream(__p.c_str(), __mode) {}
1164#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001165#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001166#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 basic_ifstream& operator=(basic_ifstream&& __rhs);
1172#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 void swap(basic_ifstream& __rhs);
1175
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001180#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001182#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1183 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1184#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001186#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001187 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001188 void open(const filesystem::path& __p,
1189 ios_base::openmode __mode = ios_base::in) {
1190 return open(__p.c_str(), __mode);
1191 }
1192#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001193
1194 _LIBCPP_INLINE_VISIBILITY
1195 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001196#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 void close();
1199
1200private:
1201 basic_filebuf<char_type, traits_type> __sb_;
1202};
1203
1204template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206basic_ifstream<_CharT, _Traits>::basic_ifstream()
1207 : basic_istream<char_type, traits_type>(&__sb_)
1208{
1209}
1210
Ed Schouten7009f4e2015-03-12 15:44:39 +00001211#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1215 : basic_istream<char_type, traits_type>(&__sb_)
1216{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001217 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 this->setstate(ios_base::failbit);
1219}
1220
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001221#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1222template <class _CharT, class _Traits>
1223inline
1224basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1225 : basic_istream<char_type, traits_type>(&__sb_)
1226{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001227 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001228 this->setstate(ios_base::failbit);
1229}
1230#endif
1231
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1235 : basic_istream<char_type, traits_type>(&__sb_)
1236{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001237 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 this->setstate(ios_base::failbit);
1239}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Eric Fiselier78ccf772017-04-18 23:38:41 +00001242#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
1244template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001247 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1248 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249{
1250 this->set_rdbuf(&__sb_);
1251}
1252
1253template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001254inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255basic_ifstream<_CharT, _Traits>&
1256basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001258 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1259 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 return *this;
1261}
1262
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001263#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
1265template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001266inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267void
1268basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1269{
1270 basic_istream<char_type, traits_type>::swap(__rhs);
1271 __sb_.swap(__rhs.__sb_);
1272}
1273
1274template <class _CharT, class _Traits>
1275inline _LIBCPP_INLINE_VISIBILITY
1276void
1277swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1278{
1279 __x.swap(__y);
1280}
1281
1282template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001283inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284basic_filebuf<_CharT, _Traits>*
1285basic_ifstream<_CharT, _Traits>::rdbuf() const
1286{
1287 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1288}
1289
1290template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001291inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292bool
1293basic_ifstream<_CharT, _Traits>::is_open() const
1294{
1295 return __sb_.is_open();
1296}
1297
Ed Schouten7009f4e2015-03-12 15:44:39 +00001298#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299template <class _CharT, class _Traits>
1300void
1301basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1302{
1303 if (__sb_.open(__s, __mode | ios_base::in))
1304 this->clear();
1305 else
1306 this->setstate(ios_base::failbit);
1307}
1308
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001309#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1310template <class _CharT, class _Traits>
1311void
1312basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1313{
1314 if (__sb_.open(__s, __mode | ios_base::in))
1315 this->clear();
1316 else
1317 this->setstate(ios_base::failbit);
1318}
1319#endif
1320
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321template <class _CharT, class _Traits>
1322void
1323basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1324{
1325 if (__sb_.open(__s, __mode | ios_base::in))
1326 this->clear();
1327 else
1328 this->setstate(ios_base::failbit);
1329}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001330
1331template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001332inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001333void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1334 ios_base::openmode __mode) {
1335 if (__sb_.__open(__fd, __mode | ios_base::in))
1336 this->clear();
1337 else
1338 this->setstate(ios_base::failbit);
1339}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001340#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341
1342template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001343inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344void
1345basic_ifstream<_CharT, _Traits>::close()
1346{
1347 if (__sb_.close() == 0)
1348 this->setstate(ios_base::failbit);
1349}
1350
1351// basic_ofstream
1352
1353template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001354class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 : public basic_ostream<_CharT, _Traits>
1356{
1357public:
1358 typedef _CharT char_type;
1359 typedef _Traits traits_type;
1360 typedef typename traits_type::int_type int_type;
1361 typedef typename traits_type::pos_type pos_type;
1362 typedef typename traits_type::off_type off_type;
1363
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001368#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1369 _LIBCPP_INLINE_VISIBILITY
1370 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1371#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001374
Louis Dionnedb84e122021-01-18 12:18:18 -05001375#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001376 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001377 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1378 : basic_ofstream(__p.c_str(), __mode) {}
1379#endif // _LIBCPP_STD_VER >= 17
1380
Eric Fiselier78ccf772017-04-18 23:38:41 +00001381#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 basic_ofstream& operator=(basic_ofstream&& __rhs);
1387#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 void swap(basic_ofstream& __rhs);
1390
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001395#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001397#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1398 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001401
Louis Dionnedb84e122021-01-18 12:18:18 -05001402#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001403 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001404 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1405 { return open(__p.c_str(), __mode); }
1406#endif // _LIBCPP_STD_VER >= 17
1407
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001408 _LIBCPP_INLINE_VISIBILITY
1409 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001410#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 void close();
1413
1414private:
1415 basic_filebuf<char_type, traits_type> __sb_;
1416};
1417
1418template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001419inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420basic_ofstream<_CharT, _Traits>::basic_ofstream()
1421 : basic_ostream<char_type, traits_type>(&__sb_)
1422{
1423}
1424
Ed Schouten7009f4e2015-03-12 15:44:39 +00001425#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001427inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1429 : basic_ostream<char_type, traits_type>(&__sb_)
1430{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001431 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 this->setstate(ios_base::failbit);
1433}
1434
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001435#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1436template <class _CharT, class _Traits>
1437inline
1438basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1439 : basic_ostream<char_type, traits_type>(&__sb_)
1440{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001441 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001442 this->setstate(ios_base::failbit);
1443}
1444#endif
1445
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001447inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1449 : basic_ostream<char_type, traits_type>(&__sb_)
1450{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001451 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 this->setstate(ios_base::failbit);
1453}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001454#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455
Eric Fiselier78ccf772017-04-18 23:38:41 +00001456#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001461 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1462 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463{
1464 this->set_rdbuf(&__sb_);
1465}
1466
1467template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001468inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469basic_ofstream<_CharT, _Traits>&
1470basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1471{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001472 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1473 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 return *this;
1475}
1476
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001477#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478
1479template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001480inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481void
1482basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1483{
1484 basic_ostream<char_type, traits_type>::swap(__rhs);
1485 __sb_.swap(__rhs.__sb_);
1486}
1487
1488template <class _CharT, class _Traits>
1489inline _LIBCPP_INLINE_VISIBILITY
1490void
1491swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1492{
1493 __x.swap(__y);
1494}
1495
1496template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001497inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498basic_filebuf<_CharT, _Traits>*
1499basic_ofstream<_CharT, _Traits>::rdbuf() const
1500{
1501 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1502}
1503
1504template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001505inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506bool
1507basic_ofstream<_CharT, _Traits>::is_open() const
1508{
1509 return __sb_.is_open();
1510}
1511
Ed Schouten7009f4e2015-03-12 15:44:39 +00001512#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513template <class _CharT, class _Traits>
1514void
1515basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1516{
1517 if (__sb_.open(__s, __mode | ios_base::out))
1518 this->clear();
1519 else
1520 this->setstate(ios_base::failbit);
1521}
1522
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001523#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1524template <class _CharT, class _Traits>
1525void
1526basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1527{
1528 if (__sb_.open(__s, __mode | ios_base::out))
1529 this->clear();
1530 else
1531 this->setstate(ios_base::failbit);
1532}
1533#endif
1534
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535template <class _CharT, class _Traits>
1536void
1537basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1538{
1539 if (__sb_.open(__s, __mode | ios_base::out))
1540 this->clear();
1541 else
1542 this->setstate(ios_base::failbit);
1543}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001544
1545template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001546inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001547void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1548 ios_base::openmode __mode) {
1549 if (__sb_.__open(__fd, __mode | ios_base::out))
1550 this->clear();
1551 else
1552 this->setstate(ios_base::failbit);
1553}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001554#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555
1556template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001557inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558void
1559basic_ofstream<_CharT, _Traits>::close()
1560{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001561 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 this->setstate(ios_base::failbit);
1563}
1564
1565// basic_fstream
1566
1567template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001568class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 : public basic_iostream<_CharT, _Traits>
1570{
1571public:
1572 typedef _CharT char_type;
1573 typedef _Traits traits_type;
1574 typedef typename traits_type::int_type int_type;
1575 typedef typename traits_type::pos_type pos_type;
1576 typedef typename traits_type::off_type off_type;
1577
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001580#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001583#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1584 _LIBCPP_INLINE_VISIBILITY
1585 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1586#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001589
Louis Dionnedb84e122021-01-18 12:18:18 -05001590#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001591 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001592 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1593 : basic_fstream(__p.c_str(), __mode) {}
1594#endif // _LIBCPP_STD_VER >= 17
1595
Ed Schouten7009f4e2015-03-12 15:44:39 +00001596#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001597#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 basic_fstream& operator=(basic_fstream&& __rhs);
1603#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 void swap(basic_fstream& __rhs);
1606
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001611#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001613#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1614 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1615#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001617
Louis Dionnedb84e122021-01-18 12:18:18 -05001618#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001619 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001620 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1621 { return open(__p.c_str(), __mode); }
1622#endif // _LIBCPP_STD_VER >= 17
1623
Ed Schouten7009f4e2015-03-12 15:44:39 +00001624#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 void close();
1627
1628private:
1629 basic_filebuf<char_type, traits_type> __sb_;
1630};
1631
1632template <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()
1635 : basic_iostream<char_type, traits_type>(&__sb_)
1636{
1637}
1638
Ed Schouten7009f4e2015-03-12 15:44:39 +00001639#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001641inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1643 : basic_iostream<char_type, traits_type>(&__sb_)
1644{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001645 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 this->setstate(ios_base::failbit);
1647}
1648
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001649#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1650template <class _CharT, class _Traits>
1651inline
1652basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1653 : basic_iostream<char_type, traits_type>(&__sb_)
1654{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001655 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001656 this->setstate(ios_base::failbit);
1657}
1658#endif
1659
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001661inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1663 : basic_iostream<char_type, traits_type>(&__sb_)
1664{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001665 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666 this->setstate(ios_base::failbit);
1667}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001668#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669
Eric Fiselier78ccf772017-04-18 23:38:41 +00001670#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671
1672template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001673inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001675 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1676 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677{
1678 this->set_rdbuf(&__sb_);
1679}
1680
1681template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683basic_fstream<_CharT, _Traits>&
1684basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1685{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001686 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1687 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 return *this;
1689}
1690
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001691#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692
1693template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001694inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695void
1696basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1697{
1698 basic_iostream<char_type, traits_type>::swap(__rhs);
1699 __sb_.swap(__rhs.__sb_);
1700}
1701
1702template <class _CharT, class _Traits>
1703inline _LIBCPP_INLINE_VISIBILITY
1704void
1705swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1706{
1707 __x.swap(__y);
1708}
1709
1710template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001711inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712basic_filebuf<_CharT, _Traits>*
1713basic_fstream<_CharT, _Traits>::rdbuf() const
1714{
1715 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1716}
1717
1718template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001719inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720bool
1721basic_fstream<_CharT, _Traits>::is_open() const
1722{
1723 return __sb_.is_open();
1724}
1725
Ed Schouten7009f4e2015-03-12 15:44:39 +00001726#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727template <class _CharT, class _Traits>
1728void
1729basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1730{
1731 if (__sb_.open(__s, __mode))
1732 this->clear();
1733 else
1734 this->setstate(ios_base::failbit);
1735}
1736
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001737#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1738template <class _CharT, class _Traits>
1739void
1740basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1741{
1742 if (__sb_.open(__s, __mode))
1743 this->clear();
1744 else
1745 this->setstate(ios_base::failbit);
1746}
1747#endif
1748
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749template <class _CharT, class _Traits>
1750void
1751basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1752{
1753 if (__sb_.open(__s, __mode))
1754 this->clear();
1755 else
1756 this->setstate(ios_base::failbit);
1757}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001758#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759
1760template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001761inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762void
1763basic_fstream<_CharT, _Traits>::close()
1764{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001765 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 this->setstate(ios_base::failbit);
1767}
1768
Louis Dionne8a79be62020-10-21 11:11:45 -04001769#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1770_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1771_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1772_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1773#endif
1774
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775_LIBCPP_END_NAMESPACE_STD
1776
Eric Fiselierf4433a32017-05-31 22:07:49 +00001777_LIBCPP_POP_MACROS
1778
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001779#endif // _LIBCPP_FSTREAM