-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (56 loc) · 2.13 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
#include <algorithm>
#include <string>
#include "mtf/MTFHashCompressor.h"
#include "randomized/TabulationHash.h"
char *get_option(char **begin, char **end, const std::string& option) {
char **itr = std::find(begin, end, option);
if (itr != end && ++itr != end) {
return *itr;
}
return nullptr;
}
bool has_option(char **begin, char **end, const std::string& option) {
return std::find(begin, end, option) != end;
}
int main(int argc, char *argv[]) {
char *path_str = get_option(argv, argv + argc, "-f");
if (!path_str) {
throw std::runtime_error("File name not provided");
}
std::string path = std::string(path_str);
bool compress = has_option(argv, argv + argc, "-c");
bool decompress = has_option(argv, argv + argc, "-d");
bool multithreaded = has_option(argv, argv + argc, "-p");
char *k_str = get_option(argv, argv + argc, "-k");
int k = 3;
if (k_str) {
k = (int) strtol(k_str, nullptr, 10);
}
char *mem_str = get_option(argv, argv + argc, "-m");
uint64_t max_mem = (uint64_t) 4 * 1024 * 1024 * 1024;
if (mem_str) {
max_mem = strtoull(k_str, nullptr, 10);
}
std::string suffix = ".mtf";
if (compress && decompress) {
throw std::runtime_error("Both compression and decompression selected");
} else if (compress) {
std::cout << "Compressing\n";
if (!multithreaded) {
MTFHashCompressor::compress_stream<MTFBuffer<8>, 8>(path, path + suffix, k, max_mem);
} else {
MTFHashCompressor::compress_block<MTFBuffer<8>, 8>(path, path + suffix, k, max_mem);
}
} else if (decompress) {
std::cout << "Decompressing\n";
std::string original_name = path.substr(0, path.length() - suffix.length());
if (!multithreaded) {
MTFHashCompressor::decompress_stream<MTFBuffer<8>, 8>(path, original_name, k, max_mem);
} else {
MTFHashCompressor::decompress_block<MTFBuffer<8>, 8>(path, original_name, k, max_mem);
}
} else {
throw std::runtime_error("Neither compression nor decompression selected");
}
return 0;
}