-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.cpp
More file actions
45 lines (33 loc) · 1.05 KB
/
reader.cpp
File metadata and controls
45 lines (33 loc) · 1.05 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
#include "reader.h"
Reader::Reader(const char* file_name) : in_() {
in_ = std::fstream(file_name, std::ios_base::in | std::ios_base::binary);
if (!in_.is_open()) {
throw std::runtime_error("error - cannot open file named " +
static_cast<std::string>(file_name));
}
ReadNextBits();
}
void Reader::ReadNextBits() {
char character;
while (bits_of_file_.size() < NUMBER_OF_BITS_TO_SEE && in_ >> std::noskipws >> character) {
for (size_t index = 0; index < NUMBER_OF_BITS_IN_BYTE; ++index) {
bits_of_file_.push_back((character >> index) & 1);
}
}
}
void Reader::DeleteUselessBitsAtTheBeginning(size_t count) {
for (size_t index = 0; index < count; ++index) {
bits_of_file_.pop_front();
}
ReadNextBits();
}
int Reader::GetValueOfNextBits(size_t size) {
ReadNextBits();
int value = 0;
for (size_t index = 0; index < size; ++index) {
if (bits_of_file_[index]) {
value |= (1 << index);
}
}
return value;
}