Some basic questions about stream_flow filtering optimization. I have dozens of functions that take the std :: ofstream & parameter
void foo(std::ofstream& outStream) { // lots of operations, like this: outStream << "various bits of text"; } void StreamSomeTextToFile(char* fileName) { ofstream myFileStream(fileName, ios::out | ios::app | ios::binary); foo(myFileStream); myFileStream.close(); }
Now I would like to use boost filtering_stream to output to a compressed ZIP file. The often quoted test code boost filtering_streams for packaging and unpacking is compiled, linked, and works great for me. I would like to replace filtering_stream:
void StreamSomeCompressedTextToFile(char* fileName) { ofstream myFileStream(destPath, std::ios_base::out | std::ios_base::app | std::ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::output> myCompressedFileStream; myCompressedFileStream.push(boost::iostreams::zlib_compressor()); myCompressedFileStream.push(myFileStream); foo(myCompressedFileStream);
THREE QUESTIONS:
1) Perform all of my functions that previously took std :: ofstream & outStream should now accept a parameter like boost :: iostreams :: filtering_streambuf &? Or is there a valid parameter type so that these numerous ("foo") functions can work with a type of type ETHER?
2) In my simple test cases, I was unable to use the stream operator syntax with filtering_streambuf:
myCompressedFileStream << "some text";
this generated an error: there is no match for 'operator <<'. I also had compilation errors with write ():
error: 'class boost::iostreams::filtering_streambuf<boost::iostreams::output, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::public_>' has no member named 'write
3) In the general test case example (below), I was confused by the fact that I could not find the file "hello.z" after creating it. The unpack code (also below) explicitly refers to it - so where can I find it? NOTE: the location was finally discovered: it was in / Library / Preferences /
void pack() { std::ofstream file("hello.z", std::ios_base::out | std::ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::output> out; out.push(boost::iostreams::zlib_compressor()); out.push(file); char data[5] = {'a', 'b', 'c', 'd', 'e'}; boost::iostreams::copy(boost::iostreams::basic_array_source<char>(data, sizeof(data)), out); file.close(); } void unpack() { std::fstream file("hello.z", std::ios_base::in | std::ios_base::binary); boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::zlib_decompressor()); in.push(file); boost::iostreams::copy(in, std::cout); }
BTW: Xcode 3.2.6, GNU 4.0, OS X 10.6.8