-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzw.cpp
69 lines (65 loc) · 2.11 KB
/
lzw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#define _ITERATOR_DEBUG_LEVEL 0
#include <iostream>
#include <fstream>
#include <cstring>
#include "lzw_streambase.h"
#include "lzw-a.h"
#include "lzw.h"
void usage()
{
std::cerr <<
"Usage:\n"
"lzw [-max max_code] -c input output #compress file input to file output\n"
"lzw [-max max_code] -c - output #compress stdin to file otuput\n"
"lzw [-max max_code] -c input #compress file input to stdout\n"
"lzw [-max max_code] -c #compress stdin to stdout\n"
"lzw [-max max_code] -d input output #decompress file input to file output\n"
"lzw [-max max_code] -d - output #decompress stdin to file otuput\n"
"lzw [-max max_code] -d input #decompress file input to stdout\n"
"lzw [-max max_code] -d #decompress stdin to stdout\n";
exit(1);
}
int main(int argc, char* argv[])
{
int max_code = 32767;
if ( argc >= 2 && !strcmp( "-max", argv[1] ) ) {
if ( sscanf( argv[2], "%d", &max_code ) != 1 )
usage();
argc -= 2;
argv += 2;
}
if ( argc < 2 )
usage();
bool compress;
if ( std::string( "-c" ) == argv[1] )
compress = true;
else if ( std::string( "-d" ) == argv[1] )
compress = false;
else
usage();
std::istream *in = &std::cin;
std::ostream *out = &std::cout;
bool delete_instream = false;
bool delete_ostream = false;
if ( argc == 3 ) {
in = new std::ifstream( argv[2] );
delete_instream = true;
}
if ( argc == 4 ) {
out = new std::ofstream( argv[3] );
delete_ostream = true;
if ( std::string("-") != argv[2] ) {
in = new std::ifstream( argv[2] );
delete_instream = true;
}
}
if ( compress )
lzw::compress( *in, *out, max_code );
else
lzw::decompress( *in, *out, max_code );
if ( delete_instream )
delete in;
if ( delete_ostream )
delete out;
return 0;
}