26 :
public std::ios_base::failure
31 std::string msg =
"zlib: ";
35 msg +=
"Z_STREAM_ERROR: ";
38 msg +=
"Z_DATA_ERROR: ";
41 msg +=
"Z_MEM_ERROR: ";
44 msg +=
"Z_VERSION_ERROR: ";
47 msg +=
"Z_BUF_ERROR: ";
50 std::ostringstream oss;
52 msg +=
"[" + oss.str() +
"]: ";
87 this->zalloc =
nullptr;
88 this->zfree =
nullptr;
89 this->opaque =
nullptr;
94 this->next_in =
nullptr;
95 ret = inflateInit2(
this, _window_bits ? _window_bits : 15+32);
99 ret = deflateInit2(
this, _level, Z_DEFLATED, _window_bits ? _window_bits : 15+16, 8, Z_DEFAULT_STRATEGY);
101 if (ret != Z_OK)
throw Exception(
this, ret);
121 :
public std::streambuf
125 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
149 pos_type
seekoff(off_type off, std::ios_base::seekdir dir,
150 std::ios_base::openmode which)
override
152 if (off != 0 || dir != std::ios_base::cur) {
153 return std::streambuf::seekoff(off, dir, which);
160 return static_cast<long int>(
zstrm_p->total_out -
static_cast<uLong
>(in_avail()));
165 if (this->gptr() == this->egptr())
168 char * out_buff_free_start =
out_buff.get();
172 if (++tries > 1000) {
173 throw std::ios_base::failure(
"Failed to fill buffer after 1000 tries");
189 unsigned char b0 = *
reinterpret_cast< unsigned char *
>(
in_buff_start);
190 unsigned char b1 = *
reinterpret_cast< unsigned char *
>(
in_buff_start + 1);
195 && ((b0 == 0x1F && b1 == 0x8B)
196 || (b0 == 0x78 && (b1 == 0x01
215 zstrm_p->next_out =
reinterpret_cast< decltype(
zstrm_p-
>next_out) >(out_buff_free_start);
217 int ret = inflate(
zstrm_p.get(), Z_NO_FLUSH);
219 if (ret != Z_OK && ret != Z_STREAM_END)
throw Exception(
zstrm_p.get(), ret);
223 out_buff_free_start =
reinterpret_cast< decltype(out_buff_free_start)
>(
zstrm_p->next_out);
226 if (ret == Z_STREAM_END) {
231 }
while (out_buff_free_start ==
out_buff.get());
237 return this->gptr() == this->egptr()
239 : traits_type::to_int_type(*this->gptr());
247 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
257 :
public std::streambuf
261 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
265 zstrm_p(new detail::z_stream_wrapper(false, _level, _window_bits)),
283 int ret = deflate(
zstrm_p.get(), flush);
284 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
294 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
316 std::streambuf::int_type
overflow(std::streambuf::int_type c = traits_type::eof())
override
318 zstrm_p->next_in =
reinterpret_cast< decltype(
zstrm_p-
>next_in) >(pbase());
319 zstrm_p->avail_in = uint32_t(pptr() - pbase());
325 setp(
nullptr,
nullptr);
326 return traits_type::eof();
330 return traits_type::eq_int_type(c, traits_type::eof()) ? traits_type::eof() : sputc(char_type(c));
336 if (! pptr())
return -1;
348 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
355 :
public std::istream
359 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
362 exceptions(std::ios_base::badbit);
367 exceptions(std::ios_base::badbit);
376 :
public std::ostream
380 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
383 exceptions(std::ios_base::badbit);
388 exceptions(std::ios_base::badbit);
399template <
typename FStream_Type >
403 :
_fs(filename, mode)
420 exceptions(std::ios_base::badbit);
431 return _fs.is_open();
436 if (rdbuf())
delete rdbuf();
451 explicit ofstream(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
456 exceptions(std::ios_base::badbit);
460 std::ostream::flush();
469 return _fs.is_open();
472 std::ostream::flush();
479 if (rdbuf())
delete rdbuf();
Exception class thrown by failed zlib operations.
static std::string error_to_message(z_stream *zstrm_p, int ret)
Exception(z_stream *zstrm_p, int ret)
z_stream_wrapper(bool _is_input, int _level, int _window_bits)
std::streampos compressed_tellg()
Return the position within the compressed file (wrapped filestream)
ifstream(const std::string filename, std::ios_base::openmode mode=std::ios_base::in, size_t buff_size=default_buff_size)
istream(std::streambuf *sbuf_p)
istream(std::istream &is, std::size_t _buff_size=default_buff_size, bool _auto_detect=true, int _window_bits=0)
istreambuf(std::streambuf *_sbuf_p, std::size_t _buff_size=default_buff_size, bool _auto_detect=true, int _window_bits=0)
std::unique_ptr< detail::z_stream_wrapper > zstrm_p
std::unique_ptr< char[]> out_buff
istreambuf(const istreambuf &)=delete
std::unique_ptr< char[]> in_buff
pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override
std::streambuf::int_type underflow() override
istreambuf & operator=(const istreambuf &)=delete
std::streampos compressed_tellp()
ofstream(const std::string filename, std::ios_base::openmode mode=std::ios_base::out, int level=Z_DEFAULT_COMPRESSION, size_t buff_size=default_buff_size)
ostream(std::ostream &os, std::size_t _buff_size=default_buff_size, int _level=Z_DEFAULT_COMPRESSION, int _window_bits=0)
ostream(std::streambuf *sbuf_p)
ostreambuf & operator=(const ostreambuf &)=delete
int deflate_loop(int flush)
std::unique_ptr< char[]> in_buff
std::unique_ptr< char[]> out_buff
ostreambuf(const ostreambuf &)=delete
ostreambuf(std::streambuf *_sbuf_p, std::size_t _buff_size=default_buff_size, int _level=Z_DEFAULT_COMPRESSION, int _window_bits=0)
std::streambuf::int_type overflow(std::streambuf::int_type c=traits_type::eof()) override
std::unique_ptr< detail::z_stream_wrapper > zstrm_p
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
static const std::size_t default_buff_size
strict_fstream_holder()=default
strict_fstream_holder(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)