blob: f553bbe7754f34db93a1cdfe657cef63b8a4de15 (
plain) (
blame)
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
|
#include "File.h"
/* Helper functions */
size_t fileSize(std::ifstream& file) {
int currIndex = file.tellg();
file.seekg(0, std::ios::end);
size_t size = file.tellg();
file.seekg(currIndex, std::ios::beg);
return size;
}
/* Private File functions */
void File::resizeBuffer() {
size_t newLen = (length == 0) ? 8 : length << 1;
Bytex* temp = new Bytex[newLen];
for (int i = 0; i < length; i++)
temp[i] = fileBuffer[i];
if (length != 0) delete[] fileBuffer;
fileBuffer = temp;
length = newLen;
}
char File::bytexToReadableChar(Bytex bytex) const {
char b = bytex.getByte();
if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z'))
return bytex.getByte();
return '.';
}
/* Public File functions */
File::File(const char* filePath) {
std::ifstream inFile(filePath);
if (!inFile.is_open()) {
std::cout << "Error opening file" << std::endl;
return;
}
size_t size = fileSize(inFile);
length = count = 0;
for (int i = 0; i < size; i++)
add(inFile.get());
inFile.close();
size_t pathLen = strlen(filePath) + 1;
originalFilePath = new char[pathLen];
for (int i = 0; i < pathLen; i++)
originalFilePath[i] = filePath[i];
}
File::~File() {
delete[] fileBuffer;
delete[] originalFilePath;
}
void File::viewFile() const {
char buffer[2];
for (size_t i = 0; i < count; i++) {
fileBuffer[i].getHex(buffer);
std::cout << buffer << " ";
}
std::cout << std::endl;
for (size_t i = 0; i < count; i++)
std::cout << bytexToReadableChar(fileBuffer[i]) << " ";
std::cout << std::endl;
}
void File::change(int index, Bytex bytex) {
fileBuffer[index] = bytex;
}
void File::add(Bytex bytex) {
if (count == length)
resizeBuffer();
fileBuffer[count++] = bytex;
}
void File::remove() {
fileBuffer[count--] = '\0';
}
void File::save() const {
saveAs(originalFilePath);
}
void File::saveAs(const char* filePath) const {
std::ofstream outFile(filePath);
for (int i = 0; i < count; i++)
outFile.put(fileBuffer[i].getByte());
outFile.close();
}
|