70 { |
71 { |
71 delete[] m_data; |
72 delete[] m_data; |
72 } |
73 } |
73 |
74 |
74 // ------------------------------------------------------------------------------------------------- |
75 // ------------------------------------------------------------------------------------------------- |
75 METHOD |
76 Bytestream& Bytestream::operator= (const Bytestream& other) |
76 Bytestream::operator= (const Bytestream& other) -> Bytestream& |
|
77 { |
77 { |
78 init (other.data(), other.written_length()); |
78 init (other.data(), other.written_length()); |
79 return *this; |
79 return *this; |
80 } |
80 } |
81 |
81 |
82 // ------------------------------------------------------------------------------------------------- |
82 // ------------------------------------------------------------------------------------------------- |
83 // |
83 // |
84 METHOD |
84 void Bytestream::resize (unsigned long newsize) |
85 Bytestream::resize (unsigned long newsize) -> void |
|
86 { |
85 { |
87 Vector<unsigned char> olddata; |
86 Vector<unsigned char> olddata; |
88 unsigned long oldsize = 0L; |
87 unsigned long oldsize = 0L; |
89 |
88 |
90 if (m_data != nullptr) |
89 if (m_data != nullptr) |
102 memcpy (m_data, olddata, min (oldsize, newsize)); |
101 memcpy (m_data, olddata, min (oldsize, newsize)); |
103 } |
102 } |
104 |
103 |
105 // ------------------------------------------------------------------------------------------------- |
104 // ------------------------------------------------------------------------------------------------- |
106 // |
105 // |
107 METHOD |
106 void Bytestream::init (const unsigned char* data, unsigned long length) |
108 Bytestream::init (const unsigned char* data, unsigned long length) -> void |
|
109 { |
107 { |
110 resize (length); |
108 resize (length); |
111 memcpy (m_data, data, length); |
109 memcpy (m_data, data, length); |
112 m_cursor = &m_data[0]; |
110 m_cursor = &m_data[0]; |
113 m_writtenLength = length; |
111 m_writtenLength = length; |
114 } |
112 } |
115 |
113 |
116 // ------------------------------------------------------------------------------------------------- |
114 // ------------------------------------------------------------------------------------------------- |
117 // |
115 // |
118 METHOD |
116 void Bytestream::clear() |
119 Bytestream::clear() -> void |
|
120 { |
117 { |
121 m_cursor = &m_data[0]; |
118 m_cursor = &m_data[0]; |
122 m_writtenLength = 0; |
119 m_writtenLength = 0; |
123 } |
120 } |
124 |
121 |
125 // ------------------------------------------------------------------------------------------------- |
122 // ------------------------------------------------------------------------------------------------- |
126 // |
123 // |
127 METHOD |
124 void Bytestream::ensure_read_space (unsigned int bytes) |
128 Bytestream::ensure_read_space (unsigned int bytes) -> void |
|
129 { |
125 { |
130 if (bytes_left() < bytes) |
126 if (bytes_left() < bytes) |
131 { |
127 { |
132 int bytesPast = bytes - bytes_left(); |
128 int bytesPast = bytes - bytes_left(); |
133 |
129 |
177 return result; |
170 return result; |
178 } |
171 } |
179 |
172 |
180 // ------------------------------------------------------------------------------------------------- |
173 // ------------------------------------------------------------------------------------------------- |
181 // |
174 // |
182 METHOD |
175 float Bytestream::read_float() |
183 Bytestream::read_float() -> float |
|
184 { |
176 { |
185 float value; |
177 float value; |
186 int intvalue = read_long(); |
178 int intvalue = read_long(); |
187 memcpy (&value, &intvalue, sizeof intvalue); |
179 memcpy (&value, &intvalue, sizeof intvalue); |
188 return value; |
180 return value; |
189 } |
181 } |
190 |
182 |
191 // ------------------------------------------------------------------------------------------------- |
183 // ------------------------------------------------------------------------------------------------- |
192 // |
184 // |
193 METHOD |
185 String Bytestream::read_string() |
194 Bytestream::read_string() -> String |
|
195 { |
186 { |
196 // Zandronum sends strings of maximum 2048 characters, though it only |
187 // Zandronum sends strings of maximum 2048 characters, though it only |
197 // reads 2047-character long ones so I guess we can follow up and do |
188 // reads 2047-character long ones so I guess we can follow up and do |
198 // the same :-) |
189 // the same :-) |
199 static char buffer[MAX_NETWORK_STRING]; |
190 static char buffer[MAX_NETWORK_STRING]; |
222 return String (buffer); |
213 return String (buffer); |
223 } |
214 } |
224 |
215 |
225 // ------------------------------------------------------------------------------------------------- |
216 // ------------------------------------------------------------------------------------------------- |
226 // |
217 // |
227 METHOD |
218 void Bytestream::read (unsigned char* buffer, unsigned long length) |
228 Bytestream::read (unsigned char* buffer, unsigned long length) -> void |
|
229 { |
219 { |
230 ensure_read_space (length); |
220 ensure_read_space (length); |
231 memcpy (buffer, m_cursor, length); |
221 memcpy (buffer, m_cursor, length); |
232 m_cursor += length; |
222 m_cursor += length; |
233 } |
223 } |
234 |
224 |
235 // ------------------------------------------------------------------------------------------------- |
225 // ------------------------------------------------------------------------------------------------- |
236 // |
226 // |
237 METHOD |
227 void Bytestream::write (unsigned char val) |
238 Bytestream::write (unsigned char val) -> void |
|
239 { |
228 { |
240 *m_cursor++ = val; |
229 *m_cursor++ = val; |
241 m_writtenLength++; |
230 m_writtenLength++; |
242 } |
231 } |
243 |
232 |
244 // ------------------------------------------------------------------------------------------------- |
233 // ------------------------------------------------------------------------------------------------- |
245 // |
234 // |
246 METHOD |
235 void Bytestream::write (const unsigned char* val, unsigned int length) |
247 Bytestream::write (const unsigned char* val, unsigned int length) -> void |
|
248 { |
236 { |
249 grow_to_fit (length); |
237 grow_to_fit (length); |
250 memcpy (m_cursor, val, length); |
238 memcpy (m_cursor, val, length); |
251 m_cursor += length; |
239 m_cursor += length; |
252 m_writtenLength += length; |
240 m_writtenLength += length; |
253 } |
241 } |
254 |
242 |
255 // ------------------------------------------------------------------------------------------------- |
243 // ------------------------------------------------------------------------------------------------- |
256 // |
244 // |
257 METHOD |
245 void Bytestream::grow_to_fit (unsigned long bytes) |
258 Bytestream::grow_to_fit (unsigned long bytes) -> void |
|
259 { |
246 { |
260 if (space_left() < bytes) |
247 if (space_left() < bytes) |
261 resize (allocated_size() + bytes + 128); |
248 resize (allocated_size() + bytes + 128); |
262 } |
249 } |
263 |
250 |
264 // ------------------------------------------------------------------------------------------------- |
251 // ------------------------------------------------------------------------------------------------- |
265 // |
252 // |
266 METHOD |
253 void Bytestream::write_byte (char val) |
267 Bytestream::write_byte (char val) -> void |
|
268 { |
254 { |
269 grow_to_fit (1); |
255 grow_to_fit (1); |
270 write (val); |
256 write (val); |
271 } |
257 } |
272 |
258 |
273 // ------------------------------------------------------------------------------------------------- |
259 // ------------------------------------------------------------------------------------------------- |
274 // |
260 // |
275 METHOD |
261 void Bytestream::write_short (short int val) |
276 Bytestream::write_short (short int val) -> void |
|
277 { |
262 { |
278 grow_to_fit (2); |
263 grow_to_fit (2); |
279 |
264 |
280 for (int i = 0; i < 2; ++i) |
265 for (int i = 0; i < 2; ++i) |
281 write ((val >> (i * 8)) & 0xFF); |
266 write ((val >> (i * 8)) & 0xFF); |
282 } |
267 } |
283 |
268 |
284 // ------------------------------------------------------------------------------------------------- |
269 // ------------------------------------------------------------------------------------------------- |
285 // |
270 // |
286 METHOD |
271 void Bytestream::write_long (long int val) |
287 Bytestream::write_long (long int val) -> void |
|
288 { |
272 { |
289 grow_to_fit (4); |
273 grow_to_fit (4); |
290 |
274 |
291 for (int i = 0; i < 4; ++i) |
275 for (int i = 0; i < 4; ++i) |
292 write ((val >> (i * 8)) & 0xFF); |
276 write ((val >> (i * 8)) & 0xFF); |
293 } |
277 } |
294 |
278 |
295 // ------------------------------------------------------------------------------------------------- |
279 // ------------------------------------------------------------------------------------------------- |
296 // |
280 // |
297 METHOD |
281 void Bytestream::write_float (float val) |
298 Bytestream::write_float (float val) -> void |
|
299 { |
282 { |
300 // I know this is probably dangerous but this is what Zandronum does so yeah |
283 // I know this is probably dangerous but this is what Zandronum does so yeah |
301 int intvalue; |
284 int intvalue; |
302 memcpy (&intvalue, &val, sizeof val); |
285 memcpy (&intvalue, &val, sizeof val); |
303 write_long (intvalue); |
286 write_long (intvalue); |
304 } |
287 } |
305 |
288 |
306 // ------------------------------------------------------------------------------------------------- |
289 // ------------------------------------------------------------------------------------------------- |
307 // |
290 // |
308 METHOD |
291 void Bytestream::write_string (const String& val) |
309 Bytestream::write_string (const String& val) -> void |
|
310 { |
292 { |
311 grow_to_fit (val.length() + 1); |
293 grow_to_fit (val.length() + 1); |
312 write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); |
294 write (reinterpret_cast<const unsigned char*> (val.chars()), val.length()); |
313 write (0); |
295 write (0); |
314 } |
296 } |
315 |
297 |
316 // ------------------------------------------------------------------------------------------------- |
298 // ------------------------------------------------------------------------------------------------- |
317 // |
299 // |
318 METHOD |
300 void Bytestream::write_buffer (const Bytestream& other) |
319 Bytestream::write_buffer (const Bytestream& other) -> void |
|
320 { |
301 { |
321 write (other.data(), other.written_length()); |
302 write (other.data(), other.written_length()); |
322 } |
303 } |
|
304 |
|
305 END_ZFC_NAMESPACE |