-
Notifications
You must be signed in to change notification settings - Fork 2
/
huff.cpp
594 lines (504 loc) · 11.6 KB
/
huff.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#define ASCII 256
#define BYTE_SIZE 8
#include <math.h>
#include <fstream>
#include <sstream>
#include "huff.h"
// Public.
Huff::Huff()
{
this->list = NULL;
}
/**
* Compress the specified file.
*
* @param filename The filename.
*/
void Huff::compress(string filename)
{
string text = this->read(filename);
List* list = this->buildList(text);
Node* node = this->buildTrie(list);
string* codes = this->buildCode(node);
string trie = this->transform(node);
string bitflow = this->transform(text, codes);
string outputFilename = getFileWithoutExtension(filename, find(filename, '.')) + ".hfz";
this->write(getFileExtension(filename, find(filename, '.')), trie, text.size(), bitflow, outputFilename);
}
/**
* Uncompress the specified file.
*
* @param filename The filename.
*/
void Huff::expand(string filename)
{
string text = this->read(filename);
string ext = this->readFileExtension(&text);
Node* root = this->readTrie(&text);
int size = this->readSize(&text);
string bitflow = this->bytesToBitflow(text);
string translated = this->translate(bitflow, size, root);
this->write(translated, getFileWithoutExtension(filename, find(filename, '.')) + "." + ext);
}
// Private.
/**
* Read the content of the file.
*
* @param filename The filename.
* @return The content of the file.
*/
string Huff::read(string filename)
{
ifstream source;
source.open(filename.c_str());
if (!source)
{
cout << "File not found!\n";
exit(-1);
}
if (!source.is_open())
{
cout << "It was not possible open the file!\n";
exit(-1);
}
char head;
stringstream buffer;
buffer << source.rdbuf();
source.close();
return buffer.str();
}
/**
* Build a string list based on character occurrence.
*
* @param text The source text.
* @return The list.
*/
List* Huff::buildList(string text)
{
List* list = new List();
for (int i = 0; i < text.size(); ++i)
{
list->add(text[i]);
}
return list;
}
/**
* Transform a bits sequency on correspondent string.
*
* @param bitflow The bits sequency.
* @return The correspondent string.
*/
string Huff::buildBytes(string bitflow)
{
unsigned char byte = 0;
unsigned int currbit = 0;
std::string output = "";
while (currbit < bitflow.size())
{
for (int i = 0; i < BYTE_SIZE; )
{
if (bitflow[currbit] == '1')
{
byte = byte << 1;
byte = byte | 1;
}
else if (bitflow[currbit] == '0')
{
byte = byte << 1;
}
++currbit;
++i;
if (currbit >= bitflow.size())
{
int remaind = 8 - i;
for (int j = 0; j < remaind; ++j)
{
byte = byte << 1;
}
break;
}
}
output += byte;
byte = 0;
}
return output;
}
/**
* Build a trie based on specified list.
*
* @param list The reference to the list.
* @return The head node of the trie.
*/
Node* Huff::buildTrie(List *list)
{
if (list == NULL)
{
throw "Argument is null!";
}
if (list->getLength() == 0)
{
throw "List is empty!";
}
while (list->getLength() > 1)
{
Node* first = list->getFirst();
Node* second = list->getFirst()->getNext();
int frequency = first->getFrequency() + second->getFrequency();
Node* parent = new Node(frequency, first, second);
list->remove(first);
list->remove(second);
list->add(parent);
}
return list->getFirst();
}
/**
* Build the codes table based on trie.
*
* @param root The head node of the trie.
* @return The codes table.
*/
string* Huff::buildCode(Node *root)
{
string* codes = new string[ASCII];
this->buildCode(codes, root, "");
return codes;
}
/**
* Build the code table based on trie.
*
* @param codes The codes table.
* @param root The reference to the head node of the trie.
* @param code The current code under construction.
* @return The code table.
*/
void Huff::buildCode(string* codes, Node* node, string code)
{
if (node->isLeaf())
{
codes[node->getSymbol()] = code;
return;
}
this->buildCode(codes, node->getLeft(), code + '0');
this->buildCode(codes, node->getRight(), code + '1');
}
/**
* Show trie on screen.
*
* @param root The head node of the trie.
*/
void Huff::printTrie(Node* root)
{
if (root->isLeaf())
{
root->print();
return;
}
this->printTrie(root->getLeft());
this->printTrie(root->getRight());
}
/**
* Show codes table on screen.
*
* @param codes The codes table.
*/
void Huff::printCode(string* codes)
{
cout << "Symbol Bits\n";
for (int i = 0; i < ASCII; ++i)
{
if (codes[i] != "")
{
char c = (char)i;
cout << " " << c << " " << codes[i] << "\n";
}
}
}
/**
* Write trie, original string size and bitflow on specified file.
*
* @param text The text to save.
* @param filename The name of target file.
*/
void Huff::write(string text, string filename)
{
// Save on file.
ofstream target;
target.open(filename.c_str());
target << text;
target.close();
}
/**
* Write trie, original string size and bitflow on specified file.
*
* @param trie The trie converted on string format.
* @param size The original string size.
* @param bitflow The bits sequency built.
*/
void Huff::write(string fileext, string trie, int size, string bitflow, string filename)
{
// Save file extension.
string output = fileext + ",";
// Save file extension.
output += trie;
// Save size of original string.
stringstream ss;
ss << size;
output += ss.str();
// Save bitflow.
output += buildBytes(bitflow);
// Save on file.
this->write(output, filename);
}
/**
* Transform trie on string.
*
* @param node The head node of the trie.
* @return The string constructed from the trie.
*/
string Huff::transform(Node* node)
{
return this->transform(node, "");
}
/**
* Transform trie on string.
*
* @param node The head node of the trie.
* @param output The string state constructed from the trie before checking the current node.
* @return The string state constructed from the trie after checking the current node.
*/
string Huff::transform(Node* node, string output)
{
if (node->isLeaf())
{
output += '1';
output += node->getSymbol();
return output;
}
output += '0';
output = transform(node->getLeft(), output);
output = transform(node->getRight(), output);
return output;
}
/**
* Read the file extension from content of compressed file.
*
* @param text Content of compressed file.
* @return The file extension.
*/
string Huff::readFileExtension(string* text)
{
string ext = "";
int i;
for (i = 0; (*text)[i] != ','; ++i)
{
ext += (*text)[i];
}
(*text).replace(0, i + 1, "");
return ext;
}
/**
* Read the trie from content of compressed file.
*
* @param text Content of compressed file.
* @return The trie.
*/
Node* Huff::readTrie(string* text)
{
int index = -1;
Node* node = this->readTrie(*text, &index);
index++;
(*text).replace(0, index, "");
return node;
}
/**
* Read the trie from content of compressed file.
*
* @param text Current content of compressed file.
* @param index Content head.
* @return The trie.
*/
Node* Huff::readTrie(string text, int* index)
{
(*index)++;
if (text[*index] == '1')
{
(*index)++;
char symbol = text[*index];
return new Node(symbol, 0, NULL, NULL, NULL, NULL);
}
return new Node('\0', 0, NULL, NULL, readTrie(text, index), readTrie(text, index));
}
/**
* Read the original bytes size from content of compressed file.
*
* @param text Content of compressed file.
* @return The original bytes size.
*/
int Huff::readSize(string* text)
{
stringstream ss(*text);
int size;
ss >> size;
ss.str("");
ss.clear();
ss << size;
int index = ss.str().size();
(*text) = (*text).replace(0, index, "");
return size;
}
/**
* Convert the original text to bitflow.
*
* @param text The original text.
* @param text The built table.
* @return The compression bitflow.
*/
string Huff::transform(string text, string* table)
{
string output = "";
for (int i = 0; i < text.size(); ++i)
{
output += table[text[i]];
}
return output;
}
/**
* Transform the original filename in a file with hfz extension.
*
* @param filename The original filename.
* @return The modified filename.
*/
std::string Huff::getOutputFilename(std::string filename)
{
std::string outputFilename = "";
int endIndex = filename.size() - 1;
while (endIndex >= 0)
{
if (filename[endIndex] == '.')
{
--endIndex;
break;
}
--endIndex;
}
if (endIndex >= 0)
{
for (int i = 0; i <= endIndex; ++i)
{
outputFilename += filename[i];
}
}
else
{
outputFilename = filename;
}
outputFilename += ".hfz";
return outputFilename;
}
/**
* Transform the compressed bytes in a string of bitflow.
*
* @param bytes The compressed bytes.
* @return The bitflow.
*/
string Huff::bytesToBitflow(string bytes)
{
string bits = "";
for (int i = 0; i < bytes.size(); ++i)
{
for (int j = 0; j < BYTE_SIZE; ++j)
{
if ((bytes[i] & 256) == 256) // compare "c" with 1000 0000
{
bits += '1';
}
else
{
bits += '0';
}
bytes[i] = bytes[i] << 1; // left shift
}
}
return bits;
}
/**
* Transform the bitflow in the original text.
*
* @param bitflow The bitflow.
* @param size The bytes size of original text.
* @param root The root node of trie.
* @return The original text.
*/
string Huff::translate(string bitflow, int size, Node* root)
{
int bit = 0;
string text = "";
for (int i = 0; i < size; ++i)
{
Node* node = root;
while (!node->isLeaf())
{
if (bitflow[bit] == '1')
{
node = node->getRight();
}
else
{
node = node->getLeft();
}
++bit;
}
text += node->getSymbol();
}
return text;
}
/**
* Searches for the specified character and return its index. If not, return -1.
*
* @param filename The filename.
* @param charact The character.
* @return The character index.
*/
int Huff::find(string filename, char charact)
{
int index = -1;
for (int i = filename.size() - 1; i > 0; --i)
{
if (filename[i] == charact)
{
index = i;
return index;
}
}
return index;
}
/**
* Get the file extension from specified filename.
*
* @param filename The filename.
* @param index The index of separator to split the filename. Most common: doth.
* @return The file extension.
*/
string Huff::getFileExtension(string filename, int index)
{
if (index != -1)
{
filename.replace(0, index + 1, "");
}
return filename;
}
/**
* Get the filename without its extension.
*
* @param filename The filename.
* @param index The index of separator to split the filename. Most common: doth.
* @return The filename without its extension.
*/
string Huff::getFileWithoutExtension(string filename, int index)
{
if (index != -1)
{
filename.replace(index, filename.size() - 1, "");
}
return filename;
}