59 namespace strict_fstream
65 if (r) { snprintf(buff, buff_size,
"unknown error: %d", r); }
76 static std::string strerror()
78 std::string buff(80,
'\0');
80 if (strerror_s(&buff[0], buff.size(), errno) != 0)
82 buff =
"Unknown error";
87 std::string tmp(p, std::strlen(p));
90 buff.resize(buff.find(
'\0'));
96 :
public std::exception
100 const char *
what() const noexcept {
return msg.c_str(); }
112 static const int n_modes = 6;
113 static const std::ios_base::openmode mode_val_v[n_modes] =
119 std::ios_base::trunc,
120 std::ios_base::binary
123 static const char * mode_name_v[n_modes] =
133 for (
int i = 0; i < n_modes; ++i)
135 if (mode & mode_val_v[i])
137 res += (! res.empty()?
"|" :
"");
138 res += mode_name_v[i];
141 if (res.empty()) { res =
"none"; }
145 std::ios_base::openmode mode)
149 throw Exception(std::string(
"strict_fstream: open('") + filename +
150 "'): mode error: trunc and not out");
152 else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
154 throw Exception(std::string(
"strict_fstream: open('") + filename +
155 "'): mode error: app and not out");
157 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
159 throw Exception(std::string(
"strict_fstream: open('") + filename +
160 "'): mode error: trunc and app");
163 static void check_open(std::ios * s_p,
const std::string& filename,
164 std::ios_base::openmode mode)
168 throw Exception(std::string(
"strict_fstream: open('")
173 static void check_peek(std::istream * is_p,
const std::string& filename,
174 std::ios_base::openmode mode)
176 bool peek_failed =
true;
180 peek_failed = is_p->fail();
182 catch (std::ios_base::failure&) {}
185 throw Exception(std::string(
"strict_fstream: open('")
196 :
public std::ifstream
201 std::ios_base::openmode mode = std::ios_base::in)
203 open(filename, mode);
205 void open(
const std::string& filename,
206 std::ios_base::openmode mode = std::ios_base::in)
208 mode |= std::ios_base::in;
209 exceptions(std::ios_base::badbit);
211 std::ifstream::open(filename, mode);
218 :
public std::ofstream
225 open(filename, mode);
227 void open(
const std::string& filename,
231 exceptions(std::ios_base::badbit);
233 std::ofstream::open(filename, mode);
239 :
public std::fstream
244 std::ios_base::openmode mode = std::ios_base::in)
246 open(filename, mode);
248 void open(
const std::string& filename,
249 std::ios_base::openmode mode = std::ios_base::in)
252 exceptions(std::ios_base::badbit);
254 std::fstream::open(filename, mode);
271 :
public std::exception
280 msg +=
"Z_STREAM_ERROR: ";
283 msg +=
"Z_DATA_ERROR: ";
286 msg +=
"Z_MEM_ERROR: ";
288 case Z_VERSION_ERROR:
289 msg +=
"Z_VERSION_ERROR: ";
292 msg +=
"Z_BUF_ERROR: ";
295 std::ostringstream oss;
297 msg +=
"[" + oss.str() +
"]: ";
303 const char *
what() const noexcept {
return msg.c_str(); }
318 : is_input(is_input_)
320 this->zalloc = Z_NULL;
321 this->zfree = Z_NULL;
322 this->opaque = Z_NULL;
327 this->next_in = Z_NULL;
328 ret = inflateInit2(
this, 15 + 32);
332 ret = deflateInit2(
this, level_, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
358 :
public std::streambuf
362 std::size_t buff_size_ = default_buff_size,
bool auto_detect_ =
true)
365 buff_size(buff_size_),
366 auto_detect(auto_detect_),
367 auto_detect_run(false),
371 in_buff =
new char[buff_size];
372 in_buff_start = in_buff;
373 in_buff_end = in_buff;
374 out_buff =
new char[buff_size];
375 setg(out_buff, out_buff, out_buff);
395 if (this->gptr() == this->egptr())
398 char *out_buff_free_start = out_buff;
402 if (in_buff_start == in_buff_end)
405 in_buff_start = in_buff;
406 std::streamsize sz = sbuf_p->sgetn(in_buff, buff_size);
407 in_buff_end = in_buff + sz;
408 if (in_buff_end == in_buff_start)
414 if (auto_detect && !auto_detect_run)
416 auto_detect_run =
true;
417 unsigned char b0 = *
reinterpret_cast<unsigned char *
>(in_buff_start);
418 unsigned char b1 = *
reinterpret_cast<unsigned char *
>(in_buff_start + 1);
422 is_text = !(in_buff_start + 2 <= in_buff_end && ((b0 == 0x1F &&
424 || (b0 == 0x78 && (b1 == 0x01
425 || b1 == 0x9C || b1 == 0xDA))));
430 assert(in_buff_start == in_buff);
431 std::swap(in_buff, out_buff);
432 out_buff_free_start = in_buff_end;
433 in_buff_start = in_buff;
434 in_buff_end = in_buff;
443 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(in_buff_start);
444 zstrm_p->avail_in = in_buff_end - in_buff_start;
445 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>
446 (out_buff_free_start);
447 zstrm_p->avail_out = (out_buff + buff_size) - out_buff_free_start;
448 int ret = inflate(zstrm_p, Z_NO_FLUSH);
450 if (ret != Z_OK && ret != Z_STREAM_END)
455 in_buff_start =
reinterpret_cast<decltype(in_buff_start)
>(zstrm_p->next_in);
456 in_buff_end = in_buff_start + zstrm_p->avail_in;
457 out_buff_free_start =
reinterpret_cast<decltype(out_buff_free_start)
>
459 assert(out_buff_free_start + zstrm_p->avail_out == out_buff + buff_size);
461 if (ret == Z_STREAM_END)
468 while (out_buff_free_start == out_buff);
472 this->setg(out_buff, out_buff, out_buff_free_start);
474 return this->gptr() == this->egptr()
476 : traits_type::to_int_type(*this->gptr());
480 std::streambuf *sbuf_p;
486 std::size_t buff_size;
488 bool auto_detect_run;
491 static const std::size_t default_buff_size = (std::size_t)1 << 20;
495 :
public std::streambuf
499 std::size_t buff_size_ = default_buff_size,
int level_ = Z_DEFAULT_COMPRESSION)
501 zstrm_p(new detail::z_stream_wrapper(false, level_)),
502 buff_size(buff_size_)
505 in_buff =
new char[buff_size];
506 out_buff =
new char[buff_size];
507 setp(in_buff, in_buff + buff_size);
519 zstrm_p->next_out =
reinterpret_cast<decltype(zstrm_p-
>next_out)>(out_buff);
520 zstrm_p->avail_out = buff_size;
521 int ret = deflate(zstrm_p, flush);
522 if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR)
526 std::streamsize sz = sbuf_p->sputn(out_buff,
527 reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff);
528 if (sz !=
reinterpret_cast<decltype(out_buff)
>(zstrm_p->next_out) - out_buff)
533 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR || sz == 0)
556 virtual std::streambuf::int_type
overflow(std::streambuf::int_type c =
559 zstrm_p->next_in =
reinterpret_cast<decltype(zstrm_p-
>next_in)>(pbase());
560 zstrm_p->avail_in = pptr() - pbase();
561 while (zstrm_p->avail_in > 0)
566 setp(
nullptr,
nullptr);
567 return traits_type::eof();
570 setp(in_buff, in_buff + buff_size);
571 return traits_type::eq_int_type(c,
585 zstrm_p->next_in =
nullptr;
586 zstrm_p->avail_in = 0;
591 deflateReset(zstrm_p);
596 std::streambuf *sbuf_p;
600 std::size_t buff_size;
602 static const std::size_t default_buff_size = (std::size_t)1 << 20;
606 :
public std::istream
612 exceptions(std::ios_base::badbit);
617 exceptions(std::ios_base::badbit);
626 :
public std::ostream
632 exceptions(std::ios_base::badbit);
637 exceptions(std::ios_base::badbit);
649 template <
typename FStream_Type>
653 std::ios_base::openmode mode = std::ios_base::in)
654 :
fs_(filename, mode)
669 std::ios_base::openmode mode = std::ios_base::in)
673 exceptions(std::ios_base::badbit);
692 mode | std::ios_base::binary),
695 exceptions(std::ios_base::badbit);
721 bool compression =
false)
723 std::ios_base::binary),
724 std::ostream(nullptr)
737 exceptions(std::ios_base::badbit);
741 char const *open_mode_chars)
743 std::ios_base::binary),
744 std::ostream(nullptr)
751 if (std::string(open_mode_chars).find(
'z') != std::string::npos)
761 setstate(
fs_.rdstate());
762 exceptions(std::ios_base::badbit);
781 std::istream(nullptr)
789 setstate(
fs_.rdstate());
790 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
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.
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)