This class is made to help reading datas of custom bit length. More...
#include <bit_istream.hpp>
Public Member Functions | |
bit_istream (stream_type &f) | |
Constructor. | |
void | read (char *buf, unsigned int n) |
Read some bits. | |
operator bool () const | |
Tell if the input stream is still valid. | |
Private Types | |
typedef Stream | stream_type |
The type of the stream we will read. | |
Private Attributes | |
stream_type & | m_stream |
The stream we're reading. | |
unsigned char | m_pending |
Some bits available for reading. | |
unsigned char | m_pending_length |
The number of valid bits in m_pending. |
This class is made to help reading datas of custom bit length.
Definition at line 40 of file bit_istream.hpp.
typedef Stream claw::bit_istream< Stream >::stream_type [private] |
The type of the stream we will read.
Definition at line 44 of file bit_istream.hpp.
claw::bit_istream< Stream >::bit_istream | ( | stream_type & | f | ) | [inline] |
Constructor.
f | The stream in which we read. |
Definition at line 38 of file bit_istream.tpp.
00039 : m_stream(f), m_pending(0), m_pending_length(0) 00040 { 00041 00042 } // bit_istream::bit_istream()
claw::bit_istream< Stream >::operator bool | ( | ) | const [inline] |
Tell if the input stream is still valid.
Definition at line 93 of file bit_istream.tpp.
References claw::bit_istream< Stream >::m_pending_length, and claw::bit_istream< Stream >::m_stream.
00094 { 00095 return m_stream || (m_pending_length > 0); 00096 } // bit_istream::operator bool()
void claw::bit_istream< Stream >::read | ( | char * | buf, | |
unsigned int | n | |||
) | [inline] |
Read some bits.
buf | A buffer in which we write the bits. | |
n | The number of bits to read. |
Definition at line 51 of file bit_istream.tpp.
References claw::bit_istream< Stream >::m_pending, claw::bit_istream< Stream >::m_pending_length, and claw::bit_istream< Stream >::m_stream.
00052 { 00053 if ( n == 0 ) 00054 return; 00055 00056 unsigned int cur_size = 0; 00057 00058 while ( (n != 0) && !!(*this) ) 00059 { 00060 while( (m_pending_length != 0) && (n!=0) && !!(*this) ) 00061 { 00062 unsigned int bits = std::min((unsigned int)m_pending_length, n); 00063 00064 if ( CHAR_BIT - cur_size < bits ) 00065 bits = CHAR_BIT - cur_size; 00066 00067 unsigned int mask = (1 << bits) - 1; 00068 00069 *buf |= (m_pending & mask) << cur_size; 00070 cur_size += bits; 00071 m_pending_length -= bits; 00072 m_pending >>= bits; 00073 n -= bits; 00074 00075 if ( cur_size == CHAR_BIT ) 00076 { 00077 ++buf; 00078 cur_size = 0; 00079 } 00080 } 00081 00082 if ( m_pending_length == 0 ) 00083 if ( m_stream.read( (char*)&m_pending, sizeof(m_pending) ) ) 00084 m_pending_length = CHAR_BIT; 00085 } 00086 } // bit_istream::read()
unsigned char claw::bit_istream< Stream >::m_pending [private] |
Some bits available for reading.
Definition at line 58 of file bit_istream.hpp.
Referenced by claw::bit_istream< Stream >::read().
unsigned char claw::bit_istream< Stream >::m_pending_length [private] |
The number of valid bits in m_pending.
Definition at line 61 of file bit_istream.hpp.
Referenced by claw::bit_istream< Stream >::operator bool(), and claw::bit_istream< Stream >::read().
stream_type& claw::bit_istream< Stream >::m_stream [private] |
The stream we're reading.
Definition at line 55 of file bit_istream.hpp.
Referenced by claw::bit_istream< Stream >::operator bool(), and claw::bit_istream< Stream >::read().