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 // 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 
00039 #ifndef _GLIBCXX_FSTREAM
00040 #define _GLIBCXX_FSTREAM 1
00041 
00042 #pragma GCC system_header
00043 
00044 #include <istream>
00045 #include <ostream>
00046 #include <locale>   // For codecvt
00047 #include <cstdio>       // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ     
00048 #include <bits/basic_file.h>
00049 #include <bits/gthr.h>
00050 
00051 namespace std
00052 {
00053   // [27.8.1.1] template class basic_filebuf
00062   // Requirements on traits_type, specific to this class:
00063   // traits_type::pos_type must be fpos<traits_type::state_type>
00064   // traits_type::off_type must be streamoff
00065   // traits_type::state_type must be Assignable and DefaultConstructable,
00066   // and traits_type::state_type() must be the initial state for codecvt.
00067   template<typename _CharT, typename _Traits>
00068     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
00069     {
00070     public:
00071       // Types:
00072       typedef _CharT                                char_type;
00073       typedef _Traits                               traits_type;
00074       typedef typename traits_type::int_type        int_type;
00075       typedef typename traits_type::pos_type        pos_type;
00076       typedef typename traits_type::off_type        off_type;
00077 
00078       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00079       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00080       typedef __basic_file<char>                __file_type;
00081       typedef typename traits_type::state_type          __state_type;
00082       typedef codecvt<char_type, char, __state_type>    __codecvt_type;
00083 
00084       friend class ios_base; // For sync_with_stdio.
00085 
00086     protected:
00087       // Data Members:
00088       // MT lock inherited from libio or other low-level io library.
00089       __c_lock              _M_lock;
00090 
00091       // External buffer.
00092       __file_type       _M_file;
00093 
00099       ios_base::openmode    _M_mode;
00100 
00101       // Beginning state type for codecvt.
00102       __state_type      _M_state_beg;
00103 
00104       // During output, the state that corresponds to pptr(),
00105       // during input, the state that corresponds to egptr() and
00106       // _M_ext_next.
00107       __state_type      _M_state_cur;
00108 
00109       // Not used for output. During input, the state that corresponds
00110       // to eback() and _M_ext_buf.
00111       __state_type      _M_state_last;
00112 
00118       char_type*        _M_buf;     
00119 
00127       size_t            _M_buf_size;
00128 
00129       // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
00130       bool          _M_buf_allocated;
00131 
00141       bool                      _M_reading;
00142       bool                      _M_writing;
00143 
00145 
00152       char_type         _M_pback; 
00153       char_type*        _M_pback_cur_save;
00154       char_type*        _M_pback_end_save;
00155       bool          _M_pback_init; 
00157 
00158       // Cached codecvt facet.
00159       const __codecvt_type*     _M_codecvt;
00160 
00168       char*         _M_ext_buf;
00169 
00175       streamsize        _M_ext_buf_size;
00176 
00184       const char*       _M_ext_next;
00185       char*         _M_ext_end;
00186 
00194       void
00195       _M_create_pback()
00196       {
00197     if (!_M_pback_init)
00198       {
00199         _M_pback_cur_save = this->gptr();
00200         _M_pback_end_save = this->egptr();
00201         this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
00202         _M_pback_init = true;
00203       }
00204       }
00205 
00213       void
00214       _M_destroy_pback() throw()
00215       {
00216     if (_M_pback_init)
00217       {
00218         // Length _M_in_cur moved in the pback buffer.
00219         _M_pback_cur_save += this->gptr() != this->eback();
00220         this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
00221         _M_pback_init = false;
00222       }
00223       }
00224 
00225     public:
00226       // Constructors/destructor:
00233       basic_filebuf();
00234 
00238       virtual
00239       ~basic_filebuf()
00240       { this->close(); }
00241 
00242       // Members:
00246       bool
00247       is_open() const throw()
00248       { return _M_file.is_open(); }
00249 
00263       __filebuf_type*
00264       open(const char* __s, ios_base::openmode __mode);
00265 
00277       __filebuf_type*
00278       close() throw();
00279 
00280     protected:
00281       void
00282       _M_allocate_internal_buffer();
00283 
00284       void
00285       _M_destroy_internal_buffer() throw();
00286 
00287       // [27.8.1.4] overridden virtual functions
00288       virtual streamsize
00289       showmanyc();
00290 
00291       // Stroustrup, 1998, p. 628
00292       // underflow() and uflow() functions are called to get the next
00293       // charater from the real input source when the buffer is empty.
00294       // Buffered input uses underflow()
00295 
00296       virtual int_type
00297       underflow();
00298 
00299       virtual int_type
00300       pbackfail(int_type __c = _Traits::eof());
00301 
00302       // Stroustrup, 1998, p 648
00303       // The overflow() function is called to transfer characters to the
00304       // real output destination when the buffer is full. A call to
00305       // overflow(c) outputs the contents of the buffer plus the
00306       // character c.
00307       // 27.5.2.4.5
00308       // Consume some sequence of the characters in the pending sequence.
00309       virtual int_type
00310       overflow(int_type __c = _Traits::eof());
00311 
00312       // Convert internal byte sequence to external, char-based
00313       // sequence via codecvt.
00314       bool
00315       _M_convert_to_external(char_type*, streamsize);
00316 
00329       virtual __streambuf_type*
00330       setbuf(char_type* __s, streamsize __n);
00331 
00332       virtual pos_type
00333       seekoff(off_type __off, ios_base::seekdir __way,
00334           ios_base::openmode __mode = ios_base::in | ios_base::out);
00335 
00336       virtual pos_type
00337       seekpos(pos_type __pos,
00338           ios_base::openmode __mode = ios_base::in | ios_base::out);
00339 
00340       // Common code for seekoff and seekpos
00341       pos_type
00342       _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
00343 
00344       virtual int
00345       sync();
00346 
00347       virtual void
00348       imbue(const locale& __loc);
00349 
00350       virtual streamsize
00351       xsgetn(char_type* __s, streamsize __n);
00352 
00353       virtual streamsize
00354       xsputn(const char_type* __s, streamsize __n);
00355 
00356       // Flushes output buffer, then writes unshift sequence.
00357       bool
00358       _M_terminate_output();
00359 
00374       void
00375       _M_set_buffer(streamsize __off)
00376       {
00377     const bool __testin = _M_mode & ios_base::in;
00378     const bool __testout = _M_mode & ios_base::out;
00379     
00380     if (__testin && __off > 0)
00381       this->setg(_M_buf, _M_buf, _M_buf + __off);
00382     else
00383       this->setg(_M_buf, _M_buf, _M_buf);
00384 
00385     if (__testout && __off == 0 && _M_buf_size > 1 )
00386       this->setp(_M_buf, _M_buf + _M_buf_size - 1);
00387     else
00388       this->setp(NULL, NULL);
00389       }
00390     };
00391 
00392   // [27.8.1.5] Template class basic_ifstream
00401   template<typename _CharT, typename _Traits>
00402     class basic_ifstream : public basic_istream<_CharT, _Traits>
00403     {
00404     public:
00405       // Types:
00406       typedef _CharT                    char_type;
00407       typedef _Traits                   traits_type;
00408       typedef typename traits_type::int_type        int_type;
00409       typedef typename traits_type::pos_type        pos_type;
00410       typedef typename traits_type::off_type        off_type;
00411 
00412       // Non-standard types:
00413       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00414       typedef basic_istream<char_type, traits_type> __istream_type;
00415 
00416     private:
00417       __filebuf_type    _M_filebuf;
00418 
00419     public:
00420       // Constructors/Destructors:
00428       basic_ifstream() : __istream_type(), _M_filebuf()
00429       { this->init(&_M_filebuf); }
00430 
00441       explicit
00442       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
00443       : __istream_type(), _M_filebuf()
00444       {
00445     this->init(&_M_filebuf);
00446     this->open(__s, __mode);
00447       }
00448 
00455       ~basic_ifstream()
00456       { }
00457 
00458       // Members:
00465       __filebuf_type*
00466       rdbuf() const
00467       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00468 
00473       bool
00474       is_open()
00475       { return _M_filebuf.is_open(); }
00476 
00477       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00478       // 365. Lack of const-qualification in clause 27
00479       bool
00480       is_open() const
00481       { return _M_filebuf.is_open(); }
00482 
00494       void
00495       open(const char* __s, ios_base::openmode __mode = ios_base::in)
00496       {
00497     if (!_M_filebuf.open(__s, __mode | ios_base::in))
00498       this->setstate(ios_base::failbit);
00499     else
00500       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00501       // 409. Closing an fstream should clear error state
00502       this->clear();
00503       }
00504 
00511       void
00512       close()
00513       {
00514     if (!_M_filebuf.close())
00515       this->setstate(ios_base::failbit);
00516       }
00517     };
00518 
00519 
00520   // [27.8.1.8] Template class basic_ofstream
00529   template<typename _CharT, typename _Traits>
00530     class basic_ofstream : public basic_ostream<_CharT,_Traits>
00531     {
00532     public:
00533       // Types:
00534       typedef _CharT                    char_type;
00535       typedef _Traits                   traits_type;
00536       typedef typename traits_type::int_type        int_type;
00537       typedef typename traits_type::pos_type        pos_type;
00538       typedef typename traits_type::off_type        off_type;
00539 
00540       // Non-standard types:
00541       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00542       typedef basic_ostream<char_type, traits_type> __ostream_type;
00543 
00544     private:
00545       __filebuf_type    _M_filebuf;
00546 
00547     public:
00548       // Constructors:
00556       basic_ofstream(): __ostream_type(), _M_filebuf()
00557       { this->init(&_M_filebuf); }
00558 
00570       explicit
00571       basic_ofstream(const char* __s,
00572              ios_base::openmode __mode = ios_base::out|ios_base::trunc)
00573       : __ostream_type(), _M_filebuf()
00574       {
00575     this->init(&_M_filebuf);
00576     this->open(__s, __mode);
00577       }
00578 
00585       ~basic_ofstream()
00586       { }
00587 
00588       // Members:
00595       __filebuf_type*
00596       rdbuf() const
00597       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00598 
00603       bool
00604       is_open()
00605       { return _M_filebuf.is_open(); }
00606 
00607       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00608       // 365. Lack of const-qualification in clause 27
00609       bool
00610       is_open() const
00611       { return _M_filebuf.is_open(); }
00612 
00624       void
00625       open(const char* __s,
00626        ios_base::openmode __mode = ios_base::out | ios_base::trunc)
00627       {
00628     if (!_M_filebuf.open(__s, __mode | ios_base::out))
00629       this->setstate(ios_base::failbit);
00630     else
00631       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00632       // 409. Closing an fstream should clear error state
00633       this->clear();
00634       }
00635 
00642       void
00643       close()
00644       {
00645     if (!_M_filebuf.close())
00646       this->setstate(ios_base::failbit);
00647       }
00648     };
00649 
00650 
00651   // [27.8.1.11] Template class basic_fstream
00660   template<typename _CharT, typename _Traits>
00661     class basic_fstream : public basic_iostream<_CharT, _Traits>
00662     {
00663     public:
00664       // Types:
00665       typedef _CharT                    char_type;
00666       typedef _Traits                   traits_type;
00667       typedef typename traits_type::int_type        int_type;
00668       typedef typename traits_type::pos_type        pos_type;
00669       typedef typename traits_type::off_type        off_type;
00670 
00671       // Non-standard types:
00672       typedef basic_filebuf<char_type, traits_type>     __filebuf_type;
00673       typedef basic_ios<char_type, traits_type>     __ios_type;
00674       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00675 
00676     private:
00677       __filebuf_type    _M_filebuf;
00678 
00679     public:
00680       // Constructors/destructor:
00688       basic_fstream()
00689       : __iostream_type(), _M_filebuf()
00690       { this->init(&_M_filebuf); }
00691 
00700       explicit
00701       basic_fstream(const char* __s,
00702             ios_base::openmode __mode = ios_base::in | ios_base::out)
00703       : __iostream_type(NULL), _M_filebuf()
00704       {
00705     this->init(&_M_filebuf);
00706     this->open(__s, __mode);
00707       }
00708 
00715       ~basic_fstream()
00716       { }
00717 
00718       // Members:
00725       __filebuf_type*
00726       rdbuf() const
00727       { return const_cast<__filebuf_type*>(&_M_filebuf); }
00728 
00733       bool
00734       is_open()
00735       { return _M_filebuf.is_open(); }
00736 
00737       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00738       // 365. Lack of const-qualification in clause 27
00739       bool
00740       is_open() const
00741       { return _M_filebuf.is_open(); }
00742 
00754       void
00755       open(const char* __s,
00756        ios_base::openmode __mode = ios_base::in | ios_base::out)
00757       {
00758     if (!_M_filebuf.open(__s, __mode))
00759       this->setstate(ios_base::failbit);
00760     else
00761       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00762       // 409. Closing an fstream should clear error state
00763       this->clear();
00764       }
00765 
00772       void
00773       close()
00774       {
00775     if (!_M_filebuf.close())
00776       this->setstate(ios_base::failbit);
00777       }
00778     };
00779 } // namespace std
00780 
00781 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00782 # include <bits/fstream.tcc>
00783 #endif
00784 
00785 #endif /* _GLIBCXX_FSTREAM */

Generated on Tue Feb 2 16:55:57 2010 for GNU C++ STL by  doxygen 1.4.7