Scribus
Open source desktop publishing at your fingertips
PRCbitStream.h
1 /************
2 *
3 * This file is part of a tool for producing 3D content in the PRC format.
4 * Copyright (C) 2008 Orest Shardt <shardtor (at) gmail dot com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *************/
20 
21 #ifndef __PRC_BIT_STREAM_H
22 #define __PRC_BIT_STREAM_H
23 
24 #ifdef _MSC_VER
25 #include <stdio.h>
26 #if _MSC_VER >= 1600
27 #include <stdint.h>
28 #else
29 typedef signed char int8_t;
30 typedef signed short int16_t;
31 typedef signed long int32_t;
32 typedef unsigned char uint8_t;
33 typedef unsigned short uint16_t;
34 typedef unsigned long uint32_t;
35 #endif // _MSC_VER >= 1600
36 #else
37 #include <inttypes.h>
38 #endif // _MSC_VER
39 #include <string>
40 #include <iostream>
41 #include <stdlib.h>
42 
43 #define CHUNK_SIZE (1024)
44 // Is this a reasonable initial size?
45 
47 {
48  public:
49  PRCbitStream(uint8_t*& buff, unsigned int l) : byteIndex(0), bitIndex(0),
50  allocatedLength(l), data(buff), compressed(false)
51  {
52  if(data == 0)
53  {
54  getAChunk();
55  }
56  }
57 
58  unsigned int getSize() const;
59  uint8_t* getData();
60 
61  PRCbitStream& operator <<(const std::string&);
62  PRCbitStream& operator <<(bool);
63  PRCbitStream& operator <<(uint32_t);
64  PRCbitStream& operator <<(uint8_t);
65  PRCbitStream& operator <<(int32_t);
66  PRCbitStream& operator <<(double);
67  PRCbitStream& operator <<(const char*);
68 
69  void compress();
70  void write(std::ostream &out) const;
71  private:
72  void writeBit(bool);
73  void writeBits(uint32_t,uint8_t);
74  void writeByte(uint8_t);
75  void nextByte();
76  void nextBit();
77  void getAChunk();
78  // bitIndex is "big endian", zero based, location of next bit to write
79  unsigned int byteIndex,bitIndex;
80  unsigned int allocatedLength;
81  uint8_t*& data;
82  bool compressed;
83  uint32_t compressedDataSize;
84 };
85 
86 #endif // __PRC_BIT_STREAM_H
Definition: PRCbitStream.h:46