-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (70 loc) · 2.33 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
//
//Created by Nandan S
//
#include <iostream>
#include "huffman.hpp"
using namespace std;
std::ifstream::pos_type filesize(const char* filename);
int main()
{
string workingMode;
string inpath;
string outpath;
cout << "Enter the working mode:";
cin >> workingMode;
if (workingMode == "compress")
{
cout << "Enter file's path which you want to compress:";
cin >> inpath;
ifstream inFile(inpath);
if (!inFile)
{
cerr << "Input file does not exist." << endl;
return 1;
}
inFile.close();
cout << "Enter path where you want to store the compressed file:";
cin >> outpath;
clock_t tStart = clock();
huffman f(inpath, outpath);
f.compress();
const char* inchararr = inpath.c_str();
const char* outchararr = outpath.c_str();
cout << "Time taken: " << (1.0 * (clock() - tStart) / CLOCKS_PER_SEC) << "sec" << endl;
cout << "Input File Size : " << filesize(inchararr) << " bytes." << endl;
cout << "Compressed File Size : " << filesize(outchararr) << " bytes." << endl;
cout << "Compression Ratio : " << (1.0 * filesize(outchararr) / filesize(inchararr)) << endl;
}
else if (workingMode == "decompress")
{
cout << "Enter file's path which you want to decompress:";
cin >> inpath;
ifstream inFile(inpath);
if (!inFile)
{
cerr << "Input file does not exist." << endl;
return 1;
}
inFile.close();
cout << "Enter path where you want to store the decompressed file:";
cin >> outpath;
clock_t tStart = clock();
huffman f(inpath, outpath);
f.decompress();
const char* inchararr = inpath.c_str();
const char* outchararr = outpath.c_str();
cout << "Time taken: " << (1.0 * (clock() - tStart) / CLOCKS_PER_SEC) << "sec" << endl;
cout << "Input File (Compressed) Size : " << filesize(inchararr) << " bytes." << endl;
cout << "DeCompressed File Size : " << filesize(outchararr) << " bytes." << endl;
}
else
{
cout << "Invalid input!";
}
return 0;
}
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}