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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
#ifndef _INCLUDED_MEDIA_HPP_
#define _INCLUDED_MEDIA_HPP_
#include <stdio.h>
#include <limits.h>
#include <string.h>
// type names that do not conflcit with anything else
typedef signed char S8;
typedef unsigned char U8;
typedef signed short int S16;
typedef unsigned short int U16;
typedef signed int S32;
typedef unsigned int U32;
class MediaMedium
{
protected:
// standard constructor
MediaMedium() :
m_fError(0),
m_nDefBufSize(1024),
m_pWriteBuffer(NULL),
m_pReadBuffer(NULL),
m_nReadBufPos(0),
m_nWriteBufPos(0),
m_nBufSize(0),
m_nBufLenUsed(0),
m_nRefCnt(1)
{}
virtual ~MediaMedium() {}
public:
// allow reference counting
unsigned AddRef() { return ++m_nRefCnt; }
unsigned Release() { if (0==(--m_nRefCnt)) { delete this; return 0;} else return m_nRefCnt; }
enum
{
// error code flags
MME_VEOFMET = 0x00000001 // virtual end of file met
,MME_EOFMET = 0x00000002 // actual end of file met
,MME_OPENFAIL = 0x00000004 // failed to open medium
,MME_CLOSEFAIL = 0x00000008 // failed to close medium
,MME_UNAVAIL = 0x00000010 // requested operation was not available
,MME_IOERROR = 0x00000020 // read/write operation failed
};
unsigned m_fError;
unsigned m_nDefBufSize; // default read or write buffer sizes for buffering small objects
// flush read/write buffers. You should call this function after the last read or write operation on the object
// alternatively, derived (implementation) classes close methods should call this
void Flush()
{
if (m_pReadBuffer)
{
unsigned nBufStartPos = DoGetPos();
CloseReadBuffer(m_nReadBufPos > m_nBufLenUsed ? m_nReadBufPos : m_nBufLenUsed);
DoSetPos(nBufStartPos + m_nReadBufPos);
m_pReadBuffer = NULL;
m_nReadBufPos = 0;
}
else if (m_pWriteBuffer)
{
unsigned nBufStartPos = DoGetPos();
CloseWriteBuffer(m_nWriteBufPos > m_nBufLenUsed ? m_nWriteBufPos : m_nBufLenUsed);
DoSetPos(nBufStartPos + m_nWriteBufPos);
m_pWriteBuffer = NULL;
m_nWriteBufPos = 0;
}
m_nBufSize = 0;
m_nBufLenUsed = 0;
}
// use this to write a block of raw data
void WriteBlock(void const * pData, unsigned nSize)
{
Flush();
DoWriteBlock(pData,nSize);
}
// this may be faster, but will only work if the block size no more than the default buffer size
void WriteBufferedBlock(void const * pData, unsigned nSize)
{
if (m_nWriteBufPos + nSize <= m_nBufSize)
{
memcpy(static_cast<char *>(m_pWriteBuffer) + m_nWriteBufPos/sizeof(char), pData, nSize);
m_nWriteBufPos += nSize;
}
else
{
Flush();
m_pWriteBuffer = GetWriteBuffer(&m_nBufSize,m_nDefBufSize);
if (nSize <= m_nBufSize)
{
memcpy(m_pWriteBuffer, pData, nSize);
m_nWriteBufPos = nSize;
}
else
{
m_fError |= MME_VEOFMET;
}
}
}
// use this to read a block of raw data
void ReadBlock(void * pData, unsigned nSize)
{
Flush();
DoReadBlock(pData,nSize);
}
// this may be faster, but will only work if the block size no more than the default buffer size
void ReadBufferedBlock(void * pData, unsigned nSize)
{
if (m_nReadBufPos + nSize <= m_nBufSize)
{
memcpy(pData, static_cast<char const *>(m_pReadBuffer) + m_nReadBufPos/sizeof(char), nSize);
m_nReadBufPos += nSize;
}
else
{
Flush();
m_pReadBuffer = GetReadBuffer(&m_nBufSize,m_nDefBufSize);
if (nSize <= m_nBufSize)
{
memcpy(pData, m_pReadBuffer, nSize);
m_nReadBufPos = nSize;
}
else
{
m_fError |= MME_VEOFMET;
}
}
}
// move the 'file' pointer nOffset bytes
// this will not necessarily cause buffers to be flushed
// if the pointer can be moved within the current buffer,
// some of the buffer may be left uninitialized, and no
// error will occur, which otherwise might (particularly
// if the object has write access)
void MovePos(signed nOffset)
{
if (m_pReadBuffer)
{
if (nOffset>0 && m_nReadBufPos+nOffset<=m_nBufSize)
{
m_nReadBufPos+=nOffset;
return;
}
else if (nOffset<=0 && m_nReadBufPos>=static_cast<unsigned>(-nOffset))
{
if (m_nBufLenUsed < m_nReadBufPos) m_nBufLenUsed = m_nReadBufPos;
m_nReadBufPos+=nOffset;
return;
}
}
else if (m_pWriteBuffer)
{
if (nOffset>0 && m_nWriteBufPos+nOffset<=m_nBufSize)
{
m_nWriteBufPos+=nOffset;
return;
}
else if (nOffset<=0 && m_nWriteBufPos>=static_cast<unsigned>(-nOffset))
{
if (m_nBufLenUsed < m_nWriteBufPos) m_nBufLenUsed = m_nWriteBufPos;
m_nWriteBufPos+=nOffset;
return;
}
}
// else
Flush();
DoSetPos(DoGetPos()+nOffset);
}
// set the 'file' pointer
// you would normally only pass values which have been
// previously returned by a call to GetPos
// note that this will not necessarily cause buffers to be flushed
// if the pointer can be moved within the current buffer,
// some of the buffer may be left uninitialized, and no
// error will occur, which otherwise might (particularly
// if the object has write access)
void SetPos(unsigned nPos)
{
unsigned nNewBufPos = nPos - DoGetPos();
if (nNewBufPos <= m_nBufSize)
{
if (m_pReadBuffer)
{
if (m_nBufLenUsed < m_nReadBufPos) m_nBufLenUsed = m_nReadBufPos;
m_nReadBufPos = nNewBufPos;
}
else // pWriteBuffer
{
if (m_nBufLenUsed < m_nWriteBufPos) m_nBufLenUsed = m_nWriteBufPos;
m_nWriteBufPos = nNewBufPos;
}
}
else
{
Flush();
DoSetPos(nPos);
}
}
// get the 'file' pointer. The returned value
// can be used in a call to SetPos
unsigned GetPos()
{
return DoGetPos()+m_nReadBufPos+m_nWriteBufPos;
}
virtual unsigned GetRemainingSize();
private:
void * m_pWriteBuffer;
void const * m_pReadBuffer;
unsigned m_nReadBufPos;
unsigned m_nWriteBufPos;
unsigned m_nBufSize;
unsigned m_nBufLenUsed;
unsigned m_nRefCnt;
protected:
// the non-pure functions default implementation sets the unavailable error flag
// it is safe to assume that these four functions will be called in a logical order
// and that only one buffer (read or write) will be required at once
// this two functions may return NULL only if *pSize is set to zero
// *pSize should otherwise be set to the actual size of the buffer returned
// get a pointer to memory where data can be written to directly
virtual void * GetWriteBuffer(unsigned * pSize, unsigned nDesiredSize);
// get a pointer to memory where data can be read from directly
virtual void const * GetReadBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer 'allocated' above and assume nPosOffset bytes were transferred
// and that the 'file' pointer should be positioned at the end of the transferred data
virtual void CloseWriteBuffer(unsigned nPosOffset);
virtual void CloseReadBuffer(unsigned nPosOffset);
// transfer a block of data
// it is safe to assume that no buffer will be open
virtual void DoWriteBlock(void const * pData, unsigned nSize);
virtual void DoReadBlock(void * pData, unsigned nSize);
// if a buffer is open, should return pos at start of buffer
virtual unsigned DoGetPos() = 0;
// it is safe to assume that no buffer will be open
virtual void DoSetPos(unsigned nPos) = 0;
friend class MediaSection;
friend void MediaRead(MediaMedium * pThis, S8 * p);
friend void MediaRead(MediaMedium * pThis, U8 * p);
friend void MediaRead(MediaMedium * pThis, S16 * p);
friend void MediaRead(MediaMedium * pThis, U16 * p);
friend void MediaRead(MediaMedium * pThis, S32 * p);
friend void MediaRead(MediaMedium * pThis, U32 * p);
};
// use this to read in simple data types
// note especially that if the size of TYPE is greater than the
// default buffer size, then the operation will fail
// and the virtual end of file error flag will be set
// - use ReadBlock instead
inline void MediaRead(MediaMedium * pThis, S8 * p)
{
if (pThis->m_nReadBufPos + sizeof(S8) <= pThis->m_nBufSize)
{
*p = static_cast<S8 const *>(pThis->m_pReadBuffer)[pThis->m_nReadBufPos];
pThis->m_nReadBufPos += sizeof(S8);
}
else
{
pThis->Flush();
pThis->m_pReadBuffer = pThis->GetReadBuffer(&pThis->m_nBufSize,pThis->m_nDefBufSize);
if (sizeof(S8) <= pThis->m_nBufSize)
{
*p = *static_cast<S8 const *>(pThis->m_pReadBuffer);
pThis->m_nReadBufPos = sizeof(S8);
}
else
{
pThis->m_fError |= MediaMedium::MME_VEOFMET;
}
}
}
inline void MediaRead(MediaMedium * pThis, U8 * p)
{
if (pThis->m_nReadBufPos + sizeof(U8) <= pThis->m_nBufSize)
{
*p = static_cast<U8 const *>(pThis->m_pReadBuffer)[pThis->m_nReadBufPos];
pThis->m_nReadBufPos += sizeof(U8);
}
else
{
pThis->Flush();
pThis->m_pReadBuffer = pThis->GetReadBuffer(&pThis->m_nBufSize,pThis->m_nDefBufSize);
if (sizeof(U8) <= pThis->m_nBufSize)
{
*p = *static_cast<U8 const *>(pThis->m_pReadBuffer);
pThis->m_nReadBufPos = sizeof(U8);
}
else
{
pThis->m_fError |= MediaMedium::MME_VEOFMET;
}
}
}
inline void MediaRead(MediaMedium * pThis, S16 * p)
{
S8 b0, b1;
::MediaRead(pThis, &b0);
::MediaRead(pThis, &b1);
*p = (b0 << 0) | (b1 << 8);
}
inline void MediaRead(MediaMedium * pThis, U16 * p)
{
U8 b0, b1;
::MediaRead(pThis, &b0);
::MediaRead(pThis, &b1);
*p = (b0 << 0) | (b1 << 8);
}
inline void MediaRead(MediaMedium * pThis, S32 * p)
{
S8 b0, b1, b2, b3;
::MediaRead(pThis, &b0);
::MediaRead(pThis, &b1);
::MediaRead(pThis, &b2);
::MediaRead(pThis, &b3);
*p = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
}
inline void MediaRead(MediaMedium * pThis, U32 * p)
{
U8 b0, b1, b2, b3;
::MediaRead(pThis, &b0);
::MediaRead(pThis, &b1);
::MediaRead(pThis, &b2);
::MediaRead(pThis, &b3);
*p = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
}
#ifdef _MEDIA_WIN_TARGET
class MediaWinFileMedium : public MediaMedium
{
public:
MediaWinFileMedium() : m_hFile(INVALID_HANDLE_VALUE), m_nReadBufLen(0) {}
void Attach(HANDLE hFile)
{
m_hFile = hFile;
}
void Detach()
{
Flush();
m_hFile = INVALID_HANDLE_VALUE;
}
void Open(char *pszFileName, DWORD dwDesiredAccess)
{
DWORD dwShareMode;
DWORD dwCreationDistribution;
switch (dwDesiredAccess & (GENERIC_READ|GENERIC_WRITE))
{
case 0:
dwShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
dwCreationDistribution = OPEN_EXISTING;
break;
case GENERIC_READ:
dwShareMode = FILE_SHARE_READ;
dwCreationDistribution = OPEN_EXISTING;
break;
case GENERIC_WRITE:
dwShareMode = 0;
dwCreationDistribution = CREATE_ALWAYS;
break;
default: // GENERIC_WRITE|GENERIC_READ
dwCreationDistribution = OPEN_ALWAYS;
dwShareMode = 0;
}
m_hFile = CreateFile
(
pszFileName,
dwDesiredAccess,
dwShareMode,
0,
dwCreationDistribution,
FILE_ATTRIBUTE_NORMAL,
0
);
if (INVALID_HANDLE_VALUE == m_hFile)
m_fError |= MME_OPENFAIL;
}
void Close()
{
if (INVALID_HANDLE_VALUE == m_hFile)
m_fError |= MME_CLOSEFAIL;
else
{
Flush();
if (!CloseHandle(m_hFile))
m_fError |= MME_CLOSEFAIL;
else
m_hFile = INVALID_HANDLE_VALUE;
}
}
~MediaWinFileMedium()
{
// should already be closed...
Close();
}
virtual unsigned GetRemainingSize();
private:
HANDLE m_hFile;
char * m_pBuffer;
unsigned m_nReadBufLen;
protected:
// get a pointer to memory where data can be written to directly
virtual void * GetWriteBuffer(unsigned * pSize, unsigned nDesiredSize);
// get a pointer to memory where data can be read from directly
virtual void const * GetReadBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer allocated above and assume nPosOffset were transferred
virtual void CloseWriteBuffer(unsigned nPosOffset);
virtual void CloseReadBuffer(unsigned nPosOffset);
// transfer a block of data: the buffer should be closed
virtual void DoWriteBlock(void const * pData, unsigned nSize);
virtual void DoReadBlock(void * pData, unsigned nSize);
// if a buffer is open, should return pos at start of buffer
virtual unsigned DoGetPos();
// requires that no buffer is oben
virtual void DoSetPos(unsigned nPos);
};
#endif // _MEDIA_WIN_TARGET
class MediaStdFileMedium : public MediaMedium
{
public:
MediaStdFileMedium() : m_pFile(NULL), m_nReadBufLen(0) {}
void Attach(FILE * pFile)
{
m_pFile = pFile;
}
void Detach()
{
Flush();
m_pFile = NULL;
}
void Open(char const * pszFileName, char const * pszOpenMode)
{
if (pszOpenMode[0] != 'r' || pszOpenMode[1] != 'b') {
fprintf(stderr, "Open(%s, %s)\n", pszFileName, pszOpenMode);
m_fError |= MME_OPENFAIL;
return;
}
m_pFile = OpenGameFile(pszFileName, FILEMODE_READONLY, FILETYPE_PERM);
if (!m_pFile)
m_fError |= MME_OPENFAIL;
}
void Close()
{
if (!m_pFile)
m_fError |= MME_CLOSEFAIL;
else
{
Flush();
if (fclose(m_pFile))
m_fError |= MME_CLOSEFAIL;
else
m_pFile = NULL;
}
}
~MediaStdFileMedium()
{
// should already be closed...
Close();
}
virtual unsigned GetRemainingSize();
private:
FILE * m_pFile;
char * m_pBuffer;
unsigned m_nReadBufLen;
protected:
// get a pointer to memory where data can be written to directly
virtual void * GetWriteBuffer(unsigned * pSize, unsigned nDesiredSize);
// get a pointer to memory where data can be read from directly
virtual void const * GetReadBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer allocated above and assume nPosOffset were transferred
virtual void CloseWriteBuffer(unsigned nPosOffset);
virtual void CloseReadBuffer(unsigned nPosOffset);
// transfer a block of data: the buffer should be closed
virtual void DoWriteBlock(void const * pData, unsigned nSize);
virtual void DoReadBlock(void * pData, unsigned nSize);
// if a buffer is open, should return pos at start of buffer
virtual unsigned DoGetPos();
// requires that no buffer is oben
virtual void DoSetPos(unsigned nPos);
};
class MediaMemoryReadMedium : public MediaMedium
{
public:
MediaMemoryReadMedium() : m_pMem(NULL) {}
void Open(void const * p)
{
m_pMem = p;
m_nOffset = 0;
}
void Close()
{
if (m_pMem)
{
Flush();
m_pMem = NULL;
}
else
m_fError |= MME_CLOSEFAIL;
}
private:
void const * m_pMem;
protected:
unsigned m_nOffset;
// get a pointer to memory where data can be read from directly
virtual void const * GetReadBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer allocated above and assume nPosOffset were transferred
virtual void CloseReadBuffer(unsigned nPosOffset);
// transfer a block of data: the buffer should be closed
virtual void DoReadBlock(void * pData, unsigned nSize);
// if a buffer is open, should return pos at start of buffer
virtual unsigned DoGetPos();
// requires that no buffer is oben
virtual void DoSetPos(unsigned nPos);
};
class MediaMemoryMedium : public MediaMemoryReadMedium
{
public:
MediaMemoryMedium() : m_pMem(NULL) {}
void Open(void * p)
{
m_pMem = p;
MediaMemoryReadMedium::Open(p);
}
void Close()
{
MediaMemoryReadMedium::Close();
m_pMem = NULL;
}
private:
void * m_pMem;
protected:
// get a pointer to memory where data can be written to directly
virtual void * GetWriteBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer allocated above and assume nPosOffset were transferred
virtual void CloseWriteBuffer(unsigned nPosOffset);
// transfer a block of data: the buffer should be closed
virtual void DoWriteBlock(void const * pData, unsigned nSize);
};
class MediaSection : public MediaMedium
{
public:
MediaSection() : m_pMedium(NULL) {}
void Open(MediaMedium * pMedium, unsigned nMaxSize = UINT_MAX)
{
m_pMedium = pMedium;
m_nMaxSize = nMaxSize;
m_nPos = 0;
m_nUsedPos = 0;
}
void Close()
{
if (m_pMedium)
Flush();
if (m_nPos > m_nUsedPos) m_nUsedPos = m_nPos;
m_pMedium = NULL;
}
unsigned GetUsedSize() const
{
return (m_nPos > m_nUsedPos) ? m_nPos : m_nUsedPos;
}
virtual unsigned GetRemainingSize();
private:
MediaMedium * m_pMedium;
unsigned m_nMaxSize;
unsigned m_nPos;
unsigned m_nUsedPos;
protected:
// get a pointer to memory where data can be written to directly
virtual void * GetWriteBuffer(unsigned * pSize, unsigned nDesiredSize);
// get a pointer to memory where data can be read from directly
virtual void const * GetReadBuffer(unsigned * pSize, unsigned nDesiredSize);
// close the buffer allocated above and assume nPosOffset were transferred
virtual void CloseWriteBuffer(unsigned nPosOffset);
virtual void CloseReadBuffer(unsigned nPosOffset);
// transfer a block of data: the buffer should be closed
virtual void DoWriteBlock(void const * pData, unsigned nSize);
virtual void DoReadBlock(void * pData, unsigned nSize);
// if a buffer is open, should return pos at start of buffer
virtual unsigned DoGetPos();
// requires that no buffer is oben
virtual void DoSetPos(unsigned nPos);
};
#endif
|