1 /*
2 ===============================================================================
3 
4   FILE:  bytestreamout.hpp
5 
6   CONTENTS:
7 
8     Abstract base class for output streams with endian handling.
9 
10   PROGRAMMERS:
11 
12     martin.isenburg@rapidlasso.com  -  http://rapidlasso.com
13 
14   COPYRIGHT:
15 
16     (c) 2007-2013, martin isenburg, rapidlasso - fast tools to catch reality
17 
18     This is free software; you can redistribute and/or modify it under the
19     terms of the GNU Lesser General Licence as published by the Free Software
20     Foundation. See the COPYING file for more information.
21 
22     This software is distributed WITHOUT ANY WARRANTY and without even the
23     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 
25   CHANGE HISTORY:
26 
27      2 January 2013 -- new functions for writing a stream of groups of bits
28      1 October 2011 -- added 64 bit file support in MSVC 6.0 at McCafe at Hbf Linz
29     10 January 2011 -- licensing change for LGPL release and liblas integration
30     12 December 2010 -- created from ByteStreamOutFile after Howard got pushy (-;
31 
32 ===============================================================================
33 */
34 #ifndef BYTE_STREAM_OUT_HPP
35 #define BYTE_STREAM_OUT_HPP
36 
37 #include "mydefs.hpp"
38 
39 class ByteStreamOut
40 {
41 public:
42 /* write single bits                                         */
putBits(U32 bits,U32 num_bits)43   inline BOOL putBits(U32 bits, U32 num_bits)
44   {
45     U64 new_bits = bits;
46     bit_buffer |= (new_bits << num_buffer);
47     num_buffer += num_bits;
48     if (num_buffer >= 32)
49     {
50       U32 output_bits = (U32)(bit_buffer & U32_MAX);
51       bit_buffer = bit_buffer >> 32;
52       num_buffer = num_buffer - 32;
53       return put32bitsLE((U8*)&output_bits);
54     }
55     return TRUE;
56   };
57 /* called after writing bits before closing or writing bytes */
flushBits()58   inline BOOL flushBits()
59   {
60     if (num_buffer)
61     {
62       U32 num_zero_bits = 32 - num_buffer;
63       U32 output_bits = (U32)(bit_buffer >> num_zero_bits);
64       bit_buffer = 0;
65       num_buffer = 0;
66       return put32bitsLE((U8*)&output_bits);
67     }
68     return TRUE;
69   };
70 /* write a single byte                                       */
71   virtual BOOL putByte(U8 byte) = 0;
72 /* write an array of bytes                                   */
73   virtual BOOL putBytes(const U8* bytes, U32 num_bytes) = 0;
74 /* write 16 bit low-endian field                             */
75   virtual BOOL put16bitsLE(const U8* bytes) = 0;
76 /* write 32 bit low-endian field                             */
77   virtual BOOL put32bitsLE(const U8* bytes) = 0;
78 /* write 64 bit low-endian field                             */
79   virtual BOOL put64bitsLE(const U8* bytes) = 0;
80 /* write 16 bit big-endian field                             */
81   virtual BOOL put16bitsBE(const U8* bytes) = 0;
82 /* write 32 bit big-endian field                             */
83   virtual BOOL put32bitsBE(const U8* bytes) = 0;
84 /* write 64 bit big-endian field                             */
85   virtual BOOL put64bitsBE(const U8* bytes) = 0;
86 /* is the stream seekable (e.g. standard out is not)         */
87   virtual BOOL isSeekable() const = 0;
88 /* get current position of stream                            */
89   virtual I64 tell() const = 0;
90 /* seek to this position in the stream                       */
91   virtual BOOL seek(const I64 position) = 0;
92 /* seek to the end of the file                               */
93   virtual BOOL seekEnd() = 0;
94 /* constructor                                               */
ByteStreamOut()95   inline ByteStreamOut() { bit_buffer = 0; num_buffer = 0; };
96 /* destructor                                                */
~ByteStreamOut()97   virtual ~ByteStreamOut() {};
98 private:
99   U64 bit_buffer;
100   U32 num_buffer;
101 };
102 
103 #endif
104