Bugfixes and additional changes

This commit is contained in:
Phuntsok Drak-pa 2018-05-25 13:28:37 +02:00
parent 3bd2e15c76
commit 74804a1ead
4 changed files with 37 additions and 112 deletions

View File

@ -42,11 +42,10 @@ constexpr size_t DICT_MAX = ipow(2, 13) - 256; /* 12 bits */
* La chaîne de caractère \p t_text est lue caractère par caractère, et est et * La chaîne de caractère \p t_text est lue caractère par caractère, et est et
* selon la valeur de retour de la fonction \ref dico (permettant dans le même * selon la valeur de retour de la fonction \ref dico (permettant dans le même
* temps la création du dictionnaire), on rajoute un mot ou pas dans le vecteur * temps la création du dictionnaire), on rajoute un mot ou pas dans le vecteur
* de caractères UTF-8 représentant des mots de chars compressés. La fonction * de caractères UTF-8 représentant des mots de chars compressés.
* renvoie ledit vecteur de uint32_t via le paramètre \p t_res.
* *
* \param[in] t_text Chaîne de caractères uint8_t représentant le fichier d'entrée * \param t_text Chaîne de caractères uint8_t représentant le fichier d'entrée
* \param[out] t_res Chaîne de caractères de sortie * \return Vecteur de chunks (vecteurs de uint32_t)
*/ */
vvuint32 lzw_compress(string &&t_text) { vvuint32 lzw_compress(string &&t_text) {
std::puts("Compressing..."); std::puts("Compressing...");
@ -88,33 +87,16 @@ vvuint32 lzw_compress(string &&t_text) {
* \param[in] t_out_file Chemin vers le fichier de sortie * \param[in] t_out_file Chemin vers le fichier de sortie
*/ */
void compress(const std::string &t_in_file, const char *t_out_file) { void compress(const std::string &t_in_file, const char *t_out_file) {
// Fichier dentrée
std::ifstream input_file{t_in_file}; std::ifstream input_file{t_in_file};
if (!input_file.is_open()) { assert(input_file.is_open());
std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 2 FILE *const out = (t_out_file != nullptr) ? fopen(t_out_file, "wb") : fopen("output.lzw", "wb");
<< ": could not open output file \"" << t_in_file
<< "\". Aborting...\n";
exit(1);
}
// Fichier de sortie
FILE *const out =
(t_out_file != nullptr) ? fopen(t_out_file, "wb") : fopen("output.lzw", "wb");
// std::ofstream out{(t_out_file != nullptr) ? t_out_file : "output.lzw",
// std::ios::binary};
if (out == nullptr) { if (out == nullptr) {
std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4 std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4 << ": could not open output file. Aborting...\n";
<< ": could not open output file. Aborting...\n";
input_file.close(); input_file.close();
exit(1); exit(1);
} }
const auto compressed_text{lzw_compress(std::string{std::istreambuf_iterator<char>(input_file), std::istreambuf_iterator<char>()})};
const auto compressed_text{
lzw_compress(std::string{std::istreambuf_iterator<char>(input_file),
std::istreambuf_iterator<char>()})};
write_file(out, compressed_text); write_file(out, compressed_text);
fclose(out); fclose(out);
input_file.close(); input_file.close();
} }

View File

@ -75,7 +75,7 @@ void write_chunk(FILE *const t_out, const vuint32 &t_chunk) {
data[0] = static_cast<unsigned char>(t_chunk[i] >> 4); data[0] = static_cast<unsigned char>(t_chunk[i] >> 4);
data[1] = static_cast<unsigned char>(t_chunk[i] << 4); data[1] = static_cast<unsigned char>(t_chunk[i] << 4);
} else { } else {
data[1] |= static_cast<unsigned char>(t_chunk[i] >> 8) & 0x0F; data[1] |= static_cast<unsigned char>(t_chunk[i] >> 8) & 0xC;
data[2] = static_cast<unsigned char>(t_chunk[i]); data[2] = static_cast<unsigned char>(t_chunk[i]);
fwrite(data.data(), sizeof(data[0]), 3, t_out); fwrite(data.data(), sizeof(data[0]), 3, t_out);
} }

View File

@ -1,5 +1,5 @@
/** /**
* \file io.h * \file io.hh
* \brief Header for file reading and writing * \brief Header for file reading and writing
*/ */

View File

@ -6,17 +6,15 @@
* *
*/ */
#ifdef Debug
constexpr bool debug_mode = true;
#else
constexpr bool debug_mode = false;
#endif
#include "compress.hh" #include "compress.hh"
#include "getopt.h" #include "getopt.h"
#include <cassert>
#include <tuple>
using std::printf; using std::printf;
using std::puts; using std::puts;
using std::string;
using std::tuple;
// custom types /////////////////////////////////////////////////////////////// // custom types ///////////////////////////////////////////////////////////////
@ -53,16 +51,8 @@ void help() {
puts("\t\textension \".uncompresed\" will be added"); puts("\t\textension \".uncompresed\" will be added");
} }
int main(int argc, char *argv[]) { std::tuple<string, string, bool> process_args(int t_argc, char *t_argv[]) {
if constexpr (debug_mode) { auto ret = std::make_tuple(string{}, string{}, false);
for (int i = 0; i < argc; ++i)
printf("argv[%d] = %s\n", i, argv[i]);
}
std::string input_path{};
std::string output_path{};
bool compressing = true;
while (true) { while (true) {
int option_index = 0; int option_index = 0;
static struct option long_options[] = { static struct option long_options[] = {
@ -72,84 +62,41 @@ int main(int argc, char *argv[]) {
{"compress", no_argument, nullptr, 'c'}, {"compress", no_argument, nullptr, 'c'},
{"uncompress", no_argument, nullptr, 'u'}, {"uncompress", no_argument, nullptr, 'u'},
{nullptr, 0, nullptr, 0}}; {nullptr, 0, nullptr, 0}};
int c = getopt_long(argc, argv, "hi:o:cu", long_options, &option_index); int c = getopt_long(t_argc, t_argv, "hi:o:cu", long_options, &option_index);
if (c == -1) if (c == -1) break;
break;
switch (c) { switch (c) {
case 0: { case 0:
if constexpr (debug_mode) {
printf("\noption %s", long_options[option_index].name);
if (optarg) {
printf(" with arg %s\n", optarg);
}
}
break; break;
} case 'h':
case 'h': {
if constexpr (debug_mode) {
printf("From main - option --help passed\n");
}
help(); help();
return 0; exit(0);
} case 'i':
case 'i': { std::get<0>(ret) = optarg;
if constexpr (debug_mode) {
printf("From main - option --input with value '%s'\n", optarg);
}
input_path = optarg;
break; break;
} case 'o':
case 'o': { std::get<1>(ret) = optarg;
if constexpr (debug_mode) {
printf("From main - option --output with value '%s'\n", optarg);
}
output_path = optarg;
break; break;
} case 'c':
case 'c': { std::get<2>(ret) = true;
if constexpr (debug_mode) {
printf("From main - option --compress\n");
}
compressing = true;
break; break;
} case 'u':
case 'u': { std::get<2>(ret) = false;
if constexpr (debug_mode) {
printf("From main - option --uncompress\n");
}
compressing = false;
break; break;
}
case '?': case '?':
default: { default:
puts("Error: unknown parameter."); puts("Error: unknown parameter.");
if constexpr (debug_mode) {
printf("From main - option -?\n");
}
help(); help();
return 1; exit(1);
}
} }
} }
return ret;
}
if (input_path.empty()) { /* TODO: compression multiple : nombre de compressions puis fichier compressé */
puts("Error: no input file specified"); int main(int argc, char *argv[]) {
return 2; const auto [input_path, output_path, compressing] = process_args(argc, argv);
} assert(!input_path.empty());
if (compressing) { if (compressing) {
/*
TODO:
- compresser le fichier dentrée morceaux par morceaux, 16Ko à la fois
- écrire la taille du segment compressé, puis le segment compressé
- multithreading
- compression multiple : nombre de compressions puis fichier compressé
- bit-packing, limiter la taille du dictionnaire pour un certain nombre de
bits.
*/
if constexpr (debug_mode) {
puts("Beginning compression");
}
if (output_path.empty()) { if (output_path.empty()) {
compress(input_path, nullptr); compress(input_path, nullptr);
} else { } else {
@ -157,11 +104,7 @@ int main(int argc, char *argv[]) {
} }
} else { } else {
puts("Not yet implemented :("); puts("Not yet implemented :(");
/* /* Inversion des types du dictionnaire pour retrouver les chaînes plus aisément */
Inversion des types du dictionnaire pour retrouver les chaînes plus
aisément
*/
} }
return 0; return 0;
} }