27 #if !defined(SEQAN3_HAS_BZIP2) && !defined(SEQAN3_HEADER_TEST)
28 #error "This file cannot be used when building without BZIP2-support."
31 #if defined(SEQAN3_HAS_BZIP2)
36 namespace seqan3::contrib
43 const size_t BZ2_INPUT_DEFAULT_BUFFER_SIZE = 4096;
47 typename Tr = std::char_traits<Elem>,
48 typename ElemA = std::allocator<Elem>,
49 typename ByteT = char,
50 typename ByteAT = std::allocator<ByteT>
52 class basic_bz2_istreambuf :
53 public std::basic_streambuf<Elem, Tr>
56 typedef std::basic_istream<Elem, Tr>& istream_reference;
57 typedef ElemA char_allocator_type;
58 typedef ByteT byte_type;
59 typedef ByteAT byte_allocator_type;
60 typedef byte_type* byte_buffer_type;
61 typedef typename Tr::char_type char_type;
62 typedef typename Tr::int_type int_type;
63 typedef std::vector<byte_type, byte_allocator_type > byte_vector_type;
64 typedef std::vector<char_type, char_allocator_type > char_vector_type;
67 istream_reference istream_,
70 size_t read_buffer_size_,
71 size_t input_buffer_size_
74 ~basic_bz2_istreambuf();
78 istream_reference get_istream() {
return m_istream;};
79 bz_stream& get_bzip2_stream() {
return m_bzip2_stream;};
80 int get_zerr()
const {
return m_err;};
82 std::streamsize unbzip2_from_stream( char_type*, std::streamsize);
83 void put_back_from_bzip2_stream();
84 size_t fill_input_buffer();
86 istream_reference m_istream;
87 bz_stream m_bzip2_stream;
89 byte_vector_type m_input_buffer;
90 char_vector_type m_buffer;
104 basic_bz2_istreambuf<
105 Elem,Tr,ElemA,ByteT,ByteAT
106 >::basic_bz2_istreambuf(
107 istream_reference istream_,
110 size_t read_buffer_size_,
111 size_t input_buffer_size_
115 m_input_buffer(input_buffer_size_),
116 m_buffer(read_buffer_size_)
119 m_bzip2_stream.bzalloc=NULL;
120 m_bzip2_stream.bzfree=NULL;
122 m_bzip2_stream.next_in=NULL;
123 m_bzip2_stream.avail_in=0;
124 m_bzip2_stream.avail_out=0;
125 m_bzip2_stream.next_out=NULL;
128 m_err=BZ2_bzDecompressInit (
130 std::min(4,
static_cast<int>(verbosity_)),
131 static_cast<int>(small_)
147 size_t basic_bz2_istreambuf<
148 Elem,Tr,ElemA,ByteT,ByteAT
149 >::fill_input_buffer()
151 m_bzip2_stream.next_in=&(m_input_buffer[0]);
154 static_cast<std::streamsize
>(m_input_buffer.size()/
sizeof(
char_type))
156 return m_bzip2_stream.avail_in=m_istream.gcount()*
sizeof(
char_type);
166 void basic_bz2_istreambuf<
167 Elem,Tr,ElemA,ByteT,ByteAT
168 >::put_back_from_bzip2_stream()
170 if (m_bzip2_stream.avail_in==0)
173 m_istream.clear( std::ios::goodbit );
175 -
static_cast<int>(m_bzip2_stream.avail_in),
179 m_bzip2_stream.avail_in=0;
190 basic_bz2_istreambuf<
191 Elem,Tr,ElemA,ByteT,ByteAT
192 >::~basic_bz2_istreambuf()
194 BZ2_bzDecompressEnd(&m_bzip2_stream);
204 typename basic_bz2_istreambuf<
205 Elem,Tr,ElemA,ByteT,ByteAT
207 basic_bz2_istreambuf<
208 Elem,Tr,ElemA,ByteT,ByteAT
211 if ( this->gptr() && ( this->gptr() < this->egptr()))
212 return *
reinterpret_cast<unsigned char *
>( this->gptr());
214 int n_putback =
static_cast<int>(this->gptr() - this->eback());
218 &(m_buffer[0]) + (4 - n_putback),
219 this->gptr() - n_putback,
223 int num = unbzip2_from_stream(
225 static_cast<std::streamsize
>((m_buffer.size()-4)*
sizeof(
char_type))
232 &(m_buffer[0]) + (4 - n_putback),
234 &(m_buffer[0]) + 4 + num);
237 return*
reinterpret_cast<unsigned char *
>( this->gptr());
248 std::streamsize basic_bz2_istreambuf<
249 Elem,Tr,ElemA,ByteT,ByteAT
250 >::unbzip2_from_stream(
252 std::streamsize buffer_size_
255 m_bzip2_stream.next_out=(byte_buffer_type)buffer_;
256 m_bzip2_stream.avail_out=buffer_size_*
sizeof(
char_type);
257 size_t count =m_bzip2_stream.avail_in;
261 if (m_bzip2_stream.avail_in==0)
262 count=fill_input_buffer();
264 if (m_bzip2_stream.avail_in)
266 m_err = BZ2_bzDecompress( &m_bzip2_stream );
268 }
while (m_err==BZ_OK && m_bzip2_stream.avail_out != 0 &&
count != 0);
270 if (m_err == BZ_STREAM_END)
271 put_back_from_bzip2_stream();
273 return buffer_size_ - m_bzip2_stream.avail_out/
sizeof(
char_type);
282 typename Tr = std::char_traits<Elem>,
283 typename ElemA = std::allocator<Elem>,
284 typename ByteT = char,
285 typename ByteAT = std::allocator<ByteT>
287 class basic_bz2_istreambase :
virtual public std::basic_ios<Elem,Tr>
290 typedef std::basic_istream<Elem, Tr>& istream_reference;
291 typedef basic_bz2_istreambuf<
292 Elem,Tr,ElemA,ByteT,ByteAT> unbzip2_streambuf_type;
294 basic_bz2_istreambase(
295 istream_reference ostream_,
298 size_t read_buffer_size_,
299 size_t input_buffer_size_
312 unbzip2_streambuf_type* rdbuf() {
return &m_buf; };
315 unbzip2_streambuf_type m_buf;
324 typename Tr = std::char_traits<Elem>,
325 typename ElemA = std::allocator<Elem>,
326 typename ByteT = char,
327 typename ByteAT = std::allocator<ByteT>
329 class basic_bz2_istream :
330 public basic_bz2_istreambase<Elem,Tr,ElemA,ByteT,ByteAT>,
331 public std::basic_istream<Elem,Tr>
334 typedef basic_bz2_istreambase<
335 Elem,Tr,ElemA,ByteT,ByteAT> bzip2_istreambase_type;
336 typedef std::basic_istream<Elem,Tr> istream_type;
337 typedef istream_type& istream_reference;
338 typedef unsigned char byte_type;
341 istream_reference istream_,
342 size_t verbosity_ = 0,
344 size_t read_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE,
345 size_t input_buffer_size_ = BZ2_INPUT_DEFAULT_BUFFER_SIZE
348 bzip2_istreambase_type(istream_,verbosity_, small_, read_buffer_size_, input_buffer_size_),
349 istream_type(bzip2_istreambase_type::rdbuf())
353 void _Add_vtordisp1() { }
354 void _Add_vtordisp2() { }
362 typedef basic_bz2_istream<char> bz2_istream;
363 typedef basic_bz2_istream<wchar_t> bz2_wistream;
Adaptations of algorithms from the Ranges TS.
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: traits.hpp:169
typename stream::int_type int_type
Declares the associated int type.
typename stream::char_type char_type
Declares the associated char type.