blob: 7bcc5d450daf46301463eb0e172da7c657859afd [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------- fstream ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant324bb032010-08-22 00:02:43 +0000125template <class charT, class traits>
Howard Hinnantbc8d3f92010-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>
173
Howard Hinnant66c6f972011-11-29 16:45:27 +0000174#include <__undef_min_max>
175
Howard Hinnant08e17472011-10-17 20:05:10 +0000176#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000178#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179
180_LIBCPP_BEGIN_NAMESPACE_STD
181
182template <class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000183class _LIBCPP_TEMPLATE_VIS basic_filebuf
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 : public basic_streambuf<_CharT, _Traits>
185{
186public:
187 typedef _CharT char_type;
188 typedef _Traits traits_type;
189 typedef typename traits_type::int_type int_type;
190 typedef typename traits_type::pos_type pos_type;
191 typedef typename traits_type::off_type off_type;
192 typedef typename traits_type::state_type state_type;
193
194 // 27.9.1.2 Constructors/destructor:
195 basic_filebuf();
Howard Hinnant73d21a42010-09-04 23:28:19 +0000196#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197 basic_filebuf(basic_filebuf&& __rhs);
198#endif
199 virtual ~basic_filebuf();
200
201 // 27.9.1.3 Assign/swap:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204 basic_filebuf& operator=(basic_filebuf&& __rhs);
205#endif
206 void swap(basic_filebuf& __rhs);
207
208 // 27.9.1.4 Members:
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000211#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216 basic_filebuf* close();
217
218protected:
219 // 27.9.1.5 Overridden virtual functions:
220 virtual int_type underflow();
221 virtual int_type pbackfail(int_type __c = traits_type::eof());
222 virtual int_type overflow (int_type __c = traits_type::eof());
223 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
224 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
225 ios_base::openmode __wch = ios_base::in | ios_base::out);
226 virtual pos_type seekpos(pos_type __sp,
227 ios_base::openmode __wch = ios_base::in | ios_base::out);
228 virtual int sync();
229 virtual void imbue(const locale& __loc);
230
231private:
232 char* __extbuf_;
233 const char* __extbufnext_;
234 const char* __extbufend_;
235 char __extbuf_min_[8];
236 size_t __ebs_;
237 char_type* __intbuf_;
238 size_t __ibs_;
239 FILE* __file_;
240 const codecvt<char_type, char, state_type>* __cv_;
241 state_type __st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000242 state_type __st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 ios_base::openmode __om_;
244 ios_base::openmode __cm_;
245 bool __owns_eb_;
246 bool __owns_ib_;
247 bool __always_noconv_;
248
249 bool __read_mode();
250 void __write_mode();
251};
252
253template <class _CharT, class _Traits>
254basic_filebuf<_CharT, _Traits>::basic_filebuf()
255 : __extbuf_(0),
256 __extbufnext_(0),
257 __extbufend_(0),
258 __ebs_(0),
259 __intbuf_(0),
260 __ibs_(0),
261 __file_(0),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000262 __cv_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263 __st_(),
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000264 __st_last_(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265 __om_(0),
266 __cm_(0),
267 __owns_eb_(false),
268 __owns_ib_(false),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000269 __always_noconv_(false)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000271 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
272 {
273 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
274 __always_noconv_ = __cv_->always_noconv();
275 }
Howard Hinnante7d59f22012-08-24 18:06:47 +0000276 setbuf(0, 4096);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277}
278
Howard Hinnant73d21a42010-09-04 23:28:19 +0000279#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280
281template <class _CharT, class _Traits>
282basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
283 : basic_streambuf<_CharT, _Traits>(__rhs)
284{
285 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
286 {
287 __extbuf_ = __extbuf_min_;
288 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
289 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
290 }
291 else
292 {
293 __extbuf_ = __rhs.__extbuf_;
294 __extbufnext_ = __rhs.__extbufnext_;
295 __extbufend_ = __rhs.__extbufend_;
296 }
297 __ebs_ = __rhs.__ebs_;
298 __intbuf_ = __rhs.__intbuf_;
299 __ibs_ = __rhs.__ibs_;
300 __file_ = __rhs.__file_;
301 __cv_ = __rhs.__cv_;
302 __st_ = __rhs.__st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000303 __st_last_ = __rhs.__st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304 __om_ = __rhs.__om_;
305 __cm_ = __rhs.__cm_;
306 __owns_eb_ = __rhs.__owns_eb_;
307 __owns_ib_ = __rhs.__owns_ib_;
308 __always_noconv_ = __rhs.__always_noconv_;
309 if (__rhs.pbase())
310 {
311 if (__rhs.pbase() == __rhs.__intbuf_)
312 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
313 else
314 this->setp((char_type*)__extbuf_,
315 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
316 this->pbump(__rhs. pptr() - __rhs.pbase());
317 }
318 else if (__rhs.eback())
319 {
320 if (__rhs.eback() == __rhs.__intbuf_)
321 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
322 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
323 else
324 this->setg((char_type*)__extbuf_,
325 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
326 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
327 }
328 __rhs.__extbuf_ = 0;
329 __rhs.__extbufnext_ = 0;
330 __rhs.__extbufend_ = 0;
331 __rhs.__ebs_ = 0;
332 __rhs.__intbuf_ = 0;
333 __rhs.__ibs_ = 0;
334 __rhs.__file_ = 0;
335 __rhs.__st_ = state_type();
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000336 __rhs.__st_last_ = state_type();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 __rhs.__om_ = 0;
338 __rhs.__cm_ = 0;
339 __rhs.__owns_eb_ = false;
340 __rhs.__owns_ib_ = false;
341 __rhs.setg(0, 0, 0);
342 __rhs.setp(0, 0);
343}
344
345template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000346inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347basic_filebuf<_CharT, _Traits>&
348basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
349{
350 close();
351 swap(__rhs);
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +0000352 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353}
354
Howard Hinnant73d21a42010-09-04 23:28:19 +0000355#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356
357template <class _CharT, class _Traits>
358basic_filebuf<_CharT, _Traits>::~basic_filebuf()
359{
360#ifndef _LIBCPP_NO_EXCEPTIONS
361 try
362 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000363#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364 close();
365#ifndef _LIBCPP_NO_EXCEPTIONS
366 }
367 catch (...)
368 {
369 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000370#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 if (__owns_eb_)
372 delete [] __extbuf_;
373 if (__owns_ib_)
374 delete [] __intbuf_;
375}
376
377template <class _CharT, class _Traits>
378void
379basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
380{
381 basic_streambuf<char_type, traits_type>::swap(__rhs);
382 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
383 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000384 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
385 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
386 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 }
388 else
389 {
390 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
391 ptrdiff_t __le = __extbufend_ - __extbuf_;
392 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
393 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
394 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
395 {
396 __extbuf_ = __rhs.__extbuf_;
397 __rhs.__extbuf_ = __rhs.__extbuf_min_;
398 }
399 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
400 {
401 __rhs.__extbuf_ = __extbuf_;
402 __extbuf_ = __extbuf_min_;
403 }
404 __extbufnext_ = __extbuf_ + __rn;
405 __extbufend_ = __extbuf_ + __re;
406 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
407 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
408 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000409 _VSTD::swap(__ebs_, __rhs.__ebs_);
410 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
411 _VSTD::swap(__ibs_, __rhs.__ibs_);
412 _VSTD::swap(__file_, __rhs.__file_);
413 _VSTD::swap(__cv_, __rhs.__cv_);
414 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000415 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000416 _VSTD::swap(__om_, __rhs.__om_);
417 _VSTD::swap(__cm_, __rhs.__cm_);
418 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
419 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
420 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
422 {
423 ptrdiff_t __n = this->gptr() - this->eback();
424 ptrdiff_t __e = this->egptr() - this->eback();
425 this->setg((char_type*)__extbuf_min_,
426 (char_type*)__extbuf_min_ + __n,
427 (char_type*)__extbuf_min_ + __e);
428 }
429 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
430 {
431 ptrdiff_t __n = this->pptr() - this->pbase();
432 ptrdiff_t __e = this->epptr() - this->pbase();
433 this->setp((char_type*)__extbuf_min_,
434 (char_type*)__extbuf_min_ + __e);
435 this->pbump(__n);
436 }
437 if (__rhs.eback() == (char_type*)__extbuf_min_)
438 {
439 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
440 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
441 __rhs.setg((char_type*)__rhs.__extbuf_min_,
442 (char_type*)__rhs.__extbuf_min_ + __n,
443 (char_type*)__rhs.__extbuf_min_ + __e);
444 }
445 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
446 {
447 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
448 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
449 __rhs.setp((char_type*)__rhs.__extbuf_min_,
450 (char_type*)__rhs.__extbuf_min_ + __e);
451 __rhs.pbump(__n);
452 }
453}
454
455template <class _CharT, class _Traits>
456inline _LIBCPP_INLINE_VISIBILITY
457void
458swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
459{
460 __x.swap(__y);
461}
462
463template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000464inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465bool
466basic_filebuf<_CharT, _Traits>::is_open() const
467{
468 return __file_ != 0;
469}
470
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000471#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472template <class _CharT, class _Traits>
473basic_filebuf<_CharT, _Traits>*
474basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
475{
476 basic_filebuf<_CharT, _Traits>* __rt = 0;
477 if (__file_ == 0)
478 {
479 __rt = this;
480 const char* __mdstr;
481 switch (__mode & ~ios_base::ate)
482 {
483 case ios_base::out:
484 case ios_base::out | ios_base::trunc:
485 __mdstr = "w";
486 break;
487 case ios_base::out | ios_base::app:
488 case ios_base::app:
489 __mdstr = "a";
490 break;
491 case ios_base::in:
492 __mdstr = "r";
493 break;
494 case ios_base::in | ios_base::out:
495 __mdstr = "r+";
496 break;
497 case ios_base::in | ios_base::out | ios_base::trunc:
498 __mdstr = "w+";
499 break;
500 case ios_base::in | ios_base::out | ios_base::app:
501 case ios_base::in | ios_base::app:
502 __mdstr = "a+";
503 break;
504 case ios_base::out | ios_base::binary:
505 case ios_base::out | ios_base::trunc | ios_base::binary:
506 __mdstr = "wb";
507 break;
508 case ios_base::out | ios_base::app | ios_base::binary:
509 case ios_base::app | ios_base::binary:
510 __mdstr = "ab";
511 break;
512 case ios_base::in | ios_base::binary:
513 __mdstr = "rb";
514 break;
515 case ios_base::in | ios_base::out | ios_base::binary:
516 __mdstr = "r+b";
517 break;
518 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
519 __mdstr = "w+b";
520 break;
521 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
522 case ios_base::in | ios_base::app | ios_base::binary:
523 __mdstr = "a+b";
524 break;
525 default:
526 __rt = 0;
527 break;
528 }
529 if (__rt)
530 {
531 __file_ = fopen(__s, __mdstr);
532 if (__file_)
533 {
534 __om_ = __mode;
535 if (__mode & ios_base::ate)
536 {
537 if (fseek(__file_, 0, SEEK_END))
538 {
539 fclose(__file_);
540 __file_ = 0;
541 __rt = 0;
542 }
543 }
544 }
545 else
546 __rt = 0;
547 }
548 }
549 return __rt;
550}
551
552template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000553inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554basic_filebuf<_CharT, _Traits>*
555basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
556{
557 return open(__s.c_str(), __mode);
558}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000559#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
561template <class _CharT, class _Traits>
562basic_filebuf<_CharT, _Traits>*
563basic_filebuf<_CharT, _Traits>::close()
564{
565 basic_filebuf<_CharT, _Traits>* __rt = 0;
566 if (__file_)
567 {
568 __rt = this;
569 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnante1a7b042012-01-12 23:37:51 +0000570 if (sync())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 __rt = 0;
572 if (fclose(__h.release()) == 0)
573 __file_ = 0;
574 else
575 __rt = 0;
576 }
577 return __rt;
578}
579
580template <class _CharT, class _Traits>
581typename basic_filebuf<_CharT, _Traits>::int_type
582basic_filebuf<_CharT, _Traits>::underflow()
583{
584 if (__file_ == 0)
585 return traits_type::eof();
586 bool __initial = __read_mode();
587 char_type __1buf;
588 if (this->gptr() == 0)
589 this->setg(&__1buf, &__1buf+1, &__1buf+1);
590 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
591 int_type __c = traits_type::eof();
592 if (this->gptr() == this->egptr())
593 {
594 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
595 if (__always_noconv_)
596 {
597 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
598 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
599 if (__nmemb != 0)
600 {
601 this->setg(this->eback(),
602 this->eback() + __unget_sz,
603 this->eback() + __unget_sz + __nmemb);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000604 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 }
606 }
607 else
608 {
Marshall Clow27006192016-06-04 16:16:59 +0000609 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
610 if (__extbufend_ != __extbufnext_)
611 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
613 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantec423cb2012-08-24 20:37:00 +0000614 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615 static_cast<size_t>(__extbufend_ - __extbufnext_));
616 codecvt_base::result __r;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000617 __st_last_ = __st_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
619 if (__nr != 0)
620 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000621 if (!__cv_)
Marshall Clow14c09a22016-08-25 15:09:01 +0000622 __throw_bad_cast();
623
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624 __extbufend_ = __extbufnext_ + __nr;
625 char_type* __inext;
626 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
627 this->eback() + __unget_sz,
Howard Hinnantec423cb2012-08-24 20:37:00 +0000628 this->eback() + __ibs_, __inext);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629 if (__r == codecvt_base::noconv)
630 {
631 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000632 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 }
634 else if (__inext != this->eback() + __unget_sz)
635 {
636 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000637 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 }
639 }
640 }
641 }
642 else
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000643 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 if (this->eback() == &__1buf)
645 this->setg(0, 0, 0);
646 return __c;
647}
648
649template <class _CharT, class _Traits>
650typename basic_filebuf<_CharT, _Traits>::int_type
651basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
652{
653 if (__file_ && this->eback() < this->gptr())
654 {
655 if (traits_type::eq_int_type(__c, traits_type::eof()))
656 {
657 this->gbump(-1);
658 return traits_type::not_eof(__c);
659 }
660 if ((__om_ & ios_base::out) ||
661 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
662 {
663 this->gbump(-1);
664 *this->gptr() = traits_type::to_char_type(__c);
665 return __c;
666 }
667 }
668 return traits_type::eof();
669}
670
671template <class _CharT, class _Traits>
672typename basic_filebuf<_CharT, _Traits>::int_type
673basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
674{
675 if (__file_ == 0)
676 return traits_type::eof();
677 __write_mode();
678 char_type __1buf;
679 char_type* __pb_save = this->pbase();
680 char_type* __epb_save = this->epptr();
681 if (!traits_type::eq_int_type(__c, traits_type::eof()))
682 {
683 if (this->pptr() == 0)
684 this->setp(&__1buf, &__1buf+1);
685 *this->pptr() = traits_type::to_char_type(__c);
686 this->pbump(1);
687 }
688 if (this->pptr() != this->pbase())
689 {
690 if (__always_noconv_)
691 {
692 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
693 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
694 return traits_type::eof();
695 }
696 else
697 {
698 char* __extbe = __extbuf_;
699 codecvt_base::result __r;
700 do
701 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000702 if (!__cv_)
Marshall Clow14c09a22016-08-25 15:09:01 +0000703 __throw_bad_cast();
704
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 const char_type* __e;
706 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
707 __extbuf_, __extbuf_ + __ebs_, __extbe);
708 if (__e == this->pbase())
709 return traits_type::eof();
710 if (__r == codecvt_base::noconv)
711 {
712 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
713 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
714 return traits_type::eof();
715 }
716 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
717 {
718 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
719 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
720 return traits_type::eof();
721 if (__r == codecvt_base::partial)
722 {
723 this->setp((char_type*)__e, this->pptr());
724 this->pbump(this->epptr() - this->pbase());
725 }
726 }
727 else
728 return traits_type::eof();
729 } while (__r == codecvt_base::partial);
730 }
731 this->setp(__pb_save, __epb_save);
732 }
733 return traits_type::not_eof(__c);
734}
735
736template <class _CharT, class _Traits>
737basic_streambuf<_CharT, _Traits>*
738basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
739{
740 this->setg(0, 0, 0);
741 this->setp(0, 0);
742 if (__owns_eb_)
743 delete [] __extbuf_;
744 if (__owns_ib_)
745 delete [] __intbuf_;
746 __ebs_ = __n;
747 if (__ebs_ > sizeof(__extbuf_min_))
748 {
749 if (__always_noconv_ && __s)
750 {
751 __extbuf_ = (char*)__s;
752 __owns_eb_ = false;
753 }
754 else
755 {
756 __extbuf_ = new char[__ebs_];
757 __owns_eb_ = true;
758 }
759 }
760 else
761 {
762 __extbuf_ = __extbuf_min_;
763 __ebs_ = sizeof(__extbuf_min_);
764 __owns_eb_ = false;
765 }
766 if (!__always_noconv_)
767 {
768 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
769 if (__s && __ibs_ >= sizeof(__extbuf_min_))
770 {
771 __intbuf_ = __s;
772 __owns_ib_ = false;
773 }
774 else
775 {
776 __intbuf_ = new char_type[__ibs_];
777 __owns_ib_ = true;
778 }
779 }
780 else
781 {
782 __ibs_ = 0;
783 __intbuf_ = 0;
784 __owns_ib_ = false;
785 }
786 return this;
787}
788
789template <class _CharT, class _Traits>
790typename basic_filebuf<_CharT, _Traits>::pos_type
791basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
792 ios_base::openmode)
793{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000794 if (!__cv_)
Marshall Clow14c09a22016-08-25 15:09:01 +0000795 __throw_bad_cast();
796
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797 int __width = __cv_->encoding();
798 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
799 return pos_type(off_type(-1));
800 // __width > 0 || __off == 0
801 int __whence;
802 switch (__way)
803 {
804 case ios_base::beg:
805 __whence = SEEK_SET;
806 break;
807 case ios_base::cur:
808 __whence = SEEK_CUR;
809 break;
810 case ios_base::end:
811 __whence = SEEK_END;
812 break;
813 default:
814 return pos_type(off_type(-1));
815 }
Shoaib Meenai3b2cf962016-10-31 15:09:10 +0000816#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000817 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
818 return pos_type(off_type(-1));
819 pos_type __r = ftell(__file_);
820#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
822 return pos_type(off_type(-1));
823 pos_type __r = ftello(__file_);
Howard Hinnantcf31d382013-04-02 22:14:51 +0000824#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 __r.state(__st_);
826 return __r;
827}
828
829template <class _CharT, class _Traits>
830typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnant639a6682010-07-15 18:18:07 +0000831basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832{
833 if (__file_ == 0 || sync())
834 return pos_type(off_type(-1));
Shoaib Meenai3b2cf962016-10-31 15:09:10 +0000835#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000836 if (fseek(__file_, __sp, SEEK_SET))
837 return pos_type(off_type(-1));
838#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839 if (fseeko(__file_, __sp, SEEK_SET))
840 return pos_type(off_type(-1));
Howard Hinnantcf31d382013-04-02 22:14:51 +0000841#endif
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000842 __st_ = __sp.state();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843 return __sp;
844}
845
846template <class _CharT, class _Traits>
847int
848basic_filebuf<_CharT, _Traits>::sync()
849{
850 if (__file_ == 0)
851 return 0;
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000852 if (!__cv_)
Marshall Clow14c09a22016-08-25 15:09:01 +0000853 __throw_bad_cast();
854
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855 if (__cm_ & ios_base::out)
856 {
857 if (this->pptr() != this->pbase())
858 if (overflow() == traits_type::eof())
859 return -1;
860 codecvt_base::result __r;
861 do
862 {
863 char* __extbe;
864 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
865 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
866 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
867 return -1;
868 } while (__r == codecvt_base::partial);
869 if (__r == codecvt_base::error)
870 return -1;
871 if (fflush(__file_))
872 return -1;
873 }
874 else if (__cm_ & ios_base::in)
875 {
876 off_type __c;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000877 state_type __state = __st_last_;
878 bool __update_st = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 if (__always_noconv_)
880 __c = this->egptr() - this->gptr();
881 else
882 {
883 int __width = __cv_->encoding();
884 __c = __extbufend_ - __extbufnext_;
885 if (__width > 0)
886 __c += __width * (this->egptr() - this->gptr());
887 else
888 {
889 if (this->gptr() != this->egptr())
890 {
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000891 const int __off = __cv_->length(__state, __extbuf_,
892 __extbufnext_,
893 this->gptr() - this->eback());
894 __c += __extbufnext_ - __extbuf_ - __off;
895 __update_st = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 }
897 }
898 }
Shoaib Meenai3b2cf962016-10-31 15:09:10 +0000899#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000900 if (fseek(__file_, -__c, SEEK_CUR))
901 return -1;
902#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903 if (fseeko(__file_, -__c, SEEK_CUR))
904 return -1;
Howard Hinnantcf31d382013-04-02 22:14:51 +0000905#endif
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000906 if (__update_st)
907 __st_ = __state;
908 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 this->setg(0, 0, 0);
910 __cm_ = 0;
911 }
912 return 0;
913}
914
915template <class _CharT, class _Traits>
916void
917basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
918{
919 sync();
920 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
921 bool __old_anc = __always_noconv_;
922 __always_noconv_ = __cv_->always_noconv();
923 if (__old_anc != __always_noconv_)
924 {
925 this->setg(0, 0, 0);
926 this->setp(0, 0);
927 // invariant, char_type is char, else we couldn't get here
928 if (__always_noconv_) // need to dump __intbuf_
929 {
930 if (__owns_eb_)
931 delete [] __extbuf_;
932 __owns_eb_ = __owns_ib_;
933 __ebs_ = __ibs_;
934 __extbuf_ = (char*)__intbuf_;
935 __ibs_ = 0;
936 __intbuf_ = 0;
937 __owns_ib_ = false;
938 }
939 else // need to obtain an __intbuf_.
940 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
941 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
942 {
943 __ibs_ = __ebs_;
944 __intbuf_ = (char_type*)__extbuf_;
945 __owns_ib_ = false;
946 __extbuf_ = new char[__ebs_];
947 __owns_eb_ = true;
948 }
949 else
950 {
951 __ibs_ = __ebs_;
952 __intbuf_ = new char_type[__ibs_];
953 __owns_ib_ = true;
954 }
955 }
956 }
957}
958
959template <class _CharT, class _Traits>
960bool
961basic_filebuf<_CharT, _Traits>::__read_mode()
962{
963 if (!(__cm_ & ios_base::in))
964 {
965 this->setp(0, 0);
966 if (__always_noconv_)
967 this->setg((char_type*)__extbuf_,
968 (char_type*)__extbuf_ + __ebs_,
969 (char_type*)__extbuf_ + __ebs_);
970 else
971 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
972 __cm_ = ios_base::in;
973 return true;
974 }
975 return false;
976}
977
978template <class _CharT, class _Traits>
979void
980basic_filebuf<_CharT, _Traits>::__write_mode()
981{
982 if (!(__cm_ & ios_base::out))
983 {
984 this->setg(0, 0, 0);
985 if (__ebs_ > sizeof(__extbuf_min_))
986 {
987 if (__always_noconv_)
988 this->setp((char_type*)__extbuf_,
989 (char_type*)__extbuf_ + (__ebs_ - 1));
990 else
991 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
992 }
993 else
994 this->setp(0, 0);
995 __cm_ = ios_base::out;
996 }
997}
998
999// basic_ifstream
1000
1001template <class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001002class _LIBCPP_TEMPLATE_VIS basic_ifstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 : public basic_istream<_CharT, _Traits>
1004{
1005public:
1006 typedef _CharT char_type;
1007 typedef _Traits traits_type;
1008 typedef typename traits_type::int_type int_type;
1009 typedef typename traits_type::pos_type pos_type;
1010 typedef typename traits_type::off_type off_type;
1011
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 basic_ifstream();
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001014#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001019#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00001020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 basic_ifstream(basic_ifstream&& __rhs);
1023#endif
1024
Howard Hinnant73d21a42010-09-04 23:28:19 +00001025#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027 basic_ifstream& operator=(basic_ifstream&& __rhs);
1028#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 void swap(basic_ifstream& __rhs);
1031
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001036#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1038 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001039#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 void close();
1042
1043private:
1044 basic_filebuf<char_type, traits_type> __sb_;
1045};
1046
1047template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001048inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049basic_ifstream<_CharT, _Traits>::basic_ifstream()
1050 : basic_istream<char_type, traits_type>(&__sb_)
1051{
1052}
1053
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001054#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001056inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1058 : basic_istream<char_type, traits_type>(&__sb_)
1059{
1060 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1061 this->setstate(ios_base::failbit);
1062}
1063
1064template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001065inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1067 : basic_istream<char_type, traits_type>(&__sb_)
1068{
1069 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1070 this->setstate(ios_base::failbit);
1071}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001072#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073
Howard Hinnant73d21a42010-09-04 23:28:19 +00001074#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075
1076template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001077inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001079 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1080 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081{
1082 this->set_rdbuf(&__sb_);
1083}
1084
1085template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001086inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087basic_ifstream<_CharT, _Traits>&
1088basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1089{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001090 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1091 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 return *this;
1093}
1094
Howard Hinnant73d21a42010-09-04 23:28:19 +00001095#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096
1097template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001098inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099void
1100basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1101{
1102 basic_istream<char_type, traits_type>::swap(__rhs);
1103 __sb_.swap(__rhs.__sb_);
1104}
1105
1106template <class _CharT, class _Traits>
1107inline _LIBCPP_INLINE_VISIBILITY
1108void
1109swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1110{
1111 __x.swap(__y);
1112}
1113
1114template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001115inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116basic_filebuf<_CharT, _Traits>*
1117basic_ifstream<_CharT, _Traits>::rdbuf() const
1118{
1119 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1120}
1121
1122template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001123inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124bool
1125basic_ifstream<_CharT, _Traits>::is_open() const
1126{
1127 return __sb_.is_open();
1128}
1129
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001130#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131template <class _CharT, class _Traits>
1132void
1133basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1134{
1135 if (__sb_.open(__s, __mode | ios_base::in))
1136 this->clear();
1137 else
1138 this->setstate(ios_base::failbit);
1139}
1140
1141template <class _CharT, class _Traits>
1142void
1143basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1144{
1145 if (__sb_.open(__s, __mode | ios_base::in))
1146 this->clear();
1147 else
1148 this->setstate(ios_base::failbit);
1149}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001150#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151
1152template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001153inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154void
1155basic_ifstream<_CharT, _Traits>::close()
1156{
1157 if (__sb_.close() == 0)
1158 this->setstate(ios_base::failbit);
1159}
1160
1161// basic_ofstream
1162
1163template <class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001164class _LIBCPP_TEMPLATE_VIS basic_ofstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165 : public basic_ostream<_CharT, _Traits>
1166{
1167public:
1168 typedef _CharT char_type;
1169 typedef _Traits traits_type;
1170 typedef typename traits_type::int_type int_type;
1171 typedef typename traits_type::pos_type pos_type;
1172 typedef typename traits_type::off_type off_type;
1173
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 basic_ofstream();
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001180#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 basic_ofstream(basic_ofstream&& __rhs);
1183#endif
1184
Howard Hinnant73d21a42010-09-04 23:28:19 +00001185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187 basic_ofstream& operator=(basic_ofstream&& __rhs);
1188#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190 void swap(basic_ofstream& __rhs);
1191
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001196#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1198 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001199#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201 void close();
1202
1203private:
1204 basic_filebuf<char_type, traits_type> __sb_;
1205};
1206
1207template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001208inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209basic_ofstream<_CharT, _Traits>::basic_ofstream()
1210 : basic_ostream<char_type, traits_type>(&__sb_)
1211{
1212}
1213
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001214#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001216inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1218 : basic_ostream<char_type, traits_type>(&__sb_)
1219{
1220 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1221 this->setstate(ios_base::failbit);
1222}
1223
1224template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001225inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1227 : basic_ostream<char_type, traits_type>(&__sb_)
1228{
1229 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1230 this->setstate(ios_base::failbit);
1231}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001232#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233
Howard Hinnant73d21a42010-09-04 23:28:19 +00001234#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235
1236template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001237inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001239 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1240 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241{
1242 this->set_rdbuf(&__sb_);
1243}
1244
1245template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001246inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247basic_ofstream<_CharT, _Traits>&
1248basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1249{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001250 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1251 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 return *this;
1253}
1254
Howard Hinnant73d21a42010-09-04 23:28:19 +00001255#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256
1257template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001258inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259void
1260basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1261{
1262 basic_ostream<char_type, traits_type>::swap(__rhs);
1263 __sb_.swap(__rhs.__sb_);
1264}
1265
1266template <class _CharT, class _Traits>
1267inline _LIBCPP_INLINE_VISIBILITY
1268void
1269swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1270{
1271 __x.swap(__y);
1272}
1273
1274template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001275inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276basic_filebuf<_CharT, _Traits>*
1277basic_ofstream<_CharT, _Traits>::rdbuf() const
1278{
1279 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1280}
1281
1282template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001283inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284bool
1285basic_ofstream<_CharT, _Traits>::is_open() const
1286{
1287 return __sb_.is_open();
1288}
1289
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001290#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291template <class _CharT, class _Traits>
1292void
1293basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1294{
1295 if (__sb_.open(__s, __mode | ios_base::out))
1296 this->clear();
1297 else
1298 this->setstate(ios_base::failbit);
1299}
1300
1301template <class _CharT, class _Traits>
1302void
1303basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1304{
1305 if (__sb_.open(__s, __mode | ios_base::out))
1306 this->clear();
1307 else
1308 this->setstate(ios_base::failbit);
1309}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001310#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311
1312template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001313inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314void
1315basic_ofstream<_CharT, _Traits>::close()
1316{
1317 if (__sb_.close() == 0)
1318 this->setstate(ios_base::failbit);
1319}
1320
1321// basic_fstream
1322
1323template <class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +00001324class _LIBCPP_TEMPLATE_VIS basic_fstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 : public basic_iostream<_CharT, _Traits>
1326{
1327public:
1328 typedef _CharT char_type;
1329 typedef _Traits traits_type;
1330 typedef typename traits_type::int_type int_type;
1331 typedef typename traits_type::pos_type pos_type;
1332 typedef typename traits_type::off_type off_type;
1333
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 basic_fstream();
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001336#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001341#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00001342#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 basic_fstream(basic_fstream&& __rhs);
1345#endif
1346
Howard Hinnant73d21a42010-09-04 23:28:19 +00001347#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 basic_fstream& operator=(basic_fstream&& __rhs);
1350#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 void swap(basic_fstream& __rhs);
1353
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 basic_filebuf<char_type, traits_type>* rdbuf() const;
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001358#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1360 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001361#endif
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 void close();
1364
1365private:
1366 basic_filebuf<char_type, traits_type> __sb_;
1367};
1368
1369template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001370inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371basic_fstream<_CharT, _Traits>::basic_fstream()
1372 : basic_iostream<char_type, traits_type>(&__sb_)
1373{
1374}
1375
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001376#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001378inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1380 : basic_iostream<char_type, traits_type>(&__sb_)
1381{
1382 if (__sb_.open(__s, __mode) == 0)
1383 this->setstate(ios_base::failbit);
1384}
1385
1386template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001387inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1389 : basic_iostream<char_type, traits_type>(&__sb_)
1390{
1391 if (__sb_.open(__s, __mode) == 0)
1392 this->setstate(ios_base::failbit);
1393}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001394#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395
Howard Hinnant73d21a42010-09-04 23:28:19 +00001396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397
1398template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001399inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001401 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1402 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403{
1404 this->set_rdbuf(&__sb_);
1405}
1406
1407template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001408inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409basic_fstream<_CharT, _Traits>&
1410basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1411{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001412 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1413 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 return *this;
1415}
1416
Howard Hinnant73d21a42010-09-04 23:28:19 +00001417#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418
1419template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001420inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421void
1422basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1423{
1424 basic_iostream<char_type, traits_type>::swap(__rhs);
1425 __sb_.swap(__rhs.__sb_);
1426}
1427
1428template <class _CharT, class _Traits>
1429inline _LIBCPP_INLINE_VISIBILITY
1430void
1431swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1432{
1433 __x.swap(__y);
1434}
1435
1436template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001437inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438basic_filebuf<_CharT, _Traits>*
1439basic_fstream<_CharT, _Traits>::rdbuf() const
1440{
1441 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1442}
1443
1444template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001445inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446bool
1447basic_fstream<_CharT, _Traits>::is_open() const
1448{
1449 return __sb_.is_open();
1450}
1451
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001452#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453template <class _CharT, class _Traits>
1454void
1455basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1456{
1457 if (__sb_.open(__s, __mode))
1458 this->clear();
1459 else
1460 this->setstate(ios_base::failbit);
1461}
1462
1463template <class _CharT, class _Traits>
1464void
1465basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1466{
1467 if (__sb_.open(__s, __mode))
1468 this->clear();
1469 else
1470 this->setstate(ios_base::failbit);
1471}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001472#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473
1474template <class _CharT, class _Traits>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +00001475inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476void
1477basic_fstream<_CharT, _Traits>::close()
1478{
1479 if (__sb_.close() == 0)
1480 this->setstate(ios_base::failbit);
1481}
1482
1483_LIBCPP_END_NAMESPACE_STD
1484
1485#endif // _LIBCPP_FSTREAM