83731a1a16d4c895aa38aaf3fe1daabcf87c8b9e
[lldb.git] / libcxx / include / fstream
1 // -*- C++ -*-
2 //===------------------------- fstream ------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_FSTREAM
12 #define _LIBCPP_FSTREAM
13
14 /*
15     fstream synopsis
16
17 template <class charT, class traits = char_traits<charT> >
18 class basic_filebuf
19     : public basic_streambuf<charT, traits>
20 {
21 public:
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
43 protected:
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
59 template <class charT, class traits>
60   void
61   swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63 typedef basic_filebuf<char>    filebuf;
64 typedef basic_filebuf<wchar_t> wfilebuf;
65
66 template <class charT, class traits = char_traits<charT> >
67 class basic_ifstream
68     : public basic_istream<charT,traits>
69 {
70 public:
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
92 template <class charT, class traits>
93   void
94   swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
95
96 typedef basic_ifstream<char>    ifstream;
97 typedef basic_ifstream<wchar_t> wifstream;
98
99 template <class charT, class traits = char_traits<charT> >
100 class basic_ofstream
101     : public basic_ostream<charT,traits>
102 {
103 public:
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
125 template <class charT, class traits>
126   void
127   swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
128
129 typedef basic_ofstream<char>    ofstream;
130 typedef basic_ofstream<wchar_t> wofstream;
131
132 template <class charT, class traits=char_traits<charT> >
133 class basic_fstream
134     : public basic_iostream<charT,traits>
135 {
136 public:
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
158 template <class charT, class traits>
159   void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
160
161 typedef basic_fstream<char>    fstream;
162 typedef 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
174 #include <__undef_min_max>
175
176 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
177 #pragma GCC system_header
178 #endif
179
180 _LIBCPP_BEGIN_NAMESPACE_STD
181
182 template <class _CharT, class _Traits>
183 class _LIBCPP_VISIBLE basic_filebuf
184     : public basic_streambuf<_CharT, _Traits>
185 {
186 public:
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();
196 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
197     basic_filebuf(basic_filebuf&& __rhs);
198 #endif
199     virtual ~basic_filebuf();
200
201     // 27.9.1.3 Assign/swap:
202 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
203     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
213 protected:
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
226 private:
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_;
237     ios_base::openmode __om_;
238     ios_base::openmode __cm_;
239     bool __owns_eb_;
240     bool __owns_ib_;
241     bool __always_noconv_;
242
243     bool __read_mode();
244     void __write_mode();
245 };
246
247 template <class _CharT, class _Traits>
248 basic_filebuf<_CharT, _Traits>::basic_filebuf()
249     : __extbuf_(0),
250       __extbufnext_(0),
251       __extbufend_(0),
252       __ebs_(0),
253       __intbuf_(0),
254       __ibs_(0),
255       __file_(0),
256       __cv_(nullptr),
257       __st_(),
258       __om_(0),
259       __cm_(0),
260       __owns_eb_(false),
261       __owns_ib_(false),
262       __always_noconv_(false)
263 {
264     if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
265     {
266         __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
267         __always_noconv_ = __cv_->always_noconv();
268     }
269     setbuf(0, 4096);
270 }
271
272 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
273
274 template <class _CharT, class _Traits>
275 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
276     : basic_streambuf<_CharT, _Traits>(__rhs)
277 {
278     if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
279     {
280         __extbuf_ = __extbuf_min_;
281         __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
282         __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
283     }
284     else
285     {
286         __extbuf_ = __rhs.__extbuf_;
287         __extbufnext_ = __rhs.__extbufnext_;
288         __extbufend_ = __rhs.__extbufend_;
289     }
290     __ebs_ = __rhs.__ebs_;
291     __intbuf_ = __rhs.__intbuf_;
292     __ibs_ = __rhs.__ibs_;
293     __file_ = __rhs.__file_;
294     __cv_ = __rhs.__cv_;
295     __st_ = __rhs.__st_;
296     __om_ = __rhs.__om_;
297     __cm_ = __rhs.__cm_;
298     __owns_eb_ = __rhs.__owns_eb_;
299     __owns_ib_ = __rhs.__owns_ib_;
300     __always_noconv_ = __rhs.__always_noconv_;
301     if (__rhs.pbase())
302     {
303         if (__rhs.pbase() == __rhs.__intbuf_)
304             this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
305         else
306             this->setp((char_type*)__extbuf_,
307                        (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
308         this->pbump(__rhs. pptr() - __rhs.pbase());
309     }
310     else if (__rhs.eback())
311     {
312         if (__rhs.eback() == __rhs.__intbuf_)
313             this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
314                                   __intbuf_ + (__rhs.egptr() - __rhs.eback()));
315         else
316             this->setg((char_type*)__extbuf_,
317                        (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
318                        (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
319     }
320     __rhs.__extbuf_ = 0;
321     __rhs.__extbufnext_ = 0;
322     __rhs.__extbufend_ = 0;
323     __rhs.__ebs_ = 0;
324     __rhs.__intbuf_ = 0;
325     __rhs.__ibs_ = 0;
326     __rhs.__file_ = 0;
327     __rhs.__st_ = state_type();
328     __rhs.__om_ = 0;
329     __rhs.__cm_ = 0;
330     __rhs.__owns_eb_ = false;
331     __rhs.__owns_ib_ = false;
332     __rhs.setg(0, 0, 0);
333     __rhs.setp(0, 0);
334 }
335
336 template <class _CharT, class _Traits>
337 inline _LIBCPP_INLINE_VISIBILITY
338 basic_filebuf<_CharT, _Traits>&
339 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
340 {
341     close();
342     swap(__rhs);
343 }
344
345 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
346
347 template <class _CharT, class _Traits>
348 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
349 {
350 #ifndef _LIBCPP_NO_EXCEPTIONS
351     try
352     {
353 #endif  // _LIBCPP_NO_EXCEPTIONS
354         close();
355 #ifndef _LIBCPP_NO_EXCEPTIONS
356     }
357     catch (...)
358     {
359     }
360 #endif  // _LIBCPP_NO_EXCEPTIONS
361     if (__owns_eb_)
362         delete [] __extbuf_;
363     if (__owns_ib_)
364         delete [] __intbuf_;
365 }
366
367 template <class _CharT, class _Traits>
368 void
369 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
370 {
371     basic_streambuf<char_type, traits_type>::swap(__rhs);
372     if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
373     {
374         _VSTD::swap(__extbuf_, __rhs.__extbuf_);
375         _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
376         _VSTD::swap(__extbufend_, __rhs.__extbufend_);
377     }
378     else
379     {
380         ptrdiff_t __ln = __extbufnext_ - __extbuf_;
381         ptrdiff_t __le = __extbufend_ - __extbuf_;
382         ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
383         ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
384         if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
385         {
386             __extbuf_ = __rhs.__extbuf_;
387             __rhs.__extbuf_ = __rhs.__extbuf_min_;
388         }
389         else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
390         {
391             __rhs.__extbuf_ = __extbuf_;
392             __extbuf_ = __extbuf_min_;
393         }
394         __extbufnext_ = __extbuf_ + __rn;
395         __extbufend_ = __extbuf_ + __re;
396         __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
397         __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
398     }
399     _VSTD::swap(__ebs_, __rhs.__ebs_);
400     _VSTD::swap(__intbuf_, __rhs.__intbuf_);
401     _VSTD::swap(__ibs_, __rhs.__ibs_);
402     _VSTD::swap(__file_, __rhs.__file_);
403     _VSTD::swap(__cv_, __rhs.__cv_);
404     _VSTD::swap(__st_, __rhs.__st_);
405     _VSTD::swap(__om_, __rhs.__om_);
406     _VSTD::swap(__cm_, __rhs.__cm_);
407     _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
408     _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
409     _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
410     if (this->eback() == (char_type*)__rhs.__extbuf_min_)
411     {
412         ptrdiff_t __n = this->gptr() - this->eback();
413         ptrdiff_t __e = this->egptr() - this->eback();
414         this->setg((char_type*)__extbuf_min_,
415                    (char_type*)__extbuf_min_ + __n,
416                    (char_type*)__extbuf_min_ + __e);
417     }
418     else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
419     {
420         ptrdiff_t __n = this->pptr() - this->pbase();
421         ptrdiff_t __e = this->epptr() - this->pbase();
422         this->setp((char_type*)__extbuf_min_,
423                    (char_type*)__extbuf_min_ + __e);
424         this->pbump(__n);
425     }
426     if (__rhs.eback() == (char_type*)__extbuf_min_)
427     {
428         ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
429         ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
430         __rhs.setg((char_type*)__rhs.__extbuf_min_,
431                    (char_type*)__rhs.__extbuf_min_ + __n,
432                    (char_type*)__rhs.__extbuf_min_ + __e);
433     }
434     else if (__rhs.pbase() == (char_type*)__extbuf_min_)
435     {
436         ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
437         ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
438         __rhs.setp((char_type*)__rhs.__extbuf_min_,
439                    (char_type*)__rhs.__extbuf_min_ + __e);
440         __rhs.pbump(__n);
441     }
442 }
443
444 template <class _CharT, class _Traits>
445 inline _LIBCPP_INLINE_VISIBILITY
446 void
447 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
448 {
449     __x.swap(__y);
450 }
451
452 template <class _CharT, class _Traits>
453 inline _LIBCPP_INLINE_VISIBILITY
454 bool
455 basic_filebuf<_CharT, _Traits>::is_open() const
456 {
457     return __file_ != 0;
458 }
459
460 template <class _CharT, class _Traits>
461 basic_filebuf<_CharT, _Traits>*
462 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
463 {
464     basic_filebuf<_CharT, _Traits>* __rt = 0;
465     if (__file_ == 0)
466     {
467         __rt = this;
468         const char* __mdstr;
469         switch (__mode & ~ios_base::ate)
470         {
471         case ios_base::out:
472         case ios_base::out | ios_base::trunc:
473             __mdstr = "w";
474             break;
475         case ios_base::out | ios_base::app:
476         case ios_base::app:
477             __mdstr = "a";
478             break;
479         case ios_base::in:
480             __mdstr = "r";
481             break;
482         case ios_base::in | ios_base::out:
483             __mdstr = "r+";
484             break;
485         case ios_base::in | ios_base::out | ios_base::trunc:
486             __mdstr = "w+";
487             break;
488         case ios_base::in | ios_base::out | ios_base::app:
489         case ios_base::in | ios_base::app:
490             __mdstr = "a+";
491             break;
492         case ios_base::out | ios_base::binary:
493         case ios_base::out | ios_base::trunc | ios_base::binary:
494             __mdstr = "wb";
495             break;
496         case ios_base::out | ios_base::app | ios_base::binary:
497         case ios_base::app | ios_base::binary:
498             __mdstr = "ab";
499             break;
500         case ios_base::in | ios_base::binary:
501             __mdstr = "rb";
502             break;
503         case ios_base::in | ios_base::out | ios_base::binary:
504             __mdstr = "r+b";
505             break;
506         case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
507             __mdstr = "w+b";
508             break;
509         case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
510         case ios_base::in | ios_base::app | ios_base::binary:
511             __mdstr = "a+b";
512             break;
513         default:
514             __rt = 0;
515             break;
516         }
517         if (__rt)
518         {
519             __file_ = fopen(__s, __mdstr);
520             if (__file_)
521             {
522                 __om_ = __mode;
523                 if (__mode & ios_base::ate)
524                 {
525                     if (fseek(__file_, 0, SEEK_END))
526                     {
527                         fclose(__file_);
528                         __file_ = 0;
529                         __rt = 0;
530                     }
531                 }
532             }
533             else
534                 __rt = 0;
535         }
536     }
537     return __rt;
538 }
539
540 template <class _CharT, class _Traits>
541 inline _LIBCPP_INLINE_VISIBILITY
542 basic_filebuf<_CharT, _Traits>*
543 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
544 {
545     return open(__s.c_str(), __mode);
546 }
547
548 template <class _CharT, class _Traits>
549 basic_filebuf<_CharT, _Traits>*
550 basic_filebuf<_CharT, _Traits>::close()
551 {
552     basic_filebuf<_CharT, _Traits>* __rt = 0;
553     if (__file_)
554     {
555         __rt = this;
556         unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
557         if (sync())
558             __rt = 0;
559         if (fclose(__h.release()) == 0)
560             __file_ = 0;
561         else
562             __rt = 0;
563     }
564     return __rt;
565 }
566
567 template <class _CharT, class _Traits>
568 typename basic_filebuf<_CharT, _Traits>::int_type
569 basic_filebuf<_CharT, _Traits>::underflow()
570 {
571     if (__file_ == 0)
572         return traits_type::eof();
573     bool __initial = __read_mode();
574     char_type __1buf;
575     if (this->gptr() == 0)
576         this->setg(&__1buf, &__1buf+1, &__1buf+1);
577     const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
578     int_type __c = traits_type::eof();
579     if (this->gptr() == this->egptr())
580     {
581         memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
582         if (__always_noconv_)
583         {
584             size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
585             __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
586             if (__nmemb != 0)
587             {
588                 this->setg(this->eback(),
589                            this->eback() + __unget_sz,
590                            this->eback() + __unget_sz + __nmemb);
591                 __c = traits_type::to_int_type(*this->gptr());
592             }
593         }
594         else
595         {
596             memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
597             __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
598             __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
599             size_t __nmemb = _VSTD::min(static_cast<size_t>(this->egptr() - this->eback() - __unget_sz),
600                                  static_cast<size_t>(__extbufend_ - __extbufnext_));
601             codecvt_base::result __r;
602             state_type __svs = __st_;
603             size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
604             if (__nr != 0)
605             {
606 #ifndef _LIBCPP_NO_EXCEPTIONS
607                 if (!__cv_)
608                     throw bad_cast();
609 #endif
610                 __extbufend_ = __extbufnext_ + __nr;
611                 char_type*  __inext;
612                 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
613                                        this->eback() + __unget_sz,
614                                        this->egptr(), __inext);
615                 if (__r == codecvt_base::noconv)
616                 {
617                     this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
618                     __c = traits_type::to_int_type(*this->gptr());
619                 }
620                 else if (__inext != this->eback() + __unget_sz)
621                 {
622                     this->setg(this->eback(), this->eback() + __unget_sz, __inext);
623                     __c = traits_type::to_int_type(*this->gptr());
624                 }
625             }
626         }
627     }
628     else
629         __c = traits_type::to_int_type(*this->gptr());
630     if (this->eback() == &__1buf)
631         this->setg(0, 0, 0);
632     return __c;
633 }
634
635 template <class _CharT, class _Traits>
636 typename basic_filebuf<_CharT, _Traits>::int_type
637 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
638 {
639     if (__file_ && this->eback() < this->gptr())
640     {
641         if (traits_type::eq_int_type(__c, traits_type::eof()))
642         {
643             this->gbump(-1);
644             return traits_type::not_eof(__c);
645         }
646         if ((__om_ & ios_base::out) ||
647             traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
648         {
649             this->gbump(-1);
650             *this->gptr() = traits_type::to_char_type(__c);
651             return __c;
652         }
653     }
654     return traits_type::eof();
655 }
656
657 template <class _CharT, class _Traits>
658 typename basic_filebuf<_CharT, _Traits>::int_type
659 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
660 {
661     if (__file_ == 0)
662         return traits_type::eof();
663     __write_mode();
664     char_type __1buf;
665     char_type* __pb_save = this->pbase();
666     char_type* __epb_save = this->epptr();
667     if (!traits_type::eq_int_type(__c, traits_type::eof()))
668     {
669         if (this->pptr() == 0)
670             this->setp(&__1buf, &__1buf+1);
671         *this->pptr() = traits_type::to_char_type(__c);
672         this->pbump(1);
673     }
674     if (this->pptr() != this->pbase())
675     {
676         if (__always_noconv_)
677         {
678             size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
679             if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
680                 return traits_type::eof();
681         }
682         else
683         {
684             char* __extbe = __extbuf_;
685             codecvt_base::result __r;
686             do
687             {
688 #ifndef _LIBCPP_NO_EXCEPTIONS
689                 if (!__cv_)
690                     throw bad_cast();
691 #endif
692                 const char_type* __e;
693                 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
694                                         __extbuf_, __extbuf_ + __ebs_, __extbe);
695                 if (__e == this->pbase())
696                     return traits_type::eof();
697                 if (__r == codecvt_base::noconv)
698                 {
699                     size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
700                     if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
701                         return traits_type::eof();
702                 }
703                 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
704                 {
705                     size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
706                     if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
707                         return traits_type::eof();
708                     if (__r == codecvt_base::partial)
709                     {
710                         this->setp((char_type*)__e, this->pptr());
711                         this->pbump(this->epptr() - this->pbase());
712                     }
713                 }
714                 else
715                     return traits_type::eof();
716             } while (__r == codecvt_base::partial);
717         }
718         this->setp(__pb_save, __epb_save);
719     }
720     return traits_type::not_eof(__c);
721 }
722
723 template <class _CharT, class _Traits>
724 basic_streambuf<_CharT, _Traits>*
725 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
726 {
727     this->setg(0, 0, 0);
728     this->setp(0, 0);
729     if (__owns_eb_)
730         delete [] __extbuf_;
731     if (__owns_ib_)
732         delete [] __intbuf_;
733     __ebs_ = __n;
734     if (__ebs_ > sizeof(__extbuf_min_))
735     {
736         if (__always_noconv_ && __s)
737         {
738             __extbuf_ = (char*)__s;
739             __owns_eb_ = false;
740         }
741         else
742         {
743             __extbuf_ = new char[__ebs_];
744             __owns_eb_ = true;
745         }
746     }
747     else
748     {
749         __extbuf_ = __extbuf_min_;
750         __ebs_ = sizeof(__extbuf_min_);
751         __owns_eb_ = false;
752     }
753     if (!__always_noconv_)
754     {
755         __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
756         if (__s && __ibs_ >= sizeof(__extbuf_min_))
757         {
758             __intbuf_ = __s;
759             __owns_ib_ = false;
760         }
761         else
762         {
763             __intbuf_ = new char_type[__ibs_];
764             __owns_ib_ = true;
765         }
766     }
767     else
768     {
769         __ibs_ = 0;
770         __intbuf_ = 0;
771         __owns_ib_ = false;
772     }
773     return this;
774 }
775
776 template <class _CharT, class _Traits>
777 typename basic_filebuf<_CharT, _Traits>::pos_type
778 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
779                                         ios_base::openmode)
780 {
781 #ifndef _LIBCPP_NO_EXCEPTIONS
782     if (!__cv_)
783         throw bad_cast();
784 #endif
785     int __width = __cv_->encoding();
786     if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
787         return pos_type(off_type(-1));
788     // __width > 0 || __off == 0
789     int __whence;
790     switch (__way)
791     {
792     case ios_base::beg:
793         __whence = SEEK_SET;
794         break;
795     case ios_base::cur:
796         __whence = SEEK_CUR;
797         break;
798     case ios_base::end:
799         __whence = SEEK_END;
800         break;
801     default:
802         return pos_type(off_type(-1));
803     }
804     if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
805         return pos_type(off_type(-1));
806     pos_type __r = ftello(__file_);
807     __r.state(__st_);
808     return __r;
809 }
810
811 template <class _CharT, class _Traits>
812 typename basic_filebuf<_CharT, _Traits>::pos_type
813 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
814 {
815     if (__file_ == 0 || sync())
816         return pos_type(off_type(-1));
817     if (fseeko(__file_, __sp, SEEK_SET))
818         return pos_type(off_type(-1));
819     return __sp;
820 }
821
822 template <class _CharT, class _Traits>
823 int
824 basic_filebuf<_CharT, _Traits>::sync()
825 {
826     if (__file_ == 0)
827         return 0;
828 #ifndef _LIBCPP_NO_EXCEPTIONS
829     if (!__cv_)
830         throw bad_cast();
831 #endif
832     if (__cm_ & ios_base::out)
833     {
834         if (this->pptr() != this->pbase())
835             if (overflow() == traits_type::eof())
836                 return -1;
837         codecvt_base::result __r;
838         do
839         {
840             char* __extbe;
841             __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
842             size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
843             if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
844                 return -1;
845         } while (__r == codecvt_base::partial);
846         if (__r == codecvt_base::error)
847             return -1;
848         if (fflush(__file_))
849             return -1;
850     }
851     else if (__cm_ & ios_base::in)
852     {
853         off_type __c;
854         if (__always_noconv_)
855             __c = this->egptr() - this->gptr();
856         else
857         {
858             int __width = __cv_->encoding();
859             __c = __extbufend_ - __extbufnext_;
860             if (__width > 0)
861                 __c += __width * (this->egptr() - this->gptr());
862             else
863             {
864                 if (this->gptr() != this->egptr())
865                 {
866                     reverse(this->gptr(), this->egptr());
867                     codecvt_base::result __r;
868                     const char_type* __e = this->gptr();
869                     char* __extbe;
870                     do
871                     {
872                         __r = __cv_->out(__st_, __e, this->egptr(), __e,
873                                          __extbuf_, __extbuf_ + __ebs_, __extbe);
874                         switch (__r)
875                         {
876                         case codecvt_base::noconv:
877                             __c += this->egptr() - this->gptr();
878                             break;
879                         case codecvt_base::ok:
880                         case codecvt_base::partial:
881                             __c += __extbe - __extbuf_;
882                             break;
883                         default:
884                             return -1;
885                         }
886                     } while (__r == codecvt_base::partial);
887                 }
888             }
889         }
890         if (fseeko(__file_, -__c, SEEK_CUR))
891             return -1;
892         this->setg(0, 0, 0);
893         __cm_ = 0;
894     }
895     return 0;
896 }
897
898 template <class _CharT, class _Traits>
899 void
900 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
901 {
902     sync();
903     __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
904     bool __old_anc = __always_noconv_;
905     __always_noconv_ = __cv_->always_noconv();
906     if (__old_anc != __always_noconv_)
907     {
908         this->setg(0, 0, 0);
909         this->setp(0, 0);
910         // invariant, char_type is char, else we couldn't get here
911         if (__always_noconv_)  // need to dump __intbuf_
912         {
913             if (__owns_eb_)
914                 delete [] __extbuf_;
915             __owns_eb_ = __owns_ib_;
916             __ebs_ = __ibs_;
917             __extbuf_ = (char*)__intbuf_;
918             __ibs_ = 0;
919             __intbuf_ = 0;
920             __owns_ib_ = false;
921         }
922         else  // need to obtain an __intbuf_.
923         {     // If __extbuf_ is user-supplied, use it, else new __intbuf_
924             if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
925             {
926                 __ibs_ = __ebs_;
927                 __intbuf_ = (char_type*)__extbuf_;
928                 __owns_ib_ = false;
929                 __extbuf_ = new char[__ebs_];
930                 __owns_eb_ = true;
931             }
932             else
933             {
934                 __ibs_ = __ebs_;
935                 __intbuf_ = new char_type[__ibs_];
936                 __owns_ib_ = true;
937             }
938         }
939     }
940 }
941
942 template <class _CharT, class _Traits>
943 bool
944 basic_filebuf<_CharT, _Traits>::__read_mode()
945 {
946     if (!(__cm_ & ios_base::in))
947     {
948         this->setp(0, 0);
949         if (__always_noconv_)
950             this->setg((char_type*)__extbuf_,
951                        (char_type*)__extbuf_ + __ebs_,
952                        (char_type*)__extbuf_ + __ebs_);
953         else
954             this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
955         __cm_ = ios_base::in;
956         return true;
957     }
958     return false;
959 }
960
961 template <class _CharT, class _Traits>
962 void
963 basic_filebuf<_CharT, _Traits>::__write_mode()
964 {
965     if (!(__cm_ & ios_base::out))
966     {
967         this->setg(0, 0, 0);
968         if (__ebs_ > sizeof(__extbuf_min_))
969         {
970             if (__always_noconv_)
971                 this->setp((char_type*)__extbuf_,
972                            (char_type*)__extbuf_ + (__ebs_ - 1));
973             else
974                 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
975         }
976         else
977             this->setp(0, 0);
978         __cm_ = ios_base::out;
979     }
980 }
981
982 // basic_ifstream
983
984 template <class _CharT, class _Traits>
985 class _LIBCPP_VISIBLE basic_ifstream
986     : public basic_istream<_CharT, _Traits>
987 {
988 public:
989     typedef _CharT                         char_type;
990     typedef _Traits                        traits_type;
991     typedef typename traits_type::int_type int_type;
992     typedef typename traits_type::pos_type pos_type;
993     typedef typename traits_type::off_type off_type;
994
995     basic_ifstream();
996     explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
997     explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
998 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
999     basic_ifstream(basic_ifstream&& __rhs);
1000 #endif
1001
1002 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1003     basic_ifstream& operator=(basic_ifstream&& __rhs);
1004 #endif
1005     void swap(basic_ifstream& __rhs);
1006
1007     basic_filebuf<char_type, traits_type>* rdbuf() const;
1008     bool is_open() const;
1009     void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1010     void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1011     void close();
1012
1013 private:
1014     basic_filebuf<char_type, traits_type> __sb_;
1015 };
1016
1017 template <class _CharT, class _Traits>
1018 inline _LIBCPP_INLINE_VISIBILITY
1019 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1020     : basic_istream<char_type, traits_type>(&__sb_)
1021 {
1022 }
1023
1024 template <class _CharT, class _Traits>
1025 inline _LIBCPP_INLINE_VISIBILITY
1026 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1027     : basic_istream<char_type, traits_type>(&__sb_)
1028 {
1029     if (__sb_.open(__s, __mode | ios_base::in) == 0)
1030         this->setstate(ios_base::failbit);
1031 }
1032
1033 template <class _CharT, class _Traits>
1034 inline _LIBCPP_INLINE_VISIBILITY
1035 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1036     : basic_istream<char_type, traits_type>(&__sb_)
1037 {
1038     if (__sb_.open(__s, __mode | ios_base::in) == 0)
1039         this->setstate(ios_base::failbit);
1040 }
1041
1042 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1043
1044 template <class _CharT, class _Traits>
1045 inline _LIBCPP_INLINE_VISIBILITY
1046 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1047     : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1048       __sb_(_VSTD::move(__rhs.__sb_))
1049 {
1050     this->set_rdbuf(&__sb_);
1051 }
1052
1053 template <class _CharT, class _Traits>
1054 inline _LIBCPP_INLINE_VISIBILITY
1055 basic_ifstream<_CharT, _Traits>&
1056 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1057 {
1058     basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1059     __sb_ = _VSTD::move(__rhs.__sb_);
1060     return *this;
1061 }
1062
1063 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1064
1065 template <class _CharT, class _Traits>
1066 inline _LIBCPP_INLINE_VISIBILITY
1067 void
1068 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1069 {
1070     basic_istream<char_type, traits_type>::swap(__rhs);
1071     __sb_.swap(__rhs.__sb_);
1072 }
1073
1074 template <class _CharT, class _Traits>
1075 inline _LIBCPP_INLINE_VISIBILITY
1076 void
1077 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1078 {
1079     __x.swap(__y);
1080 }
1081
1082 template <class _CharT, class _Traits>
1083 inline _LIBCPP_INLINE_VISIBILITY
1084 basic_filebuf<_CharT, _Traits>*
1085 basic_ifstream<_CharT, _Traits>::rdbuf() const
1086 {
1087     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1088 }
1089
1090 template <class _CharT, class _Traits>
1091 inline _LIBCPP_INLINE_VISIBILITY
1092 bool
1093 basic_ifstream<_CharT, _Traits>::is_open() const
1094 {
1095     return __sb_.is_open();
1096 }
1097
1098 template <class _CharT, class _Traits>
1099 void
1100 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1101 {
1102     if (__sb_.open(__s, __mode | ios_base::in))
1103         this->clear();
1104     else
1105         this->setstate(ios_base::failbit);
1106 }
1107
1108 template <class _CharT, class _Traits>
1109 void
1110 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1111 {
1112     if (__sb_.open(__s, __mode | ios_base::in))
1113         this->clear();
1114     else
1115         this->setstate(ios_base::failbit);
1116 }
1117
1118 template <class _CharT, class _Traits>
1119 inline _LIBCPP_INLINE_VISIBILITY
1120 void
1121 basic_ifstream<_CharT, _Traits>::close()
1122 {
1123     if (__sb_.close() == 0)
1124         this->setstate(ios_base::failbit);
1125 }
1126
1127 // basic_ofstream
1128
1129 template <class _CharT, class _Traits>
1130 class _LIBCPP_VISIBLE basic_ofstream
1131     : public basic_ostream<_CharT, _Traits>
1132 {
1133 public:
1134     typedef _CharT                         char_type;
1135     typedef _Traits                        traits_type;
1136     typedef typename traits_type::int_type int_type;
1137     typedef typename traits_type::pos_type pos_type;
1138     typedef typename traits_type::off_type off_type;
1139
1140     basic_ofstream();
1141     explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1142     explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1143 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1144     basic_ofstream(basic_ofstream&& __rhs);
1145 #endif
1146
1147 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1148     basic_ofstream& operator=(basic_ofstream&& __rhs);
1149 #endif
1150     void swap(basic_ofstream& __rhs);
1151
1152     basic_filebuf<char_type, traits_type>* rdbuf() const;
1153     bool is_open() const;
1154     void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1155     void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1156     void close();
1157
1158 private:
1159     basic_filebuf<char_type, traits_type> __sb_;
1160 };
1161
1162 template <class _CharT, class _Traits>
1163 inline _LIBCPP_INLINE_VISIBILITY
1164 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1165     : basic_ostream<char_type, traits_type>(&__sb_)
1166 {
1167 }
1168
1169 template <class _CharT, class _Traits>
1170 inline _LIBCPP_INLINE_VISIBILITY
1171 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1172     : basic_ostream<char_type, traits_type>(&__sb_)
1173 {
1174     if (__sb_.open(__s, __mode | ios_base::out) == 0)
1175         this->setstate(ios_base::failbit);
1176 }
1177
1178 template <class _CharT, class _Traits>
1179 inline _LIBCPP_INLINE_VISIBILITY
1180 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1181     : basic_ostream<char_type, traits_type>(&__sb_)
1182 {
1183     if (__sb_.open(__s, __mode | ios_base::out) == 0)
1184         this->setstate(ios_base::failbit);
1185 }
1186
1187 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1188
1189 template <class _CharT, class _Traits>
1190 inline _LIBCPP_INLINE_VISIBILITY
1191 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1192     : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1193       __sb_(_VSTD::move(__rhs.__sb_))
1194 {
1195     this->set_rdbuf(&__sb_);
1196 }
1197
1198 template <class _CharT, class _Traits>
1199 inline _LIBCPP_INLINE_VISIBILITY
1200 basic_ofstream<_CharT, _Traits>&
1201 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1202 {
1203     basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1204     __sb_ = _VSTD::move(__rhs.__sb_);
1205     return *this;
1206 }
1207
1208 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1209
1210 template <class _CharT, class _Traits>
1211 inline _LIBCPP_INLINE_VISIBILITY
1212 void
1213 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1214 {
1215     basic_ostream<char_type, traits_type>::swap(__rhs);
1216     __sb_.swap(__rhs.__sb_);
1217 }
1218
1219 template <class _CharT, class _Traits>
1220 inline _LIBCPP_INLINE_VISIBILITY
1221 void
1222 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1223 {
1224     __x.swap(__y);
1225 }
1226
1227 template <class _CharT, class _Traits>
1228 inline _LIBCPP_INLINE_VISIBILITY
1229 basic_filebuf<_CharT, _Traits>*
1230 basic_ofstream<_CharT, _Traits>::rdbuf() const
1231 {
1232     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1233 }
1234
1235 template <class _CharT, class _Traits>
1236 inline _LIBCPP_INLINE_VISIBILITY
1237 bool
1238 basic_ofstream<_CharT, _Traits>::is_open() const
1239 {
1240     return __sb_.is_open();
1241 }
1242
1243 template <class _CharT, class _Traits>
1244 void
1245 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1246 {
1247     if (__sb_.open(__s, __mode | ios_base::out))
1248         this->clear();
1249     else
1250         this->setstate(ios_base::failbit);
1251 }
1252
1253 template <class _CharT, class _Traits>
1254 void
1255 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1256 {
1257     if (__sb_.open(__s, __mode | ios_base::out))
1258         this->clear();
1259     else
1260         this->setstate(ios_base::failbit);
1261 }
1262
1263 template <class _CharT, class _Traits>
1264 inline _LIBCPP_INLINE_VISIBILITY
1265 void
1266 basic_ofstream<_CharT, _Traits>::close()
1267 {
1268     if (__sb_.close() == 0)
1269         this->setstate(ios_base::failbit);
1270 }
1271
1272 // basic_fstream
1273
1274 template <class _CharT, class _Traits>
1275 class _LIBCPP_VISIBLE basic_fstream
1276     : public basic_iostream<_CharT, _Traits>
1277 {
1278 public:
1279     typedef _CharT                         char_type;
1280     typedef _Traits                        traits_type;
1281     typedef typename traits_type::int_type int_type;
1282     typedef typename traits_type::pos_type pos_type;
1283     typedef typename traits_type::off_type off_type;
1284
1285     basic_fstream();
1286     explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1287     explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1288 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1289     basic_fstream(basic_fstream&& __rhs);
1290 #endif
1291
1292 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1293     basic_fstream& operator=(basic_fstream&& __rhs);
1294 #endif
1295     void swap(basic_fstream& __rhs);
1296
1297     basic_filebuf<char_type, traits_type>* rdbuf() const;
1298     bool is_open() const;
1299     void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1300     void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1301     void close();
1302
1303 private:
1304     basic_filebuf<char_type, traits_type> __sb_;
1305 };
1306
1307 template <class _CharT, class _Traits>
1308 inline _LIBCPP_INLINE_VISIBILITY
1309 basic_fstream<_CharT, _Traits>::basic_fstream()
1310     : basic_iostream<char_type, traits_type>(&__sb_)
1311 {
1312 }
1313
1314 template <class _CharT, class _Traits>
1315 inline _LIBCPP_INLINE_VISIBILITY
1316 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1317     : basic_iostream<char_type, traits_type>(&__sb_)
1318 {
1319     if (__sb_.open(__s, __mode) == 0)
1320         this->setstate(ios_base::failbit);
1321 }
1322
1323 template <class _CharT, class _Traits>
1324 inline _LIBCPP_INLINE_VISIBILITY
1325 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1326     : basic_iostream<char_type, traits_type>(&__sb_)
1327 {
1328     if (__sb_.open(__s, __mode) == 0)
1329         this->setstate(ios_base::failbit);
1330 }
1331
1332 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1333
1334 template <class _CharT, class _Traits>
1335 inline _LIBCPP_INLINE_VISIBILITY
1336 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1337     : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1338       __sb_(_VSTD::move(__rhs.__sb_))
1339 {
1340     this->set_rdbuf(&__sb_);
1341 }
1342
1343 template <class _CharT, class _Traits>
1344 inline _LIBCPP_INLINE_VISIBILITY
1345 basic_fstream<_CharT, _Traits>&
1346 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1347 {
1348     basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1349     __sb_ = _VSTD::move(__rhs.__sb_);
1350     return *this;
1351 }
1352
1353 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1354
1355 template <class _CharT, class _Traits>
1356 inline _LIBCPP_INLINE_VISIBILITY
1357 void
1358 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1359 {
1360     basic_iostream<char_type, traits_type>::swap(__rhs);
1361     __sb_.swap(__rhs.__sb_);
1362 }
1363
1364 template <class _CharT, class _Traits>
1365 inline _LIBCPP_INLINE_VISIBILITY
1366 void
1367 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1368 {
1369     __x.swap(__y);
1370 }
1371
1372 template <class _CharT, class _Traits>
1373 inline _LIBCPP_INLINE_VISIBILITY
1374 basic_filebuf<_CharT, _Traits>*
1375 basic_fstream<_CharT, _Traits>::rdbuf() const
1376 {
1377     return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1378 }
1379
1380 template <class _CharT, class _Traits>
1381 inline _LIBCPP_INLINE_VISIBILITY
1382 bool
1383 basic_fstream<_CharT, _Traits>::is_open() const
1384 {
1385     return __sb_.is_open();
1386 }
1387
1388 template <class _CharT, class _Traits>
1389 void
1390 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1391 {
1392     if (__sb_.open(__s, __mode))
1393         this->clear();
1394     else
1395         this->setstate(ios_base::failbit);
1396 }
1397
1398 template <class _CharT, class _Traits>
1399 void
1400 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1401 {
1402     if (__sb_.open(__s, __mode))
1403         this->clear();
1404     else
1405         this->setstate(ios_base::failbit);
1406 }
1407
1408 template <class _CharT, class _Traits>
1409 inline _LIBCPP_INLINE_VISIBILITY
1410 void
1411 basic_fstream<_CharT, _Traits>::close()
1412 {
1413     if (__sb_.close() == 0)
1414         this->setstate(ios_base::failbit);
1415 }
1416
1417 _LIBCPP_END_NAMESPACE_STD
1418
1419 #endif  // _LIBCPP_FSTREAM