-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (49 loc) · 1.72 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
/* CREATED && DEVELOPED BY SUDARSHAN */
#include <iostream>
#include <fstream>
#include <string>
#include "huffman.hpp"
using namespace std;
int main(int argc, char* argv[]) {
// demonstrates basic usage of file compression and extraction
if (argc != 4) {
cerr << "Usage: " << argv[0] << " <compress|decompress> <inputfile> <outputfile>" << endl;
return 1;
}
string mode = argv[1];
string inputFilename = argv[2];
string outputFilename = argv[3];
if (mode != "compress" && mode != "decompress") {
cerr << "Invalid mode. Use 'compress' or 'decompress'." << endl;
return 1;
}
// Check if the input file exists
ifstream inputFile(inputFilename, ios::binary);
if (!inputFile) {
cerr << "Error: Input file \"" << inputFilename << "\" could not be opened or does not exist." << endl;
return 1;
}
inputFile.close(); // Close the file since it's only being checked for existence
Huffman huffman;
try {
if (mode == "compress") {
// Compress the file
huffman.compressFile(inputFilename, outputFilename);
cout << "File compressed successfully!" << endl;
} else if (mode == "decompress") {
// Decompress the file
huffman.decompressFile(inputFilename, outputFilename);
cout << "File decompressed successfully!" << endl;
}
} catch (const ifstream::failure& e) {
cerr << "File error: " << e.what() << endl;
return 1;
} catch (const runtime_error& e) {
cerr << "Runtime error: " << e.what() << endl;
return 1;
} catch (...) {
cerr << "An unexpected error occurred." << endl;
return 1;
}
return 0;
}