-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrict_fstream.hpp
237 lines (219 loc) · 7.58 KB
/
strict_fstream.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#pragma once
#include <cassert>
#include <fstream>
#include <cstring>
#include <string>
#include <vector>
/**
* This namespace defines wrappers for std::ifstream, std::ofstream, and
* std::fstream objects. The wrappers perform the following steps:
* - check the open modes make sense
* - check that the call to open() is successful
* - (for input streams) check that the opened file is peek-able
* - turn on the badbit in the exception mask
*/
namespace strict_fstream
{
// Help people out a bit, it seems like this is a common recommenation since
// musl breaks all over the place.
#if defined(__NEED_size_t) && !defined(__MUSL__)
#warning "It seems to be recommended to patch in a define for __MUSL__ if you use musl globally: https://www.openwall.com/lists/musl/2013/02/10/5"
#define __MUSL__
#endif
// Workaround for broken musl implementation
// Since musl insists that they are perfectly compatible, ironically enough,
// they don't officially have a __musl__ or similar. But __NEED_size_t is defined in their
// relevant header (and not in working implementations), so we can use that.
#ifdef __MUSL__
#warning "Working around broken strerror_r() implementation in musl, remove when musl is fixed"
#endif
// Non-gnu variants of strerror_* don't necessarily null-terminate if
// truncating, so we have to do things manually.
inline std::string trim_to_null(const std::vector<char> &buff)
{
std::string ret(buff.begin(), buff.end());
const std::string::size_type pos = ret.find('\0');
if (pos == std::string::npos) {
ret += " [...]"; // it has been truncated
} else {
ret.resize(pos);
}
return ret;
}
/// Overload of error-reporting function, to enable use with VS and non-GNU
/// POSIX libc's
/// Ref:
/// - http://stackoverflow.com/a/901316/717706
static std::string strerror()
{
// Can't use std::string since we're pre-C++17
std::vector<char> buff(256, '\0');
#ifdef _WIN32
// Since strerror_s might set errno itself, we need to store it.
const int err_num = errno;
if (strerror_s(buff.data(), buff.size(), err_num) != 0) {
return trim_to_null(buff);
} else {
return "Unknown error (" + std::to_string(err_num) + ")";
}
#elif ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__)) && ! _GNU_SOURCE) || defined(__MUSL__)
// XSI-compliant strerror_r()
const int err_num = errno; // See above
if (strerror_r(err_num, buff.data(), buff.size()) == 0) {
return trim_to_null(buff);
} else {
return "Unknown error (" + std::to_string(err_num) + ")";
}
#else
// GNU-specific strerror_r()
char * p = strerror_r(errno, &buff[0], buff.size());
return std::string(p, std::strlen(p));
#endif
}
/// Exception class thrown by failed operations.
class Exception
: public std::exception
{
public:
Exception(const std::string& msg) : _msg(msg) {}
const char * what() const noexcept { return _msg.c_str(); }
private:
std::string _msg;
}; // class Exception
namespace detail
{
struct static_method_holder
{
static std::string mode_to_string(std::ios_base::openmode mode)
{
static const int n_modes = 6;
static const std::ios_base::openmode mode_val_v[n_modes] =
{
std::ios_base::in,
std::ios_base::out,
std::ios_base::app,
std::ios_base::ate,
std::ios_base::trunc,
std::ios_base::binary
};
static const char * mode_name_v[n_modes] =
{
"in",
"out",
"app",
"ate",
"trunc",
"binary"
};
std::string res;
for (int i = 0; i < n_modes; ++i)
{
if (mode & mode_val_v[i])
{
res += (! res.empty()? "|" : "");
res += mode_name_v[i];
}
}
if (res.empty()) res = "none";
return res;
}
static void check_mode(const std::string& filename, std::ios_base::openmode mode)
{
if ((mode & std::ios_base::trunc) && ! (mode & std::ios_base::out))
{
throw Exception(std::string("strict_fstream: open('") + filename + "'): mode error: trunc and not out");
}
else if ((mode & std::ios_base::app) && ! (mode & std::ios_base::out))
{
throw Exception(std::string("strict_fstream: open('") + filename + "'): mode error: app and not out");
}
else if ((mode & std::ios_base::trunc) && (mode & std::ios_base::app))
{
throw Exception(std::string("strict_fstream: open('") + filename + "'): mode error: trunc and app");
}
}
static void check_open(std::ios * s_p, const std::string& filename, std::ios_base::openmode mode)
{
if (s_p->fail())
{
throw Exception(std::string("strict_fstream: open('")
+ filename + "'," + mode_to_string(mode) + "): open failed: "
+ strerror());
}
}
static void check_peek(std::istream * is_p, const std::string& filename, std::ios_base::openmode mode)
{
bool peek_failed = true;
try
{
is_p->peek();
peek_failed = is_p->fail();
}
catch (const std::ios_base::failure &) {}
if (peek_failed)
{
throw Exception(std::string("strict_fstream: open('")
+ filename + "'," + mode_to_string(mode) + "): peek failed: "
+ strerror());
}
is_p->clear();
}
}; // struct static_method_holder
} // namespace detail
class ifstream
: public std::ifstream
{
public:
ifstream() = default;
ifstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
{
open(filename, mode);
}
void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
{
mode |= std::ios_base::in;
exceptions(std::ios_base::badbit);
detail::static_method_holder::check_mode(filename, mode);
std::ifstream::open(filename, mode);
detail::static_method_holder::check_open(this, filename, mode);
detail::static_method_holder::check_peek(this, filename, mode);
}
}; // class ifstream
class ofstream
: public std::ofstream
{
public:
ofstream() = default;
ofstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
{
open(filename, mode);
}
void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out)
{
mode |= std::ios_base::out;
exceptions(std::ios_base::badbit);
detail::static_method_holder::check_mode(filename, mode);
std::ofstream::open(filename, mode);
detail::static_method_holder::check_open(this, filename, mode);
}
}; // class ofstream
class fstream
: public std::fstream
{
public:
fstream() = default;
fstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
{
open(filename, mode);
}
void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
{
if (! (mode & std::ios_base::out)) mode |= std::ios_base::in;
exceptions(std::ios_base::badbit);
detail::static_method_holder::check_mode(filename, mode);
std::fstream::open(filename, mode);
detail::static_method_holder::check_open(this, filename, mode);
detail::static_method_holder::check_peek(this, filename, mode);
}
}; // class fstream
} // namespace strict_fstream