-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarizer.cpp
202 lines (184 loc) · 3.84 KB
/
binarizer.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "binarizer.h"
/**
* The void constructor for the Binarizer.
* Resets the attributes from it.
*/
Binarizer::Binarizer()
{
binaryCode = "";
bits_used = 0;
cur_byte = 0;
offset = 0;
}
/**
* A getter for the size of the "binaryCode".
*/
int Binarizer::getCodeLength()
{
finalizeEncoding();
return binaryCode.size();
}
/**
* Returns the number of non-used bits for the
* current byte.
*/
int Binarizer::getOffset()
{
finalizeEncoding();
return this->offset;
}
/**
* A getter for the attribute "binaryCode".
*/
std::string Binarizer::getBinaryCode()
{
return this->binaryCode;
}
/**
* This method will be passing bits from the binary
* string to a method that will merge them into blocks
* in the "binaryCode" string.
*/
void Binarizer::addStringToCode(const char* code)
{
int code_length = strlen(code);
int cur_shift = 0;
int bits_to_process;
while (cur_shift != code_length)
{
bits_to_process = min(BYTE_LENGTH - bits_used, code_length - cur_shift);
processString(cur_shift, bits_to_process, code);
cur_shift += bits_to_process;
}
}
/**
* This method calls the "processChar" process in an individual
* way for each bit we have to process.
*
* @param cur_shift The number that indicates which bit of the string we must
* process.
*
* @param bits_to_process The number of bits we have to process.
*
* @param code A pointer to the binary string we have to codify.
*/
void Binarizer::processString(int cur_shift, int bits_to_process, const char* code)
{
for (int i = 0; i < bits_to_process; i++)
{
processChar(code[cur_shift]);
cur_shift++;
}
}
/**
* This method can be tagged as "black magic" if you are not familiar
* with the C operators for bit manipulation.
*
* It just puts the n-th bit of a character (given by "bits_used) to
* 1 if the bit is '1'.
*
* If we have processed 8 bits, we add this character to the binary
* string and we reset the counters.
*
* @param bit The bit to codify into a position of the current byte.
*/
void Binarizer::processChar(char bit)
{
char mask = 0x01;
if (bit == '1')
{
mask = mask << bits_used;
cur_byte = cur_byte | mask;
}
bits_used++;
if (bits_used == BYTE_LENGTH)
{
binaryCode.push_back(cur_byte);
cur_byte = 0;
bits_used = 0;
}
}
/**
* We add the last byte if we have used at least
* 1 bit of it.
*/
void Binarizer::finalizeEncoding()
{
if (bits_used != 0)
{
binaryCode.push_back(cur_byte);
offset = (BYTE_LENGTH - bits_used);
bits_used = 0;
}
}
/**
* We finalize the encoding and return the binary string.
*/
std::string Binarizer::printCode()
{
finalizeEncoding();
return this->binaryCode;
}
/**
* The void constructor for the Binarizer.
* Resets the attributes from it.
*/
Debinarizer::Debinarizer()
{
binaryCode = "";
tempCode = "";
cur_char = 0;
}
/**
* A setter for the "offset" attribute.
*/
void Debinarizer::setOffset(int offset)
{
this->offset = offset;
}
/**
* This methods parses every bit from the character
* and adds '1' or '0' depending on its bits.
*
* @param character The character to be parsed.
*/
void Debinarizer::addCharToString(char character)
{
for (int i = 0; i < BYTE_LENGTH; i++)
{
if (((character >> i) & 0x01) == 1)
binaryCode.push_back('1');
else binaryCode.push_back('0');
}
}
/**
* Resets the attribute "tempCode".
*/
void Debinarizer::resetTempCode()
{
tempCode = "";
}
/**
* This methods gives us the next substring from the "binaryCode"
* string with length n.
*
* @param n The length of the substring we want to get.
*/
std::string Debinarizer::getStringOfNBits(unsigned int n)
{
resetTempCode();
for (unsigned int i = 0; i < n; i++)
{
tempCode.push_back(binaryCode[cur_char]);
cur_char++;
}
return tempCode;
}
/**
* A method to know if we have characters left
* to read.
*/
bool Debinarizer::codesLeft()
{
return (cur_char != (binaryCode.size() - offset));
}