-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
102 lines (90 loc) · 2.12 KB
/
decoder.cpp
File metadata and controls
102 lines (90 loc) · 2.12 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
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
#include "decoder.h"
Decoder::Decoder(){};
Decoder::~Decoder(){};
//Read Histogram From Encoded File
void Decoder::readHistogram(ifstream &fin)
{
int input;
for(int i = -1; i < 128; i++)
{
fin>>input;
histogram[i] = input;
}
}
//getter for histogram
unordered_map<char,int> Decoder::getHistogram()
{
return histogram;
}
//Setter for huffman codes
void Decoder::setCodes(unordered_map<char,string> codes)
{
unordered_map<char,string>::iterator it;
for(it = codes.begin(); it != codes.end(); it++)
{
huffmanCodes[it->second] = it->first;
}
}
//Generate Decoded file from encoded file
void Decoder::generateDecodedFile(ifstream &fin,ofstream &fout)
{
unsigned char input;
string temp = "";
string binary = "";
bool end = false;
//first character is not needed since text files contain character 10 in the begining
fin>>noskipws>>input;
bytesRead++;
//no skips because space maybe a character
while(fin>>noskipws>>input && end == false)
{
bytesRead++;
//look for set bits in the char and convert to binary
for(int i = 0;i < 8; i++)
{
if(((1<<i) & input) != 0)
{
temp += '1';
}
else
{
temp += '0';
}
}
//Reverse and add onto binary code
reverse(temp.begin(),temp.end());
temp.erase(0,1);
binary += temp;
//loop through entire code looking for particular code
//If found print that character to screen and erase that section of code
for(unsigned int i = 0; i < binary.size(); i++)
{
string checkForCode = binary.substr(0,i);
if(huffmanCodes.count(checkForCode) != 0)
{
if(huffmanCodes[checkForCode] == -1)
{
end = true;
}
else
{
binary.erase(0,i);
i = 0;
char output = huffmanCodes[checkForCode];
fout<<output;
bytesWritten++;
}
}
}
temp.clear();
}
}
void Decoder:: printDecoderInformation()
{
cout<<"Huffman Decoder"<<endl;
cout<<"-----------------------------------"<<endl;
cout<<"Bytes Read "<<bytesRead<<endl;
cout<<"Bytes Written "<<bytesWritten<<endl;
cout<<"Compression Ratio is "<<double(bytesRead)/double(bytesWritten)*100<<" percent"<<endl;
cout<<"-----------------------------------"<<endl;
}