-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeWriter.cpp
More file actions
67 lines (61 loc) · 1.7 KB
/
CodeWriter.cpp
File metadata and controls
67 lines (61 loc) · 1.7 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
#include "CodeWriter.h"
void displayCode(pair<int, int> code){
int buf = 0;
int val = code.second;
for(int i = 0; i < code.first; i++){
buf = (buf<<1) ^ (val & 0x01);
val = val>>1;
}
for(int i = 0; i < code.first; i++){
cout<<(buf&0x01);
buf = buf>>1;
}
cout<<" ";
}
CodeWriter::CodeWriter(ostream& out): out(out), value(0), size(0){}
void CodeWriter::writeCode(pair<int, int> code){
if(code.first + size >= 8){
int buf = 0;
int val = 0;
int to = code.first;
for(int i = 0; i < code.first; i++){
val = (val<<1) ^ (code.second & 0x01);
code.second = code.second>>1;
}
while(code.first + size >= 8){
for(int i = size; i < 8; i++){
value = (value<<1) ^ (val & 0x01);
val = val>>1;
code.first--;
}
out.write((char*)&value, 1);
// displayCode(pair<int, int>(8, value));
if(value == 0xff){
value = 0;
out.write((char*)&value, 1);
// displayCode(pair<int, int>(8, value));
}
value = 0; size = 0;
}
code.second = 0;
for(int i = 0; i < code.first; i++){
code.second= (code.second<<1) ^ (val & 0x01);
val = val>>1;
}
}
value = (value<<code.first) ^ code.second;
size += code.first;
}
void CodeWriter::flush(){
if(size != 0){
while(size < 8){
value= (value<<1) ^ 1;
size++;
}
char c = (char)value;
// displayCode(pair<int, int>(size, value));
out.write(&c, 1);
}
size = 0;
value = 0;
}