This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Shouldn't this work?


The old thread was http://gcc.gnu.org/ml/libstdc++/2002-11/msg00335.html
and this is a missing followup.

On Tue, Dec 03, 2002 at 11:28:09AM -0600, Benjamin Kosnik wrote:
> 
> Probably would be easier to figure this out if you had a full,
> self-contained test case.

Like so.

    fenric 36% cat c.cc
    #include <iostream>
    #include <streambuf>
    #include <fstream>

    class comment_filter : public std::streambuf
    {
    public:
        typedef std::streambuf   base;

        explicit comment_filter (base *p) : real(p) {}

        virtual int_type
        underflow()
        {
            int_type   ret = real->sbumpc();

            std::cerr << "have read " << (char)ret << std::endl;

            if (ret == '#' || ret == ';')
            {
                do
                {
                    ret = real->sbumpc();
                }
                while (ret != '\n' && ret != traits_type::eof());
            }
            std::cerr << "ret is " << (char)ret << std::endl;
            return ret;
        }

    private:
        base   *real;
    };


    int main()
    {
        std::filebuf   *file = new std::filebuf;
        file->open("something", std::ios::in);
        comment_filter *filter = new comment_filter(file);

        std::istream    foo (filter);

        int i1 = 998, i2 = 999;  // "unset" values
        foo >> i1 >> i2;

        std::cout << "i1 is " << i1 << " and i2 is " << i2 << std::endl;

        return 0;
    }
    fenric 37% cat something
    123456
    123456
    fenric 38% g++ c.cc
    fenric 39% ./a.out
    have read 1
    ret is 1
    have read 2
    ret is 2
    have read 3
    ret is 3
    have read 4
    ret is 4
    i1 is 2 and i2 is 999
    fenric 40%

Identical results using 3.3 and trunk from this morning.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]