-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.cpp
More file actions
114 lines (86 loc) · 2.29 KB
/
Decoder.cpp
File metadata and controls
114 lines (86 loc) · 2.29 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
#include "Decoder.h"
void Decoder::DecodeSimpleCoded(std::string fileName, std::string outFileName)
{
std::ifstream input;
input.open(fileName, std::ios_base::in | std::ios_base::binary);
std::string fileExtension;
input >> fileExtension;
std::ofstream output;
output.open(outFileName + fileExtension, std::ios_base::out | std::ios_base::binary);
char c;
std::string codeword = "";
std::string symbolStr = "";
char symbol = '\0';
std::map<std::string, char> codesTable;
input >> codeword;
while (codeword != "endList") {
input >> symbolStr;
if (symbolStr.length() == 1)
input.get(symbol);
else
symbol = symbolStr[1];
codesTable.insert(std::pair<std::string, char>(codeword, symbol));
input >> codeword;
}
input.get(c);
std::string encodedBitmapString = "";
int count = 0;
while (input.get(c)) {
count++;
for (int i = 7; i >= 0; --i) {
encodedBitmapString += char((c & (1 << i)) ? '1' : '0');
auto foundCode = codesTable.find(encodedBitmapString);
if (foundCode != codesTable.end()) {
output << foundCode->second;
encodedBitmapString = "";
}
}
}
input.close();
output.close();
}
void Decoder::DecodeExtendedCoded(std::string fileName, std::string outFileName)
{
std::ifstream input;
input.open(fileName, std::ios_base::in | std::ios_base::binary);
std::string fileExtension;
input >> fileExtension;
std::ofstream output;
output.open(outFileName + fileExtension, std::ios_base::out | std::ios_base::binary);
char c;
std::string codeword = "";
std::string symbolStr = "";
std::string symbol = "";
std::map<std::string, std::string> codesTable;
input >> codeword;
while (codeword != "endList") {
char c;
char c2;
input.get(c);
while (c != '-') {
input.get(c);
}
input.get(c);
input.get(c2);
symbol = c;
symbol += c2;
codesTable.insert(std::pair<std::string, std::string>(codeword, symbol));
input >> codeword;
}
input.get(c);
std::string encodedBitmapString = "";
int count = 0;
while (input.get(c)) {
count++;
for (int i = 7; i >= 0; --i) {
encodedBitmapString += char((c & (1 << i)) ? '1' : '0');
auto foundCode = codesTable.find(encodedBitmapString);
if (foundCode != codesTable.end()) {
output << foundCode->second;
encodedBitmapString = "";
}
}
}
input.close();
output.close();
}