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