blob: 51b26ecf79e4c82a2acab8608b6943c7c5017bb4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- fstream ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FSTREAM
11#define _LIBCPP_FSTREAM
12
13/*
14 fstream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_filebuf
18 : public basic_streambuf<charT, traits>
19{
20public:
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
26
27 // 27.9.1.2 Constructors/destructor:
28 basic_filebuf();
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
31
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
35
36 // 27.9.1.4 Members:
37 bool is_open() const;
38 basic_filebuf* open(const char* s, ios_base::openmode mode);
39 basic_filebuf* open(const string& s, ios_base::openmode mode);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000040 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 basic_filebuf* close();
42
43protected:
44 // 27.9.1.5 Overridden virtual functions:
45 virtual streamsize showmanyc();
46 virtual int_type underflow();
47 virtual int_type uflow();
48 virtual int_type pbackfail(int_type c = traits_type::eof());
49 virtual int_type overflow (int_type c = traits_type::eof());
50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52 ios_base::openmode which = ios_base::in | ios_base::out);
53 virtual pos_type seekpos(pos_type sp,
54 ios_base::openmode which = ios_base::in | ios_base::out);
55 virtual int sync();
56 virtual void imbue(const locale& loc);
57};
58
59template <class charT, class traits>
60 void
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63typedef basic_filebuf<char> filebuf;
64typedef basic_filebuf<wchar_t> wfilebuf;
65
66template <class charT, class traits = char_traits<charT> >
67class basic_ifstream
68 : public basic_istream<charT,traits>
69{
70public:
71 typedef charT char_type;
72 typedef traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
76
77 basic_ifstream();
78 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
79 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000080 explicit basic_ifstream(const filesystem::path& p,
81 ios_base::openmode mode = ios_base::in); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 basic_ifstream(basic_ifstream&& rhs);
83
84 basic_ifstream& operator=(basic_ifstream&& rhs);
85 void swap(basic_ifstream& rhs);
86
87 basic_filebuf<char_type, traits_type>* rdbuf() const;
88 bool is_open() const;
89 void open(const char* s, ios_base::openmode mode = ios_base::in);
90 void open(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000091 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 void close();
94};
95
96template <class charT, class traits>
97 void
98 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
99
100typedef basic_ifstream<char> ifstream;
101typedef basic_ifstream<wchar_t> wifstream;
102
103template <class charT, class traits = char_traits<charT> >
104class basic_ofstream
105 : public basic_ostream<charT,traits>
106{
107public:
108 typedef charT char_type;
109 typedef traits traits_type;
110 typedef typename traits_type::int_type int_type;
111 typedef typename traits_type::pos_type pos_type;
112 typedef typename traits_type::off_type off_type;
113
114 basic_ofstream();
115 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
116 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000117 explicit basic_ofstream(const filesystem::path& p,
118 ios_base::openmode mode = ios_base::out); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 basic_ofstream(basic_ofstream&& rhs);
120
121 basic_ofstream& operator=(basic_ofstream&& rhs);
122 void swap(basic_ofstream& rhs);
123
124 basic_filebuf<char_type, traits_type>* rdbuf() const;
125 bool is_open() const;
126 void open(const char* s, ios_base::openmode mode = ios_base::out);
127 void open(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000128 void open(const filesystem::path& p,
129 ios_base::openmode mode = ios_base::out); // C++17
130
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 void close();
132};
133
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000134template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 void
136 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
137
138typedef basic_ofstream<char> ofstream;
139typedef basic_ofstream<wchar_t> wofstream;
140
141template <class charT, class traits=char_traits<charT> >
142class basic_fstream
143 : public basic_iostream<charT,traits>
144{
145public:
146 typedef charT char_type;
147 typedef traits traits_type;
148 typedef typename traits_type::int_type int_type;
149 typedef typename traits_type::pos_type pos_type;
150 typedef typename traits_type::off_type off_type;
151
152 basic_fstream();
153 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000155 explicit basic_fstream(const filesystem::path& p,
156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 basic_fstream(basic_fstream&& rhs);
158
159 basic_fstream& operator=(basic_fstream&& rhs);
160 void swap(basic_fstream& rhs);
161
162 basic_filebuf<char_type, traits_type>* rdbuf() const;
163 bool is_open() const;
164 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
165 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000166 void open(const filesystem::path& s,
167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
168
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 void close();
170};
171
172template <class charT, class traits>
173 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
174
175typedef basic_fstream<char> fstream;
176typedef basic_fstream<wchar_t> wfstream;
177
178} // std
179
180*/
181
182#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500183#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184#include <ostream>
185#include <istream>
186#include <__locale>
187#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000188#include <cstdlib>
Louis Dionnedb84e122021-01-18 12:18:18 -0500189
190#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
191# include <filesystem>
192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000194#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
Eric Fiselierf4433a32017-05-31 22:07:49 +0000198_LIBCPP_PUSH_MACROS
199#include <__undef_macros>
200
Mara Sophie Grosch60cbba82021-04-12 14:19:51 -0400201#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
202# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
203#endif
Eric Fiselierf4433a32017-05-31 22:07:49 +0000204
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205_LIBCPP_BEGIN_NAMESPACE_STD
206
207template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000208class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209 : public basic_streambuf<_CharT, _Traits>
210{
211public:
212 typedef _CharT char_type;
213 typedef _Traits traits_type;
214 typedef typename traits_type::int_type int_type;
215 typedef typename traits_type::pos_type pos_type;
216 typedef typename traits_type::off_type off_type;
217 typedef typename traits_type::state_type state_type;
218
219 // 27.9.1.2 Constructors/destructor:
220 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 basic_filebuf(basic_filebuf&& __rhs);
223#endif
224 virtual ~basic_filebuf();
225
226 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000227#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 basic_filebuf& operator=(basic_filebuf&& __rhs);
230#endif
231 void swap(basic_filebuf& __rhs);
232
233 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000236#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000238#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
239 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
240#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000243
Louis Dionnedb84e122021-01-18 12:18:18 -0500244#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000245 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000246 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
247 return open(__p.c_str(), __mode);
248 }
249#endif
Louis Dionne8a79be62020-10-21 11:11:45 -0400250 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000251 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253 basic_filebuf* close();
254
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000255 _LIBCPP_INLINE_VISIBILITY
256 inline static const char*
257 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
258
259 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260 // 27.9.1.5 Overridden virtual functions:
261 virtual int_type underflow();
262 virtual int_type pbackfail(int_type __c = traits_type::eof());
263 virtual int_type overflow (int_type __c = traits_type::eof());
264 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
265 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
266 ios_base::openmode __wch = ios_base::in | ios_base::out);
267 virtual pos_type seekpos(pos_type __sp,
268 ios_base::openmode __wch = ios_base::in | ios_base::out);
269 virtual int sync();
270 virtual void imbue(const locale& __loc);
271
272private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000273 char* __extbuf_;
274 const char* __extbufnext_;
275 const char* __extbufend_;
276 char __extbuf_min_[8];
277 size_t __ebs_;
278 char_type* __intbuf_;
279 size_t __ibs_;
280 FILE* __file_;
281 const codecvt<char_type, char, state_type>* __cv_;
282 state_type __st_;
283 state_type __st_last_;
284 ios_base::openmode __om_;
285 ios_base::openmode __cm_;
286 bool __owns_eb_;
287 bool __owns_ib_;
288 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000290 bool __read_mode();
291 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292};
293
294template <class _CharT, class _Traits>
295basic_filebuf<_CharT, _Traits>::basic_filebuf()
Bruce Mitchener170d8972020-11-24 12:53:53 -0500296 : __extbuf_(nullptr),
297 __extbufnext_(nullptr),
298 __extbufend_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 __ebs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500300 __intbuf_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __ibs_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -0500302 __file_(nullptr),
Howard Hinnant0090b122012-08-24 16:52:47 +0000303 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000305 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 __om_(0),
307 __cm_(0),
308 __owns_eb_(false),
309 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000310 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311{
Howard Hinnant0090b122012-08-24 16:52:47 +0000312 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
313 {
314 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
315 __always_noconv_ = __cv_->always_noconv();
316 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500317 setbuf(nullptr, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318}
319
Eric Fiselier78ccf772017-04-18 23:38:41 +0000320#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
322template <class _CharT, class _Traits>
323basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
324 : basic_streambuf<_CharT, _Traits>(__rhs)
325{
326 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
327 {
328 __extbuf_ = __extbuf_min_;
329 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
330 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
331 }
332 else
333 {
334 __extbuf_ = __rhs.__extbuf_;
335 __extbufnext_ = __rhs.__extbufnext_;
336 __extbufend_ = __rhs.__extbufend_;
337 }
338 __ebs_ = __rhs.__ebs_;
339 __intbuf_ = __rhs.__intbuf_;
340 __ibs_ = __rhs.__ibs_;
341 __file_ = __rhs.__file_;
342 __cv_ = __rhs.__cv_;
343 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000344 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 __om_ = __rhs.__om_;
346 __cm_ = __rhs.__cm_;
347 __owns_eb_ = __rhs.__owns_eb_;
348 __owns_ib_ = __rhs.__owns_ib_;
349 __always_noconv_ = __rhs.__always_noconv_;
350 if (__rhs.pbase())
351 {
352 if (__rhs.pbase() == __rhs.__intbuf_)
353 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
354 else
355 this->setp((char_type*)__extbuf_,
356 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000357 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 }
359 else if (__rhs.eback())
360 {
361 if (__rhs.eback() == __rhs.__intbuf_)
362 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
363 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
364 else
365 this->setg((char_type*)__extbuf_,
366 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
367 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
368 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500369 __rhs.__extbuf_ = nullptr;
370 __rhs.__extbufnext_ = nullptr;
371 __rhs.__extbufend_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__ebs_ = 0;
373 __rhs.__intbuf_ = 0;
374 __rhs.__ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500375 __rhs.__file_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000377 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 __rhs.__om_ = 0;
379 __rhs.__cm_ = 0;
380 __rhs.__owns_eb_ = false;
381 __rhs.__owns_ib_ = false;
382 __rhs.setg(0, 0, 0);
383 __rhs.setp(0, 0);
384}
385
386template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388basic_filebuf<_CharT, _Traits>&
389basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
390{
391 close();
392 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000393 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394}
395
Eric Fiselier78ccf772017-04-18 23:38:41 +0000396#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template <class _CharT, class _Traits>
399basic_filebuf<_CharT, _Traits>::~basic_filebuf()
400{
401#ifndef _LIBCPP_NO_EXCEPTIONS
402 try
403 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 close();
406#ifndef _LIBCPP_NO_EXCEPTIONS
407 }
408 catch (...)
409 {
410 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000411#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 if (__owns_eb_)
413 delete [] __extbuf_;
414 if (__owns_ib_)
415 delete [] __intbuf_;
416}
417
418template <class _CharT, class _Traits>
419void
420basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
421{
422 basic_streambuf<char_type, traits_type>::swap(__rhs);
423 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
424 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000425 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
426 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
427 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 }
429 else
430 {
431 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
432 ptrdiff_t __le = __extbufend_ - __extbuf_;
433 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
434 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
435 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
436 {
437 __extbuf_ = __rhs.__extbuf_;
438 __rhs.__extbuf_ = __rhs.__extbuf_min_;
439 }
440 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
441 {
442 __rhs.__extbuf_ = __extbuf_;
443 __extbuf_ = __extbuf_min_;
444 }
445 __extbufnext_ = __extbuf_ + __rn;
446 __extbufend_ = __extbuf_ + __re;
447 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
448 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
449 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000450 _VSTD::swap(__ebs_, __rhs.__ebs_);
451 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
452 _VSTD::swap(__ibs_, __rhs.__ibs_);
453 _VSTD::swap(__file_, __rhs.__file_);
454 _VSTD::swap(__cv_, __rhs.__cv_);
455 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000456 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000457 _VSTD::swap(__om_, __rhs.__om_);
458 _VSTD::swap(__cm_, __rhs.__cm_);
459 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
460 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
461 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
463 {
464 ptrdiff_t __n = this->gptr() - this->eback();
465 ptrdiff_t __e = this->egptr() - this->eback();
466 this->setg((char_type*)__extbuf_min_,
467 (char_type*)__extbuf_min_ + __n,
468 (char_type*)__extbuf_min_ + __e);
469 }
470 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
471 {
472 ptrdiff_t __n = this->pptr() - this->pbase();
473 ptrdiff_t __e = this->epptr() - this->pbase();
474 this->setp((char_type*)__extbuf_min_,
475 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000476 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477 }
478 if (__rhs.eback() == (char_type*)__extbuf_min_)
479 {
480 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
481 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
482 __rhs.setg((char_type*)__rhs.__extbuf_min_,
483 (char_type*)__rhs.__extbuf_min_ + __n,
484 (char_type*)__rhs.__extbuf_min_ + __e);
485 }
486 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
487 {
488 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
489 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
490 __rhs.setp((char_type*)__rhs.__extbuf_min_,
491 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000492 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493 }
494}
495
496template <class _CharT, class _Traits>
497inline _LIBCPP_INLINE_VISIBILITY
498void
499swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
500{
501 __x.swap(__y);
502}
503
504template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000505inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506bool
507basic_filebuf<_CharT, _Traits>::is_open() const
508{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500509 return __file_ != nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510}
511
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000512template <class _CharT, class _Traits>
513const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
514 ios_base::openmode __mode) _NOEXCEPT {
515 switch (__mode & ~ios_base::ate) {
516 case ios_base::out:
517 case ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000518 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000519 case ios_base::out | ios_base::app:
520 case ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000521 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000522 case ios_base::in:
Dan Albert9550df62019-09-16 19:26:41 +0000523 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000524 case ios_base::in | ios_base::out:
Dan Albert9550df62019-09-16 19:26:41 +0000525 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000526 case ios_base::in | ios_base::out | ios_base::trunc:
Dan Albert9550df62019-09-16 19:26:41 +0000527 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000528 case ios_base::in | ios_base::out | ios_base::app:
529 case ios_base::in | ios_base::app:
Dan Albert9550df62019-09-16 19:26:41 +0000530 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000531 case ios_base::out | ios_base::binary:
532 case ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000533 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 case ios_base::out | ios_base::app | ios_base::binary:
535 case ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000536 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000537 case ios_base::in | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000538 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000539 case ios_base::in | ios_base::out | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000540 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000541 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000542 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000543 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
544 case ios_base::in | ios_base::app | ios_base::binary:
Dan Albert9550df62019-09-16 19:26:41 +0000545 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000546 default:
547 return nullptr;
548 }
549 _LIBCPP_UNREACHABLE();
550}
551
Ed Schouten7009f4e2015-03-12 15:44:39 +0000552#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553template <class _CharT, class _Traits>
554basic_filebuf<_CharT, _Traits>*
555basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
556{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500557 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
558 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000560 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000562 __file_ = fopen(__s, __mdstr);
563 if (__file_) {
564 __om_ = __mode;
565 if (__mode & ios_base::ate) {
566 if (fseek(__file_, 0, SEEK_END)) {
567 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500568 __file_ = nullptr;
569 __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000571 }
572 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500573 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000574 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 }
576 return __rt;
577}
578
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000579template <class _CharT, class _Traits>
Louis Dionne8a79be62020-10-21 11:11:45 -0400580inline _LIBCPP_INLINE_VISIBILITY
581basic_filebuf<_CharT, _Traits>*
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000582basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500583 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
584 if (__file_ == nullptr) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000585 if (const char* __mdstr = __make_mdstring(__mode)) {
586 __rt = this;
587 __file_ = fdopen(__fd, __mdstr);
588 if (__file_) {
589 __om_ = __mode;
590 if (__mode & ios_base::ate) {
591 if (fseek(__file_, 0, SEEK_END)) {
592 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500593 __file_ = nullptr;
594 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000595 }
596 }
597 } else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500598 __rt = nullptr;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000599 }
600 }
601 return __rt;
602}
603
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000604#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
605// This is basically the same as the char* overload except that it uses _wfopen
606// and long mode strings.
607template <class _CharT, class _Traits>
608basic_filebuf<_CharT, _Traits>*
609basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
610{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500611 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
612 if (__file_ == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000613 {
614 __rt = this;
615 const wchar_t* __mdstr;
616 switch (__mode & ~ios_base::ate)
617 {
618 case ios_base::out:
619 case ios_base::out | ios_base::trunc:
620 __mdstr = L"w";
621 break;
622 case ios_base::out | ios_base::app:
623 case ios_base::app:
624 __mdstr = L"a";
625 break;
626 case ios_base::in:
627 __mdstr = L"r";
628 break;
629 case ios_base::in | ios_base::out:
630 __mdstr = L"r+";
631 break;
632 case ios_base::in | ios_base::out | ios_base::trunc:
633 __mdstr = L"w+";
634 break;
635 case ios_base::in | ios_base::out | ios_base::app:
636 case ios_base::in | ios_base::app:
637 __mdstr = L"a+";
638 break;
639 case ios_base::out | ios_base::binary:
640 case ios_base::out | ios_base::trunc | ios_base::binary:
641 __mdstr = L"wb";
642 break;
643 case ios_base::out | ios_base::app | ios_base::binary:
644 case ios_base::app | ios_base::binary:
645 __mdstr = L"ab";
646 break;
647 case ios_base::in | ios_base::binary:
648 __mdstr = L"rb";
649 break;
650 case ios_base::in | ios_base::out | ios_base::binary:
651 __mdstr = L"r+b";
652 break;
653 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
654 __mdstr = L"w+b";
655 break;
656 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
657 case ios_base::in | ios_base::app | ios_base::binary:
658 __mdstr = L"a+b";
659 break;
660 default:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500661 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000662 break;
663 }
664 if (__rt)
665 {
666 __file_ = _wfopen(__s, __mdstr);
667 if (__file_)
668 {
669 __om_ = __mode;
670 if (__mode & ios_base::ate)
671 {
672 if (fseek(__file_, 0, SEEK_END))
673 {
674 fclose(__file_);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500675 __file_ = nullptr;
676 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000677 }
678 }
679 }
680 else
Bruce Mitchener170d8972020-11-24 12:53:53 -0500681 __rt = nullptr;
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000682 }
683 }
684 return __rt;
685}
686#endif
687
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690basic_filebuf<_CharT, _Traits>*
691basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
692{
693 return open(__s.c_str(), __mode);
694}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000695#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696
697template <class _CharT, class _Traits>
698basic_filebuf<_CharT, _Traits>*
699basic_filebuf<_CharT, _Traits>::close()
700{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500701 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 if (__file_)
703 {
704 __rt = this;
705 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000706 if (sync())
Bruce Mitchener170d8972020-11-24 12:53:53 -0500707 __rt = nullptr;
Petr Hosekca6421b2019-07-22 19:54:34 +0000708 if (fclose(__h.release()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500709 __rt = nullptr;
710 __file_ = nullptr;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000711 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 }
713 return __rt;
714}
715
716template <class _CharT, class _Traits>
717typename basic_filebuf<_CharT, _Traits>::int_type
718basic_filebuf<_CharT, _Traits>::underflow()
719{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500720 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 return traits_type::eof();
722 bool __initial = __read_mode();
723 char_type __1buf;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500724 if (this->gptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 this->setg(&__1buf, &__1buf+1, &__1buf+1);
726 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
727 int_type __c = traits_type::eof();
728 if (this->gptr() == this->egptr())
729 {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500730 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 if (__always_noconv_)
732 {
733 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
734 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
735 if (__nmemb != 0)
736 {
737 this->setg(this->eback(),
738 this->eback() + __unget_sz,
739 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000740 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 }
742 }
743 else
744 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000745 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
746 if (__extbufend_ != __extbufnext_)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500747 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
749 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000750 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 static_cast<size_t>(__extbufend_ - __extbufnext_));
752 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000753 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000754 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 if (__nr != 0)
756 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000757 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000758 __throw_bad_cast();
759
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 __extbufend_ = __extbufnext_ + __nr;
761 char_type* __inext;
762 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
763 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000764 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 if (__r == codecvt_base::noconv)
766 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000767 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000768 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000769 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 }
771 else if (__inext != this->eback() + __unget_sz)
772 {
773 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000774 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 }
776 }
777 }
778 }
779 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000780 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 if (this->eback() == &__1buf)
Bruce Mitchener170d8972020-11-24 12:53:53 -0500782 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 return __c;
784}
785
786template <class _CharT, class _Traits>
787typename basic_filebuf<_CharT, _Traits>::int_type
788basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
789{
790 if (__file_ && this->eback() < this->gptr())
791 {
792 if (traits_type::eq_int_type(__c, traits_type::eof()))
793 {
794 this->gbump(-1);
795 return traits_type::not_eof(__c);
796 }
797 if ((__om_ & ios_base::out) ||
798 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
799 {
800 this->gbump(-1);
801 *this->gptr() = traits_type::to_char_type(__c);
802 return __c;
803 }
804 }
805 return traits_type::eof();
806}
807
808template <class _CharT, class _Traits>
809typename basic_filebuf<_CharT, _Traits>::int_type
810basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
811{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500812 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 return traits_type::eof();
814 __write_mode();
815 char_type __1buf;
816 char_type* __pb_save = this->pbase();
817 char_type* __epb_save = this->epptr();
818 if (!traits_type::eq_int_type(__c, traits_type::eof()))
819 {
Bruce Mitchener170d8972020-11-24 12:53:53 -0500820 if (this->pptr() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 this->setp(&__1buf, &__1buf+1);
822 *this->pptr() = traits_type::to_char_type(__c);
823 this->pbump(1);
824 }
825 if (this->pptr() != this->pbase())
826 {
827 if (__always_noconv_)
828 {
829 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
830 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
831 return traits_type::eof();
832 }
833 else
834 {
835 char* __extbe = __extbuf_;
836 codecvt_base::result __r;
837 do
838 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000839 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000840 __throw_bad_cast();
841
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 const char_type* __e;
843 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
844 __extbuf_, __extbuf_ + __ebs_, __extbe);
845 if (__e == this->pbase())
846 return traits_type::eof();
847 if (__r == codecvt_base::noconv)
848 {
849 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
850 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
851 return traits_type::eof();
852 }
853 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
854 {
855 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
856 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
857 return traits_type::eof();
858 if (__r == codecvt_base::partial)
859 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000860 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000861 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 }
863 }
864 else
865 return traits_type::eof();
866 } while (__r == codecvt_base::partial);
867 }
868 this->setp(__pb_save, __epb_save);
869 }
870 return traits_type::not_eof(__c);
871}
872
873template <class _CharT, class _Traits>
874basic_streambuf<_CharT, _Traits>*
875basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
876{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500877 this->setg(nullptr, nullptr, nullptr);
878 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 if (__owns_eb_)
880 delete [] __extbuf_;
881 if (__owns_ib_)
882 delete [] __intbuf_;
883 __ebs_ = __n;
884 if (__ebs_ > sizeof(__extbuf_min_))
885 {
886 if (__always_noconv_ && __s)
887 {
888 __extbuf_ = (char*)__s;
889 __owns_eb_ = false;
890 }
891 else
892 {
893 __extbuf_ = new char[__ebs_];
894 __owns_eb_ = true;
895 }
896 }
897 else
898 {
899 __extbuf_ = __extbuf_min_;
900 __ebs_ = sizeof(__extbuf_min_);
901 __owns_eb_ = false;
902 }
903 if (!__always_noconv_)
904 {
905 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
906 if (__s && __ibs_ >= sizeof(__extbuf_min_))
907 {
908 __intbuf_ = __s;
909 __owns_ib_ = false;
910 }
911 else
912 {
913 __intbuf_ = new char_type[__ibs_];
914 __owns_ib_ = true;
915 }
916 }
917 else
918 {
919 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500920 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921 __owns_ib_ = false;
922 }
923 return this;
924}
925
926template <class _CharT, class _Traits>
927typename basic_filebuf<_CharT, _Traits>::pos_type
928basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
929 ios_base::openmode)
930{
Howard Hinnant0090b122012-08-24 16:52:47 +0000931 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000932 __throw_bad_cast();
933
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 int __width = __cv_->encoding();
Bruce Mitchener170d8972020-11-24 12:53:53 -0500935 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 return pos_type(off_type(-1));
937 // __width > 0 || __off == 0
938 int __whence;
939 switch (__way)
940 {
941 case ios_base::beg:
942 __whence = SEEK_SET;
943 break;
944 case ios_base::cur:
945 __whence = SEEK_CUR;
946 break;
947 case ios_base::end:
948 __whence = SEEK_END;
949 break;
950 default:
951 return pos_type(off_type(-1));
952 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000953#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000954 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
955 return pos_type(off_type(-1));
956 pos_type __r = ftell(__file_);
957#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
959 return pos_type(off_type(-1));
960 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962 __r.state(__st_);
963 return __r;
964}
965
966template <class _CharT, class _Traits>
967typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000968basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500970 if (__file_ == nullptr || sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000972#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000973 if (fseek(__file_, __sp, SEEK_SET))
974 return pos_type(off_type(-1));
975#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 if (fseeko(__file_, __sp, SEEK_SET))
977 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000978#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000979 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 return __sp;
981}
982
983template <class _CharT, class _Traits>
984int
985basic_filebuf<_CharT, _Traits>::sync()
986{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500987 if (__file_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000989 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000990 __throw_bad_cast();
991
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 if (__cm_ & ios_base::out)
993 {
994 if (this->pptr() != this->pbase())
995 if (overflow() == traits_type::eof())
996 return -1;
997 codecvt_base::result __r;
998 do
999 {
1000 char* __extbe;
1001 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1002 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1003 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1004 return -1;
1005 } while (__r == codecvt_base::partial);
1006 if (__r == codecvt_base::error)
1007 return -1;
1008 if (fflush(__file_))
1009 return -1;
1010 }
1011 else if (__cm_ & ios_base::in)
1012 {
1013 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001014 state_type __state = __st_last_;
1015 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 if (__always_noconv_)
1017 __c = this->egptr() - this->gptr();
1018 else
1019 {
1020 int __width = __cv_->encoding();
1021 __c = __extbufend_ - __extbufnext_;
1022 if (__width > 0)
1023 __c += __width * (this->egptr() - this->gptr());
1024 else
1025 {
1026 if (this->gptr() != this->egptr())
1027 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001028 const int __off = __cv_->length(__state, __extbuf_,
1029 __extbufnext_,
1030 this->gptr() - this->eback());
1031 __c += __extbufnext_ - __extbuf_ - __off;
1032 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 }
1034 }
1035 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001036#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001037 if (fseek(__file_, -__c, SEEK_CUR))
1038 return -1;
1039#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 if (fseeko(__file_, -__c, SEEK_CUR))
1041 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001042#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001043 if (__update_st)
1044 __st_ = __state;
1045 __extbufnext_ = __extbufend_ = __extbuf_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001046 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 __cm_ = 0;
1048 }
1049 return 0;
1050}
1051
1052template <class _CharT, class _Traits>
1053void
1054basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1055{
1056 sync();
1057 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1058 bool __old_anc = __always_noconv_;
1059 __always_noconv_ = __cv_->always_noconv();
1060 if (__old_anc != __always_noconv_)
1061 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001062 this->setg(nullptr, nullptr, nullptr);
1063 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 // invariant, char_type is char, else we couldn't get here
1065 if (__always_noconv_) // need to dump __intbuf_
1066 {
1067 if (__owns_eb_)
1068 delete [] __extbuf_;
1069 __owns_eb_ = __owns_ib_;
1070 __ebs_ = __ibs_;
1071 __extbuf_ = (char*)__intbuf_;
1072 __ibs_ = 0;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001073 __intbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 __owns_ib_ = false;
1075 }
1076 else // need to obtain an __intbuf_.
1077 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1078 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1079 {
1080 __ibs_ = __ebs_;
1081 __intbuf_ = (char_type*)__extbuf_;
1082 __owns_ib_ = false;
1083 __extbuf_ = new char[__ebs_];
1084 __owns_eb_ = true;
1085 }
1086 else
1087 {
1088 __ibs_ = __ebs_;
1089 __intbuf_ = new char_type[__ibs_];
1090 __owns_ib_ = true;
1091 }
1092 }
1093 }
1094}
1095
1096template <class _CharT, class _Traits>
1097bool
1098basic_filebuf<_CharT, _Traits>::__read_mode()
1099{
1100 if (!(__cm_ & ios_base::in))
1101 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001102 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 if (__always_noconv_)
1104 this->setg((char_type*)__extbuf_,
1105 (char_type*)__extbuf_ + __ebs_,
1106 (char_type*)__extbuf_ + __ebs_);
1107 else
1108 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1109 __cm_ = ios_base::in;
1110 return true;
1111 }
1112 return false;
1113}
1114
1115template <class _CharT, class _Traits>
1116void
1117basic_filebuf<_CharT, _Traits>::__write_mode()
1118{
1119 if (!(__cm_ & ios_base::out))
1120 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001121 this->setg(nullptr, nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 if (__ebs_ > sizeof(__extbuf_min_))
1123 {
1124 if (__always_noconv_)
1125 this->setp((char_type*)__extbuf_,
1126 (char_type*)__extbuf_ + (__ebs_ - 1));
1127 else
1128 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1129 }
1130 else
Bruce Mitchener170d8972020-11-24 12:53:53 -05001131 this->setp(nullptr, nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 __cm_ = ios_base::out;
1133 }
1134}
1135
1136// basic_ifstream
1137
1138template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001139class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 : public basic_istream<_CharT, _Traits>
1141{
1142public:
1143 typedef _CharT char_type;
1144 typedef _Traits traits_type;
1145 typedef typename traits_type::int_type int_type;
1146 typedef typename traits_type::pos_type pos_type;
1147 typedef typename traits_type::off_type off_type;
1148
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001151#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001154#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1155 _LIBCPP_INLINE_VISIBILITY
1156 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1157#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001160#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001161 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001162 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1163 : basic_ifstream(__p.c_str(), __mode) {}
1164#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001165#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001166#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 basic_ifstream& operator=(basic_ifstream&& __rhs);
1172#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 void swap(basic_ifstream& __rhs);
1175
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001180#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001182#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1183 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1184#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Louis Dionnedb84e122021-01-18 12:18:18 -05001186#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001187 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001188 void open(const filesystem::path& __p,
1189 ios_base::openmode __mode = ios_base::in) {
1190 return open(__p.c_str(), __mode);
1191 }
1192#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001193
1194 _LIBCPP_INLINE_VISIBILITY
1195 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001196#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 void close();
1199
1200private:
1201 basic_filebuf<char_type, traits_type> __sb_;
1202};
1203
1204template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206basic_ifstream<_CharT, _Traits>::basic_ifstream()
1207 : basic_istream<char_type, traits_type>(&__sb_)
1208{
1209}
1210
Ed Schouten7009f4e2015-03-12 15:44:39 +00001211#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1215 : basic_istream<char_type, traits_type>(&__sb_)
1216{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001217 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 this->setstate(ios_base::failbit);
1219}
1220
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001221#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1222template <class _CharT, class _Traits>
1223inline
1224basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1225 : basic_istream<char_type, traits_type>(&__sb_)
1226{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001227 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001228 this->setstate(ios_base::failbit);
1229}
1230#endif
1231
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1235 : basic_istream<char_type, traits_type>(&__sb_)
1236{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001237 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 this->setstate(ios_base::failbit);
1239}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Eric Fiselier78ccf772017-04-18 23:38:41 +00001242#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
1244template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001247 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1248 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249{
1250 this->set_rdbuf(&__sb_);
1251}
1252
1253template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001254inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255basic_ifstream<_CharT, _Traits>&
1256basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001258 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1259 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 return *this;
1261}
1262
Eric Fiselier78ccf772017-04-18 23:38:41 +00001263#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
1265template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001266inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267void
1268basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1269{
1270 basic_istream<char_type, traits_type>::swap(__rhs);
1271 __sb_.swap(__rhs.__sb_);
1272}
1273
1274template <class _CharT, class _Traits>
1275inline _LIBCPP_INLINE_VISIBILITY
1276void
1277swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1278{
1279 __x.swap(__y);
1280}
1281
1282template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001283inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284basic_filebuf<_CharT, _Traits>*
1285basic_ifstream<_CharT, _Traits>::rdbuf() const
1286{
1287 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1288}
1289
1290template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001291inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292bool
1293basic_ifstream<_CharT, _Traits>::is_open() const
1294{
1295 return __sb_.is_open();
1296}
1297
Ed Schouten7009f4e2015-03-12 15:44:39 +00001298#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299template <class _CharT, class _Traits>
1300void
1301basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1302{
1303 if (__sb_.open(__s, __mode | ios_base::in))
1304 this->clear();
1305 else
1306 this->setstate(ios_base::failbit);
1307}
1308
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001309#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1310template <class _CharT, class _Traits>
1311void
1312basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1313{
1314 if (__sb_.open(__s, __mode | ios_base::in))
1315 this->clear();
1316 else
1317 this->setstate(ios_base::failbit);
1318}
1319#endif
1320
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321template <class _CharT, class _Traits>
1322void
1323basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1324{
1325 if (__sb_.open(__s, __mode | ios_base::in))
1326 this->clear();
1327 else
1328 this->setstate(ios_base::failbit);
1329}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001330
1331template <class _CharT, class _Traits>
1332void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1333 ios_base::openmode __mode) {
1334 if (__sb_.__open(__fd, __mode | ios_base::in))
1335 this->clear();
1336 else
1337 this->setstate(ios_base::failbit);
1338}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001339#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001342inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343void
1344basic_ifstream<_CharT, _Traits>::close()
1345{
1346 if (__sb_.close() == 0)
1347 this->setstate(ios_base::failbit);
1348}
1349
1350// basic_ofstream
1351
1352template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001353class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 : public basic_ostream<_CharT, _Traits>
1355{
1356public:
1357 typedef _CharT char_type;
1358 typedef _Traits traits_type;
1359 typedef typename traits_type::int_type int_type;
1360 typedef typename traits_type::pos_type pos_type;
1361 typedef typename traits_type::off_type off_type;
1362
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 explicit basic_ofstream(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 _LIBCPP_INLINE_VISIBILITY
1369 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1370#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001373
Louis Dionnedb84e122021-01-18 12:18:18 -05001374#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001375 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001376 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1377 : basic_ofstream(__p.c_str(), __mode) {}
1378#endif // _LIBCPP_STD_VER >= 17
1379
Eric Fiselier78ccf772017-04-18 23:38:41 +00001380#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 basic_ofstream& operator=(basic_ofstream&& __rhs);
1386#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 void swap(basic_ofstream& __rhs);
1389
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001394#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001396#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1397 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1398#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001400
Louis Dionnedb84e122021-01-18 12:18:18 -05001401#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001402 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001403 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1404 { return open(__p.c_str(), __mode); }
1405#endif // _LIBCPP_STD_VER >= 17
1406
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001407 _LIBCPP_INLINE_VISIBILITY
1408 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001409#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 void close();
1412
1413private:
1414 basic_filebuf<char_type, traits_type> __sb_;
1415};
1416
1417template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001418inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419basic_ofstream<_CharT, _Traits>::basic_ofstream()
1420 : basic_ostream<char_type, traits_type>(&__sb_)
1421{
1422}
1423
Ed Schouten7009f4e2015-03-12 15:44:39 +00001424#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001426inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1428 : basic_ostream<char_type, traits_type>(&__sb_)
1429{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001430 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 this->setstate(ios_base::failbit);
1432}
1433
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001434#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1435template <class _CharT, class _Traits>
1436inline
1437basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1438 : basic_ostream<char_type, traits_type>(&__sb_)
1439{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001440 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001441 this->setstate(ios_base::failbit);
1442}
1443#endif
1444
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001446inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1448 : basic_ostream<char_type, traits_type>(&__sb_)
1449{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001450 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451 this->setstate(ios_base::failbit);
1452}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001453#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
Eric Fiselier78ccf772017-04-18 23:38:41 +00001455#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456
1457template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001458inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001460 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1461 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462{
1463 this->set_rdbuf(&__sb_);
1464}
1465
1466template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001467inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468basic_ofstream<_CharT, _Traits>&
1469basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1470{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001471 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1472 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473 return *this;
1474}
1475
Eric Fiselier78ccf772017-04-18 23:38:41 +00001476#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
1478template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001479inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480void
1481basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1482{
1483 basic_ostream<char_type, traits_type>::swap(__rhs);
1484 __sb_.swap(__rhs.__sb_);
1485}
1486
1487template <class _CharT, class _Traits>
1488inline _LIBCPP_INLINE_VISIBILITY
1489void
1490swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1491{
1492 __x.swap(__y);
1493}
1494
1495template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001496inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497basic_filebuf<_CharT, _Traits>*
1498basic_ofstream<_CharT, _Traits>::rdbuf() const
1499{
1500 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1501}
1502
1503template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001504inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505bool
1506basic_ofstream<_CharT, _Traits>::is_open() const
1507{
1508 return __sb_.is_open();
1509}
1510
Ed Schouten7009f4e2015-03-12 15:44:39 +00001511#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512template <class _CharT, class _Traits>
1513void
1514basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1515{
1516 if (__sb_.open(__s, __mode | ios_base::out))
1517 this->clear();
1518 else
1519 this->setstate(ios_base::failbit);
1520}
1521
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001522#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1523template <class _CharT, class _Traits>
1524void
1525basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1526{
1527 if (__sb_.open(__s, __mode | ios_base::out))
1528 this->clear();
1529 else
1530 this->setstate(ios_base::failbit);
1531}
1532#endif
1533
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534template <class _CharT, class _Traits>
1535void
1536basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1537{
1538 if (__sb_.open(__s, __mode | ios_base::out))
1539 this->clear();
1540 else
1541 this->setstate(ios_base::failbit);
1542}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001543
1544template <class _CharT, class _Traits>
1545void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1546 ios_base::openmode __mode) {
1547 if (__sb_.__open(__fd, __mode | ios_base::out))
1548 this->clear();
1549 else
1550 this->setstate(ios_base::failbit);
1551}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001552#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553
1554template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556void
1557basic_ofstream<_CharT, _Traits>::close()
1558{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001559 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 this->setstate(ios_base::failbit);
1561}
1562
1563// basic_fstream
1564
1565template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001566class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 : public basic_iostream<_CharT, _Traits>
1568{
1569public:
1570 typedef _CharT char_type;
1571 typedef _Traits traits_type;
1572 typedef typename traits_type::int_type int_type;
1573 typedef typename traits_type::pos_type pos_type;
1574 typedef typename traits_type::off_type off_type;
1575
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001578#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001581#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1582 _LIBCPP_INLINE_VISIBILITY
1583 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1584#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001587
Louis Dionnedb84e122021-01-18 12:18:18 -05001588#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001589 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001590 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1591 : basic_fstream(__p.c_str(), __mode) {}
1592#endif // _LIBCPP_STD_VER >= 17
1593
Ed Schouten7009f4e2015-03-12 15:44:39 +00001594#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001595#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 basic_fstream& operator=(basic_fstream&& __rhs);
1601#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 void swap(basic_fstream& __rhs);
1604
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001609#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001611#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1612 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1613#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001615
Louis Dionnedb84e122021-01-18 12:18:18 -05001616#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001617 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001618 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1619 { return open(__p.c_str(), __mode); }
1620#endif // _LIBCPP_STD_VER >= 17
1621
Ed Schouten7009f4e2015-03-12 15:44:39 +00001622#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 void close();
1625
1626private:
1627 basic_filebuf<char_type, traits_type> __sb_;
1628};
1629
1630template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001631inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632basic_fstream<_CharT, _Traits>::basic_fstream()
1633 : basic_iostream<char_type, traits_type>(&__sb_)
1634{
1635}
1636
Ed Schouten7009f4e2015-03-12 15:44:39 +00001637#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001639inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1641 : basic_iostream<char_type, traits_type>(&__sb_)
1642{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001643 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 this->setstate(ios_base::failbit);
1645}
1646
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001647#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1648template <class _CharT, class _Traits>
1649inline
1650basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1651 : basic_iostream<char_type, traits_type>(&__sb_)
1652{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001653 if (__sb_.open(__s, __mode) == nullptr)
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001654 this->setstate(ios_base::failbit);
1655}
1656#endif
1657
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001659inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1661 : basic_iostream<char_type, traits_type>(&__sb_)
1662{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001663 if (__sb_.open(__s, __mode) == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 this->setstate(ios_base::failbit);
1665}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001666#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667
Eric Fiselier78ccf772017-04-18 23:38:41 +00001668#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669
1670template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001672basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001673 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1674 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675{
1676 this->set_rdbuf(&__sb_);
1677}
1678
1679template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681basic_fstream<_CharT, _Traits>&
1682basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1683{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001684 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1685 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 return *this;
1687}
1688
Eric Fiselier78ccf772017-04-18 23:38:41 +00001689#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690
1691template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001692inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693void
1694basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1695{
1696 basic_iostream<char_type, traits_type>::swap(__rhs);
1697 __sb_.swap(__rhs.__sb_);
1698}
1699
1700template <class _CharT, class _Traits>
1701inline _LIBCPP_INLINE_VISIBILITY
1702void
1703swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1704{
1705 __x.swap(__y);
1706}
1707
1708template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001709inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710basic_filebuf<_CharT, _Traits>*
1711basic_fstream<_CharT, _Traits>::rdbuf() const
1712{
1713 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1714}
1715
1716template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001717inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718bool
1719basic_fstream<_CharT, _Traits>::is_open() const
1720{
1721 return __sb_.is_open();
1722}
1723
Ed Schouten7009f4e2015-03-12 15:44:39 +00001724#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725template <class _CharT, class _Traits>
1726void
1727basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1728{
1729 if (__sb_.open(__s, __mode))
1730 this->clear();
1731 else
1732 this->setstate(ios_base::failbit);
1733}
1734
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001735#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1736template <class _CharT, class _Traits>
1737void
1738basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1739{
1740 if (__sb_.open(__s, __mode))
1741 this->clear();
1742 else
1743 this->setstate(ios_base::failbit);
1744}
1745#endif
1746
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747template <class _CharT, class _Traits>
1748void
1749basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1750{
1751 if (__sb_.open(__s, __mode))
1752 this->clear();
1753 else
1754 this->setstate(ios_base::failbit);
1755}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001756#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757
1758template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001759inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760void
1761basic_fstream<_CharT, _Traits>::close()
1762{
Bruce Mitchener170d8972020-11-24 12:53:53 -05001763 if (__sb_.close() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 this->setstate(ios_base::failbit);
1765}
1766
Louis Dionne8a79be62020-10-21 11:11:45 -04001767#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1)
1768_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>)
1769_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>)
1770_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>)
1771#endif
1772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773_LIBCPP_END_NAMESPACE_STD
1774
Eric Fiselierf4433a32017-05-31 22:07:49 +00001775_LIBCPP_POP_MACROS
1776
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777#endif // _LIBCPP_FSTREAM