-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourceCode.cpp
246 lines (222 loc) · 6.64 KB
/
sourceCode.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "sourceCode.hpp"
void huffman::createArr() {
for (int i = 0; i < 128; i++) {
arr.push_back(new Node());
arr[i]->data = i;
arr[i]->freq = 0;
}
}
void huffman::traverse(Node* r, string str) {
if (r->left == NULL && r->right == NULL) {
r->code = str;
return;
}
traverse(r->left, str + '0');
traverse(r->right, str + '1');
}
int huffman::binToDec(string inStr) {
int res = 0;
for (auto c : inStr) {
res = res * 2 + c - '0';
}
return res;
}
string huffman::decToBin(int inNum) {
string temp = "", res = "";
while (inNum > 0) {
temp += (inNum % 2 + '0');
inNum /= 2;
}
res.append(8 - temp.length(), '0');
for (int i = temp.length() - 1; i >= 0; i--) {
res += temp[i];
}
return res;
}
void huffman::buildTree(char a_code, string& path) {
Node* curr = root;
for (int i = 0; i < path.length(); i++) {
if (path[i] == '0') {
if (curr->left == NULL) {
curr->left = new Node();
}
curr = curr->left;
}
else if (path[i] == '1') {
if (curr->right == NULL) {
curr->right = new Node();
}
curr = curr->right;
}
}
curr->data = a_code;
}
void huffman::createMinHeap() {
char id;
inFile.open(inFileName, ios::in);
inFile.get(id);
//Incrementing frequency of characters that appear in the input file
while (!inFile.eof()) {
arr[id]->freq++;
inFile.get(id);
}
inFile.close();
//Pushing the Nodes which appear in the file into the priority queue (Min Heap)
for (int i = 0; i < 128; i++) {
if (arr[i]->freq > 0) {
minHeap.push(arr[i]);
}
}
}
void huffman::createTree() {
//Creating Huffman Tree with the Min Heap created earlier
Node *left, *right;
priority_queue <Node*, vector<Node*>, Compare> tempPQ(minHeap);
while (tempPQ.size() != 1)
{
left = tempPQ.top();
tempPQ.pop();
right = tempPQ.top();
tempPQ.pop();
root = new Node();
root->freq = left->freq + right->freq;
root->left = left;
root->right = right;
tempPQ.push(root);
}
}
void huffman::createCodes() {
//Traversing the Huffman Tree and assigning specific codes to each character
traverse(root, "");
}
void huffman::saveEncodedFile() {
//Saving encoded (.huf) file
inFile.open(inFileName, ios::in);
outFile.open(outFileName, ios::out | ios::binary);
string in = "";
string s = "";
char id;
//Saving the meta data (huffman tree)
in += (char)minHeap.size();
priority_queue <Node*, vector<Node*>, Compare> tempPQ(minHeap);
while (!tempPQ.empty()) {
Node* curr = tempPQ.top();
in += curr->data;
//Saving 16 decimal values representing code of curr->data
s.assign(127 - curr->code.length(), '0');
s += '1';
s += curr->code;
//Saving decimal values of every 8-bit binary code
in += (char)binToDec(s.substr(0, 8));
for (int i = 0; i < 15; i++) {
s = s.substr(8);
in += (char)binToDec(s.substr(0, 8));
}
tempPQ.pop();
}
s.clear();
//Saving codes of every charachter appearing in the input file
inFile.get(id);
while (!inFile.eof()) {
s += arr[id]->code;
//Saving decimal values of every 8-bit binary code
while (s.length() > 8) {
in += (char)binToDec(s.substr(0, 8));
s = s.substr(8);
}
inFile.get(id);
}
//Finally if bits remaining are less than 8, append 0's
int count = 8 - s.length();
if (s.length() < 8) {
s.append(count, '0');
}
in += (char)binToDec(s);
//append count of appended 0's
in += (char)count;
//write the in string to the output file
outFile.write(in.c_str(), in.size());
inFile.close();
outFile.close();
}
void huffman::saveDecodedFile() {
inFile.open(inFileName, ios::in | ios::binary);
outFile.open(outFileName, ios::out);
unsigned char size;
inFile.read(reinterpret_cast<char*>(&size), 1);
//Reading count at the end of the file which is number of bits appended to make final value 8-bit
inFile.seekg(-1, ios::end);
char count0;
inFile.read(&count0, 1);
//Ignoring the meta data (huffman tree) (1 + 17 * size) and reading remaining file
inFile.seekg(1 + 17 * size, ios::beg);
vector<unsigned char> text;
unsigned char textseg;
inFile.read(reinterpret_cast<char*>(&textseg), 1);
while (!inFile.eof()) {
text.push_back(textseg);
inFile.read(reinterpret_cast<char*>(&textseg), 1);
}
Node *curr = root;
string path;
for (int i = 0; i < text.size() - 1; i++) {
//Converting decimal number to its equivalent 8-bit binary code
path = decToBin(text[i]);
if (i == text.size() - 2) {
path = path.substr(0, 8 - count0);
}
//Traversing huffman tree and appending resultant data to the file
for (int j = 0; j < path.size(); j++) {
if (path[j] == '0') {
curr = curr->left;
}
else {
curr = curr->right;
}
if (curr->left == NULL && curr->right == NULL) {
outFile.put(curr->data);
curr = root;
}
}
}
inFile.close();
outFile.close();
}
void huffman::getTree() {
inFile.open(inFileName, ios::in | ios::binary);
//Reading size of MinHeap
unsigned char size;
inFile.read(reinterpret_cast<char*>(&size), 1);
root = new Node();
//next size * (1 + 16) characters contain (char)data and (string)code[in decimal]
for(int i = 0; i < size; i++) {
char aCode;
unsigned char hCodeC[16];
inFile.read(&aCode, 1);
inFile.read(reinterpret_cast<char*>(hCodeC), 16);
//converting decimal characters into their binary equivalent to obtain code
string hCodeStr = "";
for (int i = 0; i < 16; i++) {
hCodeStr += decToBin(hCodeC[i]);
}
//Removing padding by ignoring first (127 - curr->code.length()) '0's and next '1' character
int j = 0;
while (hCodeStr[j] == '0') {
j++;
}
hCodeStr = hCodeStr.substr(j+1);
//Adding node with aCode data and hCodeStr string to the huffman tree
buildTree(aCode, hCodeStr);
}
inFile.close();
}
void huffman::compress() {
createMinHeap();
createTree();
createCodes();
saveEncodedFile();
}
void huffman::decompress() {
getTree();
saveDecodedFile();
}