-
Notifications
You must be signed in to change notification settings - Fork 21
/
BinReader.cpp
114 lines (100 loc) · 1.83 KB
/
BinReader.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
// 120112
#include "BinReader.h"
BinReader::BinReader(FileMapping & fm)
: m_FileMapping(fm), m_Pointer(0), m_bError(false)
{
#ifdef _DEBUG
m_ReadCount = 0;
#endif
}
#ifdef _DEBUG
#define DefReadFunc(_name, _type)\
_type BinReader::Read##_name()\
{\
if(!m_bError)\
{\
m_ReadCount++;\
size_t szType = sizeof(_type);\
size_t realSz = 0;\
void * pData = m_FileMapping.Read(m_Pointer, szType, &realSz);\
if(!pData || realSz != szType)\
{\
m_bError = true;\
return 0;\
}\
m_Pointer += szType;\
return *(_type*)pData;\
}\
return 0;\
}
#else
#define DefReadFunc(_name, _type)\
_type BinReader::Read##_name()\
{\
if(!m_bError)\
{\
size_t szType = sizeof(_type);\
size_t realSz = 0;\
void * pData = m_FileMapping.Read(m_Pointer, szType, &realSz);\
if(!pData || realSz != szType)\
{\
m_bError = true;\
return 0;\
}\
m_Pointer += szType;\
return *(_type*)pData;\
}\
return 0;\
}
#endif
DefReadFunc(Int8, char);
DefReadFunc(Int16, short);
DefReadFunc(Int32, int);
DefReadFunc(Int64, int64);
DefReadFunc(UInt8, unsigned char);
DefReadFunc(UInt16, unsigned short);
DefReadFunc(UInt32, unsigned int);
DefReadFunc(UInt64, uint64);
DefReadFunc(Single, float);
DefReadFunc(Double, double);
void BinReader::ReadWstring(std::wstring & str)
{
while(!IsEOF())
{
wchar_t ch = ReadUInt16();
if(!ch) break;
str.push_back(ch);
}
}
void BinReader::ReadAstring(std::string & str)
{
while(!IsEOF())
{
char ch = ReadInt8();
if(!ch) break;
str.push_back(ch);
}
}
bool BinReader::IsEOF() const
{
return m_bError;
}
uint64 BinReader::GetPosition() const
{
return m_Pointer;
}
void BinReader::SetPosition(uint64 ptr)
{
m_Pointer = ptr;
m_bError = false;
}
void BinReader::Reset()
{
m_FileMapping.Unmap();
m_Pointer = 0;
m_bError = false;
}
FileMapping & BinReader::GetFileMapping() const
{
return m_FileMapping;
}