From 67a88aaf91040ce7b685b1b67471ef1204eef768 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Mon, 11 Jun 2018 21:02:59 +0200 Subject: [PATCH] bug fixed as input stream was char and not unsigned char --- src/bitpack.cc | 5 ++++- src/common.cc | 11 ++++++++++- src/compress.cc | 26 ++++++++++++++++---------- src/compress.hh | 3 ++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/bitpack.cc b/src/bitpack.cc index d9f4421..fcc819f 100644 --- a/src/bitpack.cc +++ b/src/bitpack.cc @@ -29,7 +29,10 @@ using ustring = std::basic_string; */ [[nodiscard]] vuchar pack_n(const vuint16::const_iterator t_input_begin, - const vuint16::const_iterator t_input_end, const int t_n) { + const vuint16::const_iterator t_input_end, const int t_n) { +#ifdef Debug + std::printf("%d bits!\n", t_n); +#endif if (t_n == 16) { return pack_16(t_input_begin, t_input_end); } diff --git a/src/common.cc b/src/common.cc index 056a01d..3124caf 100644 --- a/src/common.cc +++ b/src/common.cc @@ -65,8 +65,11 @@ ustring dico_uncompress(std::map &t_dict, const uint16_t t_code, const uint16_t t_old) { // le code existe dans le dictionnaire s’il est < 256 if (t_code < 256) { - +#ifdef Debug + std::printf("Code : %d\tNOT EMPTY\n", t_code); +#endif ustring e{static_cast(t_code)}; + // 256 car on n'a pas encore tenté d'insérer de nouveau caractère if (t_old < 256) { t_dict[static_cast(t_dict.size() + 256)] = static_cast(t_old) + e; @@ -82,6 +85,9 @@ ustring dico_uncompress(std::map &t_dict, // le code existe dans le dictionnaire if (!e.empty()) { +#ifdef Debug + std::printf("Code : %d\tNOT EMPTY\n", t_code); +#endif str += e[0]; const uint16_t index = static_cast(t_dict.size() + 256); t_dict[index] = str; @@ -89,6 +95,9 @@ ustring dico_uncompress(std::map &t_dict, } // le code n'existe pas encore dans le dictionnaire +#ifdef Debug + std::printf("Code : %d\tEMPTY\n", t_code); +#endif str += str[0]; e = str; t_dict[t_code] = e; diff --git a/src/compress.cc b/src/compress.cc index a8ea9cf..50360ec 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -15,7 +15,7 @@ using std::uint8_t; using std::vector; using vuint16 = vector; using vvuint16 = vector; -using std::string; +using ustring = std::basic_string; using dict_t = std::map, uint16_t>; using std::printf; @@ -33,7 +33,7 @@ const size_t DICT_MAX = static_cast(ipow(2, 17) - 256); /* 16 bits */ * \param t_text Chaîne de caractères uint8_t représentant le fichier d'entrée * \return Vecteur de chunks (vecteurs de uint16_t) */ -vvuint16 lzw_compress(string &&t_text) { +vvuint16 lzw_compress(ustring &&t_text) { std::puts("Compressing..."); uint16_t w = 0xFFFF; vuint16 chunk{}; @@ -49,7 +49,7 @@ vvuint16 lzw_compress(string &&t_text) { if (const auto &[yes, pos] = dico(dict, w, static_cast(c)); yes) { w = pos; } else { - chunk.push_back(static_cast(w)); + chunk.push_back(w); w = static_cast(c); } } @@ -70,20 +70,26 @@ vvuint16 lzw_compress(string &&t_text) { * \param[in] t_out_file Chemin vers le fichier de sortie */ void compress(const std::string &t_in_file, const char *t_out_file) { - std::ifstream input_file{t_in_file}; - assert(input_file.is_open()); + FILE *const input_file = fopen(t_in_file.c_str(), "rb"); + assert(input_file); FILE *const out = (t_out_file != nullptr) ? fopen(t_out_file, "wb") : fopen("output.lzw", "wb"); if (out == nullptr) { std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4 << ": could not open output file. Aborting...\n"; - input_file.close(); + // input_file.close(); + std::fclose(input_file); exit(1); } - const auto compressed_text{ - lzw_compress(std::string{std::istreambuf_iterator(input_file), - std::istreambuf_iterator()})}; + + std::fseek(input_file, 0L, SEEK_END); + const auto file_size = static_cast(ftell(input_file)); + std::rewind(input_file); + + auto raw_text = std::make_unique(file_size); + std::fread(raw_text.get(), sizeof(unsigned char), file_size, input_file); + const auto compressed_text(lzw_compress(ustring{raw_text.get(), &raw_text[file_size]})); write_file(out, compressed_text); fclose(out); - input_file.close(); + fclose(input_file); } diff --git a/src/compress.hh b/src/compress.hh index 7e0089d..4aa4c75 100644 --- a/src/compress.hh +++ b/src/compress.hh @@ -12,7 +12,8 @@ #include /// \brief Compression d'une chaine de caractères -std::vector> lzw_compress(std::string &&); +std::vector> +lzw_compress(std::basic_string &&); /// \brief Wrapper de \ref lzw_compress void compress(const std::string &, const char *);