-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (94 loc) · 3.36 KB
/
main.cpp
File metadata and controls
120 lines (94 loc) · 3.36 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
112
113
114
115
116
117
118
119
120
#include <cxxopts.hpp>
#include <iostream>
#include <string>
#include "algorithms/LZWCompression.h"
#include "algorithms/huffmanCompression.h"
#include "utility/integerToStringSerializer.h"
#include "utility/unixFileHandler.h"
struct CompressionArgs
{
bool is_encode;
bool human_readable_output;
std::string algorithmName;
std::string inputFileName;
std::string outputFileName;
};
int parseArguments(int argc, char *argv[], CompressionArgs &args)
{
// Command line options
cxxopts::Options options("compression", "Simple Compression Utility");
options.add_options()("h,help", "Show help")("a,algorithm", "Compression algorithm (can be huffman or LZW)", cxxopts::value<std::string>()->default_value("huffman"))("r,human-readable", "Human readable output")("e,encode", "Encode")("d,decode", "Decode")("i,input", "Input file (Will be stdin if left empty)", cxxopts::value<std::string>())("o,output", "Output file (Will be stdout if left empty)", cxxopts::value<std::string>());
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help() << '\n';
return 1;
}
if (!(result.count("encode") ^ result.count("decode")))
{
std::cerr << "You must choose one between encode and decode." << '\n';
std::cout << options.help() << '\n';
return 1;
}
args.human_readable_output = result.count("human-readable") > 0;
args.is_encode = result.count("encode") > 0;
args.algorithmName = result["algorithm"].as<std::string>();
if (args.algorithmName != "huffman" && args.algorithmName != "LZW")
{
std::cerr << "Invalid algorithm. Use -h or --help for help." << '\n';
return 1;
}
args.inputFileName = "";
args.outputFileName = "";
if (result.count("input"))
{
args.inputFileName = result["input"].as<std::string>();
}
if (result.count("output"))
{
args.outputFileName = result["output"].as<std::string>();
}
return 0;
}
int runEngine(const CompressionArgs &args)
{
std::string inputContent, outputContent;
std::unique_ptr<Algorithms::IAlgorithm> compressionAlgorithm;
auto serializer =
std::make_unique<Serializers::integerToStringSerializer<uint32_t>>(args.human_readable_output);
auto fileHandler = std::make_unique<FileHandlers::UnixFileHandler>();
fileHandler->init(args.inputFileName, args.outputFileName);
if (args.algorithmName == "huffman")
{
compressionAlgorithm = std::make_unique<Algorithms::HuffmanCompression>(std::move(serializer));
}
else if (args.algorithmName == "LZW")
{
compressionAlgorithm = std::make_unique<Algorithms::LZWCompression>(std::move(serializer));
}else{
std::cerr << "Invalid algorithm. Use -h or --help for help." << '\n';
return 1;
}
if (fileHandler->load(inputContent) != 0)
return 1;
if (args.is_encode)
{
if(compressionAlgorithm->encode(inputContent, outputContent)!= 0)
return 1;
}
else
{
if(compressionAlgorithm->decode(inputContent, outputContent)!= 0)
return 1;
}
if (fileHandler->save(outputContent) != 0)
return 1;
return 0;
}
int main(int argc, char *argv[])
{
CompressionArgs args;
if (parseArguments(argc, argv, args) != 0)
return 1;
return runEngine(args);
}