-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_bytes.cpp
More file actions
72 lines (58 loc) · 1.3 KB
/
file_bytes.cpp
File metadata and controls
72 lines (58 loc) · 1.3 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
#include "./file_bytes.hpp"
File_bytes::File_bytes(std::string f_name) {
std::ifstream f;
f.open(f_name, std::ios::binary | std::ios::ate);
if (!f.is_open()) {
throw std::runtime_error("Unable to open file");
}
// get size of file
file_size = f.tellg();
f.seekg(std::fstream::beg);
head = new u_int8_t[file_size];
for (int i = 0; i < file_size; i++) {
head[i] = f.get();
}
f.close();
pos = head;
}
File_bytes::~File_bytes() {
if(head) {
delete[] head;
}
}
void File_bytes::check_range() {
if((head + file_size - 1) < pos) {
throw std::runtime_error("File_bytes: memory access past EOF");
}
if(head > pos) {
throw std::runtime_error("File_bytes: memory access before start of file");
}
}
u_int8_t File_bytes::operator[](int i) {
if((i + pos) > (head + file_size - 1)) {
throw std::runtime_error("File_bytes: memory access past EOF");
}
if(head > (i + pos)) {
throw std::runtime_error("File_bytes: memory access before start of file");
}
return pos[i];
}
File_bytes& File_bytes::operator++(int) {
pos++;
return *this;
}
File_bytes& File_bytes::operator--(int) {
pos--;
return *this;
}
File_bytes& File_bytes::operator+=(int x) {
pos += x;
return *this;
}
File_bytes& File_bytes::operator-=(int x) {
pos -= x;
return *this;
}
u_int8_t File_bytes::operator*() {
return *pos;
}