-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitReader.cpp
More file actions
47 lines (47 loc) · 961 Bytes
/
Copy pathBitReader.cpp
File metadata and controls
47 lines (47 loc) · 961 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
44
45
46
47
#include "BitReader.h"
BitReader::BitReader(std::fstream* stream) : m_stream(stream), buffer(0), bitcount(0)
{
if (!stream->is_open())
{
throw std::runtime_error("Unable to initialize closed filestream");
}
}
bool BitReader::good()
{
return (m_stream->good() && (m_stream->peek(), !m_stream->eof())) || (bitcount != 0);
}
bool BitReader::ReadBit()
{
if (!bitcount)
{
if (m_stream->good())
{
*m_stream >> buffer;
bitcount = 8;
}
else
{
throw std::runtime_error("File stream is not good :(");
}
}
--bitcount;
return (buffer >> bitcount) & 1;
}
unsigned char BitReader::ReadByte()
{
if (bitcount == 8)
{
bitcount = 0;
return buffer;
}
unsigned char res = 0;
for (int i = 0; i < 8; ++i)
{
res |= (ReadBit() << i);
}
return res;
}
void BitReader::Close()
{
m_stream->close();
}