Claw  1.7.3
bit_ostream.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file bit_ostream.tpp
27  * \brief Implementation of the claw::bit_ostream class.
28  * \author Julien Jorge
29  */
30 #include <climits>
31 
32 /*----------------------------------------------------------------------------*/
33 /**
34  * \brief Constructor.
35  * \param f The stream in which we write.
36  */
37 template<typename Stream>
38 claw::bit_ostream<Stream>::bit_ostream( stream_type& f )
39  : m_stream(f), m_pending(0), m_pending_length(0)
40 {
41 
42 } // bit_ostream::bit_ostream()
43 
44 /*----------------------------------------------------------------------------*/
45 /**
46  * \brief Destructor.
47  */
48 template<typename Stream>
49 claw::bit_ostream<Stream>::~bit_ostream()
50 {
51  if (m_pending_length != 0)
52  m_stream.write( (char*)&m_pending, sizeof(m_pending) );
53 } // bit_ostream::~bit_ostream()
54 
55 /*----------------------------------------------------------------------------*/
56 /**
57  * \brief Write some bits.
58  * \param buf A buffer from which we read the bits.
59  * \param n The number of bits to write.
60  */
61 template<typename Stream>
62 void claw::bit_ostream<Stream>::write( const char* buf, unsigned int n )
63 {
64  if ( n == 0 )
65  return;
66 
67  unsigned int cur_size = 0;
68  unsigned char data = *buf;
69 
70  while ( n != 0 )
71  {
72  while( (m_pending_length != CHAR_BIT) && (n!=0) )
73  {
74  unsigned int bits =
75  std::min(CHAR_BIT - (unsigned int)m_pending_length, n);
76 
77  if ( CHAR_BIT - cur_size < bits )
78  bits = CHAR_BIT - cur_size;
79 
80  unsigned int mask = (1 << bits) - 1;
81 
82  m_pending |= (data & mask) << m_pending_length;
83  cur_size += bits;
84  m_pending_length += bits;
85  data >>= bits;
86  n -= bits;
87 
88  if ( (cur_size == CHAR_BIT) && (n!=0) )
89  {
90  ++buf;
91  cur_size = 0;
92  data = *buf;
93  }
94  }
95 
96  if ( m_pending_length == CHAR_BIT )
97  {
98  m_stream.write( (char*)&m_pending, sizeof(m_pending) );
99  m_pending = 0;
100  m_pending_length = 0;
101  }
102  }
103 } // bit_ostream::write()