fstream

Go to the documentation of this file.
00001 // File based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file fstream
00028  *  This is a Standard C++ Library header.
00029  */
00030 
00031 //
00032 // ISO C++ 14882: 27.8  File-based streams
00033 //
00034 
00035 #ifndef _GLIBCXX_FSTREAM
00036 #define _GLIBCXX_FSTREAM 1
00037 
00038 #pragma GCC system_header
00039 
00040 #include <istream>
00041 #include <ostream>
00042 #include <bits/codecvt.h>
00043 #include <cstdio>             // For BUFSIZ
00044 #include <bits/basic_file.h>  // For __basic_file, __c_lock
00045 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00046 #include <string>             // For std::string overloads.
00047 #endif
00048 
00049 _GLIBCXX_BEGIN_NAMESPACE(std)
00050 
00051   // [27.8.1.1] template class basic_filebuf
00052   /**
00053    *  @brief  The actual work of input and output (for files).
00054    *  @ingroup io
00055    *
00056    *  This class associates both its input and output sequence with an
00057    *  external disk file, and maintains a joint file position for both
00058    *  sequences.  Many of its semantics are described in terms of similar
00059    *  behavior in the Standard C Library's @c FILE streams.
00060    */
00061   // Requirements on traits_type, specific to this class:
00062   // traits_type::pos_type must be fpos<traits_type::state_type>
00063   // traits_type::off_type must be streamoff
00064   // traits_type::state_type must be Assignable and DefaultConstructible,
00065   // and traits_type::state_type() must be the initial state for codecvt.
00066   template<typename _CharT, typename _Traits>
00067     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00068     {
00069     public:
00070       // Types:
00071       typedef _CharT                                char_type;
00072       typedef _Traits                               traits_type;
00073       typedef typename traits_type::int_type        int_type;
00074       typedef typename traits_type::pos_type        pos_type;
00075       typedef typename traits_type::off_type        off_type;
00076 
00077       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00078       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00079       typedef __basic_file<char>                __file_type;
00080       typedef typename traits_type::state_type          __state_type;
00081       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
00082 
00083       friend class ios_base; // For sync_with_stdio.
00084 
00085     protected:
00086       // Data Members:
00087       // MT lock inherited from libio or other low-level io library.
00088       __c_lock              _M_lock;
00089 
00090       // External buffer.
00091       __file_type       _M_file;
00092 
00093       /// Place to stash in || out || in | out settings for current filebuf.
00094       ios_base::openmode    _M_mode;
00095 
00096       // Beginning state type for codecvt.
00097       __state_type      _M_state_beg;
00098 
00099       // During output, the state that corresponds to pptr(),
00100       // during input, the state that corresponds to egptr() and
00101       // _M_ext_next.
00102       __state_type      _M_state_cur;
00103 
00104       // Not used for output. During input, the state that corresponds
00105       // to eback() and _M_ext_buf.
00106       __state_type      _M_state_last;
00107 
00108       /// Pointer to the beginning of internal buffer.
00109       char_type*        _M_buf;     
00110 
00111       /**
00112        *  Actual size of internal buffer. This number is equal to the size
00113        *  of the put area + 1 position, reserved for the overflow char of
00114        *  a full area.
00115        */
00116       size_t            _M_buf_size;
00117 
00118       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
00119       bool          _M_buf_allocated;
00120 
00121       /**
00122        *  _M_reading == false && _M_writing == false for @b uncommitted mode;
00123        *  _M_reading == true for @b read mode;
00124        *  _M_writing == true for @b write mode;
00125        *
00126        *  NB: _M_reading == true && _M_writing == true is unused.
00127        */
00128       bool                      _M_reading;
00129       bool                      _M_writing;
00130 
00131       //@{
00132       /**
00133        *  Necessary bits for putback buffer management.
00134        *
00135        *  @note pbacks of over one character are not currently supported.
00136        */
00137       char_type         _M_pback;
00138       char_type*        _M_pback_cur_save;
00139       char_type*        _M_pback_end_save;
00140       bool          _M_pback_init;
00141       //@}
00142 
00143       // Cached codecvt facet.
00144       const __codecvt_type*     _M_codecvt;
00145 
00146       /**
00147        *  Buffer for external characters. Used for input when
00148        *  codecvt::always_noconv() == false. When valid, this corresponds
00149        *  to eback().
00150        */
00151       char*         _M_ext_buf;
00152 
00153       /**
00154        *  Size of buffer held by _M_ext_buf.
00155        */
00156       streamsize        _M_ext_buf_size;
00157 
00158       /**
00159        *  Pointers into the buffer held by _M_ext_buf that delimit a
00160        *  subsequence of bytes that have been read but not yet converted.
00161        *  When valid, _M_ext_next corresponds to egptr().
00162        */
00163       const char*       _M_ext_next;
00164       char*         _M_ext_end;
00165 
00166       /**
00167        *  Initializes pback buffers, and moves normal buffers to safety.
00168        *  Assumptions:
00169        *  _M_in_cur has already been moved back
00170        */
00171       void
00172       _M_create_pback()
00173       {
00174     if (!_M_pback_init)
00175       {
00176         _M_pback_cur_save = this->gptr();
00177         _M_pback_end_save = this->egptr();
00178         this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00179         _M_pback_init = true;
00180       }
00181       }
00182 
00183       /**
00184        *  Deactivates pback buffer contents, and restores normal buffer.
00185        *  Assumptions:
00186        *  The pback buffer has only moved forward.
00187        */
00188       void
00189       _M_destroy_pback() throw()
00190       {
00191     if (_M_pback_init)
00192       {
00193         // Length _M_in_cur moved in the pback buffer.
00194         _M_pback_cur_save += this->gptr() != this->eback();
00195         this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
00196         _M_pback_init = false;
00197       }
00198       }
00199 
00200     public:
00201       // Constructors/destructor:
00202       /**
00203        *  @brief  Does not open any files.
00204        *
00205        *  The default constructor initializes the parent class using its
00206        *  own default ctor.
00207        */
00208       basic_filebuf();
00209 
00210       /**
00211        *  @brief  The destructor closes the file first.
00212        */
00213       virtual
00214       ~basic_filebuf()
00215       { this->close(); }
00216 
00217       // Members:
00218       /**
00219        *  @brief  Returns true if the external file is open.
00220        */
00221       bool
00222       is_open() const throw()
00223       { return _M_file.is_open(); }
00224 
00225       /**
00226        *  @brief  Opens an external file.
00227        *  @param  s  The name of the file.
00228        *  @param  mode  The open mode flags.
00229        *  @return  @c this on success, NULL on failure
00230        *
00231        *  If a file is already open, this function immediately fails.
00232        *  Otherwise it tries to open the file named @a s using the flags
00233        *  given in @a mode.
00234        *
00235        *  Table 92, adapted here, gives the relation between openmode
00236        *  combinations and the equivalent fopen() flags.
00237        *  (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
00238        *  and binary|in|app per DR 596)
00239        *  +---------------------------------------------------------+
00240        *  | ios_base Flag combination            stdio equivalent   |
00241        *  |binary  in  out  trunc  app                              |
00242        *  +---------------------------------------------------------+
00243        *  |             +                        w                  |
00244        *  |             +           +            a                  |
00245        *  |                         +            a                  |
00246        *  |             +     +                  w                  |
00247        *  |         +                            r                  |
00248        *  |         +   +                        r+                 |
00249        *  |         +   +     +                  w+                 |
00250        *  |         +   +           +            a+                 |
00251        *  |         +               +            a+                 |
00252        *  +---------------------------------------------------------+
00253        *  |   +         +                        wb                 |
00254        *  |   +         +           +            ab                 |
00255        *  |   +                     +            ab                 |
00256        *  |   +         +     +                  wb                 |
00257        *  |   +     +                            rb                 |
00258        *  |   +     +   +                        r+b                |
00259        *  |   +     +   +     +                  w+b                |
00260        *  |   +     +   +           +            a+b                |
00261        *  |   +     +               +            a+b                |
00262        *  +---------------------------------------------------------+
00263        */
00264       __filebuf_type*
00265       open(const char* __s, ios_base::openmode __mode);
00266 
00267 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00268       /**
00269        *  @brief  Opens an external file.
00270        *  @param  s  The name of the file.
00271        *  @param  mode  The open mode flags.
00272        *  @return  @c this on success, NULL on failure
00273        */
00274       __filebuf_type*
00275       open(const std::string& __s, ios_base::openmode __mode)
00276       { return open(__s.c_str(), __mode); }
00277 #endif
00278 
00279       /**
00280        *  @brief  Closes the currently associated file.
00281        *  @return  @c this on success, NULL on failure
00282        *
00283        *  If no file is currently open, this function immediately fails.
00284        *
00285        *  If a <em>put buffer area</em> exists, @c overflow(eof) is
00286        *  called to flush all the characters.  The file is then
00287        *  closed.
00288        *
00289        *  If any operations fail, this function also fails.
00290        */
00291       __filebuf_type*
00292       close();
00293 
00294     protected:
00295       void
00296       _M_allocate_internal_buffer();
00297 
00298       void
00299       _M_destroy_internal_buffer() throw();
00300 
00301       // [27.8.1.4] overridden virtual functions
00302       virtual streamsize
00303       showmanyc();
00304 
00305       // Stroustrup, 1998, p. 628
00306       // underflow() and uflow() functions are called to get the next
00307       // character from the real input source when the buffer is empty.
00308       // Buffered input uses underflow()
00309 
00310       virtual int_type
00311       underflow();
00312 
00313       virtual int_type
00314       pbackfail(int_type __c = _Traits::eof());
00315 
00316       // Stroustrup, 1998, p 648
00317       // The overflow() function is called to transfer characters to the
00318       // real output destination when the buffer is full. A call to
00319       // overflow(c) outputs the contents of the buffer plus the
00320       // character c.
00321       // 27.5.2.4.5
00322       // Consume some sequence of the characters in the pending sequence.
00323       virtual int_type
00324       overflow(int_type __c = _Traits::eof());
00325 
00326       // Convert internal byte sequence to external, char-based
00327       // sequence via codecvt.
00328       bool
00329       _M_convert_to_external(char_type*, streamsize);
00330 
00331       /**
00332        *  @brief  Manipulates the buffer.
00333        *  @param  s  Pointer to a buffer area.
00334        *  @param  n  Size of @a s.
00335        *  @return  @c this
00336        *
00337        *  If no file has been opened, and both @a s and @a n are zero, then
00338        *  the stream becomes unbuffered.  Otherwise, @c s is used as a
00339        *  buffer; see
00340        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
00341        *  for more.
00342        */
00343       virtual __streambuf_type*
00344       setbuf(char_type* __s, streamsize __n);
00345 
00346       virtual pos_type
00347       seekoff(off_type __off, ios_base::seekdir __way,
00348           ios_base::openmode __mode = ios_base::in | ios_base::out);
00349 
00350       virtual pos_type
00351       seekpos(pos_type __pos,
00352           ios_base::openmode __mode = ios_base::in | ios_base::out);
00353 
00354       // Common code for seekoff and seekpos
00355       pos_type
00356       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00357 
00358       virtual int
00359       sync();
00360 
00361       virtual void
00362       imbue(const locale& __loc);
00363 
00364       virtual streamsize
00365       xsgetn(char_type* __s, streamsize __n);
00366 
00367       virtual streamsize
00368       xsputn(const char_type* __s, streamsize __n);
00369 
00370       // Flushes output buffer, then writes unshift sequence.
00371       bool
00372       _M_terminate_output();
00373 
00374       /**
00375        *  This function sets the pointers of the internal buffer, both get
00376        *  and put areas. Typically:
00377        *
00378        *   __off == egptr() - eback() upon underflow/uflow (@b read mode);
00379        *   __off == 0 upon overflow (@b write mode);
00380        *   __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
00381        *
00382        *  NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
00383        *  reflects the actual allocated memory and the last cell is reserved
00384        *  for the overflow char of a full put area.
00385        */
00386       void
00387       _M_set_buffer(streamsize __off)
00388       {
00389     const bool __testin = _M_mode & ios_base::in;
00390     const bool __testout = _M_mode & ios_base::out;
00391     
00392     if (__testin && __off > 0)
00393       this->setg(_M_buf, _M_buf, _M_buf + __off);
00394     else
00395       this->setg(_M_buf, _M_buf, _M_buf);
00396 
00397     if (__testout && __off == 0 && _M_buf_size > 1 )
00398       this->setp(_M_buf, _M_buf + _M_buf_size - 1);
00399     else
00400       this->setp(NULL, NULL);
00401       }
00402     };
00403 
00404   // [27.8.1.5] Template class basic_ifstream
00405   /**
00406    *  @brief  Controlling input for files.
00407    *  @ingroup io
00408    *
00409    *  This class supports reading from named files, using the inherited
00410    *  functions from std::basic_istream.  To control the associated
00411    *  sequence, an instance of std::basic_filebuf is used, which this page
00412    *  refers to as @c sb.
00413    */
00414   template<typename _CharT, typename _Traits>
00415     class basic_ifstream : public basic_istream<_CharT, _Traits>
00416     {
00417     public:
00418       // Types:
00419       typedef _CharT                    char_type;
00420       typedef _Traits                   traits_type;
00421       typedef typename traits_type::int_type        int_type;
00422       typedef typename traits_type::pos_type        pos_type;
00423       typedef typename traits_type::off_type        off_type;
00424 
00425       // Non-standard types:
00426       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00427       typedef basic_istream<char_type, traits_type> __istream_type;
00428 
00429     private:
00430       __filebuf_type    _M_filebuf;
00431 
00432     public:
00433       // Constructors/Destructors:
00434       /**
00435        *  @brief  Default constructor.
00436        *
00437        *  Initializes @c sb using its default constructor, and passes
00438        *  @c &sb to the base class initializer.  Does not open any files
00439        *  (you haven't given it a filename to open).
00440        */
00441       basic_ifstream() : __istream_type(), _M_filebuf()
00442       { this->init(&_M_filebuf); }
00443 
00444       /**
00445        *  @brief  Create an input file stream.
00446        *  @param  s  Null terminated string specifying the filename.
00447        *  @param  mode  Open file in specified mode (see std::ios_base).
00448        *
00449        *  @c ios_base::in is automatically included in @a mode.
00450        *
00451        *  Tip:  When using std::string to hold the filename, you must use
00452        *  .c_str() before passing it to this constructor.
00453        */
00454       explicit
00455       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00456       : __istream_type(), _M_filebuf()
00457       {
00458     this->init(&_M_filebuf);
00459     this->open(__s, __mode);
00460       }
00461 
00462 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00463       /**
00464        *  @brief  Create an input file stream.
00465        *  @param  s  std::string specifying the filename.
00466        *  @param  mode  Open file in specified mode (see std::ios_base).
00467        *
00468        *  @c ios_base::in is automatically included in @a mode.
00469        */
00470       explicit
00471       basic_ifstream(const std::string& __s,
00472              ios_base::openmode __mode = ios_base::in)
00473       : __istream_type(), _M_filebuf()
00474       {
00475     this->init(&_M_filebuf);
00476     this->open(__s, __mode);
00477       }
00478 #endif
00479 
00480       /**
00481        *  @brief  The destructor does nothing.
00482        *
00483        *  The file is closed by the filebuf object, not the formatting
00484        *  stream.
00485        */
00486       ~basic_ifstream()
00487       { }
00488 
00489       // Members:
00490       /**
00491        *  @brief  Accessing the underlying buffer.
00492        *  @return  The current basic_filebuf buffer.
00493        *
00494        *  This hides both signatures of std::basic_ios::rdbuf().
00495        */
00496       __filebuf_type*
00497       rdbuf() const
00498       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00499 
00500       /**
00501        *  @brief  Wrapper to test for an open file.
00502        *  @return  @c rdbuf()->is_open()
00503        */
00504       bool
00505       is_open()
00506       { return _M_filebuf.is_open(); }
00507 
00508       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00509       // 365. Lack of const-qualification in clause 27
00510       bool
00511       is_open() const
00512       { return _M_filebuf.is_open(); }
00513 
00514       /**
00515        *  @brief  Opens an external file.
00516        *  @param  s  The name of the file.
00517        *  @param  mode  The open mode flags.
00518        *
00519        *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
00520        *  fails, @c failbit is set in the stream's error state.
00521        *
00522        *  Tip:  When using std::string to hold the filename, you must use
00523        *  .c_str() before passing it to this constructor.
00524        */
00525       void
00526       open(const char* __s, ios_base::openmode __mode = ios_base::in)
00527       {
00528     if (!_M_filebuf.open(__s, __mode | ios_base::in))
00529       this->setstate(ios_base::failbit);
00530     else
00531       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00532       // 409. Closing an fstream should clear error state
00533       this->clear();
00534       }
00535 
00536 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00537       /**
00538        *  @brief  Opens an external file.
00539        *  @param  s  The name of the file.
00540        *  @param  mode  The open mode flags.
00541        *
00542        *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
00543        *  fails, @c failbit is set in the stream's error state.
00544        */
00545       void
00546       open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
00547       {
00548     if (!_M_filebuf.open(__s, __mode | ios_base::in))
00549       this->setstate(ios_base::failbit);
00550     else
00551       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00552       // 409. Closing an fstream should clear error state
00553       this->clear();
00554       }
00555 #endif
00556 
00557       /**
00558        *  @brief  Close the file.
00559        *
00560        *  Calls @c std::basic_filebuf::close().  If that function
00561        *  fails, @c failbit is set in the stream's error state.
00562        */
00563       void
00564       close()
00565       {
00566     if (!_M_filebuf.close())
00567       this->setstate(ios_base::failbit);
00568       }
00569     };
00570 
00571 
00572   // [27.8.1.8] Template class basic_ofstream
00573   /**
00574    *  @brief  Controlling output for files.
00575    *  @ingroup io
00576    *
00577    *  This class supports reading from named files, using the inherited
00578    *  functions from std::basic_ostream.  To control the associated
00579    *  sequence, an instance of std::basic_filebuf is used, which this page
00580    *  refers to as @c sb.
00581    */
00582   template<typename _CharT, typename _Traits>
00583     class basic_ofstream : public basic_ostream<_CharT,_Traits>
00584     {
00585     public:
00586       // Types:
00587       typedef _CharT                    char_type;
00588       typedef _Traits                   traits_type;
00589       typedef typename traits_type::int_type        int_type;
00590       typedef typename traits_type::pos_type        pos_type;
00591       typedef typename traits_type::off_type        off_type;
00592 
00593       // Non-standard types:
00594       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00595       typedef basic_ostream<char_type, traits_type> __ostream_type;
00596 
00597     private:
00598       __filebuf_type    _M_filebuf;
00599 
00600     public:
00601       // Constructors:
00602       /**
00603        *  @brief  Default constructor.
00604        *
00605        *  Initializes @c sb using its default constructor, and passes
00606        *  @c &sb to the base class initializer.  Does not open any files
00607        *  (you haven't given it a filename to open).
00608        */
00609       basic_ofstream(): __ostream_type(), _M_filebuf()
00610       { this->init(&_M_filebuf); }
00611 
00612       /**
00613        *  @brief  Create an output file stream.
00614        *  @param  s  Null terminated string specifying the filename.
00615        *  @param  mode  Open file in specified mode (see std::ios_base).
00616        *
00617        *  @c ios_base::out|ios_base::trunc is automatically included in
00618        *  @a mode.
00619        *
00620        *  Tip:  When using std::string to hold the filename, you must use
00621        *  .c_str() before passing it to this constructor.
00622        */
00623       explicit
00624       basic_ofstream(const char* __s,
00625              ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00626       : __ostream_type(), _M_filebuf()
00627       {
00628     this->init(&_M_filebuf);
00629     this->open(__s, __mode);
00630       }
00631 
00632 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00633       /**
00634        *  @brief  Create an output file stream.
00635        *  @param  s  std::string specifying the filename.
00636        *  @param  mode  Open file in specified mode (see std::ios_base).
00637        *
00638        *  @c ios_base::out|ios_base::trunc is automatically included in
00639        *  @a mode.
00640        */
00641       explicit
00642       basic_ofstream(const std::string& __s,
00643              ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00644       : __ostream_type(), _M_filebuf()
00645       {
00646     this->init(&_M_filebuf);
00647     this->open(__s, __mode);
00648       }
00649 #endif
00650 
00651       /**
00652        *  @brief  The destructor does nothing.
00653        *
00654        *  The file is closed by the filebuf object, not the formatting
00655        *  stream.
00656        */
00657       ~basic_ofstream()
00658       { }
00659 
00660       // Members:
00661       /**
00662        *  @brief  Accessing the underlying buffer.
00663        *  @return  The current basic_filebuf buffer.
00664        *
00665        *  This hides both signatures of std::basic_ios::rdbuf().
00666        */
00667       __filebuf_type*
00668       rdbuf() const
00669       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00670 
00671       /**
00672        *  @brief  Wrapper to test for an open file.
00673        *  @return  @c rdbuf()->is_open()
00674        */
00675       bool
00676       is_open()
00677       { return _M_filebuf.is_open(); }
00678 
00679       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00680       // 365. Lack of const-qualification in clause 27
00681       bool
00682       is_open() const
00683       { return _M_filebuf.is_open(); }
00684 
00685       /**
00686        *  @brief  Opens an external file.
00687        *  @param  s  The name of the file.
00688        *  @param  mode  The open mode flags.
00689        *
00690        *  Calls @c std::basic_filebuf::open(s,mode|out|trunc).  If that
00691        *  function fails, @c failbit is set in the stream's error state.
00692        *
00693        *  Tip:  When using std::string to hold the filename, you must use
00694        *  .c_str() before passing it to this constructor.
00695        */
00696       void
00697       open(const char* __s,
00698        ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00699       {
00700     if (!_M_filebuf.open(__s, __mode | ios_base::out))
00701       this->setstate(ios_base::failbit);
00702     else
00703       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00704       // 409. Closing an fstream should clear error state
00705       this->clear();
00706       }
00707 
00708 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00709       /**
00710        *  @brief  Opens an external file.
00711        *  @param  s  The name of the file.
00712        *  @param  mode  The open mode flags.
00713        *
00714        *  Calls @c std::basic_filebuf::open(s,mode|out|trunc).  If that
00715        *  function fails, @c failbit is set in the stream's error state.
00716        */
00717       void
00718       open(const std::string& __s,
00719        ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00720       {
00721     if (!_M_filebuf.open(__s, __mode | ios_base::out))
00722       this->setstate(ios_base::failbit);
00723     else
00724       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00725       // 409. Closing an fstream should clear error state
00726       this->clear();
00727       }
00728 #endif
00729 
00730       /**
00731        *  @brief  Close the file.
00732        *
00733        *  Calls @c std::basic_filebuf::close().  If that function
00734        *  fails, @c failbit is set in the stream's error state.
00735        */
00736       void
00737       close()
00738       {
00739     if (!_M_filebuf.close())
00740       this->setstate(ios_base::failbit);
00741       }
00742     };
00743 
00744 
00745   // [27.8.1.11] Template class basic_fstream
00746   /**
00747    *  @brief  Controlling input and output for files.
00748    *  @ingroup io
00749    *
00750    *  This class supports reading from and writing to named files, using
00751    *  the inherited functions from std::basic_iostream.  To control the
00752    *  associated sequence, an instance of std::basic_filebuf is used, which
00753    *  this page refers to as @c sb.
00754    */
00755   template<typename _CharT, typename _Traits>
00756     class basic_fstream : public basic_iostream<_CharT, _Traits>
00757     {
00758     public:
00759       // Types:
00760       typedef _CharT                    char_type;
00761       typedef _Traits                   traits_type;
00762       typedef typename traits_type::int_type        int_type;
00763       typedef typename traits_type::pos_type        pos_type;
00764       typedef typename traits_type::off_type        off_type;
00765 
00766       // Non-standard types:
00767       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00768       typedef basic_ios<char_type, traits_type>     __ios_type;
00769       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00770 
00771     private:
00772       __filebuf_type    _M_filebuf;
00773 
00774     public:
00775       // Constructors/destructor:
00776       /**
00777        *  @brief  Default constructor.
00778        *
00779        *  Initializes @c sb using its default constructor, and passes
00780        *  @c &sb to the base class initializer.  Does not open any files
00781        *  (you haven't given it a filename to open).
00782        */
00783       basic_fstream()
00784       : __iostream_type(), _M_filebuf()
00785       { this->init(&_M_filebuf); }
00786 
00787       /**
00788        *  @brief  Create an input/output file stream.
00789        *  @param  s  Null terminated string specifying the filename.
00790        *  @param  mode  Open file in specified mode (see std::ios_base).
00791        *
00792        *  Tip:  When using std::string to hold the filename, you must use
00793        *  .c_str() before passing it to this constructor.
00794        */
00795       explicit
00796       basic_fstream(const char* __s,
00797             ios_base::openmode __mode = ios_base::in | ios_base::out)
00798       : __iostream_type(NULL), _M_filebuf()
00799       {
00800     this->init(&_M_filebuf);
00801     this->open(__s, __mode);
00802       }
00803 
00804 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00805       /**
00806        *  @brief  Create an input/output file stream.
00807        *  @param  s  Null terminated string specifying the filename.
00808        *  @param  mode  Open file in specified mode (see std::ios_base).
00809        */
00810       explicit
00811       basic_fstream(const std::string& __s,
00812             ios_base::openmode __mode = ios_base::in | ios_base::out)
00813       : __iostream_type(NULL), _M_filebuf()
00814       {
00815     this->init(&_M_filebuf);
00816     this->open(__s, __mode);
00817       }
00818 #endif
00819 
00820       /**
00821        *  @brief  The destructor does nothing.
00822        *
00823        *  The file is closed by the filebuf object, not the formatting
00824        *  stream.
00825        */
00826       ~basic_fstream()
00827       { }
00828 
00829       // Members:
00830       /**
00831        *  @brief  Accessing the underlying buffer.
00832        *  @return  The current basic_filebuf buffer.
00833        *
00834        *  This hides both signatures of std::basic_ios::rdbuf().
00835        */
00836       __filebuf_type*
00837       rdbuf() const
00838       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00839 
00840       /**
00841        *  @brief  Wrapper to test for an open file.
00842        *  @return  @c rdbuf()->is_open()
00843        */
00844       bool
00845       is_open()
00846       { return _M_filebuf.is_open(); }
00847 
00848       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00849       // 365. Lack of const-qualification in clause 27
00850       bool
00851       is_open() const
00852       { return _M_filebuf.is_open(); }
00853 
00854       /**
00855        *  @brief  Opens an external file.
00856        *  @param  s  The name of the file.
00857        *  @param  mode  The open mode flags.
00858        *
00859        *  Calls @c std::basic_filebuf::open(s,mode).  If that
00860        *  function fails, @c failbit is set in the stream's error state.
00861        *
00862        *  Tip:  When using std::string to hold the filename, you must use
00863        *  .c_str() before passing it to this constructor.
00864        */
00865       void
00866       open(const char* __s,
00867        ios_base::openmode __mode = ios_base::in | ios_base::out)
00868       {
00869     if (!_M_filebuf.open(__s, __mode))
00870       this->setstate(ios_base::failbit);
00871     else
00872       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00873       // 409. Closing an fstream should clear error state
00874       this->clear();
00875       }
00876 
00877 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00878       /**
00879        *  @brief  Opens an external file.
00880        *  @param  s  The name of the file.
00881        *  @param  mode  The open mode flags.
00882        *
00883        *  Calls @c std::basic_filebuf::open(s,mode).  If that
00884        *  function fails, @c failbit is set in the stream's error state.
00885        */
00886       void
00887       open(const std::string& __s,
00888        ios_base::openmode __mode = ios_base::in | ios_base::out)
00889       {
00890     if (!_M_filebuf.open(__s, __mode))
00891       this->setstate(ios_base::failbit);
00892     else
00893       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00894       // 409. Closing an fstream should clear error state
00895       this->clear();
00896       }
00897 #endif
00898 
00899       /**
00900        *  @brief  Close the file.
00901        *
00902        *  Calls @c std::basic_filebuf::close().  If that function
00903        *  fails, @c failbit is set in the stream's error state.
00904        */
00905       void
00906       close()
00907       {
00908     if (!_M_filebuf.close())
00909       this->setstate(ios_base::failbit);
00910       }
00911     };
00912 
00913 _GLIBCXX_END_NAMESPACE
00914 
00915 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00916 # include <bits/fstream.tcc>
00917 #endif
00918 
00919 #endif /* _GLIBCXX_FSTREAM */