Writing an ostream filter?

I would like to write a simple ostream that ostream argument and somehow modifies the stream before passing it to the argument stream. Conversion is something as simple as changing a letter or erasing a word

What does a simple class inherit from ostream ? What methods should be redefined?

+4
source share
3 answers

std::ostream not the best place to implement filtering. It does not have the corresponding virtual functions so you can do this.

You will probably want to write a class derived from std::streambuf containing wrapped std::ostream (or wrapped std::streambuf ) and then create std::ostream using this std::streambuf .

std::streambuf has a virtual overflow function that you can override and use to change bytes before passing them to the class of completed output.

+2
source

I always thought that writing specialized threads is the wrong approach to almost any problem. The output stream is usually the end point in your program - any data processing should be performed long before you get to the stream itself. Similarly, for input streams - putting the intelligence necessary for (of course) syntactic input into the stream puts it in the wrong place. Only my 2 cents, of course.

0
source

Consider Using Boost.Iostreams

0
source

Source: https://habr.com/ru/post/1307927/


All Articles