1 #ifndef __STRICT_FSTREAM_HPP
2 #define __STRICT_FSTREAM_HPP
4 #define __STDC_WANT_LIB_EXT1__ 1
11 #define NOEXCEPT noexcept
31 std::string buff(80,
'\0');
32 #if defined _WIN32 || defined __STDC_LIB_EXT1__
33 if (strerror_s(&buff[0], buff.size(), errno) != 0)
35 buff =
"Unknown error";
38 if (strerror_r(errno, &buff[0], buff.size()) != 0)
40 buff =
"Unknown error";
43 buff.resize(buff.find(
'\0'));
49 :
public std::exception
65 static const int n_modes = 6;
66 static const std::ios_base::openmode mode_val_v[n_modes] =
76 static const char * mode_name_v[n_modes] =
86 for (
int i = 0; i < n_modes; ++i)
88 if (mode & mode_val_v[i])
90 res += (! res.empty()?
"|" :
"");
91 res += mode_name_v[i];
94 if (res.empty()) res =
"none";
97 static void check_mode(
const std::string& filename, std::ios_base::openmode mode)
99 if ((mode & std::ios_base::trunc) && ! (mode & std::ios_base::out))
101 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: trunc and not out");
103 else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
105 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: app and not out");
107 else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
109 throw Exception(std::string(
"strict_fstream: open('") + filename +
"'): mode error: trunc and app");
112 static void check_open(std::ios * s_p,
const std::string& filename, std::ios_base::openmode mode)
116 throw Exception(std::string(
"strict_fstream: open('")
121 static void check_peek(std::istream * is_p,
const std::string& filename, std::ios_base::openmode mode)
123 bool peek_failed =
true;
127 peek_failed = is_p->fail();
129 catch (std::ios_base::failure&) {}
132 throw Exception(std::string(
"strict_fstream: open('")
143 :
public std::ifstream
147 ifstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
149 open(filename, mode);
151 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
153 mode |= std::ios_base::in;
154 exceptions(std::ios_base::badbit);
156 std::ifstream::open(filename, mode);
163 :
public std::ofstream
167 ofstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
169 open(filename, mode);
171 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
173 mode |= std::ios_base::out;
174 exceptions(std::ios_base::badbit);
176 std::ofstream::open(filename, mode);
182 :
public std::fstream
186 fstream(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
188 open(filename, mode);
190 void open(
const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
192 if (! (mode & std::ios_base::out)) mode |= std::ios_base::in;
193 exceptions(std::ios_base::badbit);
195 std::fstream::open(filename, mode);
Exception class thrown by failed operations.
Exception(const std::string &msg)
const char * what() const NOEXCEPT
fstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
void open(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)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
void open(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
ofstream(const std::string &filename, std::ios_base::openmode mode=std::ios_base::out)
static std::string strerror()
static void check_mode(const std::string &filename, std::ios_base::openmode mode)
static void check_open(std::ios *s_p, const std::string &filename, std::ios_base::openmode mode)
static std::string mode_to_string(std::ios_base::openmode mode)
static void check_peek(std::istream *is_p, const std::string &filename, std::ios_base::openmode mode)