bit_ostream.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_ostream<Stream>::bit_ostream( stream_type& f )
00039   : m_stream(f), m_pending(0), m_pending_length(0)
00040 {
00041 
00042 } // bit_ostream::bit_ostream()
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 } // bit_ostream::~bit_ostream()
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 } // bit_ostream::write()

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