Listing 5: <iterator>, part 9


        // TEMPLATE CLASS istreambuf_iterator
template<class E, class T = ios_traits<E> >
    class istreambuf_iterator
        : public input_iterator<E, T::off_type> {
public:
    typedef istreambuf_iterator<E, T> Mytype;
    typedef E char_type;
    typedef T traits_type;
    typedef T::int_type int_type;
    typedef basic_streambuf<E, T> streambuf_type;
    typedef basic_istream<E, T> istream_type;
    istreambuf_iterator(streambuf_type *sb = 0)
        : Sbuf(sb), Got(sb == 0) {}
    istreambuf_iterator(istream_type& is)
        : Sbuf(is.rdbuf()), Got(is.rdbuf() == 0) {}
    E operator*()
        {return (Got ? Val : Peek()); }
    const T *operator->() const
        {return (&**this); }
    Mytype& operator++()
        {Inc();
        return (*this); }
    Mytype operator++(int)
        {if (!Got)
            Peek();
        Mytype tmp = *this;
        Inc();
        return (tmp); }
    bool equal(const Mytype& x) const
        {if (!Got)
            ((Mytype *)this)->Peek();
        if (!x.Got)
            ((Mytype *)&x)->Peek();
        return (Sbuf == 0 && x.Sbuf == 0
            || Sbuf != 0 && x.Sbuf != 0); }
private:
    void Inc()
        {if (Sbuf == 0 || T::is_eof(Sbuf->sbumpc()))
            Sbuf = 0, Got = true;
        else
            Got = false; }
    E Peek()
        {int_type Cont;
        if (Sbuf == 0 || T::is_eof(Cont = Sbuf->sgetc()))
            Sbuf = 0;
        else
            Val = T::to_char_type(Cont);
        Got = true;
        return (Val); }
    streambuf_type *Sbuf;
    bool Got;
    E Val;
    };

        // istreambuf_iterator OPERATORS
template<class E, class T> inline
    bool operator==(const istreambuf_iterator<E, T>& x,
        const istreambuf_iterator<E, T>& y)
    {return (x.equal(y)); }

        // TEMPLATE CLASS ostreambuf_iterator
template<class E, class T = ios_traits<E> >
    class ostreambuf_iterator : public output_iterator {
    typedef ostreambuf_iterator<E, T> Myt;
public:
    typedef E char_type;
    typedef T traits_type;
    typedef basic_streambuf<E, T> streambuf_type;
    typedef basic_ostream<E, T> ostream_type;
    ostreambuf_iterator(streambuf_type *sb) throw()
        : Failed(false), Sbuf(sb) {}
    ostreambuf_iterator(ostream_type& os) throw()
        : Failed(false), Sbuf(os.rdbuf()) {}
    Myt& operator=(E x)
        {if (Sbuf == 0 || Sbuf->sputc(x) == T::eof())
            Failed = true;
        return (*this); }
    Myt& operator*()
        {return (*this); }
    Myt& operator++()
        {return (*this); }
    Myt& operator++(int)
        {return (*this); }
    bool failed() const throw()
        {return (Failed); }
private:
    bool Failed;
    streambuf_type *Sbuf;
    };
};    // end of namespace std
#endif /* _ITERATOR_ */
/* End of File */