-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.cpp
More file actions
41 lines (36 loc) · 1.29 KB
/
cli.cpp
File metadata and controls
41 lines (36 loc) · 1.29 KB
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
extern void huffmanEncode(string inFile);
extern void huffmanDecode(string inFile);
int main(int argc, char *argv[]) {
if (argc != 3) { // length check
cout << "usage ./huffman <mode> <file>" << endl;
cout << "<mode> = --encode OR --decode" << endl;
return -1;
}
string mode(argv[1]);
string inFile(argv[2]);
if (mode == "--encode" || mode == "--decode") {
ifstream file(inFile);
if (!file) { // valid file check
std::cerr << "Error: File not found\n";
return 1;
}
file.close();
if (mode == "--encode") { // --Encoding--
huffmanEncode(inFile);
string str(inFile);
string outFile = str.substr(0, str.find(".txt")) + string(".huff");
cout << "file encoded and output to " << outFile << endl;
} else if (mode == "--decode") { // --Decoding--
huffmanDecode(inFile);
string str(inFile);
string outFile = str.substr(0, str.find(".huff")) + string(".txt.uncomp");
cout << "file decoded and output to " << outFile << endl;
}
} else { // valid mode check
cout << "usage ./huffman <mode> <file>" << endl;
}
}