/
filehandle.cpp
500 lines (433 loc) · 11.1 KB
/
filehandle.cpp
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**
* @file filehandle.cpp
* Reference/handle to a unique file in the engine's virtual file system.
*
* @ingroup fs
*
* @author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program 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 General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <cctype>
#include <ctime>
#include <sys/stat.h>
#include "de_platform.h"
#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"
#include "blockset.h"
#include "filehandle.h"
#include <de/memory.h>
namespace de {
struct FileHandle::Instance
{
/// The referenced file (if any).
File1* file;
/// Either the FileList which owns this or the next FileHandle in the used object pool.
void* list;
struct dfile_flags_s {
uint open:1; /// Presently open.
uint eof:1; /// Reader has reached the end of the stream.
uint reference:1; /// This handle is a reference to another dfile instance.
} flags;
/// Offset from start of owning package.
size_t baseOffset;
FILE* hndl;
size_t size;
uint8_t* data;
uint8_t* pos;
Instance() : file(0), list(0), baseOffset(0), hndl(0), size(0), data(0), pos(0)
{
flags.eof = false;
flags.open = false;
flags.reference = false;
}
};
#if 0
// Has the handle builder been initialized yet?
static bool inited;
// A mutex is used to protect access to the shared handle block allocator and object pool.
static mutex_t mutex;
// File handles are allocated by this block allocator.
static blockset_t* handleBlockSet;
// Head of the llist of used file handles, for recycling.
static de::FileHandle* usedHandles;
#endif
static void errorIfNotValid(de::FileHandle const& file, char const* callerName)
{
if(file.isValid()) return;
Con_Error("%s: Instance %p has not yet been initialized.", callerName, (void*)&file);
exit(1); // Unreachable.
}
void FileHandleBuilder::init(void)
{
#if 0
if(!inited)
{
mutex = Sys_CreateMutex(0);
inited = true;
return;
}
Con_Error("FileHandleBuilder::init: Already initialized.");
#endif
}
void FileHandleBuilder::shutdown(void)
{
#if 0
if(inited)
{
Sys_Lock(mutex);
BlockSet_Delete(handleBlockSet); handleBlockSet = 0;
usedHandles = 0;
Sys_Unlock(mutex);
Sys_DestroyMutex(mutex); mutex = 0;
inited = false;
return;
}
#if _DEBUG
Con_Error("FileHandleBuilder::shutdown: Not presently initialized.");
#endif
#endif
}
FileHandle* FileHandleBuilder::fromFileLump(File1& container, int lumpIdx, bool dontBuffer)
{
if(!container.isValidIndex(lumpIdx)) return 0;
de::FileHandle* hndl = new de::FileHandle();
// Init and load in the lump data.
File1& file = container.lump(lumpIdx);
hndl->d->file = &file;
hndl->d->flags.open = true;
if(!dontBuffer)
{
hndl->d->size = file.size();
hndl->d->pos = hndl->d->data = (uint8_t*) M_Malloc(hndl->d->size);
if(!hndl->d->data)
Con_Error("FileHandleBuilder::fromFileLump: Failed on allocation of %lu bytes for data buffer.",
(unsigned long) hndl->d->size);
#if _DEBUG
LOG_VERBOSE("FileHandle [%p] buffering \"%s:%s\"...")
<< de::dintptr(hndl) << container.composePath().prettyNativePath() << file.composePath();
#endif
file.read((uint8_t*)hndl->d->data, 0, file.size());
}
return hndl;
}
FileHandle* FileHandleBuilder::fromFile(File1& file)
{
de::FileHandle* hndl = new de::FileHandle();
hndl->d->file = &file;
hndl->d->flags.open = true;
hndl->d->flags.reference = true;
return hndl;
}
FileHandle* FileHandleBuilder::fromNativeFile(FILE& file, size_t baseOffset)
{
de::FileHandle* hndl = new de::FileHandle();
hndl->d->flags.open = true;
hndl->d->hndl = &file;
hndl->d->baseOffset = baseOffset;
return hndl;
}
FileHandle* FileHandleBuilder::dup(de::FileHandle const& hndl)
{
de::FileHandle* clone = new de::FileHandle();
clone->d->flags.open = true;
clone->d->flags.reference = true;
clone->d->file = &hndl.file();
return clone;
}
FileHandle::FileHandle(void)
{
#if 0
Sys_Lock(mutex);
de::FileHandle* file;
if(usedHandles)
{
file = usedHandles;
usedHandles = (de::FileHandle*) file->list;
}
else
{
if(!handleBlockSet)
{
handleBlockSet = BlockSet_New(sizeof(de::FileHandle), 64);
}
file = (de::FileHandle*) BlockSet_Allocate(handleBlockSet);
}
Sys_Unlock(mutex);
#endif
d = new Instance();
}
FileHandle::~FileHandle()
{
close();
#if 0
// Copy this file to the used object pool for recycling.
Sys_Lock(mutex);
d->file = 0;
d->list = usedHandles;
usedHandles = this;
Sys_Unlock(mutex);
#endif
delete d;
}
FileHandle& FileHandle::close()
{
if(!d->flags.open) return *this;
if(d->hndl)
{
fclose(d->hndl); d->hndl = NULL;
}
else
{
// Free the stored data.
if(d->data)
{
M_Free(d->data); d->data = NULL;
}
}
d->pos = NULL;
d->flags.open = false;
return *this;
}
bool FileHandle::isValid() const
{
/// @todo write me.
return true;
}
struct filelist_s* FileHandle::list()
{
errorIfNotValid(*this, "FileHandle::list");
return (struct filelist_s*)d->list;
}
FileHandle& FileHandle::setList(struct filelist_s* list)
{
d->list = list;
return *this;
}
bool FileHandle::hasFile() const
{
errorIfNotValid(*this, "FileHandle::file");
return !!d->file;
}
File1& FileHandle::file()
{
errorIfNotValid(*this, "FileHandle::file");
return *d->file;
}
File1& FileHandle::file() const
{
errorIfNotValid(*this, "FileHandle::file const");
return *d->file;
}
size_t FileHandle::baseOffset() const
{
if(d->flags.reference)
{
return d->file->handle().baseOffset();
}
return d->baseOffset;
}
size_t FileHandle::length()
{
errorIfNotValid(*this, "FileHandle::Length");
if(d->flags.reference)
{
return d->file->handle().length();
}
else
{
size_t currentPosition = seek(0, SeekEnd);
size_t length = tell();
seek(currentPosition, SeekSet);
return length;
}
}
size_t FileHandle::read(uint8_t* buffer, size_t count)
{
errorIfNotValid(*this, "FileHandle::read");
if(d->flags.reference)
{
return d->file->handle().read(buffer, count);
}
else
{
if(d->hndl)
{
// Normal file.
count = fread(buffer, 1, count, d->hndl);
if(feof(d->hndl))
d->flags.eof = true;
return count;
}
// Is there enough room in the file?
size_t bytesleft = d->size - (d->pos - d->data);
if(count > bytesleft)
{
count = bytesleft;
d->flags.eof = true;
}
if(count)
{
memcpy(buffer, d->pos, count);
d->pos += count;
}
return count;
}
}
bool FileHandle::atEnd()
{
errorIfNotValid(*this, "FileHandle::atEnd");
if(d->flags.reference)
{
return d->file->handle().atEnd();
}
return (d->flags.eof != 0);
}
unsigned char FileHandle::getC()
{
errorIfNotValid(*this, "FileHandle::getC");
unsigned char ch = 0;
read((uint8_t*)&ch, 1);
return ch;
}
size_t FileHandle::tell()
{
errorIfNotValid(*this, "FileHandle::tell");
if(d->flags.reference)
{
return d->file->handle().tell();
}
else
{
if(d->hndl)
return (size_t) ftell(d->hndl);
return d->pos - d->data;
}
}
size_t FileHandle::seek(size_t offset, SeekMethod whence)
{
if(d->flags.reference)
{
return d->file->handle().seek(offset, whence);
}
else
{
size_t oldpos = tell();
d->flags.eof = false;
if(d->hndl)
{
int fwhence = whence == SeekSet? SEEK_SET :
whence == SeekCur? SEEK_CUR : SEEK_END;
fseek(d->hndl, (long) (d->baseOffset + offset), fwhence);
}
else
{
if(whence == SeekSet)
d->pos = d->data + offset;
else if(whence == SeekEnd)
d->pos = d->data + (d->size + offset);
else if(whence == SeekCur)
d->pos += offset;
}
return oldpos;
}
}
de::FileHandle& FileHandle::rewind()
{
seek(0, SeekSet);
return *this;
}
} // namespace de
/**
* C Wrapper API:
*/
#define TOINTERNAL(inst) \
(inst) != 0? reinterpret_cast<de::FileHandle*>(inst) : NULL
#define TOINTERNAL_CONST(inst) \
(inst) != 0? reinterpret_cast<de::FileHandle const*>(inst) : NULL
#define SELF(inst) \
DENG2_ASSERT(inst); \
de::FileHandle* self = TOINTERNAL(inst)
#define SELF_CONST(inst) \
DENG2_ASSERT(inst); \
de::FileHandle const* self = TOINTERNAL_CONST(inst)
void FileHandle_Delete(struct filehandle_s* hndl)
{
if(!hndl) return;
SELF(hndl);
delete self;
}
void FileHandle_Close(struct filehandle_s* hndl)
{
SELF(hndl);
self->close();
}
boolean FileHandle_IsValid(struct filehandle_s const* hndl)
{
SELF_CONST(hndl);
return self->isValid();
}
size_t FileHandle_Length(struct filehandle_s* hndl)
{
SELF(hndl);
return self->length();
}
size_t FileHandle_BaseOffset(struct filehandle_s const* hndl)
{
SELF_CONST(hndl);
return self->baseOffset();
}
size_t FileHandle_Read(struct filehandle_s* hndl, uint8_t* buffer, size_t count)
{
SELF(hndl);
return self->read(buffer, count);
}
unsigned char FileHandle_GetC(struct filehandle_s* hndl)
{
SELF(hndl);
return self->getC();
}
boolean FileHandle_AtEnd(struct filehandle_s* hndl)
{
SELF(hndl);
return self->atEnd();
}
size_t FileHandle_Tell(struct filehandle_s* hndl)
{
SELF(hndl);
return self->tell();
}
size_t FileHandle_Seek(struct filehandle_s* hndl, size_t offset, SeekMethod whence)
{
SELF(hndl);
return self->seek(offset, whence);
}
void FileHandle_Rewind(struct filehandle_s* hndl)
{
SELF(hndl);
self->rewind();
}
struct file1_s* FileHandle_File(struct filehandle_s* hndl)
{
SELF(hndl);
return reinterpret_cast<struct file1_s*>(&self->file());
}
struct file1_s* FileHandle_File_const(struct filehandle_s const* hndl)
{
SELF_CONST(hndl);
return reinterpret_cast<struct file1_s*>(&self->file());
}