-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (101 loc) · 2.77 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
99
100
101
102
103
104
105
106
107
108
109
110
#include "huffman.h"
int main()
{
int y;
HuffmanCode tree;
ifstream fin;
ofstream fout;
string inputfile, outputfile;
cout << "\n\t====================================";
cout << "\n\t\t Text File Compressor\n";
cout << "\t====================================";
cout << "\n\nEnter Name of File to Compress : ";
cin >> inputfile;
if (!filesystem::exists(inputfile))
{
cout << "Error !!! File does not exist.\n";
cin >> y;
exit(1);
}
// Create output file by replacing the input file extension with "cmp"
outputfile = inputfile.substr(0, inputfile.find_last_of('.')) + ".cmp";
try
{
const auto fsize = filesystem::file_size(inputfile);
// std::cout << fsize << '\n';
}
catch (const filesystem::filesystem_error& err)
{
std::cerr << "filesystem error! " << err.what() << '\n';
if (!err.path1().empty())
std::cerr << "path1: " << err.path1().string() << '\n';
if (!err.path2().empty())
std::cerr << "path2: " << err.path2().string() << '\n';
cin >> y;
exit(1);
}
catch (const std::exception& ex)
{
std::cerr << "general exception: " << ex.what() << '\n';
cin >> y;
exit(1);
}
fin.open(inputfile);
if (!fin.is_open())
{
cout << "Error !!! Cannot open Source file.\n";
cin >> y;
exit(1);
}
fout.open(outputfile, ios::binary);
if (!fout.is_open())
{
fin.close();
cout << "Error !!! Cannot open Destination file.\n";
cin >> y;
exit(1);
}
tree.CompressFile(fin, fout);
tree.PrintCodeTable();
cout << "----------------------------------------------------------------------------------- " << endl;
cout << "Active characters: " << tree.GetGetAlphabetCount() << "\t\tTotal characters: " << tree.GetTotalCharacters() << endl;
cout << "----------------------------------------------------------------------------------- " << endl;
fin.close();
fout.close();
// Reset the file date
tree.ClearCodeTable();
tree.ClearHuffmanTree();
// Open compressed file
fin.open(outputfile, ios::binary);
if (!fin.is_open())
{
cout << "Error !!! Cannot open Source file.\n";
cin >> y;
exit(1);
}
// Open destination file
fout.open("uncompressed.txt", ios::binary);
if (!fout.is_open())
{
fin.close();
cout << "Error !!! Cannot create Output file.\n";
cin >> y;
exit(1);
}
// Decode it
if (!tree.ExpandFile(fin, fout))
{
fin.close();
fout.close();
cout << "Error !!! Input file, incorrect file format file.\n";
cin >> y;
exit(1);
};
tree.PrintCodeTable();
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " << endl;
cout << "Active characters: " << tree.GetGetAlphabetCount() << "\t\tTotal characters: " << tree.GetTotalCharacters() << endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " << endl;
fin.close();
fout.close();
return 0;
}