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