-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (53 loc) · 1.81 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
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
#include <string>
#include <filesystem>
#include "libs/huffman.hpp"
#include "libs/CLI11_wrapper.hpp"
#include "libs/progress_printer.hpp"
int main(int argc, char** argv) {
// parse command line options using non-standard library
// CLI11 (https://github.com/CLIUtils/CLI11)
std::string source_path, destination_path;
bool encode = false, decode = false;
if (parse(argc, argv, source_path, destination_path, encode, decode)) {
return 1;
}
if (!encode && !decode) {
// neither given
cout << "no action specified, use exactly one of pack/unpack options" << endl;
return 1;
}
try {
// get file size of input
std::filesystem::path path(source_path);
std::uintmax_t source_size = std::filesystem::file_size(path);
// open file streams
std::fstream in, out;
in.open(source_path, std::ios::in | std::ios::binary);
out.open(destination_path, std::ios::out | std::ios::binary);
// create Huffman coder
hf::Huffman coder(in, out);
// create progress printer
ProgressPrinter printer(source_size);
coder.set_progress_printer(&printer);
coder.set_bytes_per_update(source_size / 1000 + 1); // update every 0.1%
// do the job
if (encode) {
cout << "encoding: " << source_path << " --> " << destination_path << endl;
coder.encode();
}
else if (decode) {
cout << "decoding: " << source_path << " --> " << destination_path << endl;
coder.decode();
}
// close file streams
in.close();
out.close();
}
catch (const std::filesystem::filesystem_error& e) {
cout << e.what() << endl;
}
}