Boost logo

Boost Users :

From: Daniel Oberhoff (daniel_at_[hidden])
Date: 2007-07-05 09:08:41


Hi,

I have a homebrewed matlab file library for which I am considering
using the boost filtering streams for zlib compression. In Matlab(tm)
files, variables may be compressed individually, meaning there will
be compressed blocks inside the file surrounded by umcompressed data.
Thus I would like to have (de)compressors which I can plug in at the
start of the block and take back out when I am done. I went and
applied the boost filtering streams but I have some problems, so
before I go into hardcore debugging I would like to ask if I am using
the lib right (because the docs are a bit sparse at the relevant
spots). This is how I start up the stream(s):

     File::File(std::string path, std::ios_base::openmode mode)
        : _fstream(path.c_str(), mode | std::ios_base::binary)
{
        if (!_fstream.is_open())
            throw_error("could not open matfile " << path);
        _istream.set_auto_close(false);
        _ostream.set_auto_close(false);
        _istream.push(_fstream);
        _ostream.push(_fstream);
}

and these are the methods for starting/stopping the compressed blocks
for reading/writing:

     void File::startunzipping(std::streampos len)
     {
        stopunzipping();
        _unzend = _fstream.tellg() + len;
        _unzcount = 0;
        _istream.pop();
        _istream.push(boost::iostreams::zlib_decompressor());
        _istream.push(_fstream);
        _unzipping = true;
     }

     void File::stopunzipping()
     {
        if (_unzipping==false) return;
        _istream.pop();
        _istream.pop();
        _istream.push(_fstream);
        _unzipping = false;
        _fstream.seekg(_unzend);
     }

     void File::startzipping()
     {
        stopzipping();
        write_type_and_size(TypeAndSize(Types::Compressed,0),false);
        _zstart = _fstream.tellp();
        _zcount = 0;
        _ostream.pop();
        _ostream.push(boost::iostreams::zlib_compressor());
        _ostream.push(_fstream);
        _zipping = true;
     }

     void File::stopzipping()
     {
        if (_zipping==false) return;
        _ostream.flush();
        _ostream.pop();
        _ostream.pop();
        _ostream.push(_fstream);
        _zipping = false;
        _zend = _fstream.tellp();
     }

Two open questions that come to mind:

1) does the compressor flush the zlib compression buffer when I flush/
pop it?
2) is fstream a bidirectional stream (my guess due to separate seekg/
seekp methods) or a seekable stream (one read/write head)? Because I
currently require the former.

btw: the above code runs without exceptions, but when I read back
variables I have just written they are garbled.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net