-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
69 lines (62 loc) · 2.54 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
#include"huffman.h"
#include<chrono>
#include<boost/filesystem.hpp>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 4) {
cout << "USAGE: huffman [OPTION] InputFile OutputFile\n;";
cout << "Options:\n-c\tCompress the given InputFile to OutputFile\n-d\tDecompress the given InputFile to OutputFile\n";
return 1;
}
cout << "------ Firing up Huffman Routine ------\n";
if (strcmp(argv[1],"-c") == 0) { // Start Compressing
cout << "~ Checking if file exists... ";
// Check if the input file exists
ifstream file(argv[2]);
if (!file.good()) {
cout << "\n!! Error: Can't Open Input File\nProgram Aborted\n";
return 1;
}
cout << "[OK]\n~ Creating Object and Firing Compression... ";
HuffmanTree *hObject = new HuffmanTree(argv[2], argv[3]);
// Start Timer //
auto start = chrono::high_resolution_clock::now();
hObject->encode();
cout << "[OK]\n~ Writing everything to file and cleaning up... ";
hObject->writeToFile(true);
delete hObject;
cout << "[OK]\n------ Done ------\n";
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> duration = end - start;
auto originalSize = boost::filesystem::file_size(argv[2]);
int newSize = boost::filesystem::file_size(argv[3]);
cout << "Compression: " << float(newSize) / float(originalSize)*100.0 << "% (" << originalSize << " -> " << newSize << ")\n";
cout << "Duration: " << duration.count() << " millisconds" << endl;
return 1;
}
if (strcmp(argv[1],"-d") == 0) {
cout << "~ Checking if file exists... ";
// Check if the input file exists
ifstream file(argv[2]);
if (!file.good()) {
cout << "\n!! Error: Can't Open Input File\nProgram Aborted\n";
return 1;
}
cout << "[OK]\n~ Creating Object and Firing Decompression... ";
HuffmanTree *hObject = new HuffmanTree(argv[2], argv[3]);
// Start Timer //
auto start = chrono::high_resolution_clock::now();
hObject->decode();
cout << "[OK]\n~ Writing everything to file and cleaning up... ";
hObject->writeToFile(false);
delete hObject;
cout << "[OK]\n------ Done ------\n";
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> duration = end - start;
auto originalSize = boost::filesystem::file_size(argv[2]);
int newSize = boost::filesystem::file_size(argv[3]);
cout << "Decompression: " << originalSize << " -> " << newSize << "\n";
cout << "Duration: " << duration.count() << " milliseconds" << endl;
return 1;
}
}