28#if !defined(SEQAN3_HAS_BZIP2) && !defined(SEQAN3_HEADER_TEST)
29#error "This file cannot be used when building without BZIP2-support."
32#if defined(SEQAN3_HAS_BZIP2)
37namespace seqan3::contrib
44const size_t BZ2_OUTPUT_DEFAULT_BUFFER_SIZE = 4096;
50 typename ByteT = char,
53class basic_bz2_ostreambuf :
59 typedef ElemA char_allocator_type;
60 typedef ByteT byte_type;
61 typedef ByteAT byte_allocator_type;
62 typedef byte_type* byte_buffer_type;
63 typedef typename Tr::char_type char_type;
64 typedef typename Tr::int_type int_type;
68 using basic_streambuf_type::epptr;
69 using basic_streambuf_type::pbase;
70 using basic_streambuf_type::pptr;
73 ostream_reference ostream_,
74 size_t block_size_100k_ ,
80 ~basic_bz2_ostreambuf();
83 int_type overflow (int_type c);
88 uint64_t get_in_size()
const
90 return ((uint64_t)m_bzip2_stream.total_in_hi32 << 32)
91 + m_bzip2_stream.total_in_lo32;
93 uint64_t get_out_size()
const
95 return ((uint64_t)m_bzip2_stream.total_out_hi32 << 32)
96 + m_bzip2_stream.total_out_lo32;
100 size_t fill_input_buffer();
102 ostream_reference m_ostream;
103 bz_stream m_bzip2_stream;
105 byte_vector_type m_output_buffer;
106 char_vector_type m_buffer;
121 Elem,Tr,ElemA,ByteT,ByteAT
122 >:: basic_bz2_ostreambuf(
123 ostream_reference ostream_,
124 size_t block_size_100k_,
131 m_output_buffer(buffer_size_,0),
132 m_buffer(buffer_size_,0)
134 m_bzip2_stream.bzalloc=NULL;
135 m_bzip2_stream.bzfree=NULL;
137 m_bzip2_stream.next_in=NULL;
138 m_bzip2_stream.avail_in=0;
139 m_bzip2_stream.avail_out=0;
140 m_bzip2_stream.next_out=NULL;
142 m_err=BZ2_bzCompressInit(
144 std::min( 9,
static_cast<int>(block_size_100k_) ),
145 std::min( 4,
static_cast<int>(verbosity_) ),
146 std::min( 250,
static_cast<int>(work_factor_) )
149 this->setp( &(m_buffer[0]), &(m_buffer[m_buffer.size()-1]));
160 Elem,Tr,ElemA,ByteT,ByteAT
161 >::~basic_bz2_ostreambuf()
165 m_err=BZ2_bzCompressEnd(&m_bzip2_stream);
175int basic_bz2_ostreambuf<
176 Elem,Tr,ElemA,ByteT,ByteAT
179 if ( this->pptr() && this->pptr() > this->pbase())
181 int c = overflow( EOF);
197typename basic_bz2_ostreambuf<
198 Elem,Tr,ElemA,ByteT,ByteAT
200 basic_bz2_ostreambuf<
201 Elem,Tr,ElemA,ByteT,ByteAT
203 typename basic_bz2_ostreambuf<
204 Elem,Tr,ElemA,ByteT,ByteAT
208 int w =
static_cast<int>(this->pptr() - this->pbase());
213 if ( bzip2_to_stream( this->pbase(), w)) {
214 this->setp( this->pbase(), this->epptr());
227bool basic_bz2_ostreambuf<
228 Elem,Tr,ElemA,ByteT,ByteAT
230 typename basic_bz2_ostreambuf<
231 Elem,Tr,ElemA,ByteT,ByteAT
238 m_bzip2_stream.next_in=(byte_buffer_type)buffer_;
239 m_bzip2_stream.avail_in=buffer_size_*
sizeof(
char_type);
240 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size());
241 m_bzip2_stream.next_out=&(m_output_buffer[0]);
246 m_err = BZ2_bzCompress (&m_bzip2_stream, BZ_RUN );
248 if (m_err == BZ_RUN_OK || m_err == BZ_STREAM_END)
250 written_byte_size=
static_cast<std::streamsize>(m_output_buffer.size()) - m_bzip2_stream.avail_out;
251 total_written_byte_size+=written_byte_size;
254 (
const char_type*) &(m_output_buffer[0]),
259 if ( (remainder = written_byte_size%
sizeof(
char_type))!=0)
263 &(m_output_buffer[0]),
264 &(m_output_buffer[written_byte_size-remainder]),
269 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
270 m_bzip2_stream.next_out=&m_output_buffer[
remainder];
273 while (m_bzip2_stream.avail_in != 0 && m_err == BZ_RUN_OK);
275 return m_err == BZ_RUN_OK || m_err == BZ_FLUSH_OK;
286 Elem,Tr,ElemA,ByteT,ByteAT
287 >::flush(
int flush_mode)
291 int const buffer_size =
static_cast< int >( pptr() - pbase() );
293 m_bzip2_stream.next_in=(byte_buffer_type)pbase();
294 m_bzip2_stream.avail_in=
static_cast< unsigned int >(buffer_size*
sizeof(
char_type));
295 m_bzip2_stream.avail_out=
static_cast< unsigned int >(m_output_buffer.size());
296 m_bzip2_stream.next_out=&(m_output_buffer[0]);
301 m_err = BZ2_bzCompress (&m_bzip2_stream, flush_mode);
302 if (m_err == BZ_FINISH_OK || m_err == BZ_STREAM_END)
306 - m_bzip2_stream.avail_out;
307 total_written_byte_size+=written_byte_size;
310 (
const char_type*) &(m_output_buffer[0]),
315 if ( (remainder = written_byte_size%
sizeof(
char_type))!=0)
319 &(m_output_buffer[0]),
320 &(m_output_buffer[written_byte_size-remainder]),
325 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
326 m_bzip2_stream.next_out=&(m_output_buffer[
remainder]);
328 }
while (m_err == BZ_FINISH_OK);
332 return total_written_byte_size;
343 typename ByteT = char,
346class basic_bz2_ostreambase :
virtual public std::basic_ios<Elem,Tr>
350 typedef basic_bz2_ostreambuf<
351 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_streambuf_type;
353 basic_bz2_ostreambase(
354 ostream_reference ostream_,
355 size_t block_size_100k_ ,
360 : m_buf(ostream_,block_size_100k_, verbosity_, work_factor_, buffer_size_)
365 bzip2_streambuf_type*
rdbuf() {
return &m_buf; };
368 bzip2_streambuf_type m_buf;
379 typename ByteT = char,
382class basic_bz2_ostream :
383 public basic_bz2_ostreambase<Elem,Tr,ElemA,ByteT,ByteAT>,
387 typedef basic_bz2_ostreambase<
388 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_ostreambase_type;
390 typedef ostream_type& ostream_reference;
393 ostream_reference ostream_,
394 size_t block_size_100k_ = 9,
395 size_t verbosity_ = 0,
396 size_t work_factor_ = 30,
397 size_t buffer_size_ = BZ2_OUTPUT_DEFAULT_BUFFER_SIZE
400 bzip2_ostreambase_type(ostream_,block_size_100k_, verbosity_, work_factor_,buffer_size_),
401 ostream_type(bzip2_ostreambase_type::rdbuf())
406 basic_bz2_ostream& add_header();
407 basic_bz2_ostream& zflush()
409 this->
flush(); this->rdbuf()->flush();
return *
this;
414 void _Add_vtordisp1() { }
415 void _Add_vtordisp2() { }
423typedef basic_bz2_ostream<char> bz2_ostream;
424typedef basic_bz2_ostream<wchar_t> bz2_wostream;
typename stream::int_type int_type
Declares the associated int type.
typename stream::char_type char_type
Declares the associated char type.