40 const CodeType dms {std::numeric_limits<CodeType>::max()};
50 std::vector<char>
operator + (std::vector<char> vc,
char c)
61 void compress(std::istream &is, std::ostream &os)
63 std::map<std::vector<char>,
CodeType> dictionary;
66 const auto reset_dictionary = [&dictionary] {
69 const long int minc = std::numeric_limits<char>::min();
70 const long int maxc = std::numeric_limits<char>::max();
72 for (
long int c = minc; c <= maxc; ++c)
76 const CodeType dictionary_size = dictionary.size();
78 dictionary[{
static_cast<char> (c)}] = dictionary_size;
90 if (dictionary.size() == globals::dms)
95 if (dictionary.count(s) == 0)
99 const CodeType dictionary_size = dictionary.size();
101 dictionary[s] = dictionary_size;
103 os.write(reinterpret_cast<const char *> (&dictionary.at(s)),
sizeof (
CodeType));
109 os.write(reinterpret_cast<const char *> (&dictionary.at(s)),
sizeof (
CodeType));
119 std::vector<std::vector<char>> dictionary;
122 const auto reset_dictionary = [&dictionary] {
124 dictionary.reserve(globals::dms);
126 const long int minc = std::numeric_limits<char>::min();
127 const long int maxc = std::numeric_limits<char>::max();
129 for (
long int c = minc; c <= maxc; ++c)
130 dictionary.push_back({static_cast<char> (c)});
138 while (is.read(reinterpret_cast<char *> (&k), sizeof (
CodeType)))
141 if (dictionary.size() == globals::dms)
144 if (k > dictionary.size())
145 throw std::runtime_error(
"invalid compressed code");
147 if (k == dictionary.size())
148 dictionary.push_back(s + s.front());
151 dictionary.push_back(s + dictionary.at(k).front());
153 os.write(&dictionary.at(k).front(), dictionary.at(k).size());
154 s = dictionary.at(k);
157 if (!is.eof() || is.gcount() != 0)
158 throw std::runtime_error(
"corrupted compressed file");
169 std::cerr <<
"\nERROR: " << s <<
'\n';
173 std::cerr <<
"\nUsage:\n";
174 std::cerr <<
"\tprogram -flag input_file output_file\n\n";
175 std::cerr <<
"Where `flag' is either `c' for compressing, or `d' for decompressing, and\n";
176 std::cerr <<
"`input_file' and `output_file' are distinct files.\n\n";
177 std::cerr <<
"Examples:\n";
178 std::cerr <<
"\tlzw_v1.exe -c license.txt license.lzw\n";
179 std::cerr <<
"\tlzw_v1.exe -d license.lzw new_license.txt\n";
182 std::cerr << std::endl;
192 int main(
int argc,
char *argv[])
207 if (std::string(argv[1]) ==
"-c")
210 if (std::string(argv[1]) ==
"-d")
211 m = Mode::Decompress;
214 print_usage(std::string(
"flag `") + argv[1] +
"' is not recognized.");
218 std::ifstream input_file(argv[2], std::ios_base::binary);
220 if (!input_file.is_open())
222 print_usage(std::string(
"input_file `") + argv[2] +
"' could not be opened.");
226 std::ofstream output_file(argv[3], std::ios_base::binary);
228 if (!output_file.is_open())
230 print_usage(std::string(
"output_file `") + argv[3] +
"' could not be opened.");
236 input_file.exceptions(std::ios_base::badbit);
237 output_file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
239 if (m == Mode::Compress)
242 if (m == Mode::Decompress)
245 catch (
const std::ios_base::failure &f)
247 print_usage(std::string(
"File input/output failure: ") + f.what() +
'.',
false);
250 catch (
const std::exception &e)
252 print_usage(std::string(
"Caught exception: ") + e.what() +
'.',
false);