LZW file compressor.
More...
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
#include <map>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
Go to the source code of this file.
|
using | CodeType = std::uint16_t |
| Type used to store and retrieve codes.
|
|
|
std::vector< char > | operator+ (std::vector< char > vc, char c) |
| Helper operator intended to simplify code. More...
|
|
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
- 1
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_v1.cpp.
void compress |
( |
std::istream & |
is, |
|
|
std::ostream & |
os |
|
) |
| |
Compresses the contents of is
and writes the result to os
.
- Parameters
-
[in] | is | input stream |
[out] | os | output stream |
Definition at line 61 of file lzw_v1.cpp.
Referenced by main().
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));
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 117 of file lzw_v1.cpp.
Referenced by main().
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");
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 192 of file lzw_v1.cpp.
References compress(), decompress(), and print_usage().
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);
std::vector<char> operator+ |
( |
std::vector< char > |
vc, |
|
|
char |
c |
|
) |
| |
Helper operator intended to simplify code.
- Parameters
-
vc | original vector |
c | element to be appended |
- Returns
- vector resulting from appending
c
to vc
Definition at line 50 of file lzw_v1.cpp.
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 166 of file lzw_v1.cpp.
Referenced by main().
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;