-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTextDecoder.cpp
More file actions
159 lines (133 loc) · 4.18 KB
/
TextDecoder.cpp
File metadata and controls
159 lines (133 loc) · 4.18 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
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
151
152
153
154
155
156
157
158
159
#include "TextDecoder.h"
HuffmanTree::HuffmanTree(char character)
{
this->character = character;
leftNode = NULL;
rightNode = NULL;
}
HuffmanTree::HuffmanTree()
{
root = NULL;
leftNode = NULL;
rightNode = NULL;
}
//insert nodes in a tree by creating new ones or by passing by them
void HuffmanTree::insertNode(char character, string code)
{
HuffmanTree *ptr;
if (root == NULL)
{
HuffmanTree *node = new HuffmanTree('#');
root = node;
ptr = root;
}
else ptr = root;
for (int i = 0; i < code.size(); i++)
{
char nodeChar = ' ';
//checking if we reached the end of the tree
if (i == code.size() - 1) nodeChar = character;
else nodeChar = '#';
//go left or right depending on the code char
if (code[i] == '0')
{
if (ptr->leftNode == nullptr)
{
HuffmanTree *node = new HuffmanTree(nodeChar);
ptr->leftNode = node;
ptr = ptr->leftNode;
}
else ptr = ptr->leftNode;
}
else
{
if (ptr->rightNode == nullptr)
{
HuffmanTree *node = new HuffmanTree(nodeChar);
ptr->rightNode = node;
ptr = ptr->rightNode;
}
else ptr = ptr->rightNode;
}
}
}
//traverse the tree decoding the message to it's origin
string HuffmanTree::decodeString(string code)
{
HuffmanTree *ptr = root;
string result = "";
ptr = root;
for (int i = 0; i < code.size(); i++)
{
if (code[i] == '0') ptr = ptr->leftNode;
else ptr = ptr->rightNode;
if (ptr->rightNode == nullptr && ptr->leftNode == nullptr)
{
result += ptr->character;
ptr = root;
}
}
return result;
}
//return a decoded string using huffmanCodes and the encodedString
string Decoder::decodeText(map<char, string> huffmanCodesTable, string encodedString)
{
HuffmanTree Tree;
map<char, string>::iterator it;
for (it = huffmanCodesTable.begin(); it != huffmanCodesTable.end(); it++)
Tree.insertNode(it->first, it->second);
return Tree.decodeString(encodedString);
}
//return a decoded string of the encoded text file location
string Decoder::decodeTextInFile(string fileLocation, string outputFileLocation)
{
StringCompressor compressor;
map<char, string> huffmanMapCodes;
pair<char, string> huffmanMapPair;
string encodedText, readLine, charCode;
ifstream readHuffmanFile;
readHuffmanFile.open(fileLocation);
if (readHuffmanFile.is_open())
{
//Reading the binarystream
char c;
while (readHuffmanFile.get(c)) //Read the file character by character.
{
encodedText.push_back(c); //Add the character to the string
int txtSz = encodedText.size();
if (encodedText[txtSz - 1] == 'D' && encodedText[txtSz - 2] == 'N' && encodedText[txtSz - 3] == 'E') break;
//Keep reading until the string "END" is met
}
for (int i = 0; i < 3; i++)
encodedText.pop_back(); //Pop "END" from the stream
// Read the map line by line
while (getline(readHuffmanFile, readLine))
{
int p = 0;
string keyString, valueString;
keyString += readLine[0]; //Read the first character
for (p = 1; p < readLine.size(); p++)
{
if (readLine[p] == '\t') break; //Keep reading the key until you meet the tab delimiter
keyString += readLine[p];
}
if (keyString == "\\n") keyString = "\n"; //Convert the "\n" string to a newline character
for (int i = p + 1; i < readLine.size(); i++)
valueString.push_back(readLine[i]); //Keep reading the rest of the value after the tab character
huffmanMapPair.first = keyString[0];
huffmanMapPair.second = valueString;
huffmanMapCodes.insert(huffmanMapPair);
}
}
else cout << "Couldn't open the file" << endl;
readHuffmanFile.close();
// cout << encodedText << endl;
encodedText = compressor.asciitoBinaryStream(encodedText);
// cout << encodedText << endl;
map<char, string>::iterator it;
// for (it = huffmanMapCodes.begin(); it != huffmanMapCodes.end(); it++)
// cout << it->first << " => " << it->second << endl;
ofstream decodedFile(outputFileLocation);
decodedFile << decodeText(huffmanMapCodes, encodedText);
return decodeText(huffmanMapCodes, encodedText);
}