-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompressor.cpp
155 lines (125 loc) · 3.74 KB
/
decompressor.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
151
152
153
154
155
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cstring>
#include <map>
#include <queue>
#include "huffman_utility.h"
using namespace std;
map<char, int> getFreqMap(ifstream &sourceFileHeader)
{
map<char, int> freqMap;
string line;
while (getline(sourceFileHeader, line))
{
if (line == "EOH")
break;
if (line.substr(0, 3) == "EOL")
{
freqMap['\n'] = stoi(line.substr(3));
continue;
}
char character = '\0';
string frequency = "";
if (isdigit(line[1]))
{
if (line[0] != '\0')
character = line[0];
frequency = line.substr(1);
}
cout << character << " " << frequency << endl;
freqMap[character] = stoi(frequency);
}
return freqMap;
}
void decompressFile(ifstream &sourceFile, ofstream &destinationFile, huffmanNode *huffmanTreeRoot)
{
// read bit by bit
// one char = 8 bits
char ch;
huffmanNode *currNode = huffmanTreeRoot;
cout << "decompressing file\n";
while (sourceFile.get(ch))
{
// bits have reverse ordering
// 1 1 0 1 0 1 0 0 <--- bits as placed in file
// 7 6 5 4 3 2 1 0 <--- ordering
cout << ch << " at this character.\n";
for (int i = 7; i >= 0; i--)
{
bool bit = ch & (1 << i); // check if ith bit is set or unset
cout << bit << " ";
if (bit)
currNode = currNode->right;
else
currNode = currNode->left;
if (currNode == NULL)
{
cout << "Error: Reached a null node while decompressing";
return;
}
if (currNode->character != '\0')
{
cout << currNode->character;
destinationFile << currNode->character;
currNode = huffmanTreeRoot;
}
}
}
return;
}
void printTree(huffmanNode *root)
{
if (root == NULL)
{
return;
}
queue<huffmanNode *> q;
q.push(root);
while (!q.empty())
{
int size = q.size();
while (size--)
{
(q.front()->character == '\0') ? cout << "null" << " " : cout << q.front()->character << " ";
if (q.front()->left)
q.push(q.front()->left);
if (q.front()->right)
q.push(q.front()->right);
q.pop();
}
cout << endl;
}
}
void decompress()
{
string sourceFileName, destinationFileName;
cout << "Please enter the name of file to be decompressed: ";
cin >> sourceFileName;
cout << "Please give a name for decompressed file: ";
cin >> destinationFileName;
ifstream sourceFileHeader(sourceFileName, ios::in | ios::binary);
map<char, int> freqMap = getFreqMap(sourceFileHeader);
streampos actualDataPosition = sourceFileHeader.tellg();
cout << "actualDataPosition: " << actualDataPosition << endl;
sourceFileHeader.close();
huffmanNode *huffmanTreeRoot = buildHuffmanTree(freqMap);
cout << "HuffMan Tree while decompressing\n";
printTree(huffmanTreeRoot);
ifstream sourceFile(sourceFileName, ios::in | ios::binary);
ofstream destinationFile(destinationFileName, std::ios::out | ios::binary | ios::app);
if (!sourceFile.is_open())
{
std::cerr << "Error: Could not open source file " << endl;
return;
}
if (!destinationFile.is_open())
{
std::cerr << "Error: Could not open destination file " << endl;
return;
}
sourceFile.seekg(actualDataPosition, ios::beg);
decompressFile(sourceFile, destinationFile, huffmanTreeRoot);
sourceFile.close();
destinationFile.close();
}