OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
1 // Streambuf iterators
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 /** @file streambuf_iterator.h
32  *  This is an internal header file, included by other library headers.
33  *  You should not attempt to use it directly.
34  */
35
36 #ifndef _CPP_BITS_STREAMBUF_ITERATOR_H
37 #define _CPP_BITS_STREAMBUF_ITERATOR_H 1
38
39 #pragma GCC system_header
40
41 #include <streambuf>
42
43 // NB: Should specialize copy, find algorithms for streambuf iterators.
44
45 namespace std
46 {
47   // 24.5.3 Template class istreambuf_iterator
48   template<typename _CharT, typename _Traits>
49     class istreambuf_iterator
50     : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
51                       _CharT*, _CharT&>
52     {
53     public:
54       // Types:
55       typedef _CharT                                    char_type;
56       typedef _Traits                                   traits_type;
57       typedef typename _Traits::int_type                int_type;
58       typedef basic_streambuf<_CharT, _Traits>          streambuf_type;
59       typedef basic_istream<_CharT, _Traits>            istream_type;
60
61     private:
62       // 24.5.3 istreambuf_iterator 
63       // p 1 
64       // If the end of stream is reached (streambuf_type::sgetc()
65       // returns traits_type::eof()), the iterator becomes equal to
66       // the "end of stream" iterator value.
67       // NB: This implementation assumes the "end of stream" value
68       // is EOF, or -1.
69       mutable streambuf_type*   _M_sbuf;  
70       int_type                  _M_c;
71
72     public:
73       istreambuf_iterator() throw() 
74       : _M_sbuf(0), _M_c(traits_type::eof()) { }
75       
76       istreambuf_iterator(istream_type& __s) throw()
77       : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
78
79       istreambuf_iterator(streambuf_type* __s) throw()
80       : _M_sbuf(__s), _M_c(traits_type::eof()) { }
81        
82       // NB: The result of operator*() on an end of stream is undefined.
83       char_type 
84       operator*() const
85       { return traits_type::to_char_type(_M_get()); }
86         
87       istreambuf_iterator& 
88       operator++()
89       { 
90         const int_type __eof = traits_type::eof();
91         if (_M_sbuf && traits_type::eq_int_type(_M_sbuf->sbumpc(), __eof))
92           _M_sbuf = 0;
93         else
94           _M_c = __eof;
95         return *this; 
96       }
97
98       istreambuf_iterator
99       operator++(int)
100       {
101         const int_type __eof = traits_type::eof();
102         istreambuf_iterator __old = *this;
103         if (_M_sbuf
104             && traits_type::eq_int_type((__old._M_c = _M_sbuf->sbumpc()), 
105                                         __eof))
106           _M_sbuf = 0;
107         else
108           _M_c = __eof;
109         return __old; 
110       }
111
112 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
113       // 110 istreambuf_iterator::equal not const
114       // NB: there is also number 111 (NAD, Future) pending on this function.
115       bool 
116       equal(const istreambuf_iterator& __b) const
117       {
118         const int_type __eof = traits_type::eof();
119         bool __thiseof = traits_type::eq_int_type(_M_get(), __eof);
120         bool __beof = traits_type::eq_int_type(__b._M_get(), __eof);
121         return (__thiseof && __beof || (!__thiseof && !__beof));
122       }
123 #endif
124
125     private:
126       int_type 
127       _M_get() const
128       { 
129         const int_type __eof = traits_type::eof();
130         int_type __ret = __eof;
131         if (_M_sbuf)
132           { 
133             if (!traits_type::eq_int_type(_M_c, __eof))
134               __ret = _M_c;
135             else 
136               if (traits_type::eq_int_type((__ret = _M_sbuf->sgetc()), __eof))
137                 _M_sbuf = 0;
138           }
139         return __ret;
140       }
141     };
142
143   template<typename _CharT, typename _Traits>
144     inline bool 
145     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
146                const istreambuf_iterator<_CharT, _Traits>& __b)
147     { return __a.equal(__b); }
148
149   template<typename _CharT, typename _Traits>
150     inline bool 
151     operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
152                const istreambuf_iterator<_CharT, _Traits>& __b)
153     { return !__a.equal(__b); }
154
155   template<typename _CharT, typename _Traits>
156     class ostreambuf_iterator
157     : public iterator<output_iterator_tag, void, void, void, void>
158     {
159     public:
160       // Types:
161       typedef _CharT                           char_type;
162       typedef _Traits                          traits_type;
163       typedef basic_streambuf<_CharT, _Traits> streambuf_type;
164       typedef basic_ostream<_CharT, _Traits>   ostream_type;
165
166     private:
167       streambuf_type*   _M_sbuf;
168       bool              _M_failed;
169
170     public:
171       ostreambuf_iterator(ostream_type& __s) throw ()
172       : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
173       
174       ostreambuf_iterator(streambuf_type* __s) throw ()
175       : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
176
177       ostreambuf_iterator& 
178       operator=(_CharT __c)
179       {
180         if (!_M_failed && 
181             _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
182           _M_failed = true;
183         return *this;
184       }
185
186       ostreambuf_iterator& 
187       operator*() throw()
188       { return *this; }
189
190       ostreambuf_iterator& 
191       operator++(int) throw()
192       { return *this; }
193
194       ostreambuf_iterator& 
195       operator++() throw()
196       { return *this; }
197
198       bool 
199       failed() const throw()
200       { return _M_failed; }
201
202       ostreambuf_iterator& 
203       _M_put(const _CharT* __ws, streamsize __len)
204       {
205         if (__builtin_expect(!_M_failed, true)
206             && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, false))
207           _M_failed = true;
208         return *this;
209       }
210     };
211 } // namespace std
212 #endif