1 #ifndef __STRICT_FSTREAM_HPP 2 #define __STRICT_FSTREAM_HPP 10 #define NOEXCEPT noexcept 30 std::string buff(80,
'\0');
32 if (strerror_s(&buff[0], buff.size(), errno) != 0)
34 buff =
"Unknown error";
36 #elif __APPLE__ || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE) 38 if (strerror_r(errno, &buff[0], buff.size()) != 0)
40 buff =
"Unknown error";
44 auto p = strerror_r(errno, &buff[0], buff.size());
45 std::string tmp(p, std::strlen(p));
48 buff.resize(buff.find(
'\0'));
54 :
public std::exception
70 static const int n_modes = 6;
71 static const std::ios_base::openmode mode_val_v[n_modes] =
81 static const char * mode_name_v[n_modes] =
91 for (
int i = 0; i < n_modes; ++i)
93 if (mode & mode_val_v[i])
95 res += (! res.empty()?
"|" :
"");
96 res += mode_name_v[i];
99 if (res.empty()) res =
"none";
102 static void check_mode(
const std::string& filename, std::ios_base::openmode mode)
104 if ((mode & std::ios_base::trunc) && ! (mode & std::ios_base::out))
106 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: trunc and not out");
108 else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
110 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: app and not out");
112 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
114 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: trunc and app");
117 static void check_open(std::ios * s_p,
const std::string& filename, std::ios_base::openmode mode)
121 throw Exception(std::string(
"strict_fstream: open('")
122 + filename +
"'," + mode_to_string(mode) +
"): open failed: " 126 static void check_peek(std::istream * is_p,
const std::string& filename, std::ios_base::openmode mode)
128 bool peek_failed =
true;
132 peek_failed = is_p->fail();
134 catch (std::ios_base::failure e) {}
137 throw Exception(std::string(
"strict_fstream: open('")
138 + filename +
"'," + mode_to_string(mode) +
"): peek failed: " 148 :
public std::ifstream
152 ifstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
154 open(filename, mode);
156 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
158 mode |= std::ios_base::in;
159 exceptions(std::ios_base::badbit);
161 std::ifstream::open(filename, mode);
168 :
public std::ofstream
172 ofstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
174 open(filename, mode);
176 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
178 mode |= std::ios_base::out;
179 exceptions(std::ios_base::badbit);
181 std::ofstream::open(filename, mode);
187 :
public std::fstream
191 fstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
193 open(filename, mode);
195 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
197 if (! (mode & std::ios_base::out)) mode |= std::ios_base::in;
198 exceptions(std::ios_base::badbit);
200 std::fstream::open(filename, mode);
static void check_peek(std::istream *is_p, const std::string &filename, std::ios_base::openmode mode)
ofstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
const char * what() const NOEXCEPT
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
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)
static std::string strerror()
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)
static std::string mode_to_string(std::ios_base::openmode mode)
Exception(const std::string &msg)
Exception class thrown by failed operations.
static void check_open(std::ios *s_p, const std::string &filename, std::ios_base::openmode mode)