-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
111 lines (88 loc) · 4.03 KB
/
Copy pathmain.cpp
File metadata and controls
111 lines (88 loc) · 4.03 KB
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
#include <iostream>
#include <string>
#include <cstring>
#include <cstdint>
#include <iomanip>
#include "Huffman.h"
#include "FileHandler.h"
#include "utils.h"
static void printUsage(const char* prog) {
std::cout << "Usage:\n"
<< " " << prog << " compress <input_file> <output.bin>\n"
<< " " << prog << " decompress <input.bin> <output_file>\n"
<< " " << prog << " info <input.bin>\n";
}
int main(int argc, char* argv[]) {
if (argc < 3) { printUsage(argv[0]); return 1; }
std::string cmd = argv[1];
// ── COMPRESS ────────────────────────────────────────────────
if (cmd == "compress") {
if (argc < 4) { printUsage(argv[0]); return 1; }
std::string inPath = argv[2];
std::string outPath = argv[3];
std::cout << "[+] Reading: " << inPath << "\n";
std::string text = FileHandler::readFile(inPath);
std::cout << "[+] File size: " << text.size() << " bytes, "
<< "unique chars: ";
// Build Huffman tree
Huffman huff;
huff.build(text);
std::cout << huff.codes().size() << "\n";
// Encode
std::string bits = huff.encode(text);
std::string treeData = huff.serializeTree();
// Write compressed file
FileHandler::writeCompressed(outPath, treeData, bits, (uint32_t)text.size());
// Stats
Utils::Stats stats = Utils::computeStats(text, bits, huff.codes());
Utils::printStats(stats);
Utils::printCodes(huff.codes());
std::cout << "[✓] Compressed → " << outPath << "\n";
// ── DECOMPRESS ──────────────────────────────────────────────
} else if (cmd == "decompress") {
if (argc < 4) { printUsage(argv[0]); return 1; }
std::string inPath = argv[2];
std::string outPath = argv[3];
std::cout << "[+] Reading: " << inPath << "\n";
std::string treeData, bits;
uint32_t originalSize = 0;
FileHandler::readCompressed(inPath, treeData, bits, originalSize);
// Rebuild tree
Huffman huff;
size_t pos = 0;
huff.deserializeTree(treeData, pos);
// Decode
std::string result = huff.decode(bits);
// Verify size
if (result.size() != originalSize) {
std::cerr << "[!] Warning: decoded size (" << result.size()
<< ") != expected (" << originalSize << ")\n";
}
FileHandler::writeFile(outPath, result);
std::cout << "[✓] Decompressed → " << outPath
<< " (" << result.size() << " bytes)\n";
// ── INFO ────────────────────────────────────────────────────
} else if (cmd == "info") {
std::string inPath = argv[2];
std::string treeData, bits;
uint32_t originalSize = 0;
FileHandler::readCompressed(inPath, treeData, bits, originalSize);
Huffman huff;
size_t pos = 0;
huff.deserializeTree(treeData, pos);
size_t compressedBytes = (bits.size() + 7) / 8;
double ratio = (double)compressedBytes / originalSize;
std::cout << "\n--- File Info: " << inPath << " ---\n";
std::cout << " Original size : " << originalSize << " bytes\n";
std::cout << " Compressed size : " << compressedBytes << " bytes\n";
std::cout << " Compression ratio: " << std::fixed << std::setprecision(2) << ratio << "\n";
std::cout << " Space saving : " << (1.0 - ratio) * 100.0 << "%\n";
std::cout << " Unique chars : " << huff.codes().size() << "\n\n";
Utils::printCodes(huff.codes());
} else {
std::cerr << "Unknown command: " << cmd << "\n";
printUsage(argv[0]);
return 1;
}
return 0;
}