Fri, 15 May 2015 20:06:56 +0300
Update license headers
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
1 | /* |
73
07dda51a7a8e
Update license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
2 | Copyright 2014, 2015 Teemu Piippo |
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
3 | All rights reserved. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
4 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
7 | are met: |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
8 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
14 | 3. Neither the name of the copyright holder nor the names of its |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
15 | contributors may be used to endorse or promote products derived from |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
16 | this software without specific prior written permission. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
17 | |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
20 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
21 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
22 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
23 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
24 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
29 | */ |
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
30 | |
5 | 31 | #pragma once |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
32 | #include <stdexcept> |
5 | 33 | #include "../main.h" |
34 | ||
35 | class String; | |
36 | ||
37 | enum { MAX_NETWORK_STRING = 0x800 }; | |
38 | ||
39 | // TODO: Make able to handle big-endian too | |
40 | class Bytestream | |
41 | { | |
42 | public: | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
43 | class IOError : public std::exception |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
44 | { |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
45 | String m_message; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
46 | |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
47 | public: |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
48 | IOError (String message) : |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
49 | m_message (message) {} |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
50 | |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
51 | inline METHOD |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
52 | what() const throw() -> const char* |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
53 | { |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
54 | return m_message.chars(); |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
55 | } |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
56 | }; |
5 | 57 | |
58 | Bytestream (unsigned long length = 0x800); | |
59 | Bytestream (const unsigned char* data, unsigned long length); | |
60 | Bytestream (const Vector<unsigned char>& bytes); | |
11 | 61 | Bytestream (const Bytestream& other); |
5 | 62 | ~Bytestream(); |
63 | ||
64 | inline METHOD allocated_size() const -> unsigned long; | |
65 | inline METHOD bytes_left() const -> unsigned long; | |
66 | METHOD clear() -> void; | |
67 | inline METHOD data() -> unsigned char*; | |
68 | inline METHOD data() const -> const unsigned char*; | |
69 | METHOD grow_to_fit (unsigned long bytes) -> void; | |
70 | inline METHOD position() const -> unsigned long; | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
71 | METHOD read (unsigned char* buffer, unsigned long length) -> void; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
72 | METHOD read_byte() -> char; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
73 | METHOD read_short() -> short int; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
74 | METHOD read_long() -> long int; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
75 | METHOD read_string() -> String; |
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
76 | METHOD read_float() -> float; |
5 | 77 | METHOD resize (unsigned long length) -> void; |
78 | inline METHOD rewind() -> void; | |
79 | inline METHOD seek (unsigned long pos) -> void; | |
11 | 80 | inline METHOD to_vector() const -> Vector<unsigned char>; |
5 | 81 | METHOD write (const unsigned char* val, unsigned int length) -> void; |
82 | METHOD write_buffer (const Bytestream& other) -> void; | |
83 | METHOD write_buffer (const Vector<unsigned char>& other) -> void; | |
84 | METHOD write_byte (char val) -> void; | |
85 | METHOD write_double (double val) -> void; | |
86 | METHOD write_float (float val) -> void; | |
87 | METHOD write_long (long int val) -> void; | |
88 | METHOD write_short (short int val) -> void; | |
89 | METHOD write_string (const String& val) -> void; | |
90 | inline METHOD written_length() const -> unsigned long; | |
91 | ||
92 | inline METHOD operator[] (unsigned long idx) -> unsigned char&; | |
93 | inline METHOD operator[] (unsigned long idx) const -> unsigned char; | |
11 | 94 | METHOD operator= (const Bytestream& other) -> Bytestream&; |
5 | 95 | |
96 | private: | |
97 | unsigned char* m_data; | |
98 | unsigned char* m_cursor; | |
99 | unsigned long m_allocatedSize; | |
100 | unsigned long m_writtenLength; | |
101 | ||
102 | METHOD init (const unsigned char* data, unsigned long length) -> void; | |
103 | METHOD write (unsigned char val) -> void; | |
13
09dcaeaa216b
- to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents:
11
diff
changeset
|
104 | METHOD ensure_read_space (unsigned int bytes) -> void; |
5 | 105 | inline METHOD space_left() const -> unsigned long; |
106 | }; | |
107 | ||
108 | // ------------------------------------------------------------------------------------------------- | |
109 | // | |
110 | inline METHOD | |
111 | Bytestream::allocated_size() const -> unsigned long | |
112 | { | |
113 | return m_allocatedSize; | |
114 | } | |
115 | ||
116 | // ------------------------------------------------------------------------------------------------- | |
117 | // | |
118 | inline METHOD | |
119 | Bytestream::written_length() const -> unsigned long | |
120 | { | |
121 | return m_writtenLength; | |
122 | } | |
123 | ||
124 | // ------------------------------------------------------------------------------------------------- | |
125 | // | |
126 | inline METHOD | |
127 | Bytestream::operator[] (unsigned long idx) -> unsigned char& | |
128 | { | |
129 | return m_data[idx]; | |
130 | } | |
131 | ||
132 | // ------------------------------------------------------------------------------------------------- | |
133 | // | |
134 | inline METHOD | |
135 | Bytestream::operator[] (unsigned long idx) const -> unsigned char | |
136 | { | |
137 | return m_data[idx]; | |
138 | } | |
139 | ||
140 | // ------------------------------------------------------------------------------------------------- | |
141 | // | |
142 | inline METHOD | |
143 | Bytestream::position() const -> unsigned long | |
144 | { | |
145 | return m_cursor - m_data; | |
146 | } | |
147 | ||
148 | // ------------------------------------------------------------------------------------------------- | |
149 | // | |
150 | inline METHOD | |
151 | Bytestream::seek (unsigned long pos) -> void | |
152 | { | |
153 | m_cursor = m_data + pos; | |
154 | } | |
155 | ||
156 | // ------------------------------------------------------------------------------------------------- | |
157 | // | |
158 | inline METHOD | |
159 | Bytestream::rewind() -> void | |
160 | { | |
161 | m_cursor = m_data; | |
162 | } | |
163 | ||
164 | // ------------------------------------------------------------------------------------------------- | |
165 | // | |
166 | inline METHOD | |
167 | Bytestream::bytes_left() const -> unsigned long | |
168 | { | |
169 | return (m_writtenLength - (m_cursor - &m_data[0])); | |
170 | } | |
171 | ||
172 | // ------------------------------------------------------------------------------------------------- | |
173 | // | |
174 | inline METHOD | |
175 | Bytestream::space_left() const -> unsigned long | |
176 | { | |
177 | return (m_allocatedSize - m_writtenLength); | |
178 | } | |
179 | ||
180 | // ------------------------------------------------------------------------------------------------- | |
181 | // | |
182 | inline METHOD | |
183 | Bytestream::data() -> unsigned char* | |
184 | { | |
185 | return m_data; | |
186 | } | |
187 | ||
188 | // ------------------------------------------------------------------------------------------------- | |
189 | // | |
190 | inline METHOD | |
191 | Bytestream::data() const -> const unsigned char* | |
192 | { | |
193 | return m_data; | |
194 | } | |
11 | 195 | |
196 | // ------------------------------------------------------------------------------------------------- | |
197 | // | |
198 | inline METHOD | |
199 | Bytestream::to_vector() const -> Vector<unsigned char> | |
200 | { | |
201 | return Vector<unsigned char> (m_data, m_writtenLength); | |
202 | } |