OSDN Git Service

2002-04-29 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
1 // Streambuf iterators
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // XXX Should specialize copy, find algorithms for streambuf iterators.
32
33 /** @file streambuf_iterator.h
34  *  This is an internal header file, included by other library headers.
35  *  You should not attempt to use it directly.
36  */
37
38 #ifndef _CPP_BITS_STREAMBUF_ITERATOR_H
39 #define _CPP_BITS_STREAMBUF_ITERATOR_H 1
40
41 #pragma GCC system_header
42
43 namespace std
44 {
45   // 24.5.3 Template class istreambuf_iterator
46   template<typename _CharT, typename _Traits>
47     class istreambuf_iterator
48     : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
49                       _CharT*, _CharT&>
50     {
51     public:
52       // Types:
53       typedef _CharT                                    char_type;
54       typedef _Traits                                   traits_type;
55       typedef typename _Traits::int_type                int_type;
56       typedef basic_streambuf<_CharT, _Traits>          streambuf_type;
57       typedef basic_istream<_CharT, _Traits>            istream_type;
58
59     private:
60       // 24.5.3 istreambuf_iterator 
61       // p 1 
62       // If the end of stream is reached (streambuf_type::sgetc()
63       // returns traits_type::eof()), the iterator becomes equal to
64       // the "end of stream" iterator value.
65       // NB: This implementation assumes the "end of stream" value
66       // is EOF, or -1.
67       mutable streambuf_type*   _M_sbuf;  
68       int_type                  _M_c;
69
70     public:
71       istreambuf_iterator() throw() 
72       : _M_sbuf(0), _M_c(-2) { }
73       
74       istreambuf_iterator(istream_type& __s) throw()
75       : _M_sbuf(__s.rdbuf()), _M_c(-2) { }
76
77       istreambuf_iterator(streambuf_type* __s) throw()
78       : _M_sbuf(__s), _M_c(-2) { }
79        
80       // NB: This should really have an int_type return
81       // value, so "end of stream" postion can be checked without
82       // hacking.
83       char_type 
84       operator*() const
85       { 
86         // The result of operator*() on an end of stream is undefined.
87         int_type __ret = traits_type::eof();
88         if (_M_sbuf)
89           { 
90             if (_M_c != static_cast<int_type>(-2))
91               __ret = _M_c;
92             else 
93               if ((__ret = _M_sbuf->sgetc()) == traits_type::eof())
94                 _M_sbuf = 0;
95           }
96         return traits_type::to_char_type(__ret);
97       }
98         
99       istreambuf_iterator& 
100       operator++()
101       { 
102         if (_M_sbuf && _M_sbuf->sbumpc() == traits_type::eof())
103           _M_sbuf = 0;
104         else
105           _M_c = -2;
106         return *this; 
107       }
108
109       istreambuf_iterator
110       operator++(int)
111       {
112         istreambuf_iterator __old = *this;
113         if (_M_sbuf && (__old._M_c = _M_sbuf->sbumpc()) == traits_type::eof())
114           _M_sbuf = 0;
115         else
116           _M_c = -2;
117         return __old; 
118       }
119
120 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
121       // 110 istreambuf_iterator::equal not const
122       // NB: there is also number 111 (NAD, Future) pending on this function.
123       bool 
124       equal(const istreambuf_iterator& __b) const
125       {
126         const int_type __eof = traits_type::eof();
127         bool __thiseof = traits_type::eq_int_type(this->operator*(), __eof);
128         bool __beof = traits_type::eq_int_type(__b.operator*(), __eof);
129         return (__thiseof && __beof || (!__thiseof && !__beof));
130       }
131 #endif
132     };
133
134   template<typename _CharT, typename _Traits>
135     inline bool 
136     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
137                const istreambuf_iterator<_CharT, _Traits>& __b)
138     { return __a.equal(__b); }
139
140   template<typename _CharT, typename _Traits>
141     inline bool 
142     operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
143                const istreambuf_iterator<_CharT, _Traits>& __b)
144     { return !__a.equal(__b); }
145
146   template<typename _CharT, typename _Traits>
147     class ostreambuf_iterator
148     : public iterator<output_iterator_tag, void, void, void, void>
149     {
150     public:
151       // Types:
152       typedef _CharT                           char_type;
153       typedef _Traits                          traits_type;
154       typedef basic_streambuf<_CharT, _Traits> streambuf_type;
155       typedef basic_ostream<_CharT, _Traits>   ostream_type;
156
157     private:
158       streambuf_type*   _M_sbuf;
159       bool              _M_failed;
160
161     public:
162       inline 
163       ostreambuf_iterator(ostream_type& __s) throw ()
164       : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
165       
166       ostreambuf_iterator(streambuf_type* __s) throw ()
167       : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
168
169       ostreambuf_iterator& 
170       operator=(_CharT __c);
171
172       ostreambuf_iterator& 
173       operator*() throw()
174       { return *this; }
175
176       ostreambuf_iterator& 
177       operator++(int) throw()
178       { return *this; }
179
180       ostreambuf_iterator& 
181       operator++() throw()
182       { return *this; }
183
184       bool 
185       failed() const throw()
186       { return _M_failed; }
187     };
188
189   template<typename _CharT, typename _Traits>
190     inline ostreambuf_iterator<_CharT, _Traits>&
191     ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c)
192     {
193       if (!_M_failed && 
194           _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
195         _M_failed = true;
196       return *this;
197     }
198 } // namespace std
199 #endif