-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
51 lines (44 loc) · 1.22 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
#include <iostream>
#include <fstream>
#include "huffman.h"
std::string readFile(const std::string &filename)
{
std::ifstream ifs(filename, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
return content;
}
void writeFile(const std::string &filename, const std::string &content)
{
std::ofstream ofs(filename, std::ios::binary);
ofs << content;
}
int main(int argc, char *argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << argv[0] << " <compress|decompress> <input file> <output file>" << std::endl;
return 1;
}
std::string command = argv[1];
std::string inputFile = argv[2];
std::string outputFile = argv[3];
std::string content = readFile(inputFile);
HuffmanCoding huffman;
huffman.buildTree(content);
if (command == "compress")
{
std::string encoded = huffman.encode(content);
writeFile(outputFile, encoded);
}
else if (command == "decompress")
{
std::string decoded = huffman.decode(content);
writeFile(outputFile, decoded);
}
else
{
std::cerr << "Unknown command: " << command << std::endl;
return 1;
}
return 0;
}