blob: 1f289eddc56f52bdaac1133aaf3030b9fc981297 [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>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000183class _LIBCPP_TYPE_VIS_ONLY 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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203 basic_filebuf& operator=(basic_filebuf&& __rhs);
204#endif
205 void swap(basic_filebuf& __rhs);
206
207 // 27.9.1.4 Members:
208 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000209#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
211 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000212#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213 basic_filebuf* close();
214
215protected:
216 // 27.9.1.5 Overridden virtual functions:
217 virtual int_type underflow();
218 virtual int_type pbackfail(int_type __c = traits_type::eof());
219 virtual int_type overflow (int_type __c = traits_type::eof());
220 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
221 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
222 ios_base::openmode __wch = ios_base::in | ios_base::out);
223 virtual pos_type seekpos(pos_type __sp,
224 ios_base::openmode __wch = ios_base::in | ios_base::out);
225 virtual int sync();
226 virtual void imbue(const locale& __loc);
227
228private:
229 char* __extbuf_;
230 const char* __extbufnext_;
231 const char* __extbufend_;
232 char __extbuf_min_[8];
233 size_t __ebs_;
234 char_type* __intbuf_;
235 size_t __ibs_;
236 FILE* __file_;
237 const codecvt<char_type, char, state_type>* __cv_;
238 state_type __st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000239 state_type __st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240 ios_base::openmode __om_;
241 ios_base::openmode __cm_;
242 bool __owns_eb_;
243 bool __owns_ib_;
244 bool __always_noconv_;
245
246 bool __read_mode();
247 void __write_mode();
248};
249
250template <class _CharT, class _Traits>
251basic_filebuf<_CharT, _Traits>::basic_filebuf()
252 : __extbuf_(0),
253 __extbufnext_(0),
254 __extbufend_(0),
255 __ebs_(0),
256 __intbuf_(0),
257 __ibs_(0),
258 __file_(0),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000259 __cv_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260 __st_(),
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000261 __st_last_(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 __om_(0),
263 __cm_(0),
264 __owns_eb_(false),
265 __owns_ib_(false),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000266 __always_noconv_(false)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000268 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
269 {
270 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
271 __always_noconv_ = __cv_->always_noconv();
272 }
Howard Hinnante7d59f22012-08-24 18:06:47 +0000273 setbuf(0, 4096);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274}
275
Howard Hinnant73d21a42010-09-04 23:28:19 +0000276#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277
278template <class _CharT, class _Traits>
279basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
280 : basic_streambuf<_CharT, _Traits>(__rhs)
281{
282 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
283 {
284 __extbuf_ = __extbuf_min_;
285 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
286 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
287 }
288 else
289 {
290 __extbuf_ = __rhs.__extbuf_;
291 __extbufnext_ = __rhs.__extbufnext_;
292 __extbufend_ = __rhs.__extbufend_;
293 }
294 __ebs_ = __rhs.__ebs_;
295 __intbuf_ = __rhs.__intbuf_;
296 __ibs_ = __rhs.__ibs_;
297 __file_ = __rhs.__file_;
298 __cv_ = __rhs.__cv_;
299 __st_ = __rhs.__st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000300 __st_last_ = __rhs.__st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 __om_ = __rhs.__om_;
302 __cm_ = __rhs.__cm_;
303 __owns_eb_ = __rhs.__owns_eb_;
304 __owns_ib_ = __rhs.__owns_ib_;
305 __always_noconv_ = __rhs.__always_noconv_;
306 if (__rhs.pbase())
307 {
308 if (__rhs.pbase() == __rhs.__intbuf_)
309 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
310 else
311 this->setp((char_type*)__extbuf_,
312 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
313 this->pbump(__rhs. pptr() - __rhs.pbase());
314 }
315 else if (__rhs.eback())
316 {
317 if (__rhs.eback() == __rhs.__intbuf_)
318 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
319 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
320 else
321 this->setg((char_type*)__extbuf_,
322 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
323 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
324 }
325 __rhs.__extbuf_ = 0;
326 __rhs.__extbufnext_ = 0;
327 __rhs.__extbufend_ = 0;
328 __rhs.__ebs_ = 0;
329 __rhs.__intbuf_ = 0;
330 __rhs.__ibs_ = 0;
331 __rhs.__file_ = 0;
332 __rhs.__st_ = state_type();
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000333 __rhs.__st_last_ = state_type();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 __rhs.__om_ = 0;
335 __rhs.__cm_ = 0;
336 __rhs.__owns_eb_ = false;
337 __rhs.__owns_ib_ = false;
338 __rhs.setg(0, 0, 0);
339 __rhs.setp(0, 0);
340}
341
342template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700343inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344basic_filebuf<_CharT, _Traits>&
345basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
346{
347 close();
348 swap(__rhs);
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +0000349 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350}
351
Howard Hinnant73d21a42010-09-04 23:28:19 +0000352#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353
354template <class _CharT, class _Traits>
355basic_filebuf<_CharT, _Traits>::~basic_filebuf()
356{
357#ifndef _LIBCPP_NO_EXCEPTIONS
358 try
359 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000360#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361 close();
362#ifndef _LIBCPP_NO_EXCEPTIONS
363 }
364 catch (...)
365 {
366 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000367#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 if (__owns_eb_)
369 delete [] __extbuf_;
370 if (__owns_ib_)
371 delete [] __intbuf_;
372}
373
374template <class _CharT, class _Traits>
375void
376basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
377{
378 basic_streambuf<char_type, traits_type>::swap(__rhs);
379 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
380 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000381 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
382 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
383 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 }
385 else
386 {
387 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
388 ptrdiff_t __le = __extbufend_ - __extbuf_;
389 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
390 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
391 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
392 {
393 __extbuf_ = __rhs.__extbuf_;
394 __rhs.__extbuf_ = __rhs.__extbuf_min_;
395 }
396 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
397 {
398 __rhs.__extbuf_ = __extbuf_;
399 __extbuf_ = __extbuf_min_;
400 }
401 __extbufnext_ = __extbuf_ + __rn;
402 __extbufend_ = __extbuf_ + __re;
403 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
404 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
405 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000406 _VSTD::swap(__ebs_, __rhs.__ebs_);
407 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
408 _VSTD::swap(__ibs_, __rhs.__ibs_);
409 _VSTD::swap(__file_, __rhs.__file_);
410 _VSTD::swap(__cv_, __rhs.__cv_);
411 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000412 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000413 _VSTD::swap(__om_, __rhs.__om_);
414 _VSTD::swap(__cm_, __rhs.__cm_);
415 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
416 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
417 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
419 {
420 ptrdiff_t __n = this->gptr() - this->eback();
421 ptrdiff_t __e = this->egptr() - this->eback();
422 this->setg((char_type*)__extbuf_min_,
423 (char_type*)__extbuf_min_ + __n,
424 (char_type*)__extbuf_min_ + __e);
425 }
426 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
427 {
428 ptrdiff_t __n = this->pptr() - this->pbase();
429 ptrdiff_t __e = this->epptr() - this->pbase();
430 this->setp((char_type*)__extbuf_min_,
431 (char_type*)__extbuf_min_ + __e);
432 this->pbump(__n);
433 }
434 if (__rhs.eback() == (char_type*)__extbuf_min_)
435 {
436 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
437 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
438 __rhs.setg((char_type*)__rhs.__extbuf_min_,
439 (char_type*)__rhs.__extbuf_min_ + __n,
440 (char_type*)__rhs.__extbuf_min_ + __e);
441 }
442 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
443 {
444 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
445 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
446 __rhs.setp((char_type*)__rhs.__extbuf_min_,
447 (char_type*)__rhs.__extbuf_min_ + __e);
448 __rhs.pbump(__n);
449 }
450}
451
452template <class _CharT, class _Traits>
453inline _LIBCPP_INLINE_VISIBILITY
454void
455swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
456{
457 __x.swap(__y);
458}
459
460template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462bool
463basic_filebuf<_CharT, _Traits>::is_open() const
464{
465 return __file_ != 0;
466}
467
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000468#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469template <class _CharT, class _Traits>
470basic_filebuf<_CharT, _Traits>*
471basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
472{
473 basic_filebuf<_CharT, _Traits>* __rt = 0;
474 if (__file_ == 0)
475 {
476 __rt = this;
477 const char* __mdstr;
478 switch (__mode & ~ios_base::ate)
479 {
480 case ios_base::out:
481 case ios_base::out | ios_base::trunc:
482 __mdstr = "w";
483 break;
484 case ios_base::out | ios_base::app:
485 case ios_base::app:
486 __mdstr = "a";
487 break;
488 case ios_base::in:
489 __mdstr = "r";
490 break;
491 case ios_base::in | ios_base::out:
492 __mdstr = "r+";
493 break;
494 case ios_base::in | ios_base::out | ios_base::trunc:
495 __mdstr = "w+";
496 break;
497 case ios_base::in | ios_base::out | ios_base::app:
498 case ios_base::in | ios_base::app:
499 __mdstr = "a+";
500 break;
501 case ios_base::out | ios_base::binary:
502 case ios_base::out | ios_base::trunc | ios_base::binary:
503 __mdstr = "wb";
504 break;
505 case ios_base::out | ios_base::app | ios_base::binary:
506 case ios_base::app | ios_base::binary:
507 __mdstr = "ab";
508 break;
509 case ios_base::in | ios_base::binary:
510 __mdstr = "rb";
511 break;
512 case ios_base::in | ios_base::out | ios_base::binary:
513 __mdstr = "r+b";
514 break;
515 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
516 __mdstr = "w+b";
517 break;
518 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
519 case ios_base::in | ios_base::app | ios_base::binary:
520 __mdstr = "a+b";
521 break;
522 default:
523 __rt = 0;
524 break;
525 }
526 if (__rt)
527 {
528 __file_ = fopen(__s, __mdstr);
529 if (__file_)
530 {
531 __om_ = __mode;
532 if (__mode & ios_base::ate)
533 {
534 if (fseek(__file_, 0, SEEK_END))
535 {
536 fclose(__file_);
537 __file_ = 0;
538 __rt = 0;
539 }
540 }
541 }
542 else
543 __rt = 0;
544 }
545 }
546 return __rt;
547}
548
549template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551basic_filebuf<_CharT, _Traits>*
552basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
553{
554 return open(__s.c_str(), __mode);
555}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +0000556#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557
558template <class _CharT, class _Traits>
559basic_filebuf<_CharT, _Traits>*
560basic_filebuf<_CharT, _Traits>::close()
561{
562 basic_filebuf<_CharT, _Traits>* __rt = 0;
563 if (__file_)
564 {
565 __rt = this;
566 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnante1a7b042012-01-12 23:37:51 +0000567 if (sync())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 __rt = 0;
569 if (fclose(__h.release()) == 0)
570 __file_ = 0;
571 else
572 __rt = 0;
573 }
574 return __rt;
575}
576
577template <class _CharT, class _Traits>
578typename basic_filebuf<_CharT, _Traits>::int_type
579basic_filebuf<_CharT, _Traits>::underflow()
580{
581 if (__file_ == 0)
582 return traits_type::eof();
583 bool __initial = __read_mode();
584 char_type __1buf;
585 if (this->gptr() == 0)
586 this->setg(&__1buf, &__1buf+1, &__1buf+1);
587 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
588 int_type __c = traits_type::eof();
589 if (this->gptr() == this->egptr())
590 {
591 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
592 if (__always_noconv_)
593 {
594 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
595 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
596 if (__nmemb != 0)
597 {
598 this->setg(this->eback(),
599 this->eback() + __unget_sz,
600 this->eback() + __unget_sz + __nmemb);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000601 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 }
603 }
604 else
605 {
606 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
607 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
608 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantec423cb2012-08-24 20:37:00 +0000609 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 static_cast<size_t>(__extbufend_ - __extbufnext_));
611 codecvt_base::result __r;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000612 __st_last_ = __st_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
614 if (__nr != 0)
615 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000616#ifndef _LIBCPP_NO_EXCEPTIONS
617 if (!__cv_)
618 throw bad_cast();
619#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 __extbufend_ = __extbufnext_ + __nr;
621 char_type* __inext;
622 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
623 this->eback() + __unget_sz,
Howard Hinnantec423cb2012-08-24 20:37:00 +0000624 this->eback() + __ibs_, __inext);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625 if (__r == codecvt_base::noconv)
626 {
627 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000628 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629 }
630 else if (__inext != this->eback() + __unget_sz)
631 {
632 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000633 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 }
635 }
636 }
637 }
638 else
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000639 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 if (this->eback() == &__1buf)
641 this->setg(0, 0, 0);
642 return __c;
643}
644
645template <class _CharT, class _Traits>
646typename basic_filebuf<_CharT, _Traits>::int_type
647basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
648{
649 if (__file_ && this->eback() < this->gptr())
650 {
651 if (traits_type::eq_int_type(__c, traits_type::eof()))
652 {
653 this->gbump(-1);
654 return traits_type::not_eof(__c);
655 }
656 if ((__om_ & ios_base::out) ||
657 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
658 {
659 this->gbump(-1);
660 *this->gptr() = traits_type::to_char_type(__c);
661 return __c;
662 }
663 }
664 return traits_type::eof();
665}
666
667template <class _CharT, class _Traits>
668typename basic_filebuf<_CharT, _Traits>::int_type
669basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
670{
671 if (__file_ == 0)
672 return traits_type::eof();
673 __write_mode();
674 char_type __1buf;
675 char_type* __pb_save = this->pbase();
676 char_type* __epb_save = this->epptr();
677 if (!traits_type::eq_int_type(__c, traits_type::eof()))
678 {
679 if (this->pptr() == 0)
680 this->setp(&__1buf, &__1buf+1);
681 *this->pptr() = traits_type::to_char_type(__c);
682 this->pbump(1);
683 }
684 if (this->pptr() != this->pbase())
685 {
686 if (__always_noconv_)
687 {
688 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
689 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
690 return traits_type::eof();
691 }
692 else
693 {
694 char* __extbe = __extbuf_;
695 codecvt_base::result __r;
696 do
697 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000698#ifndef _LIBCPP_NO_EXCEPTIONS
699 if (!__cv_)
700 throw bad_cast();
701#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 const char_type* __e;
703 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
704 __extbuf_, __extbuf_ + __ebs_, __extbe);
705 if (__e == this->pbase())
706 return traits_type::eof();
707 if (__r == codecvt_base::noconv)
708 {
709 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
710 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
711 return traits_type::eof();
712 }
713 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
714 {
715 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
716 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
717 return traits_type::eof();
718 if (__r == codecvt_base::partial)
719 {
720 this->setp((char_type*)__e, this->pptr());
721 this->pbump(this->epptr() - this->pbase());
722 }
723 }
724 else
725 return traits_type::eof();
726 } while (__r == codecvt_base::partial);
727 }
728 this->setp(__pb_save, __epb_save);
729 }
730 return traits_type::not_eof(__c);
731}
732
733template <class _CharT, class _Traits>
734basic_streambuf<_CharT, _Traits>*
735basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
736{
737 this->setg(0, 0, 0);
738 this->setp(0, 0);
739 if (__owns_eb_)
740 delete [] __extbuf_;
741 if (__owns_ib_)
742 delete [] __intbuf_;
743 __ebs_ = __n;
744 if (__ebs_ > sizeof(__extbuf_min_))
745 {
746 if (__always_noconv_ && __s)
747 {
748 __extbuf_ = (char*)__s;
749 __owns_eb_ = false;
750 }
751 else
752 {
753 __extbuf_ = new char[__ebs_];
754 __owns_eb_ = true;
755 }
756 }
757 else
758 {
759 __extbuf_ = __extbuf_min_;
760 __ebs_ = sizeof(__extbuf_min_);
761 __owns_eb_ = false;
762 }
763 if (!__always_noconv_)
764 {
765 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
766 if (__s && __ibs_ >= sizeof(__extbuf_min_))
767 {
768 __intbuf_ = __s;
769 __owns_ib_ = false;
770 }
771 else
772 {
773 __intbuf_ = new char_type[__ibs_];
774 __owns_ib_ = true;
775 }
776 }
777 else
778 {
779 __ibs_ = 0;
780 __intbuf_ = 0;
781 __owns_ib_ = false;
782 }
783 return this;
784}
785
786template <class _CharT, class _Traits>
787typename basic_filebuf<_CharT, _Traits>::pos_type
788basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
789 ios_base::openmode)
790{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000791#ifndef _LIBCPP_NO_EXCEPTIONS
792 if (!__cv_)
793 throw bad_cast();
794#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795 int __width = __cv_->encoding();
796 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
797 return pos_type(off_type(-1));
798 // __width > 0 || __off == 0
799 int __whence;
800 switch (__way)
801 {
802 case ios_base::beg:
803 __whence = SEEK_SET;
804 break;
805 case ios_base::cur:
806 __whence = SEEK_CUR;
807 break;
808 case ios_base::end:
809 __whence = SEEK_END;
810 break;
811 default:
812 return pos_type(off_type(-1));
813 }
Jonathan Roelofsc6893ae2015-02-03 15:34:17 +0000814#if defined(_WIN32) || defined(_NEWLIB_VERSION)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000815 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
816 return pos_type(off_type(-1));
817 pos_type __r = ftell(__file_);
818#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
820 return pos_type(off_type(-1));
821 pos_type __r = ftello(__file_);
Howard Hinnantcf31d382013-04-02 22:14:51 +0000822#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000823 __r.state(__st_);
824 return __r;
825}
826
827template <class _CharT, class _Traits>
828typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnant639a6682010-07-15 18:18:07 +0000829basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830{
831 if (__file_ == 0 || sync())
832 return pos_type(off_type(-1));
Jonathan Roelofsc6893ae2015-02-03 15:34:17 +0000833#if defined(_WIN32) || defined(_NEWLIB_VERSION)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000834 if (fseek(__file_, __sp, SEEK_SET))
835 return pos_type(off_type(-1));
836#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 if (fseeko(__file_, __sp, SEEK_SET))
838 return pos_type(off_type(-1));
Howard Hinnantcf31d382013-04-02 22:14:51 +0000839#endif
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000840 __st_ = __sp.state();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841 return __sp;
842}
843
844template <class _CharT, class _Traits>
845int
846basic_filebuf<_CharT, _Traits>::sync()
847{
848 if (__file_ == 0)
849 return 0;
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000850#ifndef _LIBCPP_NO_EXCEPTIONS
851 if (!__cv_)
852 throw bad_cast();
853#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854 if (__cm_ & ios_base::out)
855 {
856 if (this->pptr() != this->pbase())
857 if (overflow() == traits_type::eof())
858 return -1;
859 codecvt_base::result __r;
860 do
861 {
862 char* __extbe;
863 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
864 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
865 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
866 return -1;
867 } while (__r == codecvt_base::partial);
868 if (__r == codecvt_base::error)
869 return -1;
870 if (fflush(__file_))
871 return -1;
872 }
873 else if (__cm_ & ios_base::in)
874 {
875 off_type __c;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000876 state_type __state = __st_last_;
877 bool __update_st = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 if (__always_noconv_)
879 __c = this->egptr() - this->gptr();
880 else
881 {
882 int __width = __cv_->encoding();
883 __c = __extbufend_ - __extbufnext_;
884 if (__width > 0)
885 __c += __width * (this->egptr() - this->gptr());
886 else
887 {
888 if (this->gptr() != this->egptr())
889 {
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000890 const int __off = __cv_->length(__state, __extbuf_,
891 __extbufnext_,
892 this->gptr() - this->eback());
893 __c += __extbufnext_ - __extbuf_ - __off;
894 __update_st = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 }
896 }
897 }
Jonathan Roelofsc6893ae2015-02-03 15:34:17 +0000898#if defined(_WIN32) || defined(_NEWLIB_VERSION)
Howard Hinnantcf31d382013-04-02 22:14:51 +0000899 if (fseek(__file_, -__c, SEEK_CUR))
900 return -1;
901#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902 if (fseeko(__file_, -__c, SEEK_CUR))
903 return -1;
Howard Hinnantcf31d382013-04-02 22:14:51 +0000904#endif
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000905 if (__update_st)
906 __st_ = __state;
907 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908 this->setg(0, 0, 0);
909 __cm_ = 0;
910 }
911 return 0;
912}
913
914template <class _CharT, class _Traits>
915void
916basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
917{
918 sync();
919 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
920 bool __old_anc = __always_noconv_;
921 __always_noconv_ = __cv_->always_noconv();
922 if (__old_anc != __always_noconv_)
923 {
924 this->setg(0, 0, 0);
925 this->setp(0, 0);
926 // invariant, char_type is char, else we couldn't get here
927 if (__always_noconv_) // need to dump __intbuf_
928 {
929 if (__owns_eb_)
930 delete [] __extbuf_;
931 __owns_eb_ = __owns_ib_;
932 __ebs_ = __ibs_;
933 __extbuf_ = (char*)__intbuf_;
934 __ibs_ = 0;
935 __intbuf_ = 0;
936 __owns_ib_ = false;
937 }
938 else // need to obtain an __intbuf_.
939 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
940 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
941 {
942 __ibs_ = __ebs_;
943 __intbuf_ = (char_type*)__extbuf_;
944 __owns_ib_ = false;
945 __extbuf_ = new char[__ebs_];
946 __owns_eb_ = true;
947 }
948 else
949 {
950 __ibs_ = __ebs_;
951 __intbuf_ = new char_type[__ibs_];
952 __owns_ib_ = true;
953 }
954 }
955 }
956}
957
958template <class _CharT, class _Traits>
959bool
960basic_filebuf<_CharT, _Traits>::__read_mode()
961{
962 if (!(__cm_ & ios_base::in))
963 {
964 this->setp(0, 0);
965 if (__always_noconv_)
966 this->setg((char_type*)__extbuf_,
967 (char_type*)__extbuf_ + __ebs_,
968 (char_type*)__extbuf_ + __ebs_);
969 else
970 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
971 __cm_ = ios_base::in;
972 return true;
973 }
974 return false;
975}
976
977template <class _CharT, class _Traits>
978void
979basic_filebuf<_CharT, _Traits>::__write_mode()
980{
981 if (!(__cm_ & ios_base::out))
982 {
983 this->setg(0, 0, 0);
984 if (__ebs_ > sizeof(__extbuf_min_))
985 {
986 if (__always_noconv_)
987 this->setp((char_type*)__extbuf_,
988 (char_type*)__extbuf_ + (__ebs_ - 1));
989 else
990 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
991 }
992 else
993 this->setp(0, 0);
994 __cm_ = ios_base::out;
995 }
996}
997
998// basic_ifstream
999
1000template <class _CharT, class _Traits>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001001class _LIBCPP_TYPE_VIS_ONLY basic_ifstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002 : public basic_istream<_CharT, _Traits>
1003{
1004public:
1005 typedef _CharT char_type;
1006 typedef _Traits traits_type;
1007 typedef typename traits_type::int_type int_type;
1008 typedef typename traits_type::pos_type pos_type;
1009 typedef typename traits_type::off_type off_type;
1010
1011 basic_ifstream();
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001012#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1014 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001015#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00001016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 basic_ifstream(basic_ifstream&& __rhs);
1018#endif
1019
Howard Hinnant73d21a42010-09-04 23:28:19 +00001020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 basic_ifstream& operator=(basic_ifstream&& __rhs);
1022#endif
1023 void swap(basic_ifstream& __rhs);
1024
1025 basic_filebuf<char_type, traits_type>* rdbuf() const;
1026 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001027#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1029 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001030#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 void close();
1032
1033private:
1034 basic_filebuf<char_type, traits_type> __sb_;
1035};
1036
1037template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039basic_ifstream<_CharT, _Traits>::basic_ifstream()
1040 : basic_istream<char_type, traits_type>(&__sb_)
1041{
1042}
1043
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001044#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001046inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1048 : basic_istream<char_type, traits_type>(&__sb_)
1049{
1050 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1051 this->setstate(ios_base::failbit);
1052}
1053
1054template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1057 : basic_istream<char_type, traits_type>(&__sb_)
1058{
1059 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1060 this->setstate(ios_base::failbit);
1061}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001062#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063
Howard Hinnant73d21a42010-09-04 23:28:19 +00001064#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065
1066template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001069 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1070 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071{
1072 this->set_rdbuf(&__sb_);
1073}
1074
1075template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001076inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077basic_ifstream<_CharT, _Traits>&
1078basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1079{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001080 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1081 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082 return *this;
1083}
1084
Howard Hinnant73d21a42010-09-04 23:28:19 +00001085#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086
1087template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089void
1090basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1091{
1092 basic_istream<char_type, traits_type>::swap(__rhs);
1093 __sb_.swap(__rhs.__sb_);
1094}
1095
1096template <class _CharT, class _Traits>
1097inline _LIBCPP_INLINE_VISIBILITY
1098void
1099swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1100{
1101 __x.swap(__y);
1102}
1103
1104template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001105inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106basic_filebuf<_CharT, _Traits>*
1107basic_ifstream<_CharT, _Traits>::rdbuf() const
1108{
1109 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1110}
1111
1112template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001113inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114bool
1115basic_ifstream<_CharT, _Traits>::is_open() const
1116{
1117 return __sb_.is_open();
1118}
1119
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001120#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121template <class _CharT, class _Traits>
1122void
1123basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1124{
1125 if (__sb_.open(__s, __mode | ios_base::in))
1126 this->clear();
1127 else
1128 this->setstate(ios_base::failbit);
1129}
1130
1131template <class _CharT, class _Traits>
1132void
1133basic_ifstream<_CharT, _Traits>::open(const string& __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}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001140#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141
1142template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144void
1145basic_ifstream<_CharT, _Traits>::close()
1146{
1147 if (__sb_.close() == 0)
1148 this->setstate(ios_base::failbit);
1149}
1150
1151// basic_ofstream
1152
1153template <class _CharT, class _Traits>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001154class _LIBCPP_TYPE_VIS_ONLY basic_ofstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155 : public basic_ostream<_CharT, _Traits>
1156{
1157public:
1158 typedef _CharT char_type;
1159 typedef _Traits traits_type;
1160 typedef typename traits_type::int_type int_type;
1161 typedef typename traits_type::pos_type pos_type;
1162 typedef typename traits_type::off_type off_type;
1163
1164 basic_ofstream();
1165 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1166 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001167#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 basic_ofstream(basic_ofstream&& __rhs);
1169#endif
1170
Howard Hinnant73d21a42010-09-04 23:28:19 +00001171#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 basic_ofstream& operator=(basic_ofstream&& __rhs);
1173#endif
1174 void swap(basic_ofstream& __rhs);
1175
1176 basic_filebuf<char_type, traits_type>* rdbuf() const;
1177 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001178#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1180 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 void close();
1183
1184private:
1185 basic_filebuf<char_type, traits_type> __sb_;
1186};
1187
1188template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001189inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190basic_ofstream<_CharT, _Traits>::basic_ofstream()
1191 : basic_ostream<char_type, traits_type>(&__sb_)
1192{
1193}
1194
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001195#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001197inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1199 : basic_ostream<char_type, traits_type>(&__sb_)
1200{
1201 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1202 this->setstate(ios_base::failbit);
1203}
1204
1205template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001206inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1208 : basic_ostream<char_type, traits_type>(&__sb_)
1209{
1210 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1211 this->setstate(ios_base::failbit);
1212}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001213#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214
Howard Hinnant73d21a42010-09-04 23:28:19 +00001215#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216
1217template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001218inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001220 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1221 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222{
1223 this->set_rdbuf(&__sb_);
1224}
1225
1226template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001227inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228basic_ofstream<_CharT, _Traits>&
1229basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1230{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001231 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1232 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233 return *this;
1234}
1235
Howard Hinnant73d21a42010-09-04 23:28:19 +00001236#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237
1238template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001239inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240void
1241basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1242{
1243 basic_ostream<char_type, traits_type>::swap(__rhs);
1244 __sb_.swap(__rhs.__sb_);
1245}
1246
1247template <class _CharT, class _Traits>
1248inline _LIBCPP_INLINE_VISIBILITY
1249void
1250swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1251{
1252 __x.swap(__y);
1253}
1254
1255template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257basic_filebuf<_CharT, _Traits>*
1258basic_ofstream<_CharT, _Traits>::rdbuf() const
1259{
1260 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1261}
1262
1263template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265bool
1266basic_ofstream<_CharT, _Traits>::is_open() const
1267{
1268 return __sb_.is_open();
1269}
1270
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001271#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272template <class _CharT, class _Traits>
1273void
1274basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1275{
1276 if (__sb_.open(__s, __mode | ios_base::out))
1277 this->clear();
1278 else
1279 this->setstate(ios_base::failbit);
1280}
1281
1282template <class _CharT, class _Traits>
1283void
1284basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1285{
1286 if (__sb_.open(__s, __mode | ios_base::out))
1287 this->clear();
1288 else
1289 this->setstate(ios_base::failbit);
1290}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001291#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292
1293template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295void
1296basic_ofstream<_CharT, _Traits>::close()
1297{
1298 if (__sb_.close() == 0)
1299 this->setstate(ios_base::failbit);
1300}
1301
1302// basic_fstream
1303
1304template <class _CharT, class _Traits>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001305class _LIBCPP_TYPE_VIS_ONLY basic_fstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 : public basic_iostream<_CharT, _Traits>
1307{
1308public:
1309 typedef _CharT char_type;
1310 typedef _Traits traits_type;
1311 typedef typename traits_type::int_type int_type;
1312 typedef typename traits_type::pos_type pos_type;
1313 typedef typename traits_type::off_type off_type;
1314
1315 basic_fstream();
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001316#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1318 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001319#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00001320#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 basic_fstream(basic_fstream&& __rhs);
1322#endif
1323
Howard Hinnant73d21a42010-09-04 23:28:19 +00001324#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 basic_fstream& operator=(basic_fstream&& __rhs);
1326#endif
1327 void swap(basic_fstream& __rhs);
1328
1329 basic_filebuf<char_type, traits_type>* rdbuf() const;
1330 bool is_open() const;
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001331#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1333 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001334#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 void close();
1336
1337private:
1338 basic_filebuf<char_type, traits_type> __sb_;
1339};
1340
1341template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343basic_fstream<_CharT, _Traits>::basic_fstream()
1344 : basic_iostream<char_type, traits_type>(&__sb_)
1345{
1346}
1347
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001348#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001351basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1352 : basic_iostream<char_type, traits_type>(&__sb_)
1353{
1354 if (__sb_.open(__s, __mode) == 0)
1355 this->setstate(ios_base::failbit);
1356}
1357
1358template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001359inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1361 : basic_iostream<char_type, traits_type>(&__sb_)
1362{
1363 if (__sb_.open(__s, __mode) == 0)
1364 this->setstate(ios_base::failbit);
1365}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001366#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367
Howard Hinnant73d21a42010-09-04 23:28:19 +00001368#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369
1370template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001373 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1374 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375{
1376 this->set_rdbuf(&__sb_);
1377}
1378
1379template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001380inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381basic_fstream<_CharT, _Traits>&
1382basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1383{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001384 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1385 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001386 return *this;
1387}
1388
Howard Hinnant73d21a42010-09-04 23:28:19 +00001389#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390
1391template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001392inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393void
1394basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1395{
1396 basic_iostream<char_type, traits_type>::swap(__rhs);
1397 __sb_.swap(__rhs.__sb_);
1398}
1399
1400template <class _CharT, class _Traits>
1401inline _LIBCPP_INLINE_VISIBILITY
1402void
1403swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1404{
1405 __x.swap(__y);
1406}
1407
1408template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001409inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410basic_filebuf<_CharT, _Traits>*
1411basic_fstream<_CharT, _Traits>::rdbuf() const
1412{
1413 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1414}
1415
1416template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001417inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418bool
1419basic_fstream<_CharT, _Traits>::is_open() const
1420{
1421 return __sb_.is_open();
1422}
1423
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001424#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425template <class _CharT, class _Traits>
1426void
1427basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1428{
1429 if (__sb_.open(__s, __mode))
1430 this->clear();
1431 else
1432 this->setstate(ios_base::failbit);
1433}
1434
1435template <class _CharT, class _Traits>
1436void
1437basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1438{
1439 if (__sb_.open(__s, __mode))
1440 this->clear();
1441 else
1442 this->setstate(ios_base::failbit);
1443}
Ed Schoutenb33ae5b2015-03-12 15:44:39 +00001444#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445
1446template <class _CharT, class _Traits>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001447inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448void
1449basic_fstream<_CharT, _Traits>::close()
1450{
1451 if (__sb_.close() == 0)
1452 this->setstate(ios_base::failbit);
1453}
1454
1455_LIBCPP_END_NAMESPACE_STD
1456
1457#endif // _LIBCPP_FSTREAM