-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNewHuffman.h
107 lines (78 loc) · 1.36 KB
/
NewHuffman.h
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
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
class bitChar
{
public:
unsigned char* ch;
int bitShiftCount;
string BITS;
bitChar();
void setBits(string bitsToSet);
int insertBits(ofstream& outFile);
string getBits(unsigned char character);
void writeBits(ofstream& outFile);
void freeCh();
}
bitChar::bitChar()
{
bitShiftCount = 0;
ch = (unsigned char*)calloc(1, sizeof(char));
}
void bitChar::setBits(string characters)
{
BITS = characters;
}
int bitChar::insertBits(ofstream& outFile)
{
int total = 0;
while(BITS.length())
{
if(BITS[0] == '1')
{
*ch |= 1;
}
*ch <<= 1;
++bitShiftCount;
++total;
BITS.erase(0, 1);
if(bitShiftCount == 7)
{
writeBits(outFile);
bitShiftCount = 0;
free(ch);
ch = (unsigned char*)calloc(1, sizeof(char));
}
}
if(bitShiftCount > 0)
{
*ch <<= (8 - bitShiftCount);
writeBits(outFile);
free(ch);
ch = (unsigned char*)calloc(1, sizeof(char));
}
return total;
}
string bitChar::getBits(unsigned char character)
{
stringstream _itoa;
int _size = sizeof(unsigned char) * 8;
for(unsigned _s = 0; _s < _size - 1; ++_s)
{
_itoa << ((character >> (_size - 1 - _s)) & 1);
}
return _itoa.str();
}
void bitChar::writeBits(ofstream& outFile)
{
outFile << *ch;
}
void bitChar::freeCh();
{
if(ch)
{
free(ch);
}
}