-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
77 lines (72 loc) · 1.93 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
#include "huffman.hpp"
/**
* getExtension takes the full path of the input file and returns
* the extension of the file (it has to be only 3 characters)
*
* @param path is the full path of the input file
* @return string that returns the extension (i.e. .aaa)
*/
inline std::string getExtension(const std::string& path)
{
std::string::size_type index = path.rfind(".");
if (index != std::string::npos && index+3 < path.length())
{
return path.substr(index, 4);
}
else
{
return "";
}
}
int main(int argc, char** argv)
{
if (argc == 1)
{
std::cerr << "You have to Pass the file to be encoded or decoded.\n";
}
else
{
std::string path = argv[1];
std::string::size_type index = std::string(argv[1]).rfind("/");
std::string fileName = "";
fileName.append(path.begin()+ index + 1,path.end() - 4);
HuffmanCoding H(fileName);
if (argc == 2)
{
if (getExtension(path) == ".pgm")
{
H.encode(path);
}
else
{
std::cerr << "Please Input .pgm file to be encoded\nor a .huf and .frq files to be decoded";
}
}
else if(argc == 3)
{
if (getExtension(argv[1]) == ".huf")
{
if(getExtension(argv[2]) == ".frq")
{
H.decode(argv[1], argv[2]);
}
else
{
std::cerr << "Please Input the .frq file";
}
}
else if (getExtension(argv[1]) == ".frq")
{
if(getExtension(argv[2]) == ".huf")
{
H.decode(argv[2], argv[1]);
}
else
{
std::cerr << "Please Input the .huf file";
}
}
}
}
return 0;
}