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