-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemFileCxx.cpp
More file actions
294 lines (251 loc) · 5.43 KB
/
SystemFileCxx.cpp
File metadata and controls
294 lines (251 loc) · 5.43 KB
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
/*
* System::File class implementation using standard C++
*
* Copyright (C) 2016 Thomas Faber
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SystemCxx.hpp"
#include <cerrno>
#include <cstdio>
namespace System
{
static File::Handle HandleFromFILE(std::FILE *P)
{
File::Handle h;
h.P = P;
return h;
}
static FILE *FILEFromHandle(File::Handle H)
{
return static_cast<FILE *>(H.P);
}
File::File() :
m_Handle(HandleFromFILE(nullptr))
{
}
bool
File::Valid() const
{
return m_Handle.P != nullptr;
}
Error
File::Open(
const char *Path,
OpenFlags Flags)
{
const char *mode;
if (Valid())
{
Close();
}
Assert(!Valid());
if (Flags & OpenForWrite)
{
if (Flags & OpenCreate)
{
mode = "wb+";
}
else
{
mode = "rb+";
}
}
else
{
if (Flags & OpenCreate)
{
Assert(!( (Flags & OpenCreate) && !(Flags & OpenForWrite)) );
mode = "wb+";
}
else
{
mode = "rb";
}
}
m_Handle.P = std::fopen(Path, mode);
if (m_Handle.P == nullptr)
{
Assert(!Valid());
return ErrorFromErrno(errno);
}
Assert(Valid());
return ErrorOk;
}
void
File::Close()
{
Assert(Valid());
if (m_Handle.P)
{
std::fclose(FILEFromHandle(m_Handle));
}
m_Handle.P = nullptr;
Assert(!Valid());
}
Error
File::GetSize(
uint64_t *Size) const
{
Error error;
uint64_t oldPos;
uint64_t fileSize;
*Size = 0;
if (!Valid())
{
Assert(Valid());
return ErrorInvalidOperation;
}
error = Tell(&oldPos);
if (error)
{
return error;
}
error = Seek(0, SeekEnd);
if (error)
{
return error;
}
error = Tell(&fileSize);
(void)Seek(oldPos, SeekSet);
if (error)
{
return error;
}
*Size = fileSize;
return ErrorOk;
}
Error
File::Seek(
uint64_t Position,
SeekOrigins Origin) const
{
int ret;
int origin;
if (!Valid())
{
Assert(Valid());
return ErrorInvalidOperation;
}
switch (Origin)
{
case SeekSet:
origin = SEEK_SET;
break;
case SeekCur:
origin = SEEK_CUR;
break;
case SeekEnd:
origin = SEEK_END;
break;
default:
Assert(Origin == SeekSet || Origin == SeekCur || Origin == SeekEnd);
return ErrorInvalidParameter;
}
#if defined(_WIN32)
ret = ::_fseeki64(FILEFromHandle(m_Handle), static_cast<int64_t>(Position), origin);
#else /* defined(_WIN32) */
if (Position > LONG_MAX)
{
return ErrorInvalidParameter;
}
ret = std::fseek(FILEFromHandle(m_Handle), Position, origin);
#endif /* defined(_WIN32) */
if (ret != 0)
{
return ErrorUnknown;
}
return ErrorOk;
}
Error
File::Tell(
uint64_t *Position) const
{
int64_t pos;
*Position = 0;
if (!Valid())
{
Assert(Valid());
return ErrorInvalidOperation;
}
#ifdef _WIN32
pos = ::_ftelli64(FILEFromHandle(m_Handle));
#else
pos = std::ftell(FILEFromHandle(m_Handle));
#endif
if (pos < 0)
{
return ErrorUnknown;
}
*Position = static_cast<uint64_t>(pos);
return ErrorOk;
}
Error
File::Read(
void *Buffer,
unsigned Length,
unsigned *BytesRead) const
{
size_t bytes;
*BytesRead = 0;
if (!Valid())
{
Assert(Valid());
return ErrorInvalidOperation;
}
bytes = fread(Buffer, 1, Length, FILEFromHandle(m_Handle));
if (bytes != Length)
{
Assert(bytes < Length);
if (std::ferror(FILEFromHandle(m_Handle)))
{
return ErrorUnknown;
}
}
*BytesRead = bytes;
return ErrorOk;
}
Error
File::Write(
const void *Buffer,
unsigned Length,
unsigned *BytesWritten) const
{
size_t bytes;
*BytesWritten = 0;
if (!Valid())
{
Assert(Valid());
return ErrorInvalidOperation;
}
bytes = fwrite(Buffer, 1, Length, FILEFromHandle(m_Handle));
if (bytes != Length)
{
Assert(bytes < Length);
if (std::ferror(FILEFromHandle(m_Handle)))
{
return ErrorUnknown;
}
}
*BytesWritten = bytes;
return ErrorOk;
}
File::Handle
File::GetHandle(void) const
{
Assert(Valid());
return m_Handle;
}
} /* namespace System */