-
Notifications
You must be signed in to change notification settings - Fork 2
/
cio.hpp
476 lines (384 loc) · 8.1 KB
/
cio.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#ifndef CIO_CIO_HPP
#define CIO_CIO_HPP
#include <cstdio>
#include <functional>
#include <limits>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
namespace cio
{
enum class mode
{
read = 1 << 0,
write = 1 << 1,
append = 1 << 2,
text = 1 << 3,
no_overwrite = 1 << 4
};
constexpr mode operator|(mode a, mode b) noexcept
{
return mode(unsigned(a) | unsigned(b));
}
constexpr mode operator&(mode a, mode b) noexcept
{
return mode(unsigned(a) & unsigned(b));
}
namespace impl
{
std::string flags_to_modestr(cio::mode flags) noexcept;
}
struct no_ownership_t { };
static constexpr no_ownership_t no_ownership = {};
class stream
{
private:
std::FILE* fh;
bool m_unowned = false;
public:
typedef std::FILE* element_type;
stream(std::FILE* fh) noexcept
: fh(fh)
{ }
stream(std::FILE* fh, no_ownership_t) noexcept
: fh(fh)
, m_unowned(true)
{ }
explicit stream(const char* filename, cio::mode flags = {}) noexcept
: fh(nullptr)
{
open(filename, flags);
}
stream(const char* filename, const char* modestr) noexcept
: fh(nullptr)
{
open(filename, modestr);
}
stream(stream&& other) noexcept
: fh(other.fh)
, m_unowned(other.m_unowned)
{
other.fh = nullptr;
other.m_unowned = true;
}
stream(const stream&) = delete;
stream& operator=(const stream&) = delete;
bool open(const char* filename, cio::mode flags = {}) noexcept
{
if (fh && !m_unowned)
std::fclose(fh);
fh = std::fopen(filename, impl::flags_to_modestr(flags).c_str());
m_unowned = false;
return (fh != nullptr);
}
bool open(const char* filename, const char* modestr) noexcept
{
if (fh && !m_unowned)
std::fclose(fh);
fh = std::fopen(filename, modestr);
m_unowned = false;
return (fh != nullptr);
}
void close() noexcept
{
std::fclose(fh);
fh = nullptr;
}
std::FILE* handle() noexcept
{
return fh;
}
const std::FILE* handle() const noexcept
{
return fh;
}
std::FILE* release() noexcept
{
std::FILE* temp_fh = fh;
fh = nullptr;
return temp_fh;
}
void reset(std::FILE* new_fh = nullptr) noexcept
{
if (fh && !m_unowned)
std::fclose(fh);
fh = new_fh;
m_unowned = false;
}
void reset(std::FILE* new_fh, no_ownership_t) noexcept
{
if (fh && !m_unowned)
std::fclose(fh);
fh = new_fh;
m_unowned = true;
}
void swap(stream& other) noexcept
{
std::swap(fh, other.fh);
std::swap(m_unowned, other.m_unowned);
}
bool is_open() const noexcept
{
return fh != nullptr;
}
bool eof() const noexcept
{
return std::feof(fh);
}
bool error() const noexcept
{
return std::ferror(fh);
}
void clear() noexcept
{
std::clearerr(fh);
}
explicit operator bool() const noexcept
{
return is_open() && !error();
}
bool put(char c) noexcept
{
int result = std::fputc(static_cast<int>(c), fh);
return (result != EOF);
}
bool put(signed char c) noexcept
{
int result = std::fputc(static_cast<int>(c), fh);
return (result != EOF);
}
bool put(unsigned char c) noexcept
{
int result = std::fputc(static_cast<int>(c), fh);
return (result != EOF);
}
bool get(char& c) noexcept
{
int result = std::fgetc(fh);
bool ok = (result != EOF);
if (ok)
c = static_cast<char>(result);
return ok;
}
bool get(signed char& c) noexcept
{
int result = std::fgetc(fh);
bool ok = (result != EOF);
if (ok)
c = static_cast<signed char>(result);
return ok;
}
bool get(unsigned char& c) noexcept
{
int result = std::fgetc(fh);
bool ok = (result != EOF);
if (ok)
c = static_cast<unsigned char>(result);
return ok;
}
bool unget(char c) noexcept
{
int result = std::ungetc(static_cast<int>(c), fh);
return (result != EOF);
}
bool unget(signed char c) noexcept
{
int result = std::ungetc(static_cast<int>(c), fh);
return (result != EOF);
}
bool unget(unsigned char c) noexcept
{
int result = std::ungetc(static_cast<int>(c), fh);
return (result != EOF);
}
bool write(const char* data) noexcept
{
int result = std::fputs(data, fh);
return (result > 0);
}
std::size_t write(const char* data, std::size_t n) noexcept
{
return std::fwrite(data, 1, n, fh);
}
std::size_t read(char* data, std::size_t n) noexcept
{
return std::fread(data, 1, n, fh);
}
void rewind() noexcept
{
std::rewind(fh);
}
bool seek(long offset) noexcept
{
int result = std::fseek(fh, offset, SEEK_SET);
return (result == 0);
}
bool skip(long offset) noexcept
{
int result = std::fseek(fh, offset, SEEK_CUR);
return (result == 0);
}
bool seek_reverse(long offset) noexcept
{
int result = std::fseek(fh, offset, SEEK_END);
return (result == 0);
}
long tell() noexcept
{
return std::ftell(fh);
}
void flush() noexcept
{
std::fflush(fh);
}
~stream() noexcept
{
if (!m_unowned && fh)
close();
}
};
struct io_flush_term_t { };
struct io_endl_term_t { };
constexpr io_flush_term_t flush;
constexpr io_endl_term_t endl;
inline stream& operator <<(stream& io, bool b) noexcept
{
io.write(b ? "true" : "false");
return io;
}
inline stream& operator <<(stream& io, void* p) noexcept
{
std::fprintf(io.handle(), "%p", p);
return io;
}
inline stream& operator <<(stream& io, char c) noexcept
{
io.put(c);
return io;
}
inline stream& operator <<(stream& io, signed char c) noexcept
{
io.put(c);
return io;
}
inline stream& operator <<(stream& io, unsigned char c) noexcept
{
io.put(c);
return io;
}
inline stream& operator <<(stream& io, int i) noexcept
{
std::fprintf(io.handle(), "%i", i);
return io;
}
inline stream& operator <<(stream& io, unsigned int i) noexcept
{
std::fprintf(io.handle(), "%u", i);
return io;
}
inline stream& operator <<(stream& io, short i) noexcept
{
return (io << static_cast<int>(i));
}
inline stream& operator <<(stream& io, unsigned short i) noexcept
{
return (io << static_cast<unsigned int>(i));
}
inline stream& operator <<(stream& io, long i) noexcept
{
std::fprintf(io.handle(), "%li", i);
return io;
}
inline stream& operator <<(stream& io, unsigned long i) noexcept
{
std::fprintf(io.handle(), "%lu", i);
return io;
}
inline stream& operator <<(stream& io, long long i) noexcept
{
std::fprintf(io.handle(), "%lli", i);
return io;
}
inline stream& operator <<(stream& io, unsigned long long i) noexcept
{
std::fprintf(io.handle(), "%llu", i);
return io;
}
inline stream& operator <<(stream& io, float f) noexcept
{
std::fprintf(io.handle(), "%f", double(f));
return io;
}
inline stream& operator <<(stream& io, double f) noexcept
{
std::fprintf(io.handle(), "%f", f);
return io;
}
inline stream& operator <<(stream& io, long double f) noexcept
{
std::fprintf(io.handle(), "%Lf", f);
return io;
}
inline stream& operator <<(stream& io, const char* s) noexcept
{
io.write(s);
return io;
}
template <std::size_t Sz> stream& operator <<(stream& io, const char s[Sz]) noexcept
{
io.write(s, Sz);
return io;
}
inline stream& operator <<(stream& io, const std::string& s) noexcept
{
io.write(s.data(), s.size());
return io;
}
inline stream& operator <<(stream& io, const io_flush_term_t&) noexcept
{
io.flush();
return io;
}
inline stream& operator <<(stream& io, const io_endl_term_t&) noexcept
{
io.put('\n');
io.flush();
return io;
}
inline stream& operator <<(stream& io, const std::string_view& sv) noexcept
{
io.write(sv.data(), sv.size());
return io;
}
inline void swap(stream& a, stream& b) noexcept
{
a.swap(b);
}
extern stream in;
extern stream out;
extern stream err;
}
namespace std
{
template <> struct hash<::cio::stream>
{
typedef ::cio::stream argument_type;
typedef size_t result_type;
size_t operator()(const ::cio::stream& io) const noexcept
{
return hash<const std::FILE*>()(io.handle());
}
};
template <> struct hash<::cio::mode>
{
typedef ::cio::mode argument_type;
typedef size_t result_type;
size_t operator()(const ::cio::mode& mode) const noexcept
{
using UT = underlying_type<::cio::mode>::type;
return hash<UT>()(UT(mode));
}
};
}
#endif // CIO_CIO_HPP