C++ – chaining c++ streams

c++iostream

I was thinking of "chaining" a couple of c++ iostreams toghether to filter input twice. I'm using gzstreams to read zlib compressed files and I was thinking of coding a stream that reads from a stream and performs encoding conversions. Perhaps by passing an opened stream as constructor parameter… How do you think this could be best accomplished?

Best Solution

I haven't used this but boost's filtering_stream may help.

As an example I found a mailing list post with indent.hpp, which implements an output filter that indents outputs:

boost::iostreams::filtering_ostream out; 
indent_filter::push(out,2); 
out.push(std::cout); 

And use it like so:

out << "Hello Filter!\n" 
    << indent_in 
    << "this is\n" 
    << "indented\n" 
    << indent_out 
    << "until here\n" 
    ; 

Which will result in output:

Hello Filter! 
  this is 
  indented 
until here