-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStream.cpp
More file actions
43 lines (37 loc) · 905 Bytes
/
Copy pathStream.cpp
File metadata and controls
43 lines (37 loc) · 905 Bytes
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
#include <Stream.h>
#include <ostream>
Stream::~Stream() {
const auto extra = buffer.size() % 8;
if (extra != 0)
for (int i = 0; i < (8 - extra); ++i)
buffer.push_back(false);
flush();
}
void Stream::write(const char raw) {
const uint8_t octet = raw;
if (buffer.empty()) {
stream.put(octet);
} else {
for (int i = 7; i >= 0; --i)
buffer.push_back(bool(octet & (1 << i)));
}
flush();
}
void Stream::write(const uint64_t data, const int bits) {
for (int i = bits - 1; i >= 0; --i)
buffer.push_back(bool(data & (1 << i)));
flush();
}
void Stream::write_bit(const bool bit) {
buffer.push_back(bit);
flush();
}
void Stream::flush() {
while (buffer.size() >= 8) {
uint8_t octet = 0;
for (int i = 0; i < 8; ++i)
octet |= *(buffer.begin() + i) << (7 - i);
buffer.erase(buffer.begin(), buffer.begin() + 8);
stream.put(octet);
}
}