LZW file compressor.
More...
#include <algorithm>
#include <array>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
Go to the source code of this file.
|
using | CodeType = std::uint16_t |
| Type used to store and retrieve codes.
|
|
|
void | compress (std::istream &is, std::ostream &os) |
| Compresses the contents of is and writes the result to os . More...
|
|
void | decompress (std::istream &is, std::ostream &os) |
| Decompresses the contents of is and writes the result to os . More...
|
|
void | print_usage (const std::string &s="", bool su=true) |
| Prints usage information and a custom error message. More...
|
|
int | main (int argc, char *argv[]) |
| Actual program entry point. More...
|
|
|
const CodeType | globals::dms {std::numeric_limits<CodeType>::max()} |
| Dictionary Maximum Size (when reached, the dictionary will be reset)
|
|
LZW file compressor.
- Author
- Julius Pettersson
- Copyright
- MIT/Expat License.
- Version
- 5
This is the C++11 implementation of a Lempel-Ziv-Welch single-file command-line compressor. It uses the simpler fixed-width code compression method. It was written with Doxygen comments.
- See Also
- http://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
-
http://marknelson.us/2011/11/08/lzw-revisited/
-
http://www.cs.duke.edu/csed/curious/compression/lzw.html
-
http://warp.povusers.org/EfficientLZW/index.html
-
http://en.cppreference.com/
-
http://www.doxygen.org/
Definition in file lzw_v5.cpp.
void compress |
( |
std::istream & |
is, |
|
|
std::ostream & |
os |
|
) |
| |
void decompress |
( |
std::istream & |
is, |
|
|
std::ostream & |
os |
|
) |
| |
Decompresses the contents of is
and writes the result to os
.
- Parameters
-
[in] | is | input stream |
[out] | os | output stream |
Definition at line 211 of file lzw_v5.cpp.
Referenced by main().
213 std::vector<std::pair<CodeType, char>> dictionary;
216 const auto reset_dictionary = [&dictionary] {
218 dictionary.reserve(globals::dms);
220 const long int minc = std::numeric_limits<char>::min();
221 const long int maxc = std::numeric_limits<char>::max();
223 for (
long int c = minc; c <= maxc; ++c)
224 dictionary.push_back({globals::dms, static_cast<char> (c)});
227 const auto rebuild_string = [&dictionary](
CodeType k) ->
const std::vector<char> * {
228 static std::vector<char> s;
233 s.reserve(globals::dms);
235 while (k != globals::dms)
237 s.push_back(dictionary[k].second);
238 k = dictionary[k].first;
241 std::reverse(s.begin(), s.end());
250 while (is.read(reinterpret_cast<char *> (&k), sizeof (
CodeType)))
253 if (dictionary.size() == globals::dms)
256 if (k > dictionary.size())
257 throw std::runtime_error(
"invalid compressed code");
259 const std::vector<char> *s;
261 if (k == dictionary.size())
263 dictionary.push_back({i, rebuild_string(i)->front()});
264 s = rebuild_string(k);
268 s = rebuild_string(k);
270 if (i != globals::dms)
271 dictionary.push_back({i, s->front()});
274 os.write(&s->front(), s->size());
278 if (!is.eof() || is.gcount() != 0)
279 throw std::runtime_error(
"corrupted compressed file");
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Actual program entry point.
- Parameters
-
| argc | number of command line arguments |
[in] | argv | array of command line arguments |
- Return values
-
EXIT_FAILURE | for failed operation |
EXIT_SUCCESS | for successful operation |
Definition at line 313 of file lzw_v5.cpp.
References compress(), decompress(), and print_usage().
328 if (std::string(argv[1]) ==
"-c")
331 if (std::string(argv[1]) ==
"-d")
332 m = Mode::Decompress;
335 print_usage(std::string(
"flag `") + argv[1] +
"' is not recognized.");
339 const std::size_t buffer_size {1024 * 1024};
342 const std::unique_ptr<char[]> input_buffer(
new char[buffer_size]);
343 const std::unique_ptr<char[]> output_buffer(
new char[buffer_size]);
345 std::ifstream input_file;
346 std::ofstream output_file;
348 input_file.rdbuf()->pubsetbuf(input_buffer.get(), buffer_size);
349 input_file.open(argv[2], std::ios_base::binary);
351 if (!input_file.is_open())
353 print_usage(std::string(
"input_file `") + argv[2] +
"' could not be opened.");
357 output_file.rdbuf()->pubsetbuf(output_buffer.get(), buffer_size);
358 output_file.open(argv[3], std::ios_base::binary);
360 if (!output_file.is_open())
362 print_usage(std::string(
"output_file `") + argv[3] +
"' could not be opened.");
368 input_file.exceptions(std::ios_base::badbit);
369 output_file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
371 if (m == Mode::Compress)
374 if (m == Mode::Decompress)
377 catch (
const std::ios_base::failure &f)
379 print_usage(std::string(
"File input/output failure: ") + f.what() +
'.',
false);
382 catch (
const std::exception &e)
384 print_usage(std::string(
"Caught exception: ") + e.what() +
'.',
false);
void print_usage |
( |
const std::string & |
s = "" , |
|
|
bool |
su = true |
|
) |
| |
Prints usage information and a custom error message.
- Parameters
-
s | custom error message to be printed |
su | Show Usage information |
Definition at line 287 of file lzw_v5.cpp.
Referenced by main().
290 std::cerr <<
"\nERROR: " << s <<
'\n';
294 std::cerr <<
"\nUsage:\n";
295 std::cerr <<
"\tprogram -flag input_file output_file\n\n";
296 std::cerr <<
"Where `flag' is either `c' for compressing, or `d' for decompressing, and\n";
297 std::cerr <<
"`input_file' and `output_file' are distinct files.\n\n";
298 std::cerr <<
"Examples:\n";
299 std::cerr <<
"\tlzw_v5.exe -c license.txt license.lzw\n";
300 std::cerr <<
"\tlzw_v5.exe -d license.lzw new_license.txt\n";
303 std::cerr << std::endl;