blob: 711e484e2dcff8785691ff68c009d09e4ce01c6c [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- fstream ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FSTREAM
12#define _LIBCPP_FSTREAM
13
14/*
15 fstream synopsis
16
17template <class charT, class traits = char_traits<charT> >
18class basic_filebuf
19 : public basic_streambuf<charT, traits>
20{
21public:
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
27
28 // 27.9.1.2 Constructors/destructor:
29 basic_filebuf();
30 basic_filebuf(basic_filebuf&& rhs);
31 virtual ~basic_filebuf();
32
33 // 27.9.1.3 Assign/swap:
34 basic_filebuf& operator=(basic_filebuf&& rhs);
35 void swap(basic_filebuf& rhs);
36
37 // 27.9.1.4 Members:
38 bool is_open() const;
39 basic_filebuf* open(const char* s, ios_base::openmode mode);
40 basic_filebuf* open(const string& s, ios_base::openmode mode);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000041 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 basic_filebuf* close();
43
44protected:
45 // 27.9.1.5 Overridden virtual functions:
46 virtual streamsize showmanyc();
47 virtual int_type underflow();
48 virtual int_type uflow();
49 virtual int_type pbackfail(int_type c = traits_type::eof());
50 virtual int_type overflow (int_type c = traits_type::eof());
51 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
52 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
53 ios_base::openmode which = ios_base::in | ios_base::out);
54 virtual pos_type seekpos(pos_type sp,
55 ios_base::openmode which = ios_base::in | ios_base::out);
56 virtual int sync();
57 virtual void imbue(const locale& loc);
58};
59
60template <class charT, class traits>
61 void
62 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
63
64typedef basic_filebuf<char> filebuf;
65typedef basic_filebuf<wchar_t> wfilebuf;
66
67template <class charT, class traits = char_traits<charT> >
68class basic_ifstream
69 : public basic_istream<charT,traits>
70{
71public:
72 typedef charT char_type;
73 typedef traits traits_type;
74 typedef typename traits_type::int_type int_type;
75 typedef typename traits_type::pos_type pos_type;
76 typedef typename traits_type::off_type off_type;
77
78 basic_ifstream();
79 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
80 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000081 explicit basic_ifstream(const filesystem::path& p,
82 ios_base::openmode mode = ios_base::in); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 basic_ifstream(basic_ifstream&& rhs);
84
85 basic_ifstream& operator=(basic_ifstream&& rhs);
86 void swap(basic_ifstream& rhs);
87
88 basic_filebuf<char_type, traits_type>* rdbuf() const;
89 bool is_open() const;
90 void open(const char* s, ios_base::openmode mode = ios_base::in);
91 void open(const string& s, ios_base::openmode mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +000092 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
93
Howard Hinnantc51e1022010-05-11 19:42:16 +000094 void close();
95};
96
97template <class charT, class traits>
98 void
99 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
100
101typedef basic_ifstream<char> ifstream;
102typedef basic_ifstream<wchar_t> wifstream;
103
104template <class charT, class traits = char_traits<charT> >
105class basic_ofstream
106 : public basic_ostream<charT,traits>
107{
108public:
109 typedef charT char_type;
110 typedef traits traits_type;
111 typedef typename traits_type::int_type int_type;
112 typedef typename traits_type::pos_type pos_type;
113 typedef typename traits_type::off_type off_type;
114
115 basic_ofstream();
116 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
117 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000118 explicit basic_ofstream(const filesystem::path& p,
119 ios_base::openmode mode = ios_base::out); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 basic_ofstream(basic_ofstream&& rhs);
121
122 basic_ofstream& operator=(basic_ofstream&& rhs);
123 void swap(basic_ofstream& rhs);
124
125 basic_filebuf<char_type, traits_type>* rdbuf() const;
126 bool is_open() const;
127 void open(const char* s, ios_base::openmode mode = ios_base::out);
128 void open(const string& s, ios_base::openmode mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000129 void open(const filesystem::path& p,
130 ios_base::openmode mode = ios_base::out); // C++17
131
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 void close();
133};
134
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000135template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136 void
137 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
138
139typedef basic_ofstream<char> ofstream;
140typedef basic_ofstream<wchar_t> wofstream;
141
142template <class charT, class traits=char_traits<charT> >
143class basic_fstream
144 : public basic_iostream<charT,traits>
145{
146public:
147 typedef charT char_type;
148 typedef traits traits_type;
149 typedef typename traits_type::int_type int_type;
150 typedef typename traits_type::pos_type pos_type;
151 typedef typename traits_type::off_type off_type;
152
153 basic_fstream();
154 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
155 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000156 explicit basic_fstream(const filesystem::path& p,
157 ios_base::openmode mode = ios_base::in|ios_base::out); C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 basic_fstream(basic_fstream&& rhs);
159
160 basic_fstream& operator=(basic_fstream&& rhs);
161 void swap(basic_fstream& rhs);
162
163 basic_filebuf<char_type, traits_type>* rdbuf() const;
164 bool is_open() const;
165 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
166 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000167 void open(const filesystem::path& s,
168 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17
169
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 void close();
171};
172
173template <class charT, class traits>
174 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
175
176typedef basic_fstream<char> fstream;
177typedef basic_fstream<wchar_t> wfstream;
178
179} // std
180
181*/
182
183#include <__config>
184#include <ostream>
185#include <istream>
186#include <__locale>
187#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000188#include <cstdlib>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000189#include <filesystem>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000191#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000193#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194
Eric Fiselierf4433a32017-05-31 22:07:49 +0000195_LIBCPP_PUSH_MACROS
196#include <__undef_macros>
197
198
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199_LIBCPP_BEGIN_NAMESPACE_STD
200
201template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000202class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 : public basic_streambuf<_CharT, _Traits>
204{
205public:
206 typedef _CharT char_type;
207 typedef _Traits traits_type;
208 typedef typename traits_type::int_type int_type;
209 typedef typename traits_type::pos_type pos_type;
210 typedef typename traits_type::off_type off_type;
211 typedef typename traits_type::state_type state_type;
212
213 // 27.9.1.2 Constructors/destructor:
214 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000215#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216 basic_filebuf(basic_filebuf&& __rhs);
217#endif
218 virtual ~basic_filebuf();
219
220 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000221#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_filebuf& operator=(basic_filebuf&& __rhs);
224#endif
225 void swap(basic_filebuf& __rhs);
226
227 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000230#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000232#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
233 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
234#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000237
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000238#if _LIBCPP_STD_VER >= 17
239 _LIBCPP_INLINE_VISIBILITY
240 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) {
241 return open(__p.c_str(), __mode);
242 }
243#endif
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000244 _LIBCPP_INLINE_VISIBILITY
245 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_filebuf* close();
248
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000249 _LIBCPP_INLINE_VISIBILITY
250 inline static const char*
251 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
252
253 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 // 27.9.1.5 Overridden virtual functions:
255 virtual int_type underflow();
256 virtual int_type pbackfail(int_type __c = traits_type::eof());
257 virtual int_type overflow (int_type __c = traits_type::eof());
258 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
259 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
260 ios_base::openmode __wch = ios_base::in | ios_base::out);
261 virtual pos_type seekpos(pos_type __sp,
262 ios_base::openmode __wch = ios_base::in | ios_base::out);
263 virtual int sync();
264 virtual void imbue(const locale& __loc);
265
266private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000267 char* __extbuf_;
268 const char* __extbufnext_;
269 const char* __extbufend_;
270 char __extbuf_min_[8];
271 size_t __ebs_;
272 char_type* __intbuf_;
273 size_t __ibs_;
274 FILE* __file_;
275 const codecvt<char_type, char, state_type>* __cv_;
276 state_type __st_;
277 state_type __st_last_;
278 ios_base::openmode __om_;
279 ios_base::openmode __cm_;
280 bool __owns_eb_;
281 bool __owns_ib_;
282 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000284 bool __read_mode();
285 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286};
287
288template <class _CharT, class _Traits>
289basic_filebuf<_CharT, _Traits>::basic_filebuf()
290 : __extbuf_(0),
291 __extbufnext_(0),
292 __extbufend_(0),
293 __ebs_(0),
294 __intbuf_(0),
295 __ibs_(0),
296 __file_(0),
Howard Hinnant0090b122012-08-24 16:52:47 +0000297 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000299 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __om_(0),
301 __cm_(0),
302 __owns_eb_(false),
303 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000304 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305{
Howard Hinnant0090b122012-08-24 16:52:47 +0000306 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
307 {
308 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
309 __always_noconv_ = __cv_->always_noconv();
310 }
Howard Hinnantfd49f462012-08-24 18:06:47 +0000311 setbuf(0, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312}
313
Eric Fiselier78ccf772017-04-18 23:38:41 +0000314#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315
316template <class _CharT, class _Traits>
317basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
318 : basic_streambuf<_CharT, _Traits>(__rhs)
319{
320 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
321 {
322 __extbuf_ = __extbuf_min_;
323 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
324 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
325 }
326 else
327 {
328 __extbuf_ = __rhs.__extbuf_;
329 __extbufnext_ = __rhs.__extbufnext_;
330 __extbufend_ = __rhs.__extbufend_;
331 }
332 __ebs_ = __rhs.__ebs_;
333 __intbuf_ = __rhs.__intbuf_;
334 __ibs_ = __rhs.__ibs_;
335 __file_ = __rhs.__file_;
336 __cv_ = __rhs.__cv_;
337 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000338 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 __om_ = __rhs.__om_;
340 __cm_ = __rhs.__cm_;
341 __owns_eb_ = __rhs.__owns_eb_;
342 __owns_ib_ = __rhs.__owns_ib_;
343 __always_noconv_ = __rhs.__always_noconv_;
344 if (__rhs.pbase())
345 {
346 if (__rhs.pbase() == __rhs.__intbuf_)
347 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
348 else
349 this->setp((char_type*)__extbuf_,
350 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000351 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 }
353 else if (__rhs.eback())
354 {
355 if (__rhs.eback() == __rhs.__intbuf_)
356 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
357 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
358 else
359 this->setg((char_type*)__extbuf_,
360 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
361 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
362 }
363 __rhs.__extbuf_ = 0;
364 __rhs.__extbufnext_ = 0;
365 __rhs.__extbufend_ = 0;
366 __rhs.__ebs_ = 0;
367 __rhs.__intbuf_ = 0;
368 __rhs.__ibs_ = 0;
369 __rhs.__file_ = 0;
370 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000371 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 __rhs.__om_ = 0;
373 __rhs.__cm_ = 0;
374 __rhs.__owns_eb_ = false;
375 __rhs.__owns_ib_ = false;
376 __rhs.setg(0, 0, 0);
377 __rhs.setp(0, 0);
378}
379
380template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382basic_filebuf<_CharT, _Traits>&
383basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
384{
385 close();
386 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000387 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388}
389
Eric Fiselier78ccf772017-04-18 23:38:41 +0000390#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template <class _CharT, class _Traits>
393basic_filebuf<_CharT, _Traits>::~basic_filebuf()
394{
395#ifndef _LIBCPP_NO_EXCEPTIONS
396 try
397 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000398#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 close();
400#ifndef _LIBCPP_NO_EXCEPTIONS
401 }
402 catch (...)
403 {
404 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000405#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 if (__owns_eb_)
407 delete [] __extbuf_;
408 if (__owns_ib_)
409 delete [] __intbuf_;
410}
411
412template <class _CharT, class _Traits>
413void
414basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
415{
416 basic_streambuf<char_type, traits_type>::swap(__rhs);
417 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
418 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000419 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
420 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
421 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 }
423 else
424 {
425 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
426 ptrdiff_t __le = __extbufend_ - __extbuf_;
427 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
428 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
429 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
430 {
431 __extbuf_ = __rhs.__extbuf_;
432 __rhs.__extbuf_ = __rhs.__extbuf_min_;
433 }
434 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
435 {
436 __rhs.__extbuf_ = __extbuf_;
437 __extbuf_ = __extbuf_min_;
438 }
439 __extbufnext_ = __extbuf_ + __rn;
440 __extbufend_ = __extbuf_ + __re;
441 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
442 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
443 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000444 _VSTD::swap(__ebs_, __rhs.__ebs_);
445 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
446 _VSTD::swap(__ibs_, __rhs.__ibs_);
447 _VSTD::swap(__file_, __rhs.__file_);
448 _VSTD::swap(__cv_, __rhs.__cv_);
449 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000450 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000451 _VSTD::swap(__om_, __rhs.__om_);
452 _VSTD::swap(__cm_, __rhs.__cm_);
453 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
454 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
455 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
457 {
458 ptrdiff_t __n = this->gptr() - this->eback();
459 ptrdiff_t __e = this->egptr() - this->eback();
460 this->setg((char_type*)__extbuf_min_,
461 (char_type*)__extbuf_min_ + __n,
462 (char_type*)__extbuf_min_ + __e);
463 }
464 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
465 {
466 ptrdiff_t __n = this->pptr() - this->pbase();
467 ptrdiff_t __e = this->epptr() - this->pbase();
468 this->setp((char_type*)__extbuf_min_,
469 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000470 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 }
472 if (__rhs.eback() == (char_type*)__extbuf_min_)
473 {
474 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
475 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
476 __rhs.setg((char_type*)__rhs.__extbuf_min_,
477 (char_type*)__rhs.__extbuf_min_ + __n,
478 (char_type*)__rhs.__extbuf_min_ + __e);
479 }
480 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
481 {
482 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
483 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
484 __rhs.setp((char_type*)__rhs.__extbuf_min_,
485 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000486 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 }
488}
489
490template <class _CharT, class _Traits>
491inline _LIBCPP_INLINE_VISIBILITY
492void
493swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
494{
495 __x.swap(__y);
496}
497
498template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000499inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500bool
501basic_filebuf<_CharT, _Traits>::is_open() const
502{
503 return __file_ != 0;
504}
505
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000506template <class _CharT, class _Traits>
507const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
508 ios_base::openmode __mode) _NOEXCEPT {
509 switch (__mode & ~ios_base::ate) {
510 case ios_base::out:
511 case ios_base::out | ios_base::trunc:
512 return "w";
513 case ios_base::out | ios_base::app:
514 case ios_base::app:
515 return "a";
516 case ios_base::in:
517 return "r";
518 case ios_base::in | ios_base::out:
519 return "r+";
520 case ios_base::in | ios_base::out | ios_base::trunc:
521 return "w+";
522 case ios_base::in | ios_base::out | ios_base::app:
523 case ios_base::in | ios_base::app:
524 return "a+";
525 case ios_base::out | ios_base::binary:
526 case ios_base::out | ios_base::trunc | ios_base::binary:
527 return "wb";
528 case ios_base::out | ios_base::app | ios_base::binary:
529 case ios_base::app | ios_base::binary:
530 return "ab";
531 case ios_base::in | ios_base::binary:
532 return "rb";
533 case ios_base::in | ios_base::out | ios_base::binary:
534 return "r+b";
535 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
536 return "w+b";
537 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
538 case ios_base::in | ios_base::app | ios_base::binary:
539 return "a+b";
540 default:
541 return nullptr;
542 }
543 _LIBCPP_UNREACHABLE();
544}
545
Ed Schouten7009f4e2015-03-12 15:44:39 +0000546#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547template <class _CharT, class _Traits>
548basic_filebuf<_CharT, _Traits>*
549basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
550{
551 basic_filebuf<_CharT, _Traits>* __rt = 0;
552 if (__file_ == 0)
553 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000554 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000556 __file_ = fopen(__s, __mdstr);
557 if (__file_) {
558 __om_ = __mode;
559 if (__mode & ios_base::ate) {
560 if (fseek(__file_, 0, SEEK_END)) {
561 fclose(__file_);
562 __file_ = 0;
563 __rt = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000565 }
566 } else
567 __rt = 0;
568 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 }
570 return __rt;
571}
572
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000573template <class _CharT, class _Traits>
574_LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>*
575basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
576 basic_filebuf<_CharT, _Traits>* __rt = 0;
577 if (__file_ == 0) {
578 if (const char* __mdstr = __make_mdstring(__mode)) {
579 __rt = this;
580 __file_ = fdopen(__fd, __mdstr);
581 if (__file_) {
582 __om_ = __mode;
583 if (__mode & ios_base::ate) {
584 if (fseek(__file_, 0, SEEK_END)) {
585 fclose(__file_);
586 __file_ = 0;
587 __rt = 0;
588 }
589 }
590 } else
591 __rt = 0;
592 }
593 }
594 return __rt;
595}
596
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000597#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
598// This is basically the same as the char* overload except that it uses _wfopen
599// and long mode strings.
600template <class _CharT, class _Traits>
601basic_filebuf<_CharT, _Traits>*
602basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
603{
604 basic_filebuf<_CharT, _Traits>* __rt = 0;
605 if (__file_ == 0)
606 {
607 __rt = this;
608 const wchar_t* __mdstr;
609 switch (__mode & ~ios_base::ate)
610 {
611 case ios_base::out:
612 case ios_base::out | ios_base::trunc:
613 __mdstr = L"w";
614 break;
615 case ios_base::out | ios_base::app:
616 case ios_base::app:
617 __mdstr = L"a";
618 break;
619 case ios_base::in:
620 __mdstr = L"r";
621 break;
622 case ios_base::in | ios_base::out:
623 __mdstr = L"r+";
624 break;
625 case ios_base::in | ios_base::out | ios_base::trunc:
626 __mdstr = L"w+";
627 break;
628 case ios_base::in | ios_base::out | ios_base::app:
629 case ios_base::in | ios_base::app:
630 __mdstr = L"a+";
631 break;
632 case ios_base::out | ios_base::binary:
633 case ios_base::out | ios_base::trunc | ios_base::binary:
634 __mdstr = L"wb";
635 break;
636 case ios_base::out | ios_base::app | ios_base::binary:
637 case ios_base::app | ios_base::binary:
638 __mdstr = L"ab";
639 break;
640 case ios_base::in | ios_base::binary:
641 __mdstr = L"rb";
642 break;
643 case ios_base::in | ios_base::out | ios_base::binary:
644 __mdstr = L"r+b";
645 break;
646 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
647 __mdstr = L"w+b";
648 break;
649 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
650 case ios_base::in | ios_base::app | ios_base::binary:
651 __mdstr = L"a+b";
652 break;
653 default:
654 __rt = 0;
655 break;
656 }
657 if (__rt)
658 {
659 __file_ = _wfopen(__s, __mdstr);
660 if (__file_)
661 {
662 __om_ = __mode;
663 if (__mode & ios_base::ate)
664 {
665 if (fseek(__file_, 0, SEEK_END))
666 {
667 fclose(__file_);
668 __file_ = 0;
669 __rt = 0;
670 }
671 }
672 }
673 else
674 __rt = 0;
675 }
676 }
677 return __rt;
678}
679#endif
680
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683basic_filebuf<_CharT, _Traits>*
684basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
685{
686 return open(__s.c_str(), __mode);
687}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
690template <class _CharT, class _Traits>
691basic_filebuf<_CharT, _Traits>*
692basic_filebuf<_CharT, _Traits>::close()
693{
694 basic_filebuf<_CharT, _Traits>* __rt = 0;
695 if (__file_)
696 {
697 __rt = this;
698 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000699 if (sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 __rt = 0;
701 if (fclose(__h.release()) == 0)
702 __file_ = 0;
703 else
704 __rt = 0;
Marshall Clow3c7658a2019-01-08 02:48:45 +0000705 setbuf(0, 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 }
707 return __rt;
708}
709
710template <class _CharT, class _Traits>
711typename basic_filebuf<_CharT, _Traits>::int_type
712basic_filebuf<_CharT, _Traits>::underflow()
713{
714 if (__file_ == 0)
715 return traits_type::eof();
716 bool __initial = __read_mode();
717 char_type __1buf;
718 if (this->gptr() == 0)
719 this->setg(&__1buf, &__1buf+1, &__1buf+1);
720 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
721 int_type __c = traits_type::eof();
722 if (this->gptr() == this->egptr())
723 {
724 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
725 if (__always_noconv_)
726 {
727 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
728 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
729 if (__nmemb != 0)
730 {
731 this->setg(this->eback(),
732 this->eback() + __unget_sz,
733 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000734 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 }
736 }
737 else
738 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000739 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
740 if (__extbufend_ != __extbufnext_)
741 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
743 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000744 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 static_cast<size_t>(__extbufend_ - __extbufnext_));
746 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000747 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000748 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 if (__nr != 0)
750 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000751 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000752 __throw_bad_cast();
753
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754 __extbufend_ = __extbufnext_ + __nr;
755 char_type* __inext;
756 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
757 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000758 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 if (__r == codecvt_base::noconv)
760 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000761 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
762 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000763 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 }
765 else if (__inext != this->eback() + __unget_sz)
766 {
767 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000768 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 }
770 }
771 }
772 }
773 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000774 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 if (this->eback() == &__1buf)
776 this->setg(0, 0, 0);
777 return __c;
778}
779
780template <class _CharT, class _Traits>
781typename basic_filebuf<_CharT, _Traits>::int_type
782basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
783{
784 if (__file_ && this->eback() < this->gptr())
785 {
786 if (traits_type::eq_int_type(__c, traits_type::eof()))
787 {
788 this->gbump(-1);
789 return traits_type::not_eof(__c);
790 }
791 if ((__om_ & ios_base::out) ||
792 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
793 {
794 this->gbump(-1);
795 *this->gptr() = traits_type::to_char_type(__c);
796 return __c;
797 }
798 }
799 return traits_type::eof();
800}
801
802template <class _CharT, class _Traits>
803typename basic_filebuf<_CharT, _Traits>::int_type
804basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
805{
806 if (__file_ == 0)
807 return traits_type::eof();
808 __write_mode();
809 char_type __1buf;
810 char_type* __pb_save = this->pbase();
811 char_type* __epb_save = this->epptr();
812 if (!traits_type::eq_int_type(__c, traits_type::eof()))
813 {
814 if (this->pptr() == 0)
815 this->setp(&__1buf, &__1buf+1);
816 *this->pptr() = traits_type::to_char_type(__c);
817 this->pbump(1);
818 }
819 if (this->pptr() != this->pbase())
820 {
821 if (__always_noconv_)
822 {
823 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
824 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
825 return traits_type::eof();
826 }
827 else
828 {
829 char* __extbe = __extbuf_;
830 codecvt_base::result __r;
831 do
832 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000833 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000834 __throw_bad_cast();
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 const char_type* __e;
837 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
838 __extbuf_, __extbuf_ + __ebs_, __extbe);
839 if (__e == this->pbase())
840 return traits_type::eof();
841 if (__r == codecvt_base::noconv)
842 {
843 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
844 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
845 return traits_type::eof();
846 }
847 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
848 {
849 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
850 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
851 return traits_type::eof();
852 if (__r == codecvt_base::partial)
853 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000854 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000855 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 }
857 }
858 else
859 return traits_type::eof();
860 } while (__r == codecvt_base::partial);
861 }
862 this->setp(__pb_save, __epb_save);
863 }
864 return traits_type::not_eof(__c);
865}
866
867template <class _CharT, class _Traits>
868basic_streambuf<_CharT, _Traits>*
869basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
870{
871 this->setg(0, 0, 0);
872 this->setp(0, 0);
873 if (__owns_eb_)
874 delete [] __extbuf_;
875 if (__owns_ib_)
876 delete [] __intbuf_;
877 __ebs_ = __n;
878 if (__ebs_ > sizeof(__extbuf_min_))
879 {
880 if (__always_noconv_ && __s)
881 {
882 __extbuf_ = (char*)__s;
883 __owns_eb_ = false;
884 }
885 else
886 {
887 __extbuf_ = new char[__ebs_];
888 __owns_eb_ = true;
889 }
890 }
891 else
892 {
893 __extbuf_ = __extbuf_min_;
894 __ebs_ = sizeof(__extbuf_min_);
895 __owns_eb_ = false;
896 }
897 if (!__always_noconv_)
898 {
899 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
900 if (__s && __ibs_ >= sizeof(__extbuf_min_))
901 {
902 __intbuf_ = __s;
903 __owns_ib_ = false;
904 }
905 else
906 {
907 __intbuf_ = new char_type[__ibs_];
908 __owns_ib_ = true;
909 }
910 }
911 else
912 {
913 __ibs_ = 0;
914 __intbuf_ = 0;
915 __owns_ib_ = false;
916 }
917 return this;
918}
919
920template <class _CharT, class _Traits>
921typename basic_filebuf<_CharT, _Traits>::pos_type
922basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
923 ios_base::openmode)
924{
Howard Hinnant0090b122012-08-24 16:52:47 +0000925 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000926 __throw_bad_cast();
927
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 int __width = __cv_->encoding();
929 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
930 return pos_type(off_type(-1));
931 // __width > 0 || __off == 0
932 int __whence;
933 switch (__way)
934 {
935 case ios_base::beg:
936 __whence = SEEK_SET;
937 break;
938 case ios_base::cur:
939 __whence = SEEK_CUR;
940 break;
941 case ios_base::end:
942 __whence = SEEK_END;
943 break;
944 default:
945 return pos_type(off_type(-1));
946 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000947#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000948 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
949 return pos_type(off_type(-1));
950 pos_type __r = ftell(__file_);
951#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
953 return pos_type(off_type(-1));
954 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000955#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 __r.state(__st_);
957 return __r;
958}
959
960template <class _CharT, class _Traits>
961typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000962basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963{
964 if (__file_ == 0 || sync())
965 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000966#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000967 if (fseek(__file_, __sp, SEEK_SET))
968 return pos_type(off_type(-1));
969#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 if (fseeko(__file_, __sp, SEEK_SET))
971 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000972#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000973 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 return __sp;
975}
976
977template <class _CharT, class _Traits>
978int
979basic_filebuf<_CharT, _Traits>::sync()
980{
981 if (__file_ == 0)
982 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000983 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000984 __throw_bad_cast();
985
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 if (__cm_ & ios_base::out)
987 {
988 if (this->pptr() != this->pbase())
989 if (overflow() == traits_type::eof())
990 return -1;
991 codecvt_base::result __r;
992 do
993 {
994 char* __extbe;
995 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
996 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
997 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
998 return -1;
999 } while (__r == codecvt_base::partial);
1000 if (__r == codecvt_base::error)
1001 return -1;
1002 if (fflush(__file_))
1003 return -1;
1004 }
1005 else if (__cm_ & ios_base::in)
1006 {
1007 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001008 state_type __state = __st_last_;
1009 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 if (__always_noconv_)
1011 __c = this->egptr() - this->gptr();
1012 else
1013 {
1014 int __width = __cv_->encoding();
1015 __c = __extbufend_ - __extbufnext_;
1016 if (__width > 0)
1017 __c += __width * (this->egptr() - this->gptr());
1018 else
1019 {
1020 if (this->gptr() != this->egptr())
1021 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001022 const int __off = __cv_->length(__state, __extbuf_,
1023 __extbufnext_,
1024 this->gptr() - this->eback());
1025 __c += __extbufnext_ - __extbuf_ - __off;
1026 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 }
1028 }
1029 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001030#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001031 if (fseek(__file_, -__c, SEEK_CUR))
1032 return -1;
1033#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 if (fseeko(__file_, -__c, SEEK_CUR))
1035 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001036#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001037 if (__update_st)
1038 __st_ = __state;
1039 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 this->setg(0, 0, 0);
1041 __cm_ = 0;
1042 }
1043 return 0;
1044}
1045
1046template <class _CharT, class _Traits>
1047void
1048basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1049{
1050 sync();
1051 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1052 bool __old_anc = __always_noconv_;
1053 __always_noconv_ = __cv_->always_noconv();
1054 if (__old_anc != __always_noconv_)
1055 {
1056 this->setg(0, 0, 0);
1057 this->setp(0, 0);
1058 // invariant, char_type is char, else we couldn't get here
1059 if (__always_noconv_) // need to dump __intbuf_
1060 {
1061 if (__owns_eb_)
1062 delete [] __extbuf_;
1063 __owns_eb_ = __owns_ib_;
1064 __ebs_ = __ibs_;
1065 __extbuf_ = (char*)__intbuf_;
1066 __ibs_ = 0;
1067 __intbuf_ = 0;
1068 __owns_ib_ = false;
1069 }
1070 else // need to obtain an __intbuf_.
1071 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1072 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1073 {
1074 __ibs_ = __ebs_;
1075 __intbuf_ = (char_type*)__extbuf_;
1076 __owns_ib_ = false;
1077 __extbuf_ = new char[__ebs_];
1078 __owns_eb_ = true;
1079 }
1080 else
1081 {
1082 __ibs_ = __ebs_;
1083 __intbuf_ = new char_type[__ibs_];
1084 __owns_ib_ = true;
1085 }
1086 }
1087 }
1088}
1089
1090template <class _CharT, class _Traits>
1091bool
1092basic_filebuf<_CharT, _Traits>::__read_mode()
1093{
1094 if (!(__cm_ & ios_base::in))
1095 {
1096 this->setp(0, 0);
1097 if (__always_noconv_)
1098 this->setg((char_type*)__extbuf_,
1099 (char_type*)__extbuf_ + __ebs_,
1100 (char_type*)__extbuf_ + __ebs_);
1101 else
1102 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1103 __cm_ = ios_base::in;
1104 return true;
1105 }
1106 return false;
1107}
1108
1109template <class _CharT, class _Traits>
1110void
1111basic_filebuf<_CharT, _Traits>::__write_mode()
1112{
1113 if (!(__cm_ & ios_base::out))
1114 {
1115 this->setg(0, 0, 0);
1116 if (__ebs_ > sizeof(__extbuf_min_))
1117 {
1118 if (__always_noconv_)
1119 this->setp((char_type*)__extbuf_,
1120 (char_type*)__extbuf_ + (__ebs_ - 1));
1121 else
1122 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1123 }
1124 else
1125 this->setp(0, 0);
1126 __cm_ = ios_base::out;
1127 }
1128}
1129
1130// basic_ifstream
1131
1132template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001133class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 : public basic_istream<_CharT, _Traits>
1135{
1136public:
1137 typedef _CharT char_type;
1138 typedef _Traits traits_type;
1139 typedef typename traits_type::int_type int_type;
1140 typedef typename traits_type::pos_type pos_type;
1141 typedef typename traits_type::off_type off_type;
1142
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001145#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001148#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 _LIBCPP_INLINE_VISIBILITY
1150 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1151#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001154#if _LIBCPP_STD_VER >= 17
1155 _LIBCPP_INLINE_VISIBILITY
1156 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1157 : basic_ifstream(__p.c_str(), __mode) {}
1158#endif // _LIBCPP_STD_VER >= 17
Ed Schouten7009f4e2015-03-12 15:44:39 +00001159#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001160#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 basic_ifstream& operator=(basic_ifstream&& __rhs);
1166#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 void swap(basic_ifstream& __rhs);
1169
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001174#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001176#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1177 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1178#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001180#if _LIBCPP_STD_VER >= 17
1181 _LIBCPP_INLINE_VISIBILITY
1182 void open(const filesystem::path& __p,
1183 ios_base::openmode __mode = ios_base::in) {
1184 return open(__p.c_str(), __mode);
1185 }
1186#endif // _LIBCPP_STD_VER >= 17
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001187
1188 _LIBCPP_INLINE_VISIBILITY
1189 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001190#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 void close();
1193
1194private:
1195 basic_filebuf<char_type, traits_type> __sb_;
1196};
1197
1198template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001199inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200basic_ifstream<_CharT, _Traits>::basic_ifstream()
1201 : basic_istream<char_type, traits_type>(&__sb_)
1202{
1203}
1204
Ed Schouten7009f4e2015-03-12 15:44:39 +00001205#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001207inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1209 : basic_istream<char_type, traits_type>(&__sb_)
1210{
1211 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1212 this->setstate(ios_base::failbit);
1213}
1214
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001215#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1216template <class _CharT, class _Traits>
1217inline
1218basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1219 : basic_istream<char_type, traits_type>(&__sb_)
1220{
1221 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1222 this->setstate(ios_base::failbit);
1223}
1224#endif
1225
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001227inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1229 : basic_istream<char_type, traits_type>(&__sb_)
1230{
1231 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1232 this->setstate(ios_base::failbit);
1233}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Eric Fiselier78ccf772017-04-18 23:38:41 +00001236#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
1238template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001239inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001241 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1242 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243{
1244 this->set_rdbuf(&__sb_);
1245}
1246
1247template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001248inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249basic_ifstream<_CharT, _Traits>&
1250basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001252 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1253 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 return *this;
1255}
1256
Eric Fiselier78ccf772017-04-18 23:38:41 +00001257#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
1259template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001260inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261void
1262basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1263{
1264 basic_istream<char_type, traits_type>::swap(__rhs);
1265 __sb_.swap(__rhs.__sb_);
1266}
1267
1268template <class _CharT, class _Traits>
1269inline _LIBCPP_INLINE_VISIBILITY
1270void
1271swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1272{
1273 __x.swap(__y);
1274}
1275
1276template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001277inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278basic_filebuf<_CharT, _Traits>*
1279basic_ifstream<_CharT, _Traits>::rdbuf() const
1280{
1281 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1282}
1283
1284template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001285inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286bool
1287basic_ifstream<_CharT, _Traits>::is_open() const
1288{
1289 return __sb_.is_open();
1290}
1291
Ed Schouten7009f4e2015-03-12 15:44:39 +00001292#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293template <class _CharT, class _Traits>
1294void
1295basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1296{
1297 if (__sb_.open(__s, __mode | ios_base::in))
1298 this->clear();
1299 else
1300 this->setstate(ios_base::failbit);
1301}
1302
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001303#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1304template <class _CharT, class _Traits>
1305void
1306basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1307{
1308 if (__sb_.open(__s, __mode | ios_base::in))
1309 this->clear();
1310 else
1311 this->setstate(ios_base::failbit);
1312}
1313#endif
1314
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315template <class _CharT, class _Traits>
1316void
1317basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1318{
1319 if (__sb_.open(__s, __mode | ios_base::in))
1320 this->clear();
1321 else
1322 this->setstate(ios_base::failbit);
1323}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001324
1325template <class _CharT, class _Traits>
1326void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1327 ios_base::openmode __mode) {
1328 if (__sb_.__open(__fd, __mode | ios_base::in))
1329 this->clear();
1330 else
1331 this->setstate(ios_base::failbit);
1332}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001333#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
1335template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337void
1338basic_ifstream<_CharT, _Traits>::close()
1339{
1340 if (__sb_.close() == 0)
1341 this->setstate(ios_base::failbit);
1342}
1343
1344// basic_ofstream
1345
1346template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001347class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 : public basic_ostream<_CharT, _Traits>
1349{
1350public:
1351 typedef _CharT char_type;
1352 typedef _Traits traits_type;
1353 typedef typename traits_type::int_type int_type;
1354 typedef typename traits_type::pos_type pos_type;
1355 typedef typename traits_type::off_type off_type;
1356
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001361#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1362 _LIBCPP_INLINE_VISIBILITY
1363 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1364#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001367
1368#if _LIBCPP_STD_VER >= 17
1369 _LIBCPP_INLINE_VISIBILITY
1370 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1371 : basic_ofstream(__p.c_str(), __mode) {}
1372#endif // _LIBCPP_STD_VER >= 17
1373
Eric Fiselier78ccf772017-04-18 23:38:41 +00001374#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 basic_ofstream& operator=(basic_ofstream&& __rhs);
1380#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 void swap(basic_ofstream& __rhs);
1383
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001388#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001390#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1391 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001394
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001395#if _LIBCPP_STD_VER >= 17
1396 _LIBCPP_INLINE_VISIBILITY
1397 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1398 { return open(__p.c_str(), __mode); }
1399#endif // _LIBCPP_STD_VER >= 17
1400
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001401 _LIBCPP_INLINE_VISIBILITY
1402 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001403#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 void close();
1406
1407private:
1408 basic_filebuf<char_type, traits_type> __sb_;
1409};
1410
1411template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001412inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413basic_ofstream<_CharT, _Traits>::basic_ofstream()
1414 : basic_ostream<char_type, traits_type>(&__sb_)
1415{
1416}
1417
Ed Schouten7009f4e2015-03-12 15:44:39 +00001418#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001420inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1422 : basic_ostream<char_type, traits_type>(&__sb_)
1423{
1424 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1425 this->setstate(ios_base::failbit);
1426}
1427
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001428#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1429template <class _CharT, class _Traits>
1430inline
1431basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1432 : basic_ostream<char_type, traits_type>(&__sb_)
1433{
1434 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1435 this->setstate(ios_base::failbit);
1436}
1437#endif
1438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001440inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1442 : basic_ostream<char_type, traits_type>(&__sb_)
1443{
1444 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1445 this->setstate(ios_base::failbit);
1446}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001447#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448
Eric Fiselier78ccf772017-04-18 23:38:41 +00001449#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
1451template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001452inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001454 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1455 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456{
1457 this->set_rdbuf(&__sb_);
1458}
1459
1460template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001461inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462basic_ofstream<_CharT, _Traits>&
1463basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1464{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001465 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1466 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467 return *this;
1468}
1469
Eric Fiselier78ccf772017-04-18 23:38:41 +00001470#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
1472template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001473inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474void
1475basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1476{
1477 basic_ostream<char_type, traits_type>::swap(__rhs);
1478 __sb_.swap(__rhs.__sb_);
1479}
1480
1481template <class _CharT, class _Traits>
1482inline _LIBCPP_INLINE_VISIBILITY
1483void
1484swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1485{
1486 __x.swap(__y);
1487}
1488
1489template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001490inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491basic_filebuf<_CharT, _Traits>*
1492basic_ofstream<_CharT, _Traits>::rdbuf() const
1493{
1494 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1495}
1496
1497template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001498inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499bool
1500basic_ofstream<_CharT, _Traits>::is_open() const
1501{
1502 return __sb_.is_open();
1503}
1504
Ed Schouten7009f4e2015-03-12 15:44:39 +00001505#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506template <class _CharT, class _Traits>
1507void
1508basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1509{
1510 if (__sb_.open(__s, __mode | ios_base::out))
1511 this->clear();
1512 else
1513 this->setstate(ios_base::failbit);
1514}
1515
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001516#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1517template <class _CharT, class _Traits>
1518void
1519basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1520{
1521 if (__sb_.open(__s, __mode | ios_base::out))
1522 this->clear();
1523 else
1524 this->setstate(ios_base::failbit);
1525}
1526#endif
1527
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528template <class _CharT, class _Traits>
1529void
1530basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1531{
1532 if (__sb_.open(__s, __mode | ios_base::out))
1533 this->clear();
1534 else
1535 this->setstate(ios_base::failbit);
1536}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001537
1538template <class _CharT, class _Traits>
1539void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1540 ios_base::openmode __mode) {
1541 if (__sb_.__open(__fd, __mode | ios_base::out))
1542 this->clear();
1543 else
1544 this->setstate(ios_base::failbit);
1545}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001546#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547
1548template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001549inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550void
1551basic_ofstream<_CharT, _Traits>::close()
1552{
1553 if (__sb_.close() == 0)
1554 this->setstate(ios_base::failbit);
1555}
1556
1557// basic_fstream
1558
1559template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001560class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 : public basic_iostream<_CharT, _Traits>
1562{
1563public:
1564 typedef _CharT char_type;
1565 typedef _Traits traits_type;
1566 typedef typename traits_type::int_type int_type;
1567 typedef typename traits_type::pos_type pos_type;
1568 typedef typename traits_type::off_type off_type;
1569
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001572#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001575#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1576 _LIBCPP_INLINE_VISIBILITY
1577 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1578#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001581
1582#if _LIBCPP_STD_VER >= 17
1583 _LIBCPP_INLINE_VISIBILITY
1584 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1585 : basic_fstream(__p.c_str(), __mode) {}
1586#endif // _LIBCPP_STD_VER >= 17
1587
Ed Schouten7009f4e2015-03-12 15:44:39 +00001588#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001589#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 basic_fstream& operator=(basic_fstream&& __rhs);
1595#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 void swap(basic_fstream& __rhs);
1598
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001603#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001605#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1606 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1607#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001609
1610#if _LIBCPP_STD_VER >= 17
1611 _LIBCPP_INLINE_VISIBILITY
1612 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)
1613 { return open(__p.c_str(), __mode); }
1614#endif // _LIBCPP_STD_VER >= 17
1615
Ed Schouten7009f4e2015-03-12 15:44:39 +00001616#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 void close();
1619
1620private:
1621 basic_filebuf<char_type, traits_type> __sb_;
1622};
1623
1624template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001625inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626basic_fstream<_CharT, _Traits>::basic_fstream()
1627 : basic_iostream<char_type, traits_type>(&__sb_)
1628{
1629}
1630
Ed Schouten7009f4e2015-03-12 15:44:39 +00001631#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001633inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1635 : basic_iostream<char_type, traits_type>(&__sb_)
1636{
1637 if (__sb_.open(__s, __mode) == 0)
1638 this->setstate(ios_base::failbit);
1639}
1640
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001641#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1642template <class _CharT, class _Traits>
1643inline
1644basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1645 : basic_iostream<char_type, traits_type>(&__sb_)
1646{
1647 if (__sb_.open(__s, __mode) == 0)
1648 this->setstate(ios_base::failbit);
1649}
1650#endif
1651
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1655 : basic_iostream<char_type, traits_type>(&__sb_)
1656{
1657 if (__sb_.open(__s, __mode) == 0)
1658 this->setstate(ios_base::failbit);
1659}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001660#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661
Eric Fiselier78ccf772017-04-18 23:38:41 +00001662#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663
1664template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001665inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001667 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1668 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669{
1670 this->set_rdbuf(&__sb_);
1671}
1672
1673template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001674inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675basic_fstream<_CharT, _Traits>&
1676basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1677{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001678 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1679 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 return *this;
1681}
1682
Eric Fiselier78ccf772017-04-18 23:38:41 +00001683#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684
1685template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001686inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687void
1688basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1689{
1690 basic_iostream<char_type, traits_type>::swap(__rhs);
1691 __sb_.swap(__rhs.__sb_);
1692}
1693
1694template <class _CharT, class _Traits>
1695inline _LIBCPP_INLINE_VISIBILITY
1696void
1697swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1698{
1699 __x.swap(__y);
1700}
1701
1702template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704basic_filebuf<_CharT, _Traits>*
1705basic_fstream<_CharT, _Traits>::rdbuf() const
1706{
1707 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1708}
1709
1710template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001711inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712bool
1713basic_fstream<_CharT, _Traits>::is_open() const
1714{
1715 return __sb_.is_open();
1716}
1717
Ed Schouten7009f4e2015-03-12 15:44:39 +00001718#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719template <class _CharT, class _Traits>
1720void
1721basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1722{
1723 if (__sb_.open(__s, __mode))
1724 this->clear();
1725 else
1726 this->setstate(ios_base::failbit);
1727}
1728
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001729#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1730template <class _CharT, class _Traits>
1731void
1732basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1733{
1734 if (__sb_.open(__s, __mode))
1735 this->clear();
1736 else
1737 this->setstate(ios_base::failbit);
1738}
1739#endif
1740
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741template <class _CharT, class _Traits>
1742void
1743basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1744{
1745 if (__sb_.open(__s, __mode))
1746 this->clear();
1747 else
1748 this->setstate(ios_base::failbit);
1749}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001750#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751
1752template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001753inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754void
1755basic_fstream<_CharT, _Traits>::close()
1756{
1757 if (__sb_.close() == 0)
1758 this->setstate(ios_base::failbit);
1759}
1760
1761_LIBCPP_END_NAMESPACE_STD
1762
Eric Fiselierf4433a32017-05-31 22:07:49 +00001763_LIBCPP_POP_MACROS
1764
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765#endif // _LIBCPP_FSTREAM