-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitWriter.cpp
More file actions
43 lines (42 loc) · 829 Bytes
/
Copy pathBitWriter.cpp
File metadata and controls
43 lines (42 loc) · 829 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 "BitWriter.h"
BitWriter::BitWriter(std::fstream* stream) : m_stream(stream), buffer(0), bitcount(0)
{
if (!stream->is_open())
{
throw std::runtime_error("Unable to initialize closed filestream");
}
}
void BitWriter::WriteBit(bool bit)
{
if (bitcount == 8)
{
*m_stream << buffer;
bitcount = 0;
buffer = 0;
}
buffer = buffer | ((bit ? 1 : 0) << (7 - bitcount));
++bitcount;
}
void BitWriter::WriteByte(unsigned char byte)
{
if (bitcount == 0)
{
*m_stream << byte;
}
else {
for (int i = 0; i < 8; ++i)
{
WriteBit((byte >> (7 - i)) & 1);
}
}
}
void BitWriter::Close()
{
if (bitcount)
{
*m_stream << buffer;
bitcount = 0;
buffer = 0;
}
m_stream->close();
}