blob: 7db9017aca9e79d6b750cfca622ff6b0a0cb27a3 [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>
183#include <ostream>
184#include <istream>
185#include <__locale>
186#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000187#include <cstdlib>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000188#include <filesystem>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000190#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193
Eric Fiselierf4433a32017-05-31 22:07:49 +0000194_LIBCPP_PUSH_MACROS
195#include <__undef_macros>
196
197
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198_LIBCPP_BEGIN_NAMESPACE_STD
199
200template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000201class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 : public basic_streambuf<_CharT, _Traits>
203{
204public:
205 typedef _CharT char_type;
206 typedef _Traits traits_type;
207 typedef typename traits_type::int_type int_type;
208 typedef typename traits_type::pos_type pos_type;
209 typedef typename traits_type::off_type off_type;
210 typedef typename traits_type::state_type state_type;
211
212 // 27.9.1.2 Constructors/destructor:
213 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000214#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_filebuf(basic_filebuf&& __rhs);
216#endif
217 virtual ~basic_filebuf();
218
219 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000220#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 basic_filebuf& operator=(basic_filebuf&& __rhs);
223#endif
224 void swap(basic_filebuf& __rhs);
225
226 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000229#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000230 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000231#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
232 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
233#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000236
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000237#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000238 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000239 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
240 return open(__p.c_str(), __mode);
241 }
242#endif
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000243 _LIBCPP_INLINE_VISIBILITY
244 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 basic_filebuf* close();
247
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000248 _LIBCPP_INLINE_VISIBILITY
249 inline static const char*
250 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
251
252 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253 // 27.9.1.5 Overridden virtual functions:
254 virtual int_type underflow();
255 virtual int_type pbackfail(int_type __c = traits_type::eof());
256 virtual int_type overflow (int_type __c = traits_type::eof());
257 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
258 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
259 ios_base::openmode __wch = ios_base::in | ios_base::out);
260 virtual pos_type seekpos(pos_type __sp,
261 ios_base::openmode __wch = ios_base::in | ios_base::out);
262 virtual int sync();
263 virtual void imbue(const locale& __loc);
264
265private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000266 char* __extbuf_;
267 const char* __extbufnext_;
268 const char* __extbufend_;
269 char __extbuf_min_[8];
270 size_t __ebs_;
271 char_type* __intbuf_;
272 size_t __ibs_;
273 FILE* __file_;
274 const codecvt<char_type, char, state_type>* __cv_;
275 state_type __st_;
276 state_type __st_last_;
277 ios_base::openmode __om_;
278 ios_base::openmode __cm_;
279 bool __owns_eb_;
280 bool __owns_ib_;
281 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000283 bool __read_mode();
284 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285};
286
287template <class _CharT, class _Traits>
288basic_filebuf<_CharT, _Traits>::basic_filebuf()
289 : __extbuf_(0),
290 __extbufnext_(0),
291 __extbufend_(0),
292 __ebs_(0),
293 __intbuf_(0),
294 __ibs_(0),
295 __file_(0),
Howard Hinnant0090b122012-08-24 16:52:47 +0000296 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000298 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 __om_(0),
300 __cm_(0),
301 __owns_eb_(false),
302 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000303 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304{
Howard Hinnant0090b122012-08-24 16:52:47 +0000305 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
306 {
307 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
308 __always_noconv_ = __cv_->always_noconv();
309 }
Howard Hinnantfd49f462012-08-24 18:06:47 +0000310 setbuf(0, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311}
312
Eric Fiselier78ccf772017-04-18 23:38:41 +0000313#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
315template <class _CharT, class _Traits>
316basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
317 : basic_streambuf<_CharT, _Traits>(__rhs)
318{
319 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
320 {
321 __extbuf_ = __extbuf_min_;
322 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
323 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
324 }
325 else
326 {
327 __extbuf_ = __rhs.__extbuf_;
328 __extbufnext_ = __rhs.__extbufnext_;
329 __extbufend_ = __rhs.__extbufend_;
330 }
331 __ebs_ = __rhs.__ebs_;
332 __intbuf_ = __rhs.__intbuf_;
333 __ibs_ = __rhs.__ibs_;
334 __file_ = __rhs.__file_;
335 __cv_ = __rhs.__cv_;
336 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000337 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 __om_ = __rhs.__om_;
339 __cm_ = __rhs.__cm_;
340 __owns_eb_ = __rhs.__owns_eb_;
341 __owns_ib_ = __rhs.__owns_ib_;
342 __always_noconv_ = __rhs.__always_noconv_;
343 if (__rhs.pbase())
344 {
345 if (__rhs.pbase() == __rhs.__intbuf_)
346 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
347 else
348 this->setp((char_type*)__extbuf_,
349 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000350 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351 }
352 else if (__rhs.eback())
353 {
354 if (__rhs.eback() == __rhs.__intbuf_)
355 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
356 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
357 else
358 this->setg((char_type*)__extbuf_,
359 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
360 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
361 }
362 __rhs.__extbuf_ = 0;
363 __rhs.__extbufnext_ = 0;
364 __rhs.__extbufend_ = 0;
365 __rhs.__ebs_ = 0;
366 __rhs.__intbuf_ = 0;
367 __rhs.__ibs_ = 0;
368 __rhs.__file_ = 0;
369 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000370 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371 __rhs.__om_ = 0;
372 __rhs.__cm_ = 0;
373 __rhs.__owns_eb_ = false;
374 __rhs.__owns_ib_ = false;
375 __rhs.setg(0, 0, 0);
376 __rhs.setp(0, 0);
377}
378
379template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381basic_filebuf<_CharT, _Traits>&
382basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
383{
384 close();
385 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000386 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387}
388
Eric Fiselier78ccf772017-04-18 23:38:41 +0000389#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
391template <class _CharT, class _Traits>
392basic_filebuf<_CharT, _Traits>::~basic_filebuf()
393{
394#ifndef _LIBCPP_NO_EXCEPTIONS
395 try
396 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000397#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 close();
399#ifndef _LIBCPP_NO_EXCEPTIONS
400 }
401 catch (...)
402 {
403 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 if (__owns_eb_)
406 delete [] __extbuf_;
407 if (__owns_ib_)
408 delete [] __intbuf_;
409}
410
411template <class _CharT, class _Traits>
412void
413basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
414{
415 basic_streambuf<char_type, traits_type>::swap(__rhs);
416 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
417 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000418 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
419 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
420 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 }
422 else
423 {
424 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
425 ptrdiff_t __le = __extbufend_ - __extbuf_;
426 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
427 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
428 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
429 {
430 __extbuf_ = __rhs.__extbuf_;
431 __rhs.__extbuf_ = __rhs.__extbuf_min_;
432 }
433 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
434 {
435 __rhs.__extbuf_ = __extbuf_;
436 __extbuf_ = __extbuf_min_;
437 }
438 __extbufnext_ = __extbuf_ + __rn;
439 __extbufend_ = __extbuf_ + __re;
440 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
441 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
442 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000443 _VSTD::swap(__ebs_, __rhs.__ebs_);
444 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
445 _VSTD::swap(__ibs_, __rhs.__ibs_);
446 _VSTD::swap(__file_, __rhs.__file_);
447 _VSTD::swap(__cv_, __rhs.__cv_);
448 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000449 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000450 _VSTD::swap(__om_, __rhs.__om_);
451 _VSTD::swap(__cm_, __rhs.__cm_);
452 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
453 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
454 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
456 {
457 ptrdiff_t __n = this->gptr() - this->eback();
458 ptrdiff_t __e = this->egptr() - this->eback();
459 this->setg((char_type*)__extbuf_min_,
460 (char_type*)__extbuf_min_ + __n,
461 (char_type*)__extbuf_min_ + __e);
462 }
463 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
464 {
465 ptrdiff_t __n = this->pptr() - this->pbase();
466 ptrdiff_t __e = this->epptr() - this->pbase();
467 this->setp((char_type*)__extbuf_min_,
468 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000469 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470 }
471 if (__rhs.eback() == (char_type*)__extbuf_min_)
472 {
473 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
474 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
475 __rhs.setg((char_type*)__rhs.__extbuf_min_,
476 (char_type*)__rhs.__extbuf_min_ + __n,
477 (char_type*)__rhs.__extbuf_min_ + __e);
478 }
479 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
480 {
481 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
482 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
483 __rhs.setp((char_type*)__rhs.__extbuf_min_,
484 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000485 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000486 }
487}
488
489template <class _CharT, class _Traits>
490inline _LIBCPP_INLINE_VISIBILITY
491void
492swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
493{
494 __x.swap(__y);
495}
496
497template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000498inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499bool
500basic_filebuf<_CharT, _Traits>::is_open() const
501{
502 return __file_ != 0;
503}
504
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000505template <class _CharT, class _Traits>
506const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
507 ios_base::openmode __mode) _NOEXCEPT {
508 switch (__mode & ~ios_base::ate) {
509 case ios_base::out:
510 case ios_base::out | ios_base::trunc:
511 return "w";
512 case ios_base::out | ios_base::app:
513 case ios_base::app:
514 return "a";
515 case ios_base::in:
516 return "r";
517 case ios_base::in | ios_base::out:
518 return "r+";
519 case ios_base::in | ios_base::out | ios_base::trunc:
520 return "w+";
521 case ios_base::in | ios_base::out | ios_base::app:
522 case ios_base::in | ios_base::app:
523 return "a+";
524 case ios_base::out | ios_base::binary:
525 case ios_base::out | ios_base::trunc | ios_base::binary:
526 return "wb";
527 case ios_base::out | ios_base::app | ios_base::binary:
528 case ios_base::app | ios_base::binary:
529 return "ab";
530 case ios_base::in | ios_base::binary:
531 return "rb";
532 case ios_base::in | ios_base::out | ios_base::binary:
533 return "r+b";
534 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
535 return "w+b";
536 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
537 case ios_base::in | ios_base::app | ios_base::binary:
538 return "a+b";
539 default:
540 return nullptr;
541 }
542 _LIBCPP_UNREACHABLE();
543}
544
Ed Schouten7009f4e2015-03-12 15:44:39 +0000545#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546template <class _CharT, class _Traits>
547basic_filebuf<_CharT, _Traits>*
548basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
549{
550 basic_filebuf<_CharT, _Traits>* __rt = 0;
551 if (__file_ == 0)
552 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000553 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000555 __file_ = fopen(__s, __mdstr);
556 if (__file_) {
557 __om_ = __mode;
558 if (__mode & ios_base::ate) {
559 if (fseek(__file_, 0, SEEK_END)) {
560 fclose(__file_);
561 __file_ = 0;
562 __rt = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000564 }
565 } else
566 __rt = 0;
567 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 }
569 return __rt;
570}
571
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000572template <class _CharT, class _Traits>
573_LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>*
574basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
575 basic_filebuf<_CharT, _Traits>* __rt = 0;
576 if (__file_ == 0) {
577 if (const char* __mdstr = __make_mdstring(__mode)) {
578 __rt = this;
579 __file_ = fdopen(__fd, __mdstr);
580 if (__file_) {
581 __om_ = __mode;
582 if (__mode & ios_base::ate) {
583 if (fseek(__file_, 0, SEEK_END)) {
584 fclose(__file_);
585 __file_ = 0;
586 __rt = 0;
587 }
588 }
589 } else
590 __rt = 0;
591 }
592 }
593 return __rt;
594}
595
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000596#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
597// This is basically the same as the char* overload except that it uses _wfopen
598// and long mode strings.
599template <class _CharT, class _Traits>
600basic_filebuf<_CharT, _Traits>*
601basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
602{
603 basic_filebuf<_CharT, _Traits>* __rt = 0;
604 if (__file_ == 0)
605 {
606 __rt = this;
607 const wchar_t* __mdstr;
608 switch (__mode & ~ios_base::ate)
609 {
610 case ios_base::out:
611 case ios_base::out | ios_base::trunc:
612 __mdstr = L"w";
613 break;
614 case ios_base::out | ios_base::app:
615 case ios_base::app:
616 __mdstr = L"a";
617 break;
618 case ios_base::in:
619 __mdstr = L"r";
620 break;
621 case ios_base::in | ios_base::out:
622 __mdstr = L"r+";
623 break;
624 case ios_base::in | ios_base::out | ios_base::trunc:
625 __mdstr = L"w+";
626 break;
627 case ios_base::in | ios_base::out | ios_base::app:
628 case ios_base::in | ios_base::app:
629 __mdstr = L"a+";
630 break;
631 case ios_base::out | ios_base::binary:
632 case ios_base::out | ios_base::trunc | ios_base::binary:
633 __mdstr = L"wb";
634 break;
635 case ios_base::out | ios_base::app | ios_base::binary:
636 case ios_base::app | ios_base::binary:
637 __mdstr = L"ab";
638 break;
639 case ios_base::in | ios_base::binary:
640 __mdstr = L"rb";
641 break;
642 case ios_base::in | ios_base::out | ios_base::binary:
643 __mdstr = L"r+b";
644 break;
645 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
646 __mdstr = L"w+b";
647 break;
648 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
649 case ios_base::in | ios_base::app | ios_base::binary:
650 __mdstr = L"a+b";
651 break;
652 default:
653 __rt = 0;
654 break;
655 }
656 if (__rt)
657 {
658 __file_ = _wfopen(__s, __mdstr);
659 if (__file_)
660 {
661 __om_ = __mode;
662 if (__mode & ios_base::ate)
663 {
664 if (fseek(__file_, 0, SEEK_END))
665 {
666 fclose(__file_);
667 __file_ = 0;
668 __rt = 0;
669 }
670 }
671 }
672 else
673 __rt = 0;
674 }
675 }
676 return __rt;
677}
678#endif
679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000681inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682basic_filebuf<_CharT, _Traits>*
683basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
684{
685 return open(__s.c_str(), __mode);
686}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
689template <class _CharT, class _Traits>
690basic_filebuf<_CharT, _Traits>*
691basic_filebuf<_CharT, _Traits>::close()
692{
693 basic_filebuf<_CharT, _Traits>* __rt = 0;
694 if (__file_)
695 {
696 __rt = this;
697 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000698 if (sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 __rt = 0;
Petr Hosekca6421b2019-07-22 19:54:34 +0000700 if (fclose(__h.release()))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 __rt = 0;
Petr Hosekca6421b2019-07-22 19:54:34 +0000702 __file_ = 0;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000703 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 }
705 return __rt;
706}
707
708template <class _CharT, class _Traits>
709typename basic_filebuf<_CharT, _Traits>::int_type
710basic_filebuf<_CharT, _Traits>::underflow()
711{
712 if (__file_ == 0)
713 return traits_type::eof();
714 bool __initial = __read_mode();
715 char_type __1buf;
716 if (this->gptr() == 0)
717 this->setg(&__1buf, &__1buf+1, &__1buf+1);
718 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
719 int_type __c = traits_type::eof();
720 if (this->gptr() == this->egptr())
721 {
722 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
723 if (__always_noconv_)
724 {
725 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
726 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
727 if (__nmemb != 0)
728 {
729 this->setg(this->eback(),
730 this->eback() + __unget_sz,
731 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000732 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 }
734 }
735 else
736 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000737 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
738 if (__extbufend_ != __extbufnext_)
739 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
741 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000742 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 static_cast<size_t>(__extbufend_ - __extbufnext_));
744 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000745 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000746 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 if (__nr != 0)
748 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000749 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000750 __throw_bad_cast();
751
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 __extbufend_ = __extbufnext_ + __nr;
753 char_type* __inext;
754 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
755 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000756 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 if (__r == codecvt_base::noconv)
758 {
Louis Dionnef5ba8aa2019-03-20 14:34:00 +0000759 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
Marshall Clowbeda7142017-06-14 20:00:36 +0000760 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000761 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 }
763 else if (__inext != this->eback() + __unget_sz)
764 {
765 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000766 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 }
768 }
769 }
770 }
771 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000772 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 if (this->eback() == &__1buf)
774 this->setg(0, 0, 0);
775 return __c;
776}
777
778template <class _CharT, class _Traits>
779typename basic_filebuf<_CharT, _Traits>::int_type
780basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
781{
782 if (__file_ && this->eback() < this->gptr())
783 {
784 if (traits_type::eq_int_type(__c, traits_type::eof()))
785 {
786 this->gbump(-1);
787 return traits_type::not_eof(__c);
788 }
789 if ((__om_ & ios_base::out) ||
790 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
791 {
792 this->gbump(-1);
793 *this->gptr() = traits_type::to_char_type(__c);
794 return __c;
795 }
796 }
797 return traits_type::eof();
798}
799
800template <class _CharT, class _Traits>
801typename basic_filebuf<_CharT, _Traits>::int_type
802basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
803{
804 if (__file_ == 0)
805 return traits_type::eof();
806 __write_mode();
807 char_type __1buf;
808 char_type* __pb_save = this->pbase();
809 char_type* __epb_save = this->epptr();
810 if (!traits_type::eq_int_type(__c, traits_type::eof()))
811 {
812 if (this->pptr() == 0)
813 this->setp(&__1buf, &__1buf+1);
814 *this->pptr() = traits_type::to_char_type(__c);
815 this->pbump(1);
816 }
817 if (this->pptr() != this->pbase())
818 {
819 if (__always_noconv_)
820 {
821 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
822 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
823 return traits_type::eof();
824 }
825 else
826 {
827 char* __extbe = __extbuf_;
828 codecvt_base::result __r;
829 do
830 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000831 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000832 __throw_bad_cast();
833
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 const char_type* __e;
835 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
836 __extbuf_, __extbuf_ + __ebs_, __extbe);
837 if (__e == this->pbase())
838 return traits_type::eof();
839 if (__r == codecvt_base::noconv)
840 {
841 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
842 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
843 return traits_type::eof();
844 }
845 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
846 {
847 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
848 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
849 return traits_type::eof();
850 if (__r == codecvt_base::partial)
851 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000852 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000853 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 }
855 }
856 else
857 return traits_type::eof();
858 } while (__r == codecvt_base::partial);
859 }
860 this->setp(__pb_save, __epb_save);
861 }
862 return traits_type::not_eof(__c);
863}
864
865template <class _CharT, class _Traits>
866basic_streambuf<_CharT, _Traits>*
867basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
868{
869 this->setg(0, 0, 0);
870 this->setp(0, 0);
871 if (__owns_eb_)
872 delete [] __extbuf_;
873 if (__owns_ib_)
874 delete [] __intbuf_;
875 __ebs_ = __n;
876 if (__ebs_ > sizeof(__extbuf_min_))
877 {
878 if (__always_noconv_ && __s)
879 {
880 __extbuf_ = (char*)__s;
881 __owns_eb_ = false;
882 }
883 else
884 {
885 __extbuf_ = new char[__ebs_];
886 __owns_eb_ = true;
887 }
888 }
889 else
890 {
891 __extbuf_ = __extbuf_min_;
892 __ebs_ = sizeof(__extbuf_min_);
893 __owns_eb_ = false;
894 }
895 if (!__always_noconv_)
896 {
897 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
898 if (__s && __ibs_ >= sizeof(__extbuf_min_))
899 {
900 __intbuf_ = __s;
901 __owns_ib_ = false;
902 }
903 else
904 {
905 __intbuf_ = new char_type[__ibs_];
906 __owns_ib_ = true;
907 }
908 }
909 else
910 {
911 __ibs_ = 0;
912 __intbuf_ = 0;
913 __owns_ib_ = false;
914 }
915 return this;
916}
917
918template <class _CharT, class _Traits>
919typename basic_filebuf<_CharT, _Traits>::pos_type
920basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
921 ios_base::openmode)
922{
Howard Hinnant0090b122012-08-24 16:52:47 +0000923 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000924 __throw_bad_cast();
925
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 int __width = __cv_->encoding();
927 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
928 return pos_type(off_type(-1));
929 // __width > 0 || __off == 0
930 int __whence;
931 switch (__way)
932 {
933 case ios_base::beg:
934 __whence = SEEK_SET;
935 break;
936 case ios_base::cur:
937 __whence = SEEK_CUR;
938 break;
939 case ios_base::end:
940 __whence = SEEK_END;
941 break;
942 default:
943 return pos_type(off_type(-1));
944 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000945#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000946 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
947 return pos_type(off_type(-1));
948 pos_type __r = ftell(__file_);
949#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
951 return pos_type(off_type(-1));
952 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000953#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954 __r.state(__st_);
955 return __r;
956}
957
958template <class _CharT, class _Traits>
959typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000960basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961{
962 if (__file_ == 0 || sync())
963 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000964#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000965 if (fseek(__file_, __sp, SEEK_SET))
966 return pos_type(off_type(-1));
967#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 if (fseeko(__file_, __sp, SEEK_SET))
969 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000970#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000971 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 return __sp;
973}
974
975template <class _CharT, class _Traits>
976int
977basic_filebuf<_CharT, _Traits>::sync()
978{
979 if (__file_ == 0)
980 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000981 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000982 __throw_bad_cast();
983
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 if (__cm_ & ios_base::out)
985 {
986 if (this->pptr() != this->pbase())
987 if (overflow() == traits_type::eof())
988 return -1;
989 codecvt_base::result __r;
990 do
991 {
992 char* __extbe;
993 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
994 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
995 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
996 return -1;
997 } while (__r == codecvt_base::partial);
998 if (__r == codecvt_base::error)
999 return -1;
1000 if (fflush(__file_))
1001 return -1;
1002 }
1003 else if (__cm_ & ios_base::in)
1004 {
1005 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001006 state_type __state = __st_last_;
1007 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 if (__always_noconv_)
1009 __c = this->egptr() - this->gptr();
1010 else
1011 {
1012 int __width = __cv_->encoding();
1013 __c = __extbufend_ - __extbufnext_;
1014 if (__width > 0)
1015 __c += __width * (this->egptr() - this->gptr());
1016 else
1017 {
1018 if (this->gptr() != this->egptr())
1019 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001020 const int __off = __cv_->length(__state, __extbuf_,
1021 __extbufnext_,
1022 this->gptr() - this->eback());
1023 __c += __extbufnext_ - __extbuf_ - __off;
1024 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 }
1026 }
1027 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001028#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001029 if (fseek(__file_, -__c, SEEK_CUR))
1030 return -1;
1031#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 if (fseeko(__file_, -__c, SEEK_CUR))
1033 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001034#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001035 if (__update_st)
1036 __st_ = __state;
1037 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 this->setg(0, 0, 0);
1039 __cm_ = 0;
1040 }
1041 return 0;
1042}
1043
1044template <class _CharT, class _Traits>
1045void
1046basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1047{
1048 sync();
1049 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1050 bool __old_anc = __always_noconv_;
1051 __always_noconv_ = __cv_->always_noconv();
1052 if (__old_anc != __always_noconv_)
1053 {
1054 this->setg(0, 0, 0);
1055 this->setp(0, 0);
1056 // invariant, char_type is char, else we couldn't get here
1057 if (__always_noconv_) // need to dump __intbuf_
1058 {
1059 if (__owns_eb_)
1060 delete [] __extbuf_;
1061 __owns_eb_ = __owns_ib_;
1062 __ebs_ = __ibs_;
1063 __extbuf_ = (char*)__intbuf_;
1064 __ibs_ = 0;
1065 __intbuf_ = 0;
1066 __owns_ib_ = false;
1067 }
1068 else // need to obtain an __intbuf_.
1069 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1070 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1071 {
1072 __ibs_ = __ebs_;
1073 __intbuf_ = (char_type*)__extbuf_;
1074 __owns_ib_ = false;
1075 __extbuf_ = new char[__ebs_];
1076 __owns_eb_ = true;
1077 }
1078 else
1079 {
1080 __ibs_ = __ebs_;
1081 __intbuf_ = new char_type[__ibs_];
1082 __owns_ib_ = true;
1083 }
1084 }
1085 }
1086}
1087
1088template <class _CharT, class _Traits>
1089bool
1090basic_filebuf<_CharT, _Traits>::__read_mode()
1091{
1092 if (!(__cm_ & ios_base::in))
1093 {
1094 this->setp(0, 0);
1095 if (__always_noconv_)
1096 this->setg((char_type*)__extbuf_,
1097 (char_type*)__extbuf_ + __ebs_,
1098 (char_type*)__extbuf_ + __ebs_);
1099 else
1100 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1101 __cm_ = ios_base::in;
1102 return true;
1103 }
1104 return false;
1105}
1106
1107template <class _CharT, class _Traits>
1108void
1109basic_filebuf<_CharT, _Traits>::__write_mode()
1110{
1111 if (!(__cm_ & ios_base::out))
1112 {
1113 this->setg(0, 0, 0);
1114 if (__ebs_ > sizeof(__extbuf_min_))
1115 {
1116 if (__always_noconv_)
1117 this->setp((char_type*)__extbuf_,
1118 (char_type*)__extbuf_ + (__ebs_ - 1));
1119 else
1120 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1121 }
1122 else
1123 this->setp(0, 0);
1124 __cm_ = ios_base::out;
1125 }
1126}
1127
1128// basic_ifstream
1129
1130template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001131class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 : public basic_istream<_CharT, _Traits>
1133{
1134public:
1135 typedef _CharT char_type;
1136 typedef _Traits traits_type;
1137 typedef typename traits_type::int_type int_type;
1138 typedef typename traits_type::pos_type pos_type;
1139 typedef typename traits_type::off_type off_type;
1140
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001143#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001146#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1147 _LIBCPP_INLINE_VISIBILITY
1148 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1149#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001152#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001153 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001154 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1155 : basic_ifstream(__p.c_str(), __mode) {}
1156#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001157#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001158#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 basic_ifstream& operator=(basic_ifstream&& __rhs);
1164#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 void swap(basic_ifstream& __rhs);
1167
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001172#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001174#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1175 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1176#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001178#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001179 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001180 void open(const filesystem::path& __p,
1181 ios_base::openmode __mode = ios_base::in) {
1182 return open(__p.c_str(), __mode);
1183 }
1184#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001185
1186 _LIBCPP_INLINE_VISIBILITY
1187 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001188#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 void close();
1191
1192private:
1193 basic_filebuf<char_type, traits_type> __sb_;
1194};
1195
1196template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001197inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198basic_ifstream<_CharT, _Traits>::basic_ifstream()
1199 : basic_istream<char_type, traits_type>(&__sb_)
1200{
1201}
1202
Ed Schouten7009f4e2015-03-12 15:44:39 +00001203#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204template <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(const char* __s, ios_base::openmode __mode)
1207 : basic_istream<char_type, traits_type>(&__sb_)
1208{
1209 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1210 this->setstate(ios_base::failbit);
1211}
1212
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001213#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1214template <class _CharT, class _Traits>
1215inline
1216basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1217 : basic_istream<char_type, traits_type>(&__sb_)
1218{
1219 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1220 this->setstate(ios_base::failbit);
1221}
1222#endif
1223
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001225inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1227 : basic_istream<char_type, traits_type>(&__sb_)
1228{
1229 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1230 this->setstate(ios_base::failbit);
1231}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
Eric Fiselier78ccf772017-04-18 23:38:41 +00001234#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
1236template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001237inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001239 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1240 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241{
1242 this->set_rdbuf(&__sb_);
1243}
1244
1245template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001246inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247basic_ifstream<_CharT, _Traits>&
1248basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1249{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001250 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1251 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 return *this;
1253}
1254
Eric Fiselier78ccf772017-04-18 23:38:41 +00001255#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
1257template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001258inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259void
1260basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1261{
1262 basic_istream<char_type, traits_type>::swap(__rhs);
1263 __sb_.swap(__rhs.__sb_);
1264}
1265
1266template <class _CharT, class _Traits>
1267inline _LIBCPP_INLINE_VISIBILITY
1268void
1269swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1270{
1271 __x.swap(__y);
1272}
1273
1274template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001275inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276basic_filebuf<_CharT, _Traits>*
1277basic_ifstream<_CharT, _Traits>::rdbuf() const
1278{
1279 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1280}
1281
1282template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001283inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284bool
1285basic_ifstream<_CharT, _Traits>::is_open() const
1286{
1287 return __sb_.is_open();
1288}
1289
Ed Schouten7009f4e2015-03-12 15:44:39 +00001290#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291template <class _CharT, class _Traits>
1292void
1293basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1294{
1295 if (__sb_.open(__s, __mode | ios_base::in))
1296 this->clear();
1297 else
1298 this->setstate(ios_base::failbit);
1299}
1300
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001301#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1302template <class _CharT, class _Traits>
1303void
1304basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1305{
1306 if (__sb_.open(__s, __mode | ios_base::in))
1307 this->clear();
1308 else
1309 this->setstate(ios_base::failbit);
1310}
1311#endif
1312
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313template <class _CharT, class _Traits>
1314void
1315basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1316{
1317 if (__sb_.open(__s, __mode | ios_base::in))
1318 this->clear();
1319 else
1320 this->setstate(ios_base::failbit);
1321}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001322
1323template <class _CharT, class _Traits>
1324void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1325 ios_base::openmode __mode) {
1326 if (__sb_.__open(__fd, __mode | ios_base::in))
1327 this->clear();
1328 else
1329 this->setstate(ios_base::failbit);
1330}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001331#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332
1333template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001334inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335void
1336basic_ifstream<_CharT, _Traits>::close()
1337{
1338 if (__sb_.close() == 0)
1339 this->setstate(ios_base::failbit);
1340}
1341
1342// basic_ofstream
1343
1344template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001345class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346 : public basic_ostream<_CharT, _Traits>
1347{
1348public:
1349 typedef _CharT char_type;
1350 typedef _Traits traits_type;
1351 typedef typename traits_type::int_type int_type;
1352 typedef typename traits_type::pos_type pos_type;
1353 typedef typename traits_type::off_type off_type;
1354
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001359#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1360 _LIBCPP_INLINE_VISIBILITY
1361 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1362#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001365
1366#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001367 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001368 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1369 : basic_ofstream(__p.c_str(), __mode) {}
1370#endif // _LIBCPP_STD_VER >= 17
1371
Eric Fiselier78ccf772017-04-18 23:38:41 +00001372#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 basic_ofstream& operator=(basic_ofstream&& __rhs);
1378#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 void swap(basic_ofstream& __rhs);
1381
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001386#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001388#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1389 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1390#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001392
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001393#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001394 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001395 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1396 { return open(__p.c_str(), __mode); }
1397#endif // _LIBCPP_STD_VER >= 17
1398
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001399 _LIBCPP_INLINE_VISIBILITY
1400 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001401#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 void close();
1404
1405private:
1406 basic_filebuf<char_type, traits_type> __sb_;
1407};
1408
1409template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001410inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411basic_ofstream<_CharT, _Traits>::basic_ofstream()
1412 : basic_ostream<char_type, traits_type>(&__sb_)
1413{
1414}
1415
Ed Schouten7009f4e2015-03-12 15:44:39 +00001416#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417template <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(const char* __s, ios_base::openmode __mode)
1420 : basic_ostream<char_type, traits_type>(&__sb_)
1421{
1422 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1423 this->setstate(ios_base::failbit);
1424}
1425
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001426#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1427template <class _CharT, class _Traits>
1428inline
1429basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1430 : basic_ostream<char_type, traits_type>(&__sb_)
1431{
1432 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1433 this->setstate(ios_base::failbit);
1434}
1435#endif
1436
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001438inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1440 : basic_ostream<char_type, traits_type>(&__sb_)
1441{
1442 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1443 this->setstate(ios_base::failbit);
1444}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001445#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
Eric Fiselier78ccf772017-04-18 23:38:41 +00001447#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448
1449template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001450inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001452 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1453 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454{
1455 this->set_rdbuf(&__sb_);
1456}
1457
1458template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460basic_ofstream<_CharT, _Traits>&
1461basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1462{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001463 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1464 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 return *this;
1466}
1467
Eric Fiselier78ccf772017-04-18 23:38:41 +00001468#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469
1470template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001471inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472void
1473basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1474{
1475 basic_ostream<char_type, traits_type>::swap(__rhs);
1476 __sb_.swap(__rhs.__sb_);
1477}
1478
1479template <class _CharT, class _Traits>
1480inline _LIBCPP_INLINE_VISIBILITY
1481void
1482swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1483{
1484 __x.swap(__y);
1485}
1486
1487template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001488inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489basic_filebuf<_CharT, _Traits>*
1490basic_ofstream<_CharT, _Traits>::rdbuf() const
1491{
1492 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1493}
1494
1495template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001496inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497bool
1498basic_ofstream<_CharT, _Traits>::is_open() const
1499{
1500 return __sb_.is_open();
1501}
1502
Ed Schouten7009f4e2015-03-12 15:44:39 +00001503#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504template <class _CharT, class _Traits>
1505void
1506basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1507{
1508 if (__sb_.open(__s, __mode | ios_base::out))
1509 this->clear();
1510 else
1511 this->setstate(ios_base::failbit);
1512}
1513
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001514#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1515template <class _CharT, class _Traits>
1516void
1517basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1518{
1519 if (__sb_.open(__s, __mode | ios_base::out))
1520 this->clear();
1521 else
1522 this->setstate(ios_base::failbit);
1523}
1524#endif
1525
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526template <class _CharT, class _Traits>
1527void
1528basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1529{
1530 if (__sb_.open(__s, __mode | ios_base::out))
1531 this->clear();
1532 else
1533 this->setstate(ios_base::failbit);
1534}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001535
1536template <class _CharT, class _Traits>
1537void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1538 ios_base::openmode __mode) {
1539 if (__sb_.__open(__fd, __mode | ios_base::out))
1540 this->clear();
1541 else
1542 this->setstate(ios_base::failbit);
1543}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001544#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545
1546template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001547inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548void
1549basic_ofstream<_CharT, _Traits>::close()
1550{
1551 if (__sb_.close() == 0)
1552 this->setstate(ios_base::failbit);
1553}
1554
1555// basic_fstream
1556
1557template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001558class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 : public basic_iostream<_CharT, _Traits>
1560{
1561public:
1562 typedef _CharT char_type;
1563 typedef _Traits traits_type;
1564 typedef typename traits_type::int_type int_type;
1565 typedef typename traits_type::pos_type pos_type;
1566 typedef typename traits_type::off_type off_type;
1567
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001570#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001573#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1574 _LIBCPP_INLINE_VISIBILITY
1575 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1576#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001579
1580#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001581 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001582 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1583 : basic_fstream(__p.c_str(), __mode) {}
1584#endif // _LIBCPP_STD_VER >= 17
1585
Ed Schouten7009f4e2015-03-12 15:44:39 +00001586#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001587#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 basic_fstream& operator=(basic_fstream&& __rhs);
1593#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 void swap(basic_fstream& __rhs);
1596
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001601#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001603#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1604 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1605#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001607
1608#if _LIBCPP_STD_VER >= 17
Louis Dionnef5ba8aa2019-03-20 14:34:00 +00001609 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001610 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1611 { return open(__p.c_str(), __mode); }
1612#endif // _LIBCPP_STD_VER >= 17
1613
Ed Schouten7009f4e2015-03-12 15:44:39 +00001614#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 void close();
1617
1618private:
1619 basic_filebuf<char_type, traits_type> __sb_;
1620};
1621
1622template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001623inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624basic_fstream<_CharT, _Traits>::basic_fstream()
1625 : basic_iostream<char_type, traits_type>(&__sb_)
1626{
1627}
1628
Ed Schouten7009f4e2015-03-12 15:44:39 +00001629#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630template <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(const char* __s, ios_base::openmode __mode)
1633 : basic_iostream<char_type, traits_type>(&__sb_)
1634{
1635 if (__sb_.open(__s, __mode) == 0)
1636 this->setstate(ios_base::failbit);
1637}
1638
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001639#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1640template <class _CharT, class _Traits>
1641inline
1642basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1643 : basic_iostream<char_type, traits_type>(&__sb_)
1644{
1645 if (__sb_.open(__s, __mode) == 0)
1646 this->setstate(ios_base::failbit);
1647}
1648#endif
1649
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001651inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1653 : basic_iostream<char_type, traits_type>(&__sb_)
1654{
1655 if (__sb_.open(__s, __mode) == 0)
1656 this->setstate(ios_base::failbit);
1657}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001658#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
Eric Fiselier78ccf772017-04-18 23:38:41 +00001660#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661
1662template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001665 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1666 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667{
1668 this->set_rdbuf(&__sb_);
1669}
1670
1671template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001672inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673basic_fstream<_CharT, _Traits>&
1674basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1675{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001676 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1677 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 return *this;
1679}
1680
Eric Fiselier78ccf772017-04-18 23:38:41 +00001681#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682
1683template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001684inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685void
1686basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1687{
1688 basic_iostream<char_type, traits_type>::swap(__rhs);
1689 __sb_.swap(__rhs.__sb_);
1690}
1691
1692template <class _CharT, class _Traits>
1693inline _LIBCPP_INLINE_VISIBILITY
1694void
1695swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1696{
1697 __x.swap(__y);
1698}
1699
1700template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001701inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702basic_filebuf<_CharT, _Traits>*
1703basic_fstream<_CharT, _Traits>::rdbuf() const
1704{
1705 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1706}
1707
1708template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001709inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710bool
1711basic_fstream<_CharT, _Traits>::is_open() const
1712{
1713 return __sb_.is_open();
1714}
1715
Ed Schouten7009f4e2015-03-12 15:44:39 +00001716#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717template <class _CharT, class _Traits>
1718void
1719basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1720{
1721 if (__sb_.open(__s, __mode))
1722 this->clear();
1723 else
1724 this->setstate(ios_base::failbit);
1725}
1726
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001727#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1728template <class _CharT, class _Traits>
1729void
1730basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1731{
1732 if (__sb_.open(__s, __mode))
1733 this->clear();
1734 else
1735 this->setstate(ios_base::failbit);
1736}
1737#endif
1738
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739template <class _CharT, class _Traits>
1740void
1741basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1742{
1743 if (__sb_.open(__s, __mode))
1744 this->clear();
1745 else
1746 this->setstate(ios_base::failbit);
1747}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001748#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749
1750template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001751inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752void
1753basic_fstream<_CharT, _Traits>::close()
1754{
1755 if (__sb_.close() == 0)
1756 this->setstate(ios_base::failbit);
1757}
1758
1759_LIBCPP_END_NAMESPACE_STD
1760
Eric Fiselierf4433a32017-05-31 22:07:49 +00001761_LIBCPP_POP_MACROS
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763#endif // _LIBCPP_FSTREAM