blob: 8b9aefaaca3328432e6100bf120c98c6930153a1 [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);
41 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);
80 basic_ifstream(basic_ifstream&& rhs);
81
82 basic_ifstream& operator=(basic_ifstream&& rhs);
83 void swap(basic_ifstream& rhs);
84
85 basic_filebuf<char_type, traits_type>* rdbuf() const;
86 bool is_open() const;
87 void open(const char* s, ios_base::openmode mode = ios_base::in);
88 void open(const string& s, ios_base::openmode mode = ios_base::in);
89 void close();
90};
91
92template <class charT, class traits>
93 void
94 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
95
96typedef basic_ifstream<char> ifstream;
97typedef basic_ifstream<wchar_t> wifstream;
98
99template <class charT, class traits = char_traits<charT> >
100class basic_ofstream
101 : public basic_ostream<charT,traits>
102{
103public:
104 typedef charT char_type;
105 typedef traits traits_type;
106 typedef typename traits_type::int_type int_type;
107 typedef typename traits_type::pos_type pos_type;
108 typedef typename traits_type::off_type off_type;
109
110 basic_ofstream();
111 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
112 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
113 basic_ofstream(basic_ofstream&& rhs);
114
115 basic_ofstream& operator=(basic_ofstream&& rhs);
116 void swap(basic_ofstream& rhs);
117
118 basic_filebuf<char_type, traits_type>* rdbuf() const;
119 bool is_open() const;
120 void open(const char* s, ios_base::openmode mode = ios_base::out);
121 void open(const string& s, ios_base::openmode mode = ios_base::out);
122 void close();
123};
124
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000125template <class charT, class traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 void
127 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
128
129typedef basic_ofstream<char> ofstream;
130typedef basic_ofstream<wchar_t> wofstream;
131
132template <class charT, class traits=char_traits<charT> >
133class basic_fstream
134 : public basic_iostream<charT,traits>
135{
136public:
137 typedef charT char_type;
138 typedef traits traits_type;
139 typedef typename traits_type::int_type int_type;
140 typedef typename traits_type::pos_type pos_type;
141 typedef typename traits_type::off_type off_type;
142
143 basic_fstream();
144 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
145 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
146 basic_fstream(basic_fstream&& rhs);
147
148 basic_fstream& operator=(basic_fstream&& rhs);
149 void swap(basic_fstream& rhs);
150
151 basic_filebuf<char_type, traits_type>* rdbuf() const;
152 bool is_open() const;
153 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
155 void close();
156};
157
158template <class charT, class traits>
159 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
160
161typedef basic_fstream<char> fstream;
162typedef basic_fstream<wchar_t> wfstream;
163
164} // std
165
166*/
167
168#include <__config>
169#include <ostream>
170#include <istream>
171#include <__locale>
172#include <cstdio>
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000173#include <cstdlib>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000175#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000177#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
Eric Fiselierf4433a32017-05-31 22:07:49 +0000179_LIBCPP_PUSH_MACROS
180#include <__undef_macros>
181
182
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183_LIBCPP_BEGIN_NAMESPACE_STD
184
185template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000186class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 : public basic_streambuf<_CharT, _Traits>
188{
189public:
190 typedef _CharT char_type;
191 typedef _Traits traits_type;
192 typedef typename traits_type::int_type int_type;
193 typedef typename traits_type::pos_type pos_type;
194 typedef typename traits_type::off_type off_type;
195 typedef typename traits_type::state_type state_type;
196
197 // 27.9.1.2 Constructors/destructor:
198 basic_filebuf();
Eric Fiselier78ccf772017-04-18 23:38:41 +0000199#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200 basic_filebuf(basic_filebuf&& __rhs);
201#endif
202 virtual ~basic_filebuf();
203
204 // 27.9.1.3 Assign/swap:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000205#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 basic_filebuf& operator=(basic_filebuf&& __rhs);
208#endif
209 void swap(basic_filebuf& __rhs);
210
211 // 27.9.1.4 Members:
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +0000214#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000216#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
217 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
218#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000221
222 _LIBCPP_INLINE_VISIBILITY
223 basic_filebuf* __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +0000224#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 basic_filebuf* close();
226
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000227 _LIBCPP_INLINE_VISIBILITY
228 inline static const char*
229 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
230
231 protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 // 27.9.1.5 Overridden virtual functions:
233 virtual int_type underflow();
234 virtual int_type pbackfail(int_type __c = traits_type::eof());
235 virtual int_type overflow (int_type __c = traits_type::eof());
236 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
237 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
238 ios_base::openmode __wch = ios_base::in | ios_base::out);
239 virtual pos_type seekpos(pos_type __sp,
240 ios_base::openmode __wch = ios_base::in | ios_base::out);
241 virtual int sync();
242 virtual void imbue(const locale& __loc);
243
244private:
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000245 char* __extbuf_;
246 const char* __extbufnext_;
247 const char* __extbufend_;
248 char __extbuf_min_[8];
249 size_t __ebs_;
250 char_type* __intbuf_;
251 size_t __ibs_;
252 FILE* __file_;
253 const codecvt<char_type, char, state_type>* __cv_;
254 state_type __st_;
255 state_type __st_last_;
256 ios_base::openmode __om_;
257 ios_base::openmode __cm_;
258 bool __owns_eb_;
259 bool __owns_ib_;
260 bool __always_noconv_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000262 bool __read_mode();
263 void __write_mode();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264};
265
266template <class _CharT, class _Traits>
267basic_filebuf<_CharT, _Traits>::basic_filebuf()
268 : __extbuf_(0),
269 __extbufnext_(0),
270 __extbufend_(0),
271 __ebs_(0),
272 __intbuf_(0),
273 __ibs_(0),
274 __file_(0),
Howard Hinnant0090b122012-08-24 16:52:47 +0000275 __cv_(nullptr),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276 __st_(),
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000277 __st_last_(),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278 __om_(0),
279 __cm_(0),
280 __owns_eb_(false),
281 __owns_ib_(false),
Howard Hinnant0090b122012-08-24 16:52:47 +0000282 __always_noconv_(false)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283{
Howard Hinnant0090b122012-08-24 16:52:47 +0000284 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
285 {
286 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
287 __always_noconv_ = __cv_->always_noconv();
288 }
Howard Hinnantfd49f462012-08-24 18:06:47 +0000289 setbuf(0, 4096);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290}
291
Eric Fiselier78ccf772017-04-18 23:38:41 +0000292#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
294template <class _CharT, class _Traits>
295basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
296 : basic_streambuf<_CharT, _Traits>(__rhs)
297{
298 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
299 {
300 __extbuf_ = __extbuf_min_;
301 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
302 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
303 }
304 else
305 {
306 __extbuf_ = __rhs.__extbuf_;
307 __extbufnext_ = __rhs.__extbufnext_;
308 __extbufend_ = __rhs.__extbufend_;
309 }
310 __ebs_ = __rhs.__ebs_;
311 __intbuf_ = __rhs.__intbuf_;
312 __ibs_ = __rhs.__ibs_;
313 __file_ = __rhs.__file_;
314 __cv_ = __rhs.__cv_;
315 __st_ = __rhs.__st_;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000316 __st_last_ = __rhs.__st_last_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317 __om_ = __rhs.__om_;
318 __cm_ = __rhs.__cm_;
319 __owns_eb_ = __rhs.__owns_eb_;
320 __owns_ib_ = __rhs.__owns_ib_;
321 __always_noconv_ = __rhs.__always_noconv_;
322 if (__rhs.pbase())
323 {
324 if (__rhs.pbase() == __rhs.__intbuf_)
325 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
326 else
327 this->setp((char_type*)__extbuf_,
328 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
Marshall Clow33932622017-09-12 15:00:43 +0000329 this->__pbump(__rhs. pptr() - __rhs.pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 }
331 else if (__rhs.eback())
332 {
333 if (__rhs.eback() == __rhs.__intbuf_)
334 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
335 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
336 else
337 this->setg((char_type*)__extbuf_,
338 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
339 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
340 }
341 __rhs.__extbuf_ = 0;
342 __rhs.__extbufnext_ = 0;
343 __rhs.__extbufend_ = 0;
344 __rhs.__ebs_ = 0;
345 __rhs.__intbuf_ = 0;
346 __rhs.__ibs_ = 0;
347 __rhs.__file_ = 0;
348 __rhs.__st_ = state_type();
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000349 __rhs.__st_last_ = state_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350 __rhs.__om_ = 0;
351 __rhs.__cm_ = 0;
352 __rhs.__owns_eb_ = false;
353 __rhs.__owns_ib_ = false;
354 __rhs.setg(0, 0, 0);
355 __rhs.setp(0, 0);
356}
357
358template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000359inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360basic_filebuf<_CharT, _Traits>&
361basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
362{
363 close();
364 swap(__rhs);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000365 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366}
367
Eric Fiselier78ccf772017-04-18 23:38:41 +0000368#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369
370template <class _CharT, class _Traits>
371basic_filebuf<_CharT, _Traits>::~basic_filebuf()
372{
373#ifndef _LIBCPP_NO_EXCEPTIONS
374 try
375 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000376#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 close();
378#ifndef _LIBCPP_NO_EXCEPTIONS
379 }
380 catch (...)
381 {
382 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000383#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384 if (__owns_eb_)
385 delete [] __extbuf_;
386 if (__owns_ib_)
387 delete [] __intbuf_;
388}
389
390template <class _CharT, class _Traits>
391void
392basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
393{
394 basic_streambuf<char_type, traits_type>::swap(__rhs);
395 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
396 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000397 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
398 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
399 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 }
401 else
402 {
403 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
404 ptrdiff_t __le = __extbufend_ - __extbuf_;
405 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
406 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
407 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
408 {
409 __extbuf_ = __rhs.__extbuf_;
410 __rhs.__extbuf_ = __rhs.__extbuf_min_;
411 }
412 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
413 {
414 __rhs.__extbuf_ = __extbuf_;
415 __extbuf_ = __extbuf_min_;
416 }
417 __extbufnext_ = __extbuf_ + __rn;
418 __extbufend_ = __extbuf_ + __re;
419 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
420 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
421 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000422 _VSTD::swap(__ebs_, __rhs.__ebs_);
423 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
424 _VSTD::swap(__ibs_, __rhs.__ibs_);
425 _VSTD::swap(__file_, __rhs.__file_);
426 _VSTD::swap(__cv_, __rhs.__cv_);
427 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000428 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000429 _VSTD::swap(__om_, __rhs.__om_);
430 _VSTD::swap(__cm_, __rhs.__cm_);
431 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
432 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
433 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
435 {
436 ptrdiff_t __n = this->gptr() - this->eback();
437 ptrdiff_t __e = this->egptr() - this->eback();
438 this->setg((char_type*)__extbuf_min_,
439 (char_type*)__extbuf_min_ + __n,
440 (char_type*)__extbuf_min_ + __e);
441 }
442 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
443 {
444 ptrdiff_t __n = this->pptr() - this->pbase();
445 ptrdiff_t __e = this->epptr() - this->pbase();
446 this->setp((char_type*)__extbuf_min_,
447 (char_type*)__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000448 this->__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449 }
450 if (__rhs.eback() == (char_type*)__extbuf_min_)
451 {
452 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
453 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
454 __rhs.setg((char_type*)__rhs.__extbuf_min_,
455 (char_type*)__rhs.__extbuf_min_ + __n,
456 (char_type*)__rhs.__extbuf_min_ + __e);
457 }
458 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
459 {
460 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
461 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
462 __rhs.setp((char_type*)__rhs.__extbuf_min_,
463 (char_type*)__rhs.__extbuf_min_ + __e);
Marshall Clow33932622017-09-12 15:00:43 +0000464 __rhs.__pbump(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465 }
466}
467
468template <class _CharT, class _Traits>
469inline _LIBCPP_INLINE_VISIBILITY
470void
471swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
472{
473 __x.swap(__y);
474}
475
476template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000477inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478bool
479basic_filebuf<_CharT, _Traits>::is_open() const
480{
481 return __file_ != 0;
482}
483
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000484template <class _CharT, class _Traits>
485const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
486 ios_base::openmode __mode) _NOEXCEPT {
487 switch (__mode & ~ios_base::ate) {
488 case ios_base::out:
489 case ios_base::out | ios_base::trunc:
490 return "w";
491 case ios_base::out | ios_base::app:
492 case ios_base::app:
493 return "a";
494 case ios_base::in:
495 return "r";
496 case ios_base::in | ios_base::out:
497 return "r+";
498 case ios_base::in | ios_base::out | ios_base::trunc:
499 return "w+";
500 case ios_base::in | ios_base::out | ios_base::app:
501 case ios_base::in | ios_base::app:
502 return "a+";
503 case ios_base::out | ios_base::binary:
504 case ios_base::out | ios_base::trunc | ios_base::binary:
505 return "wb";
506 case ios_base::out | ios_base::app | ios_base::binary:
507 case ios_base::app | ios_base::binary:
508 return "ab";
509 case ios_base::in | ios_base::binary:
510 return "rb";
511 case ios_base::in | ios_base::out | ios_base::binary:
512 return "r+b";
513 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
514 return "w+b";
515 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
516 case ios_base::in | ios_base::app | ios_base::binary:
517 return "a+b";
518 default:
519 return nullptr;
520 }
521 _LIBCPP_UNREACHABLE();
522}
523
Ed Schouten7009f4e2015-03-12 15:44:39 +0000524#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525template <class _CharT, class _Traits>
526basic_filebuf<_CharT, _Traits>*
527basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
528{
529 basic_filebuf<_CharT, _Traits>* __rt = 0;
530 if (__file_ == 0)
531 {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000532 if (const char* __mdstr = __make_mdstring(__mode)) {
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533 __rt = this;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000534 __file_ = fopen(__s, __mdstr);
535 if (__file_) {
536 __om_ = __mode;
537 if (__mode & ios_base::ate) {
538 if (fseek(__file_, 0, SEEK_END)) {
539 fclose(__file_);
540 __file_ = 0;
541 __rt = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000543 }
544 } else
545 __rt = 0;
546 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547 }
548 return __rt;
549}
550
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000551template <class _CharT, class _Traits>
552_LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>*
553basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
554 basic_filebuf<_CharT, _Traits>* __rt = 0;
555 if (__file_ == 0) {
556 if (const char* __mdstr = __make_mdstring(__mode)) {
557 __rt = this;
558 __file_ = fdopen(__fd, __mdstr);
559 if (__file_) {
560 __om_ = __mode;
561 if (__mode & ios_base::ate) {
562 if (fseek(__file_, 0, SEEK_END)) {
563 fclose(__file_);
564 __file_ = 0;
565 __rt = 0;
566 }
567 }
568 } else
569 __rt = 0;
570 }
571 }
572 return __rt;
573}
574
Peter Collingbourne63ebbd72018-01-23 02:07:27 +0000575#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
576// This is basically the same as the char* overload except that it uses _wfopen
577// and long mode strings.
578template <class _CharT, class _Traits>
579basic_filebuf<_CharT, _Traits>*
580basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
581{
582 basic_filebuf<_CharT, _Traits>* __rt = 0;
583 if (__file_ == 0)
584 {
585 __rt = this;
586 const wchar_t* __mdstr;
587 switch (__mode & ~ios_base::ate)
588 {
589 case ios_base::out:
590 case ios_base::out | ios_base::trunc:
591 __mdstr = L"w";
592 break;
593 case ios_base::out | ios_base::app:
594 case ios_base::app:
595 __mdstr = L"a";
596 break;
597 case ios_base::in:
598 __mdstr = L"r";
599 break;
600 case ios_base::in | ios_base::out:
601 __mdstr = L"r+";
602 break;
603 case ios_base::in | ios_base::out | ios_base::trunc:
604 __mdstr = L"w+";
605 break;
606 case ios_base::in | ios_base::out | ios_base::app:
607 case ios_base::in | ios_base::app:
608 __mdstr = L"a+";
609 break;
610 case ios_base::out | ios_base::binary:
611 case ios_base::out | ios_base::trunc | ios_base::binary:
612 __mdstr = L"wb";
613 break;
614 case ios_base::out | ios_base::app | ios_base::binary:
615 case ios_base::app | ios_base::binary:
616 __mdstr = L"ab";
617 break;
618 case ios_base::in | ios_base::binary:
619 __mdstr = L"rb";
620 break;
621 case ios_base::in | ios_base::out | ios_base::binary:
622 __mdstr = L"r+b";
623 break;
624 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
625 __mdstr = L"w+b";
626 break;
627 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
628 case ios_base::in | ios_base::app | ios_base::binary:
629 __mdstr = L"a+b";
630 break;
631 default:
632 __rt = 0;
633 break;
634 }
635 if (__rt)
636 {
637 __file_ = _wfopen(__s, __mdstr);
638 if (__file_)
639 {
640 __om_ = __mode;
641 if (__mode & ios_base::ate)
642 {
643 if (fseek(__file_, 0, SEEK_END))
644 {
645 fclose(__file_);
646 __file_ = 0;
647 __rt = 0;
648 }
649 }
650 }
651 else
652 __rt = 0;
653 }
654 }
655 return __rt;
656}
657#endif
658
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000660inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661basic_filebuf<_CharT, _Traits>*
662basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
663{
664 return open(__s.c_str(), __mode);
665}
Ed Schouten7009f4e2015-03-12 15:44:39 +0000666#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667
668template <class _CharT, class _Traits>
669basic_filebuf<_CharT, _Traits>*
670basic_filebuf<_CharT, _Traits>::close()
671{
672 basic_filebuf<_CharT, _Traits>* __rt = 0;
673 if (__file_)
674 {
675 __rt = this;
676 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnantc8697b62012-01-12 23:37:51 +0000677 if (sync())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 __rt = 0;
679 if (fclose(__h.release()) == 0)
680 __file_ = 0;
681 else
682 __rt = 0;
683 }
684 return __rt;
685}
686
687template <class _CharT, class _Traits>
688typename basic_filebuf<_CharT, _Traits>::int_type
689basic_filebuf<_CharT, _Traits>::underflow()
690{
691 if (__file_ == 0)
692 return traits_type::eof();
693 bool __initial = __read_mode();
694 char_type __1buf;
695 if (this->gptr() == 0)
696 this->setg(&__1buf, &__1buf+1, &__1buf+1);
697 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
698 int_type __c = traits_type::eof();
699 if (this->gptr() == this->egptr())
700 {
701 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
702 if (__always_noconv_)
703 {
704 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
705 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
706 if (__nmemb != 0)
707 {
708 this->setg(this->eback(),
709 this->eback() + __unget_sz,
710 this->eback() + __unget_sz + __nmemb);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000711 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 }
713 }
714 else
715 {
Marshall Clow6184b9a2016-06-04 16:16:59 +0000716 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
717 if (__extbufend_ != __extbufnext_)
718 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
720 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant50e10122012-08-24 20:37:00 +0000721 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 static_cast<size_t>(__extbufend_ - __extbufnext_));
723 codecvt_base::result __r;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000724 __st_last_ = __st_;
Marshall Clowbeda7142017-06-14 20:00:36 +0000725 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 if (__nr != 0)
727 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000728 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000729 __throw_bad_cast();
730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 __extbufend_ = __extbufnext_ + __nr;
732 char_type* __inext;
733 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
734 this->eback() + __unget_sz,
Howard Hinnant50e10122012-08-24 20:37:00 +0000735 this->eback() + __ibs_, __inext);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 if (__r == codecvt_base::noconv)
737 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000738 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
739 (char_type*)const_cast<char *>(__extbufend_));
Howard Hinnant9da939d2011-02-02 17:37:16 +0000740 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 }
742 else if (__inext != this->eback() + __unget_sz)
743 {
744 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant9da939d2011-02-02 17:37:16 +0000745 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 }
747 }
748 }
749 }
750 else
Howard Hinnant9da939d2011-02-02 17:37:16 +0000751 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 if (this->eback() == &__1buf)
753 this->setg(0, 0, 0);
754 return __c;
755}
756
757template <class _CharT, class _Traits>
758typename basic_filebuf<_CharT, _Traits>::int_type
759basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
760{
761 if (__file_ && this->eback() < this->gptr())
762 {
763 if (traits_type::eq_int_type(__c, traits_type::eof()))
764 {
765 this->gbump(-1);
766 return traits_type::not_eof(__c);
767 }
768 if ((__om_ & ios_base::out) ||
769 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
770 {
771 this->gbump(-1);
772 *this->gptr() = traits_type::to_char_type(__c);
773 return __c;
774 }
775 }
776 return traits_type::eof();
777}
778
779template <class _CharT, class _Traits>
780typename basic_filebuf<_CharT, _Traits>::int_type
781basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
782{
783 if (__file_ == 0)
784 return traits_type::eof();
785 __write_mode();
786 char_type __1buf;
787 char_type* __pb_save = this->pbase();
788 char_type* __epb_save = this->epptr();
789 if (!traits_type::eq_int_type(__c, traits_type::eof()))
790 {
791 if (this->pptr() == 0)
792 this->setp(&__1buf, &__1buf+1);
793 *this->pptr() = traits_type::to_char_type(__c);
794 this->pbump(1);
795 }
796 if (this->pptr() != this->pbase())
797 {
798 if (__always_noconv_)
799 {
800 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
801 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
802 return traits_type::eof();
803 }
804 else
805 {
806 char* __extbe = __extbuf_;
807 codecvt_base::result __r;
808 do
809 {
Howard Hinnant0090b122012-08-24 16:52:47 +0000810 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000811 __throw_bad_cast();
812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 const char_type* __e;
814 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
815 __extbuf_, __extbuf_ + __ebs_, __extbe);
816 if (__e == this->pbase())
817 return traits_type::eof();
818 if (__r == codecvt_base::noconv)
819 {
820 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
821 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
822 return traits_type::eof();
823 }
824 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
825 {
826 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
827 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
828 return traits_type::eof();
829 if (__r == codecvt_base::partial)
830 {
Marshall Clowbeda7142017-06-14 20:00:36 +0000831 this->setp(const_cast<char_type*>(__e), this->pptr());
Marshall Clow33932622017-09-12 15:00:43 +0000832 this->__pbump(this->epptr() - this->pbase());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833 }
834 }
835 else
836 return traits_type::eof();
837 } while (__r == codecvt_base::partial);
838 }
839 this->setp(__pb_save, __epb_save);
840 }
841 return traits_type::not_eof(__c);
842}
843
844template <class _CharT, class _Traits>
845basic_streambuf<_CharT, _Traits>*
846basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
847{
848 this->setg(0, 0, 0);
849 this->setp(0, 0);
850 if (__owns_eb_)
851 delete [] __extbuf_;
852 if (__owns_ib_)
853 delete [] __intbuf_;
854 __ebs_ = __n;
855 if (__ebs_ > sizeof(__extbuf_min_))
856 {
857 if (__always_noconv_ && __s)
858 {
859 __extbuf_ = (char*)__s;
860 __owns_eb_ = false;
861 }
862 else
863 {
864 __extbuf_ = new char[__ebs_];
865 __owns_eb_ = true;
866 }
867 }
868 else
869 {
870 __extbuf_ = __extbuf_min_;
871 __ebs_ = sizeof(__extbuf_min_);
872 __owns_eb_ = false;
873 }
874 if (!__always_noconv_)
875 {
876 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
877 if (__s && __ibs_ >= sizeof(__extbuf_min_))
878 {
879 __intbuf_ = __s;
880 __owns_ib_ = false;
881 }
882 else
883 {
884 __intbuf_ = new char_type[__ibs_];
885 __owns_ib_ = true;
886 }
887 }
888 else
889 {
890 __ibs_ = 0;
891 __intbuf_ = 0;
892 __owns_ib_ = false;
893 }
894 return this;
895}
896
897template <class _CharT, class _Traits>
898typename basic_filebuf<_CharT, _Traits>::pos_type
899basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
900 ios_base::openmode)
901{
Howard Hinnant0090b122012-08-24 16:52:47 +0000902 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000903 __throw_bad_cast();
904
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 int __width = __cv_->encoding();
906 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
907 return pos_type(off_type(-1));
908 // __width > 0 || __off == 0
909 int __whence;
910 switch (__way)
911 {
912 case ios_base::beg:
913 __whence = SEEK_SET;
914 break;
915 case ios_base::cur:
916 __whence = SEEK_CUR;
917 break;
918 case ios_base::end:
919 __whence = SEEK_END;
920 break;
921 default:
922 return pos_type(off_type(-1));
923 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000924#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000925 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
926 return pos_type(off_type(-1));
927 pos_type __r = ftell(__file_);
928#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
930 return pos_type(off_type(-1));
931 pos_type __r = ftello(__file_);
Howard Hinnant456539a2013-04-02 22:14:51 +0000932#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 __r.state(__st_);
934 return __r;
935}
936
937template <class _CharT, class _Traits>
938typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnantf3aff4f2010-07-15 18:18:07 +0000939basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940{
941 if (__file_ == 0 || sync())
942 return pos_type(off_type(-1));
Shoaib Meenai3879c9b2016-10-31 15:09:10 +0000943#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +0000944 if (fseek(__file_, __sp, SEEK_SET))
945 return pos_type(off_type(-1));
946#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 if (fseeko(__file_, __sp, SEEK_SET))
948 return pos_type(off_type(-1));
Howard Hinnant456539a2013-04-02 22:14:51 +0000949#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000950 __st_ = __sp.state();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 return __sp;
952}
953
954template <class _CharT, class _Traits>
955int
956basic_filebuf<_CharT, _Traits>::sync()
957{
958 if (__file_ == 0)
959 return 0;
Howard Hinnant0090b122012-08-24 16:52:47 +0000960 if (!__cv_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000961 __throw_bad_cast();
962
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 if (__cm_ & ios_base::out)
964 {
965 if (this->pptr() != this->pbase())
966 if (overflow() == traits_type::eof())
967 return -1;
968 codecvt_base::result __r;
969 do
970 {
971 char* __extbe;
972 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
973 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
974 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
975 return -1;
976 } while (__r == codecvt_base::partial);
977 if (__r == codecvt_base::error)
978 return -1;
979 if (fflush(__file_))
980 return -1;
981 }
982 else if (__cm_ & ios_base::in)
983 {
984 off_type __c;
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000985 state_type __state = __st_last_;
986 bool __update_st = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 if (__always_noconv_)
988 __c = this->egptr() - this->gptr();
989 else
990 {
991 int __width = __cv_->encoding();
992 __c = __extbufend_ - __extbufnext_;
993 if (__width > 0)
994 __c += __width * (this->egptr() - this->gptr());
995 else
996 {
997 if (this->gptr() != this->egptr())
998 {
Howard Hinnant2f7c18b2012-08-24 21:20:56 +0000999 const int __off = __cv_->length(__state, __extbuf_,
1000 __extbufnext_,
1001 this->gptr() - this->eback());
1002 __c += __extbufnext_ - __extbuf_ - __off;
1003 __update_st = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 }
1005 }
1006 }
Shoaib Meenai3879c9b2016-10-31 15:09:10 +00001007#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnant456539a2013-04-02 22:14:51 +00001008 if (fseek(__file_, -__c, SEEK_CUR))
1009 return -1;
1010#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 if (fseeko(__file_, -__c, SEEK_CUR))
1012 return -1;
Howard Hinnant456539a2013-04-02 22:14:51 +00001013#endif
Howard Hinnant2f7c18b2012-08-24 21:20:56 +00001014 if (__update_st)
1015 __st_ = __state;
1016 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 this->setg(0, 0, 0);
1018 __cm_ = 0;
1019 }
1020 return 0;
1021}
1022
1023template <class _CharT, class _Traits>
1024void
1025basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1026{
1027 sync();
1028 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
1029 bool __old_anc = __always_noconv_;
1030 __always_noconv_ = __cv_->always_noconv();
1031 if (__old_anc != __always_noconv_)
1032 {
1033 this->setg(0, 0, 0);
1034 this->setp(0, 0);
1035 // invariant, char_type is char, else we couldn't get here
1036 if (__always_noconv_) // need to dump __intbuf_
1037 {
1038 if (__owns_eb_)
1039 delete [] __extbuf_;
1040 __owns_eb_ = __owns_ib_;
1041 __ebs_ = __ibs_;
1042 __extbuf_ = (char*)__intbuf_;
1043 __ibs_ = 0;
1044 __intbuf_ = 0;
1045 __owns_ib_ = false;
1046 }
1047 else // need to obtain an __intbuf_.
1048 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1049 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1050 {
1051 __ibs_ = __ebs_;
1052 __intbuf_ = (char_type*)__extbuf_;
1053 __owns_ib_ = false;
1054 __extbuf_ = new char[__ebs_];
1055 __owns_eb_ = true;
1056 }
1057 else
1058 {
1059 __ibs_ = __ebs_;
1060 __intbuf_ = new char_type[__ibs_];
1061 __owns_ib_ = true;
1062 }
1063 }
1064 }
1065}
1066
1067template <class _CharT, class _Traits>
1068bool
1069basic_filebuf<_CharT, _Traits>::__read_mode()
1070{
1071 if (!(__cm_ & ios_base::in))
1072 {
1073 this->setp(0, 0);
1074 if (__always_noconv_)
1075 this->setg((char_type*)__extbuf_,
1076 (char_type*)__extbuf_ + __ebs_,
1077 (char_type*)__extbuf_ + __ebs_);
1078 else
1079 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1080 __cm_ = ios_base::in;
1081 return true;
1082 }
1083 return false;
1084}
1085
1086template <class _CharT, class _Traits>
1087void
1088basic_filebuf<_CharT, _Traits>::__write_mode()
1089{
1090 if (!(__cm_ & ios_base::out))
1091 {
1092 this->setg(0, 0, 0);
1093 if (__ebs_ > sizeof(__extbuf_min_))
1094 {
1095 if (__always_noconv_)
1096 this->setp((char_type*)__extbuf_,
1097 (char_type*)__extbuf_ + (__ebs_ - 1));
1098 else
1099 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1100 }
1101 else
1102 this->setp(0, 0);
1103 __cm_ = ios_base::out;
1104 }
1105}
1106
1107// basic_ifstream
1108
1109template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001110class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 : public basic_istream<_CharT, _Traits>
1112{
1113public:
1114 typedef _CharT char_type;
1115 typedef _Traits traits_type;
1116 typedef typename traits_type::int_type int_type;
1117 typedef typename traits_type::pos_type pos_type;
1118 typedef typename traits_type::off_type off_type;
1119
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_ifstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001122#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001125#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1126 _LIBCPP_INLINE_VISIBILITY
1127 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1128#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001131#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001132#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 basic_ifstream(basic_ifstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 basic_ifstream& operator=(basic_ifstream&& __rhs);
1138#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 void swap(basic_ifstream& __rhs);
1141
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001146#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 void open(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 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1150#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001152
1153 _LIBCPP_INLINE_VISIBILITY
1154 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001155#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 void close();
1158
1159private:
1160 basic_filebuf<char_type, traits_type> __sb_;
1161};
1162
1163template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001164inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165basic_ifstream<_CharT, _Traits>::basic_ifstream()
1166 : basic_istream<char_type, traits_type>(&__sb_)
1167{
1168}
1169
Ed Schouten7009f4e2015-03-12 15:44:39 +00001170#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001172inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1174 : basic_istream<char_type, traits_type>(&__sb_)
1175{
1176 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1177 this->setstate(ios_base::failbit);
1178}
1179
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001180#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1181template <class _CharT, class _Traits>
1182inline
1183basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1184 : basic_istream<char_type, traits_type>(&__sb_)
1185{
1186 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1187 this->setstate(ios_base::failbit);
1188}
1189#endif
1190
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1194 : basic_istream<char_type, traits_type>(&__sb_)
1195{
1196 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1197 this->setstate(ios_base::failbit);
1198}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200
Eric Fiselier78ccf772017-04-18 23:38:41 +00001201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202
1203template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001204inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001206 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1207 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208{
1209 this->set_rdbuf(&__sb_);
1210}
1211
1212template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214basic_ifstream<_CharT, _Traits>&
1215basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1216{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001217 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1218 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 return *this;
1220}
1221
Eric Fiselier78ccf772017-04-18 23:38:41 +00001222#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223
1224template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001225inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226void
1227basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1228{
1229 basic_istream<char_type, traits_type>::swap(__rhs);
1230 __sb_.swap(__rhs.__sb_);
1231}
1232
1233template <class _CharT, class _Traits>
1234inline _LIBCPP_INLINE_VISIBILITY
1235void
1236swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1237{
1238 __x.swap(__y);
1239}
1240
1241template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001242inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243basic_filebuf<_CharT, _Traits>*
1244basic_ifstream<_CharT, _Traits>::rdbuf() const
1245{
1246 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1247}
1248
1249template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251bool
1252basic_ifstream<_CharT, _Traits>::is_open() const
1253{
1254 return __sb_.is_open();
1255}
1256
Ed Schouten7009f4e2015-03-12 15:44:39 +00001257#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258template <class _CharT, class _Traits>
1259void
1260basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1261{
1262 if (__sb_.open(__s, __mode | ios_base::in))
1263 this->clear();
1264 else
1265 this->setstate(ios_base::failbit);
1266}
1267
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001268#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1269template <class _CharT, class _Traits>
1270void
1271basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1272{
1273 if (__sb_.open(__s, __mode | ios_base::in))
1274 this->clear();
1275 else
1276 this->setstate(ios_base::failbit);
1277}
1278#endif
1279
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280template <class _CharT, class _Traits>
1281void
1282basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1283{
1284 if (__sb_.open(__s, __mode | ios_base::in))
1285 this->clear();
1286 else
1287 this->setstate(ios_base::failbit);
1288}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001289
1290template <class _CharT, class _Traits>
1291void basic_ifstream<_CharT, _Traits>::__open(int __fd,
1292 ios_base::openmode __mode) {
1293 if (__sb_.__open(__fd, __mode | ios_base::in))
1294 this->clear();
1295 else
1296 this->setstate(ios_base::failbit);
1297}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001298#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
1300template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001301inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302void
1303basic_ifstream<_CharT, _Traits>::close()
1304{
1305 if (__sb_.close() == 0)
1306 this->setstate(ios_base::failbit);
1307}
1308
1309// basic_ofstream
1310
1311template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001312class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313 : public basic_ostream<_CharT, _Traits>
1314{
1315public:
1316 typedef _CharT char_type;
1317 typedef _Traits traits_type;
1318 typedef typename traits_type::int_type int_type;
1319 typedef typename traits_type::pos_type pos_type;
1320 typedef typename traits_type::off_type off_type;
1321
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323 basic_ofstream();
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001326#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1327 _LIBCPP_INLINE_VISIBILITY
1328 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1329#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselier78ccf772017-04-18 23:38:41 +00001332#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 basic_ofstream(basic_ofstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001335
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 basic_ofstream& operator=(basic_ofstream&& __rhs);
1338#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 void swap(basic_ofstream& __rhs);
1341
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001346#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001348#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1349 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1350#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001352
1353 _LIBCPP_INLINE_VISIBILITY
1354 void __open(int __fd, ios_base::openmode __mode);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001355#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357 void close();
1358
1359private:
1360 basic_filebuf<char_type, traits_type> __sb_;
1361};
1362
1363template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001364inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365basic_ofstream<_CharT, _Traits>::basic_ofstream()
1366 : basic_ostream<char_type, traits_type>(&__sb_)
1367{
1368}
1369
Ed Schouten7009f4e2015-03-12 15:44:39 +00001370#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001372inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1374 : basic_ostream<char_type, traits_type>(&__sb_)
1375{
1376 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1377 this->setstate(ios_base::failbit);
1378}
1379
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001380#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1381template <class _CharT, class _Traits>
1382inline
1383basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1384 : basic_ostream<char_type, traits_type>(&__sb_)
1385{
1386 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1387 this->setstate(ios_base::failbit);
1388}
1389#endif
1390
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001392inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1394 : basic_ostream<char_type, traits_type>(&__sb_)
1395{
1396 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1397 this->setstate(ios_base::failbit);
1398}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400
Eric Fiselier78ccf772017-04-18 23:38:41 +00001401#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
1403template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001404inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001406 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1407 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408{
1409 this->set_rdbuf(&__sb_);
1410}
1411
1412template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001413inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414basic_ofstream<_CharT, _Traits>&
1415basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1416{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001417 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1418 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419 return *this;
1420}
1421
Eric Fiselier78ccf772017-04-18 23:38:41 +00001422#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423
1424template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001425inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426void
1427basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1428{
1429 basic_ostream<char_type, traits_type>::swap(__rhs);
1430 __sb_.swap(__rhs.__sb_);
1431}
1432
1433template <class _CharT, class _Traits>
1434inline _LIBCPP_INLINE_VISIBILITY
1435void
1436swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1437{
1438 __x.swap(__y);
1439}
1440
1441template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001442inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443basic_filebuf<_CharT, _Traits>*
1444basic_ofstream<_CharT, _Traits>::rdbuf() const
1445{
1446 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1447}
1448
1449template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001450inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451bool
1452basic_ofstream<_CharT, _Traits>::is_open() const
1453{
1454 return __sb_.is_open();
1455}
1456
Ed Schouten7009f4e2015-03-12 15:44:39 +00001457#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458template <class _CharT, class _Traits>
1459void
1460basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1461{
1462 if (__sb_.open(__s, __mode | ios_base::out))
1463 this->clear();
1464 else
1465 this->setstate(ios_base::failbit);
1466}
1467
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001468#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1469template <class _CharT, class _Traits>
1470void
1471basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1472{
1473 if (__sb_.open(__s, __mode | ios_base::out))
1474 this->clear();
1475 else
1476 this->setstate(ios_base::failbit);
1477}
1478#endif
1479
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480template <class _CharT, class _Traits>
1481void
1482basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1483{
1484 if (__sb_.open(__s, __mode | ios_base::out))
1485 this->clear();
1486 else
1487 this->setstate(ios_base::failbit);
1488}
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001489
1490template <class _CharT, class _Traits>
1491void basic_ofstream<_CharT, _Traits>::__open(int __fd,
1492 ios_base::openmode __mode) {
1493 if (__sb_.__open(__fd, __mode | ios_base::out))
1494 this->clear();
1495 else
1496 this->setstate(ios_base::failbit);
1497}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001498#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
1500template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001501inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502void
1503basic_ofstream<_CharT, _Traits>::close()
1504{
1505 if (__sb_.close() == 0)
1506 this->setstate(ios_base::failbit);
1507}
1508
1509// basic_fstream
1510
1511template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001512class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513 : public basic_iostream<_CharT, _Traits>
1514{
1515public:
1516 typedef _CharT char_type;
1517 typedef _Traits traits_type;
1518 typedef typename traits_type::int_type int_type;
1519 typedef typename traits_type::pos_type pos_type;
1520 typedef typename traits_type::off_type off_type;
1521
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 basic_fstream();
Ed Schouten7009f4e2015-03-12 15:44:39 +00001524#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001527#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1528 _LIBCPP_INLINE_VISIBILITY
1529 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1530#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001533#endif
Eric Fiselier78ccf772017-04-18 23:38:41 +00001534#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 basic_fstream(basic_fstream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 basic_fstream& operator=(basic_fstream&& __rhs);
1540#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 void swap(basic_fstream& __rhs);
1543
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 bool is_open() const;
Ed Schouten7009f4e2015-03-12 15:44:39 +00001548#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001550#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1551 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1552#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schouten7009f4e2015-03-12 15:44:39 +00001554#endif
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 void close();
1557
1558private:
1559 basic_filebuf<char_type, traits_type> __sb_;
1560};
1561
1562template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001563inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564basic_fstream<_CharT, _Traits>::basic_fstream()
1565 : basic_iostream<char_type, traits_type>(&__sb_)
1566{
1567}
1568
Ed Schouten7009f4e2015-03-12 15:44:39 +00001569#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001571inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1573 : basic_iostream<char_type, traits_type>(&__sb_)
1574{
1575 if (__sb_.open(__s, __mode) == 0)
1576 this->setstate(ios_base::failbit);
1577}
1578
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001579#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1580template <class _CharT, class _Traits>
1581inline
1582basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1583 : basic_iostream<char_type, traits_type>(&__sb_)
1584{
1585 if (__sb_.open(__s, __mode) == 0)
1586 this->setstate(ios_base::failbit);
1587}
1588#endif
1589
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001591inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1593 : basic_iostream<char_type, traits_type>(&__sb_)
1594{
1595 if (__sb_.open(__s, __mode) == 0)
1596 this->setstate(ios_base::failbit);
1597}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001598#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599
Eric Fiselier78ccf772017-04-18 23:38:41 +00001600#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601
1602template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001603inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001605 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1606 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607{
1608 this->set_rdbuf(&__sb_);
1609}
1610
1611template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001612inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613basic_fstream<_CharT, _Traits>&
1614basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1615{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001616 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1617 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 return *this;
1619}
1620
Eric Fiselier78ccf772017-04-18 23:38:41 +00001621#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622
1623template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001624inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625void
1626basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1627{
1628 basic_iostream<char_type, traits_type>::swap(__rhs);
1629 __sb_.swap(__rhs.__sb_);
1630}
1631
1632template <class _CharT, class _Traits>
1633inline _LIBCPP_INLINE_VISIBILITY
1634void
1635swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1636{
1637 __x.swap(__y);
1638}
1639
1640template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001641inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642basic_filebuf<_CharT, _Traits>*
1643basic_fstream<_CharT, _Traits>::rdbuf() const
1644{
1645 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1646}
1647
1648template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650bool
1651basic_fstream<_CharT, _Traits>::is_open() const
1652{
1653 return __sb_.is_open();
1654}
1655
Ed Schouten7009f4e2015-03-12 15:44:39 +00001656#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657template <class _CharT, class _Traits>
1658void
1659basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1660{
1661 if (__sb_.open(__s, __mode))
1662 this->clear();
1663 else
1664 this->setstate(ios_base::failbit);
1665}
1666
Peter Collingbourne63ebbd72018-01-23 02:07:27 +00001667#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1668template <class _CharT, class _Traits>
1669void
1670basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode)
1671{
1672 if (__sb_.open(__s, __mode))
1673 this->clear();
1674 else
1675 this->setstate(ios_base::failbit);
1676}
1677#endif
1678
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679template <class _CharT, class _Traits>
1680void
1681basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1682{
1683 if (__sb_.open(__s, __mode))
1684 this->clear();
1685 else
1686 this->setstate(ios_base::failbit);
1687}
Ed Schouten7009f4e2015-03-12 15:44:39 +00001688#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689
1690template <class _CharT, class _Traits>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +00001691inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692void
1693basic_fstream<_CharT, _Traits>::close()
1694{
1695 if (__sb_.close() == 0)
1696 this->setstate(ios_base::failbit);
1697}
1698
1699_LIBCPP_END_NAMESPACE_STD
1700
Eric Fiselierf4433a32017-05-31 22:07:49 +00001701_LIBCPP_POP_MACROS
1702
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703#endif // _LIBCPP_FSTREAM