MLIB
Loading...
Searching...
No Matches
bitstream.h
Go to the documentation of this file.
1
6#pragma once
7#include <iostream>
8
9#if __has_include("defs.h")
10#include "defs.h"
11#endif
12
13namespace mlib {
14
17{
18public:
19 bitstream (std::iostream& str, unsigned int pack);
20
21 bool read ();
22 void write (int val);
23 void flush ();
24
26 int mread (unsigned int sz, bool is_signed = false);
27
29 void mwrite (int val, unsigned int sz);
30
31protected:
32 const unsigned int packing;
33 std::iostream& s;
34
36 virtual void encode (unsigned char bits, char& chr);
37
39 virtual void decode (unsigned char& bits, char chr);
40
41private:
42 unsigned char buffer;
43 int nbits;
44};
45
51inline void bitstream::encode (unsigned char bits, char& chr)
52{
53 chr = (bits & (1 << packing) - 1);
54}
55
61inline void bitstream::decode (unsigned char& bits, char chr)
62{
63 bits = (chr & (1 << packing) - 1);
64}
65
66}; // namespace mlib
int mread(unsigned int sz, bool is_signed=false)
Read a variable number of bits.
Definition bitstream.cpp:121
bitstream(std::iostream &str, unsigned int pack)
Definition bitstream.cpp:26
void write(int val)
Write a bit to stream.
Definition bitstream.cpp:64
virtual void decode(unsigned char &bits, char chr)
Decode bit field from a byte.
Definition bitstream.h:61
virtual void encode(unsigned char bits, char &chr)
Encode bit field in a byte.
Definition bitstream.h:51
void mwrite(int val, unsigned int sz)
Write a variable number of bits.
Definition bitstream.cpp:103
void flush()
If there are any leftover bits, encode them in one character and write them out to stream.
Definition bitstream.cpp:84
bool read()
Read the next bit.
Definition bitstream.cpp:41