-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryFileReader.h
More file actions
193 lines (170 loc) · 4.42 KB
/
BinaryFileReader.h
File metadata and controls
193 lines (170 loc) · 4.42 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
#pragma once
/*#include <cstdio>
failed c attempt
class FILE_LOADER {
FILE* filx;
bool loaded = false;
errno_t lasterror = 0;
public:
FILE_LOADER() { }
FILE_LOADER(char* fname) { open(fname); }
~FILE_LOADER() { close(); }
void open(const char* fname) {
errno_t er = fopen_s(&filx, fname, "rb"); //open file in readonly binary
if (er == NULL) {
loaded = true;
} else {
lasterror = er;
perror("Couldn't open file");
loaded = false;
}
}
void close() {
errno_t er = fclose(filx);
if (er == NULL)
loaded = false;
else {
lasterror = er;
perror("Couldn't close file");
}
}
unsigned char BinaryRead() {
unsigned char buffer[1]{};
errno_t er = fread(buffer, (size_t)1, (size_t)1, filx);
if (er) {
printf("Error Reading file.\n");
return '\0';
}
return *buffer;
}
void Read(char* ret, size_t size) {
if (isLoaded())
fread(ret, size, 1, filx);
}
bool isLoaded() { return loaded; }
};
*/
#include <fstream>
class BinaryFileReader {
std::ifstream filx;
bool loaded = false;
public:
BinaryFileReader() {}
BinaryFileReader(char* fname) {
open(fname);
}
~BinaryFileReader() { close(); }
void open(char* fname) {
filx.open(fname, std::ios::in | std::ios::binary);
if (filx.is_open())
loaded = true;
else
printf("Error trying to open file.\n");
}
void close() { filx.close(); }
unsigned char ReadByte() {
if (!loaded && filx.good()) {
printf("File is not open, can't read.");
return '\0';
}
unsigned char buffer;
//filx.read((char*)&buffer, sizeof(buffer));
filx.read((char*)&buffer, 1);
return buffer;
}
unsigned char* ReadBytes(size_t s) {
unsigned char* b = new unsigned char[s];
for (int a = 0; a < s; a++)
b[a] = ReadByte();
return b;
}
//Pray to crap that this doesn't cause an infinite loop
std::string ReadString() {
std::string a = "";
while (true) {
unsigned char bt = ReadByte();
if (bt == 0)
break;
a += bt;
}
a += '\0'; //close string just in case.
filx.seekg(-1, std::ios_base::cur); //move cursor back 1 byte because we are overreading. (doesn't work)
return a;
}
//Read bytes until hitting a 0x00. Maximum readability: 500 bytes.
unsigned char* ReadBytesUntilBreak(size_t &return_size) {
unsigned char* res = new unsigned char[500];
int ct = 0;
while (true) {
unsigned char b = ReadByte();
if (b == 0)
break;
res[ct] = b;
ct++;
}
return_size = ct;
filx.seekg(-1, std::ios_base::cur); //move cursor back 1 byte because we are overreading.
return res;
}
int32_t ReadInt32() {
unsigned char* b = ReadBytes(4);
int32_t a = 0;
for (size_t i = 0; i < 4; ++i)
a += b[i] << 8 * i;
return a;
}
int16_t ReadInt16() {
unsigned char *b = ReadBytes(2);
int16_t a = 0;
for (size_t i = 0; i < 2; ++i)
a += b[i] << 8 * i;
return a;
}
static int ConvertBytesToAnyInt(unsigned char* bytes, size_t s) {
int a = 0;
for (size_t i = 0; i < s; ++i)
a += bytes[i] << 8 * i;
return a;
}
//A backup function if the other one doesn't work due to endian offsets.
static int ConvertBytesToAnyInt2(unsigned char* bits, size_t s, bool little_endian = true) {
int result = 0;
if (little_endian)
for (int n = s; n >= 0; n--)
result = (result << 8) + bits[n];
else
for (size_t n = 0; n < s; n++)
result = (result << 8) + bits[n];
return result;
}
static char* ConvertBytesToCharArray(unsigned char* bytes, size_t s) {
char *res = new char[s];
for (size_t i = 0; i < s; i++)
res[i] = bytes[i];
res[s - 1] = '\0';
return res;
}
static void DisplayBytesHex(unsigned char* bytes, size_t s) {
for (size_t i = 0; i < s; i++)
printf("%02x ", bytes[i]);
printf("\n");
}
//Checks if file is loaded successfully.
bool isLoaded() { return loaded; }
//Checks End Of File.
bool isEOF() { return filx.eof(); }
//Returns the size of the binary file.
long long size() {
//save cursor position
std::streampos currpos = filx.tellg();
long long s;
//seek start to end
filx.seekg(0, std::ios::end);
//save size
s = filx.tellg();
//seek back to cursor pos
filx.seekg(0, currpos);
//return size
return s;
}
};