-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitInputStream.cpp
More file actions
78 lines (66 loc) · 1.49 KB
/
Copy pathBitInputStream.cpp
File metadata and controls
78 lines (66 loc) · 1.49 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
#ifndef BITINPUTSTREAM
#define BITINPUTSTREAM
/*
* BitInputStream.cpp
* Author: saito
* Date: May 17, 2018
* About:
*/
#include <fstream>
#include <iostream>
using namespace std;
class BitInputStream {
private:
char buffer=0;
char current=-1;
ifstream* input;
public:
//Return eof if the input stream ever threw eof, and
bool eof(){
return (input->eof() && current == 0);
};
//Retrieve the next bit from the buffer
bool get(){
bool returnval;
if(current < 0)
flush();
//Create a bitmask by shifting current, and AND it with the buffer
//If the resulting number >0, return true
returnval = buffer & (1<<current);
/* if(!returnval) {
cout << "0";
} else {
cout << "1";
}*/
current--;
return returnval;
};
/*Called with the knowledge the next size bits(!) are expected to represent a number
* Reads them and returns the stored value. Primarily used in reading the header.*/
unsigned int read(char size){
string bitStream = "";
int value = 0;
//Get each value and OR it to its appropriate location
for (int index = size-1; index >= 0; index--){
bool insert = get();
if(insert){
bitStream.append("1");
} else {bitStream.append("0");}
value |= insert << index;
}
return value;
};
void connect(ifstream& stream){
input = &stream;
};
//Get a new set of bits and reset the buffer
void flush(){
input->get(buffer);
current = 7;
};
//Wrapper to relay close() command
void close(){
input->close();
};
};
#endif // BITINPUTSTREAM