Whole document tree
    

Whole document tree

sbuf_iter.h Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sbuf_iter.h

Go to the documentation of this file.
00001 // Streambuf iterators
00002 
00003 // Copyright (C) 1997-2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 // XXX Should specialize copy, find algorithms for streambuf iterators.
00031 
00032 #ifndef _CPP_BITS_SBUF_ITER_H
00033 #define _CPP_BITS_SBUF_ITER_H 1
00034 
00035 #pragma GCC system_header
00036 
00037 namespace std
00038 {
00039   template<typename _CharT, typename _Traits>
00040     class ostreambuf_iterator
00041     : public iterator<output_iterator_tag, void, void, void, void>
00042     {
00043     public:
00044       // Types:
00045       typedef _CharT                           char_type;
00046       typedef _Traits                          traits_type;
00047       typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00048       typedef basic_ostream<_CharT, _Traits>   ostream_type;
00049 
00050     private:
00051       streambuf_type*   _M_sbuf;
00052       bool      _M_failed;
00053 
00054     public:
00055       inline 
00056       ostreambuf_iterator(ostream_type& __s) throw ()
00057       : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
00058       
00059       ostreambuf_iterator(streambuf_type* __s) throw ()
00060       : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
00061 
00062       ostreambuf_iterator& 
00063       operator=(_CharT __c);
00064 
00065       ostreambuf_iterator& 
00066       operator*() throw()
00067       { return *this; }
00068 
00069       ostreambuf_iterator& 
00070       operator++(int) throw()
00071       { return *this; }
00072 
00073       ostreambuf_iterator& 
00074       operator++() throw()
00075       { return *this; }
00076 
00077       bool 
00078       failed() const throw()
00079       { return _M_failed; }
00080     };
00081 
00082   template<typename _CharT, typename _Traits>
00083     inline ostreambuf_iterator<_CharT, _Traits>&
00084     ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c)
00085     {
00086       if (!_M_failed && 
00087           _Traits::eq_int_type(_M_sbuf->sputc(__c),_Traits::eof()))
00088       _M_failed = true;
00089       return *this;
00090     }
00091 
00092 
00093   // 24.5.3 Template class istreambuf_iterator
00094   template<typename _CharT, typename _Traits>
00095     class istreambuf_iterator
00096     : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
00097                   _CharT*, _CharT&>
00098     {
00099     public:
00100       // Types:
00101       typedef _CharT                                char_type;
00102       typedef _Traits                               traits_type;
00103       typedef typename _Traits::int_type            int_type;
00104       typedef basic_streambuf<_CharT, _Traits>      streambuf_type;
00105       typedef basic_istream<_CharT, _Traits>            istream_type;
00106       // Non-standard Types:
00107       typedef istreambuf_iterator<_CharT, _Traits>  __istreambufiter_type;
00108 
00109     private:
00110       // 24.5.3 istreambuf_iterator 
00111       // p 1 
00112       // If the end of stream is reached (streambuf_type::sgetc()
00113       // returns traits_type::eof()), the iterator becomes equal to
00114       // the "end of stream" iterator value.
00115       // NB: This implementation assumes the "end of stream" value
00116       // is EOF, or -1.
00117       streambuf_type*       _M_sbuf;  
00118       int_type          _M_c;
00119 
00120     public:
00121       istreambuf_iterator() throw() 
00122       : _M_sbuf(NULL), _M_c(-2) { }
00123       
00124       istreambuf_iterator(istream_type& __s) throw()
00125       : _M_sbuf(__s.rdbuf()), _M_c(-2) { }
00126 
00127       istreambuf_iterator(streambuf_type* __s) throw()
00128       : _M_sbuf(__s), _M_c(-2) { }
00129        
00130       // NB: This should really have an int_type return
00131       // value, so "end of stream" postion can be checked without
00132       // hacking.
00133       char_type 
00134       operator*() const
00135       { 
00136     // The result of operator*() on an end of stream is undefined.
00137     char_type __ret;
00138     if (_M_sbuf && _M_c != static_cast<int_type>(-2))
00139       __ret = _M_c;
00140     else if (_M_sbuf)
00141       __ret = traits_type::to_char_type(_M_sbuf->sgetc()); 
00142     else
00143       __ret = static_cast<char_type>(traits_type::eof());
00144     return __ret;
00145       }
00146     
00147       __istreambufiter_type& 
00148       operator++()
00149       { 
00150     if (_M_sbuf)
00151       _M_sbuf->sbumpc();
00152     _M_c = -2;
00153     return *this; 
00154       }
00155 
00156       __istreambufiter_type
00157       operator++(int)
00158       {
00159     __istreambufiter_type __old = *this;
00160     if (_M_sbuf)
00161       __old._M_c = _M_sbuf->sbumpc();
00162     _M_c = -2;
00163     return __old; 
00164       }
00165 
00166       bool 
00167       equal(const __istreambufiter_type& __b)
00168       { 
00169     int_type __eof = traits_type::eof();
00170     bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
00171     bool __beof = !__b._M_sbuf 
00172               || __b._M_sbuf->sgetc() == __eof;
00173     return (__thiseof && __beof || (!__thiseof && !__beof));
00174       }
00175 
00176 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00177       // 110 istreambuf_iterator::equal not const
00178       // NB: there is also number 111 pending on this function.
00179       bool 
00180       equal(const __istreambufiter_type& __b) const
00181       {
00182     int_type __eof = traits_type::eof();
00183     bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
00184     bool __beof = !__b._M_sbuf 
00185               || __b._M_sbuf->sgetc() == __eof;
00186     return (__thiseof && __beof || (!__thiseof && !__beof));
00187       }
00188 #endif
00189     };
00190 
00191   template<typename _CharT, typename _Traits>
00192     inline bool 
00193     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
00194            const istreambuf_iterator<_CharT, _Traits>& __b)
00195     { return __a.equal(__b); }
00196 
00197   template<typename _CharT, typename _Traits>
00198     inline bool 
00199     operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
00200            const istreambuf_iterator<_CharT, _Traits>& __b)
00201     { return !__a.equal(__b); }
00202 } // namespace std
00203 #endif

Generated on Mon Apr 8 03:11:31 2002 for libstdc++-v3 Source by doxygen1.2.15