-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
98 lines (81 loc) · 1.67 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "huffman.h"
#include <iostream>
#include <string>
Size_t
filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::in | std::ifstream::binary);
in.seekg(0, std::ifstream::end);
return in.tellg();
}
static void
usage()
{
std::cerr << "Usage: [-c|-d] [-i <input file>] [-o <output file>]"
<< std::endl
<< "-i - input file"
<< std::endl
<< "-o - output file"
<< std::endl
<< "-d - decompress"
<< std::endl
<< "-c - compress (default)"
<< std::endl;
}
int
main(int argc,
char** argv)
{
char compress = 1;
const char *file_in = NULL, *file_out = NULL;
std::ifstream in;
std::ofstream out;
if (argc < 6)
{
usage();
return 1;
}
/* Get the command line arguments. */
std::string i = "-i",
o = "-o",
c = "-c",
d = "-d";
for (int j = 1; j < argc; ++j)
{
std::string s(argv[j]);
if (s == i)
file_in = argv[++j];
else if (s == o)
file_out = argv[++j];
else if (s == c)
compress = 1;
else if (s == d)
compress = 0;
}
Size_t size = filesize(file_in);
if (file_in)
{
in.open(file_in, std::ios::in | std::ios::binary);
if (!in)
{
std::cerr << "Can't open input file "
<< file_in
<< std::endl;
return 1;
}
}
if (file_out)
{
out.open(file_out, std::ios::out | std::ios::binary);
if (!out)
{
std::cerr << "Can't open output file "
<< file_out
<< std::endl;
return 1;
}
}
compress ?
huffmanEncodeFile(in, out, size) : huffmanDecodeFile(in, out);
return 0;
}