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