support for arguments + moved compression lines
This commit is contained in:
parent
2345c3642a
commit
d48012eb1d
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
||||
*~
|
||||
|
||||
gmon\.out
|
||||
|
@ -27,7 +27,7 @@ using dic_t = std::map<std::pair<uint32_t, uint8_t>, uint32_t>;
|
||||
* \param c Caractère suivant la chaine de caractères \p nr_chaine
|
||||
* \return std::pair<bool, uint16_t>
|
||||
*/
|
||||
std::pair<bool, uint32_t>
|
||||
const std::pair<bool, uint32_t>
|
||||
dico(std::map<std::pair<uint32_t, uint8_t>, uint32_t> &t_dictionary,
|
||||
uint32_t t_nr_chaine, uint8_t t_c) {
|
||||
if (t_nr_chaine == 0xFFFFFFFF)
|
||||
|
@ -10,7 +10,7 @@
|
||||
#include <map>
|
||||
|
||||
/// \brief Recherche ou ajout de chaine dans le dictionnaire
|
||||
std::pair<bool, std::uint32_t>
|
||||
const std::pair<bool, std::uint32_t>
|
||||
dico(std::map<std::pair<std::uint32_t, std::uint8_t>, std::uint32_t>
|
||||
&t_dictionary,
|
||||
std::uint32_t t_nr_chaine, std::uint8_t t_c);
|
||||
|
@ -4,11 +4,14 @@
|
||||
*/
|
||||
|
||||
#include "compress.hh"
|
||||
#include "utf8.hh"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
|
||||
using dict_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
|
||||
using std::printf;
|
||||
|
||||
/**
|
||||
* La chaine de caractère \p text est lue caractère par caractère, et est et
|
||||
@ -21,20 +24,26 @@ using uvec = std::vector<uint32_t>; // chaine encodée
|
||||
* \param t_dictionary Dictionnaire de compression
|
||||
* \return std::vector<uint16_t>
|
||||
*/
|
||||
const uvec compress(const ustring &t_text, dict_t &t_dictionary) {
|
||||
const uvec lzw_compress(const ustring &t_text, dict_t &t_dictionary) {
|
||||
std::puts("Compressing...");
|
||||
uvec res{};
|
||||
uint32_t w = 0xFFFFFFFF;
|
||||
uint32_t len = 0;
|
||||
|
||||
constexpr size_t DICT_MAX = 7936; /* 12 bits */
|
||||
|
||||
#ifdef Debug
|
||||
size_t progress = 0;
|
||||
#endif
|
||||
|
||||
for (const auto &c : t_text) {
|
||||
++len;
|
||||
|
||||
#ifdef Debug
|
||||
printf("\rprogress: %zu / %zu", progress++, t_text.size());
|
||||
printf("\rprogress: %zu / %zu", ++progress, t_text.size());
|
||||
#endif
|
||||
if (len > 9) {
|
||||
|
||||
if (/* len > LENGTH_MAX || */ t_dictionary.size() >= DICT_MAX ) {
|
||||
res.push_back(static_cast<uint32_t>(w));
|
||||
w = c;
|
||||
len = 0;
|
||||
@ -50,3 +59,61 @@ const uvec compress(const ustring &t_text, dict_t &t_dictionary) {
|
||||
printf("\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief function description
|
||||
*
|
||||
* Wrapper de la fonction \ref lzw_compress gérant l'ouverture, la lecture,
|
||||
* l'écriture et la fermeture des fichiers d’entrée et de sortie. Si \p
|
||||
* t_out_file est nul (chemin non spécifié), il prendra alors la valeur de
|
||||
* \p t_in_file à laquelle sera annexé l’extension `.lzw`.
|
||||
*
|
||||
* \param t_in_file Chemin vers le fichier d’entrée
|
||||
* \param t_out_file Chemin vers le fichier de sortie
|
||||
*/
|
||||
void compress(const std::string &t_in_file, const char *t_out_file) {
|
||||
// Fichier d’entrée
|
||||
std::ifstream input_file{t_in_file};
|
||||
|
||||
// Fichier de sortie
|
||||
FILE *out = nullptr;
|
||||
if (t_out_file) {
|
||||
out = fopen(t_out_file, "wb");
|
||||
} else {
|
||||
std::string out_name{t_in_file};
|
||||
out_name.append(".lzw");
|
||||
out = fopen(out_name.c_str(), "wb");
|
||||
}
|
||||
|
||||
input_file.seekg(0, std::ios::end);
|
||||
// string contenant le fichier d’entrée
|
||||
ustring str(static_cast<unsigned long>(input_file.tellg()),
|
||||
static_cast<unsigned char>(0));
|
||||
input_file.seekg(0, std::ios::beg);
|
||||
|
||||
// assignation du contenu du fichier à str
|
||||
str.assign((std::istreambuf_iterator<char>(input_file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
printf("Size of input file: %zu\n", str.size());
|
||||
|
||||
dict_t dictionary{};
|
||||
|
||||
const auto comp_str{lzw_compress(str, dictionary)};
|
||||
|
||||
printf("\n############################################\n");
|
||||
printf(" Compressed!\n");
|
||||
printf("############################################\n\n");
|
||||
printf("Size of compressed string: %zu\n", comp_str.size());
|
||||
printf("Size of dictionary: %zu\n", dictionary.size());
|
||||
printf("Compression ratio: %.10f\n",
|
||||
static_cast<double>(str.size() / comp_str.size()));
|
||||
|
||||
for(const auto c : comp_str)
|
||||
write_utf8(out, c);
|
||||
|
||||
fclose(out);
|
||||
input_file.close();
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -11,8 +11,11 @@
|
||||
|
||||
/// \brief Compression d'une chaine de caractères
|
||||
const std::vector<std::uint32_t>
|
||||
compress(const std::basic_string<uint8_t> &t_text,
|
||||
lzw_compress(const std::basic_string<uint8_t> &t_text,
|
||||
std::map<std::pair<std::uint32_t, std::uint8_t>, std::uint32_t>
|
||||
&t_dictionary);
|
||||
|
||||
/// \brief Wrapper de \ref lzw_compress
|
||||
void compress(const std::string &t_in_file, const char *t_out_file);
|
||||
|
||||
#endif /* LZW_SRC_COMPRESS_H_ */
|
||||
|
144
src/main.cc
144
src/main.cc
@ -6,11 +6,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "utf8.hh"
|
||||
#include "compress.hh"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <streambuf>
|
||||
#include "getopt.h"
|
||||
|
||||
using std::printf;
|
||||
using std::puts;
|
||||
@ -31,53 +28,110 @@ 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() {
|
||||
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");
|
||||
puts("\t-d\tdecompresses the input file to the output file. If no output");
|
||||
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");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
puts("Usage: lzw <filename>");
|
||||
return 1;
|
||||
} else {
|
||||
#ifdef Debug
|
||||
for (int i = 0; i < argc; ++i)
|
||||
printf("argv[%d] = %s\n", i, argv[i]);
|
||||
#endif
|
||||
|
||||
std::string input_path{};
|
||||
std::string output_path{};
|
||||
bool compressing = true;
|
||||
|
||||
while (1) {
|
||||
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'},
|
||||
{0, 0, 0, 0}};
|
||||
int c = getopt_long(argc, argv, "hi:o:cu", long_options, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
switch (c) {
|
||||
case 0: {
|
||||
#ifdef Debug
|
||||
printf("\noption %s", long_options[option_index].name);
|
||||
if (optarg) {
|
||||
printf(" with arg %s\n", optarg);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case 'h': {
|
||||
#ifdef Debug
|
||||
printf("From main - option --help passed\n");
|
||||
#endif
|
||||
help();
|
||||
return 0;
|
||||
}
|
||||
case 'i': {
|
||||
#ifdef Debug
|
||||
printf("From main - option --input with value '%s'\n", optarg);
|
||||
#endif
|
||||
input_path = optarg;
|
||||
break;
|
||||
}
|
||||
case 'o': {
|
||||
#ifdef Debug
|
||||
printf("From main - option --output with value '%s'\n", optarg);
|
||||
#endif
|
||||
output_path = optarg;
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
#ifdef Debug
|
||||
printf("From main - option --compress\n");
|
||||
#endif
|
||||
compressing = true;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
#ifdef Debug
|
||||
printf("From main - option --uncompress\n");
|
||||
#endif
|
||||
compressing = false;
|
||||
break;
|
||||
}
|
||||
case '?': {
|
||||
puts("Error: unknown parameter.");
|
||||
#ifdef Debug
|
||||
printf("From main - option -?\n");
|
||||
#endif
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fichier d'entrée
|
||||
std::ifstream t{argv[1]};
|
||||
if (input_path == "") {
|
||||
puts("Error: no input file specified");
|
||||
return 2;
|
||||
}
|
||||
|
||||
// fichier de sortie
|
||||
FILE *out = fopen("output.lzw", "wb");
|
||||
|
||||
// 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);
|
||||
|
||||
// assigner au string les caractères du fichier
|
||||
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{};
|
||||
|
||||
// compresssion du texte
|
||||
const auto comp_str{compress(str, dictionary)};
|
||||
|
||||
printf("Compressed, size of compressed string: %zu\n", comp_str.size());
|
||||
printf("Compression ratio: %.10f\n",
|
||||
static_cast<double>(str.size()) / static_cast<double>(comp_str.size()));
|
||||
|
||||
printf("Number of custom words in the dictionary: %zu\n", dictionary.size());
|
||||
|
||||
for(const auto c : comp_str)
|
||||
write_utf8(out, c);
|
||||
|
||||
fclose(out);
|
||||
t.close();
|
||||
if (compressing) {
|
||||
compress(input_path, output_path.c_str());
|
||||
} else {
|
||||
puts("Not yet implemented :(");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user