59 namespace strict_fstream
64 static std::string strerror()
66 std::string buff(80,
'\0');
68 if (strerror_s(&buff[0], buff.size(), errno) != 0)
70 buff =
"Unknown error";
72 #elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE || defined(__APPLE__)
74 if (strerror_r(errno, &buff[0], buff.size()) != 0)
76 buff =
"Unknown error";
80 auto p = strerror_r(errno, &buff[0], buff.size());
81 std::string tmp(p, std::strlen(p));
84 buff.resize(buff.find(
'\0'));
90 :
public std::exception
94 const char *
what() const noexcept {
return _msg.c_str(); }
106 static const int n_modes = 6;
107 static const std::ios_base::openmode mode_val_v[n_modes] =
113 std::ios_base::trunc,
114 std::ios_base::binary
117 static const char * mode_name_v[n_modes] =
127 for (
int i = 0; i < n_modes; ++i)
129 if (mode & mode_val_v[i])
131 res += (! res.empty()?
"|" :
"");
132 res += mode_name_v[i];
135 if (res.empty()) { res =
"none"; }
139 std::ios_base::openmode mode)
143 throw Exception(std::string(
"strict_fstream: open('") + filename +
144 "'): mode error: trunc and not out");
146 else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
148 throw Exception(std::string(
"strict_fstream: open('") + filename +
149 "'): mode error: app and not out");
151 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
153 throw Exception(std::string(
"strict_fstream: open('") + filename +
154 "'): mode error: trunc and app");
157 static void check_open(std::ios * s_p,
const std::string& filename,
158 std::ios_base::openmode mode)
162 throw Exception(std::string(
"strict_fstream: open('")
167 static void check_peek(std::istream * is_p,
const std::string& filename,
168 std::ios_base::openmode mode)
170 bool peek_failed =
true;
174 peek_failed = is_p->fail();
176 catch (std::ios_base::failure &e) {}
179 throw Exception(std::string(
"strict_fstream: open('")
190 :
public std::ifstream
195 std::ios_base::openmode mode = std::ios_base::in)
197 open(filename, mode);
199 void open(
const std::string& filename,
200 std::ios_base::openmode mode = std::ios_base::in)
202 mode |= std::ios_base::in;
203 exceptions(std::ios_base::badbit);
205 std::ifstream::open(filename, mode);
212 :
public std::ofstream
219 open(filename, mode);
221 void open(
const std::string& filename,
225 exceptions(std::ios_base::badbit);
227 std::ofstream::open(filename, mode);
233 :
public std::fstream
238 std::ios_base::openmode mode = std::ios_base::in)
240 open(filename, mode);
242 void open(
const std::string& filename,
243 std::ios_base::openmode mode = std::ios_base::in)
246 exceptions(std::ios_base::badbit);
248 std::fstream::open(filename, mode);
265 :
public std::exception
274 _msg +=
"Z_STREAM_ERROR: ";
277 _msg +=
"Z_DATA_ERROR: ";
280 _msg +=
"Z_MEM_ERROR: ";
282 case Z_VERSION_ERROR:
283 _msg +=
"Z_VERSION_ERROR: ";
286 _msg +=
"Z_BUF_ERROR: ";
289 std::ostringstream oss;
291 _msg +=
"[" + oss.str() +
"]: ";
294 _msg += zstrm_p->msg;
297 const char *
what() const noexcept {
return _msg.c_str(); }
312 : is_input(_is_input)
314 this->zalloc = Z_NULL;
315 this->zfree = Z_NULL;
316 this->opaque = Z_NULL;
321 this->next_in = Z_NULL;
322 ret = inflateInit2(
this, 15 + 32);
326 ret = deflateInit2(
this, _level, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
352 :
public std::streambuf
356 std::size_t _buff_size = default_buff_size,
bool _auto_detect =
true)
359 buff_size(_buff_size),
360 auto_detect(_auto_detect),
361 auto_detect_run(false),
365 in_buff =
new char[buff_size];
366 in_buff_start = in_buff;
367 in_buff_end = in_buff;
368 out_buff =
new char[buff_size];
369 setg(out_buff, out_buff, out_buff);
389 if (this->gptr() == this->egptr())
392 char *out_buff_free_start = out_buff;
396 if (in_buff_start == in_buff_end)
399 in_buff_start = in_buff;
400 std::streamsize sz = sbuf_p->sgetn(in_buff, buff_size);
401 in_buff_end = in_buff + sz;
402 if (in_buff_end == in_buff_start)
408 if (auto_detect && !auto_detect_run)
410 auto_detect_run =
true;
411 unsigned char b0 = *
reinterpret_cast<unsigned char *
>(in_buff_start);
412 unsigned char b1 = *
reinterpret_cast<unsigned char *
>(in_buff_start + 1);
416 is_text = !(in_buff_start + 2 <= in_buff_end && ((b0 == 0x1F &&
418 || (b0 == 0x78 && (b1 == 0x01
419 || b1 == 0x9C || b1 == 0xDA))));
424 assert(in_buff_start == in_buff);
425 std::swap(in_buff, out_buff);
426 out_buff_free_start = in_buff_end;
427 in_buff_start = in_buff;
428 in_buff_end = in_buff;
437 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(in_buff_start);
438 zstrm_p->avail_in = in_buff_end - in_buff_start;
439 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>
440 (out_buff_free_start);
441 zstrm_p->avail_out = (out_buff + buff_size) - out_buff_free_start;
442 int ret = inflate(zstrm_p, Z_NO_FLUSH);
444 if (ret != Z_OK && ret != Z_STREAM_END)
449 in_buff_start =
reinterpret_cast<decltype(in_buff_start)
>(zstrm_p->next_in);
450 in_buff_end = in_buff_start + zstrm_p->avail_in;
451 out_buff_free_start =
reinterpret_cast<decltype(out_buff_free_start)
>
453 assert(out_buff_free_start + zstrm_p->avail_out == out_buff + buff_size);
455 if (ret == Z_STREAM_END)
462 while (out_buff_free_start == out_buff);
466 this->setg(out_buff, out_buff, out_buff_free_start);
468 return this->gptr() == this->egptr()
470 : traits_type::to_int_type(*this->gptr());
474 std::streambuf *sbuf_p;
480 std::size_t buff_size;
482 bool auto_detect_run;
485 static const std::size_t default_buff_size = (std::size_t)1 << 20;
489 :
public std::streambuf
493 std::size_t _buff_size = default_buff_size,
int _level = Z_DEFAULT_COMPRESSION)
495 zstrm_p(new detail::z_stream_wrapper(false, _level)),
496 buff_size(_buff_size)
499 in_buff =
new char[buff_size];
500 out_buff =
new char[buff_size];
501 setp(in_buff, in_buff + buff_size);
513 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>(out_buff);
514 zstrm_p->avail_out = buff_size;
515 int ret = deflate(zstrm_p, flush);
516 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR)
520 std::streamsize sz = sbuf_p->sputn(out_buff,
521 reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff);
522 if (sz !=
reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff)
527 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
550 virtual std::streambuf::int_type
overflow(std::streambuf::int_type c =
553 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(pbase());
554 zstrm_p->avail_in = pptr() - pbase();
555 while (zstrm_p->avail_in > 0)
560 setp(
nullptr,
nullptr);
561 return traits_type::eof();
564 setp(in_buff, in_buff + buff_size);
565 return traits_type::eq_int_type(c,
579 zstrm_p->next_in =
nullptr;
580 zstrm_p->avail_in = 0;
585 deflateReset(zstrm_p);
590 std::streambuf *sbuf_p;
594 std::size_t buff_size;
596 static const std::size_t default_buff_size = (std::size_t)1 << 20;
600 :
public std::istream
606 exceptions(std::ios_base::badbit);
611 exceptions(std::ios_base::badbit);
620 :
public std::ostream
626 exceptions(std::ios_base::badbit);
631 exceptions(std::ios_base::badbit);
643 template <
typename FStream_Type>
647 std::ios_base::openmode mode = std::ios_base::in)
648 :
_fs(filename, mode)
663 std::ios_base::openmode mode = std::ios_base::in)
667 exceptions(std::ios_base::badbit);
686 mode | std::ios_base::binary),
689 exceptions(std::ios_base::badbit);
715 bool compression =
false)
717 std::ios_base::binary),
718 std::ostream(nullptr)
731 exceptions(std::ios_base::badbit);
735 char const *open_mode_chars)
737 std::ios_base::binary),
738 std::ostream(nullptr)
745 if (std::string(open_mode_chars).find(
'z') != std::string::npos)
755 exceptions(std::ios_base::badbit);
774 std::istream(nullptr)
782 exceptions(std::ios_base::badbit);
static void check_peek(std::istream *is_p, const std::string &filename, std::ios_base::openmode mode)
const char * what() const noexcept
ofstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
const char * what() const noexcept
virtual std::streambuf::int_type underflow()
ifgzstream(const std::string &filename)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
istream(std::istream &is)
ostreambuf(std::streambuf *_sbuf_p, std::size_t _buff_size=default_buff_size, int _level=Z_DEFAULT_COMPRESSION)
Exception class thrown by failed zlib operations.
fstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
ifstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
Exception(z_stream *zstrm_p, int ret)
named_ifgzstream(const std::string &mesh_name)
ostreambuf & operator=(const ostreambuf &)=delete
ostream(std::streambuf *sbuf_p)
Exception(const std::string &msg)
int deflate_loop(int flush)
z_stream_wrapper(bool _is_input=true, int _level=Z_DEFAULT_COMPRESSION)
static void check_mode(const std::string &filename, std::ios_base::openmode mode)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
ostream(std::ostream &os)
istreambuf & operator=(const istreambuf &)=delete
ifstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
virtual std::streambuf::int_type overflow(std::streambuf::int_type c=traits_type::eof())
ofgzstream(const std::string &filename, bool compression=false)
ofstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
static std::string mode_to_string(std::ios_base::openmode mode)
Exception(const std::string msg)
istream(std::streambuf *sbuf_p)
strict_fstream_holder(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
istreambuf(std::streambuf *_sbuf_p, std::size_t _buff_size=default_buff_size, bool _auto_detect=true)
const std::string filename
Exception class thrown by failed operations.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
ofgzstream(const std::string &filename, char const *open_mode_chars)
static void check_open(std::ios *s_p, const std::string &filename, std::ios_base::openmode mode)