Repository navigation
Expand file tree
/
Copy pathm_buffer.cpp
More file actions
513 lines (449 loc) · 9.19 KB
/
Copy pathm_buffer.cpp
File metadata and controls
513 lines (449 loc) · 9.19 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
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
501
502
503
504
505
506
507
508
509
510
511
512
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2010 James Haley
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//--------------------------------------------------------------------------
//
// DESCRIPTION:
// Buffered file input/output.
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "i_system.h"
#include "m_buffer.h"
#include "m_swap.h"
//=============================================================================
//
// BufferedFileBase
//
// haleyjd 11/26/10: Having converted to C++ with the aim of adding an input
// buffer as well, these base class methods implement shared functionality.
//
//
// BufferedFileBase::InitBuffer
//
// Sets up the buffer
//
void BufferedFileBase::InitBuffer(size_t pLen, int pEndian)
{
buffer = ecalloc(byte *, pLen, sizeof(byte));
len = pLen;
idx = 0;
endian = pEndian;
}
//
// BufferedFileBase::Tell
//
// Gives the current file offset; this does not account for any data that might
// be currently pending in an output buffer.
//
long BufferedFileBase::Tell()
{
return ftell(f);
}
//
// BufferedFileBase::Close
//
// Common functionality for closing the file.
//
void BufferedFileBase::Close()
{
if(f)
{
fclose(f);
f = NULL;
}
idx = 0;
len = 0;
if(buffer)
{
efree(buffer);
buffer = NULL;
}
ownFile = false;
}
//
// BufferedFileBase::SwapULong
//
// Transform a uint32 value based on the 'endian' setting.
//
void BufferedFileBase::SwapULong(uint32_t &x)
{
switch(endian)
{
case LENDIAN:
x = ::SwapULong(x);
break;
case BENDIAN:
x = ::SwapBigULong(x);
break;
default:
break;
}
}
//
// BufferedFileBase::SwapLong
//
// As above but for signed ints.
//
void BufferedFileBase::SwapLong(int32_t &x)
{
switch(endian)
{
case LENDIAN:
x = ::SwapLong(x);
break;
case BENDIAN:
x = ::SwapBigLong(x);
break;
default:
break;
}
}
//
// BufferedFileBase::SwapUShort
//
// Transform a uint16 value based on the 'endian' setting.
//
void BufferedFileBase::SwapUShort(uint16_t &x)
{
switch(endian)
{
case LENDIAN:
x = ::SwapUShort(x);
break;
case BENDIAN:
x = ::SwapBigUShort(x);
break;
default:
break;
}
}
//
// BufferedFileBase::SwapShort
//
// As above but for signed 16-bit shorts.
//
void BufferedFileBase::SwapShort(int16_t &x)
{
switch(endian)
{
case LENDIAN:
x = ::SwapShort(x);
break;
case BENDIAN:
x = ::SwapBigShort(x);
break;
default:
break;
}
}
//=============================================================================
//
// OutBuffer
//
// Buffered file output
//
//
// OutBuffer::CreateFile
//
// Opens a file for buffered binary output with the given filename. The buffer
// size is determined by the len parameter.
//
bool OutBuffer::CreateFile(const char *filename, size_t pLen, int pEndian)
{
if(!(f = fopen(filename, "wb")))
return false;
InitBuffer(pLen, pEndian);
ownFile = true;
return true;
}
//
// OutBuffer::Flush
//
// Call to flush the contents of the buffer to the output file. This will be
// called automatically before the file is closed, but must be called explicitly
// if a current file offset is needed. Returns false if an IO error occurs.
//
bool OutBuffer::Flush()
{
if(idx)
{
if(fwrite(buffer, sizeof(byte), idx, f) < idx)
{
if(throwing)
throw BufferedIOException("fwrite did not write the requested amount");
return false;
}
idx = 0;
}
return true;
}
//
// OutBuffer::Close
//
// Overrides BufferedFileBase::Close()
// Closes the output file, writing any pending data in the buffer first.
// The output buffer is also freed.
//
void OutBuffer::Close()
{
try
{
if(f)
Flush();
}
catch(...)
{
}
BufferedFileBase::Close();
}
//
// OutBuffer::Write
//
// Buffered writing function.
//
bool OutBuffer::Write(const void *data, size_t size)
{
const byte *lSrc = (const byte *)data;
size_t lWriteAmt;
size_t lBytesToWrite = size;
while(lBytesToWrite)
{
lWriteAmt = len - idx;
if(!lWriteAmt)
{
if(!Flush())
return false;
lWriteAmt = len;
}
if(lBytesToWrite < lWriteAmt)
lWriteAmt = lBytesToWrite;
memcpy(&(buffer[idx]), lSrc, lWriteAmt);
idx += lWriteAmt;
lSrc += lWriteAmt;
lBytesToWrite -= lWriteAmt;
}
return true;
}
//
// OutBuffer::WriteUint32
//
// Convenience routine to write an unsigned integer into the buffer.
//
bool OutBuffer::WriteUint32(uint32_t num)
{
SwapULong(num);
return Write(&num, sizeof(uint32_t));
}
//
// OutBuffer::WriteSint32
//
// Convenience routine to write an integer into the buffer.
//
bool OutBuffer::WriteSint32(int32_t num)
{
SwapLong(num);
return Write(&num, sizeof(int32_t));
}
//
// OutBuffer::WriteUint16
//
// Convenience routine to write an unsigned short int into the buffer.
//
bool OutBuffer::WriteUint16(uint16_t num)
{
SwapUShort(num);
return Write(&num, sizeof(uint16_t));
}
//
// OutBuffer::WriteSint16
//
// Convenience routine to write a short int into the buffer.
//
bool OutBuffer::WriteSint16(int16_t num)
{
SwapShort(num);
return Write(&num, sizeof(int16_t));
}
//
// OutBuffer::WriteUint8
//
// Routine to write an unsigned byte into the buffer.
// This is much more efficient than calling M_BufferWrite for individual bytes.
//
bool OutBuffer::WriteUint8(uint8_t num)
{
if(idx == len)
{
if(!Flush())
return false;
}
buffer[idx] = num;
++idx;
return true;
}
//
// OutBuffer::WriteSint8
//
// Routine to write a byte into the buffer.
// This is much more efficient than calling M_BufferWrite for individual bytes.
//
bool OutBuffer::WriteSint8(int8_t num)
{
return WriteUint8((uint8_t)num);
}
//=============================================================================
//
// InBuffer
//
// haleyjd 11/26/10: Buffered file input
//
//
// InBuffer::openFile
//
// Opens a file for binary input.
//
bool InBuffer::openFile(const char *filename, int pEndian)
{
if(!(f = fopen(filename, "rb")))
return false;
endian = pEndian;
ownFile = true;
return true;
}
//
// InBuffer::openExisting
//
// Attach the input buffer to an already open file.
//
bool InBuffer::openExisting(FILE *pf, int pEndian)
{
if(!(f = pf))
return false;
endian = pEndian;
ownFile = false;
return true;
}
//
// InBuffer::seek
//
// Seeks inside the file via fseek, and then clears the internal buffer.
//
int InBuffer::seek(long offset, int origin)
{
return fseek(f, offset, origin);
}
//
// InBuffer::read
//
// Read 'size' amount of bytes from the file. Reads are done from the physical
// medium in chunks of the buffer's length.
//
size_t InBuffer::read(void *dest, size_t size)
{
return fread(dest, 1, size, f);
}
//
// InBuffer::skip
//
// Skip a given number of bytes forward.
//
int InBuffer::skip(size_t skipAmt)
{
return fseek(f, skipAmt, SEEK_CUR);
}
//
// InBuffer::readUint32
//
// Read a uint32 value from the input file.
//
bool InBuffer::readUint32(uint32_t &num)
{
uint32_t lNum;
if(read(&lNum, sizeof(lNum)) != sizeof(lNum))
return false;
SwapULong(lNum);
num = lNum;
return true;
}
//
// InBuffer::readSint32
//
// Read an int32 value from the input file.
//
bool InBuffer::readSint32(int32_t &num)
{
int32_t lNum;
if(read(&lNum, sizeof(lNum)) != sizeof(lNum))
return false;
SwapLong(lNum);
num = lNum;
return true;
}
//
// InBuffer::readUint16
//
// Read a uint16 value from the input file.
//
bool InBuffer::readUint16(uint16_t &num)
{
uint16_t lNum;
if(read(&lNum, sizeof(lNum)) != sizeof(lNum))
return false;
SwapUShort(lNum);
num = lNum;
return true;
}
//
// InBuffer::readSint16
//
// Read an int16 value from the input file.
//
bool InBuffer::readSint16(int16_t &num)
{
int16_t lNum;
if(read(&lNum, sizeof(lNum)) != sizeof(lNum))
return false;
SwapShort(lNum);
num = lNum;
return true;
}
//
// InBuffer::readUint8
//
// Read a uint8 value from input file.
//
bool InBuffer::readUint8(uint8_t &num)
{
if(read(&num, sizeof(num)) != sizeof(num))
return false;
return true;
}
//
// InBuffer::readSint8
//
// Read an int8 value from input file.
//
bool InBuffer::readSint8(int8_t &num)
{
if(read(&num, sizeof(num)) != sizeof(num))
return false;
return true;
}
// EOF