src/bytestream.h

changeset 18
6bf57b4f42cd
parent 17
b41d74bacdea
child 19
c9b6dd9dd4cd
equal deleted inserted replaced
17:b41d74bacdea 18:6bf57b4f42cd
1 /*
2 * ZanDemo: Zandronum demo launcher
3 * Copyright (C) 2013 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef BYTESTREAM_H
20 #define BYTESTREAM_H
21
22 #include "types.h"
23
24 class Bytestream {
25 private:
26 uint8* m_data;
27 uint8* m_ptr;
28 ulong m_size, m_len;
29
30 void doWrite (uint8_t val);
31
32 public:
33 Bytestream (ulong len = 2048);
34 Bytestream (const char* data, ulong len);
35
36 void init (const char* data, ulong len);
37 void rewind ();
38 void seek (ulong pos);
39 void clear ();
40 void merge (const Bytestream& other);
41 bool tryMerge (const Bytestream& other);
42 void resize (ulong len);
43 size_t len () const;
44 ulong bytesLeft () const;
45 ulong spaceLeft () const;
46 void growToFit (ulong bytes);
47 const uint8* data () const;
48
49 bool readBytes (uint8 numbytes, uint8* val);
50 bool readByte (uint8& val);
51 bool readShort (uint16& val);
52 bool readLong (uint32& val);
53 bool readString (str& val);
54 bool readFloat (float& val);
55
56 void writeBytes (uint8 numbytes, const uint8* val);
57 void writeByte (uint8 val);
58 void writeShort (uint16 val);
59 void writeLong (uint32 val);
60 void writeFloat (float val);
61 void writeString (str val);
62
63 Bytestream& operator<< (const Bytestream& other) {
64 merge (other);
65 return *this;
66 }
67
68 uint8& subscript (ulong idx);
69 const uint8& const_subscript (ulong idx) const;
70
71 uint8& operator[] (ulong idx) {
72 return subscript (idx);
73 }
74
75 const uint8& operator[] (ulong idx) const {
76 return const_subscript (idx);
77 }
78 };
79
80 #endif // BYTESTREAM_H

mercurial