blob: aa5d7e2ee6a382569f1a9d6db38a15323a8d9437 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100190#include <version>
Louis Dionnedb84e122021-01-18 12:18:18 -0500191
192#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
193# include <filesystem>
194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000196#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500197# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199
Eric Fiselierf4433a32017-05-31 22:07:49 +0000200_LIBCPP_PUSH_MACROS
201#include <__undef_macros>
202
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400203#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
204# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
205#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000206
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207_LIBCPP_BEGIN_NAMESPACE_STD
208
209template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000210class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 : public basic_streambuf<_CharT, _Traits>
212{
213public:
214 typedef _CharT char_type;
215 typedef _Traits traits_type;
216 typedef typename traits_type::int_type int_type;
217 typedef typename traits_type::pos_type pos_type;
218 typedef typename traits_type::off_type off_type;
219 typedef typename traits_type::state_type state_type;
220
221 // 27.9.1.2 Constructors/destructor:
222 basic_filebuf();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_filebuf(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 virtual ~basic_filebuf();
225
226 // 27.9.1.3 Assign/swap:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 basic_filebuf& operator=(basic_filebuf&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 void swap(basic_filebuf& __rhs);
230
231 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 bool is_open() const;
234 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000235#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
236 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
237#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000240
Louis Dionnedb84e122021-01-18 12:18:18 -0500241#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000242 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000243 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
244 return open(__p.c_str(), __mode);
245 }
246#endif
jasonliu9c5d70a2021-04-12 19:22:12 +0000247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000248 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249 basic_filebuf* close();
250
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000251 _LIBCPP_INLINE_VISIBILITY
252 inline static const char*
253 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
254
255 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 // 27.9.1.5 Overridden virtual functions:
257 virtual int_type underflow();
258 virtual int_type pbackfail(int_type __c = traits_type::eof());
259 virtual int_type overflow (int_type __c = traits_type::eof());
260 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
261 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
262 ios_base::openmode __wch = ios_base::in | ios_base::out);
263 virtual pos_type seekpos(pos_type __sp,
264 ios_base::openmode __wch = ios_base::in | ios_base::out);
265 virtual int sync();
266 virtual void imbue(const locale& __loc);
267
268private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000269 char* __extbuf_;
270 const char* __extbufnext_;
271 const char* __extbufend_;
272 char __extbuf_min_[8];
273 size_t __ebs_;
274 char_type* __intbuf_;
275 size_t __ibs_;
276 FILE* __file_;
277 const codecvt<char_type, char, state_type>* __cv_;
278 state_type __st_;
279 state_type __st_last_;
280 ios_base::openmode __om_;
281 ios_base::openmode __cm_;
282 bool __owns_eb_;
283 bool __owns_ib_;
284 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000286 bool __read_mode();
287 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288};
289
290template <class _CharT, class _Traits>
291basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500292 : __extbuf_(nullptr),
293 __extbufnext_(nullptr),
294 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500296 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500298 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000299 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000301 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 __om_(0),
303 __cm_(0),
304 __owns_eb_(false),
305 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000306 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307{
Howard Hinnant0090b122012-08-24 16:52:47 +0000308 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
309 {
310 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
311 __always_noconv_ = __cv_->always_noconv();
312 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500313 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314}
315
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316template <class _CharT, class _Traits>
317basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
318 : basic_streambuf<_CharT, _Traits>(__rhs)
319{
320 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
321 {
322 __extbuf_ = __extbuf_min_;
323 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
324 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
325 }
326 else
327 {
328 __extbuf_ = __rhs.__extbuf_;
329 __extbufnext_ = __rhs.__extbufnext_;
330 __extbufend_ = __rhs.__extbufend_;
331 }
332 __ebs_ = __rhs.__ebs_;
333 __intbuf_ = __rhs.__intbuf_;
334 __ibs_ = __rhs.__ibs_;
335 __file_ = __rhs.__file_;
336 __cv_ = __rhs.__cv_;
337 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000338 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 __om_ = __rhs.__om_;
340 __cm_ = __rhs.__cm_;
341 __owns_eb_ = __rhs.__owns_eb_;
342 __owns_ib_ = __rhs.__owns_ib_;
343 __always_noconv_ = __rhs.__always_noconv_;
344 if (__rhs.pbase())
345 {
346 if (__rhs.pbase() == __rhs.__intbuf_)
347 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
348 else
349 this->setp((char_type*)__extbuf_,
350 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000351 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 }
353 else if (__rhs.eback())
354 {
355 if (__rhs.eback() == __rhs.__intbuf_)
356 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
357 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
358 else
359 this->setg((char_type*)__extbuf_,
360 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
361 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
362 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500363 __rhs.__extbuf_ = nullptr;
364 __rhs.__extbufnext_ = nullptr;
365 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 __rhs.__ebs_ = 0;
367 __rhs.__intbuf_ = 0;
368 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500369 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000371 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__om_ = 0;
373 __rhs.__cm_ = 0;
374 __rhs.__owns_eb_ = false;
375 __rhs.__owns_ib_ = false;
376 __rhs.setg(0, 0, 0);
377 __rhs.setp(0, 0);
378}
379
380template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382basic_filebuf<_CharT, _Traits>&
383basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
384{
385 close();
386 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000387 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388}
389
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390template <class _CharT, class _Traits>
391basic_filebuf<_CharT, _Traits>::~basic_filebuf()
392{
393#ifndef _LIBCPP_NO_EXCEPTIONS
394 try
395 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400396#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 close();
398#ifndef _LIBCPP_NO_EXCEPTIONS
399 }
400 catch (...)
401 {
402 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400403#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404 if (__owns_eb_)
405 delete [] __extbuf_;
406 if (__owns_ib_)
407 delete [] __intbuf_;
408}
409
410template <class _CharT, class _Traits>
411void
412basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
413{
414 basic_streambuf<char_type, traits_type>::swap(__rhs);
415 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
416 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000417 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
418 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
419 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 }
421 else
422 {
423 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
424 ptrdiff_t __le = __extbufend_ - __extbuf_;
425 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
426 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
427 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
428 {
429 __extbuf_ = __rhs.__extbuf_;
430 __rhs.__extbuf_ = __rhs.__extbuf_min_;
431 }
432 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
433 {
434 __rhs.__extbuf_ = __extbuf_;
435 __extbuf_ = __extbuf_min_;
436 }
437 __extbufnext_ = __extbuf_ + __rn;
438 __extbufend_ = __extbuf_ + __re;
439 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
440 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
441 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000442 _VSTD::swap(__ebs_, __rhs.__ebs_);
443 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
444 _VSTD::swap(__ibs_, __rhs.__ibs_);
445 _VSTD::swap(__file_, __rhs.__file_);
446 _VSTD::swap(__cv_, __rhs.__cv_);
447 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000448 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000449 _VSTD::swap(__om_, __rhs.__om_);
450 _VSTD::swap(__cm_, __rhs.__cm_);
451 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
452 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
453 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
455 {
456 ptrdiff_t __n = this->gptr() - this->eback();
457 ptrdiff_t __e = this->egptr() - this->eback();
458 this->setg((char_type*)__extbuf_min_,
459 (char_type*)__extbuf_min_ + __n,
460 (char_type*)__extbuf_min_ + __e);
461 }
462 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
463 {
464 ptrdiff_t __n = this->pptr() - this->pbase();
465 ptrdiff_t __e = this->epptr() - this->pbase();
466 this->setp((char_type*)__extbuf_min_,
467 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000468 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 }
470 if (__rhs.eback() == (char_type*)__extbuf_min_)
471 {
472 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
473 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
474 __rhs.setg((char_type*)__rhs.__extbuf_min_,
475 (char_type*)__rhs.__extbuf_min_ + __n,
476 (char_type*)__rhs.__extbuf_min_ + __e);
477 }
478 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
479 {
480 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
481 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
482 __rhs.setp((char_type*)__rhs.__extbuf_min_,
483 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000484 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485 }
486}
487
488template <class _CharT, class _Traits>
489inline _LIBCPP_INLINE_VISIBILITY
490void
491swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
492{
493 __x.swap(__y);
494}
495
496template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000497inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498bool
499basic_filebuf<_CharT, _Traits>::is_open() const
500{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500501 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502}
503
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000504template <class _CharT, class _Traits>
505const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
506 ios_base::openmode __mode) _NOEXCEPT {
507 switch (__mode & ~ios_base::ate) {
508 case ios_base::out:
509 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000510 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000511 case ios_base::out | ios_base::app:
512 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000513 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000514 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000515 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000516 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000517 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000518 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000519 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000520 case ios_base::in | ios_base::out | ios_base::app:
521 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000522 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000523 case ios_base::out | ios_base::binary:
524 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000525 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000526 case ios_base::out | ios_base::app | ios_base::binary:
527 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000528 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000529 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000532 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000533 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000534 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000535 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
536 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000537 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000538 default:
539 return nullptr;
540 }
541 _LIBCPP_UNREACHABLE();
542}
543
Howard Hinnantc51e1022010-05-11 19:42:16 +0000544template <class _CharT, class _Traits>
545basic_filebuf<_CharT, _Traits>*
546basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
547{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500548 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
549 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000551 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000553 __file_ = fopen(__s, __mdstr);
554 if (__file_) {
555 __om_ = __mode;
556 if (__mode & ios_base::ate) {
557 if (fseek(__file_, 0, SEEK_END)) {
558 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500559 __file_ = nullptr;
560 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000562 }
563 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500564 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000565 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 }
567 return __rt;
568}
569
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000570template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +0000571inline
Louis Dionne8a79be62020-10-21 11:11:45 -0400572basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000573basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500574 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
575 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000576 if (const char* __mdstr = __make_mdstring(__mode)) {
577 __rt = this;
578 __file_ = fdopen(__fd, __mdstr);
579 if (__file_) {
580 __om_ = __mode;
581 if (__mode & ios_base::ate) {
582 if (fseek(__file_, 0, SEEK_END)) {
583 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500584 __file_ = nullptr;
585 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000586 }
587 }
588 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500589 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000590 }
591 }
592 return __rt;
593}
594
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000595#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
596// This is basically the same as the char* overload except that it uses _wfopen
597// and long mode strings.
598template <class _CharT, class _Traits>
599basic_filebuf<_CharT, _Traits>*
600basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
601{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500602 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
603 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000604 {
605 __rt = this;
606 const wchar_t* __mdstr;
607 switch (__mode & ~ios_base::ate)
608 {
609 case ios_base::out:
610 case ios_base::out | ios_base::trunc:
611 __mdstr = L"w";
612 break;
613 case ios_base::out | ios_base::app:
614 case ios_base::app:
615 __mdstr = L"a";
616 break;
617 case ios_base::in:
618 __mdstr = L"r";
619 break;
620 case ios_base::in | ios_base::out:
621 __mdstr = L"r+";
622 break;
623 case ios_base::in | ios_base::out | ios_base::trunc:
624 __mdstr = L"w+";
625 break;
626 case ios_base::in | ios_base::out | ios_base::app:
627 case ios_base::in | ios_base::app:
628 __mdstr = L"a+";
629 break;
630 case ios_base::out | ios_base::binary:
631 case ios_base::out | ios_base::trunc | ios_base::binary:
632 __mdstr = L"wb";
633 break;
634 case ios_base::out | ios_base::app | ios_base::binary:
635 case ios_base::app | ios_base::binary:
636 __mdstr = L"ab";
637 break;
638 case ios_base::in | ios_base::binary:
639 __mdstr = L"rb";
640 break;
641 case ios_base::in | ios_base::out | ios_base::binary:
642 __mdstr = L"r+b";
643 break;
644 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
645 __mdstr = L"w+b";
646 break;
647 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
648 case ios_base::in | ios_base::app | ios_base::binary:
649 __mdstr = L"a+b";
650 break;
651 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500652 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000653 break;
654 }
655 if (__rt)
656 {
657 __file_ = _wfopen(__s, __mdstr);
658 if (__file_)
659 {
660 __om_ = __mode;
661 if (__mode & ios_base::ate)
662 {
663 if (fseek(__file_, 0, SEEK_END))
664 {
665 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500666 __file_ = nullptr;
667 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000668 }
669 }
670 }
671 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500672 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000673 }
674 }
675 return __rt;
676}
677#endif
678
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681basic_filebuf<_CharT, _Traits>*
682basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
683{
684 return open(__s.c_str(), __mode);
685}
686
687template <class _CharT, class _Traits>
688basic_filebuf<_CharT, _Traits>*
689basic_filebuf<_CharT, _Traits>::close()
690{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500691 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 if (__file_)
693 {
694 __rt = this;
695 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000696 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500697 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000698 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500699 __rt = nullptr;
700 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000701 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 }
703 return __rt;
704}
705
706template <class _CharT, class _Traits>
707typename basic_filebuf<_CharT, _Traits>::int_type
708basic_filebuf<_CharT, _Traits>::underflow()
709{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500710 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 return traits_type::eof();
712 bool __initial = __read_mode();
713 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500714 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 this->setg(&__1buf, &__1buf+1, &__1buf+1);
716 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
717 int_type __c = traits_type::eof();
718 if (this->gptr() == this->egptr())
719 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500720 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 if (__always_noconv_)
722 {
723 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
724 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
725 if (__nmemb != 0)
726 {
727 this->setg(this->eback(),
728 this->eback() + __unget_sz,
729 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000730 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 }
732 }
733 else
734 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000735 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
736 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500737 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
739 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000740 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 static_cast<size_t>(__extbufend_ - __extbufnext_));
742 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000743 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000744 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 if (__nr != 0)
746 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000747 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000748 __throw_bad_cast();
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 __extbufend_ = __extbufnext_ + __nr;
751 char_type* __inext;
752 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
753 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000754 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 if (__r == codecvt_base::noconv)
756 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000757 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000758 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000759 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 }
761 else if (__inext != this->eback() + __unget_sz)
762 {
763 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000764 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 }
766 }
767 }
768 }
769 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000770 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500772 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 return __c;
774}
775
776template <class _CharT, class _Traits>
777typename basic_filebuf<_CharT, _Traits>::int_type
778basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
779{
780 if (__file_ && this->eback() < this->gptr())
781 {
782 if (traits_type::eq_int_type(__c, traits_type::eof()))
783 {
784 this->gbump(-1);
785 return traits_type::not_eof(__c);
786 }
787 if ((__om_ & ios_base::out) ||
788 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
789 {
790 this->gbump(-1);
791 *this->gptr() = traits_type::to_char_type(__c);
792 return __c;
793 }
794 }
795 return traits_type::eof();
796}
797
798template <class _CharT, class _Traits>
799typename basic_filebuf<_CharT, _Traits>::int_type
800basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
801{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500802 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 return traits_type::eof();
804 __write_mode();
805 char_type __1buf;
806 char_type* __pb_save = this->pbase();
807 char_type* __epb_save = this->epptr();
808 if (!traits_type::eq_int_type(__c, traits_type::eof()))
809 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500810 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 this->setp(&__1buf, &__1buf+1);
812 *this->pptr() = traits_type::to_char_type(__c);
813 this->pbump(1);
814 }
815 if (this->pptr() != this->pbase())
816 {
817 if (__always_noconv_)
818 {
819 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
820 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
821 return traits_type::eof();
822 }
823 else
824 {
825 char* __extbe = __extbuf_;
826 codecvt_base::result __r;
827 do
828 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000829 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000830 __throw_bad_cast();
831
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 const char_type* __e;
833 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
834 __extbuf_, __extbuf_ + __ebs_, __extbe);
835 if (__e == this->pbase())
836 return traits_type::eof();
837 if (__r == codecvt_base::noconv)
838 {
839 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
840 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
841 return traits_type::eof();
842 }
843 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
844 {
845 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
846 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
847 return traits_type::eof();
848 if (__r == codecvt_base::partial)
849 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000850 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000851 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852 }
853 }
854 else
855 return traits_type::eof();
856 } while (__r == codecvt_base::partial);
857 }
858 this->setp(__pb_save, __epb_save);
859 }
860 return traits_type::not_eof(__c);
861}
862
863template <class _CharT, class _Traits>
864basic_streambuf<_CharT, _Traits>*
865basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
866{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500867 this->setg(nullptr, nullptr, nullptr);
868 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869 if (__owns_eb_)
870 delete [] __extbuf_;
871 if (__owns_ib_)
872 delete [] __intbuf_;
873 __ebs_ = __n;
874 if (__ebs_ > sizeof(__extbuf_min_))
875 {
876 if (__always_noconv_ && __s)
877 {
878 __extbuf_ = (char*)__s;
879 __owns_eb_ = false;
880 }
881 else
882 {
883 __extbuf_ = new char[__ebs_];
884 __owns_eb_ = true;
885 }
886 }
887 else
888 {
889 __extbuf_ = __extbuf_min_;
890 __ebs_ = sizeof(__extbuf_min_);
891 __owns_eb_ = false;
892 }
893 if (!__always_noconv_)
894 {
895 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
896 if (__s && __ibs_ >= sizeof(__extbuf_min_))
897 {
898 __intbuf_ = __s;
899 __owns_ib_ = false;
900 }
901 else
902 {
903 __intbuf_ = new char_type[__ibs_];
904 __owns_ib_ = true;
905 }
906 }
907 else
908 {
909 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500910 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911 __owns_ib_ = false;
912 }
913 return this;
914}
915
916template <class _CharT, class _Traits>
917typename basic_filebuf<_CharT, _Traits>::pos_type
918basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
919 ios_base::openmode)
920{
Howard Hinnant0090b122012-08-24 16:52:47 +0000921 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000922 __throw_bad_cast();
923
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500925 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 return pos_type(off_type(-1));
927 // __width > 0 || __off == 0
928 int __whence;
929 switch (__way)
930 {
931 case ios_base::beg:
932 __whence = SEEK_SET;
933 break;
934 case ios_base::cur:
935 __whence = SEEK_CUR;
936 break;
937 case ios_base::end:
938 __whence = SEEK_END;
939 break;
940 default:
941 return pos_type(off_type(-1));
942 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000943#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000944 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
945 return pos_type(off_type(-1));
946 pos_type __r = ftell(__file_);
947#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
949 return pos_type(off_type(-1));
950 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000951#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 __r.state(__st_);
953 return __r;
954}
955
956template <class _CharT, class _Traits>
957typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000958basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500960 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000962#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000963 if (fseek(__file_, __sp, SEEK_SET))
964 return pos_type(off_type(-1));
965#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 if (fseeko(__file_, __sp, SEEK_SET))
967 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000968#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000969 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 return __sp;
971}
972
973template <class _CharT, class _Traits>
974int
975basic_filebuf<_CharT, _Traits>::sync()
976{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500977 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000979 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000980 __throw_bad_cast();
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 if (__cm_ & ios_base::out)
983 {
984 if (this->pptr() != this->pbase())
985 if (overflow() == traits_type::eof())
986 return -1;
987 codecvt_base::result __r;
988 do
989 {
990 char* __extbe;
991 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
992 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
993 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
994 return -1;
995 } while (__r == codecvt_base::partial);
996 if (__r == codecvt_base::error)
997 return -1;
998 if (fflush(__file_))
999 return -1;
1000 }
1001 else if (__cm_ & ios_base::in)
1002 {
1003 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001004 state_type __state = __st_last_;
1005 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 if (__always_noconv_)
1007 __c = this->egptr() - this->gptr();
1008 else
1009 {
1010 int __width = __cv_->encoding();
1011 __c = __extbufend_ - __extbufnext_;
1012 if (__width > 0)
1013 __c += __width * (this->egptr() - this->gptr());
1014 else
1015 {
1016 if (this->gptr() != this->egptr())
1017 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001018 const int __off = __cv_->length(__state, __extbuf_,
1019 __extbufnext_,
1020 this->gptr() - this->eback());
1021 __c += __extbufnext_ - __extbuf_ - __off;
1022 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 }
1024 }
1025 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001026#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001027 if (fseek(__file_, -__c, SEEK_CUR))
1028 return -1;
1029#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 if (fseeko(__file_, -__c, SEEK_CUR))
1031 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001032#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001033 if (__update_st)
1034 __st_ = __state;
1035 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001036 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 __cm_ = 0;
1038 }
1039 return 0;
1040}
1041
1042template <class _CharT, class _Traits>
1043void
1044basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1045{
1046 sync();
1047 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1048 bool __old_anc = __always_noconv_;
1049 __always_noconv_ = __cv_->always_noconv();
1050 if (__old_anc != __always_noconv_)
1051 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001052 this->setg(nullptr, nullptr, nullptr);
1053 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 // invariant, char_type is char, else we couldn't get here
1055 if (__always_noconv_) // need to dump __intbuf_
1056 {
1057 if (__owns_eb_)
1058 delete [] __extbuf_;
1059 __owns_eb_ = __owns_ib_;
1060 __ebs_ = __ibs_;
1061 __extbuf_ = (char*)__intbuf_;
1062 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001063 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 __owns_ib_ = false;
1065 }
1066 else // need to obtain an __intbuf_.
1067 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1068 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1069 {
1070 __ibs_ = __ebs_;
1071 __intbuf_ = (char_type*)__extbuf_;
1072 __owns_ib_ = false;
1073 __extbuf_ = new char[__ebs_];
1074 __owns_eb_ = true;
1075 }
1076 else
1077 {
1078 __ibs_ = __ebs_;
1079 __intbuf_ = new char_type[__ibs_];
1080 __owns_ib_ = true;
1081 }
1082 }
1083 }
1084}
1085
1086template <class _CharT, class _Traits>
1087bool
1088basic_filebuf<_CharT, _Traits>::__read_mode()
1089{
1090 if (!(__cm_ & ios_base::in))
1091 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001092 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 if (__always_noconv_)
1094 this->setg((char_type*)__extbuf_,
1095 (char_type*)__extbuf_ + __ebs_,
1096 (char_type*)__extbuf_ + __ebs_);
1097 else
1098 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1099 __cm_ = ios_base::in;
1100 return true;
1101 }
1102 return false;
1103}
1104
1105template <class _CharT, class _Traits>
1106void
1107basic_filebuf<_CharT, _Traits>::__write_mode()
1108{
1109 if (!(__cm_ & ios_base::out))
1110 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001111 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 if (__ebs_ > sizeof(__extbuf_min_))
1113 {
1114 if (__always_noconv_)
1115 this->setp((char_type*)__extbuf_,
1116 (char_type*)__extbuf_ + (__ebs_ - 1));
1117 else
1118 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1119 }
1120 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001121 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 __cm_ = ios_base::out;
1123 }
1124}
1125
1126// basic_ifstream
1127
1128template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001129class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 : public basic_istream<_CharT, _Traits>
1131{
1132public:
1133 typedef _CharT char_type;
1134 typedef _Traits traits_type;
1135 typedef typename traits_type::int_type int_type;
1136 typedef typename traits_type::pos_type pos_type;
1137 typedef typename traits_type::off_type off_type;
1138
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_ifstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001143#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1144 _LIBCPP_INLINE_VISIBILITY
1145 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1146#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001149#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001150 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001151 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1152 : basic_ifstream(__p.c_str(), __mode) {}
1153#endif // _LIBCPP_STD_VER >= 17
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 basic_ifstream(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 basic_ifstream& operator=(basic_ifstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 void swap(basic_ifstream& __rhs);
1160
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 bool is_open() const;
1165 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001166#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1167 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1168#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001170#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001171 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001172 void open(const filesystem::path& __p,
1173 ios_base::openmode __mode = ios_base::in) {
1174 return open(__p.c_str(), __mode);
1175 }
1176#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001177
1178 _LIBCPP_INLINE_VISIBILITY
1179 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 void close();
1182
1183private:
1184 basic_filebuf<char_type, traits_type> __sb_;
1185};
1186
1187template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001188inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189basic_ifstream<_CharT, _Traits>::basic_ifstream()
1190 : basic_istream<char_type, traits_type>(&__sb_)
1191{
1192}
1193
1194template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1197 : basic_istream<char_type, traits_type>(&__sb_)
1198{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001199 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 this->setstate(ios_base::failbit);
1201}
1202
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001203#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1204template <class _CharT, class _Traits>
1205inline
1206basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1207 : basic_istream<char_type, traits_type>(&__sb_)
1208{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001209 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001210 this->setstate(ios_base::failbit);
1211}
1212#endif
1213
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001215inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1217 : basic_istream<char_type, traits_type>(&__sb_)
1218{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001219 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 this->setstate(ios_base::failbit);
1221}
1222
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001224inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001226 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1227 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228{
1229 this->set_rdbuf(&__sb_);
1230}
1231
1232template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234basic_ifstream<_CharT, _Traits>&
1235basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1236{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001237 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1238 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 return *this;
1240}
1241
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001243inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244void
1245basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1246{
1247 basic_istream<char_type, traits_type>::swap(__rhs);
1248 __sb_.swap(__rhs.__sb_);
1249}
1250
1251template <class _CharT, class _Traits>
1252inline _LIBCPP_INLINE_VISIBILITY
1253void
1254swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1255{
1256 __x.swap(__y);
1257}
1258
1259template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001260inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261basic_filebuf<_CharT, _Traits>*
1262basic_ifstream<_CharT, _Traits>::rdbuf() const
1263{
1264 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1265}
1266
1267template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001268inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269bool
1270basic_ifstream<_CharT, _Traits>::is_open() const
1271{
1272 return __sb_.is_open();
1273}
1274
1275template <class _CharT, class _Traits>
1276void
1277basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1278{
1279 if (__sb_.open(__s, __mode | ios_base::in))
1280 this->clear();
1281 else
1282 this->setstate(ios_base::failbit);
1283}
1284
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001285#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1286template <class _CharT, class _Traits>
1287void
1288basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1289{
1290 if (__sb_.open(__s, __mode | ios_base::in))
1291 this->clear();
1292 else
1293 this->setstate(ios_base::failbit);
1294}
1295#endif
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <class _CharT, class _Traits>
1298void
1299basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1300{
1301 if (__sb_.open(__s, __mode | ios_base::in))
1302 this->clear();
1303 else
1304 this->setstate(ios_base::failbit);
1305}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001306
1307template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001308inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001309void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1310 ios_base::openmode __mode) {
1311 if (__sb_.__open(__fd, __mode | ios_base::in))
1312 this->clear();
1313 else
1314 this->setstate(ios_base::failbit);
1315}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316
1317template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001318inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319void
1320basic_ifstream<_CharT, _Traits>::close()
1321{
1322 if (__sb_.close() == 0)
1323 this->setstate(ios_base::failbit);
1324}
1325
1326// basic_ofstream
1327
1328template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001329class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 : public basic_ostream<_CharT, _Traits>
1331{
1332public:
1333 typedef _CharT char_type;
1334 typedef _Traits traits_type;
1335 typedef typename traits_type::int_type int_type;
1336 typedef typename traits_type::pos_type pos_type;
1337 typedef typename traits_type::off_type off_type;
1338
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001343#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1344 _LIBCPP_INLINE_VISIBILITY
1345 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1346#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001349
Louis Dionnedb84e122021-01-18 12:18:18 -05001350#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001351 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001352 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1353 : basic_ofstream(__p.c_str(), __mode) {}
1354#endif // _LIBCPP_STD_VER >= 17
1355
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357 basic_ofstream(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 basic_ofstream& operator=(basic_ofstream&& __rhs);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 void swap(basic_ofstream& __rhs);
1362
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 bool is_open() const;
1367 void open(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 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1370#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001372
Louis Dionnedb84e122021-01-18 12:18:18 -05001373#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001374 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001375 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1376 { return open(__p.c_str(), __mode); }
1377#endif // _LIBCPP_STD_VER >= 17
1378
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001379 _LIBCPP_INLINE_VISIBILITY
1380 void __open(int __fd, ios_base::openmode __mode);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 void close();
1383
1384private:
1385 basic_filebuf<char_type, traits_type> __sb_;
1386};
1387
1388template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001389inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390basic_ofstream<_CharT, _Traits>::basic_ofstream()
1391 : basic_ostream<char_type, traits_type>(&__sb_)
1392{
1393}
1394
1395template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001396inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1398 : basic_ostream<char_type, traits_type>(&__sb_)
1399{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001400 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 this->setstate(ios_base::failbit);
1402}
1403
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001404#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1405template <class _CharT, class _Traits>
1406inline
1407basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1408 : basic_ostream<char_type, traits_type>(&__sb_)
1409{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001410 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001411 this->setstate(ios_base::failbit);
1412}
1413#endif
1414
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001416inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1418 : basic_ostream<char_type, traits_type>(&__sb_)
1419{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001420 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 this->setstate(ios_base::failbit);
1422}
1423
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001425inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001427 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1428 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429{
1430 this->set_rdbuf(&__sb_);
1431}
1432
1433template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001434inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435basic_ofstream<_CharT, _Traits>&
1436basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1437{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001438 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1439 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 return *this;
1441}
1442
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445void
1446basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1447{
1448 basic_ostream<char_type, traits_type>::swap(__rhs);
1449 __sb_.swap(__rhs.__sb_);
1450}
1451
1452template <class _CharT, class _Traits>
1453inline _LIBCPP_INLINE_VISIBILITY
1454void
1455swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1456{
1457 __x.swap(__y);
1458}
1459
1460template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001461inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462basic_filebuf<_CharT, _Traits>*
1463basic_ofstream<_CharT, _Traits>::rdbuf() const
1464{
1465 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1466}
1467
1468template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001469inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470bool
1471basic_ofstream<_CharT, _Traits>::is_open() const
1472{
1473 return __sb_.is_open();
1474}
1475
1476template <class _CharT, class _Traits>
1477void
1478basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1479{
1480 if (__sb_.open(__s, __mode | ios_base::out))
1481 this->clear();
1482 else
1483 this->setstate(ios_base::failbit);
1484}
1485
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001486#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1487template <class _CharT, class _Traits>
1488void
1489basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1490{
1491 if (__sb_.open(__s, __mode | ios_base::out))
1492 this->clear();
1493 else
1494 this->setstate(ios_base::failbit);
1495}
1496#endif
1497
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498template <class _CharT, class _Traits>
1499void
1500basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1501{
1502 if (__sb_.open(__s, __mode | ios_base::out))
1503 this->clear();
1504 else
1505 this->setstate(ios_base::failbit);
1506}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001507
1508template <class _CharT, class _Traits>
jasonliu9c5d70a2021-04-12 19:22:12 +00001509inline
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001510void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1511 ios_base::openmode __mode) {
1512 if (__sb_.__open(__fd, __mode | ios_base::out))
1513 this->clear();
1514 else
1515 this->setstate(ios_base::failbit);
1516}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517
1518template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001519inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520void
1521basic_ofstream<_CharT, _Traits>::close()
1522{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001523 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524 this->setstate(ios_base::failbit);
1525}
1526
1527// basic_fstream
1528
1529template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001530class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 : public basic_iostream<_CharT, _Traits>
1532{
1533public:
1534 typedef _CharT char_type;
1535 typedef _Traits traits_type;
1536 typedef typename traits_type::int_type int_type;
1537 typedef typename traits_type::pos_type pos_type;
1538 typedef typename traits_type::off_type off_type;
1539
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 basic_fstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001544#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1545 _LIBCPP_INLINE_VISIBILITY
1546 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1547#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001550
Louis Dionnedb84e122021-01-18 12:18:18 -05001551#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001552 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001553 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1554 : basic_fstream(__p.c_str(), __mode) {}
1555#endif // _LIBCPP_STD_VER >= 17
1556
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 basic_fstream& operator=(basic_fstream&& __rhs);
Arthur O'Dwyer8dcd2982021-06-15 12:47:05 -04001562
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 void swap(basic_fstream& __rhs);
1565
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 bool is_open() const;
1570 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001571#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1572 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1573#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001575
Louis Dionnedb84e122021-01-18 12:18:18 -05001576#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001577 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001578 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1579 { return open(__p.c_str(), __mode); }
1580#endif // _LIBCPP_STD_VER >= 17
1581
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 void close();
1584
1585private:
1586 basic_filebuf<char_type, traits_type> __sb_;
1587};
1588
1589template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001590inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591basic_fstream<_CharT, _Traits>::basic_fstream()
1592 : basic_iostream<char_type, traits_type>(&__sb_)
1593{
1594}
1595
1596template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001597inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1599 : basic_iostream<char_type, traits_type>(&__sb_)
1600{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001601 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 this->setstate(ios_base::failbit);
1603}
1604
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001605#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606template <class _CharT, class _Traits>
1607inline
1608basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1609 : basic_iostream<char_type, traits_type>(&__sb_)
1610{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001611 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001612 this->setstate(ios_base::failbit);
1613}
1614#endif
1615
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001617inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1619 : basic_iostream<char_type, traits_type>(&__sb_)
1620{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001621 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 this->setstate(ios_base::failbit);
1623}
1624
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001626inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001628 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1629 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630{
1631 this->set_rdbuf(&__sb_);
1632}
1633
1634template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001635inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636basic_fstream<_CharT, _Traits>&
1637basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1638{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001639 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1640 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 return *this;
1642}
1643
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646void
1647basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1648{
1649 basic_iostream<char_type, traits_type>::swap(__rhs);
1650 __sb_.swap(__rhs.__sb_);
1651}
1652
1653template <class _CharT, class _Traits>
1654inline _LIBCPP_INLINE_VISIBILITY
1655void
1656swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1657{
1658 __x.swap(__y);
1659}
1660
1661template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001662inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663basic_filebuf<_CharT, _Traits>*
1664basic_fstream<_CharT, _Traits>::rdbuf() const
1665{
1666 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1667}
1668
1669template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001670inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001671bool
1672basic_fstream<_CharT, _Traits>::is_open() const
1673{
1674 return __sb_.is_open();
1675}
1676
1677template <class _CharT, class _Traits>
1678void
1679basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1680{
1681 if (__sb_.open(__s, __mode))
1682 this->clear();
1683 else
1684 this->setstate(ios_base::failbit);
1685}
1686
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001687#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1688template <class _CharT, class _Traits>
1689void
1690basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1691{
1692 if (__sb_.open(__s, __mode))
1693 this->clear();
1694 else
1695 this->setstate(ios_base::failbit);
1696}
1697#endif
1698
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699template <class _CharT, class _Traits>
1700void
1701basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1702{
1703 if (__sb_.open(__s, __mode))
1704 this->clear();
1705 else
1706 this->setstate(ios_base::failbit);
1707}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708
1709template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001710inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711void
1712basic_fstream<_CharT, _Traits>::close()
1713{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001714 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 this->setstate(ios_base::failbit);
1716}
1717
Louis Dionne8a79be62020-10-21 11:11:45 -04001718#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1719_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1720_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1721_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1722#endif
1723
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724_LIBCPP_END_NAMESPACE_STD
1725
Eric Fiselierf4433a32017-05-31 22:07:49 +00001726_LIBCPP_POP_MACROS
1727
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001728#endif // _LIBCPP_FSTREAM