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_ostream<Stream>::bit_ostream( stream_type& f )
00039 : m_stream(f), m_pending(0), m_pending_length(0)
00040 {
00041
00042 }
00043
00044
00048 template<typename Stream>
00049 claw::bit_ostream<Stream>::~bit_ostream()
00050 {
00051 if (m_pending_length != 0)
00052 m_stream.write( (char*)&m_pending, sizeof(m_pending) );
00053 }
00054
00055
00061 template<typename Stream>
00062 void claw::bit_ostream<Stream>::write( const char* buf, unsigned int n )
00063 {
00064 if ( n == 0 )
00065 return;
00066
00067 unsigned int cur_size = 0;
00068 unsigned char data = *buf;
00069
00070 while ( n != 0 )
00071 {
00072 while( (m_pending_length != CHAR_BIT) && (n!=0) )
00073 {
00074 unsigned int bits =
00075 std::min(CHAR_BIT - (unsigned int)m_pending_length, n);
00076
00077 if ( CHAR_BIT - cur_size < bits )
00078 bits = CHAR_BIT - cur_size;
00079
00080 unsigned int mask = (1 << bits) - 1;
00081
00082 m_pending |= (data & mask) << m_pending_length;
00083 cur_size += bits;
00084 m_pending_length += bits;
00085 data >>= bits;
00086 n -= bits;
00087
00088 if ( (cur_size == CHAR_BIT) && (n!=0) )
00089 {
00090 ++buf;
00091 cur_size = 0;
00092 data = *buf;
00093 }
00094 }
00095
00096 if ( m_pending_length == CHAR_BIT )
00097 {
00098 m_stream.write( (char*)&m_pending, sizeof(m_pending) );
00099 m_pending = 0;
00100 m_pending_length = 0;
00101 }
00102 }
00103 }