]>
Commit | Line | Data |
---|---|---|
cf447356 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: datstrm.cpp | |
3 | // Purpose: Data stream classes | |
4 | // Author: Guilhem Lavaux | |
53663be8 | 5 | // Modified by: Mickael Gilabert |
cf447356 GL |
6 | // Created: 28/06/98 |
7 | // RCS-ID: $Id$ | |
13111b2a | 8 | // Copyright: (c) Guilhem Lavaux |
68379eaf | 9 | // Licence: wxWindows licence |
cf447356 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
cf447356 GL |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
ce4169a4 | 16 | #pragma hdrstop |
cf447356 GL |
17 | #endif |
18 | ||
ce4169a4 RR |
19 | #if wxUSE_STREAMS |
20 | ||
cf447356 | 21 | #include "wx/datstrm.h" |
3cacae09 | 22 | |
f4ada568 GL |
23 | // --------------------------------------------------------------------------- |
24 | // wxDataInputStream | |
25 | // --------------------------------------------------------------------------- | |
26 | ||
a99acbb0 VS |
27 | #if wxUSE_UNICODE |
28 | wxDataInputStream::wxDataInputStream(wxInputStream& s, wxMBConv& conv) | |
68379eaf | 29 | : m_input(&s), m_be_order(false), m_conv(conv) |
a99acbb0 | 30 | #else |
3d4c6a21 | 31 | wxDataInputStream::wxDataInputStream(wxInputStream& s) |
68379eaf | 32 | : m_input(&s), m_be_order(false) |
a99acbb0 | 33 | #endif |
cf447356 | 34 | { |
cf447356 GL |
35 | } |
36 | ||
41b0a113 RL |
37 | wxUint64 wxDataInputStream::Read64() |
38 | { | |
39 | wxUint64 i64; | |
40 | ||
41 | m_input->Read(&i64, 8); | |
42 | ||
43 | if (m_be_order) | |
44 | return wxUINT64_SWAP_ON_LE(i64); | |
45 | else | |
46 | return wxUINT64_SWAP_ON_BE(i64); | |
47 | } | |
48 | ||
7b8bd818 | 49 | wxUint32 wxDataInputStream::Read32() |
cf447356 | 50 | { |
5a96d2f4 | 51 | wxUint32 i32; |
cf447356 | 52 | |
5a96d2f4 | 53 | m_input->Read(&i32, 4); |
cf447356 | 54 | |
5a96d2f4 GL |
55 | if (m_be_order) |
56 | return wxUINT32_SWAP_ON_LE(i32); | |
57 | else | |
58 | return wxUINT32_SWAP_ON_BE(i32); | |
cf447356 GL |
59 | } |
60 | ||
7b8bd818 | 61 | wxUint16 wxDataInputStream::Read16() |
cf447356 | 62 | { |
5a96d2f4 | 63 | wxUint16 i16; |
cf447356 | 64 | |
5a96d2f4 | 65 | m_input->Read(&i16, 2); |
cf447356 | 66 | |
5a96d2f4 GL |
67 | if (m_be_order) |
68 | return wxUINT16_SWAP_ON_LE(i16); | |
69 | else | |
70 | return wxUINT16_SWAP_ON_BE(i16); | |
cf447356 GL |
71 | } |
72 | ||
7b8bd818 | 73 | wxUint8 wxDataInputStream::Read8() |
cf447356 | 74 | { |
7b8bd818 | 75 | wxUint8 buf; |
cf447356 | 76 | |
5a96d2f4 | 77 | m_input->Read(&buf, 1); |
7b8bd818 | 78 | return (wxUint8)buf; |
cf447356 GL |
79 | } |
80 | ||
5260b1c5 JS |
81 | // Must be at global scope for VC++ 5 |
82 | extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes); | |
83 | ||
3d4c6a21 | 84 | double wxDataInputStream::ReadDouble() |
cf447356 | 85 | { |
47d67540 | 86 | #if wxUSE_APPLE_IEEE |
cf447356 GL |
87 | char buf[10]; |
88 | ||
fae05df5 | 89 | m_input->Read(buf, 10); |
cf447356 GL |
90 | return ConvertFromIeeeExtended((unsigned char *)buf); |
91 | #else | |
92 | return 0.0; | |
93 | #endif | |
94 | } | |
95 | ||
3d4c6a21 | 96 | wxString wxDataInputStream::ReadString() |
eafc087e | 97 | { |
13111b2a | 98 | size_t len; |
eafc087e | 99 | |
eafc087e | 100 | len = Read32(); |
eafc087e | 101 | |
39a16cb4 VS |
102 | if (len > 0) |
103 | { | |
2ae47e3f | 104 | #if wxUSE_UNICODE |
de564874 MB |
105 | wxCharBuffer tmp(len + 1); |
106 | m_input->Read(tmp.data(), len); | |
107 | tmp.data()[len] = '\0'; | |
108 | wxString ret(m_conv.cMB2WX(tmp.data())); | |
2ae47e3f | 109 | #else |
2b5f62a0 | 110 | wxString ret; |
de564874 | 111 | m_input->Read( wxStringBuffer(ret, len), len); |
2ae47e3f | 112 | #endif |
2b5f62a0 | 113 | return ret; |
39a16cb4 | 114 | } |
38caaa61 | 115 | else |
39a16cb4 | 116 | return wxEmptyString; |
eafc087e | 117 | } |
13111b2a | 118 | |
53663be8 VZ |
119 | void wxDataInputStream::Read64(wxUint64 *buffer, size_t size) |
120 | { | |
121 | m_input->Read(buffer, size * 8); | |
122 | ||
123 | if (m_be_order) | |
124 | { | |
125 | for (wxUint32 i=0; i<size; i++) | |
126 | { | |
127 | wxUint64 v = wxUINT64_SWAP_ON_LE(*buffer); | |
128 | *(buffer++) = v; | |
129 | } | |
130 | } | |
131 | else | |
132 | { | |
133 | for (wxUint32 i=0; i<size; i++) | |
134 | { | |
135 | wxUint64 v = wxUINT64_SWAP_ON_BE(*buffer); | |
136 | *(buffer++) = v; | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | void wxDataInputStream::Read32(wxUint32 *buffer, size_t size) | |
142 | { | |
7448de8d | 143 | m_input->Read(buffer, size * 4); |
53663be8 | 144 | |
7448de8d | 145 | if (m_be_order) |
53663be8 | 146 | { |
7448de8d WS |
147 | for (wxUint32 i=0; i<size; i++) |
148 | { | |
149 | wxUint32 v = wxUINT32_SWAP_ON_LE(*buffer); | |
150 | *(buffer++) = v; | |
151 | } | |
53663be8 | 152 | } |
7448de8d | 153 | else |
53663be8 | 154 | { |
7448de8d WS |
155 | for (wxUint32 i=0; i<size; i++) |
156 | { | |
157 | wxUint32 v = wxUINT32_SWAP_ON_BE(*buffer); | |
158 | *(buffer++) = v; | |
159 | } | |
53663be8 | 160 | } |
53663be8 VZ |
161 | } |
162 | ||
163 | void wxDataInputStream::Read16(wxUint16 *buffer, size_t size) | |
164 | { | |
165 | m_input->Read(buffer, size * 2); | |
166 | ||
167 | if (m_be_order) | |
168 | { | |
169 | for (wxUint32 i=0; i<size; i++) | |
170 | { | |
171 | wxUint16 v = wxUINT16_SWAP_ON_LE(*buffer); | |
172 | *(buffer++) = v; | |
173 | } | |
174 | } | |
175 | else | |
176 | { | |
177 | for (wxUint32 i=0; i<size; i++) | |
178 | { | |
179 | wxUint16 v = wxUINT16_SWAP_ON_BE(*buffer); | |
180 | *(buffer++) = v; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | void wxDataInputStream::Read8(wxUint8 *buffer, size_t size) | |
186 | { | |
187 | m_input->Read(buffer, size); | |
188 | } | |
189 | ||
190 | void wxDataInputStream::ReadDouble(double *buffer, size_t size) | |
191 | { | |
192 | for (wxUint32 i=0; i<size; i++) | |
193 | { | |
194 | *(buffer++) = ReadDouble(); | |
195 | } | |
196 | } | |
197 | ||
fae05df5 GL |
198 | wxDataInputStream& wxDataInputStream::operator>>(wxString& s) |
199 | { | |
200 | s = ReadString(); | |
201 | return *this; | |
202 | } | |
203 | ||
204 | wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c) | |
205 | { | |
206 | c = (wxInt8)Read8(); | |
207 | return *this; | |
208 | } | |
209 | ||
210 | wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i) | |
211 | { | |
212 | i = (wxInt16)Read16(); | |
213 | return *this; | |
214 | } | |
215 | ||
216 | wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i) | |
217 | { | |
218 | i = (wxInt32)Read32(); | |
219 | return *this; | |
220 | } | |
221 | ||
222 | wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c) | |
223 | { | |
224 | c = Read8(); | |
225 | return *this; | |
226 | } | |
227 | ||
228 | wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i) | |
229 | { | |
230 | i = Read16(); | |
231 | return *this; | |
232 | } | |
233 | ||
234 | wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i) | |
235 | { | |
236 | i = Read32(); | |
237 | return *this; | |
238 | } | |
239 | ||
41b0a113 RL |
240 | wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i) |
241 | { | |
242 | i = Read64(); | |
243 | return *this; | |
244 | } | |
245 | ||
fae05df5 GL |
246 | wxDataInputStream& wxDataInputStream::operator>>(double& i) |
247 | { | |
248 | i = ReadDouble(); | |
249 | return *this; | |
250 | } | |
251 | ||
252 | wxDataInputStream& wxDataInputStream::operator>>(float& f) | |
253 | { | |
254 | f = (float)ReadDouble(); | |
255 | return *this; | |
256 | } | |
eafc087e | 257 | |
f4ada568 GL |
258 | // --------------------------------------------------------------------------- |
259 | // wxDataOutputStream | |
260 | // --------------------------------------------------------------------------- | |
261 | ||
a99acbb0 VS |
262 | #if wxUSE_UNICODE |
263 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, wxMBConv& conv) | |
68379eaf | 264 | : m_output(&s), m_be_order(false), m_conv(conv) |
a99acbb0 | 265 | #else |
3d4c6a21 | 266 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) |
68379eaf | 267 | : m_output(&s), m_be_order(false) |
a99acbb0 | 268 | #endif |
3d4c6a21 GL |
269 | { |
270 | } | |
271 | ||
41b0a113 RL |
272 | void wxDataOutputStream::Write64(wxUint64 i) |
273 | { | |
274 | wxUint64 i64; | |
275 | ||
276 | if (m_be_order) | |
277 | i64 = wxUINT64_SWAP_ON_LE(i); | |
278 | else | |
279 | i64 = wxUINT64_SWAP_ON_BE(i); | |
280 | m_output->Write(&i64, 8); | |
281 | } | |
282 | ||
7b8bd818 | 283 | void wxDataOutputStream::Write32(wxUint32 i) |
cf447356 | 284 | { |
5a96d2f4 | 285 | wxUint32 i32; |
cf447356 | 286 | |
5a96d2f4 GL |
287 | if (m_be_order) |
288 | i32 = wxUINT32_SWAP_ON_LE(i); | |
289 | else | |
290 | i32 = wxUINT32_SWAP_ON_BE(i); | |
291 | m_output->Write(&i32, 4); | |
cf447356 GL |
292 | } |
293 | ||
7b8bd818 | 294 | void wxDataOutputStream::Write16(wxUint16 i) |
cf447356 | 295 | { |
5a96d2f4 | 296 | wxUint16 i16; |
cf447356 | 297 | |
5a96d2f4 GL |
298 | if (m_be_order) |
299 | i16 = wxUINT16_SWAP_ON_LE(i); | |
300 | else | |
301 | i16 = wxUINT16_SWAP_ON_BE(i); | |
302 | ||
303 | m_output->Write(&i16, 2); | |
cf447356 GL |
304 | } |
305 | ||
7b8bd818 | 306 | void wxDataOutputStream::Write8(wxUint8 i) |
cf447356 | 307 | { |
fae05df5 | 308 | m_output->Write(&i, 1); |
cf447356 GL |
309 | } |
310 | ||
3d4c6a21 | 311 | void wxDataOutputStream::WriteString(const wxString& string) |
eafc087e | 312 | { |
a99acbb0 VS |
313 | #if wxUSE_UNICODE |
314 | const wxWX2MBbuf buf = string.mb_str(m_conv); | |
315 | #else | |
c980c992 | 316 | const wxWX2MBbuf buf = string.mb_str(); |
a99acbb0 VS |
317 | #endif |
318 | size_t len = strlen(buf); | |
319 | Write32(len); | |
320 | if (len > 0) | |
321 | m_output->Write(buf, len); | |
cf447356 GL |
322 | } |
323 | ||
5260b1c5 JS |
324 | // Must be at global scope for VC++ 5 |
325 | extern "C" void ConvertToIeeeExtended(double num, unsigned char *bytes); | |
326 | ||
3d4c6a21 | 327 | void wxDataOutputStream::WriteDouble(double d) |
cf447356 | 328 | { |
cf447356 GL |
329 | char buf[10]; |
330 | ||
47d67540 | 331 | #if wxUSE_APPLE_IEEE |
cf447356 | 332 | ConvertToIeeeExtended(d, (unsigned char *)buf); |
0e338ff9 | 333 | #else |
c5ceb215 | 334 | #if !defined(__VMS__) && !defined(__GNUG__) |
338dd992 JJ |
335 | # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" |
336 | #endif | |
337 | buf[0] = '\0'; | |
0e338ff9 | 338 | #endif |
fae05df5 GL |
339 | m_output->Write(buf, 10); |
340 | } | |
341 | ||
53663be8 VZ |
342 | void wxDataOutputStream::Write64(const wxUint64 *buffer, size_t size) |
343 | { | |
344 | if (m_be_order) | |
345 | { | |
346 | for (wxUint32 i=0; i<size ;i++) | |
347 | { | |
348 | wxUint64 i64 = wxUINT64_SWAP_ON_LE(*buffer); | |
349 | buffer++; | |
350 | m_output->Write(&i64, 8); | |
351 | } | |
352 | } | |
353 | else | |
354 | { | |
355 | for (wxUint32 i=0; i<size ;i++) | |
356 | { | |
357 | wxUint64 i64 = wxUINT64_SWAP_ON_BE(*buffer); | |
358 | buffer++; | |
359 | m_output->Write(&i64, 8); | |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | void wxDataOutputStream::Write32(const wxUint32 *buffer, size_t size) | |
365 | { | |
366 | if (m_be_order) | |
367 | { | |
368 | for (wxUint32 i=0; i<size ;i++) | |
369 | { | |
370 | wxUint32 i32 = wxUINT32_SWAP_ON_LE(*buffer); | |
371 | buffer++; | |
372 | m_output->Write(&i32, 4); | |
373 | } | |
374 | } | |
375 | else | |
376 | { | |
377 | for (wxUint32 i=0; i<size ;i++) | |
378 | { | |
379 | wxUint32 i32 = wxUINT32_SWAP_ON_BE(*buffer); | |
380 | buffer++; | |
381 | m_output->Write(&i32, 4); | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | void wxDataOutputStream::Write16(const wxUint16 *buffer, size_t size) | |
387 | { | |
388 | if (m_be_order) | |
389 | { | |
390 | for (wxUint32 i=0; i<size ;i++) | |
391 | { | |
392 | wxUint16 i16 = wxUINT16_SWAP_ON_LE(*buffer); | |
393 | buffer++; | |
394 | m_output->Write(&i16, 2); | |
395 | } | |
396 | } | |
397 | else | |
398 | { | |
399 | for (wxUint32 i=0; i<size ;i++) | |
400 | { | |
401 | wxUint16 i16 = wxUINT16_SWAP_ON_BE(*buffer); | |
402 | buffer++; | |
403 | m_output->Write(&i16, 2); | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | void wxDataOutputStream::Write8(const wxUint8 *buffer, size_t size) | |
409 | { | |
410 | m_output->Write(buffer, size); | |
411 | } | |
412 | ||
413 | void wxDataOutputStream::WriteDouble(const double *buffer, size_t size) | |
414 | { | |
415 | for (wxUint32 i=0; i<size; i++) | |
416 | { | |
417 | WriteDouble(*(buffer++)); | |
418 | } | |
419 | } | |
420 | ||
fae05df5 GL |
421 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string) |
422 | { | |
423 | Write32(wxStrlen(string)); | |
424 | m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar)); | |
425 | return *this; | |
426 | } | |
427 | ||
38caaa61 | 428 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string) |
fae05df5 GL |
429 | { |
430 | WriteString(string); | |
431 | return *this; | |
432 | } | |
433 | ||
434 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c) | |
435 | { | |
436 | Write8((wxUint8)c); | |
437 | return *this; | |
438 | } | |
439 | ||
440 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i) | |
441 | { | |
442 | Write16((wxUint16)i); | |
443 | return *this; | |
444 | } | |
445 | ||
446 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i) | |
447 | { | |
448 | Write32((wxUint32)i); | |
449 | return *this; | |
450 | } | |
451 | ||
452 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c) | |
453 | { | |
454 | Write8(c); | |
455 | return *this; | |
456 | } | |
457 | ||
458 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i) | |
459 | { | |
460 | Write16(i); | |
461 | return *this; | |
462 | } | |
463 | ||
464 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i) | |
465 | { | |
466 | Write32(i); | |
467 | return *this; | |
468 | } | |
469 | ||
41b0a113 RL |
470 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i) |
471 | { | |
472 | Write64(i); | |
473 | return *this; | |
474 | } | |
475 | ||
fae05df5 GL |
476 | wxDataOutputStream& wxDataOutputStream::operator<<(double f) |
477 | { | |
478 | WriteDouble(f); | |
479 | return *this; | |
480 | } | |
481 | ||
482 | wxDataOutputStream& wxDataOutputStream::operator<<(float f) | |
483 | { | |
484 | WriteDouble((double)f); | |
485 | return *this; | |
cf447356 | 486 | } |
ce4169a4 RR |
487 | |
488 | #endif | |
489 | // wxUSE_STREAMS | |
13111b2a | 490 |