Boost logo

Boost :

Subject: Re: [boost] [iostreams] array and gzip_decompressor
From: Chris Hamilton (chamilton_at_[hidden])
Date: 2010-03-01 07:43:54


> On 2/23/2010 10:48 PM, Nikolay Mladenov wrote:
>>
>> Is there another way I should be creating gzipped stream into memory?
>> Or maybe the arrays could be made peekable?
>>
>
> Perhaps you could read it into a stringstream.
>
> namespace io = boost::iostreams;
>
> io::filtering_istream in;
> in.push(io::gzip_decompressor());
> in.push(io::file_source(fileName.c_str(), std::ios_base::in |
> std::ios_base::binary));
> std::stringstream memStream;
> memStream << in.rdbuf();

If you don't know before-hand how big the output can be, you can write
into a std::vector relatively easily. The following creates an
ovectorstream object:

template<typename Ch>
struct basic_vector_sink
{
  typedef Ch char_type;
  typedef bio::sink_tag category;

  typedef std::vector<char_type> vector_type;

  basic_vector_sink( vector_type& vector ):
    _vector(vector)
  {
  }

  std::streamsize write( const char_type *buf, std::streamsize n )
  {
    if ( n <= 0 ) return n;
    size_t s = _vector.size();
    _vector.resize( s + n );
    memcpy( (void*)((&_vector[0])+s), (const void*)buf,
n*sizeof(char_type) );
    return n;
  }

  vector_type& _vector;
};
typedef basic_vector_sink<char> vector_sink;
typedef bio::stream<vector_sink> ovectorstream;

In my mind, something like arraystream (but for vectors) would be useful
in boost::iostreams. The above is a pale imitation of the full
functionality offered by an arraystream (bidirectional, seekable, etc),
but it does the job for me.

Cheers,

Chris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk