38 #include "../config/config.hpp" 67 if (r) { snprintf(buff, buff_size,
"unknown error: %d", r); }
78 static std::string strerror()
80 std::string buff(80,
'\0');
82 if (strerror_s(&buff[0], buff.size(), errno) != 0)
84 buff =
"Unknown error";
89 std::string tmp(
p, std::strlen(
p));
92 buff.resize(buff.find(
'\0'));
98 :
public std::exception
102 const char *
what() const noexcept {
return msg.c_str(); }
114 static const int n_modes = 6;
115 static const std::ios_base::openmode mode_val_v[n_modes] =
121 std::ios_base::trunc,
122 std::ios_base::binary
125 static const char * mode_name_v[n_modes] =
135 for (
int i = 0; i < n_modes; ++i)
137 if (mode & mode_val_v[i])
139 res += (! res.empty()?
"|" :
"");
140 res += mode_name_v[i];
143 if (res.empty()) { res =
"none"; }
147 std::ios_base::openmode mode)
151 throw Exception(std::string(
"strict_fstream: open('") + filename +
152 "'): mode error: trunc and not out");
156 throw Exception(std::string(
"strict_fstream: open('") + filename +
157 "'): mode error: app and not out");
159 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
161 throw Exception(std::string(
"strict_fstream: open('") + filename +
162 "'): mode error: trunc and app");
165 static void check_open(std::ios * s_p,
const std::string& filename,
166 std::ios_base::openmode mode)
170 throw Exception(std::string(
"strict_fstream: open('")
175 static void check_peek(std::istream * is_p,
const std::string& filename,
176 std::ios_base::openmode mode)
178 bool peek_failed =
true;
182 peek_failed = is_p->fail();
184 catch (std::ios_base::failure&) {}
187 throw Exception(std::string(
"strict_fstream: open('")
198 :
public std::ifstream
203 std::ios_base::openmode mode = std::ios_base::in)
205 open(filename, mode);
207 void open(
const std::string& filename,
208 std::ios_base::openmode mode = std::ios_base::in)
210 mode |= std::ios_base::in;
211 exceptions(std::ios_base::badbit);
213 std::ifstream::open(filename, mode);
220 :
public std::ofstream
227 open(filename, mode);
229 void open(
const std::string& filename,
233 exceptions(std::ios_base::badbit);
235 std::ofstream::open(filename, mode);
241 :
public std::fstream
246 std::ios_base::openmode mode = std::ios_base::in)
248 open(filename, mode);
250 void open(
const std::string& filename,
251 std::ios_base::openmode mode = std::ios_base::in)
254 exceptions(std::ios_base::badbit);
256 std::fstream::open(filename, mode);
273 :
public std::exception
282 msg +=
"Z_STREAM_ERROR: ";
285 msg +=
"Z_DATA_ERROR: ";
288 msg +=
"Z_MEM_ERROR: ";
290 case Z_VERSION_ERROR:
291 msg +=
"Z_VERSION_ERROR: ";
294 msg +=
"Z_BUF_ERROR: ";
297 std::ostringstream oss;
299 msg +=
"[" + oss.str() +
"]: ";
305 const char *
what() const noexcept {
return msg.c_str(); }
320 : is_input(is_input_)
322 this->zalloc = Z_NULL;
323 this->zfree = Z_NULL;
324 this->opaque = Z_NULL;
329 this->next_in = Z_NULL;
330 ret = inflateInit2(
this, 15 + 32);
334 ret = deflateInit2(
this, level_, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
360 :
public std::streambuf
364 std::size_t buff_size_ = default_buff_size,
bool auto_detect_ =
true)
367 buff_size(buff_size_),
368 auto_detect(auto_detect_),
369 auto_detect_run(false),
373 in_buff =
new char[buff_size];
374 in_buff_start = in_buff;
375 in_buff_end = in_buff;
376 out_buff =
new char[buff_size];
377 setg(out_buff, out_buff, out_buff);
397 if (this->gptr() == this->egptr())
400 char *out_buff_free_start = out_buff;
404 if (in_buff_start == in_buff_end)
407 in_buff_start = in_buff;
408 std::streamsize sz = sbuf_p->sgetn(in_buff, buff_size);
409 in_buff_end = in_buff + sz;
410 if (in_buff_end == in_buff_start)
416 if (auto_detect && !auto_detect_run)
418 auto_detect_run =
true;
419 unsigned char b0 = *
reinterpret_cast<unsigned char *
>(in_buff_start);
420 unsigned char b1 = *
reinterpret_cast<unsigned char *
>(in_buff_start + 1);
424 is_text = !(in_buff_start + 2 <= in_buff_end && ((b0 == 0x1F &&
426 || (b0 == 0x78 && (b1 == 0x01
427 || b1 == 0x9C || b1 == 0xDA))));
432 assert(in_buff_start == in_buff);
433 std::swap(in_buff, out_buff);
434 out_buff_free_start = in_buff_end;
435 in_buff_start = in_buff;
436 in_buff_end = in_buff;
445 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(in_buff_start);
446 zstrm_p->avail_in = in_buff_end - in_buff_start;
447 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>
448 (out_buff_free_start);
449 zstrm_p->avail_out = (out_buff + buff_size) - out_buff_free_start;
450 int ret = inflate(zstrm_p, Z_NO_FLUSH);
452 if (ret != Z_OK && ret != Z_STREAM_END)
457 in_buff_start =
reinterpret_cast<decltype(in_buff_start)
>(zstrm_p->next_in);
458 in_buff_end = in_buff_start + zstrm_p->avail_in;
459 out_buff_free_start =
reinterpret_cast<decltype(out_buff_free_start)
> 461 assert(out_buff_free_start + zstrm_p->avail_out == out_buff + buff_size);
463 if (ret == Z_STREAM_END)
470 while (out_buff_free_start == out_buff);
474 this->setg(out_buff, out_buff, out_buff_free_start);
476 return this->gptr() == this->egptr()
478 : traits_type::to_int_type(*this->gptr());
482 std::streambuf *sbuf_p;
488 std::size_t buff_size;
490 bool auto_detect_run;
493 static const std::size_t default_buff_size = (std::size_t)1 << 20;
497 :
public std::streambuf
501 std::size_t buff_size_ = default_buff_size,
int level_ = Z_DEFAULT_COMPRESSION)
503 zstrm_p(new detail::z_stream_wrapper(false, level_)),
504 buff_size(buff_size_)
507 in_buff =
new char[buff_size];
508 out_buff =
new char[buff_size];
509 setp(in_buff, in_buff + buff_size);
521 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>(out_buff);
522 zstrm_p->avail_out = buff_size;
523 int ret = deflate(zstrm_p, flush);
524 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR)
528 std::streamsize sz = sbuf_p->sputn(out_buff,
529 reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff);
530 if (sz !=
reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff)
535 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
558 virtual std::streambuf::int_type
overflow(std::streambuf::int_type c =
561 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(pbase());
562 zstrm_p->avail_in = pptr() - pbase();
563 while (zstrm_p->avail_in > 0)
568 setp(
nullptr,
nullptr);
569 return traits_type::eof();
572 setp(in_buff, in_buff + buff_size);
573 return traits_type::eq_int_type(c,
587 zstrm_p->next_in =
nullptr;
588 zstrm_p->avail_in = 0;
593 deflateReset(zstrm_p);
598 std::streambuf *sbuf_p;
602 std::size_t buff_size;
604 static const std::size_t default_buff_size = (std::size_t)1 << 20;
608 :
public std::istream
614 exceptions(std::ios_base::badbit);
619 exceptions(std::ios_base::badbit);
628 :
public std::ostream
634 exceptions(std::ios_base::badbit);
639 exceptions(std::ios_base::badbit);
651 template <
typename FStream_Type>
655 std::ios_base::openmode mode = std::ios_base::in)
656 :
fs_(filename, mode)
671 std::ios_base::openmode mode = std::ios_base::in)
675 exceptions(std::ios_base::badbit);
694 mode |
std::ios_base::binary),
697 exceptions(std::ios_base::badbit);
723 bool compression =
false)
725 std::ios_base::binary),
726 std::ostream(nullptr)
739 exceptions(std::ios_base::badbit);
743 char const *open_mode_chars)
745 std::ios_base::binary),
746 std::ostream(nullptr)
753 if (std::string(open_mode_chars).find(
'z') != std::string::npos)
763 setstate(
fs_.rdstate());
764 exceptions(std::ios_base::badbit);
783 std::istream(nullptr)
791 setstate(
fs_.rdstate());
792 exceptions(std::ios_base::badbit);
char * check_strerror_r(int r, char *buff, size_t buff_size)
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)
Exception class thrown by failed zlib operations.
ostreambuf(std::streambuf *sbuf_p_, std::size_t buff_size_=default_buff_size, int level_=Z_DEFAULT_COMPRESSION)
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)
istreambuf(std::streambuf *sbuf_p_, std::size_t buff_size_=default_buff_size, bool auto_detect_=true)
ostreambuf & operator=(const ostreambuf &)=delete
Exception(const std::string msg_)
ostream(std::streambuf *sbuf_p)
int deflate_loop(int flush)
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
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
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)
Exception(const std::string &msg_)
static std::string mode_to_string(std::ios_base::openmode mode)
istream(std::streambuf *sbuf_p)
z_stream_wrapper(bool is_input_=true, int level_=Z_DEFAULT_COMPRESSION)
strict_fstream_holder(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
const std::string filename
Exception class thrown by failed operations.
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)