-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteReader.cpp
130 lines (96 loc) · 2.1 KB
/
ByteReader.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
//All copyrights goes to JordanCpp, thnx for allowing me to use it - APAMK2
#include "ByteReader.hpp"
#define HTONS(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8))
#define HTONL(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
((((uint32_t)(n) & 0xFF00)) << 8) | \
((((uint32_t)(n) & 0xFF0000)) >> 8) | \
((((uint32_t)(n) & 0xFF000000)) >> 24))
bool ByteReader::Reset(const std::string & path, size_t type)
{
if (_File.is_open())
_File.close();
_File.open(path.c_str(), std::ios::in | std::ios::binary);
_Type = type;
if (_File.is_open())
{
_File.seekg(0, std::ios::end);
std::streampos length(_File.tellg());
_File.seekg(0, std::ios::beg);
_Bytes = (size_t)length;
}
return _File.is_open();
}
void ByteReader::Close()
{
_File.close();
}
ByteReader::~ByteReader()
{
Close();
}
size_t ByteReader::Bytes()
{
return _Bytes;
}
void ByteReader::Pos(size_t value)
{
_File.seekg(value, std::ios::beg);
}
void ByteReader::Read(void* buffer, size_t bytes)
{
_File.read((char*)buffer, bytes);
}
bool ByteReader::Eof()
{
return _File.good();
}
uint8_t ByteReader::u8()
{
uint8_t val;
_File.read((char*)&val, sizeof(uint8_t));
return val;
}
uint16_t ByteReader::u16()
{
uint16_t val;
_File.read((char*)&val, sizeof(uint16_t));
if (_Type == BigEndian)
return val;
else
return HTONS(val);
}
int16_t ByteReader::i16()
{
int16_t val;
_File.read((char*)&val, sizeof(int16_t));
if (_Type == BigEndian)
return val;
else
return HTONS(val);
}
uint32_t ByteReader::u32()
{
uint32_t val;
_File.read((char*)&val, sizeof(uint32_t));
if (_Type == BigEndian)
return val;
else
return HTONL(val);
}
int32_t ByteReader::i32()
{
int32_t val;
_File.read((char*)&val, sizeof(int32_t));
if (_Type == BigEndian)
return val;
else
return HTONL(val);
}
std::string ByteReader::string(int len)
{
char* buffer = new char[len];
_File.read(buffer, len);
std::string val(buffer);
delete[] buffer;
return val;
}