-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (87 loc) · 3.48 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
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <vector>
#include "HuffmanTree.h"
#include "Compressor.h"
void PrintBanner() {
std::cout << "┌───────────────────────────┐" << '\n';
std::cout << "│ ██╗░░██╗██╗░░░██╗███████╗ │" << '\n';
std::cout << "│ ██║░░██║██║░░░██║██╔════╝ │" << '\n';
std::cout << "│ ███████║██║░░░██║█████╗░░ │" << '\n';
std::cout << "│ ██╔══██║██║░░░██║██╔══╝░░ │" << '\n';
std::cout << "│ ██║░░██║╚██████╔╝██║░░░░░ │" << '\n';
std::cout << "│ ╚═╝░░╚═╝░╚═════╝░╚═╝░ │" << '\n';
std::cout << "└─── C O M P R E S S O R ───┘" << '\n';
}
// Returns whether the file exists, for use in main()
bool FileExists(const std::string& filename) {
std::ifstream ifs(filename);
if(ifs.is_open()) {
ifs.close();
return true;
} else {
return false;
}
}
// Returns file length, for use in main() to show size before/after compression
int GetFileLength(const std::string& filename) {
int length = 0;
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
ifs.ignore(std::numeric_limits<std::streamsize>::max());
length = ifs.gcount();
return length;
}
// Prompt the user for the name of a file that exists,
// or the empty string if the user wishes to quit
std::string PromptForFileNameOrQuit() {
std::string filename = "";
std::cout << "Enter file to compress or \".huf\" file to decompress." << '\n';
std::cout << "(Leave blank to quit.)" << '\n';
while(true) {
std::cout << "> ";
std::getline(std::cin, filename);
if(FileExists(filename) || filename == "") {
return filename;
} else {
std::cout << "File \"" << filename << "\" not found." << '\n';
std::cout << "Is it spelled incorrectly or missing a directory?" << '\n';
std::cout << "(Leave blank to quit.)" << '\n';
}
}
}
int main() {
PrintBanner();
std::cout << '\n';
// Create compressor instance to use compress & decompress functions
Compressor compressor;
while(true) {
std::string filename = PromptForFileNameOrQuit();
if(filename.empty()) { break; }
// The file is not a ".huf" file, so compress it
// and place the resulting file in the same directory.
std::string extension = filename.substr(filename.find('.') + 1);
if(extension != "huf") {
std::string compressed_filename = compressor.compress(filename);
int original_length = GetFileLength(filename);
int compressed_length = GetFileLength(compressed_filename);
// Display results and file size before/after
std::cout << "Compressed file \"" << compressed_filename
<< "\" created" << '\n';
std::cout << original_length << " bytes -> "
<< compressed_length << " bytes" << '\n';
std::cout << '\n';
}
// The file is a ".huf" file, so decompress it
// and place the resulting file in the same directory.
else {
std::string decompressed_filename = compressor.decompress(filename);
// Display results;
std::cout << "Decompressed file \"" << decompressed_filename
<< "\" created" << '\n';
std::cout << '\n';
}
}
return 0;
}