Thu, 23 Jul 2015 17:51:35 +0300
NULL -> nullptr
|
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:
61
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 | #include "bytestream.h" |
| 32 | #include <string.h> | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
33 | BEGIN_ZFC_NAMESPACE |
| 5 | 34 | |
| 35 | // ------------------------------------------------------------------------------------------------- | |
| 36 | // | |
| 37 | Bytestream::Bytestream (unsigned long length) : | |
| 38 | m_data (nullptr) | |
| 39 | { | |
| 40 | resize (length); | |
| 41 | clear(); | |
| 42 | } | |
| 43 | ||
| 44 | // ------------------------------------------------------------------------------------------------- | |
| 45 | // | |
| 46 | Bytestream::Bytestream (const unsigned char* data, unsigned long length) : | |
| 47 | m_data (nullptr) | |
| 48 | { | |
| 49 | init (data, length); | |
| 50 | } | |
| 51 | ||
| 52 | // ------------------------------------------------------------------------------------------------- | |
| 53 | // | |
| 54 | Bytestream::Bytestream (const Vector<unsigned char>& bytes) : | |
| 55 | m_data (nullptr) | |
| 56 | { | |
| 57 | init (bytes.data(), bytes.size()); | |
| 58 | } | |
| 59 | ||
| 60 | // ------------------------------------------------------------------------------------------------- | |
| 61 | // | |
| 11 | 62 | Bytestream::Bytestream (const Bytestream& other) : |
| 63 | m_data (nullptr) | |
| 64 | { | |
| 65 | init (other.data(), other.written_length()); | |
| 66 | } | |
| 67 | ||
| 68 | // ------------------------------------------------------------------------------------------------- | |
|
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
69 | // |
|
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
70 | Bytestream::~Bytestream() |
|
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
71 | { |
|
58
d175243ad169
- rcon sessions are no longer allocated on the heap
Teemu Piippo <crimsondusk64@gmail.com>
parents:
18
diff
changeset
|
72 | delete[] m_data; |
|
18
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
73 | } |
|
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
74 | |
|
56a1ac7d931b
- converted bytestream to use trailing return types
Teemu Piippo <crimsondusk64@gmail.com>
parents:
13
diff
changeset
|
75 | // ------------------------------------------------------------------------------------------------- |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
76 | Bytestream& Bytestream::operator= (const Bytestream& other) |
| 11 | 77 | { |
| 78 | init (other.data(), other.written_length()); | |
| 79 | return *this; | |
| 80 | } | |
| 81 | ||
| 82 | // ------------------------------------------------------------------------------------------------- | |
| 83 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
84 | void Bytestream::resize (unsigned long newsize) |
| 5 | 85 | { |
| 86 | Vector<unsigned char> olddata; | |
| 87 | unsigned long oldsize = 0L; | |
| 88 | ||
| 89 | if (m_data != nullptr) | |
| 90 | { | |
| 91 | oldsize = allocated_size(); | |
| 92 | olddata.resize (oldsize); | |
| 93 | memcpy (olddata.data(), m_data, oldsize); | |
| 94 | } | |
| 95 | ||
| 96 | delete[] m_data; | |
| 97 | m_allocatedSize = newsize; | |
| 98 | m_data = new unsigned char[newsize]; | |
| 99 | ||
|
94
294803d4ab5d
Fix bad comparision in Bytestream::resize
Teemu Piippo <tsapii@utu.fi>
parents:
88
diff
changeset
|
100 | if (oldsize > 0L) |
| 5 | 101 | memcpy (m_data, olddata, min (oldsize, newsize)); |
| 102 | } | |
| 103 | ||
| 104 | // ------------------------------------------------------------------------------------------------- | |
| 105 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
106 | void Bytestream::init (const unsigned char* data, unsigned long length) |
| 5 | 107 | { |
| 108 | resize (length); | |
| 109 | memcpy (m_data, data, length); | |
| 110 | m_cursor = &m_data[0]; | |
| 111 | m_writtenLength = length; | |
| 112 | } | |
| 113 | ||
| 114 | // ------------------------------------------------------------------------------------------------- | |
| 115 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
116 | void Bytestream::clear() |
| 5 | 117 | { |
| 118 | m_cursor = &m_data[0]; | |
| 119 | m_writtenLength = 0; | |
| 120 | } | |
| 121 | ||
| 122 | // ------------------------------------------------------------------------------------------------- | |
| 123 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
124 | void Bytestream::ensure_read_space (unsigned int bytes) |
| 5 | 125 | { |
|
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
|
126 | if (bytes_left() < bytes) |
|
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
|
127 | { |
|
83
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
128 | int bytesPast = bytes - bytes_left(); |
|
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
129 | |
|
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
130 | String message; |
|
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
131 | message.sprintf ("attempted to read %d byte%s past the end of bytestream", |
|
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
132 | bytesPast, bytesPast != -1 ? "s" : ""); |
|
08bfc3d9d2ae
Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
133 | throw IOError (message); |
|
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
|
134 | } |
| 5 | 135 | } |
| 136 | ||
| 137 | // ------------------------------------------------------------------------------------------------- | |
| 138 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
139 | char Bytestream::read_byte() |
| 5 | 140 | { |
|
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
|
141 | ensure_read_space (1); |
|
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
|
142 | return *m_cursor++; |
|
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
|
143 | } |
| 5 | 144 | |
|
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
|
145 | // ------------------------------------------------------------------------------------------------- |
|
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
|
146 | // |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
147 | short int Bytestream::read_short() |
|
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
|
148 | { |
|
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
|
149 | ensure_read_space (2); |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
150 | short int result = 0; |
| 5 | 151 | |
| 152 | for (int i = 0; i < 2; ++i) | |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
153 | result |= m_cursor[i] << (i * 8); |
| 5 | 154 | |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
155 | m_cursor += 2; |
|
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
156 | return result; |
| 5 | 157 | } |
| 158 | ||
| 159 | // ------------------------------------------------------------------------------------------------- | |
| 160 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
161 | long int Bytestream::read_long() |
| 5 | 162 | { |
|
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
|
163 | ensure_read_space (4); |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
164 | long int result = 0; |
| 5 | 165 | |
| 166 | for (int i = 0; i < 4; ++i) | |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
167 | result |= m_cursor[i] << (i * 8); |
| 5 | 168 | |
|
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
169 | m_cursor += 4; |
|
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
170 | return result; |
| 5 | 171 | } |
| 172 | ||
| 173 | // ------------------------------------------------------------------------------------------------- | |
| 174 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
175 | float Bytestream::read_float() |
| 5 | 176 | { |
|
61
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
177 | float value; |
|
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
178 | int intvalue = read_long(); |
|
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
179 | memcpy (&value, &intvalue, sizeof intvalue); |
|
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
180 | return value; |
| 5 | 181 | } |
| 182 | ||
| 183 | // ------------------------------------------------------------------------------------------------- | |
| 184 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
185 | String Bytestream::read_string() |
| 5 | 186 | { |
| 187 | // Zandronum sends strings of maximum 2048 characters, though it only | |
| 188 | // reads 2047-character long ones so I guess we can follow up and do | |
| 189 | // the same :-) | |
| 190 | static char buffer[MAX_NETWORK_STRING]; | |
| 191 | unsigned char* stringEnd; | |
| 192 | unsigned char* stringBegin = m_cursor; | |
| 193 | unsigned char* end = m_data + allocated_size(); | |
| 194 | ||
| 195 | // where's the end of the string? | |
| 196 | for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd) | |
| 197 | { | |
| 198 | if (stringEnd == end) | |
| 199 | // past the end of the buffer! Argh! | |
|
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
|
200 | throw IOError ("unterminated string in packet"); |
| 5 | 201 | } |
| 202 | ||
| 203 | m_cursor = stringEnd + 1; | |
| 204 | unsigned int length = stringEnd - m_cursor; | |
| 205 | ||
| 206 | // ensure we won't write past the buffer (note: we still moved | |
| 207 | // past the excess bytes in the above statement, those are ignored) | |
| 208 | if (length >= MAX_NETWORK_STRING) | |
| 209 | length = MAX_NETWORK_STRING - 1; | |
| 210 | ||
| 211 | memcpy (buffer, stringBegin, length); | |
| 212 | buffer[length] = '\0'; | |
| 213 | return String (buffer); | |
| 214 | } | |
| 215 | ||
| 216 | // ------------------------------------------------------------------------------------------------- | |
| 217 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
218 | void Bytestream::read (unsigned char* buffer, unsigned long length) |
| 5 | 219 | { |
|
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
|
220 | ensure_read_space (length); |
| 5 | 221 | memcpy (buffer, m_cursor, length); |
| 222 | m_cursor += length; | |
| 223 | } | |
| 224 | ||
| 225 | // ------------------------------------------------------------------------------------------------- | |
| 226 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
227 | void Bytestream::write (unsigned char val) |
| 5 | 228 | { |
| 229 | *m_cursor++ = val; | |
| 230 | m_writtenLength++; | |
| 231 | } | |
| 232 | ||
| 233 | // ------------------------------------------------------------------------------------------------- | |
| 234 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
235 | void Bytestream::write (const unsigned char* val, unsigned int length) |
| 5 | 236 | { |
| 237 | grow_to_fit (length); | |
| 238 | memcpy (m_cursor, val, length); | |
| 239 | m_cursor += length; | |
| 240 | m_writtenLength += length; | |
| 241 | } | |
| 242 | ||
| 243 | // ------------------------------------------------------------------------------------------------- | |
| 244 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
245 | void Bytestream::grow_to_fit (unsigned long bytes) |
| 5 | 246 | { |
| 247 | if (space_left() < bytes) | |
| 248 | resize (allocated_size() + bytes + 128); | |
| 249 | } | |
| 250 | ||
| 251 | // ------------------------------------------------------------------------------------------------- | |
| 252 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
253 | void Bytestream::write_byte (char val) |
| 5 | 254 | { |
| 255 | grow_to_fit (1); | |
| 256 | write (val); | |
| 257 | } | |
| 258 | ||
| 259 | // ------------------------------------------------------------------------------------------------- | |
| 260 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
261 | void Bytestream::write_short (short int val) |
| 5 | 262 | { |
| 263 | grow_to_fit (2); | |
| 264 | ||
| 265 | for (int i = 0; i < 2; ++i) | |
| 266 | write ((val >> (i * 8)) & 0xFF); | |
| 267 | } | |
| 268 | ||
| 269 | // ------------------------------------------------------------------------------------------------- | |
| 270 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
271 | void Bytestream::write_long (long int val) |
| 5 | 272 | { |
| 273 | grow_to_fit (4); | |
| 274 | ||
| 275 | for (int i = 0; i < 4; ++i) | |
| 276 | write ((val >> (i * 8)) & 0xFF); | |
| 277 | } | |
| 278 | ||
| 279 | // ------------------------------------------------------------------------------------------------- | |
| 280 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
281 | void Bytestream::write_float (float val) |
| 5 | 282 | { |
| 283 | // I know this is probably dangerous but this is what Zandronum does so yeah | |
|
61
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
284 | int intvalue; |
|
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
285 | memcpy (&intvalue, &val, sizeof val); |
|
cdf3c8af1545
- changed write_float/read_float to use memcpy
Teemu Piippo <crimsondusk64@gmail.com>
parents:
58
diff
changeset
|
286 | write_long (intvalue); |
| 5 | 287 | } |
| 288 | ||
| 289 | // ------------------------------------------------------------------------------------------------- | |
| 290 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
291 | void Bytestream::write_string (const String& val) |
| 5 | 292 | { |
| 293 | grow_to_fit (val.length() + 1); | |
| 294 | write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); | |
| 295 | write (0); | |
| 296 | } | |
| 297 | ||
| 298 | // ------------------------------------------------------------------------------------------------- | |
| 299 | // | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
300 | void Bytestream::write_buffer (const Bytestream& other) |
| 5 | 301 | { |
| 302 | write (other.data(), other.written_length()); | |
| 303 | } | |
|
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
304 | |
|
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
83
diff
changeset
|
305 | END_ZFC_NAMESPACE |