blob: 7b6578f1e57db0a9497184618a5830b65a9e4ec5 [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 Hinnant42a63a72010-09-21 22:55:27 +0000183class _LIBCPP_VISIBLE 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;
209 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
210 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
211 basic_filebuf* close();
212
213protected:
214 // 27.9.1.5 Overridden virtual functions:
215 virtual int_type underflow();
216 virtual int_type pbackfail(int_type __c = traits_type::eof());
217 virtual int_type overflow (int_type __c = traits_type::eof());
218 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
219 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
220 ios_base::openmode __wch = ios_base::in | ios_base::out);
221 virtual pos_type seekpos(pos_type __sp,
222 ios_base::openmode __wch = ios_base::in | ios_base::out);
223 virtual int sync();
224 virtual void imbue(const locale& __loc);
225
226private:
227 char* __extbuf_;
228 const char* __extbufnext_;
229 const char* __extbufend_;
230 char __extbuf_min_[8];
231 size_t __ebs_;
232 char_type* __intbuf_;
233 size_t __ibs_;
234 FILE* __file_;
235 const codecvt<char_type, char, state_type>* __cv_;
236 state_type __st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000237 state_type __st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 ios_base::openmode __om_;
239 ios_base::openmode __cm_;
240 bool __owns_eb_;
241 bool __owns_ib_;
242 bool __always_noconv_;
243
244 bool __read_mode();
245 void __write_mode();
246};
247
248template <class _CharT, class _Traits>
249basic_filebuf<_CharT, _Traits>::basic_filebuf()
250 : __extbuf_(0),
251 __extbufnext_(0),
252 __extbufend_(0),
253 __ebs_(0),
254 __intbuf_(0),
255 __ibs_(0),
256 __file_(0),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000257 __cv_(nullptr),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 __st_(),
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000259 __st_last_(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260 __om_(0),
261 __cm_(0),
262 __owns_eb_(false),
263 __owns_ib_(false),
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000264 __always_noconv_(false)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000266 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
267 {
268 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
269 __always_noconv_ = __cv_->always_noconv();
270 }
Howard Hinnante7d59f22012-08-24 18:06:47 +0000271 setbuf(0, 4096);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272}
273
Howard Hinnant73d21a42010-09-04 23:28:19 +0000274#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275
276template <class _CharT, class _Traits>
277basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
278 : basic_streambuf<_CharT, _Traits>(__rhs)
279{
280 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
281 {
282 __extbuf_ = __extbuf_min_;
283 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
284 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
285 }
286 else
287 {
288 __extbuf_ = __rhs.__extbuf_;
289 __extbufnext_ = __rhs.__extbufnext_;
290 __extbufend_ = __rhs.__extbufend_;
291 }
292 __ebs_ = __rhs.__ebs_;
293 __intbuf_ = __rhs.__intbuf_;
294 __ibs_ = __rhs.__ibs_;
295 __file_ = __rhs.__file_;
296 __cv_ = __rhs.__cv_;
297 __st_ = __rhs.__st_;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000298 __st_last_ = __rhs.__st_last_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 __om_ = __rhs.__om_;
300 __cm_ = __rhs.__cm_;
301 __owns_eb_ = __rhs.__owns_eb_;
302 __owns_ib_ = __rhs.__owns_ib_;
303 __always_noconv_ = __rhs.__always_noconv_;
304 if (__rhs.pbase())
305 {
306 if (__rhs.pbase() == __rhs.__intbuf_)
307 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
308 else
309 this->setp((char_type*)__extbuf_,
310 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
311 this->pbump(__rhs. pptr() - __rhs.pbase());
312 }
313 else if (__rhs.eback())
314 {
315 if (__rhs.eback() == __rhs.__intbuf_)
316 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
317 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
318 else
319 this->setg((char_type*)__extbuf_,
320 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
321 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
322 }
323 __rhs.__extbuf_ = 0;
324 __rhs.__extbufnext_ = 0;
325 __rhs.__extbufend_ = 0;
326 __rhs.__ebs_ = 0;
327 __rhs.__intbuf_ = 0;
328 __rhs.__ibs_ = 0;
329 __rhs.__file_ = 0;
330 __rhs.__st_ = state_type();
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000331 __rhs.__st_last_ = state_type();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332 __rhs.__om_ = 0;
333 __rhs.__cm_ = 0;
334 __rhs.__owns_eb_ = false;
335 __rhs.__owns_ib_ = false;
336 __rhs.setg(0, 0, 0);
337 __rhs.setp(0, 0);
338}
339
340template <class _CharT, class _Traits>
341inline _LIBCPP_INLINE_VISIBILITY
342basic_filebuf<_CharT, _Traits>&
343basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
344{
345 close();
346 swap(__rhs);
347}
348
Howard Hinnant73d21a42010-09-04 23:28:19 +0000349#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350
351template <class _CharT, class _Traits>
352basic_filebuf<_CharT, _Traits>::~basic_filebuf()
353{
354#ifndef _LIBCPP_NO_EXCEPTIONS
355 try
356 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000357#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358 close();
359#ifndef _LIBCPP_NO_EXCEPTIONS
360 }
361 catch (...)
362 {
363 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000364#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 if (__owns_eb_)
366 delete [] __extbuf_;
367 if (__owns_ib_)
368 delete [] __intbuf_;
369}
370
371template <class _CharT, class _Traits>
372void
373basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
374{
375 basic_streambuf<char_type, traits_type>::swap(__rhs);
376 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
377 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000378 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
379 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
380 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 }
382 else
383 {
384 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
385 ptrdiff_t __le = __extbufend_ - __extbuf_;
386 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
387 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
388 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
389 {
390 __extbuf_ = __rhs.__extbuf_;
391 __rhs.__extbuf_ = __rhs.__extbuf_min_;
392 }
393 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
394 {
395 __rhs.__extbuf_ = __extbuf_;
396 __extbuf_ = __extbuf_min_;
397 }
398 __extbufnext_ = __extbuf_ + __rn;
399 __extbufend_ = __extbuf_ + __re;
400 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
401 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
402 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000403 _VSTD::swap(__ebs_, __rhs.__ebs_);
404 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
405 _VSTD::swap(__ibs_, __rhs.__ibs_);
406 _VSTD::swap(__file_, __rhs.__file_);
407 _VSTD::swap(__cv_, __rhs.__cv_);
408 _VSTD::swap(__st_, __rhs.__st_);
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000409 _VSTD::swap(__st_last_, __rhs.__st_last_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000410 _VSTD::swap(__om_, __rhs.__om_);
411 _VSTD::swap(__cm_, __rhs.__cm_);
412 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
413 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
414 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
416 {
417 ptrdiff_t __n = this->gptr() - this->eback();
418 ptrdiff_t __e = this->egptr() - this->eback();
419 this->setg((char_type*)__extbuf_min_,
420 (char_type*)__extbuf_min_ + __n,
421 (char_type*)__extbuf_min_ + __e);
422 }
423 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
424 {
425 ptrdiff_t __n = this->pptr() - this->pbase();
426 ptrdiff_t __e = this->epptr() - this->pbase();
427 this->setp((char_type*)__extbuf_min_,
428 (char_type*)__extbuf_min_ + __e);
429 this->pbump(__n);
430 }
431 if (__rhs.eback() == (char_type*)__extbuf_min_)
432 {
433 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
434 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
435 __rhs.setg((char_type*)__rhs.__extbuf_min_,
436 (char_type*)__rhs.__extbuf_min_ + __n,
437 (char_type*)__rhs.__extbuf_min_ + __e);
438 }
439 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
440 {
441 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
442 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
443 __rhs.setp((char_type*)__rhs.__extbuf_min_,
444 (char_type*)__rhs.__extbuf_min_ + __e);
445 __rhs.pbump(__n);
446 }
447}
448
449template <class _CharT, class _Traits>
450inline _LIBCPP_INLINE_VISIBILITY
451void
452swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
453{
454 __x.swap(__y);
455}
456
457template <class _CharT, class _Traits>
458inline _LIBCPP_INLINE_VISIBILITY
459bool
460basic_filebuf<_CharT, _Traits>::is_open() const
461{
462 return __file_ != 0;
463}
464
465template <class _CharT, class _Traits>
466basic_filebuf<_CharT, _Traits>*
467basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
468{
469 basic_filebuf<_CharT, _Traits>* __rt = 0;
470 if (__file_ == 0)
471 {
472 __rt = this;
473 const char* __mdstr;
474 switch (__mode & ~ios_base::ate)
475 {
476 case ios_base::out:
477 case ios_base::out | ios_base::trunc:
478 __mdstr = "w";
479 break;
480 case ios_base::out | ios_base::app:
481 case ios_base::app:
482 __mdstr = "a";
483 break;
484 case ios_base::in:
485 __mdstr = "r";
486 break;
487 case ios_base::in | ios_base::out:
488 __mdstr = "r+";
489 break;
490 case ios_base::in | ios_base::out | ios_base::trunc:
491 __mdstr = "w+";
492 break;
493 case ios_base::in | ios_base::out | ios_base::app:
494 case ios_base::in | ios_base::app:
495 __mdstr = "a+";
496 break;
497 case ios_base::out | ios_base::binary:
498 case ios_base::out | ios_base::trunc | ios_base::binary:
499 __mdstr = "wb";
500 break;
501 case ios_base::out | ios_base::app | ios_base::binary:
502 case ios_base::app | ios_base::binary:
503 __mdstr = "ab";
504 break;
505 case ios_base::in | ios_base::binary:
506 __mdstr = "rb";
507 break;
508 case ios_base::in | ios_base::out | ios_base::binary:
509 __mdstr = "r+b";
510 break;
511 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
512 __mdstr = "w+b";
513 break;
514 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
515 case ios_base::in | ios_base::app | ios_base::binary:
516 __mdstr = "a+b";
517 break;
518 default:
519 __rt = 0;
520 break;
521 }
522 if (__rt)
523 {
524 __file_ = fopen(__s, __mdstr);
525 if (__file_)
526 {
527 __om_ = __mode;
528 if (__mode & ios_base::ate)
529 {
530 if (fseek(__file_, 0, SEEK_END))
531 {
532 fclose(__file_);
533 __file_ = 0;
534 __rt = 0;
535 }
536 }
537 }
538 else
539 __rt = 0;
540 }
541 }
542 return __rt;
543}
544
545template <class _CharT, class _Traits>
546inline _LIBCPP_INLINE_VISIBILITY
547basic_filebuf<_CharT, _Traits>*
548basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
549{
550 return open(__s.c_str(), __mode);
551}
552
553template <class _CharT, class _Traits>
554basic_filebuf<_CharT, _Traits>*
555basic_filebuf<_CharT, _Traits>::close()
556{
557 basic_filebuf<_CharT, _Traits>* __rt = 0;
558 if (__file_)
559 {
560 __rt = this;
561 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
Howard Hinnante1a7b042012-01-12 23:37:51 +0000562 if (sync())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 __rt = 0;
564 if (fclose(__h.release()) == 0)
565 __file_ = 0;
566 else
567 __rt = 0;
568 }
569 return __rt;
570}
571
572template <class _CharT, class _Traits>
573typename basic_filebuf<_CharT, _Traits>::int_type
574basic_filebuf<_CharT, _Traits>::underflow()
575{
576 if (__file_ == 0)
577 return traits_type::eof();
578 bool __initial = __read_mode();
579 char_type __1buf;
580 if (this->gptr() == 0)
581 this->setg(&__1buf, &__1buf+1, &__1buf+1);
582 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
583 int_type __c = traits_type::eof();
584 if (this->gptr() == this->egptr())
585 {
586 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
587 if (__always_noconv_)
588 {
589 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
590 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
591 if (__nmemb != 0)
592 {
593 this->setg(this->eback(),
594 this->eback() + __unget_sz,
595 this->eback() + __unget_sz + __nmemb);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000596 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 }
598 }
599 else
600 {
601 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
602 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
603 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnantec423cb2012-08-24 20:37:00 +0000604 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 static_cast<size_t>(__extbufend_ - __extbufnext_));
606 codecvt_base::result __r;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000607 __st_last_ = __st_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
609 if (__nr != 0)
610 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000611#ifndef _LIBCPP_NO_EXCEPTIONS
612 if (!__cv_)
613 throw bad_cast();
614#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615 __extbufend_ = __extbufnext_ + __nr;
616 char_type* __inext;
617 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
618 this->eback() + __unget_sz,
Howard Hinnantec423cb2012-08-24 20:37:00 +0000619 this->eback() + __ibs_, __inext);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 if (__r == codecvt_base::noconv)
621 {
622 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000623 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624 }
625 else if (__inext != this->eback() + __unget_sz)
626 {
627 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
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 }
631 }
632 }
633 else
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000634 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 if (this->eback() == &__1buf)
636 this->setg(0, 0, 0);
637 return __c;
638}
639
640template <class _CharT, class _Traits>
641typename basic_filebuf<_CharT, _Traits>::int_type
642basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
643{
644 if (__file_ && this->eback() < this->gptr())
645 {
646 if (traits_type::eq_int_type(__c, traits_type::eof()))
647 {
648 this->gbump(-1);
649 return traits_type::not_eof(__c);
650 }
651 if ((__om_ & ios_base::out) ||
652 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
653 {
654 this->gbump(-1);
655 *this->gptr() = traits_type::to_char_type(__c);
656 return __c;
657 }
658 }
659 return traits_type::eof();
660}
661
662template <class _CharT, class _Traits>
663typename basic_filebuf<_CharT, _Traits>::int_type
664basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
665{
666 if (__file_ == 0)
667 return traits_type::eof();
668 __write_mode();
669 char_type __1buf;
670 char_type* __pb_save = this->pbase();
671 char_type* __epb_save = this->epptr();
672 if (!traits_type::eq_int_type(__c, traits_type::eof()))
673 {
674 if (this->pptr() == 0)
675 this->setp(&__1buf, &__1buf+1);
676 *this->pptr() = traits_type::to_char_type(__c);
677 this->pbump(1);
678 }
679 if (this->pptr() != this->pbase())
680 {
681 if (__always_noconv_)
682 {
683 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
684 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
685 return traits_type::eof();
686 }
687 else
688 {
689 char* __extbe = __extbuf_;
690 codecvt_base::result __r;
691 do
692 {
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000693#ifndef _LIBCPP_NO_EXCEPTIONS
694 if (!__cv_)
695 throw bad_cast();
696#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 const char_type* __e;
698 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
699 __extbuf_, __extbuf_ + __ebs_, __extbe);
700 if (__e == this->pbase())
701 return traits_type::eof();
702 if (__r == codecvt_base::noconv)
703 {
704 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
705 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
706 return traits_type::eof();
707 }
708 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
709 {
710 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
711 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
712 return traits_type::eof();
713 if (__r == codecvt_base::partial)
714 {
715 this->setp((char_type*)__e, this->pptr());
716 this->pbump(this->epptr() - this->pbase());
717 }
718 }
719 else
720 return traits_type::eof();
721 } while (__r == codecvt_base::partial);
722 }
723 this->setp(__pb_save, __epb_save);
724 }
725 return traits_type::not_eof(__c);
726}
727
728template <class _CharT, class _Traits>
729basic_streambuf<_CharT, _Traits>*
730basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
731{
732 this->setg(0, 0, 0);
733 this->setp(0, 0);
734 if (__owns_eb_)
735 delete [] __extbuf_;
736 if (__owns_ib_)
737 delete [] __intbuf_;
738 __ebs_ = __n;
739 if (__ebs_ > sizeof(__extbuf_min_))
740 {
741 if (__always_noconv_ && __s)
742 {
743 __extbuf_ = (char*)__s;
744 __owns_eb_ = false;
745 }
746 else
747 {
748 __extbuf_ = new char[__ebs_];
749 __owns_eb_ = true;
750 }
751 }
752 else
753 {
754 __extbuf_ = __extbuf_min_;
755 __ebs_ = sizeof(__extbuf_min_);
756 __owns_eb_ = false;
757 }
758 if (!__always_noconv_)
759 {
760 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
761 if (__s && __ibs_ >= sizeof(__extbuf_min_))
762 {
763 __intbuf_ = __s;
764 __owns_ib_ = false;
765 }
766 else
767 {
768 __intbuf_ = new char_type[__ibs_];
769 __owns_ib_ = true;
770 }
771 }
772 else
773 {
774 __ibs_ = 0;
775 __intbuf_ = 0;
776 __owns_ib_ = false;
777 }
778 return this;
779}
780
781template <class _CharT, class _Traits>
782typename basic_filebuf<_CharT, _Traits>::pos_type
783basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
784 ios_base::openmode)
785{
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000786#ifndef _LIBCPP_NO_EXCEPTIONS
787 if (!__cv_)
788 throw bad_cast();
789#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790 int __width = __cv_->encoding();
791 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
792 return pos_type(off_type(-1));
793 // __width > 0 || __off == 0
794 int __whence;
795 switch (__way)
796 {
797 case ios_base::beg:
798 __whence = SEEK_SET;
799 break;
800 case ios_base::cur:
801 __whence = SEEK_CUR;
802 break;
803 case ios_base::end:
804 __whence = SEEK_END;
805 break;
806 default:
807 return pos_type(off_type(-1));
808 }
809 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
810 return pos_type(off_type(-1));
811 pos_type __r = ftello(__file_);
812 __r.state(__st_);
813 return __r;
814}
815
816template <class _CharT, class _Traits>
817typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnant639a6682010-07-15 18:18:07 +0000818basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819{
820 if (__file_ == 0 || sync())
821 return pos_type(off_type(-1));
822 if (fseeko(__file_, __sp, SEEK_SET))
823 return pos_type(off_type(-1));
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000824 __st_ = __sp.state();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 return __sp;
826}
827
828template <class _CharT, class _Traits>
829int
830basic_filebuf<_CharT, _Traits>::sync()
831{
832 if (__file_ == 0)
833 return 0;
Howard Hinnant8540d4c2012-08-24 16:52:47 +0000834#ifndef _LIBCPP_NO_EXCEPTIONS
835 if (!__cv_)
836 throw bad_cast();
837#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838 if (__cm_ & ios_base::out)
839 {
840 if (this->pptr() != this->pbase())
841 if (overflow() == traits_type::eof())
842 return -1;
843 codecvt_base::result __r;
844 do
845 {
846 char* __extbe;
847 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
848 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
849 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
850 return -1;
851 } while (__r == codecvt_base::partial);
852 if (__r == codecvt_base::error)
853 return -1;
854 if (fflush(__file_))
855 return -1;
856 }
857 else if (__cm_ & ios_base::in)
858 {
859 off_type __c;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000860 state_type __state = __st_last_;
861 bool __update_st = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862 if (__always_noconv_)
863 __c = this->egptr() - this->gptr();
864 else
865 {
866 int __width = __cv_->encoding();
867 __c = __extbufend_ - __extbufnext_;
868 if (__width > 0)
869 __c += __width * (this->egptr() - this->gptr());
870 else
871 {
872 if (this->gptr() != this->egptr())
873 {
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000874 const int __off = __cv_->length(__state, __extbuf_,
875 __extbufnext_,
876 this->gptr() - this->eback());
877 __c += __extbufnext_ - __extbuf_ - __off;
878 __update_st = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 }
880 }
881 }
882 if (fseeko(__file_, -__c, SEEK_CUR))
883 return -1;
Howard Hinnantd305d3c2012-08-24 21:20:56 +0000884 if (__update_st)
885 __st_ = __state;
886 __extbufnext_ = __extbufend_ = __extbuf_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887 this->setg(0, 0, 0);
888 __cm_ = 0;
889 }
890 return 0;
891}
892
893template <class _CharT, class _Traits>
894void
895basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
896{
897 sync();
898 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
899 bool __old_anc = __always_noconv_;
900 __always_noconv_ = __cv_->always_noconv();
901 if (__old_anc != __always_noconv_)
902 {
903 this->setg(0, 0, 0);
904 this->setp(0, 0);
905 // invariant, char_type is char, else we couldn't get here
906 if (__always_noconv_) // need to dump __intbuf_
907 {
908 if (__owns_eb_)
909 delete [] __extbuf_;
910 __owns_eb_ = __owns_ib_;
911 __ebs_ = __ibs_;
912 __extbuf_ = (char*)__intbuf_;
913 __ibs_ = 0;
914 __intbuf_ = 0;
915 __owns_ib_ = false;
916 }
917 else // need to obtain an __intbuf_.
918 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
919 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
920 {
921 __ibs_ = __ebs_;
922 __intbuf_ = (char_type*)__extbuf_;
923 __owns_ib_ = false;
924 __extbuf_ = new char[__ebs_];
925 __owns_eb_ = true;
926 }
927 else
928 {
929 __ibs_ = __ebs_;
930 __intbuf_ = new char_type[__ibs_];
931 __owns_ib_ = true;
932 }
933 }
934 }
935}
936
937template <class _CharT, class _Traits>
938bool
939basic_filebuf<_CharT, _Traits>::__read_mode()
940{
941 if (!(__cm_ & ios_base::in))
942 {
943 this->setp(0, 0);
944 if (__always_noconv_)
945 this->setg((char_type*)__extbuf_,
946 (char_type*)__extbuf_ + __ebs_,
947 (char_type*)__extbuf_ + __ebs_);
948 else
949 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
950 __cm_ = ios_base::in;
951 return true;
952 }
953 return false;
954}
955
956template <class _CharT, class _Traits>
957void
958basic_filebuf<_CharT, _Traits>::__write_mode()
959{
960 if (!(__cm_ & ios_base::out))
961 {
962 this->setg(0, 0, 0);
963 if (__ebs_ > sizeof(__extbuf_min_))
964 {
965 if (__always_noconv_)
966 this->setp((char_type*)__extbuf_,
967 (char_type*)__extbuf_ + (__ebs_ - 1));
968 else
969 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
970 }
971 else
972 this->setp(0, 0);
973 __cm_ = ios_base::out;
974 }
975}
976
977// basic_ifstream
978
979template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000980class _LIBCPP_VISIBLE basic_ifstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981 : public basic_istream<_CharT, _Traits>
982{
983public:
984 typedef _CharT char_type;
985 typedef _Traits traits_type;
986 typedef typename traits_type::int_type int_type;
987 typedef typename traits_type::pos_type pos_type;
988 typedef typename traits_type::off_type off_type;
989
990 basic_ifstream();
991 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
992 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000993#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 basic_ifstream(basic_ifstream&& __rhs);
995#endif
996
Howard Hinnant73d21a42010-09-04 23:28:19 +0000997#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 basic_ifstream& operator=(basic_ifstream&& __rhs);
999#endif
1000 void swap(basic_ifstream& __rhs);
1001
1002 basic_filebuf<char_type, traits_type>* rdbuf() const;
1003 bool is_open() const;
1004 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1005 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1006 void close();
1007
1008private:
1009 basic_filebuf<char_type, traits_type> __sb_;
1010};
1011
1012template <class _CharT, class _Traits>
1013inline _LIBCPP_INLINE_VISIBILITY
1014basic_ifstream<_CharT, _Traits>::basic_ifstream()
1015 : basic_istream<char_type, traits_type>(&__sb_)
1016{
1017}
1018
1019template <class _CharT, class _Traits>
1020inline _LIBCPP_INLINE_VISIBILITY
1021basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1022 : basic_istream<char_type, traits_type>(&__sb_)
1023{
1024 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1025 this->setstate(ios_base::failbit);
1026}
1027
1028template <class _CharT, class _Traits>
1029inline _LIBCPP_INLINE_VISIBILITY
1030basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1031 : basic_istream<char_type, traits_type>(&__sb_)
1032{
1033 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1034 this->setstate(ios_base::failbit);
1035}
1036
Howard Hinnant73d21a42010-09-04 23:28:19 +00001037#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038
1039template <class _CharT, class _Traits>
1040inline _LIBCPP_INLINE_VISIBILITY
1041basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001042 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1043 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044{
1045 this->set_rdbuf(&__sb_);
1046}
1047
1048template <class _CharT, class _Traits>
1049inline _LIBCPP_INLINE_VISIBILITY
1050basic_ifstream<_CharT, _Traits>&
1051basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1052{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001053 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1054 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055 return *this;
1056}
1057
Howard Hinnant73d21a42010-09-04 23:28:19 +00001058#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059
1060template <class _CharT, class _Traits>
1061inline _LIBCPP_INLINE_VISIBILITY
1062void
1063basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1064{
1065 basic_istream<char_type, traits_type>::swap(__rhs);
1066 __sb_.swap(__rhs.__sb_);
1067}
1068
1069template <class _CharT, class _Traits>
1070inline _LIBCPP_INLINE_VISIBILITY
1071void
1072swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1073{
1074 __x.swap(__y);
1075}
1076
1077template <class _CharT, class _Traits>
1078inline _LIBCPP_INLINE_VISIBILITY
1079basic_filebuf<_CharT, _Traits>*
1080basic_ifstream<_CharT, _Traits>::rdbuf() const
1081{
1082 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1083}
1084
1085template <class _CharT, class _Traits>
1086inline _LIBCPP_INLINE_VISIBILITY
1087bool
1088basic_ifstream<_CharT, _Traits>::is_open() const
1089{
1090 return __sb_.is_open();
1091}
1092
1093template <class _CharT, class _Traits>
1094void
1095basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1096{
1097 if (__sb_.open(__s, __mode | ios_base::in))
1098 this->clear();
1099 else
1100 this->setstate(ios_base::failbit);
1101}
1102
1103template <class _CharT, class _Traits>
1104void
1105basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1106{
1107 if (__sb_.open(__s, __mode | ios_base::in))
1108 this->clear();
1109 else
1110 this->setstate(ios_base::failbit);
1111}
1112
1113template <class _CharT, class _Traits>
1114inline _LIBCPP_INLINE_VISIBILITY
1115void
1116basic_ifstream<_CharT, _Traits>::close()
1117{
1118 if (__sb_.close() == 0)
1119 this->setstate(ios_base::failbit);
1120}
1121
1122// basic_ofstream
1123
1124template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001125class _LIBCPP_VISIBLE basic_ofstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126 : public basic_ostream<_CharT, _Traits>
1127{
1128public:
1129 typedef _CharT char_type;
1130 typedef _Traits traits_type;
1131 typedef typename traits_type::int_type int_type;
1132 typedef typename traits_type::pos_type pos_type;
1133 typedef typename traits_type::off_type off_type;
1134
1135 basic_ofstream();
1136 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1137 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001138#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 basic_ofstream(basic_ofstream&& __rhs);
1140#endif
1141
Howard Hinnant73d21a42010-09-04 23:28:19 +00001142#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 basic_ofstream& operator=(basic_ofstream&& __rhs);
1144#endif
1145 void swap(basic_ofstream& __rhs);
1146
1147 basic_filebuf<char_type, traits_type>* rdbuf() const;
1148 bool is_open() const;
1149 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1150 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1151 void close();
1152
1153private:
1154 basic_filebuf<char_type, traits_type> __sb_;
1155};
1156
1157template <class _CharT, class _Traits>
1158inline _LIBCPP_INLINE_VISIBILITY
1159basic_ofstream<_CharT, _Traits>::basic_ofstream()
1160 : basic_ostream<char_type, traits_type>(&__sb_)
1161{
1162}
1163
1164template <class _CharT, class _Traits>
1165inline _LIBCPP_INLINE_VISIBILITY
1166basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1167 : basic_ostream<char_type, traits_type>(&__sb_)
1168{
1169 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1170 this->setstate(ios_base::failbit);
1171}
1172
1173template <class _CharT, class _Traits>
1174inline _LIBCPP_INLINE_VISIBILITY
1175basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1176 : basic_ostream<char_type, traits_type>(&__sb_)
1177{
1178 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1179 this->setstate(ios_base::failbit);
1180}
1181
Howard Hinnant73d21a42010-09-04 23:28:19 +00001182#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184template <class _CharT, class _Traits>
1185inline _LIBCPP_INLINE_VISIBILITY
1186basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1188 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189{
1190 this->set_rdbuf(&__sb_);
1191}
1192
1193template <class _CharT, class _Traits>
1194inline _LIBCPP_INLINE_VISIBILITY
1195basic_ofstream<_CharT, _Traits>&
1196basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1197{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001198 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1199 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 return *this;
1201}
1202
Howard Hinnant73d21a42010-09-04 23:28:19 +00001203#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204
1205template <class _CharT, class _Traits>
1206inline _LIBCPP_INLINE_VISIBILITY
1207void
1208basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1209{
1210 basic_ostream<char_type, traits_type>::swap(__rhs);
1211 __sb_.swap(__rhs.__sb_);
1212}
1213
1214template <class _CharT, class _Traits>
1215inline _LIBCPP_INLINE_VISIBILITY
1216void
1217swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1218{
1219 __x.swap(__y);
1220}
1221
1222template <class _CharT, class _Traits>
1223inline _LIBCPP_INLINE_VISIBILITY
1224basic_filebuf<_CharT, _Traits>*
1225basic_ofstream<_CharT, _Traits>::rdbuf() const
1226{
1227 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1228}
1229
1230template <class _CharT, class _Traits>
1231inline _LIBCPP_INLINE_VISIBILITY
1232bool
1233basic_ofstream<_CharT, _Traits>::is_open() const
1234{
1235 return __sb_.is_open();
1236}
1237
1238template <class _CharT, class _Traits>
1239void
1240basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1241{
1242 if (__sb_.open(__s, __mode | ios_base::out))
1243 this->clear();
1244 else
1245 this->setstate(ios_base::failbit);
1246}
1247
1248template <class _CharT, class _Traits>
1249void
1250basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1251{
1252 if (__sb_.open(__s, __mode | ios_base::out))
1253 this->clear();
1254 else
1255 this->setstate(ios_base::failbit);
1256}
1257
1258template <class _CharT, class _Traits>
1259inline _LIBCPP_INLINE_VISIBILITY
1260void
1261basic_ofstream<_CharT, _Traits>::close()
1262{
1263 if (__sb_.close() == 0)
1264 this->setstate(ios_base::failbit);
1265}
1266
1267// basic_fstream
1268
1269template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001270class _LIBCPP_VISIBLE basic_fstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 : public basic_iostream<_CharT, _Traits>
1272{
1273public:
1274 typedef _CharT char_type;
1275 typedef _Traits traits_type;
1276 typedef typename traits_type::int_type int_type;
1277 typedef typename traits_type::pos_type pos_type;
1278 typedef typename traits_type::off_type off_type;
1279
1280 basic_fstream();
1281 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1282 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001283#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 basic_fstream(basic_fstream&& __rhs);
1285#endif
1286
Howard Hinnant73d21a42010-09-04 23:28:19 +00001287#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 basic_fstream& operator=(basic_fstream&& __rhs);
1289#endif
1290 void swap(basic_fstream& __rhs);
1291
1292 basic_filebuf<char_type, traits_type>* rdbuf() const;
1293 bool is_open() const;
1294 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1295 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1296 void close();
1297
1298private:
1299 basic_filebuf<char_type, traits_type> __sb_;
1300};
1301
1302template <class _CharT, class _Traits>
1303inline _LIBCPP_INLINE_VISIBILITY
1304basic_fstream<_CharT, _Traits>::basic_fstream()
1305 : basic_iostream<char_type, traits_type>(&__sb_)
1306{
1307}
1308
1309template <class _CharT, class _Traits>
1310inline _LIBCPP_INLINE_VISIBILITY
1311basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1312 : basic_iostream<char_type, traits_type>(&__sb_)
1313{
1314 if (__sb_.open(__s, __mode) == 0)
1315 this->setstate(ios_base::failbit);
1316}
1317
1318template <class _CharT, class _Traits>
1319inline _LIBCPP_INLINE_VISIBILITY
1320basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1321 : basic_iostream<char_type, traits_type>(&__sb_)
1322{
1323 if (__sb_.open(__s, __mode) == 0)
1324 this->setstate(ios_base::failbit);
1325}
1326
Howard Hinnant73d21a42010-09-04 23:28:19 +00001327#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328
1329template <class _CharT, class _Traits>
1330inline _LIBCPP_INLINE_VISIBILITY
1331basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001332 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1333 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334{
1335 this->set_rdbuf(&__sb_);
1336}
1337
1338template <class _CharT, class _Traits>
1339inline _LIBCPP_INLINE_VISIBILITY
1340basic_fstream<_CharT, _Traits>&
1341basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1342{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001343 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1344 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 return *this;
1346}
1347
Howard Hinnant73d21a42010-09-04 23:28:19 +00001348#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349
1350template <class _CharT, class _Traits>
1351inline _LIBCPP_INLINE_VISIBILITY
1352void
1353basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1354{
1355 basic_iostream<char_type, traits_type>::swap(__rhs);
1356 __sb_.swap(__rhs.__sb_);
1357}
1358
1359template <class _CharT, class _Traits>
1360inline _LIBCPP_INLINE_VISIBILITY
1361void
1362swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1363{
1364 __x.swap(__y);
1365}
1366
1367template <class _CharT, class _Traits>
1368inline _LIBCPP_INLINE_VISIBILITY
1369basic_filebuf<_CharT, _Traits>*
1370basic_fstream<_CharT, _Traits>::rdbuf() const
1371{
1372 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1373}
1374
1375template <class _CharT, class _Traits>
1376inline _LIBCPP_INLINE_VISIBILITY
1377bool
1378basic_fstream<_CharT, _Traits>::is_open() const
1379{
1380 return __sb_.is_open();
1381}
1382
1383template <class _CharT, class _Traits>
1384void
1385basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1386{
1387 if (__sb_.open(__s, __mode))
1388 this->clear();
1389 else
1390 this->setstate(ios_base::failbit);
1391}
1392
1393template <class _CharT, class _Traits>
1394void
1395basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1396{
1397 if (__sb_.open(__s, __mode))
1398 this->clear();
1399 else
1400 this->setstate(ios_base::failbit);
1401}
1402
1403template <class _CharT, class _Traits>
1404inline _LIBCPP_INLINE_VISIBILITY
1405void
1406basic_fstream<_CharT, _Traits>::close()
1407{
1408 if (__sb_.close() == 0)
1409 this->setstate(ios_base::failbit);
1410}
1411
1412_LIBCPP_END_NAMESPACE_STD
1413
1414#endif // _LIBCPP_FSTREAM