/* Copyright (C) 2004 Garrett A. Kajmowicz This file is part of the uClibc++ Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #ifndef __STD_HEADER_ITERATOR #define __STD_HEADER_ITERATOR 1 namespace std{ // subclause _lib.stream.iterators_, stream iterators: template , class Distance = ptrdiff_t> class istream_iterator; template bool operator==(const istream_iterator& x, const istream_iterator& y); template bool operator!=(const istream_iterator& x, const istream_iterator& y); template > class ostream_iterator; template > class istreambuf_iterator; template bool operator==(const istreambuf_iterator& a, const istreambuf_iterator& b); template bool operator!=(const istreambuf_iterator& a, const istreambuf_iterator& b); template > class ostreambuf_iterator; template < class T, class charT, class traits, class Distance > class _UCXXEXPORT istream_iterator : public iterator { public: typedef charT char_type; typedef traits traits_type; typedef basic_istream istream_type; istream_iterator() : in_stream(0), value(0) {} istream_iterator(istream_type& s) : in_stream(&s), value() { *in_stream >> value; } istream_iterator(const istream_iterator& x) : in_stream(x.in_stream), value(x.value) { } ~istream_iterator() { } const T& operator*() const{ return value; } const T* operator->() const{ return &value; } istream_iterator& operator++() { *in_stream >> value; return *this; } istream_iterator operator++(int){ istream_iterator tmp = *this; *in_stream >> value; return (tmp); } bool m_equal(const istream_iterator& x) const{ return (in_stream == x.in_stream); } private: basic_istream* in_stream; T value; }; template _UCXXEXPORT bool operator==(const istream_iterator& x, const istream_iterator& y) { return x.m_equal(y); } template _UCXXEXPORT bool operator!=(const istream_iterator& x, const istream_iterator& y) { return !(x == y); } template class _UCXXEXPORT ostream_iterator : public iterator { public: typedef charT char_type; typedef traits traits_type; typedef basic_ostream ostream_type; ostream_iterator(ostream_type& s) : out_stream(&s), delim(0) { } ostream_iterator(ostream_type& s, const charT* delimiter) : out_stream(&s), delim(delimiter) { } ostream_iterator(const ostream_iterator& x) : out_stream(x.out_stream), delim(x.delim) { } ~ostream_iterator() { } ostream_iterator& operator=(const T& value){ *out_stream << value; if(delim != 0){ *out_stream << delim; } return (*this); } ostream_iterator& operator*(){ return *this; } ostream_iterator& operator++() { return *this; } ostream_iterator operator++(int) { return *this; } private: basic_ostream* out_stream; const char* delim; }; template class _UCXXEXPORT istreambuf_iterator : public iterator { public: typedef charT char_type; typedef traits traits_type; typedef typename traits::int_type int_type; typedef basic_streambuf streambuf_type; typedef basic_istream istream_type; class _UCXXEXPORT proxy{ charT val; basic_streambuf * buf; proxy(charT v, basic_streambuf * b) : val(v), buf(b) { } public: charT operator*() { return val; } }; istreambuf_iterator() throw() : sbuf(0) { } istreambuf_iterator(istream_type& s) throw() : sbuf(s.rdbuf()) { } istreambuf_iterator(streambuf_type* s) throw() : sbuf(s) { } istreambuf_iterator(const proxy& p) throw() : sbuf(&p.buf) { } charT operator*() const{ return sbuf->sgetc(); } istreambuf_iterator& operator++(){ sbuf->sbumpc(); return *this; } proxy operator++(int){ istreambuf_iterator tmp = *this; sbuf->sbumpc(); return(tmp); } bool equal(const istreambuf_iterator& b) const{ return sbuf == b.sbuf || is_eof() && b.is_eof(); } private: streambuf_type* sbuf; inline bool is_eof() const{ return sbuf == 0 || sbuf->sgetc() == traits_type::eof(); } }; template _UCXXEXPORT bool operator==(const istreambuf_iterator& a, const istreambuf_iterator& b) { return a.equal(b); } template bool _UCXXEXPORT operator!=(const istreambuf_iterator& a, const istreambuf_iterator& b) { return !a.equal(b); } template class _UCXXEXPORT ostreambuf_iterator : iterator { public: typedef charT char_type; typedef traits traits_type; typedef basic_streambuf streambuf_type; typedef basic_ostream ostream_type; public: ostreambuf_iterator(ostream_type& s) throw() : sbuf(s.rdbuf()), f(false) { } ostreambuf_iterator(streambuf_type* s) throw() : sbuf(s), f(false) { } ostreambuf_iterator& operator=(charT c){ if(failed() == false){ if(sbuf->sputc(c) == traits::eof()){ f = true; } } return *this; } ostreambuf_iterator& operator*(){ return *this; } ostreambuf_iterator& operator++() { return *this; } ostreambuf_iterator operator++(int) { return *this; } bool failed() const throw(){ return f; } private: streambuf_type* sbuf; bool f; }; } #endif