-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
340 lines (317 loc) · 9.19 KB
/
main.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// STARTER CODE: main.cpp
//
// Shanon Reckinger
// U. of Illinois, Chicago
// CS 251: Fall 2020
//
// This file is the driver for the huffman encoding application.
// Do not edit this file!
//
#include <iostream>
#include <map>
#include <unordered_map>
#include <fstream>
#include <queue>
#include <vector>
#include <functional>
#include <ctype.h>
#include <math.h>
#include "hashmap.h"
#include "bitstream.h"
#include "util.h"
using namespace std;
// Function prototypes
string menu();
bool is123456(string choice);
void do123456(string choice, string &filename, bool &isFile,
hashmapF &frequencyMap,
HuffmanNode* &encodingTree,
hashmapE &encodingMap);
string printChar(int val);
void printMap(hashmapE &map);
void printMap(hashmapF &map);
void printTree(HuffmanNode* node, string str);
void printTextFile(string filename);
void printBinaryFile(string filename);
int main() {
hashmapF frequencyMap;
HuffmanNode* encodingTree = nullptr;
hashmapE encodingMap;
string filename;
bool isFile = true;
string choice = "wee";
while (choice != "Q") {
choice = menu();
if (is123456(choice)){
do123456(choice, filename, isFile, frequencyMap,
encodingTree, encodingMap);
} else if (choice == "C") {
cout << "Enter filename: ";
cin >> filename;
compress(filename);
} else if (choice == "D") {
cout << "Enter filename: ";
cin >> filename;
decompress(filename);
} else if (choice == "B") {
cout << "Enter filename: ";
cin >> filename;
printBinaryFile(filename);
} else if (choice == "T") {
cout << "Enter filename: ";
cin >> filename;
printTextFile(filename);
}
}
return 0;
}
//
// menu
// Prints message to screen and gets response from keyboard.
//
string menu() {
cout << "Welcome to the file compression app!" << endl;
cout << "1. Build character frequency map" << endl;
cout << "2. Build encoding tree" << endl;
cout << "3. Build encoding map" << endl;
cout << "4. Encode data" << endl;
cout << "5. Decode data" << endl;
cout << "6. Free tree memory" << endl;
cout << endl;
cout << "C. Compress file" << endl;
cout << "D. Decompress file" << endl;
cout << endl;
cout << "B. Binary file viewer" << endl;
cout << "T. Text file viewer" << endl;
cout << "Q. Quit" << endl;
cout << endl;
cout << "Enter choice: ";
string choice;
cin >> choice;
return choice;
}
//
// is123456
// This function checks if choice is 1, 2, 3, 4, 5, or 6.
//
bool is123456(string choice) {
if (choice == "1" || choice == "2" ||choice == "3" ||
choice == "4" ||choice == "5" || choice == "6") {
return true;
} else {
return false;
}
}
//
// do123456
// This function runs code for choice equal to 1, 2, 3, 4, 5, and 6.
//
void do123456(string choice, string &filename, bool &isFile,
hashmapF &frequencyMap,
HuffmanNode* &encodingTree,
hashmapE &encodingMap) {
// gets file/string and filename.
if (choice == "1") {
cout << "[F]ilename or [S]tring? ";
string fORs;
cin >> fORs;
isFile = (fORs == "F" ? true : false);
if (isFile) {
cout << "Enter file name: ";
} else {
cout << "Enter string: ";
}
cin >> filename;
}
// Build Frequency Map
if (choice == "1") {
buildFrequencyMap(filename, isFile, frequencyMap);
cout << endl;
cout << "Building frequency map..." << endl;
printMap(frequencyMap);
cout << endl;
// Build Encoding Tree
} else if (choice == "2") {
encodingTree = buildEncodingTree(frequencyMap);
cout << endl;
cout << "Building encoding tree..." << endl;
printTree(encodingTree, "");
cout << endl;
// Build Encoding Map
} else if (choice == "3") {
encodingMap = buildEncodingMap(encodingTree);
cout << endl;
cout << "Building encoding map..." << endl;
printMap(encodingMap);
cout << endl;
// Encode text
} else if (choice == "4") {
// this step is only valid for files
if (!isFile) {
cout << endl;
cout << "********************************" << endl;
cout << "Must provide file to run encode." << endl;
cout << "Enter Q to start over and try again." << endl;
cout << "********************************" << endl;
cout << endl;
return;
}
cout << endl;
cout << "Encoding..." << endl;
string fn = (isFile) ? filename : ("file_" + filename + ".txt");
ofbitstream output(filename + ".huf");
ifstream input(filename);
stringstream ss;
// note: << is overloaded for the hashmap class. super nice!
ss << frequencyMap;
output << frequencyMap; // add the frequency map to the file
int size = 0;
string codeStr = encode(input, encodingMap, output, size, true);
// count bytes in frequency map header
size = ss.str().length() + ceil((double)size / 8);
cout << "Compressed file size: " << size << endl;
cout << codeStr << endl;
cout << endl;
output.close(); // must close file so autograder can open for testing
// Decode text
} else if (choice == "5") {
// this step is only valid for files
if (!isFile) {
cout << endl;
cout << "********************************" << endl;
cout << "Must provide file to run encode." << endl;
cout << "Enter Q to start over and try again." << endl;
cout << "********************************" << endl;
cout << endl;
return;
}
cout << endl;
cout << "Decoding..." << endl;
size_t pos = filename.find(".huf");
if ((int)pos >= 0) {
filename = filename.substr(0, pos);
}
pos = filename.find(".");
string ext = filename.substr(pos, filename.length() - pos);
filename = filename.substr(0, pos);
ifbitstream input(filename + ext + ".huf");
ofstream output(filename + "_unc" + ext);
hashmapF dump;
input >> dump; // get rid of frequency map at top of file
string decodeStr = decode(input, encodingTree, output);
cout << decodeStr << endl;
cout << endl;
output.close(); // must close file so autograder can open for testing
// Free the Encoding Tree
} else if (choice == "6") {
cout << "Freeing encoding tree..." << endl;
freeTree(encodingTree);
}
}
//
// printChar
// This function takes in an integer value and prints the ASCII character with
// singles quotes. e.g. val = 98, 'b' is returned.
//
string printChar(int ch){
if (ch == '\n') {
return "'\\n'";
} else if (ch == '\t') {
return "'\\t'";
} else if (ch == '\r') {
return "'\\r'";
} else if (ch == '\f') {
return "'\\f'";
} else if (ch == '\b') {
return "'\\b'";
} else if (ch == '\0') {
return "'\\0'";
} else if (ch == ' ') {
return "' '";
} else if (ch == (int) PSEUDO_EOF) {
return "EOF";
} else if (ch == (int) NOT_A_CHAR) {
return "N/A";
} else {
return string("'") + (char) ch + string("'");
}
}
//
// printFrequencyMap
//
//
void printMap(hashmapF &map) {
for (int key : map.keys()) {
cout << key << ": " << '\t' << printChar(key);
cout << '\t' << "-->" << '\t' << map.get(key) << endl;
}
}
//
// printEncodingMap
//
//
void printMap(hashmapE &map) {
for (auto e : map) {
cout << e.first << ": " << '\t' << printChar(e.first);
cout << '\t' << "-->" << '\t' << e.second << endl;
}
}
//
// printTree
//
//
void printTree(HuffmanNode* node, string str) {
if (node == nullptr) {
return;
} else {
cout << str << "{" << printChar(node->character);
if (node->character != NOT_A_CHAR)
cout << "(" << node->character << ")";
cout << ", count=" << node->count << "}" << endl;
printTree(node->zero, str+" ");
printTree(node->one, str+" ");
}
}
//
// printTextFile
//
//
void printTextFile(string filename) {
cout << filename << endl;
ifstream inFile(filename);
if (!inFile.is_open()) {
cout << "File does not exist." << endl;
}
while (true) {
char ch = inFile.get();
if (ch == EOF) break;
cout << ch;
}
cout << endl;
}
//
// printBinaryFile
//
//
void printBinaryFile(string filename) {
cout << filename << endl;
ifbitstream input(filename);
if (!input.is_open()) {
cout << "File does not exist." << endl;
}
int i = 0;
while (true) {
i++;
int bit = input.readBit();
if (input.fail()) break;
cout << bit;
if (i > 0 && i % 8 == 0) {
cout << " ";
}
if (i > 0 && i % 64 == 0) {
cout << endl;
}
}
cout << endl;
}