lzw-assignment/src/main.cc

98 lines
2.7 KiB
C++
Raw Normal View History

#include "compress.hh"
#include "uncompress.hh"
2018-06-21 15:38:51 +00:00
#include <cassert>
#include <getopt.h>
#include <tuple>
using std::printf;
using std::puts;
2018-05-25 11:28:37 +00:00
using std::string;
using std::tuple;
2018-04-02 17:39:53 +00:00
// custom types ///////////////////////////////////////////////////////////////
using dic_t = std::map<std::pair<uint32_t, uint8_t>, uint32_t>;
using ustring = std::basic_string<uint8_t>; // chaine non encodée
using uvec = std::vector<uint32_t>; // chaine encodée
void help() {
2018-11-21 09:28:13 +00:00
puts("Usage:\n\
lzw [-options] [-i path] [-o path]\n\n\
The default action is to compress the input file to a .lzw file\n\
in which the directory in which the software is executed.\n\
Options available:\n\
-h --help\n\
\tdisplay the current message\n\
-i --input\n\
\tpath to the input file (MANDATORY)\n\
-o --output\n\
\tpath to the output file (if the file already exists, it will be\n\n\
\toverwritten). Default: input path + \".lzw\\n\
-c --compress\n\
\tcompress the input file\n\
-u --uncompress\n\
\tuncompresses the input file to the output file. If no output path\n\
\thas not been entered and if the input file ends with \".lzw\",\n\
\tthe extension \".lzw\" will be removed; otherwise, the extension\n\
\t\"_uncompresed\" will be added");
}
2018-04-02 17:39:53 +00:00
2019-08-19 14:40:13 +00:00
[[nodiscard]] std::tuple<string, string, bool> process_args(int t_argc,
char *t_argv[]) {
2018-06-18 15:53:38 +00:00
auto ret = std::make_tuple(string{}, string{}, true);
while (true) {
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"input", required_argument, nullptr, 'i'},
{"output", required_argument, nullptr, 'o'},
{"compress", no_argument, nullptr, 'c'},
{"uncompress", no_argument, nullptr, 'u'},
{nullptr, 0, nullptr, 0}};
2018-05-25 11:28:37 +00:00
int c = getopt_long(t_argc, t_argv, "hi:o:cu", long_options, &option_index);
2018-06-21 15:38:51 +00:00
if (c == -1)
break;
switch (c) {
2018-05-25 11:28:37 +00:00
case 0:
break;
2018-05-25 11:28:37 +00:00
case 'h':
help();
2018-05-25 11:28:37 +00:00
exit(0);
case 'i':
std::get<0>(ret) = optarg;
break;
2018-05-25 11:28:37 +00:00
case 'o':
std::get<1>(ret) = optarg;
break;
2018-05-25 11:28:37 +00:00
case 'c':
std::get<2>(ret) = true;
break;
2018-05-25 11:28:37 +00:00
case 'u':
std::get<2>(ret) = false;
break;
case '?':
2018-06-09 20:59:11 +00:00
[[fallthrough]];
2018-05-25 11:28:37 +00:00
default:
puts("Error: unknown parameter.");
help();
2018-05-25 11:28:37 +00:00
exit(1);
}
}
2018-05-25 11:28:37 +00:00
return ret;
}
2018-05-25 11:28:37 +00:00
int main(int argc, char *argv[]) {
const auto [input_path, output_path, compressing] = process_args(argc, argv);
2019-08-19 14:40:13 +00:00
if (input_path.empty()) {
help();
return 0;
}
if (compressing) {
compress(input_path, (output_path.empty()) ? nullptr : output_path.c_str());
} else {
uncompress(input_path,
(output_path.empty()) ? nullptr : output_path.c_str());
}
return 0;
2018-03-20 20:41:46 +00:00
}