20#if defined(__GNUC__) && !defined(__clang__)
21#if (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__>0)
22#define CAN_MOVE_IOSTREAM
25#define CAN_MOVE_IOSTREAM
35 :
public std::ios_base::failure
40 std::string msg =
"zlib: ";
44 msg +=
"Z_STREAM_ERROR: ";
47 msg +=
"Z_DATA_ERROR: ";
50 msg +=
"Z_MEM_ERROR: ";
53 msg +=
"Z_VERSION_ERROR: ";
56 msg +=
"Z_BUF_ERROR: ";
59 std::ostringstream oss;
61 msg +=
"[" + oss.str() +
"]: ";
69 std::to_string(uintptr_t(zstrm_p->next_in)) +
71 std::to_string(uintptr_t(zstrm_p->avail_in)) +
73 std::to_string(uintptr_t(zstrm_p->next_out)) +
75 std::to_string(uintptr_t(zstrm_p->avail_out)) +
96 this->zalloc =
nullptr;
97 this->zfree =
nullptr;
98 this->opaque =
nullptr;
103 this->next_in =
nullptr;
104 ret = inflateInit2(
this, _window_bits ? _window_bits : 15+32);
108 ret = deflateInit2(
this, _level, Z_DEFLATED, _window_bits ? _window_bits : 15+16, 8, Z_DEFAULT_STRATEGY);
110 if (ret != Z_OK)
throw Exception(
this, ret);
130 :
public std::streambuf
134 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
158 pos_type
seekoff(off_type off, std::ios_base::seekdir dir,
159 std::ios_base::openmode which)
override
161 if (off != 0 || dir != std::ios_base::cur) {
162 return std::streambuf::seekoff(off, dir, which);
169 return static_cast<long int>(
zstrm_p->total_out -
static_cast<uLong
>(in_avail()));
158 pos_type
seekoff(off_type off, std::ios_base::seekdir dir, {
…}
174 if (this->gptr() == this->egptr())
177 char * out_buff_free_start =
out_buff.get();
181 if (++tries > 1000) {
182 throw std::ios_base::failure(
"Failed to fill buffer after 1000 tries");
198 unsigned char b0 = *
reinterpret_cast< unsigned char *
>(
in_buff_start);
199 unsigned char b1 = *
reinterpret_cast< unsigned char *
>(
in_buff_start + 1);
204 && ((b0 == 0x1F && b1 == 0x8B)
205 || (b0 == 0x78 && (b1 == 0x01
224 zstrm_p->next_out =
reinterpret_cast< decltype(
zstrm_p-
>next_out) >(out_buff_free_start);
226 int ret = inflate(
zstrm_p.get(), Z_NO_FLUSH);
228 if (ret != Z_OK && ret != Z_STREAM_END)
throw Exception(
zstrm_p.get(), ret);
232 out_buff_free_start =
reinterpret_cast< decltype(out_buff_free_start)
>(
zstrm_p->next_out);
235 if (ret == Z_STREAM_END) {
240 }
while (out_buff_free_start ==
out_buff.get());
246 return this->gptr() == this->egptr()
248 : traits_type::to_int_type(*this->gptr());
256 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
266 :
public std::streambuf
270 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
274 zstrm_p(new detail::z_stream_wrapper(false, _level, _window_bits)),
292 int ret = deflate(
zstrm_p.get(), flush);
293 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) {
303 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
325 std::streambuf::int_type
overflow(std::streambuf::int_type c = traits_type::eof())
override
327 zstrm_p->next_in =
reinterpret_cast< decltype(
zstrm_p-
>next_in) >(pbase());
328 zstrm_p->avail_in = uint32_t(pptr() - pbase());
334 setp(
nullptr,
nullptr);
335 return traits_type::eof();
339 return traits_type::eq_int_type(c, traits_type::eof()) ? traits_type::eof() : sputc(char_type(c));
325 std::streambuf::int_type
overflow(std::streambuf::int_type c = traits_type::eof())
override {
…}
345 if (! pptr())
return -1;
357 std::unique_ptr<detail::z_stream_wrapper>
zstrm_p;
364 :
public std::istream
368 std::size_t _buff_size =
default_buff_size,
bool _auto_detect =
true,
int _window_bits = 0)
371 exceptions(std::ios_base::badbit);
376 exceptions(std::ios_base::badbit);
385 :
public std::ostream
389 std::size_t _buff_size =
default_buff_size,
int _level = Z_DEFAULT_COMPRESSION,
int _window_bits = 0)
392 exceptions(std::ios_base::badbit);
397 exceptions(std::ios_base::badbit);
408template <
typename FStream_Type >
412 :
_fs(filename, mode)
429 exceptions(std::ios_base::badbit);
435 #ifdef CAN_MOVE_IOSTREAM
436 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::in) {
438 std::istream::operator=(std::istream(
new istreambuf(
_fs.rdbuf())));
436 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::in) {
…}
442 return _fs.is_open();
447 if (rdbuf())
delete rdbuf();
462 explicit ofstream(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
467 exceptions(std::ios_base::badbit);
462 explicit ofstream(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out, {
…}
471 std::ostream::flush();
474 #ifdef CAN_MOVE_IOSTREAM
475 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
int level = Z_DEFAULT_COMPRESSION) {
477 _fs.
open(filename, mode | std::ios_base::binary);
475 void open(
const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
int level = Z_DEFAULT_COMPRESSION) {
…}
482 return _fs.is_open();
485 std::ostream::flush();
492 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)