blob: 6e84614ec1057694b1e3ade2bad2e7f979042ee3 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- fstream ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FSTREAM
11#define _LIBCPP_FSTREAM
12
13/*
14 fstream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_filebuf
18 : public basic_streambuf<charT, traits>
19{
20public:
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
26
27 // 27.9.1.2 Constructors/destructor:
28 basic_filebuf();
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
31
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
35
36 // 27.9.1.4 Members:
37 bool is_open() const;
38 basic_filebuf* open(const char* s, ios_base::openmode mode);
39 basic_filebuf* open(const string& s, ios_base::openmode mode);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000040 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 basic_filebuf* close();
42
43protected:
44 // 27.9.1.5 Overridden virtual functions:
45 virtual streamsize showmanyc();
46 virtual int_type underflow();
47 virtual int_type uflow();
48 virtual int_type pbackfail(int_type c = traits_type::eof());
49 virtual int_type overflow (int_type c = traits_type::eof());
50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52 ios_base::openmode which = ios_base::in | ios_base::out);
53 virtual pos_type seekpos(pos_type sp,
54 ios_base::openmode which = ios_base::in | ios_base::out);
55 virtual int sync();
56 virtual void imbue(const locale& loc);
57};
58
59template <class charT, class traits>
60 void
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63typedef basic_filebuf<char> filebuf;
64typedef basic_filebuf<wchar_t> wfilebuf;
65
66template <class charT, class traits = char_traits<charT> >
67class basic_ifstream
68 : public basic_istream<charT,traits>
69{
70public:
71 typedef charT char_type;
72 typedef traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
76
77 basic_ifstream();
78 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
79 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000080 explicit basic_ifstream(const filesystem::path& p,
81 ios_base::openmode mode = ios_base::in); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 basic_ifstream(basic_ifstream&& rhs);
83
84 basic_ifstream& operator=(basic_ifstream&& rhs);
85 void swap(basic_ifstream& rhs);
86
87 basic_filebuf<char_type, traits_type>* rdbuf() const;
88 bool is_open() const;
89 void open(const char* s, ios_base::openmode mode = ios_base::in);
90 void open(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000091 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 void close();
94};
95
96template <class charT, class traits>
97 void
98 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
99
100typedef basic_ifstream<char> ifstream;
101typedef basic_ifstream<wchar_t> wifstream;
102
103template <class charT, class traits = char_traits<charT> >
104class basic_ofstream
105 : public basic_ostream<charT,traits>
106{
107public:
108 typedef charT char_type;
109 typedef traits traits_type;
110 typedef typename traits_type::int_type int_type;
111 typedef typename traits_type::pos_type pos_type;
112 typedef typename traits_type::off_type off_type;
113
114 basic_ofstream();
115 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
116 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000117 explicit basic_ofstream(const filesystem::path& p,
118 ios_base::openmode mode = ios_base::out); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 basic_ofstream(basic_ofstream&& rhs);
120
121 basic_ofstream& operator=(basic_ofstream&& rhs);
122 void swap(basic_ofstream& rhs);
123
124 basic_filebuf<char_type, traits_type>* rdbuf() const;
125 bool is_open() const;
126 void open(const char* s, ios_base::openmode mode = ios_base::out);
127 void open(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000128 void open(const filesystem::path& p,
129 ios_base::openmode mode = ios_base::out); // C++17
130
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 void close();
132};
133
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000134template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 void
136 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
137
138typedef basic_ofstream<char> ofstream;
139typedef basic_ofstream<wchar_t> wofstream;
140
141template <class charT, class traits=char_traits<charT> >
142class basic_fstream
143 : public basic_iostream<charT,traits>
144{
145public:
146 typedef charT char_type;
147 typedef traits traits_type;
148 typedef typename traits_type::int_type int_type;
149 typedef typename traits_type::pos_type pos_type;
150 typedef typename traits_type::off_type off_type;
151
152 basic_fstream();
153 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000155 explicit basic_fstream(const filesystem::path& p,
156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 basic_fstream(basic_fstream&& rhs);
158
159 basic_fstream& operator=(basic_fstream&& rhs);
160 void swap(basic_fstream& rhs);
161
162 basic_filebuf<char_type, traits_type>* rdbuf() const;
163 bool is_open() const;
164 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
165 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000166 void open(const filesystem::path& s,
167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
168
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 void close();
170};
171
172template <class charT, class traits>
173 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
174
175typedef basic_fstream<char> fstream;
176typedef basic_fstream<wchar_t> wfstream;
177
178} // std
179
180*/
181
Louis Dionne73912b22020-11-04 15:01:25 -0500182#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400183#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400184#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185#include <__locale>
186#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000187#include <cstdlib>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400188#include <istream>
189#include <ostream>
Louis Dionnedb84e122021-01-18 12:18:18 -0500190
191#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
192# include <filesystem>
193#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000195#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198
Eric Fiselierf4433a32017-05-31 22:07:49 +0000199_LIBCPP_PUSH_MACROS
200#include <__undef_macros>
201
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400202#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
203# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
204#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000205
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206_LIBCPP_BEGIN_NAMESPACE_STD
207
208template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000209class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 : public basic_streambuf<_CharT, _Traits>
211{
212public:
213 typedef _CharT char_type;
214 typedef _Traits traits_type;
215 typedef typename traits_type::int_type int_type;
216 typedef typename traits_type::pos_type pos_type;
217 typedef typename traits_type::off_type off_type;
218 typedef typename traits_type::state_type state_type;
219
220 // 27.9.1.2 Constructors/destructor:
221 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 basic_filebuf(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 virtual ~basic_filebuf();
224
225 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 void swap(basic_filebuf& __rhs);
229
230 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 bool is_open() const;
233 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000234#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
235 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
236#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000239
Louis Dionnedb84e122021-01-18 12:18:18 -0500240#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000241 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000242 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
243 return open(__p.c_str(), __mode);
244 }
245#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000246 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000247 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 basic_filebuf* close();
249
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000250 _LIBCPP_INLINE_VISIBILITY
251 inline static const char*
252 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
253
254 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255 // 27.9.1.5 Overridden virtual functions:
256 virtual int_type underflow();
257 virtual int_type pbackfail(int_type __c = traits_type::eof());
258 virtual int_type overflow (int_type __c = traits_type::eof());
259 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
260 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
261 ios_base::openmode __wch = ios_base::in | ios_base::out);
262 virtual pos_type seekpos(pos_type __sp,
263 ios_base::openmode __wch = ios_base::in | ios_base::out);
264 virtual int sync();
265 virtual void imbue(const locale& __loc);
266
267private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000268 char* __extbuf_;
269 const char* __extbufnext_;
270 const char* __extbufend_;
271 char __extbuf_min_[8];
272 size_t __ebs_;
273 char_type* __intbuf_;
274 size_t __ibs_;
275 FILE* __file_;
276 const codecvt<char_type, char, state_type>* __cv_;
277 state_type __st_;
278 state_type __st_last_;
279 ios_base::openmode __om_;
280 ios_base::openmode __cm_;
281 bool __owns_eb_;
282 bool __owns_ib_;
283 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000285 bool __read_mode();
286 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287};
288
289template <class _CharT, class _Traits>
290basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500291 : __extbuf_(nullptr),
292 __extbufnext_(nullptr),
293 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500295 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500297 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000298 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000300 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __om_(0),
302 __cm_(0),
303 __owns_eb_(false),
304 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000305 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306{
Howard Hinnant0090b122012-08-24 16:52:47 +0000307 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
308 {
309 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
310 __always_noconv_ = __cv_->always_noconv();
311 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500312 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313}
314
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315template <class _CharT, class _Traits>
316basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
317 : basic_streambuf<_CharT, _Traits>(__rhs)
318{
319 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
320 {
321 __extbuf_ = __extbuf_min_;
322 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
323 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
324 }
325 else
326 {
327 __extbuf_ = __rhs.__extbuf_;
328 __extbufnext_ = __rhs.__extbufnext_;
329 __extbufend_ = __rhs.__extbufend_;
330 }
331 __ebs_ = __rhs.__ebs_;
332 __intbuf_ = __rhs.__intbuf_;
333 __ibs_ = __rhs.__ibs_;
334 __file_ = __rhs.__file_;
335 __cv_ = __rhs.__cv_;
336 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000337 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 __om_ = __rhs.__om_;
339 __cm_ = __rhs.__cm_;
340 __owns_eb_ = __rhs.__owns_eb_;
341 __owns_ib_ = __rhs.__owns_ib_;
342 __always_noconv_ = __rhs.__always_noconv_;
343 if (__rhs.pbase())
344 {
345 if (__rhs.pbase() == __rhs.__intbuf_)
346 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
347 else
348 this->setp((char_type*)__extbuf_,
349 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000350 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351 }
352 else if (__rhs.eback())
353 {
354 if (__rhs.eback() == __rhs.__intbuf_)
355 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
356 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
357 else
358 this->setg((char_type*)__extbuf_,
359 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
360 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
361 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500362 __rhs.__extbuf_ = nullptr;
363 __rhs.__extbufnext_ = nullptr;
364 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 __rhs.__ebs_ = 0;
366 __rhs.__intbuf_ = 0;
367 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500368 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000370 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 __rhs.__om_ = 0;
372 __rhs.__cm_ = 0;
373 __rhs.__owns_eb_ = false;
374 __rhs.__owns_ib_ = false;
375 __rhs.setg(0, 0, 0);
376 __rhs.setp(0, 0);
377}
378
379template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381basic_filebuf<_CharT, _Traits>&
382basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
383{
384 close();
385 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000386 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387}
388
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389template <class _CharT, class _Traits>
390basic_filebuf<_CharT, _Traits>::~basic_filebuf()
391{
392#ifndef _LIBCPP_NO_EXCEPTIONS
393 try
394 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400395#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 close();
397#ifndef _LIBCPP_NO_EXCEPTIONS
398 }
399 catch (...)
400 {
401 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400402#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 if (__owns_eb_)
404 delete [] __extbuf_;
405 if (__owns_ib_)
406 delete [] __intbuf_;
407}
408
409template <class _CharT, class _Traits>
410void
411basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
412{
413 basic_streambuf<char_type, traits_type>::swap(__rhs);
414 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
415 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000416 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
417 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
418 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 }
420 else
421 {
422 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
423 ptrdiff_t __le = __extbufend_ - __extbuf_;
424 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
425 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
426 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
427 {
428 __extbuf_ = __rhs.__extbuf_;
429 __rhs.__extbuf_ = __rhs.__extbuf_min_;
430 }
431 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
432 {
433 __rhs.__extbuf_ = __extbuf_;
434 __extbuf_ = __extbuf_min_;
435 }
436 __extbufnext_ = __extbuf_ + __rn;
437 __extbufend_ = __extbuf_ + __re;
438 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
439 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
440 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000441 _VSTD::swap(__ebs_, __rhs.__ebs_);
442 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
443 _VSTD::swap(__ibs_, __rhs.__ibs_);
444 _VSTD::swap(__file_, __rhs.__file_);
445 _VSTD::swap(__cv_, __rhs.__cv_);
446 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000447 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000448 _VSTD::swap(__om_, __rhs.__om_);
449 _VSTD::swap(__cm_, __rhs.__cm_);
450 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
451 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
452 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
454 {
455 ptrdiff_t __n = this->gptr() - this->eback();
456 ptrdiff_t __e = this->egptr() - this->eback();
457 this->setg((char_type*)__extbuf_min_,
458 (char_type*)__extbuf_min_ + __n,
459 (char_type*)__extbuf_min_ + __e);
460 }
461 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
462 {
463 ptrdiff_t __n = this->pptr() - this->pbase();
464 ptrdiff_t __e = this->epptr() - this->pbase();
465 this->setp((char_type*)__extbuf_min_,
466 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000467 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 }
469 if (__rhs.eback() == (char_type*)__extbuf_min_)
470 {
471 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
472 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
473 __rhs.setg((char_type*)__rhs.__extbuf_min_,
474 (char_type*)__rhs.__extbuf_min_ + __n,
475 (char_type*)__rhs.__extbuf_min_ + __e);
476 }
477 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
478 {
479 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
480 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
481 __rhs.setp((char_type*)__rhs.__extbuf_min_,
482 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000483 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000484 }
485}
486
487template <class _CharT, class _Traits>
488inline _LIBCPP_INLINE_VISIBILITY
489void
490swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
491{
492 __x.swap(__y);
493}
494
495template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000496inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000497bool
498basic_filebuf<_CharT, _Traits>::is_open() const
499{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500500 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501}
502
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000503template <class _CharT, class _Traits>
504const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
505 ios_base::openmode __mode) _NOEXCEPT {
506 switch (__mode & ~ios_base::ate) {
507 case ios_base::out:
508 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000509 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000510 case ios_base::out | ios_base::app:
511 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000512 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000513 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000514 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000515 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000516 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000517 case ios_base::in | ios_base::out | 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::in | ios_base::out | ios_base::app:
520 case ios_base::in | 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::out | ios_base::binary:
523 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000524 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000525 case ios_base::out | ios_base::app | ios_base::binary:
526 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000529 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000530 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000531 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000532 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
535 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000536 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000537 default:
538 return nullptr;
539 }
540 _LIBCPP_UNREACHABLE();
541}
542
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543template <class _CharT, class _Traits>
544basic_filebuf<_CharT, _Traits>*
545basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
546{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500547 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
548 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000550 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000552 __file_ = fopen(__s, __mdstr);
553 if (__file_) {
554 __om_ = __mode;
555 if (__mode & ios_base::ate) {
556 if (fseek(__file_, 0, SEEK_END)) {
557 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500558 __file_ = nullptr;
559 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000561 }
562 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500563 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000564 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 }
566 return __rt;
567}
568
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000569template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000570inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400571basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000572basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500573 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
574 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000575 if (const char* __mdstr = __make_mdstring(__mode)) {
576 __rt = this;
577 __file_ = fdopen(__fd, __mdstr);
578 if (__file_) {
579 __om_ = __mode;
580 if (__mode & ios_base::ate) {
581 if (fseek(__file_, 0, SEEK_END)) {
582 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500583 __file_ = nullptr;
584 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000585 }
586 }
587 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500588 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000589 }
590 }
591 return __rt;
592}
593
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000594#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
595// This is basically the same as the char* overload except that it uses _wfopen
596// and long mode strings.
597template <class _CharT, class _Traits>
598basic_filebuf<_CharT, _Traits>*
599basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
600{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500601 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
602 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000603 {
604 __rt = this;
605 const wchar_t* __mdstr;
606 switch (__mode & ~ios_base::ate)
607 {
608 case ios_base::out:
609 case ios_base::out | ios_base::trunc:
610 __mdstr = L"w";
611 break;
612 case ios_base::out | ios_base::app:
613 case ios_base::app:
614 __mdstr = L"a";
615 break;
616 case ios_base::in:
617 __mdstr = L"r";
618 break;
619 case ios_base::in | ios_base::out:
620 __mdstr = L"r+";
621 break;
622 case ios_base::in | ios_base::out | ios_base::trunc:
623 __mdstr = L"w+";
624 break;
625 case ios_base::in | ios_base::out | ios_base::app:
626 case ios_base::in | ios_base::app:
627 __mdstr = L"a+";
628 break;
629 case ios_base::out | ios_base::binary:
630 case ios_base::out | ios_base::trunc | ios_base::binary:
631 __mdstr = L"wb";
632 break;
633 case ios_base::out | ios_base::app | ios_base::binary:
634 case ios_base::app | ios_base::binary:
635 __mdstr = L"ab";
636 break;
637 case ios_base::in | ios_base::binary:
638 __mdstr = L"rb";
639 break;
640 case ios_base::in | ios_base::out | ios_base::binary:
641 __mdstr = L"r+b";
642 break;
643 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
644 __mdstr = L"w+b";
645 break;
646 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
647 case ios_base::in | ios_base::app | ios_base::binary:
648 __mdstr = L"a+b";
649 break;
650 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500651 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000652 break;
653 }
654 if (__rt)
655 {
656 __file_ = _wfopen(__s, __mdstr);
657 if (__file_)
658 {
659 __om_ = __mode;
660 if (__mode & ios_base::ate)
661 {
662 if (fseek(__file_, 0, SEEK_END))
663 {
664 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500665 __file_ = nullptr;
666 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000667 }
668 }
669 }
670 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500671 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000672 }
673 }
674 return __rt;
675}
676#endif
677
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000679inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680basic_filebuf<_CharT, _Traits>*
681basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
682{
683 return open(__s.c_str(), __mode);
684}
685
686template <class _CharT, class _Traits>
687basic_filebuf<_CharT, _Traits>*
688basic_filebuf<_CharT, _Traits>::close()
689{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500690 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691 if (__file_)
692 {
693 __rt = this;
694 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000695 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500696 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000697 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500698 __rt = nullptr;
699 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000700 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 }
702 return __rt;
703}
704
705template <class _CharT, class _Traits>
706typename basic_filebuf<_CharT, _Traits>::int_type
707basic_filebuf<_CharT, _Traits>::underflow()
708{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500709 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710 return traits_type::eof();
711 bool __initial = __read_mode();
712 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500713 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 this->setg(&__1buf, &__1buf+1, &__1buf+1);
715 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
716 int_type __c = traits_type::eof();
717 if (this->gptr() == this->egptr())
718 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500719 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 if (__always_noconv_)
721 {
722 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
723 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
724 if (__nmemb != 0)
725 {
726 this->setg(this->eback(),
727 this->eback() + __unget_sz,
728 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000729 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730 }
731 }
732 else
733 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000734 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
735 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500736 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
738 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000739 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 static_cast<size_t>(__extbufend_ - __extbufnext_));
741 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000742 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000743 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744 if (__nr != 0)
745 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000746 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000747 __throw_bad_cast();
748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 __extbufend_ = __extbufnext_ + __nr;
750 char_type* __inext;
751 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
752 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000753 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 if (__r == codecvt_base::noconv)
755 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000756 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000757 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000758 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 }
760 else if (__inext != this->eback() + __unget_sz)
761 {
762 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
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 }
766 }
767 }
768 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000769 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500771 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 return __c;
773}
774
775template <class _CharT, class _Traits>
776typename basic_filebuf<_CharT, _Traits>::int_type
777basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
778{
779 if (__file_ && this->eback() < this->gptr())
780 {
781 if (traits_type::eq_int_type(__c, traits_type::eof()))
782 {
783 this->gbump(-1);
784 return traits_type::not_eof(__c);
785 }
786 if ((__om_ & ios_base::out) ||
787 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
788 {
789 this->gbump(-1);
790 *this->gptr() = traits_type::to_char_type(__c);
791 return __c;
792 }
793 }
794 return traits_type::eof();
795}
796
797template <class _CharT, class _Traits>
798typename basic_filebuf<_CharT, _Traits>::int_type
799basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
800{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500801 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 return traits_type::eof();
803 __write_mode();
804 char_type __1buf;
805 char_type* __pb_save = this->pbase();
806 char_type* __epb_save = this->epptr();
807 if (!traits_type::eq_int_type(__c, traits_type::eof()))
808 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500809 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 this->setp(&__1buf, &__1buf+1);
811 *this->pptr() = traits_type::to_char_type(__c);
812 this->pbump(1);
813 }
814 if (this->pptr() != this->pbase())
815 {
816 if (__always_noconv_)
817 {
818 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
819 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
820 return traits_type::eof();
821 }
822 else
823 {
824 char* __extbe = __extbuf_;
825 codecvt_base::result __r;
826 do
827 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000828 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000829 __throw_bad_cast();
830
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 const char_type* __e;
832 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
833 __extbuf_, __extbuf_ + __ebs_, __extbe);
834 if (__e == this->pbase())
835 return traits_type::eof();
836 if (__r == codecvt_base::noconv)
837 {
838 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
839 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
840 return traits_type::eof();
841 }
842 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
843 {
844 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
845 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
846 return traits_type::eof();
847 if (__r == codecvt_base::partial)
848 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000849 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000850 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851 }
852 }
853 else
854 return traits_type::eof();
855 } while (__r == codecvt_base::partial);
856 }
857 this->setp(__pb_save, __epb_save);
858 }
859 return traits_type::not_eof(__c);
860}
861
862template <class _CharT, class _Traits>
863basic_streambuf<_CharT, _Traits>*
864basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
865{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500866 this->setg(nullptr, nullptr, nullptr);
867 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 if (__owns_eb_)
869 delete [] __extbuf_;
870 if (__owns_ib_)
871 delete [] __intbuf_;
872 __ebs_ = __n;
873 if (__ebs_ > sizeof(__extbuf_min_))
874 {
875 if (__always_noconv_ && __s)
876 {
877 __extbuf_ = (char*)__s;
878 __owns_eb_ = false;
879 }
880 else
881 {
882 __extbuf_ = new char[__ebs_];
883 __owns_eb_ = true;
884 }
885 }
886 else
887 {
888 __extbuf_ = __extbuf_min_;
889 __ebs_ = sizeof(__extbuf_min_);
890 __owns_eb_ = false;
891 }
892 if (!__always_noconv_)
893 {
894 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
895 if (__s && __ibs_ >= sizeof(__extbuf_min_))
896 {
897 __intbuf_ = __s;
898 __owns_ib_ = false;
899 }
900 else
901 {
902 __intbuf_ = new char_type[__ibs_];
903 __owns_ib_ = true;
904 }
905 }
906 else
907 {
908 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500909 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 __owns_ib_ = false;
911 }
912 return this;
913}
914
915template <class _CharT, class _Traits>
916typename basic_filebuf<_CharT, _Traits>::pos_type
917basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
918 ios_base::openmode)
919{
Howard Hinnant0090b122012-08-24 16:52:47 +0000920 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000921 __throw_bad_cast();
922
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500924 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925 return pos_type(off_type(-1));
926 // __width > 0 || __off == 0
927 int __whence;
928 switch (__way)
929 {
930 case ios_base::beg:
931 __whence = SEEK_SET;
932 break;
933 case ios_base::cur:
934 __whence = SEEK_CUR;
935 break;
936 case ios_base::end:
937 __whence = SEEK_END;
938 break;
939 default:
940 return pos_type(off_type(-1));
941 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000942#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000943 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
944 return pos_type(off_type(-1));
945 pos_type __r = ftell(__file_);
946#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
948 return pos_type(off_type(-1));
949 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000950#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 __r.state(__st_);
952 return __r;
953}
954
955template <class _CharT, class _Traits>
956typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000957basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500959 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000961#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000962 if (fseek(__file_, __sp, SEEK_SET))
963 return pos_type(off_type(-1));
964#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 if (fseeko(__file_, __sp, SEEK_SET))
966 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000967#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000968 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 return __sp;
970}
971
972template <class _CharT, class _Traits>
973int
974basic_filebuf<_CharT, _Traits>::sync()
975{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500976 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000978 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000979 __throw_bad_cast();
980
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 if (__cm_ & ios_base::out)
982 {
983 if (this->pptr() != this->pbase())
984 if (overflow() == traits_type::eof())
985 return -1;
986 codecvt_base::result __r;
987 do
988 {
989 char* __extbe;
990 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
991 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
992 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
993 return -1;
994 } while (__r == codecvt_base::partial);
995 if (__r == codecvt_base::error)
996 return -1;
997 if (fflush(__file_))
998 return -1;
999 }
1000 else if (__cm_ & ios_base::in)
1001 {
1002 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001003 state_type __state = __st_last_;
1004 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 if (__always_noconv_)
1006 __c = this->egptr() - this->gptr();
1007 else
1008 {
1009 int __width = __cv_->encoding();
1010 __c = __extbufend_ - __extbufnext_;
1011 if (__width > 0)
1012 __c += __width * (this->egptr() - this->gptr());
1013 else
1014 {
1015 if (this->gptr() != this->egptr())
1016 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001017 const int __off = __cv_->length(__state, __extbuf_,
1018 __extbufnext_,
1019 this->gptr() - this->eback());
1020 __c += __extbufnext_ - __extbuf_ - __off;
1021 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 }
1023 }
1024 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001025#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001026 if (fseek(__file_, -__c, SEEK_CUR))
1027 return -1;
1028#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 if (fseeko(__file_, -__c, SEEK_CUR))
1030 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001031#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001032 if (__update_st)
1033 __st_ = __state;
1034 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001035 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 __cm_ = 0;
1037 }
1038 return 0;
1039}
1040
1041template <class _CharT, class _Traits>
1042void
1043basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1044{
1045 sync();
1046 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1047 bool __old_anc = __always_noconv_;
1048 __always_noconv_ = __cv_->always_noconv();
1049 if (__old_anc != __always_noconv_)
1050 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001051 this->setg(nullptr, nullptr, nullptr);
1052 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 // invariant, char_type is char, else we couldn't get here
1054 if (__always_noconv_) // need to dump __intbuf_
1055 {
1056 if (__owns_eb_)
1057 delete [] __extbuf_;
1058 __owns_eb_ = __owns_ib_;
1059 __ebs_ = __ibs_;
1060 __extbuf_ = (char*)__intbuf_;
1061 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001062 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 __owns_ib_ = false;
1064 }
1065 else // need to obtain an __intbuf_.
1066 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1067 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1068 {
1069 __ibs_ = __ebs_;
1070 __intbuf_ = (char_type*)__extbuf_;
1071 __owns_ib_ = false;
1072 __extbuf_ = new char[__ebs_];
1073 __owns_eb_ = true;
1074 }
1075 else
1076 {
1077 __ibs_ = __ebs_;
1078 __intbuf_ = new char_type[__ibs_];
1079 __owns_ib_ = true;
1080 }
1081 }
1082 }
1083}
1084
1085template <class _CharT, class _Traits>
1086bool
1087basic_filebuf<_CharT, _Traits>::__read_mode()
1088{
1089 if (!(__cm_ & ios_base::in))
1090 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001091 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 if (__always_noconv_)
1093 this->setg((char_type*)__extbuf_,
1094 (char_type*)__extbuf_ + __ebs_,
1095 (char_type*)__extbuf_ + __ebs_);
1096 else
1097 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1098 __cm_ = ios_base::in;
1099 return true;
1100 }
1101 return false;
1102}
1103
1104template <class _CharT, class _Traits>
1105void
1106basic_filebuf<_CharT, _Traits>::__write_mode()
1107{
1108 if (!(__cm_ & ios_base::out))
1109 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001110 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 if (__ebs_ > sizeof(__extbuf_min_))
1112 {
1113 if (__always_noconv_)
1114 this->setp((char_type*)__extbuf_,
1115 (char_type*)__extbuf_ + (__ebs_ - 1));
1116 else
1117 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1118 }
1119 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001120 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 __cm_ = ios_base::out;
1122 }
1123}
1124
1125// basic_ifstream
1126
1127template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001128class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 : public basic_istream<_CharT, _Traits>
1130{
1131public:
1132 typedef _CharT char_type;
1133 typedef _Traits traits_type;
1134 typedef typename traits_type::int_type int_type;
1135 typedef typename traits_type::pos_type pos_type;
1136 typedef typename traits_type::off_type off_type;
1137
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001142#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1143 _LIBCPP_INLINE_VISIBILITY
1144 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1145#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001148#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001149 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001150 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1151 : basic_ifstream(__p.c_str(), __mode) {}
1152#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 void swap(basic_ifstream& __rhs);
1159
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 bool is_open() const;
1164 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001165#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1166 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001169#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001170 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001171 void open(const filesystem::path& __p,
1172 ios_base::openmode __mode = ios_base::in) {
1173 return open(__p.c_str(), __mode);
1174 }
1175#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001176
1177 _LIBCPP_INLINE_VISIBILITY
1178 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 void close();
1181
1182private:
1183 basic_filebuf<char_type, traits_type> __sb_;
1184};
1185
1186template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001187inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188basic_ifstream<_CharT, _Traits>::basic_ifstream()
1189 : basic_istream<char_type, traits_type>(&__sb_)
1190{
1191}
1192
1193template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001194inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1196 : basic_istream<char_type, traits_type>(&__sb_)
1197{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001198 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 this->setstate(ios_base::failbit);
1200}
1201
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001202#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1203template <class _CharT, class _Traits>
1204inline
1205basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1206 : basic_istream<char_type, traits_type>(&__sb_)
1207{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001208 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001209 this->setstate(ios_base::failbit);
1210}
1211#endif
1212
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1216 : basic_istream<char_type, traits_type>(&__sb_)
1217{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001218 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 this->setstate(ios_base::failbit);
1220}
1221
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001223inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001225 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1226 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227{
1228 this->set_rdbuf(&__sb_);
1229}
1230
1231template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001232inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233basic_ifstream<_CharT, _Traits>&
1234basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1235{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001236 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1237 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 return *this;
1239}
1240
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243void
1244basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1245{
1246 basic_istream<char_type, traits_type>::swap(__rhs);
1247 __sb_.swap(__rhs.__sb_);
1248}
1249
1250template <class _CharT, class _Traits>
1251inline _LIBCPP_INLINE_VISIBILITY
1252void
1253swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1254{
1255 __x.swap(__y);
1256}
1257
1258template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001259inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260basic_filebuf<_CharT, _Traits>*
1261basic_ifstream<_CharT, _Traits>::rdbuf() const
1262{
1263 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1264}
1265
1266template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001267inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268bool
1269basic_ifstream<_CharT, _Traits>::is_open() const
1270{
1271 return __sb_.is_open();
1272}
1273
1274template <class _CharT, class _Traits>
1275void
1276basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1277{
1278 if (__sb_.open(__s, __mode | ios_base::in))
1279 this->clear();
1280 else
1281 this->setstate(ios_base::failbit);
1282}
1283
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001284#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1285template <class _CharT, class _Traits>
1286void
1287basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1288{
1289 if (__sb_.open(__s, __mode | ios_base::in))
1290 this->clear();
1291 else
1292 this->setstate(ios_base::failbit);
1293}
1294#endif
1295
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296template <class _CharT, class _Traits>
1297void
1298basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1299{
1300 if (__sb_.open(__s, __mode | ios_base::in))
1301 this->clear();
1302 else
1303 this->setstate(ios_base::failbit);
1304}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001305
1306template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001307inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001308void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1309 ios_base::openmode __mode) {
1310 if (__sb_.__open(__fd, __mode | ios_base::in))
1311 this->clear();
1312 else
1313 this->setstate(ios_base::failbit);
1314}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315
1316template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001317inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318void
1319basic_ifstream<_CharT, _Traits>::close()
1320{
1321 if (__sb_.close() == 0)
1322 this->setstate(ios_base::failbit);
1323}
1324
1325// basic_ofstream
1326
1327template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001328class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 : public basic_ostream<_CharT, _Traits>
1330{
1331public:
1332 typedef _CharT char_type;
1333 typedef _Traits traits_type;
1334 typedef typename traits_type::int_type int_type;
1335 typedef typename traits_type::pos_type pos_type;
1336 typedef typename traits_type::off_type off_type;
1337
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001342#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1343 _LIBCPP_INLINE_VISIBILITY
1344 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1345#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001348
Louis Dionnedb84e122021-01-18 12:18:18 -05001349#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001350 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001351 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1352 : basic_ofstream(__p.c_str(), __mode) {}
1353#endif // _LIBCPP_STD_VER >= 17
1354
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 void swap(basic_ofstream& __rhs);
1361
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 bool is_open() const;
1366 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001367#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1368 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1369#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001371
Louis Dionnedb84e122021-01-18 12:18:18 -05001372#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001373 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001374 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1375 { return open(__p.c_str(), __mode); }
1376#endif // _LIBCPP_STD_VER >= 17
1377
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001378 _LIBCPP_INLINE_VISIBILITY
1379 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 void close();
1382
1383private:
1384 basic_filebuf<char_type, traits_type> __sb_;
1385};
1386
1387template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001388inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389basic_ofstream<_CharT, _Traits>::basic_ofstream()
1390 : basic_ostream<char_type, traits_type>(&__sb_)
1391{
1392}
1393
1394template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001395inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1397 : basic_ostream<char_type, traits_type>(&__sb_)
1398{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001399 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 this->setstate(ios_base::failbit);
1401}
1402
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001403#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1404template <class _CharT, class _Traits>
1405inline
1406basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1407 : basic_ostream<char_type, traits_type>(&__sb_)
1408{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001409 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001410 this->setstate(ios_base::failbit);
1411}
1412#endif
1413
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001415inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1417 : basic_ostream<char_type, traits_type>(&__sb_)
1418{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001419 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 this->setstate(ios_base::failbit);
1421}
1422
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001424inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001426 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1427 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428{
1429 this->set_rdbuf(&__sb_);
1430}
1431
1432template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001433inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434basic_ofstream<_CharT, _Traits>&
1435basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1436{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001437 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1438 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 return *this;
1440}
1441
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001443inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444void
1445basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1446{
1447 basic_ostream<char_type, traits_type>::swap(__rhs);
1448 __sb_.swap(__rhs.__sb_);
1449}
1450
1451template <class _CharT, class _Traits>
1452inline _LIBCPP_INLINE_VISIBILITY
1453void
1454swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1455{
1456 __x.swap(__y);
1457}
1458
1459template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001460inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461basic_filebuf<_CharT, _Traits>*
1462basic_ofstream<_CharT, _Traits>::rdbuf() const
1463{
1464 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1465}
1466
1467template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001468inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469bool
1470basic_ofstream<_CharT, _Traits>::is_open() const
1471{
1472 return __sb_.is_open();
1473}
1474
1475template <class _CharT, class _Traits>
1476void
1477basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1478{
1479 if (__sb_.open(__s, __mode | ios_base::out))
1480 this->clear();
1481 else
1482 this->setstate(ios_base::failbit);
1483}
1484
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001485#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1486template <class _CharT, class _Traits>
1487void
1488basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1489{
1490 if (__sb_.open(__s, __mode | ios_base::out))
1491 this->clear();
1492 else
1493 this->setstate(ios_base::failbit);
1494}
1495#endif
1496
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497template <class _CharT, class _Traits>
1498void
1499basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1500{
1501 if (__sb_.open(__s, __mode | ios_base::out))
1502 this->clear();
1503 else
1504 this->setstate(ios_base::failbit);
1505}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001506
1507template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001508inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001509void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1510 ios_base::openmode __mode) {
1511 if (__sb_.__open(__fd, __mode | ios_base::out))
1512 this->clear();
1513 else
1514 this->setstate(ios_base::failbit);
1515}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516
1517template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001518inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519void
1520basic_ofstream<_CharT, _Traits>::close()
1521{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001522 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 this->setstate(ios_base::failbit);
1524}
1525
1526// basic_fstream
1527
1528template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001529class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 : public basic_iostream<_CharT, _Traits>
1531{
1532public:
1533 typedef _CharT char_type;
1534 typedef _Traits traits_type;
1535 typedef typename traits_type::int_type int_type;
1536 typedef typename traits_type::pos_type pos_type;
1537 typedef typename traits_type::off_type off_type;
1538
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001543#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1544 _LIBCPP_INLINE_VISIBILITY
1545 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1546#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001549
Louis Dionnedb84e122021-01-18 12:18:18 -05001550#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001551 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001552 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1553 : basic_fstream(__p.c_str(), __mode) {}
1554#endif // _LIBCPP_STD_VER >= 17
1555
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001561
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 void swap(basic_fstream& __rhs);
1564
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 bool is_open() const;
1569 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001570#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1571 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1572#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001574
Louis Dionnedb84e122021-01-18 12:18:18 -05001575#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001576 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001577 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1578 { return open(__p.c_str(), __mode); }
1579#endif // _LIBCPP_STD_VER >= 17
1580
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 void close();
1583
1584private:
1585 basic_filebuf<char_type, traits_type> __sb_;
1586};
1587
1588template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001589inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590basic_fstream<_CharT, _Traits>::basic_fstream()
1591 : basic_iostream<char_type, traits_type>(&__sb_)
1592{
1593}
1594
1595template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001596inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1598 : basic_iostream<char_type, traits_type>(&__sb_)
1599{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001600 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 this->setstate(ios_base::failbit);
1602}
1603
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001604#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1605template <class _CharT, class _Traits>
1606inline
1607basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1608 : basic_iostream<char_type, traits_type>(&__sb_)
1609{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001610 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001611 this->setstate(ios_base::failbit);
1612}
1613#endif
1614
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001616inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1618 : basic_iostream<char_type, traits_type>(&__sb_)
1619{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001620 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 this->setstate(ios_base::failbit);
1622}
1623
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624template <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(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001627 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1628 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629{
1630 this->set_rdbuf(&__sb_);
1631}
1632
1633template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001634inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635basic_fstream<_CharT, _Traits>&
1636basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1637{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001638 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1639 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 return *this;
1641}
1642
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001644inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645void
1646basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1647{
1648 basic_iostream<char_type, traits_type>::swap(__rhs);
1649 __sb_.swap(__rhs.__sb_);
1650}
1651
1652template <class _CharT, class _Traits>
1653inline _LIBCPP_INLINE_VISIBILITY
1654void
1655swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1656{
1657 __x.swap(__y);
1658}
1659
1660template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001661inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662basic_filebuf<_CharT, _Traits>*
1663basic_fstream<_CharT, _Traits>::rdbuf() const
1664{
1665 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1666}
1667
1668template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001669inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670bool
1671basic_fstream<_CharT, _Traits>::is_open() const
1672{
1673 return __sb_.is_open();
1674}
1675
1676template <class _CharT, class _Traits>
1677void
1678basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1679{
1680 if (__sb_.open(__s, __mode))
1681 this->clear();
1682 else
1683 this->setstate(ios_base::failbit);
1684}
1685
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001686#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1687template <class _CharT, class _Traits>
1688void
1689basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1690{
1691 if (__sb_.open(__s, __mode))
1692 this->clear();
1693 else
1694 this->setstate(ios_base::failbit);
1695}
1696#endif
1697
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698template <class _CharT, class _Traits>
1699void
1700basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1701{
1702 if (__sb_.open(__s, __mode))
1703 this->clear();
1704 else
1705 this->setstate(ios_base::failbit);
1706}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707
1708template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001709inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710void
1711basic_fstream<_CharT, _Traits>::close()
1712{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001713 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714 this->setstate(ios_base::failbit);
1715}
1716
Louis Dionne8a79be62020-10-21 11:11:45 -04001717#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1718_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1719_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1720_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1721#endif
1722
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723_LIBCPP_END_NAMESPACE_STD
1724
Eric Fiselierf4433a32017-05-31 22:07:49 +00001725_LIBCPP_POP_MACROS
1726
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001727#endif // _LIBCPP_FSTREAM