fstream

Go to the documentation of this file.
00001 // File based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 27.8  File-based streams
00033 //
00034 
00040 #ifndef _GLIBCXX_FSTREAM
00041 #define _GLIBCXX_FSTREAM 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <istream>
00046 #include <ostream>
00047 #include <locale>   // For codecvt
00048 #include <cstdio>       // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ     
00049 #include <bits/basic_file.h>
00050 #include <bits/gthr.h>
00051 
00052 namespace std
00053 {
00054   // [27.8.1.1] template class basic_filebuf
00063   // Requirements on traits_type, specific to this class:
00064   // traits_type::pos_type must be fpos<traits_type::state_type>
00065   // traits_type::off_type must be streamoff
00066   // traits_type::state_type must be Assignable and DefaultConstructable,
00067   // and traits_type::state_type() must be the initial state for codecvt.
00068   template<typename _CharT, typename _Traits>
00069     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00070     {
00071     public:
00072       // Types:
00073       typedef _CharT                                char_type;
00074       typedef _Traits                               traits_type;
00075       typedef typename traits_type::int_type        int_type;
00076       typedef typename traits_type::pos_type        pos_type;
00077       typedef typename traits_type::off_type        off_type;
00078 
00080 
00085       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00086       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00087       typedef __basic_file<char>                __file_type;
00088       typedef typename traits_type::state_type          __state_type;
00089       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
00091 
00092       friend class ios_base; // For sync_with_stdio.
00093 
00094     protected:
00095       // Data Members:
00096       // MT lock inherited from libio or other low-level io library.
00102       __c_lock              _M_lock;
00103 
00104       // External buffer.
00110       __file_type       _M_file;
00111 
00117       ios_base::openmode    _M_mode;
00118 
00119       // Beginning state type for codecvt.
00125       __state_type      _M_state_beg;
00126 
00127       // During output, the state that corresponds to pptr(),
00128       // during input, the state that corresponds to egptr() and
00129       // _M_ext_next.
00135       __state_type      _M_state_cur;
00136 
00137       // Not used for output. During input, the state that corresponds
00138       // to eback() and _M_ext_buf.
00144       __state_type      _M_state_last;
00145 
00151       char_type*        _M_buf;     
00152 
00160       size_t            _M_buf_size;
00161 
00162       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
00168       bool          _M_buf_allocated;
00169 
00179       bool                      _M_reading;
00180       bool                      _M_writing;
00181 
00183 
00190       char_type         _M_pback; 
00191       char_type*        _M_pback_cur_save;
00192       char_type*        _M_pback_end_save;
00193       bool          _M_pback_init; 
00195 
00196       // Cached codecvt facet.
00197       const __codecvt_type*     _M_codecvt;
00198 
00206       char*         _M_ext_buf;
00207 
00213       streamsize        _M_ext_buf_size;
00214 
00222       const char*       _M_ext_next;
00223       char*         _M_ext_end;
00224 
00232       void
00233       _M_create_pback()
00234       {
00235     if (!_M_pback_init)
00236       {
00237         _M_pback_cur_save = this->gptr();
00238         _M_pback_end_save = this->egptr();
00239         this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00240         _M_pback_init = true;
00241       }
00242       }
00243 
00251       void
00252       _M_destroy_pback() throw()
00253       {
00254     if (_M_pback_init)
00255       {
00256         // Length _M_in_cur moved in the pback buffer.
00257         _M_pback_cur_save += this->gptr() != this->eback();
00258         this->setg(this->_M_buf, _M_pback_cur_save, _M_pback_end_save);
00259         _M_pback_init = false;
00260       }
00261       }
00262 
00263     public:
00264       // Constructors/destructor:
00271       basic_filebuf();
00272 
00276       virtual
00277       ~basic_filebuf()
00278       { this->close(); }
00279 
00280       // Members:
00284       bool
00285       is_open() const throw() { return _M_file.is_open(); }
00286 
00300       __filebuf_type*
00301       open(const char* __s, ios_base::openmode __mode);
00302 
00314       __filebuf_type*
00315       close() throw();
00316 
00317     protected:
00323       void
00324       _M_allocate_internal_buffer();
00325 
00331       void
00332       _M_destroy_internal_buffer() throw();
00333 
00334       // [27.8.1.4] overridden virtual functions
00335       // [documentation is inherited]
00336       virtual streamsize
00337       showmanyc();
00338 
00339       // Stroustrup, 1998, p. 628
00340       // underflow() and uflow() functions are called to get the next
00341       // charater from the real input source when the buffer is empty.
00342       // Buffered input uses underflow()
00343 
00344       // [documentation is inherited]
00345       virtual int_type
00346       underflow();
00347 
00348       // [documentation is inherited]
00349       virtual int_type
00350       pbackfail(int_type __c = _Traits::eof());
00351 
00352       // Stroustrup, 1998, p 648
00353       // The overflow() function is called to transfer characters to the
00354       // real output destination when the buffer is full. A call to
00355       // overflow(c) outputs the contents of the buffer plus the
00356       // character c.
00357       // 27.5.2.4.5
00358       // Consume some sequence of the characters in the pending sequence.
00364       virtual int_type
00365       overflow(int_type __c = _Traits::eof());
00366 
00367       // Convert internal byte sequence to external, char-based
00368       // sequence via codecvt.
00374       bool
00375       _M_convert_to_external(char_type*, streamsize);
00376 
00389       virtual __streambuf_type*
00390       setbuf(char_type* __s, streamsize __n);
00391 
00392       // [documentation is inherited]
00393       virtual pos_type
00394       seekoff(off_type __off, ios_base::seekdir __way,
00395           ios_base::openmode __mode = ios_base::in | ios_base::out);
00396 
00397       // [documentation is inherited]
00398       virtual pos_type
00399       seekpos(pos_type __pos,
00400           ios_base::openmode __mode = ios_base::in | ios_base::out);
00401 
00402       // Common code for seekoff and seekpos
00408       pos_type
00409       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00410 
00411       // [documentation is inherited]
00412       virtual int
00413       sync();
00414 
00415       // [documentation is inherited]
00416       virtual void
00417       imbue(const locale& __loc);
00418 
00419       // [documentation is inherited]
00420       virtual streamsize
00421       xsgetn(char_type* __s, streamsize __n);
00422 
00423       // [documentation is inherited]
00424       virtual streamsize
00425       xsputn(const char_type* __s, streamsize __n);
00426 
00427       // Flushes output buffer, then writes unshift sequence.
00433       bool
00434       _M_terminate_output();
00435 
00450       void
00451       _M_set_buffer(streamsize __off)
00452       {
00453     const bool __testin = this->_M_mode & ios_base::in;
00454     const bool __testout = this->_M_mode & ios_base::out;
00455     
00456     if (__testin && __off > 0)
00457       this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
00458     else
00459       this->setg(this->_M_buf, this->_M_buf, this->_M_buf);
00460 
00461     if (__testout && __off == 0 && this->_M_buf_size > 1 )
00462       this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size - 1);
00463     else
00464       this->setp(NULL, NULL);
00465       }
00466     };
00467 
00468   // [27.8.1.5] Template class basic_ifstream
00477   template<typename _CharT, typename _Traits>
00478     class basic_ifstream : public basic_istream<_CharT, _Traits>
00479     {
00480     public:
00481       // Types:
00482       typedef _CharT                    char_type;
00483       typedef _Traits                   traits_type;
00484       typedef typename traits_type::int_type        int_type;
00485       typedef typename traits_type::pos_type        pos_type;
00486       typedef typename traits_type::off_type        off_type;
00487 
00488       // Non-standard types:
00489       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00490       typedef basic_istream<char_type, traits_type> __istream_type;
00491 
00492     private:
00498       __filebuf_type    _M_filebuf;
00499 
00500     public:
00501       // Constructors/Destructors:
00509       basic_ifstream() : __istream_type(), _M_filebuf()
00510       { this->init(&_M_filebuf); }
00511 
00522       explicit
00523       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00524       : __istream_type(), _M_filebuf()
00525       {
00526     this->init(&_M_filebuf);
00527     this->open(__s, __mode);
00528       }
00529 
00536       ~basic_ifstream()
00537       { }
00538 
00539       // Members:
00546       __filebuf_type*
00547       rdbuf() const
00548       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00549 
00554       bool
00555       is_open() { return _M_filebuf.is_open(); }
00556 
00568       void
00569       open(const char* __s, ios_base::openmode __mode = ios_base::in)
00570       {
00571     if (!_M_filebuf.open(__s, __mode | ios_base::in))
00572       this->setstate(ios_base::failbit);
00573       }
00574 
00581       void
00582       close()
00583       {
00584     if (!_M_filebuf.close())
00585       this->setstate(ios_base::failbit);
00586       }
00587     };
00588 
00589 
00590   // [27.8.1.8] Template class basic_ofstream
00599   template<typename _CharT, typename _Traits>
00600     class basic_ofstream : public basic_ostream<_CharT,_Traits>
00601     {
00602     public:
00603       // Types:
00604       typedef _CharT                    char_type;
00605       typedef _Traits                   traits_type;
00606       typedef typename traits_type::int_type        int_type;
00607       typedef typename traits_type::pos_type        pos_type;
00608       typedef typename traits_type::off_type        off_type;
00609 
00610       // Non-standard types:
00611       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00612       typedef basic_ostream<char_type, traits_type> __ostream_type;
00613 
00614     private:
00620       __filebuf_type    _M_filebuf;
00621 
00622     public:
00623       // Constructors:
00631       basic_ofstream(): __ostream_type(), _M_filebuf()
00632       { this->init(&_M_filebuf); }
00633 
00645       explicit
00646       basic_ofstream(const char* __s,
00647              ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00648       : __ostream_type(), _M_filebuf()
00649       {
00650     this->init(&_M_filebuf);
00651     this->open(__s, __mode);
00652       }
00653 
00660       ~basic_ofstream()
00661       { }
00662 
00663       // Members:
00670       __filebuf_type*
00671       rdbuf() const
00672       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00673 
00678       bool
00679       is_open() { return _M_filebuf.is_open(); }
00680 
00692       void
00693       open(const char* __s,
00694        ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00695       {
00696     if (!_M_filebuf.open(__s, __mode | ios_base::out))
00697       this->setstate(ios_base::failbit);
00698       }
00699 
00706       void
00707       close()
00708       {
00709     if (!_M_filebuf.close())
00710       this->setstate(ios_base::failbit);
00711       }
00712     };
00713 
00714 
00715   // [27.8.1.11] Template class basic_fstream
00724   template<typename _CharT, typename _Traits>
00725     class basic_fstream : public basic_iostream<_CharT, _Traits>
00726     {
00727     public:
00728       // Types:
00729       typedef _CharT                    char_type;
00730       typedef _Traits                   traits_type;
00731       typedef typename traits_type::int_type        int_type;
00732       typedef typename traits_type::pos_type        pos_type;
00733       typedef typename traits_type::off_type        off_type;
00734 
00735       // Non-standard types:
00736       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00737       typedef basic_ios<char_type, traits_type>     __ios_type;
00738       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00739 
00740     private:
00746       __filebuf_type    _M_filebuf;
00747 
00748     public:
00749       // Constructors/destructor:
00757       basic_fstream()
00758       : __iostream_type(), _M_filebuf()
00759       { this->init(&_M_filebuf); }
00760 
00769       explicit
00770       basic_fstream(const char* __s,
00771             ios_base::openmode __mode = ios_base::in | ios_base::out)
00772       : __iostream_type(NULL), _M_filebuf()
00773       {
00774     this->init(&_M_filebuf);
00775     this->open(__s, __mode);
00776       }
00777 
00784       ~basic_fstream()
00785       { }
00786 
00787       // Members:
00794       __filebuf_type*
00795       rdbuf() const
00796       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00797 
00802       bool
00803       is_open() { return _M_filebuf.is_open(); }
00804 
00816       void
00817       open(const char* __s,
00818        ios_base::openmode __mode = ios_base::in | ios_base::out)
00819       {
00820     if (!_M_filebuf.open(__s, __mode))
00821       this->setstate(ios_base::failbit);
00822       }
00823 
00830       void
00831       close()
00832       {
00833     if (!_M_filebuf.close())
00834       this->setstate(ios_base::failbit);
00835       }
00836     };
00837 } // namespace std
00838 
00839 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00840 # include <bits/fstream.tcc>
00841 #endif
00842 
00843 #endif /* _GLIBCXX_FSTREAM */

Generated on Tue Jan 30 17:31:49 2007 for GNU C++ STL by doxygen 1.3.6