-
Notifications
You must be signed in to change notification settings - Fork 2
/
cio_physfs.hpp
297 lines (234 loc) · 4.84 KB
/
cio_physfs.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
#ifndef CIO_PHYSIO_HPP
#define CIO_PHYSIO_HPP
#include "cio.hpp"
#include "physfs.h"
namespace cio
{
class physfs_stream
{
private:
PHYSFS_File* fh;
bool m_unowned = false;
PHYSFS_ErrorCode m_error = PHYSFS_ERR_OK;
public:
typedef PHYSFS_File* element_type;
physfs_stream(PHYSFS_File* fh) noexcept
: fh(fh)
{ }
physfs_stream(PHYSFS_File* fh, no_ownership_t) noexcept
: fh(fh)
, m_unowned(true)
{ }
explicit physfs_stream(const char* filename) noexcept
: fh(nullptr)
{
open(filename);
}
physfs_stream(physfs_stream&& other) noexcept
: fh(other.fh)
, m_unowned(other.m_unowned)
, m_error(other.m_error)
{
other.fh = nullptr;
other.m_unowned = true;
}
physfs_stream(const physfs_stream&) = delete;
physfs_stream& operator=(const physfs_stream&) = delete;
bool open(const char* filename) noexcept
{
if (fh && !m_unowned)
close();
fh = PHYSFS_openRead(filename);
m_unowned = false;
return (fh != nullptr);
}
void close() noexcept
{
PHYSFS_close(fh);
fh = nullptr;
m_error = PHYSFS_ERR_OK;
}
PHYSFS_File* handle() noexcept
{
return fh;
}
const PHYSFS_File* handle() const noexcept
{
return fh;
}
PHYSFS_File* release() noexcept
{
PHYSFS_File* temp_fh = fh;
fh = nullptr;
return temp_fh;
}
void reset(PHYSFS_File* new_fh = nullptr) noexcept
{
if (fh && !m_unowned)
close();
fh = new_fh;
m_unowned = false;
}
void reset(PHYSFS_File* new_fh, no_ownership_t) noexcept
{
if (fh && !m_unowned)
close();
fh = new_fh;
m_unowned = true;
}
void swap(physfs_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 PHYSFS_eof(fh);
}
bool error() const noexcept
{
return eof() || m_error != PHYSFS_ERR_OK;
}
void clear() noexcept
{
m_error = PHYSFS_ERR_OK;
}
explicit operator bool() const noexcept
{
return is_open() && !error();
}
bool get(char& c) noexcept
{
std::size_t bytes_read = read(&c, 1);
return (bytes_read == 1);
}
bool get(signed char& c) noexcept
{
std::size_t bytes_read = read(reinterpret_cast<char*>(&c), 1);
return (bytes_read == 1);
}
bool get(unsigned char& c) noexcept
{
std::size_t bytes_read = read(reinterpret_cast<char*>(&c), 1);
return (bytes_read == 1);
}
std::size_t read(char* data, std::size_t n) noexcept
{
PHYSFS_sint64 result = PHYSFS_readBytes(fh, data, n);
if (result == -1 || result == 0)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
return static_cast<std::size_t>(result);
}
void rewind() noexcept
{
seek(0);
}
bool seek(long offset) noexcept
{
if (offset < 0)
{
m_error = PHYSFS_ERR_PAST_EOF;
return false;
}
int result = PHYSFS_seek(fh, static_cast<PHYSFS_uint64>(offset));
if (result != 0)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
return true;
}
bool skip(long offset) noexcept
{
PHYSFS_sint64 pos = PHYSFS_tell(fh);
if (pos == -1)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
pos += static_cast<PHYSFS_sint64>(offset);
if (pos < 0)
{
m_error = PHYSFS_ERR_PAST_EOF;
return false;
}
int result = PHYSFS_seek(fh, static_cast<PHYSFS_uint64>(pos));
if (result != 0)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
return true;
}
bool seek_reverse(long offset) noexcept
{
PHYSFS_sint64 file_length = PHYSFS_fileLength(fh);
if (file_length == -1)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
PHYSFS_sint64 pos = file_length - static_cast<PHYSFS_sint64>(offset);
if (pos < 0)
{
m_error = PHYSFS_ERR_PAST_EOF;
return false;
}
int result = PHYSFS_seek(fh, static_cast<PHYSFS_uint64>(pos));
if (result != 0)
{
m_error = PHYSFS_getLastErrorCode();
return false;
}
return true;
}
long tell() noexcept
{
PHYSFS_sint64 result = PHYSFS_tell(fh);
if (result == -1)
m_error = PHYSFS_getLastErrorCode();
return static_cast<long>(result);
}
void flush() noexcept
{
PHYSFS_flush(fh);
}
PHYSFS_ErrorCode errcode() const noexcept
{
return m_error;
}
const char* errstr() const noexcept
{
return PHYSFS_getErrorByCode(m_error);
}
~physfs_stream() noexcept
{
if (!m_unowned && fh)
close();
}
};
inline void swap(physfs_stream& a, physfs_stream& b) noexcept
{
a.swap(b);
}
}
namespace std
{
template <> struct hash<::cio::physfs_stream>
{
typedef ::cio::physfs_stream argument_type;
typedef size_t result_type;
size_t operator()(const ::cio::physfs_stream& io) const noexcept
{
return hash<const PHYSFS_File*>()(io.handle());
}
};
}
#endif // CIO_PHYSFS_HPP