2018-04-05 16:47:07 +00:00
|
|
|
/**
|
|
|
|
* \file main.cc
|
|
|
|
* \brief Main file
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "compress.hh"
|
2018-03-21 00:22:05 +00:00
|
|
|
#include <fstream>
|
2018-03-20 20:41:46 +00:00
|
|
|
#include <iostream>
|
2018-03-21 00:22:05 +00:00
|
|
|
#include <streambuf>
|
|
|
|
|
|
|
|
using std::printf;
|
|
|
|
using std::puts;
|
|
|
|
|
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
|
|
|
|
using uvec = std::vector<uint32_t>; // chaine encodée
|
|
|
|
|
2018-03-21 00:22:05 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 2) {
|
|
|
|
puts("Usage: lzw <filename>");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
printf("argv[%d] = %s\n", i, argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fichier d'entrée
|
|
|
|
std::ifstream t{argv[1]};
|
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
// fichier de sortie
|
|
|
|
FILE *out = fopen("output.lzw", "wb");
|
2018-03-21 00:22:05 +00:00
|
|
|
|
|
|
|
// string contenant le fichier d'entrée
|
|
|
|
ustring str{};
|
|
|
|
|
|
|
|
// réserver la mémoire dans le string pour le fichier à compresser
|
|
|
|
t.seekg(0, std::ios::end);
|
|
|
|
str.reserve(static_cast<unsigned long>(t.tellg()));
|
|
|
|
t.seekg(0, std::ios::beg);
|
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
// assigner au string les caractères du fichier
|
2018-03-21 00:22:05 +00:00
|
|
|
str.assign((std::istreambuf_iterator<char>(t)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
// test pour voir si tout est bien chargé
|
|
|
|
printf("Size of str: %zu\n", str.size());
|
|
|
|
|
|
|
|
// le dictionnaire, initialement vide.
|
|
|
|
dic_t dictionary{};
|
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
// compresssion du texte
|
2018-03-21 00:22:05 +00:00
|
|
|
const auto comp_str{compress(str, dictionary)};
|
|
|
|
|
|
|
|
printf("Compressed, size of compressed string: %zu\n", comp_str.size());
|
2018-04-02 17:39:53 +00:00
|
|
|
printf("Compression ratio: %.10f\n",
|
|
|
|
static_cast<double>(str.size()) / static_cast<double>(comp_str.size()));
|
2018-03-21 00:22:05 +00:00
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
printf("Number of custom words in the dictionary: %zu\n", dictionary.size());
|
2018-03-21 00:22:05 +00:00
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
for(const auto c : comp_str)
|
2018-04-03 14:11:54 +00:00
|
|
|
write_utf8(out, c);
|
2018-03-21 00:22:05 +00:00
|
|
|
|
2018-04-02 17:39:53 +00:00
|
|
|
fclose(out);
|
2018-03-21 00:22:05 +00:00
|
|
|
t.close();
|
|
|
|
|
|
|
|
return 0;
|
2018-03-20 20:41:46 +00:00
|
|
|
}
|