2018-04-05 16:47:07 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file main.cc
|
|
|
|
|
* \brief Main file
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2018-06-10 19:21:06 +00:00
|
|
|
|
#include <getopt.h>
|
2018-05-25 11:28:37 +00:00
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <tuple>
|
2018-06-10 19:21:06 +00:00
|
|
|
|
#include "compress.hh"
|
|
|
|
|
#include "uncompress.hh"
|
2018-03-21 00:22:05 +00:00
|
|
|
|
|
|
|
|
|
using std::printf;
|
|
|
|
|
using std::puts;
|
2018-05-25 11:28:37 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
using std::tuple;
|
2018-03-21 00:22:05 +00:00
|
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
|
// custom types ///////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Dictionnaire :
|
|
|
|
|
<
|
|
|
|
|
<
|
|
|
|
|
numéro chaine précédente,
|
|
|
|
|
caractère ASCII
|
|
|
|
|
>
|
|
|
|
|
numéro chaine courante
|
|
|
|
|
>
|
|
|
|
|
*/
|
|
|
|
|
using dic_t = std::map<std::pair<uint32_t, uint8_t>, uint32_t>;
|
|
|
|
|
using ustring = std::basic_string<uint8_t>; // chaine non encodée
|
2018-04-10 08:39:41 +00:00
|
|
|
|
using uvec = std::vector<uint32_t>; // chaine encodée
|
|
|
|
|
|
2018-04-10 09:25:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* \brief Affichage d’aide
|
|
|
|
|
*/
|
2018-04-10 08:39:41 +00:00
|
|
|
|
void help() {
|
|
|
|
|
puts("Usage:");
|
|
|
|
|
puts("lzw [-options] [-i path] [-o path]");
|
|
|
|
|
puts("\tThe default action is to compress the input file to a .lzw file");
|
|
|
|
|
puts("\tin which the directory in which the software is executed.");
|
|
|
|
|
puts("\tOptions available:");
|
|
|
|
|
puts("\t-i\tpath to the input file (mandatory)");
|
|
|
|
|
puts("\t-o\tpath to the output file (if the file already exists, it will");
|
|
|
|
|
puts("\t\tbe overwritten). Default: input path + \".lzw\"");
|
|
|
|
|
puts("\t-c\tcompress the input file");
|
2018-06-10 19:21:06 +00:00
|
|
|
|
puts("\t-u\tuncompresses the input file to the output file. If no output");
|
2018-04-10 08:39:41 +00:00
|
|
|
|
puts("\t\tpath has not been entered and if the input file ends with ");
|
|
|
|
|
puts("\t\t\".lzw\", the extension \".lzw\" will be removed; otherwise, the ");
|
|
|
|
|
puts("\t\textension \".uncompresed\" will be added");
|
|
|
|
|
}
|
2018-04-02 17:39:53 +00:00
|
|
|
|
|
2018-05-25 11:28:37 +00:00
|
|
|
|
std::tuple<string, string, bool> process_args(int t_argc, char *t_argv[]) {
|
|
|
|
|
auto ret = std::make_tuple(string{}, string{}, false);
|
2018-04-26 11:49:39 +00:00
|
|
|
|
while (true) {
|
2018-04-10 08:39:41 +00:00
|
|
|
|
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'},
|
2018-04-26 11:49:39 +00:00
|
|
|
|
{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);
|
|
|
|
|
if (c == -1) break;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
switch (c) {
|
2018-05-25 11:28:37 +00:00
|
|
|
|
case 0:
|
2018-04-10 08:39:41 +00:00
|
|
|
|
break;
|
2018-05-25 11:28:37 +00:00
|
|
|
|
case 'h':
|
2018-04-10 08:39:41 +00:00
|
|
|
|
help();
|
2018-05-25 11:28:37 +00:00
|
|
|
|
exit(0);
|
|
|
|
|
case 'i':
|
|
|
|
|
std::get<0>(ret) = optarg;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
break;
|
2018-05-25 11:28:37 +00:00
|
|
|
|
case 'o':
|
|
|
|
|
std::get<1>(ret) = optarg;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
break;
|
2018-05-25 11:28:37 +00:00
|
|
|
|
case 'c':
|
|
|
|
|
std::get<2>(ret) = true;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
break;
|
2018-05-25 11:28:37 +00:00
|
|
|
|
case 'u':
|
|
|
|
|
std::get<2>(ret) = false;
|
2018-04-10 08:39:41 +00:00
|
|
|
|
break;
|
2018-04-26 09:54:02 +00:00
|
|
|
|
case '?':
|
2018-06-09 20:59:11 +00:00
|
|
|
|
[[fallthrough]];
|
2018-05-25 11:28:37 +00:00
|
|
|
|
default:
|
2018-04-10 08:39:41 +00:00
|
|
|
|
puts("Error: unknown parameter.");
|
|
|
|
|
help();
|
2018-05-25 11:28:37 +00:00
|
|
|
|
exit(1);
|
2018-04-10 08:39:41 +00:00
|
|
|
|
}
|
2018-03-21 00:22:05 +00:00
|
|
|
|
}
|
2018-05-25 11:28:37 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-03-21 00:22:05 +00:00
|
|
|
|
|
2018-05-25 11:28:37 +00:00
|
|
|
|
/* TODO: compression multiple : nombre de compressions puis fichier compressé */
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
const auto [input_path, output_path, compressing] = process_args(argc, argv);
|
|
|
|
|
assert(!input_path.empty());
|
2018-04-10 08:39:41 +00:00
|
|
|
|
if (compressing) {
|
2018-06-10 19:21:06 +00:00
|
|
|
|
compress(input_path, (output_path.empty()) ? nullptr : output_path.c_str());
|
2018-04-10 08:39:41 +00:00
|
|
|
|
} else {
|
2018-06-10 19:21:06 +00:00
|
|
|
|
uncompress(input_path,
|
|
|
|
|
(output_path.empty()) ? nullptr : output_path.c_str());
|
2018-04-10 08:39:41 +00:00
|
|
|
|
}
|
2018-03-21 00:22:05 +00:00
|
|
|
|
return 0;
|
2018-03-20 20:41:46 +00:00
|
|
|
|
}
|