Skip to content

Commit 66e9092

Browse files
authored
Base commit
1 parent 6583576 commit 66e9092

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

BinaryFileReader.h

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#pragma once
2+
/*#include <cstdio>
3+
failed c attempt
4+
5+
class FILE_LOADER {
6+
FILE* filx;
7+
bool loaded = false;
8+
errno_t lasterror = 0;
9+
10+
public:
11+
FILE_LOADER() { }
12+
FILE_LOADER(char* fname) { open(fname); }
13+
~FILE_LOADER() { close(); }
14+
15+
void open(const char* fname) {
16+
errno_t er = fopen_s(&filx, fname, "rb"); //open file in readonly binary
17+
if (er == NULL) {
18+
loaded = true;
19+
} else {
20+
lasterror = er;
21+
perror("Couldn't open file");
22+
loaded = false;
23+
}
24+
}
25+
26+
void close() {
27+
errno_t er = fclose(filx);
28+
if (er == NULL)
29+
loaded = false;
30+
else {
31+
lasterror = er;
32+
perror("Couldn't close file");
33+
}
34+
}
35+
36+
unsigned char BinaryRead() {
37+
unsigned char buffer[1]{};
38+
errno_t er = fread(buffer, (size_t)1, (size_t)1, filx);
39+
if (er) {
40+
printf("Error Reading file.\n");
41+
return '\0';
42+
}
43+
return *buffer;
44+
}
45+
void Read(char* ret, size_t size) {
46+
if (isLoaded())
47+
fread(ret, size, 1, filx);
48+
}
49+
bool isLoaded() { return loaded; }
50+
};
51+
*/
52+
53+
#include <fstream>
54+
55+
class BinaryFileReader {
56+
std::ifstream filx;
57+
bool loaded = false;
58+
59+
public:
60+
BinaryFileReader() {}
61+
BinaryFileReader(char* fname) {
62+
open(fname);
63+
}
64+
~BinaryFileReader() { close(); }
65+
66+
void open(char* fname) {
67+
filx.open(fname, std::ios::in | std::ios::binary);
68+
if (filx.is_open())
69+
loaded = true;
70+
else
71+
printf("Error trying to open file.\n");
72+
}
73+
74+
void close() { filx.close(); }
75+
76+
unsigned char ReadByte() {
77+
if (!loaded && filx.good()) {
78+
printf("File is not open, can't read.");
79+
return '\0';
80+
}
81+
unsigned char buffer;
82+
//filx.read((char*)&buffer, sizeof(buffer));
83+
filx.read((char*)&buffer, 1);
84+
return buffer;
85+
}
86+
87+
unsigned char* ReadBytes(size_t s) {
88+
unsigned char* b = new unsigned char[s];
89+
for (int a = 0; a < s; a++)
90+
b[a] = ReadByte();
91+
return b;
92+
}
93+
94+
//Pray to crap that this doesn't cause an infinite loop
95+
std::string ReadString() {
96+
std::string a = "";
97+
while (true) {
98+
unsigned char bt = ReadByte();
99+
if (bt == 0)
100+
break;
101+
a += bt;
102+
}
103+
a += '\0'; //close string just in case.
104+
filx.seekg(-1, std::ios_base::cur); //move cursor back 1 byte because we are overreading. (doesn't work)
105+
return a;
106+
}
107+
108+
//Read bytes until hitting a 0x00. Maximum readability: 500 bytes.
109+
unsigned char* ReadBytesUntilBreak(size_t &return_size) {
110+
unsigned char* res = new unsigned char[500];
111+
int ct = 0;
112+
while (true) {
113+
unsigned char b = ReadByte();
114+
if (b == 0)
115+
break;
116+
res[ct] = b;
117+
ct++;
118+
}
119+
return_size = ct;
120+
filx.seekg(-1, std::ios_base::cur); //move cursor back 1 byte because we are overreading.
121+
return res;
122+
}
123+
124+
int32_t ReadInt32() {
125+
unsigned char* b = ReadBytes(4);
126+
int32_t a = 0;
127+
for (size_t i = 0; i < 4; ++i)
128+
a += b[i] << 8 * i;
129+
return a;
130+
}
131+
132+
int16_t ReadInt16() {
133+
unsigned char *b = ReadBytes(2);
134+
int16_t a = 0;
135+
for (size_t i = 0; i < 2; ++i)
136+
a += b[i] << 8 * i;
137+
return a;
138+
}
139+
140+
static int ConvertBytesToAnyInt(unsigned char* bytes, size_t s) {
141+
int a = 0;
142+
for (size_t i = 0; i < s; ++i)
143+
a += bytes[i] << 8 * i;
144+
return a;
145+
}
146+
147+
//A backup function if the other one doesn't work due to endian offsets.
148+
static int ConvertBytesToAnyInt2(unsigned char* bits, size_t s, bool little_endian = true) {
149+
int result = 0;
150+
if (little_endian)
151+
for (int n = s; n >= 0; n--)
152+
result = (result << 8) + bits[n];
153+
else
154+
for (size_t n = 0; n < s; n++)
155+
result = (result << 8) + bits[n];
156+
return result;
157+
}
158+
159+
static char* ConvertBytesToCharArray(unsigned char* bytes, size_t s) {
160+
char *res = new char[s];
161+
for (size_t i = 0; i < s; i++)
162+
res[i] = bytes[i];
163+
res[s - 1] = '\0';
164+
return res;
165+
}
166+
167+
static void DisplayBytesHex(unsigned char* bytes, size_t s) {
168+
for (size_t i = 0; i < s; i++)
169+
printf("%02x ", bytes[i]);
170+
printf("\n");
171+
}
172+
173+
//Checks if file is loaded successfully.
174+
bool isLoaded() { return loaded; }
175+
176+
//Checks End Of File.
177+
bool isEOF() { return filx.eof(); }
178+
179+
//Returns the size of the binary file.
180+
long long size() {
181+
//save cursor position
182+
std::streampos currpos = filx.tellg();
183+
long long s;
184+
//seek start to end
185+
filx.seekg(0, std::ios::end);
186+
//save size
187+
s = filx.tellg();
188+
//seek back to cursor pos
189+
filx.seekg(0, currpos);
190+
//return size
191+
return s;
192+
}
193+
};

0 commit comments

Comments
 (0)