bit_istream.tpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
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 } // bit_istream::bit_istream()
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 } // bit_istream::read()
00087 
00088 /*----------------------------------------------------------------------------*/
00092 template<typename Stream>
00093 claw::bit_istream<Stream>::operator bool() const
00094 {
00095   return m_stream || (m_pending_length > 0);
00096 } // bit_istream::operator bool()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1