blob: b6a2ab0e158a7e409899e9aaf5ab558973da0b90 [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 Hinnant08e17472011-10-17 20:05:10 +0000174#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000176#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177
178_LIBCPP_BEGIN_NAMESPACE_STD
179
180template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000181class _LIBCPP_VISIBLE basic_filebuf
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 : public basic_streambuf<_CharT, _Traits>
183{
184public:
185 typedef _CharT char_type;
186 typedef _Traits traits_type;
187 typedef typename traits_type::int_type int_type;
188 typedef typename traits_type::pos_type pos_type;
189 typedef typename traits_type::off_type off_type;
190 typedef typename traits_type::state_type state_type;
191
192 // 27.9.1.2 Constructors/destructor:
193 basic_filebuf();
Howard Hinnant73d21a42010-09-04 23:28:19 +0000194#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 basic_filebuf(basic_filebuf&& __rhs);
196#endif
197 virtual ~basic_filebuf();
198
199 // 27.9.1.3 Assign/swap:
Howard Hinnant73d21a42010-09-04 23:28:19 +0000200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201 basic_filebuf& operator=(basic_filebuf&& __rhs);
202#endif
203 void swap(basic_filebuf& __rhs);
204
205 // 27.9.1.4 Members:
206 bool is_open() const;
207 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
208 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
209 basic_filebuf* close();
210
211protected:
212 // 27.9.1.5 Overridden virtual functions:
213 virtual int_type underflow();
214 virtual int_type pbackfail(int_type __c = traits_type::eof());
215 virtual int_type overflow (int_type __c = traits_type::eof());
216 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
217 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
218 ios_base::openmode __wch = ios_base::in | ios_base::out);
219 virtual pos_type seekpos(pos_type __sp,
220 ios_base::openmode __wch = ios_base::in | ios_base::out);
221 virtual int sync();
222 virtual void imbue(const locale& __loc);
223
224private:
225 char* __extbuf_;
226 const char* __extbufnext_;
227 const char* __extbufend_;
228 char __extbuf_min_[8];
229 size_t __ebs_;
230 char_type* __intbuf_;
231 size_t __ibs_;
232 FILE* __file_;
233 const codecvt<char_type, char, state_type>* __cv_;
234 state_type __st_;
235 ios_base::openmode __om_;
236 ios_base::openmode __cm_;
237 bool __owns_eb_;
238 bool __owns_ib_;
239 bool __always_noconv_;
240
241 bool __read_mode();
242 void __write_mode();
243};
244
245template <class _CharT, class _Traits>
246basic_filebuf<_CharT, _Traits>::basic_filebuf()
247 : __extbuf_(0),
248 __extbufnext_(0),
249 __extbufend_(0),
250 __ebs_(0),
251 __intbuf_(0),
252 __ibs_(0),
253 __file_(0),
254 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
255 __st_(),
256 __om_(0),
257 __cm_(0),
258 __owns_eb_(false),
259 __owns_ib_(false),
260 __always_noconv_(__cv_->always_noconv())
261{
262 setbuf(0, 4096);
263}
264
Howard Hinnant73d21a42010-09-04 23:28:19 +0000265#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
267template <class _CharT, class _Traits>
268basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
269 : basic_streambuf<_CharT, _Traits>(__rhs)
270{
271 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
272 {
273 __extbuf_ = __extbuf_min_;
274 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
275 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
276 }
277 else
278 {
279 __extbuf_ = __rhs.__extbuf_;
280 __extbufnext_ = __rhs.__extbufnext_;
281 __extbufend_ = __rhs.__extbufend_;
282 }
283 __ebs_ = __rhs.__ebs_;
284 __intbuf_ = __rhs.__intbuf_;
285 __ibs_ = __rhs.__ibs_;
286 __file_ = __rhs.__file_;
287 __cv_ = __rhs.__cv_;
288 __st_ = __rhs.__st_;
289 __om_ = __rhs.__om_;
290 __cm_ = __rhs.__cm_;
291 __owns_eb_ = __rhs.__owns_eb_;
292 __owns_ib_ = __rhs.__owns_ib_;
293 __always_noconv_ = __rhs.__always_noconv_;
294 if (__rhs.pbase())
295 {
296 if (__rhs.pbase() == __rhs.__intbuf_)
297 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
298 else
299 this->setp((char_type*)__extbuf_,
300 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
301 this->pbump(__rhs. pptr() - __rhs.pbase());
302 }
303 else if (__rhs.eback())
304 {
305 if (__rhs.eback() == __rhs.__intbuf_)
306 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
307 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
308 else
309 this->setg((char_type*)__extbuf_,
310 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
311 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
312 }
313 __rhs.__extbuf_ = 0;
314 __rhs.__extbufnext_ = 0;
315 __rhs.__extbufend_ = 0;
316 __rhs.__ebs_ = 0;
317 __rhs.__intbuf_ = 0;
318 __rhs.__ibs_ = 0;
319 __rhs.__file_ = 0;
320 __rhs.__st_ = state_type();
321 __rhs.__om_ = 0;
322 __rhs.__cm_ = 0;
323 __rhs.__owns_eb_ = false;
324 __rhs.__owns_ib_ = false;
325 __rhs.setg(0, 0, 0);
326 __rhs.setp(0, 0);
327}
328
329template <class _CharT, class _Traits>
330inline _LIBCPP_INLINE_VISIBILITY
331basic_filebuf<_CharT, _Traits>&
332basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
333{
334 close();
335 swap(__rhs);
336}
337
Howard Hinnant73d21a42010-09-04 23:28:19 +0000338#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339
340template <class _CharT, class _Traits>
341basic_filebuf<_CharT, _Traits>::~basic_filebuf()
342{
343#ifndef _LIBCPP_NO_EXCEPTIONS
344 try
345 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000346#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 close();
348#ifndef _LIBCPP_NO_EXCEPTIONS
349 }
350 catch (...)
351 {
352 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000353#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354 if (__owns_eb_)
355 delete [] __extbuf_;
356 if (__owns_ib_)
357 delete [] __intbuf_;
358}
359
360template <class _CharT, class _Traits>
361void
362basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
363{
364 basic_streambuf<char_type, traits_type>::swap(__rhs);
365 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
366 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000367 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
368 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
369 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370 }
371 else
372 {
373 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
374 ptrdiff_t __le = __extbufend_ - __extbuf_;
375 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
376 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
377 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
378 {
379 __extbuf_ = __rhs.__extbuf_;
380 __rhs.__extbuf_ = __rhs.__extbuf_min_;
381 }
382 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
383 {
384 __rhs.__extbuf_ = __extbuf_;
385 __extbuf_ = __extbuf_min_;
386 }
387 __extbufnext_ = __extbuf_ + __rn;
388 __extbufend_ = __extbuf_ + __re;
389 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
390 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
391 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000392 _VSTD::swap(__ebs_, __rhs.__ebs_);
393 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
394 _VSTD::swap(__ibs_, __rhs.__ibs_);
395 _VSTD::swap(__file_, __rhs.__file_);
396 _VSTD::swap(__cv_, __rhs.__cv_);
397 _VSTD::swap(__st_, __rhs.__st_);
398 _VSTD::swap(__om_, __rhs.__om_);
399 _VSTD::swap(__cm_, __rhs.__cm_);
400 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
401 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
402 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
404 {
405 ptrdiff_t __n = this->gptr() - this->eback();
406 ptrdiff_t __e = this->egptr() - this->eback();
407 this->setg((char_type*)__extbuf_min_,
408 (char_type*)__extbuf_min_ + __n,
409 (char_type*)__extbuf_min_ + __e);
410 }
411 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
412 {
413 ptrdiff_t __n = this->pptr() - this->pbase();
414 ptrdiff_t __e = this->epptr() - this->pbase();
415 this->setp((char_type*)__extbuf_min_,
416 (char_type*)__extbuf_min_ + __e);
417 this->pbump(__n);
418 }
419 if (__rhs.eback() == (char_type*)__extbuf_min_)
420 {
421 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
422 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
423 __rhs.setg((char_type*)__rhs.__extbuf_min_,
424 (char_type*)__rhs.__extbuf_min_ + __n,
425 (char_type*)__rhs.__extbuf_min_ + __e);
426 }
427 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
428 {
429 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
430 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
431 __rhs.setp((char_type*)__rhs.__extbuf_min_,
432 (char_type*)__rhs.__extbuf_min_ + __e);
433 __rhs.pbump(__n);
434 }
435}
436
437template <class _CharT, class _Traits>
438inline _LIBCPP_INLINE_VISIBILITY
439void
440swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
441{
442 __x.swap(__y);
443}
444
445template <class _CharT, class _Traits>
446inline _LIBCPP_INLINE_VISIBILITY
447bool
448basic_filebuf<_CharT, _Traits>::is_open() const
449{
450 return __file_ != 0;
451}
452
453template <class _CharT, class _Traits>
454basic_filebuf<_CharT, _Traits>*
455basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
456{
457 basic_filebuf<_CharT, _Traits>* __rt = 0;
458 if (__file_ == 0)
459 {
460 __rt = this;
461 const char* __mdstr;
462 switch (__mode & ~ios_base::ate)
463 {
464 case ios_base::out:
465 case ios_base::out | ios_base::trunc:
466 __mdstr = "w";
467 break;
468 case ios_base::out | ios_base::app:
469 case ios_base::app:
470 __mdstr = "a";
471 break;
472 case ios_base::in:
473 __mdstr = "r";
474 break;
475 case ios_base::in | ios_base::out:
476 __mdstr = "r+";
477 break;
478 case ios_base::in | ios_base::out | ios_base::trunc:
479 __mdstr = "w+";
480 break;
481 case ios_base::in | ios_base::out | ios_base::app:
482 case ios_base::in | ios_base::app:
483 __mdstr = "a+";
484 break;
485 case ios_base::out | ios_base::binary:
486 case ios_base::out | ios_base::trunc | ios_base::binary:
487 __mdstr = "wb";
488 break;
489 case ios_base::out | ios_base::app | ios_base::binary:
490 case ios_base::app | ios_base::binary:
491 __mdstr = "ab";
492 break;
493 case ios_base::in | ios_base::binary:
494 __mdstr = "rb";
495 break;
496 case ios_base::in | ios_base::out | ios_base::binary:
497 __mdstr = "r+b";
498 break;
499 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
500 __mdstr = "w+b";
501 break;
502 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
503 case ios_base::in | ios_base::app | ios_base::binary:
504 __mdstr = "a+b";
505 break;
506 default:
507 __rt = 0;
508 break;
509 }
510 if (__rt)
511 {
512 __file_ = fopen(__s, __mdstr);
513 if (__file_)
514 {
515 __om_ = __mode;
516 if (__mode & ios_base::ate)
517 {
518 if (fseek(__file_, 0, SEEK_END))
519 {
520 fclose(__file_);
521 __file_ = 0;
522 __rt = 0;
523 }
524 }
525 }
526 else
527 __rt = 0;
528 }
529 }
530 return __rt;
531}
532
533template <class _CharT, class _Traits>
534inline _LIBCPP_INLINE_VISIBILITY
535basic_filebuf<_CharT, _Traits>*
536basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
537{
538 return open(__s.c_str(), __mode);
539}
540
541template <class _CharT, class _Traits>
542basic_filebuf<_CharT, _Traits>*
543basic_filebuf<_CharT, _Traits>::close()
544{
545 basic_filebuf<_CharT, _Traits>* __rt = 0;
546 if (__file_)
547 {
548 __rt = this;
549 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
550 if ((__cm_ & ios_base::out) && sync())
551 __rt = 0;
552 if (fclose(__h.release()) == 0)
553 __file_ = 0;
554 else
555 __rt = 0;
556 }
557 return __rt;
558}
559
560template <class _CharT, class _Traits>
561typename basic_filebuf<_CharT, _Traits>::int_type
562basic_filebuf<_CharT, _Traits>::underflow()
563{
564 if (__file_ == 0)
565 return traits_type::eof();
566 bool __initial = __read_mode();
567 char_type __1buf;
568 if (this->gptr() == 0)
569 this->setg(&__1buf, &__1buf+1, &__1buf+1);
570 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
571 int_type __c = traits_type::eof();
572 if (this->gptr() == this->egptr())
573 {
574 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
575 if (__always_noconv_)
576 {
577 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
578 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
579 if (__nmemb != 0)
580 {
581 this->setg(this->eback(),
582 this->eback() + __unget_sz,
583 this->eback() + __unget_sz + __nmemb);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000584 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 }
586 }
587 else
588 {
589 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
590 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
591 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000592 size_t __nmemb = _VSTD::min(static_cast<size_t>(this->egptr() - this->eback() - __unget_sz),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 static_cast<size_t>(__extbufend_ - __extbufnext_));
594 codecvt_base::result __r;
595 state_type __svs = __st_;
596 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
597 if (__nr != 0)
598 {
599 __extbufend_ = __extbufnext_ + __nr;
600 char_type* __inext;
601 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
602 this->eback() + __unget_sz,
603 this->egptr(), __inext);
604 if (__r == codecvt_base::noconv)
605 {
606 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000607 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 }
609 else if (__inext != this->eback() + __unget_sz)
610 {
611 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000612 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 }
614 }
615 }
616 }
617 else
Howard Hinnant47a7cce2011-02-02 17:37:16 +0000618 __c = traits_type::to_int_type(*this->gptr());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 if (this->eback() == &__1buf)
620 this->setg(0, 0, 0);
621 return __c;
622}
623
624template <class _CharT, class _Traits>
625typename basic_filebuf<_CharT, _Traits>::int_type
626basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
627{
628 if (__file_ && this->eback() < this->gptr())
629 {
630 if (traits_type::eq_int_type(__c, traits_type::eof()))
631 {
632 this->gbump(-1);
633 return traits_type::not_eof(__c);
634 }
635 if ((__om_ & ios_base::out) ||
636 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
637 {
638 this->gbump(-1);
639 *this->gptr() = traits_type::to_char_type(__c);
640 return __c;
641 }
642 }
643 return traits_type::eof();
644}
645
646template <class _CharT, class _Traits>
647typename basic_filebuf<_CharT, _Traits>::int_type
648basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
649{
650 if (__file_ == 0)
651 return traits_type::eof();
652 __write_mode();
653 char_type __1buf;
654 char_type* __pb_save = this->pbase();
655 char_type* __epb_save = this->epptr();
656 if (!traits_type::eq_int_type(__c, traits_type::eof()))
657 {
658 if (this->pptr() == 0)
659 this->setp(&__1buf, &__1buf+1);
660 *this->pptr() = traits_type::to_char_type(__c);
661 this->pbump(1);
662 }
663 if (this->pptr() != this->pbase())
664 {
665 if (__always_noconv_)
666 {
667 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
668 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
669 return traits_type::eof();
670 }
671 else
672 {
673 char* __extbe = __extbuf_;
674 codecvt_base::result __r;
675 do
676 {
677 const char_type* __e;
678 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
679 __extbuf_, __extbuf_ + __ebs_, __extbe);
680 if (__e == this->pbase())
681 return traits_type::eof();
682 if (__r == codecvt_base::noconv)
683 {
684 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
685 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
686 return traits_type::eof();
687 }
688 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
689 {
690 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
691 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
692 return traits_type::eof();
693 if (__r == codecvt_base::partial)
694 {
695 this->setp((char_type*)__e, this->pptr());
696 this->pbump(this->epptr() - this->pbase());
697 }
698 }
699 else
700 return traits_type::eof();
701 } while (__r == codecvt_base::partial);
702 }
703 this->setp(__pb_save, __epb_save);
704 }
705 return traits_type::not_eof(__c);
706}
707
708template <class _CharT, class _Traits>
709basic_streambuf<_CharT, _Traits>*
710basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
711{
712 this->setg(0, 0, 0);
713 this->setp(0, 0);
714 if (__owns_eb_)
715 delete [] __extbuf_;
716 if (__owns_ib_)
717 delete [] __intbuf_;
718 __ebs_ = __n;
719 if (__ebs_ > sizeof(__extbuf_min_))
720 {
721 if (__always_noconv_ && __s)
722 {
723 __extbuf_ = (char*)__s;
724 __owns_eb_ = false;
725 }
726 else
727 {
728 __extbuf_ = new char[__ebs_];
729 __owns_eb_ = true;
730 }
731 }
732 else
733 {
734 __extbuf_ = __extbuf_min_;
735 __ebs_ = sizeof(__extbuf_min_);
736 __owns_eb_ = false;
737 }
738 if (!__always_noconv_)
739 {
740 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
741 if (__s && __ibs_ >= sizeof(__extbuf_min_))
742 {
743 __intbuf_ = __s;
744 __owns_ib_ = false;
745 }
746 else
747 {
748 __intbuf_ = new char_type[__ibs_];
749 __owns_ib_ = true;
750 }
751 }
752 else
753 {
754 __ibs_ = 0;
755 __intbuf_ = 0;
756 __owns_ib_ = false;
757 }
758 return this;
759}
760
761template <class _CharT, class _Traits>
762typename basic_filebuf<_CharT, _Traits>::pos_type
763basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
764 ios_base::openmode)
765{
766 int __width = __cv_->encoding();
767 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
768 return pos_type(off_type(-1));
769 // __width > 0 || __off == 0
770 int __whence;
771 switch (__way)
772 {
773 case ios_base::beg:
774 __whence = SEEK_SET;
775 break;
776 case ios_base::cur:
777 __whence = SEEK_CUR;
778 break;
779 case ios_base::end:
780 __whence = SEEK_END;
781 break;
782 default:
783 return pos_type(off_type(-1));
784 }
785 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
786 return pos_type(off_type(-1));
787 pos_type __r = ftello(__file_);
788 __r.state(__st_);
789 return __r;
790}
791
792template <class _CharT, class _Traits>
793typename basic_filebuf<_CharT, _Traits>::pos_type
Howard Hinnant639a6682010-07-15 18:18:07 +0000794basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795{
796 if (__file_ == 0 || sync())
797 return pos_type(off_type(-1));
798 if (fseeko(__file_, __sp, SEEK_SET))
799 return pos_type(off_type(-1));
800 return __sp;
801}
802
803template <class _CharT, class _Traits>
804int
805basic_filebuf<_CharT, _Traits>::sync()
806{
807 if (__file_ == 0)
808 return 0;
809 if (__cm_ & ios_base::out)
810 {
811 if (this->pptr() != this->pbase())
812 if (overflow() == traits_type::eof())
813 return -1;
814 codecvt_base::result __r;
815 do
816 {
817 char* __extbe;
818 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
819 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
820 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
821 return -1;
822 } while (__r == codecvt_base::partial);
823 if (__r == codecvt_base::error)
824 return -1;
825 if (fflush(__file_))
826 return -1;
827 }
828 else if (__cm_ & ios_base::in)
829 {
830 off_type __c;
831 if (__always_noconv_)
832 __c = this->egptr() - this->gptr();
833 else
834 {
835 int __width = __cv_->encoding();
836 __c = __extbufend_ - __extbufnext_;
837 if (__width > 0)
838 __c += __width * (this->egptr() - this->gptr());
839 else
840 {
841 if (this->gptr() != this->egptr())
842 {
843 reverse(this->gptr(), this->egptr());
844 codecvt_base::result __r;
845 const char_type* __e = this->gptr();
846 char* __extbe;
847 do
848 {
849 __r = __cv_->out(__st_, __e, this->egptr(), __e,
850 __extbuf_, __extbuf_ + __ebs_, __extbe);
851 switch (__r)
852 {
853 case codecvt_base::noconv:
854 __c += this->egptr() - this->gptr();
855 break;
856 case codecvt_base::ok:
857 case codecvt_base::partial:
858 __c += __extbe - __extbuf_;
859 break;
860 default:
861 return -1;
862 }
863 } while (__r == codecvt_base::partial);
864 }
865 }
866 }
867 if (fseeko(__file_, -__c, SEEK_CUR))
868 return -1;
869 this->setg(0, 0, 0);
870 __cm_ = 0;
871 }
872 return 0;
873}
874
875template <class _CharT, class _Traits>
876void
877basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
878{
879 sync();
880 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
881 bool __old_anc = __always_noconv_;
882 __always_noconv_ = __cv_->always_noconv();
883 if (__old_anc != __always_noconv_)
884 {
885 this->setg(0, 0, 0);
886 this->setp(0, 0);
887 // invariant, char_type is char, else we couldn't get here
888 if (__always_noconv_) // need to dump __intbuf_
889 {
890 if (__owns_eb_)
891 delete [] __extbuf_;
892 __owns_eb_ = __owns_ib_;
893 __ebs_ = __ibs_;
894 __extbuf_ = (char*)__intbuf_;
895 __ibs_ = 0;
896 __intbuf_ = 0;
897 __owns_ib_ = false;
898 }
899 else // need to obtain an __intbuf_.
900 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
901 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
902 {
903 __ibs_ = __ebs_;
904 __intbuf_ = (char_type*)__extbuf_;
905 __owns_ib_ = false;
906 __extbuf_ = new char[__ebs_];
907 __owns_eb_ = true;
908 }
909 else
910 {
911 __ibs_ = __ebs_;
912 __intbuf_ = new char_type[__ibs_];
913 __owns_ib_ = true;
914 }
915 }
916 }
917}
918
919template <class _CharT, class _Traits>
920bool
921basic_filebuf<_CharT, _Traits>::__read_mode()
922{
923 if (!(__cm_ & ios_base::in))
924 {
925 this->setp(0, 0);
926 if (__always_noconv_)
927 this->setg((char_type*)__extbuf_,
928 (char_type*)__extbuf_ + __ebs_,
929 (char_type*)__extbuf_ + __ebs_);
930 else
931 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
932 __cm_ = ios_base::in;
933 return true;
934 }
935 return false;
936}
937
938template <class _CharT, class _Traits>
939void
940basic_filebuf<_CharT, _Traits>::__write_mode()
941{
942 if (!(__cm_ & ios_base::out))
943 {
944 this->setg(0, 0, 0);
945 if (__ebs_ > sizeof(__extbuf_min_))
946 {
947 if (__always_noconv_)
948 this->setp((char_type*)__extbuf_,
949 (char_type*)__extbuf_ + (__ebs_ - 1));
950 else
951 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
952 }
953 else
954 this->setp(0, 0);
955 __cm_ = ios_base::out;
956 }
957}
958
959// basic_ifstream
960
961template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +0000962class _LIBCPP_VISIBLE basic_ifstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963 : public basic_istream<_CharT, _Traits>
964{
965public:
966 typedef _CharT char_type;
967 typedef _Traits traits_type;
968 typedef typename traits_type::int_type int_type;
969 typedef typename traits_type::pos_type pos_type;
970 typedef typename traits_type::off_type off_type;
971
972 basic_ifstream();
973 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
974 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000975#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 basic_ifstream(basic_ifstream&& __rhs);
977#endif
978
Howard Hinnant73d21a42010-09-04 23:28:19 +0000979#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980 basic_ifstream& operator=(basic_ifstream&& __rhs);
981#endif
982 void swap(basic_ifstream& __rhs);
983
984 basic_filebuf<char_type, traits_type>* rdbuf() const;
985 bool is_open() const;
986 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
987 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
988 void close();
989
990private:
991 basic_filebuf<char_type, traits_type> __sb_;
992};
993
994template <class _CharT, class _Traits>
995inline _LIBCPP_INLINE_VISIBILITY
996basic_ifstream<_CharT, _Traits>::basic_ifstream()
997 : basic_istream<char_type, traits_type>(&__sb_)
998{
999}
1000
1001template <class _CharT, class _Traits>
1002inline _LIBCPP_INLINE_VISIBILITY
1003basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1004 : basic_istream<char_type, traits_type>(&__sb_)
1005{
1006 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1007 this->setstate(ios_base::failbit);
1008}
1009
1010template <class _CharT, class _Traits>
1011inline _LIBCPP_INLINE_VISIBILITY
1012basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1013 : basic_istream<char_type, traits_type>(&__sb_)
1014{
1015 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1016 this->setstate(ios_base::failbit);
1017}
1018
Howard Hinnant73d21a42010-09-04 23:28:19 +00001019#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020
1021template <class _CharT, class _Traits>
1022inline _LIBCPP_INLINE_VISIBILITY
1023basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001024 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1025 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026{
1027 this->set_rdbuf(&__sb_);
1028}
1029
1030template <class _CharT, class _Traits>
1031inline _LIBCPP_INLINE_VISIBILITY
1032basic_ifstream<_CharT, _Traits>&
1033basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1034{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001035 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1036 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 return *this;
1038}
1039
Howard Hinnant73d21a42010-09-04 23:28:19 +00001040#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041
1042template <class _CharT, class _Traits>
1043inline _LIBCPP_INLINE_VISIBILITY
1044void
1045basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1046{
1047 basic_istream<char_type, traits_type>::swap(__rhs);
1048 __sb_.swap(__rhs.__sb_);
1049}
1050
1051template <class _CharT, class _Traits>
1052inline _LIBCPP_INLINE_VISIBILITY
1053void
1054swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1055{
1056 __x.swap(__y);
1057}
1058
1059template <class _CharT, class _Traits>
1060inline _LIBCPP_INLINE_VISIBILITY
1061basic_filebuf<_CharT, _Traits>*
1062basic_ifstream<_CharT, _Traits>::rdbuf() const
1063{
1064 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1065}
1066
1067template <class _CharT, class _Traits>
1068inline _LIBCPP_INLINE_VISIBILITY
1069bool
1070basic_ifstream<_CharT, _Traits>::is_open() const
1071{
1072 return __sb_.is_open();
1073}
1074
1075template <class _CharT, class _Traits>
1076void
1077basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1078{
1079 if (__sb_.open(__s, __mode | ios_base::in))
1080 this->clear();
1081 else
1082 this->setstate(ios_base::failbit);
1083}
1084
1085template <class _CharT, class _Traits>
1086void
1087basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1088{
1089 if (__sb_.open(__s, __mode | ios_base::in))
1090 this->clear();
1091 else
1092 this->setstate(ios_base::failbit);
1093}
1094
1095template <class _CharT, class _Traits>
1096inline _LIBCPP_INLINE_VISIBILITY
1097void
1098basic_ifstream<_CharT, _Traits>::close()
1099{
1100 if (__sb_.close() == 0)
1101 this->setstate(ios_base::failbit);
1102}
1103
1104// basic_ofstream
1105
1106template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001107class _LIBCPP_VISIBLE basic_ofstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108 : public basic_ostream<_CharT, _Traits>
1109{
1110public:
1111 typedef _CharT char_type;
1112 typedef _Traits traits_type;
1113 typedef typename traits_type::int_type int_type;
1114 typedef typename traits_type::pos_type pos_type;
1115 typedef typename traits_type::off_type off_type;
1116
1117 basic_ofstream();
1118 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1119 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121 basic_ofstream(basic_ofstream&& __rhs);
1122#endif
1123
Howard Hinnant73d21a42010-09-04 23:28:19 +00001124#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 basic_ofstream& operator=(basic_ofstream&& __rhs);
1126#endif
1127 void swap(basic_ofstream& __rhs);
1128
1129 basic_filebuf<char_type, traits_type>* rdbuf() const;
1130 bool is_open() const;
1131 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1132 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1133 void close();
1134
1135private:
1136 basic_filebuf<char_type, traits_type> __sb_;
1137};
1138
1139template <class _CharT, class _Traits>
1140inline _LIBCPP_INLINE_VISIBILITY
1141basic_ofstream<_CharT, _Traits>::basic_ofstream()
1142 : basic_ostream<char_type, traits_type>(&__sb_)
1143{
1144}
1145
1146template <class _CharT, class _Traits>
1147inline _LIBCPP_INLINE_VISIBILITY
1148basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1149 : basic_ostream<char_type, traits_type>(&__sb_)
1150{
1151 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1152 this->setstate(ios_base::failbit);
1153}
1154
1155template <class _CharT, class _Traits>
1156inline _LIBCPP_INLINE_VISIBILITY
1157basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1158 : basic_ostream<char_type, traits_type>(&__sb_)
1159{
1160 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1161 this->setstate(ios_base::failbit);
1162}
1163
Howard Hinnant73d21a42010-09-04 23:28:19 +00001164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165
1166template <class _CharT, class _Traits>
1167inline _LIBCPP_INLINE_VISIBILITY
1168basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001169 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1170 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171{
1172 this->set_rdbuf(&__sb_);
1173}
1174
1175template <class _CharT, class _Traits>
1176inline _LIBCPP_INLINE_VISIBILITY
1177basic_ofstream<_CharT, _Traits>&
1178basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1179{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001180 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1181 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 return *this;
1183}
1184
Howard Hinnant73d21a42010-09-04 23:28:19 +00001185#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186
1187template <class _CharT, class _Traits>
1188inline _LIBCPP_INLINE_VISIBILITY
1189void
1190basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1191{
1192 basic_ostream<char_type, traits_type>::swap(__rhs);
1193 __sb_.swap(__rhs.__sb_);
1194}
1195
1196template <class _CharT, class _Traits>
1197inline _LIBCPP_INLINE_VISIBILITY
1198void
1199swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1200{
1201 __x.swap(__y);
1202}
1203
1204template <class _CharT, class _Traits>
1205inline _LIBCPP_INLINE_VISIBILITY
1206basic_filebuf<_CharT, _Traits>*
1207basic_ofstream<_CharT, _Traits>::rdbuf() const
1208{
1209 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1210}
1211
1212template <class _CharT, class _Traits>
1213inline _LIBCPP_INLINE_VISIBILITY
1214bool
1215basic_ofstream<_CharT, _Traits>::is_open() const
1216{
1217 return __sb_.is_open();
1218}
1219
1220template <class _CharT, class _Traits>
1221void
1222basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1223{
1224 if (__sb_.open(__s, __mode | ios_base::out))
1225 this->clear();
1226 else
1227 this->setstate(ios_base::failbit);
1228}
1229
1230template <class _CharT, class _Traits>
1231void
1232basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1233{
1234 if (__sb_.open(__s, __mode | ios_base::out))
1235 this->clear();
1236 else
1237 this->setstate(ios_base::failbit);
1238}
1239
1240template <class _CharT, class _Traits>
1241inline _LIBCPP_INLINE_VISIBILITY
1242void
1243basic_ofstream<_CharT, _Traits>::close()
1244{
1245 if (__sb_.close() == 0)
1246 this->setstate(ios_base::failbit);
1247}
1248
1249// basic_fstream
1250
1251template <class _CharT, class _Traits>
Howard Hinnant42a63a72010-09-21 22:55:27 +00001252class _LIBCPP_VISIBLE basic_fstream
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253 : public basic_iostream<_CharT, _Traits>
1254{
1255public:
1256 typedef _CharT char_type;
1257 typedef _Traits traits_type;
1258 typedef typename traits_type::int_type int_type;
1259 typedef typename traits_type::pos_type pos_type;
1260 typedef typename traits_type::off_type off_type;
1261
1262 basic_fstream();
1263 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1264 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001265#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 basic_fstream(basic_fstream&& __rhs);
1267#endif
1268
Howard Hinnant73d21a42010-09-04 23:28:19 +00001269#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270 basic_fstream& operator=(basic_fstream&& __rhs);
1271#endif
1272 void swap(basic_fstream& __rhs);
1273
1274 basic_filebuf<char_type, traits_type>* rdbuf() const;
1275 bool is_open() const;
1276 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1277 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1278 void close();
1279
1280private:
1281 basic_filebuf<char_type, traits_type> __sb_;
1282};
1283
1284template <class _CharT, class _Traits>
1285inline _LIBCPP_INLINE_VISIBILITY
1286basic_fstream<_CharT, _Traits>::basic_fstream()
1287 : basic_iostream<char_type, traits_type>(&__sb_)
1288{
1289}
1290
1291template <class _CharT, class _Traits>
1292inline _LIBCPP_INLINE_VISIBILITY
1293basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1294 : basic_iostream<char_type, traits_type>(&__sb_)
1295{
1296 if (__sb_.open(__s, __mode) == 0)
1297 this->setstate(ios_base::failbit);
1298}
1299
1300template <class _CharT, class _Traits>
1301inline _LIBCPP_INLINE_VISIBILITY
1302basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1303 : basic_iostream<char_type, traits_type>(&__sb_)
1304{
1305 if (__sb_.open(__s, __mode) == 0)
1306 this->setstate(ios_base::failbit);
1307}
1308
Howard Hinnant73d21a42010-09-04 23:28:19 +00001309#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310
1311template <class _CharT, class _Traits>
1312inline _LIBCPP_INLINE_VISIBILITY
1313basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001314 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1315 __sb_(_VSTD::move(__rhs.__sb_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316{
1317 this->set_rdbuf(&__sb_);
1318}
1319
1320template <class _CharT, class _Traits>
1321inline _LIBCPP_INLINE_VISIBILITY
1322basic_fstream<_CharT, _Traits>&
1323basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1324{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001325 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1326 __sb_ = _VSTD::move(__rhs.__sb_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327 return *this;
1328}
1329
Howard Hinnant73d21a42010-09-04 23:28:19 +00001330#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331
1332template <class _CharT, class _Traits>
1333inline _LIBCPP_INLINE_VISIBILITY
1334void
1335basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1336{
1337 basic_iostream<char_type, traits_type>::swap(__rhs);
1338 __sb_.swap(__rhs.__sb_);
1339}
1340
1341template <class _CharT, class _Traits>
1342inline _LIBCPP_INLINE_VISIBILITY
1343void
1344swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1345{
1346 __x.swap(__y);
1347}
1348
1349template <class _CharT, class _Traits>
1350inline _LIBCPP_INLINE_VISIBILITY
1351basic_filebuf<_CharT, _Traits>*
1352basic_fstream<_CharT, _Traits>::rdbuf() const
1353{
1354 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1355}
1356
1357template <class _CharT, class _Traits>
1358inline _LIBCPP_INLINE_VISIBILITY
1359bool
1360basic_fstream<_CharT, _Traits>::is_open() const
1361{
1362 return __sb_.is_open();
1363}
1364
1365template <class _CharT, class _Traits>
1366void
1367basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1368{
1369 if (__sb_.open(__s, __mode))
1370 this->clear();
1371 else
1372 this->setstate(ios_base::failbit);
1373}
1374
1375template <class _CharT, class _Traits>
1376void
1377basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1378{
1379 if (__sb_.open(__s, __mode))
1380 this->clear();
1381 else
1382 this->setstate(ios_base::failbit);
1383}
1384
1385template <class _CharT, class _Traits>
1386inline _LIBCPP_INLINE_VISIBILITY
1387void
1388basic_fstream<_CharT, _Traits>::close()
1389{
1390 if (__sb_.close() == 0)
1391 this->setstate(ios_base::failbit);
1392}
1393
1394_LIBCPP_END_NAMESPACE_STD
1395
1396#endif // _LIBCPP_FSTREAM