19#if defined(__GNUC__) && !defined(__clang__)
20#if (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__>0)
21#define CAN_MOVE_IOSTREAM
24#define CAN_MOVE_IOSTREAM
34 :
public std::ios_base::failure
39 std::string msg =
"zlib: ";
43 msg +=
"Z_STREAM_ERROR: ";
46 msg +=
"Z_DATA_ERROR: ";
49 msg +=
"Z_MEM_ERROR: ";
52 msg +=
"Z_VERSION_ERROR: ";
55 msg +=
"Z_BUF_ERROR: ";
58 std::ostringstream oss;
60 msg +=
"[" + oss.str() +
"]: ";
68 std::to_string(uintptr_t(zstrm_p->next_in)) +
70 std::to_string(uintptr_t(zstrm_p->avail_in)) +
72 std::to_string(uintptr_t(zstrm_p->next_out)) +
74 std::to_string(uintptr_t(zstrm_p->avail_out)) +
95 this->zalloc =
nullptr;
96 this->zfree =
nullptr;
97 this->opaque =
nullptr;
102 this->next_in =
nullptr;
103 ret = inflateInit2(
this, _window_bits ? _window_bits : 15+32);
107 ret = deflateInit2(
this, _level, Z_DEFLATED, _window_bits ? _window_bits : 15+16, 8, Z_DEFAULT_STRATEGY);
109 if (ret != Z_OK)
throw Exception(
this, ret);
129 :
public std::streambuf
133 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
157 pos_type
seekoff(off_type off, std::ios_base::seekdir dir,
158 std::ios_base::openmode which)
override
160 if (off != 0 || dir != std::ios_base::cur) {
161 return std::streambuf::seekoff(off, dir, which);
168 return static_cast<long int>(
zstrm_p->total_out -
static_cast<uLong
>(in_avail()));
173 if (this->gptr() == this->egptr())
176 char * out_buff_free_start =
out_buff.get();
180 if (++tries > 1000) {
181 throw std::ios_base::failure(
"Failed to fill buffer after 1000 tries");
197 unsigned char b0 = *
reinterpret_cast< unsigned char *
>(
in_buff_start);
198 unsigned char b1 = *
reinterpret_cast< unsigned char *
>(
in_buff_start + 1);
203 && ((b0 == 0x1F && b1 == 0x8B)
204 || (b0 == 0x78 && (b1 == 0x01
223 zstrm_p->next_out =
reinterpret_cast< decltype(
zstrm_p-
>next_out) >(out_buff_free_start);
225 int ret = inflate(
zstrm_p.get(), Z_NO_FLUSH);
227 if (ret != Z_OK && ret != Z_STREAM_END)
throw Exception(
zstrm_p.get(), ret);
231 out_buff_free_start =
reinterpret_cast< decltype(out_buff_free_start)
>(
zstrm_p->next_out);
234 if (ret == Z_STREAM_END) {
239 }
while (out_buff_free_start ==
out_buff.get());
245 return this->gptr() == this->egptr()
247 : traits_type::to_int_type(*this->gptr());
255 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
265 :
public std::streambuf
269 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
273 zstrm_p(new detail::z_stream_wrapper(false, _level, _window_bits)),
291 int ret = deflate(
zstrm_p.get(), flush);
292 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
302 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
324 std::streambuf::int_type
overflow(std::streambuf::int_type c = traits_type::eof())
override
326 zstrm_p->next_in =
reinterpret_cast< decltype(
zstrm_p-
>next_in) >(pbase());
327 zstrm_p->avail_in = uint32_t(pptr() - pbase());
333 setp(
nullptr,
nullptr);
334 return traits_type::eof();
338 return traits_type::eq_int_type(c, traits_type::eof()) ? traits_type::eof() : sputc(char_type(c));
344 if (! pptr())
return -1;
356 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
363 :
public std::istream
367 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
370 exceptions(std::ios_base::badbit);
375 exceptions(std::ios_base::badbit);
384 :
public std::ostream
388 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
391 exceptions(std::ios_base::badbit);
396 exceptions(std::ios_base::badbit);
407template <
typename FStream_Type >
411 :
_fs(filename, mode)
428 exceptions(std::ios_base::badbit);
434 #ifdef CAN_MOVE_IOSTREAM
435 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::in) {
437 std::istream::operator=(std::istream(
new istreambuf(
_fs.rdbuf())));
441 return _fs.is_open();
446 if (rdbuf())
delete rdbuf();
461 explicit ofstream(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
466 exceptions(std::ios_base::badbit);
470 std::ostream::flush();
473 #ifdef CAN_MOVE_IOSTREAM
474 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
int level = Z_DEFAULT_COMPRESSION) {
476 _fs.
open(filename, mode | std::ios_base::binary);
481 return _fs.is_open();
484 std::ostream::flush();
491 if (rdbuf())
delete rdbuf();
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
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)
void open(const std::string filename, std::ios_base::openmode mode=std::ios_base::in)
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()
void open(const std::string filename, std::ios_base::openmode mode=std::ios_base::out, int level=Z_DEFAULT_COMPRESSION)
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
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)