-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryReader.cpp
126 lines (104 loc) · 1.99 KB
/
binaryReader.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
#include "binaryReader.h"
binaryReader::binaryReader(std::string fileName)
{
m_File = std::ifstream(fileName, std::ios::binary);
m_File.seekg(0, std::ios::end);
m_nSize = m_File.tellg();
m_File.seekg(0, std::ios::beg);
m_pBuffer = new char[m_nSize];
m_File.read((char*)m_pBuffer, m_nSize);
m_nPos = 0;
}
bool binaryReader::readBool()
{
char buffer [1];
m_File.seekg(m_nPos);
m_File.read(buffer, 1);
m_nPos += 1;
bool value;
std::memcpy(&value, buffer, 1);
return value;
}
int binaryReader::readInt()
{
char buffer[4];
m_File.seekg(m_nPos);
m_File.read(buffer, 4);
m_nPos += 4;
int value;
value = (int &)buffer;
return value;
}
uint8_t binaryReader::readUInt()
{
char buffer[4];
m_File.seekg(m_nPos);
m_File.read(buffer, 4);
m_nPos += 4;
uint8_t value;
std::memcpy(&value, buffer, 4);
return value;
}
float binaryReader::readFloat()
{
char buffer[4];
m_File.seekg(m_nPos);
m_File.read(buffer, 4);
m_nPos += 4;
float value;
std::memcpy(&value, buffer, 4);
return value;
}
char* binaryReader::readString()
{
readByte();
char* str = new char[1];
int n = 0;
char* c = readByte();
while (c[0] != '\x00')
{
str[n] = c[0];
n++;
str = (char*)realloc(str, n + 1);
c = readByte();
}
str[n] = '\0';
return str;
}
char* binaryReader::readByte() {
char buffer[1];
//m_File.seekg(m_nPos);
m_File.read(buffer, 1);
m_nPos++;
return buffer;
}
std::vector<uint8_t> binaryReader::readBytes(size_t numBytes) {
std::vector<uint8_t> buffer(numBytes);
for (size_t i = 0; i < numBytes; i++) {
m_File.seekg(m_nPos);
buffer[i] = static_cast<uint8_t>(m_File.get());
m_nPos++;
}
return buffer;
};
int binaryReader::getPos()
{
return m_nPos;
}
int binaryReader::getSize()
{
return m_nSize;
}
Vector3 binaryReader::readVector3()
{
return Vector3(readFloat(),readFloat(),readFloat());
}
Vector4 binaryReader::readVector4()
{
return Vector4(readFloat(), readFloat(), readFloat(), readFloat());
}
void binaryReader::setPos(int nPos)
{
m_nPos = nPos;
}
// Path: binaryReader.h