OSDN Git Service

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