-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompression.cpp
150 lines (129 loc) · 3.26 KB
/
Compression.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "Compression.h"
namespace compression {
// extracts the relevant huffman tree code from the header
int extractTreeSegemntCode(const string& code)
{
unsigned char c = code[0];
int leaves = (int)c;
int branches = (leaves - 1) + leaves;
int compeltion = 0;
if ((branches % 8) > 0)
compeltion = (8 - (branches % 8));
branches = (branches + compeltion) / 8;
int total = 1 + branches + leaves;
return total;
}
string encodeText(const string& txt, Hash& dict)
{
BitStreamer stream;
for (int i = 0; i < txt.length(); i++)
{
stream << dict.getCharObj(txt[i])->getBits();
}
return stream.finish();
}
Heap& getHeap(const string& text)
{
Hash hash(1024);
for (int i = 0; i < text.length(); i++)
hash.handleChar(text[i]);
Heap* heap = new Heap(MIN);
Character** charsArr = hash.getCharsArray();
for (int i = 0; i < hash.getNumOfItems(); i++)
heap->insert(new Node(charsArr[i]->getChar(), charsArr[i]->getFreq()));
for (int i = 0; i < hash.getNumOfItems(); i++)
delete charsArr[i];
delete[] charsArr;
return *heap;
}
string& encode(const string& text)
{
string* encodedText = new string();
Heap* heap = &getHeap(text);
HuffmanTree tree(*heap);
(*encodedText) += tree.buildFullHeader();
(*encodedText) += tree.encode(text);
return (*encodedText);
}
string& decode(const string& encodedText)
{
int len = extractTreeSegemntCode(encodedText);
string treeCode(encodedText.begin(), encodedText.begin() + len);
HuffmanTreeDecoder tree(treeCode);
string encoded(encodedText.begin() + len, encodedText.end());
string* decoded = &tree.decode(encoded);
return *decoded;
}
string& readFromFile(const string& path)
{
ifstream file(path, ios::in | ios::binary);
if (file) {
file.seekg(0, file.end);
int len = file.tellg();
file.seekg(0, file.beg);
char* data = new char[len];
file.read(data, len);
file.close();
string *str = new string(data, len);
return *str;
}
else {
throw "Source file not found";
}
}
void writeToFile(ofstream& file, const string& data)
{
for (int i = 0; i < data.length(); i++)
file.write((char*)&data[i], sizeof(char));
file.close();
}
void compress(const string& data, const string& path)
{
string code = encode(data);
ofstream file(path, ios::out | ios::binary);
writeToFile(file, code);
}
void compressFile(const string& sourceFile, const string& targetFile)
{
string* data;
try {
data = &readFromFile(sourceFile);
compress(*data, targetFile);
}
catch (char* error)
{
cout << error;
}
}
void decompress(const string& data, const string& path)
{
string decodedData = decode(data);
ofstream file(path, ios::out | ios::binary);
writeToFile(file, decodedData);
}
string& decompress(const string& path)
{
string* data;
try {
data = &readFromFile(path);
return decode(*data);
}
catch (char* error)
{
cout << error;
}
}
void decompressFile(const string& sourceFile, const string& targetFile)
{
string* data;
try
{
data = &readFromFile(sourceFile);
decompress(*data, targetFile);
}
catch (char* error)
{
cout << error;
}
}
}