27 #ifndef SEQAN3_HAS_BZIP2
28 #error "This file cannot be used when building without BZIP2-support."
34 namespace seqan3::contrib
41 const size_t BZ2_OUTPUT_DEFAULT_BUFFER_SIZE = 4096;
47 typename ByteT = char,
50 class basic_bz2_ostreambuf :
56 typedef ElemA char_allocator_type;
57 typedef ByteT byte_type;
58 typedef ByteAT byte_allocator_type;
59 typedef byte_type* byte_buffer_type;
60 typedef typename Tr::char_type char_type;
61 typedef typename Tr::int_type int_type;
70 ostream_reference ostream_,
71 size_t block_size_100k_ ,
77 ~basic_bz2_ostreambuf();
85 uint64_t get_in_size()
const
87 return ((uint64_t)m_bzip2_stream.total_in_hi32 << 32)
88 + m_bzip2_stream.total_in_lo32;
90 uint64_t get_out_size()
const
92 return ((uint64_t)m_bzip2_stream.total_out_hi32 << 32)
93 + m_bzip2_stream.total_out_lo32;
97 size_t fill_input_buffer();
99 ostream_reference m_ostream;
100 bz_stream m_bzip2_stream;
102 byte_vector_type m_output_buffer;
103 char_vector_type m_buffer;
117 basic_bz2_ostreambuf<
118 Elem,Tr,ElemA,ByteT,ByteAT
119 >:: basic_bz2_ostreambuf(
120 ostream_reference ostream_,
121 size_t block_size_100k_,
128 m_output_buffer(buffer_size_,0),
129 m_buffer(buffer_size_,0)
131 m_bzip2_stream.bzalloc=NULL;
132 m_bzip2_stream.bzfree=NULL;
134 m_bzip2_stream.next_in=NULL;
135 m_bzip2_stream.avail_in=0;
136 m_bzip2_stream.avail_out=0;
137 m_bzip2_stream.next_out=NULL;
139 m_err=BZ2_bzCompressInit(
141 std::min( 9,
static_cast<int>(block_size_100k_) ),
142 std::min( 4,
static_cast<int>(verbosity_) ),
143 std::min( 250,
static_cast<int>(work_factor_) )
146 this->setp( &(m_buffer[0]), &(m_buffer[m_buffer.size()-1]));
156 basic_bz2_ostreambuf<
157 Elem,Tr,ElemA,ByteT,ByteAT
158 >::~basic_bz2_ostreambuf()
162 m_err=BZ2_bzCompressEnd(&m_bzip2_stream);
172 int basic_bz2_ostreambuf<
173 Elem,Tr,ElemA,ByteT,ByteAT
176 if ( this->pptr() && this->pptr() > this->pbase())
178 int c = overflow( EOF);
194 typename basic_bz2_ostreambuf<
195 Elem,Tr,ElemA,ByteT,ByteAT
197 basic_bz2_ostreambuf<
198 Elem,Tr,ElemA,ByteT,ByteAT
200 typename basic_bz2_ostreambuf<
201 Elem,Tr,ElemA,ByteT,ByteAT
205 int w =
static_cast<int>(this->pptr() - this->pbase());
210 if ( bzip2_to_stream( this->pbase(), w)) {
211 this->setp( this->pbase(), this->epptr());
224 bool basic_bz2_ostreambuf<
225 Elem,Tr,ElemA,ByteT,ByteAT
227 typename basic_bz2_ostreambuf<
228 Elem,Tr,ElemA,ByteT,ByteAT
235 m_bzip2_stream.next_in=(byte_buffer_type)buffer_;
236 m_bzip2_stream.avail_in=buffer_size_*
sizeof(
char_type);
237 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size());
238 m_bzip2_stream.next_out=&(m_output_buffer[0]);
243 m_err = BZ2_bzCompress (&m_bzip2_stream, BZ_RUN );
245 if (m_err == BZ_RUN_OK || m_err == BZ_STREAM_END)
247 written_byte_size=
static_cast<std::streamsize>(m_output_buffer.size()) - m_bzip2_stream.avail_out;
248 total_written_byte_size+=written_byte_size;
251 (
const char_type*) &(m_output_buffer[0]),
256 if ( (remainder = written_byte_size%
sizeof(
char_type))!=0)
260 &(m_output_buffer[0]),
261 &(m_output_buffer[written_byte_size-remainder]),
266 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
267 m_bzip2_stream.next_out=&m_output_buffer[
remainder];
270 while (m_bzip2_stream.avail_in != 0 && m_err == BZ_RUN_OK);
272 return m_err == BZ_RUN_OK || m_err == BZ_FLUSH_OK;
283 Elem,Tr,ElemA,ByteT,ByteAT
284 >::flush(
int flush_mode)
288 int const buffer_size =
static_cast< int >( pptr() - pbase() );
290 m_bzip2_stream.next_in=(byte_buffer_type)pbase();
291 m_bzip2_stream.avail_in=
static_cast< unsigned int >(buffer_size*
sizeof(
char_type));
292 m_bzip2_stream.avail_out=
static_cast< unsigned int >(m_output_buffer.size());
293 m_bzip2_stream.next_out=&(m_output_buffer[0]);
298 m_err = BZ2_bzCompress (&m_bzip2_stream, flush_mode);
299 if (m_err == BZ_FINISH_OK || m_err == BZ_STREAM_END)
303 - m_bzip2_stream.avail_out;
304 total_written_byte_size+=written_byte_size;
307 (
const char_type*) &(m_output_buffer[0]),
312 if ( (remainder = written_byte_size%
sizeof(
char_type))!=0)
316 &(m_output_buffer[0]),
317 &(m_output_buffer[written_byte_size-remainder]),
322 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
323 m_bzip2_stream.next_out=&(m_output_buffer[
remainder]);
325 }
while (m_err == BZ_FINISH_OK);
329 return total_written_byte_size;
340 typename ByteT = char,
343 class basic_bz2_ostreambase :
virtual public std::basic_ios<Elem,Tr>
347 typedef basic_bz2_ostreambuf<
348 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_streambuf_type;
350 basic_bz2_ostreambase(
351 ostream_reference ostream_,
352 size_t block_size_100k_ ,
357 : m_buf(ostream_,block_size_100k_, verbosity_, work_factor_, buffer_size_)
362 bzip2_streambuf_type*
rdbuf() {
return &m_buf; };
365 bzip2_streambuf_type m_buf;
376 typename ByteT = char,
379 class basic_bz2_ostream :
380 public basic_bz2_ostreambase<Elem,Tr,ElemA,ByteT,ByteAT>,
384 typedef basic_bz2_ostreambase<
385 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_ostreambase_type;
387 typedef ostream_type& ostream_reference;
390 ostream_reference ostream_,
391 size_t block_size_100k_ = 9,
392 size_t verbosity_ = 0,
393 size_t work_factor_ = 30,
394 size_t buffer_size_ = BZ2_OUTPUT_DEFAULT_BUFFER_SIZE
397 bzip2_ostreambase_type(ostream_,block_size_100k_, verbosity_, work_factor_,buffer_size_),
398 ostream_type(bzip2_ostreambase_type::rdbuf())
403 basic_bz2_ostream& add_header();
404 basic_bz2_ostream& zflush()
406 this->
flush(); this->
rdbuf()->flush();
return *
this;
411 void _Add_vtordisp1() { }
412 void _Add_vtordisp2() { }
420 typedef basic_bz2_ostream<char> bz2_ostream;
421 typedef basic_bz2_ostream<wchar_t> bz2_wostream;