|
/*
IoBind Library License:
--------------------------
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution
*/
#include <iobind/unzip_stream.hpp>
namespace iobind{
namespace zlib_stream{
unzip_stream::unzip_stream(
istream_reference istream_,
size_t window_size_,
size_t read_buffer_size_,
size_t input_buffer_size_
)
:
m_istream(istream_),
m_read_buffer(read_buffer_size_),
m_input_buffer(input_buffer_size_)
{
// setting zalloc, zfree and opaque
m_zip_stream.zalloc=(alloc_func)0;
m_zip_stream.zfree=(free_func)0;
m_zip_stream.next_in=NULL;
m_zip_stream.avail_in=0;
m_zip_stream.avail_out=0;
m_zip_stream.next_out=NULL;
m_err=inflateInit2(
&m_zip_stream,
static_cast<int>(window_size_)
);
};
unzip_stream::~unzip_stream()
{
inflateEnd(&m_zip_stream);
};
unzip_stream& unzip_stream::read( byte_vector_type& buffer_)
{
size_t read_buffer_size;
buffer_.clear();
m_zip_stream.avail_in=0;
do
{
if (m_zip_stream.avail_in==0)
fill_input_buffer();
reset_read_buffer_zip_stream();
if (m_zip_stream.avail_in)
{
m_err = inflate( &m_zip_stream, Z_SYNC_FLUSH );
if (m_err==Z_OK || m_err == Z_STREAM_END)
{
read_buffer_size = m_read_buffer.size() - m_zip_stream.avail_out;
if (read_buffer_size)
{
size_t bs=buffer_.size();
buffer_.resize( buffer_.size() + read_buffer_size );
std::copy(
m_read_buffer.begin(),
m_read_buffer.begin()+read_buffer_size,
buffer_.begin()+bs
);
}
}
}
} while (m_err==Z_OK);
if (!good())
buffer_.clear();
return *this;
};
unzip_stream& unzip_stream::read( std::ostream& ostream_)
{
size_t read_buffer_size;
m_zip_stream.avail_in=0;
do
{
if (m_zip_stream.avail_in==0)
fill_input_buffer();
reset_read_buffer_zip_stream();
if (m_zip_stream.avail_in)
{
m_err = inflate( &m_zip_stream, Z_SYNC_FLUSH );
if (m_err==Z_OK || m_err == Z_STREAM_END)
{
read_buffer_size = m_read_buffer.size() - m_zip_stream.avail_out;
if (read_buffer_size)
{
ostream_.write(
(const char*)(&(m_read_buffer[0])),
static_cast<std::streamsize>(read_buffer_size)
);
}
}
}
} while (m_err==Z_OK);
return *this;
}
unzip_stream& unzip_stream::read( std::string& string_)
{
std::ostringstream output_;
read(output_);
if (good())
string_=output_.str();
else
string_=zError( m_err );
return *this;
}
}; // zlib_stream
}; // iobind
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).