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 || \
73 defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
74 defined(__NetBSD__) || defined(__DragonFly__)
76 if (strerror_r(errno, &buff[0], buff.size()) != 0)
78 buff =
"Unknown error";
82 auto p = strerror_r(errno, &buff[0], buff.size());
83 std::string tmp(
p, std::strlen(
p));
86 buff.resize(buff.find(
'\0'));
92 :
public std::exception
96 const char *
what() const noexcept {
return _msg.c_str(); }
108 static const int n_modes = 6;
109 static const std::ios_base::openmode mode_val_v[n_modes] =
115 std::ios_base::trunc,
116 std::ios_base::binary
119 static const char * mode_name_v[n_modes] =
129 for (
int i = 0; i < n_modes; ++i)
131 if (mode & mode_val_v[i])
133 res += (! res.empty()?
"|" :
"");
134 res += mode_name_v[i];
137 if (res.empty()) { res =
"none"; }
141 std::ios_base::openmode mode)
145 throw Exception(std::string(
"strict_fstream: open('") + filename +
146 "'): mode error: trunc and not out");
148 else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
150 throw Exception(std::string(
"strict_fstream: open('") + filename +
151 "'): mode error: app and not out");
153 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
155 throw Exception(std::string(
"strict_fstream: open('") + filename +
156 "'): mode error: trunc and app");
159 static void check_open(std::ios * s_p,
const std::string& filename,
160 std::ios_base::openmode mode)
164 throw Exception(std::string(
"strict_fstream: open('")
169 static void check_peek(std::istream * is_p,
const std::string& filename,
170 std::ios_base::openmode mode)
172 bool peek_failed =
true;
176 peek_failed = is_p->fail();
178 catch (std::ios_base::failure&) {}
181 throw Exception(std::string(
"strict_fstream: open('")
192 :
public std::ifstream
197 std::ios_base::openmode mode = std::ios_base::in)
199 open(filename, mode);
201 void open(
const std::string& filename,
202 std::ios_base::openmode mode = std::ios_base::in)
204 mode |= std::ios_base::in;
205 exceptions(std::ios_base::badbit);
207 std::ifstream::open(filename, mode);
214 :
public std::ofstream
221 open(filename, mode);
223 void open(
const std::string& filename,
227 exceptions(std::ios_base::badbit);
229 std::ofstream::open(filename, mode);
235 :
public std::fstream
240 std::ios_base::openmode mode = std::ios_base::in)
242 open(filename, mode);
244 void open(
const std::string& filename,
245 std::ios_base::openmode mode = std::ios_base::in)
248 exceptions(std::ios_base::badbit);
250 std::fstream::open(filename, mode);
267 :
public std::exception
276 _msg +=
"Z_STREAM_ERROR: ";
279 _msg +=
"Z_DATA_ERROR: ";
282 _msg +=
"Z_MEM_ERROR: ";
284 case Z_VERSION_ERROR:
285 _msg +=
"Z_VERSION_ERROR: ";
288 _msg +=
"Z_BUF_ERROR: ";
291 std::ostringstream oss;
293 _msg +=
"[" + oss.str() +
"]: ";
296 _msg += zstrm_p->msg;
299 const char *
what() const noexcept {
return _msg.c_str(); }
314 : is_input(_is_input)
316 this->zalloc = Z_NULL;
317 this->zfree = Z_NULL;
318 this->opaque = Z_NULL;
323 this->next_in = Z_NULL;
324 ret = inflateInit2(
this, 15 + 32);
328 ret = deflateInit2(
this, _level, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
354 :
public std::streambuf
358 std::size_t _buff_size = default_buff_size,
bool _auto_detect =
true)
361 buff_size(_buff_size),
362 auto_detect(_auto_detect),
363 auto_detect_run(false),
367 in_buff =
new char[buff_size];
368 in_buff_start = in_buff;
369 in_buff_end = in_buff;
370 out_buff =
new char[buff_size];
371 setg(out_buff, out_buff, out_buff);
391 if (this->gptr() == this->egptr())
394 char *out_buff_free_start = out_buff;
398 if (in_buff_start == in_buff_end)
401 in_buff_start = in_buff;
402 std::streamsize sz = sbuf_p->sgetn(in_buff, buff_size);
403 in_buff_end = in_buff + sz;
404 if (in_buff_end == in_buff_start)
410 if (auto_detect && !auto_detect_run)
412 auto_detect_run =
true;
413 unsigned char b0 = *
reinterpret_cast<unsigned char *
>(in_buff_start);
414 unsigned char b1 = *
reinterpret_cast<unsigned char *
>(in_buff_start + 1);
418 is_text = !(in_buff_start + 2 <= in_buff_end && ((b0 == 0x1F &&
420 || (b0 == 0x78 && (b1 == 0x01
421 || b1 == 0x9C || b1 == 0xDA))));
426 assert(in_buff_start == in_buff);
427 std::swap(in_buff, out_buff);
428 out_buff_free_start = in_buff_end;
429 in_buff_start = in_buff;
430 in_buff_end = in_buff;
439 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(in_buff_start);
440 zstrm_p->avail_in = in_buff_end - in_buff_start;
441 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>
442 (out_buff_free_start);
443 zstrm_p->avail_out = (out_buff + buff_size) - out_buff_free_start;
444 int ret = inflate(zstrm_p, Z_NO_FLUSH);
446 if (ret != Z_OK && ret != Z_STREAM_END)
451 in_buff_start =
reinterpret_cast<decltype(in_buff_start)
>(zstrm_p->next_in);
452 in_buff_end = in_buff_start + zstrm_p->avail_in;
453 out_buff_free_start =
reinterpret_cast<decltype(out_buff_free_start)
>
455 assert(out_buff_free_start + zstrm_p->avail_out == out_buff + buff_size);
457 if (ret == Z_STREAM_END)
464 while (out_buff_free_start == out_buff);
468 this->setg(out_buff, out_buff, out_buff_free_start);
470 return this->gptr() == this->egptr()
472 : traits_type::to_int_type(*this->gptr());
476 std::streambuf *sbuf_p;
482 std::size_t buff_size;
484 bool auto_detect_run;
487 static const std::size_t default_buff_size = (std::size_t)1 << 20;
491 :
public std::streambuf
495 std::size_t _buff_size = default_buff_size,
int _level = Z_DEFAULT_COMPRESSION)
497 zstrm_p(new detail::z_stream_wrapper(false, _level)),
498 buff_size(_buff_size)
501 in_buff =
new char[buff_size];
502 out_buff =
new char[buff_size];
503 setp(in_buff, in_buff + buff_size);
515 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>(out_buff);
516 zstrm_p->avail_out = buff_size;
517 int ret = deflate(zstrm_p, flush);
518 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR)
522 std::streamsize sz = sbuf_p->sputn(out_buff,
523 reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff);
524 if (sz !=
reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff)
529 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
552 virtual std::streambuf::int_type
overflow(std::streambuf::int_type c =
555 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(pbase());
556 zstrm_p->avail_in = pptr() - pbase();
557 while (zstrm_p->avail_in > 0)
562 setp(
nullptr,
nullptr);
563 return traits_type::eof();
566 setp(in_buff, in_buff + buff_size);
567 return traits_type::eq_int_type(c,
581 zstrm_p->next_in =
nullptr;
582 zstrm_p->avail_in = 0;
587 deflateReset(zstrm_p);
592 std::streambuf *sbuf_p;
596 std::size_t buff_size;
598 static const std::size_t default_buff_size = (std::size_t)1 << 20;
602 :
public std::istream
608 exceptions(std::ios_base::badbit);
613 exceptions(std::ios_base::badbit);
622 :
public std::ostream
628 exceptions(std::ios_base::badbit);
633 exceptions(std::ios_base::badbit);
645 template <
typename FStream_Type>
649 std::ios_base::openmode mode = std::ios_base::in)
650 :
_fs(filename, mode)
665 std::ios_base::openmode mode = std::ios_base::in)
669 exceptions(std::ios_base::badbit);
688 mode | std::ios_base::binary),
691 exceptions(std::ios_base::badbit);
717 bool compression =
false)
719 std::ios_base::binary),
720 std::ostream(nullptr)
733 exceptions(std::ios_base::badbit);
737 char const *open_mode_chars)
739 std::ios_base::binary),
740 std::ostream(nullptr)
747 if (std::string(open_mode_chars).find(
'z') != std::string::npos)
757 setstate(
_fs.rdstate());
758 exceptions(std::ios_base::badbit);
777 std::istream(nullptr)
785 setstate(
_fs.rdstate());
786 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)
double p(const Vector &x, double t)
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)