2

I want to write a simple istream object, that would simply transform another istream.

I want to only implement readline (which would read a line from the original stream, would process it, and return the processed line), and have some generic code that upon read would use my read line, cache it, and give the required amount of bytes as output.

Is there any class that would allow me to do that?

For example

struct mystream : istreamByReadLine {
  istream& s;
  mystream(istream& _s):s(_s){}
  virtual string getline() {
    string line;
    getline(s,line);
    f(line);
    return line;
  }
}

class istreamByReadLine : istream {
  ... // implementing everything needed to be istream compatible, using my
  ... // getline() virtual method
}
0

2 Answers 2

4

Have you looked at boost.iostreams? It does most of the grunt work for you (possibly not for your exact use case, but for C++ standard library streams in general).

Sign up to request clarification or add additional context in comments.

Comments

1

Are you sure this is the way to go? In similar cases, I've either defined a class (e.g. Line), with a >> operator which did what I wanted, and read that, e.g.:

Line line
while ( source >> line ) ...

The class itself can be very simple, with just a std::string member, and an operator std::string() const function which returns it. All of the filtering work would be done in the std::istream& operator>>( std::istream&, Line& dest ) function. Or I've installed a filtering streambuf in front of the normal streambuf ; Boost iostream has good support for this.

2 Comments

I want to give my stream to other functions that receives an istream.
In which case, you have to use an istream. If these other programs should see the filtered data, then you need a filtering streambuf; if they should see the raw data, then the solution with Line is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.