00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <climits>
00031
00032
00037 template<typename Stream>
00038 claw::bit_istream<Stream>::bit_istream( stream_type& f )
00039 : m_stream(f), m_pending(0), m_pending_length(0)
00040 {
00041
00042 }
00043
00044
00050 template<typename Stream>
00051 void claw::bit_istream<Stream>::read( char* buf, unsigned int n )
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 }
00087
00088
00092 template<typename Stream>
00093 claw::bit_istream<Stream>::operator bool() const
00094 {
00095 return m_stream || (m_pending_length > 0);
00096 }