The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cReadBits.h
Go to the documentation of this file.
1 /**
2  * @class cReadBits
3  * file name: cReadBits.h
4  * @author Betti Oesterholz
5  * @date 30.01.2010
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * With this class it is possible to read a input stream bit for bit.
11  * Copyright (C) @c LGPL3 2009 Betti Oesterholz
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License (LGPL) as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  *
27  * With this class it is possible to read a input stream bit for bit.
28  * It implements methods to read a given number of bits from a input
29  * stream and buffers a number of byts of the input stream.
30  *
31  */
32 /*
33 History:
34 30.01.2009 Oesterholz created
35 */
36 
37 #ifndef ___C_READ_BITS_H__
38 #define ___C_READ_BITS_H__
39 
40 
41 #include <cstring>
42 #include <istream>
43 
44 
45 using namespace std;
46 
47 namespace fib{
48 
49 
50 class cReadBits{
51 protected:
52  /**
53  * This is a pointer to the buffer wher the readed byts of the stream
54  * will be bufferd.
55  * The byts bufferd are readed round. If the end of the buffer is
56  * reached, the beginning is the next byte.
57  */
58  char * szByteBuffer;
59 
60  /**
61  * The number of byts to buffer.
62  */
63  unsigned int uiBitsToBuffer;
64 
65  /**
66  * The bitposition in the buffer of the bit to read next from the buffer.
67  */
68  unsigned int uiBitPositionBuffer;
69 
70  /**
71  * The bitposition of the bit which will be stored next to the buffer.
72  */
73  unsigned int uiBitReadedInBuffer;
74 
75  /**
76  * The the number of bits readed from the stream.
77  */
78  unsigned long ulBitReadedCount;
79 
80  /**
81  * A pointer to the input stream to buffer.
82  */
83  istream * pIstream;
84 
85 public:
86 
87  /**
88  * Standardconstructor
89  * Beware: Don't use istreamIn elsewher, while using it with theas class.
90  * This class should only use istreamIn.
91  *
92  * @param istreamIn the stream to read the bits from
93  * @param uiBytsToBuffer the number of byts to buffer from the stream
94  */
95  cReadBits( istream & istreamIn, unsigned int uiBytsToBuffer = 256 );
96 
97  /**
98  * destructor
99  */
100  ~cReadBits();
101 
102  /**
103  * This method reads a number of bits from the stream.
104  *
105  * @param pBuffer the character array wher to writes the readed bits to,
106  * the first bit will begin at the pointer pBuffer
107  * @param uiBitsToRead the number of bits to read
108  * @return the number of bits readed
109  */
110  unsigned int readBits( char * pBuffer, unsigned int uiBitsToRead );
111 
112  /**
113  * This method reads a number of bits from the stream into a integer
114  * variable.
115  *
116  * @param iBuffer a reference to the variable, wher to read the number to
117  * @param uiBitsToRead the number of bits to read
118  * @return the number of bits readed
119  */
120  unsigned int readBits( int & iBuffer, unsigned int uiBitsToRead );
121 
122  /**
123  * This method reads a number of bits from the stream into a integer
124  * variable.
125  *
126  * @param lBuffer a reference to the variable, wher to read the number to
127  * @param uiBitsToRead the number of bits to read
128  * @return the number of bits readed
129  */
130  unsigned int readBits( long & lBuffer, unsigned int uiBitsToRead );
131 
132  /**
133  * This method reads a number of bits from the stream into a integer
134  * variable.
135  *
136  * @param lBuffer a reference to the variable, wher to read the number to
137  * @param uiBitsToRead the number of bits to read
138  * @return the number of bits readed
139  */
140  unsigned int readBits( long long & lBuffer, unsigned int uiBitsToRead );
141 
142  /**
143  * This method reads a number of bits from the stream into a integer
144  * variable.
145  *
146  * @param uiBuffer a reference to the variable, wher to read the number to
147  * @param uiBitsToRead the number of bits to read
148  * @return the number of bits readed
149  */
150  unsigned int readBits( unsigned int & uiBuffer, unsigned int uiBitsToRead );
151 
152  /**
153  * This method reads a number of bits from the stream into a integer
154  * variable.
155  *
156  * @param uiBuffer a reference to the variable, wher to read the number to
157  * @param uiBitsToRead the number of bits to read
158  * @return the number of bits readed
159  */
160  unsigned int readBits( unsigned long & ulBuffer, unsigned int uiBitsToRead );
161 
162  /**
163  * This method reads a number of bits from the stream into a integer
164  * variable.
165  *
166  * @param uiBuffer a reference to the variable, wher to read the number to
167  * @param uiBitsToRead the number of bits to read
168  * @return the number of bits readed
169  */
170  unsigned int readBits( unsigned long long & ulBuffer, unsigned int uiBitsToRead );
171 
172  /**
173  * This method reads to the beginning of the next full byte, if it isn't
174  * allready readed to a full byte.
175  * After the method call the readpoint stands on the beginning of a byte,
176  * which is after or at the read position befor the call.
177  *
178  * @return the number of bits readed
179  */
180  unsigned int readTillNextFullByte();
181 
182  /**
183  * This function will rollbach uiBitsToRollback bits of the stream so
184  * they will be read again.
185  *
186  * @param uiBitsToRollback the number of bits to rollback
187  * @return the number of bits rollbacked
188  */
189  unsigned int rollbackBits( unsigned int uiBitsToRollback );
190 
191  /**
192  * @return a pointer to the bufferd input stream
193  * Beware: The readposition will be after the byte of last readed bit,
194  * without the rollbacks.
195  */
196  istream * getStream();
197 
198  /**
199  * @return the number of bits readed from the stream or respectively
200  * the bitposition in the stream (rollbacks inclusive)
201  */
202  unsigned long getBitReadedCount() const;
203 
204 
205 };//end class cReadBits
206 
207 
208 }//end namespace fib
209 
210 #endif //___C_READ_BITS_H__