30#if !defined(SEQAN3_HAS_BZIP2) && !defined(SEQAN3_HEADER_TEST)
31#error "This file cannot be used when building without BZIP2-support."
34#if defined(SEQAN3_HAS_BZIP2)
39namespace seqan3::contrib
46const size_t BZ2_OUTPUT_DEFAULT_BUFFER_SIZE = 4096;
52 typename ByteT = char,
55class basic_bz2_ostreambuf :
61 typedef ElemA char_allocator_type;
62 typedef ByteT byte_type;
63 typedef ByteAT byte_allocator_type;
64 typedef byte_type* byte_buffer_type;
65 typedef typename Tr::char_type char_type;
66 typedef typename Tr::int_type int_type;
70 using basic_streambuf_type::epptr;
71 using basic_streambuf_type::pbase;
72 using basic_streambuf_type::pptr;
75 ostream_reference ostream_,
76 size_t block_size_100k_ ,
82 ~basic_bz2_ostreambuf();
85 int_type overflow (int_type c);
90 uint64_t get_in_size()
const
92 return ((uint64_t)m_bzip2_stream.total_in_hi32 << 32)
93 + m_bzip2_stream.total_in_lo32;
95 uint64_t get_out_size()
const
97 return ((uint64_t)m_bzip2_stream.total_out_hi32 << 32)
98 + m_bzip2_stream.total_out_lo32;
102 size_t fill_input_buffer();
104 ostream_reference m_ostream;
105 bz_stream m_bzip2_stream;
107 byte_vector_type m_output_buffer;
108 char_vector_type m_buffer;
123 Elem,Tr,ElemA,ByteT,ByteAT
124 >:: basic_bz2_ostreambuf(
125 ostream_reference ostream_,
126 size_t block_size_100k_,
133 m_output_buffer(buffer_size_,0),
134 m_buffer(buffer_size_,0)
136 m_bzip2_stream.bzalloc=NULL;
137 m_bzip2_stream.bzfree=NULL;
139 m_bzip2_stream.next_in=NULL;
140 m_bzip2_stream.avail_in=0;
141 m_bzip2_stream.avail_out=0;
142 m_bzip2_stream.next_out=NULL;
144 m_err=BZ2_bzCompressInit(
146 std::min( 9,
static_cast<int>(block_size_100k_) ),
147 std::min( 4,
static_cast<int>(verbosity_) ),
148 std::min( 250,
static_cast<int>(work_factor_) )
151 this->setp( &(m_buffer[0]), &(m_buffer[m_buffer.size()-1]));
162 Elem,Tr,ElemA,ByteT,ByteAT
163 >::~basic_bz2_ostreambuf()
167 m_err=BZ2_bzCompressEnd(&m_bzip2_stream);
177int basic_bz2_ostreambuf<
178 Elem,Tr,ElemA,ByteT,ByteAT
181 if ( this->pptr() && this->pptr() > this->pbase())
183 int c = overflow( EOF);
199typename basic_bz2_ostreambuf<
200 Elem,Tr,ElemA,ByteT,ByteAT
202 basic_bz2_ostreambuf<
203 Elem,Tr,ElemA,ByteT,ByteAT
205 typename basic_bz2_ostreambuf<
206 Elem,Tr,ElemA,ByteT,ByteAT
210 int w =
static_cast<int>(this->pptr() - this->pbase());
215 if ( bzip2_to_stream( this->pbase(), w)) {
216 this->setp( this->pbase(), this->epptr());
229bool basic_bz2_ostreambuf<
230 Elem,Tr,ElemA,ByteT,ByteAT
232 typename basic_bz2_ostreambuf<
233 Elem,Tr,ElemA,ByteT,ByteAT
234 >::char_type* buffer_,
240 m_bzip2_stream.next_in=(byte_buffer_type)buffer_;
241 m_bzip2_stream.avail_in=buffer_size_*
sizeof(char_type);
242 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size());
243 m_bzip2_stream.next_out=&(m_output_buffer[0]);
248 m_err = BZ2_bzCompress (&m_bzip2_stream, BZ_RUN );
250 if (m_err == BZ_RUN_OK || m_err == BZ_STREAM_END)
252 written_byte_size=
static_cast<std::streamsize>(m_output_buffer.size()) - m_bzip2_stream.avail_out;
255 (
const char_type*) &(m_output_buffer[0]),
260 if ( (remainder = written_byte_size%
sizeof(char_type))!=0)
264 &(m_output_buffer[0]),
265 &(m_output_buffer[written_byte_size-remainder]),
270 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
271 m_bzip2_stream.next_out=&m_output_buffer[
remainder];
274 while (m_bzip2_stream.avail_in != 0 && m_err == BZ_RUN_OK);
276 return m_err == BZ_RUN_OK || m_err == BZ_FLUSH_OK;
287 Elem,Tr,ElemA,ByteT,ByteAT
288 >::flush(
int flush_mode)
292 int const buffer_size =
static_cast< int >( pptr() - pbase() );
294 m_bzip2_stream.next_in=(byte_buffer_type)pbase();
295 m_bzip2_stream.avail_in=
static_cast< unsigned int >(buffer_size*
sizeof(char_type));
296 m_bzip2_stream.avail_out=
static_cast< unsigned int >(m_output_buffer.size());
297 m_bzip2_stream.next_out=&(m_output_buffer[0]);
302 m_err = BZ2_bzCompress (&m_bzip2_stream, flush_mode);
303 if (m_err == BZ_FINISH_OK || m_err == BZ_STREAM_END)
307 - m_bzip2_stream.avail_out;
308 total_written_byte_size+=written_byte_size;
311 (
const char_type*) &(m_output_buffer[0]),
312 static_cast<std::streamsize>( written_byte_size/
sizeof(char_type)*
sizeof(
char) )
316 if ( (remainder = written_byte_size%
sizeof(char_type))!=0)
320 &(m_output_buffer[0]),
321 &(m_output_buffer[written_byte_size-remainder]),
326 m_bzip2_stream.avail_out=
static_cast<unsigned int>(m_output_buffer.size()-
remainder);
327 m_bzip2_stream.next_out=&(m_output_buffer[
remainder]);
329 }
while (m_err == BZ_FINISH_OK);
333 return total_written_byte_size;
344 typename ByteT = char,
347class basic_bz2_ostreambase :
virtual public std::basic_ios<Elem,Tr>
351 typedef basic_bz2_ostreambuf<
352 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_streambuf_type;
354 basic_bz2_ostreambase(
355 ostream_reference ostream_,
356 size_t block_size_100k_ ,
361 : m_buf(ostream_,block_size_100k_, verbosity_, work_factor_, buffer_size_)
366 bzip2_streambuf_type*
rdbuf() {
return &m_buf; };
369 bzip2_streambuf_type m_buf;
380 typename ByteT = char,
383class basic_bz2_ostream :
384 public basic_bz2_ostreambase<Elem,Tr,ElemA,ByteT,ByteAT>,
388 typedef basic_bz2_ostreambase<
389 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_ostreambase_type;
391 typedef ostream_type& ostream_reference;
394 ostream_reference ostream_,
395 size_t block_size_100k_ = 9,
396 size_t verbosity_ = 0,
397 size_t work_factor_ = 30,
398 size_t buffer_size_ = BZ2_OUTPUT_DEFAULT_BUFFER_SIZE
401 bzip2_ostreambase_type(ostream_,block_size_100k_, verbosity_, work_factor_,buffer_size_),
402 ostream_type(bzip2_ostreambase_type::rdbuf())
407 basic_bz2_ostream& add_header();
408 basic_bz2_ostream& zflush()
410 this->
flush(); this->rdbuf()->flush();
return *
this;
415 void _Add_vtordisp1() { }
416 void _Add_vtordisp2() { }
424typedef basic_bz2_ostream<char> bz2_ostream;
425typedef basic_bz2_ostream<wchar_t> bz2_wostream;