Files
iostreams/doc/examples/zlib_output.html
2005-02-06 21:09:54 +00:00

103 lines
2.7 KiB
HTML
Executable File

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<LINK REL='stylesheet' HREF='../../../../boost.css'>
<LINK REL='stylesheet' HREF='../theme/iostreams.css'>
<STYLE>
PRE { background-color: white; border:0; font-size: 10pt }
</STYLE>
<TITLE>Zlib Example Output</TITLE>
</HEAD>
<BODY>
<PRE>
**** Length of code ****
883
**** Length of compressed code ****
458
**** Code before compression ****
class zlib_compressor : public one_step_output_filter {
public:
explicit zlib_compressor(int level = Z_DEFAULT_COMPRESSION)
: level_(level)
{
assert( level_ >= 0 && level_ <= 9 ||
level == Z_DEFAULT_COMPRESSION );
}
protected:
void do_filter(const vector_type& src, vector_type& dest)
{
dest.assign(src.begin(), src.end());
dest.insert(dest.end(), (size_t) ceil(src.size() * 1.001) + 12, 0);
unsigned long length = (unsigned long) dest.size();
int result =
compress2( (Bytef*) &dest[0], &length,
(Bytef*) &src[0], (unsigned long) src.size(),
level_ );
if (result == Z_OK)
dest.erase(dest.begin() + length, dest.end());
else if (result == Z_MEM_ERROR)
throw std::bad_alloc();
else if (result == Z_STREAM_ERROR)
throw std::ios_base::failure("invalid level");
}
private:
int level_;
};
**** Code after decompression ****
class zlib_compressor : public one_step_output_filter {
public:
explicit zlib_compressor(int level = Z_DEFAULT_COMPRESSION)
: level_(level)
{
assert( level_ >= 0 && level_ <= 9 ||
level == Z_DEFAULT_COMPRESSION );
}
protected:
void do_filter(const vector_type& src, vector_type& dest)
{
dest.assign(src.begin(), src.end());
dest.insert(dest.end(), (size_t) ceil(src.size() * 1.001) + 12, 0);
unsigned long length = (unsigned long) dest.size();
int result =
compress2( (Bytef*) &dest[0], &length,
(Bytef*) &src[0], (unsigned long) src.size(),
level_ );
if (result == Z_OK)
dest.erase(dest.begin() + length, dest.end());
else if (result == Z_MEM_ERROR)
throw std::bad_alloc();
else if (result == Z_STREAM_ERROR)
throw std::ios_base::failure("invalid level");
}
private:
int level_;
};
</PRE>
<!-- Begin Footer -->
<HR>
<P CLASS='copyright'>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
20 May, 2004
<!--webbot bot="Timestamp" endspan i-checksum="38504" -->
</P>
<P CLASS='copyright'>&copy; Copyright Jonathan Turkanis, 2004</P>
<P CLASS='copyright'>
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF='http://www.boost.org/LICENSE_1_0.txt'>http://www.boost.org/LICENSE_1_0.txt</A>)
</P>
<!-- End Footer -->
</BODY>