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