]>
Commit | Line | Data |
---|---|---|
cf447356 | 1 | ///////////////////////////////////////////////////////////////////////////// |
40319aa0 | 2 | // Name: src/common/datstrm.cpp |
cf447356 GL |
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" |
18680f86 WS |
22 | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/math.h" | |
25 | #endif //WX_PRECOMP | |
3cacae09 | 26 | |
789ab840 VZ |
27 | namespace |
28 | { | |
29 | ||
30 | // helper unions used to swap bytes of floats and doubles | |
31 | union Float32Data | |
32 | { | |
33 | wxFloat32 f; | |
34 | wxUint32 i; | |
35 | }; | |
36 | ||
37 | union Float64Data | |
38 | { | |
39 | wxFloat64 f; | |
40 | wxUint32 i[2]; | |
41 | }; | |
42 | ||
43 | } // anonymous namespace | |
44 | ||
61eb6bb6 VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // wxDataStreamBase | |
47 | // ---------------------------------------------------------------------------- | |
f4ada568 | 48 | |
61eb6bb6 | 49 | wxDataStreamBase::wxDataStreamBase(const wxMBConv& conv) |
a99acbb0 | 50 | #if wxUSE_UNICODE |
61eb6bb6 VZ |
51 | : m_conv(conv.Clone()) |
52 | #endif // wxUSE_UNICODE | |
53 | { | |
54 | // It is unused in non-Unicode build, so suppress a warning there. | |
55 | wxUnusedVar(conv); | |
56 | ||
57 | m_be_order = false; | |
789ab840 VZ |
58 | |
59 | // For compatibility with the existing data files, we use extended | |
60 | // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on. | |
61 | #if wxUSE_APPLE_IEEE | |
62 | m_useExtendedPrecision = true; | |
63 | #endif // wxUSE_APPLE_IEEE | |
61eb6bb6 VZ |
64 | } |
65 | ||
66 | #if wxUSE_UNICODE | |
67 | void wxDataStreamBase::SetConv( const wxMBConv &conv ) | |
cf447356 | 68 | { |
61eb6bb6 VZ |
69 | delete m_conv; |
70 | m_conv = conv.Clone(); | |
cf447356 | 71 | } |
61eb6bb6 | 72 | #endif |
cf447356 | 73 | |
61eb6bb6 | 74 | wxDataStreamBase::~wxDataStreamBase() |
d36c9347 VZ |
75 | { |
76 | #if wxUSE_UNICODE | |
77 | delete m_conv; | |
78 | #endif // wxUSE_UNICODE | |
79 | } | |
80 | ||
61eb6bb6 VZ |
81 | // --------------------------------------------------------------------------- |
82 | // wxDataInputStream | |
83 | // --------------------------------------------------------------------------- | |
84 | ||
85 | wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv) | |
86 | : wxDataStreamBase(conv), | |
87 | m_input(&s) | |
10c2f98a | 88 | { |
10c2f98a | 89 | } |
10c2f98a | 90 | |
216a72f3 | 91 | #if wxHAS_INT64 |
41b0a113 RL |
92 | wxUint64 wxDataInputStream::Read64() |
93 | { | |
216a72f3 VZ |
94 | wxUint64 tmp; |
95 | Read64(&tmp, 1); | |
96 | return tmp; | |
41b0a113 | 97 | } |
216a72f3 | 98 | #endif // wxHAS_INT64 |
41b0a113 | 99 | |
7b8bd818 | 100 | wxUint32 wxDataInputStream::Read32() |
cf447356 | 101 | { |
5a96d2f4 | 102 | wxUint32 i32; |
cf447356 | 103 | |
5a96d2f4 | 104 | m_input->Read(&i32, 4); |
cf447356 | 105 | |
5a96d2f4 GL |
106 | if (m_be_order) |
107 | return wxUINT32_SWAP_ON_LE(i32); | |
108 | else | |
109 | return wxUINT32_SWAP_ON_BE(i32); | |
cf447356 GL |
110 | } |
111 | ||
7b8bd818 | 112 | wxUint16 wxDataInputStream::Read16() |
cf447356 | 113 | { |
5a96d2f4 | 114 | wxUint16 i16; |
cf447356 | 115 | |
5a96d2f4 | 116 | m_input->Read(&i16, 2); |
cf447356 | 117 | |
5a96d2f4 GL |
118 | if (m_be_order) |
119 | return wxUINT16_SWAP_ON_LE(i16); | |
120 | else | |
121 | return wxUINT16_SWAP_ON_BE(i16); | |
cf447356 GL |
122 | } |
123 | ||
7b8bd818 | 124 | wxUint8 wxDataInputStream::Read8() |
cf447356 | 125 | { |
7b8bd818 | 126 | wxUint8 buf; |
cf447356 | 127 | |
5a96d2f4 | 128 | m_input->Read(&buf, 1); |
7b8bd818 | 129 | return (wxUint8)buf; |
cf447356 GL |
130 | } |
131 | ||
3d4c6a21 | 132 | double wxDataInputStream::ReadDouble() |
cf447356 | 133 | { |
47d67540 | 134 | #if wxUSE_APPLE_IEEE |
789ab840 VZ |
135 | if ( m_useExtendedPrecision ) |
136 | { | |
137 | char buf[10]; | |
cf447356 | 138 | |
789ab840 VZ |
139 | m_input->Read(buf, 10); |
140 | return wxConvertFromIeeeExtended((const wxInt8 *)buf); | |
141 | } | |
142 | else | |
143 | #endif // wxUSE_APPLE_IEEE | |
144 | { | |
145 | Float64Data floatData; | |
146 | ||
147 | if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) ) | |
148 | { | |
149 | floatData.i[0] = Read32(); | |
150 | floatData.i[1] = Read32(); | |
151 | } | |
152 | else | |
153 | { | |
154 | floatData.i[1] = Read32(); | |
155 | floatData.i[0] = Read32(); | |
156 | } | |
157 | ||
158 | return static_cast<double>(floatData.f); | |
159 | } | |
160 | } | |
161 | ||
162 | float wxDataInputStream::ReadFloat() | |
163 | { | |
164 | #if wxUSE_APPLE_IEEE | |
165 | if ( m_useExtendedPrecision ) | |
166 | { | |
167 | return (float)ReadDouble(); | |
168 | } | |
169 | else | |
170 | #endif // wxUSE_APPLE_IEEE | |
171 | { | |
172 | Float32Data floatData; | |
173 | ||
174 | floatData.i = Read32(); | |
175 | return static_cast<float>(floatData.f); | |
176 | } | |
cf447356 GL |
177 | } |
178 | ||
3d4c6a21 | 179 | wxString wxDataInputStream::ReadString() |
eafc087e | 180 | { |
0094bc4c | 181 | wxString ret; |
eafc087e | 182 | |
0094bc4c VZ |
183 | const size_t len = Read32(); |
184 | if ( len > 0 ) | |
185 | { | |
2ae47e3f | 186 | #if wxUSE_UNICODE |
935693c4 | 187 | wxCharBuffer tmp(len); |
0094bc4c VZ |
188 | if ( tmp ) |
189 | { | |
190 | m_input->Read(tmp.data(), len); | |
0094bc4c VZ |
191 | ret = m_conv->cMB2WX(tmp.data()); |
192 | } | |
2ae47e3f | 193 | #else |
0094bc4c VZ |
194 | wxStringBuffer buf(ret, len); |
195 | if ( buf ) | |
196 | m_input->Read(buf, len); | |
2ae47e3f | 197 | #endif |
0094bc4c VZ |
198 | } |
199 | ||
2b5f62a0 | 200 | return ret; |
eafc087e | 201 | } |
13111b2a | 202 | |
216a72f3 VZ |
203 | #if wxUSE_LONGLONG |
204 | ||
205 | template <class T> | |
206 | static | |
207 | void DoReadLL(T *buffer, size_t size, wxInputStream *input, bool be_order) | |
53663be8 | 208 | { |
216a72f3 VZ |
209 | typedef T DataType; |
210 | unsigned char *pchBuffer = new unsigned char[size * 8]; | |
211 | // TODO: Check for overflow when size is of type uint and is > than 512m | |
212 | input->Read(pchBuffer, size * 8); | |
213 | size_t idx_base = 0; | |
214 | if ( be_order ) | |
215 | { | |
216 | for ( size_t uiIndex = 0; uiIndex != size; ++uiIndex ) | |
217 | { | |
218 | buffer[uiIndex] = 0l; | |
219 | for ( unsigned ui = 0; ui != 8; ++ui ) | |
220 | { | |
221 | buffer[uiIndex] = buffer[uiIndex] * 256l + | |
222 | DataType((unsigned long) pchBuffer[idx_base + ui]); | |
223 | } | |
224 | ||
225 | idx_base += 8; | |
226 | } | |
227 | } | |
228 | else // little endian | |
229 | { | |
230 | for ( size_t uiIndex=0; uiIndex!=size; ++uiIndex ) | |
231 | { | |
232 | buffer[uiIndex] = 0l; | |
233 | for ( unsigned ui=0; ui!=8; ++ui ) | |
234 | buffer[uiIndex] = buffer[uiIndex] * 256l + | |
235 | DataType((unsigned long) pchBuffer[idx_base + 7 - ui]); | |
236 | idx_base += 8; | |
237 | } | |
238 | } | |
239 | delete[] pchBuffer; | |
240 | } | |
53663be8 | 241 | |
216a72f3 VZ |
242 | template <class T> |
243 | static void DoWriteLL(const T *buffer, size_t size, wxOutputStream *output, bool be_order) | |
244 | { | |
245 | typedef T DataType; | |
246 | unsigned char *pchBuffer = new unsigned char[size * 8]; | |
247 | size_t idx_base = 0; | |
248 | if ( be_order ) | |
249 | { | |
250 | for ( size_t uiIndex = 0; uiIndex != size; ++uiIndex ) | |
251 | { | |
252 | DataType i64 = buffer[uiIndex]; | |
253 | for ( unsigned ui = 0; ui != 8; ++ui ) | |
254 | { | |
255 | pchBuffer[idx_base + 7 - ui] = | |
256 | (unsigned char) (i64.GetLo() & 255l); | |
257 | i64 >>= 8l; | |
258 | } | |
259 | ||
260 | idx_base += 8; | |
261 | } | |
262 | } | |
263 | else // little endian | |
264 | { | |
265 | for ( size_t uiIndex=0; uiIndex != size; ++uiIndex ) | |
266 | { | |
267 | DataType i64 = buffer[uiIndex]; | |
268 | for (unsigned ui=0; ui!=8; ++ui) | |
269 | { | |
270 | pchBuffer[idx_base + ui] = | |
271 | (unsigned char) (i64.GetLo() & 255l); | |
272 | i64 >>= 8l; | |
273 | } | |
274 | ||
275 | idx_base += 8; | |
276 | } | |
277 | } | |
278 | ||
279 | // TODO: Check for overflow when size is of type uint and is > than 512m | |
280 | output->Write(pchBuffer, size * 8); | |
281 | delete[] pchBuffer; | |
282 | } | |
283 | ||
284 | #endif // wxUSE_LONGLONG | |
285 | ||
286 | #ifdef wxLongLong_t | |
287 | ||
288 | template <class T> | |
289 | static | |
290 | void DoReadI64(T *buffer, size_t size, wxInputStream *input, bool be_order) | |
291 | { | |
292 | typedef T DataType; | |
293 | unsigned char *pchBuffer = (unsigned char*) buffer; | |
294 | // TODO: Check for overflow when size is of type uint and is > than 512m | |
295 | input->Read(pchBuffer, size * 8); | |
296 | if ( be_order ) | |
297 | { | |
298 | for ( wxUint32 i = 0; i < size; i++ ) | |
299 | { | |
300 | DataType v = wxUINT64_SWAP_ON_LE(*buffer); | |
301 | *(buffer++) = v; | |
302 | } | |
303 | } | |
304 | else // little endian | |
305 | { | |
306 | for ( wxUint32 i=0; i<size; i++ ) | |
307 | { | |
308 | DataType v = wxUINT64_SWAP_ON_BE(*buffer); | |
309 | *(buffer++) = v; | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | template <class T> | |
315 | static | |
316 | void DoWriteI64(const T *buffer, size_t size, wxOutputStream *output, bool be_order) | |
317 | { | |
318 | typedef T DataType; | |
319 | if ( be_order ) | |
53663be8 | 320 | { |
216a72f3 | 321 | for ( size_t i = 0; i < size; i++ ) |
53663be8 | 322 | { |
216a72f3 VZ |
323 | DataType i64 = wxUINT64_SWAP_ON_LE(*buffer); |
324 | buffer++; | |
325 | output->Write(&i64, 8); | |
53663be8 VZ |
326 | } |
327 | } | |
216a72f3 | 328 | else // little endian |
53663be8 | 329 | { |
216a72f3 | 330 | for ( size_t i=0; i < size; i++ ) |
53663be8 | 331 | { |
216a72f3 VZ |
332 | DataType i64 = wxUINT64_SWAP_ON_BE(*buffer); |
333 | buffer++; | |
334 | output->Write(&i64, 8); | |
53663be8 VZ |
335 | } |
336 | } | |
337 | } | |
338 | ||
216a72f3 VZ |
339 | #endif // wxLongLong_t |
340 | ||
341 | ||
342 | #if wxHAS_INT64 | |
343 | void wxDataInputStream::Read64(wxUint64 *buffer, size_t size) | |
344 | { | |
345 | #ifndef wxLongLong_t | |
346 | DoReadLL(buffer, size, m_input, m_be_order); | |
347 | #else | |
348 | DoReadI64(buffer, size, m_input, m_be_order); | |
349 | #endif | |
350 | } | |
351 | ||
352 | void wxDataInputStream::Read64(wxInt64 *buffer, size_t size) | |
353 | { | |
354 | #ifndef wxLongLong_t | |
355 | DoReadLL(buffer, size, m_input, m_be_order); | |
356 | #else | |
357 | DoReadI64(buffer, size, m_input, m_be_order); | |
358 | #endif | |
359 | } | |
360 | #endif // wxHAS_INT64 | |
361 | ||
362 | #if defined(wxLongLong_t) && wxUSE_LONGLONG | |
363 | void wxDataInputStream::Read64(wxULongLong *buffer, size_t size) | |
364 | { | |
365 | DoReadLL(buffer, size, m_input, m_be_order); | |
366 | } | |
367 | ||
368 | void wxDataInputStream::Read64(wxLongLong *buffer, size_t size) | |
369 | { | |
370 | DoReadLL(buffer, size, m_input, m_be_order); | |
371 | } | |
372 | #endif // wxLongLong_t | |
373 | ||
374 | #if wxUSE_LONGLONG | |
375 | void wxDataInputStream::ReadLL(wxULongLong *buffer, size_t size) | |
376 | { | |
377 | DoReadLL(buffer, size, m_input, m_be_order); | |
378 | } | |
379 | ||
380 | void wxDataInputStream::ReadLL(wxLongLong *buffer, size_t size) | |
381 | { | |
382 | DoReadLL(buffer, size, m_input, m_be_order); | |
383 | } | |
384 | ||
385 | wxLongLong wxDataInputStream::ReadLL(void) | |
386 | { | |
387 | wxLongLong ll; | |
40319aa0 | 388 | DoReadLL(&ll, (size_t)1, m_input, m_be_order); |
216a72f3 VZ |
389 | return ll; |
390 | } | |
391 | #endif // wxUSE_LONGLONG | |
392 | ||
53663be8 VZ |
393 | void wxDataInputStream::Read32(wxUint32 *buffer, size_t size) |
394 | { | |
7448de8d | 395 | m_input->Read(buffer, size * 4); |
53663be8 | 396 | |
7448de8d | 397 | if (m_be_order) |
53663be8 | 398 | { |
7448de8d WS |
399 | for (wxUint32 i=0; i<size; i++) |
400 | { | |
401 | wxUint32 v = wxUINT32_SWAP_ON_LE(*buffer); | |
402 | *(buffer++) = v; | |
403 | } | |
53663be8 | 404 | } |
7448de8d | 405 | else |
53663be8 | 406 | { |
7448de8d WS |
407 | for (wxUint32 i=0; i<size; i++) |
408 | { | |
409 | wxUint32 v = wxUINT32_SWAP_ON_BE(*buffer); | |
410 | *(buffer++) = v; | |
411 | } | |
53663be8 | 412 | } |
53663be8 VZ |
413 | } |
414 | ||
415 | void wxDataInputStream::Read16(wxUint16 *buffer, size_t size) | |
416 | { | |
417 | m_input->Read(buffer, size * 2); | |
418 | ||
419 | if (m_be_order) | |
420 | { | |
421 | for (wxUint32 i=0; i<size; i++) | |
422 | { | |
423 | wxUint16 v = wxUINT16_SWAP_ON_LE(*buffer); | |
424 | *(buffer++) = v; | |
425 | } | |
426 | } | |
427 | else | |
428 | { | |
429 | for (wxUint32 i=0; i<size; i++) | |
430 | { | |
431 | wxUint16 v = wxUINT16_SWAP_ON_BE(*buffer); | |
432 | *(buffer++) = v; | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
437 | void wxDataInputStream::Read8(wxUint8 *buffer, size_t size) | |
438 | { | |
439 | m_input->Read(buffer, size); | |
440 | } | |
441 | ||
442 | void wxDataInputStream::ReadDouble(double *buffer, size_t size) | |
443 | { | |
444 | for (wxUint32 i=0; i<size; i++) | |
445 | { | |
446 | *(buffer++) = ReadDouble(); | |
447 | } | |
448 | } | |
449 | ||
789ab840 VZ |
450 | void wxDataInputStream::ReadFloat(float *buffer, size_t size) |
451 | { | |
452 | for (wxUint32 i=0; i<size; i++) | |
453 | { | |
454 | *(buffer++) = ReadFloat(); | |
455 | } | |
456 | } | |
457 | ||
fae05df5 GL |
458 | wxDataInputStream& wxDataInputStream::operator>>(wxString& s) |
459 | { | |
460 | s = ReadString(); | |
461 | return *this; | |
462 | } | |
463 | ||
464 | wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c) | |
465 | { | |
466 | c = (wxInt8)Read8(); | |
467 | return *this; | |
468 | } | |
469 | ||
470 | wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i) | |
471 | { | |
472 | i = (wxInt16)Read16(); | |
473 | return *this; | |
474 | } | |
475 | ||
476 | wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i) | |
477 | { | |
478 | i = (wxInt32)Read32(); | |
479 | return *this; | |
480 | } | |
481 | ||
482 | wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c) | |
483 | { | |
484 | c = Read8(); | |
485 | return *this; | |
486 | } | |
487 | ||
488 | wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i) | |
489 | { | |
490 | i = Read16(); | |
491 | return *this; | |
492 | } | |
493 | ||
494 | wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i) | |
495 | { | |
496 | i = Read32(); | |
497 | return *this; | |
498 | } | |
499 | ||
216a72f3 | 500 | #if wxHAS_INT64 |
41b0a113 RL |
501 | wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i) |
502 | { | |
503 | i = Read64(); | |
504 | return *this; | |
505 | } | |
506 | ||
216a72f3 VZ |
507 | wxDataInputStream& wxDataInputStream::operator>>(wxInt64& i) |
508 | { | |
509 | i = Read64(); | |
510 | return *this; | |
511 | } | |
512 | #endif // wxHAS_INT64 | |
513 | ||
514 | #if defined(wxLongLong_t) && wxUSE_LONGLONG | |
515 | wxDataInputStream& wxDataInputStream::operator>>(wxULongLong& i) | |
516 | { | |
517 | i = ReadLL(); | |
518 | return *this; | |
519 | } | |
520 | ||
521 | wxDataInputStream& wxDataInputStream::operator>>(wxLongLong& i) | |
522 | { | |
523 | i = ReadLL(); | |
524 | return *this; | |
525 | } | |
526 | #endif // wxLongLong_t | |
527 | ||
337bbb7a | 528 | wxDataInputStream& wxDataInputStream::operator>>(double& d) |
fae05df5 | 529 | { |
337bbb7a | 530 | d = ReadDouble(); |
fae05df5 GL |
531 | return *this; |
532 | } | |
533 | ||
534 | wxDataInputStream& wxDataInputStream::operator>>(float& f) | |
535 | { | |
789ab840 | 536 | f = ReadFloat(); |
fae05df5 GL |
537 | return *this; |
538 | } | |
eafc087e | 539 | |
f4ada568 GL |
540 | // --------------------------------------------------------------------------- |
541 | // wxDataOutputStream | |
542 | // --------------------------------------------------------------------------- | |
543 | ||
830f8f11 | 544 | wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, const wxMBConv& conv) |
61eb6bb6 VZ |
545 | : wxDataStreamBase(conv), |
546 | m_output(&s) | |
d36c9347 | 547 | { |
10c2f98a | 548 | } |
10c2f98a | 549 | |
216a72f3 | 550 | #if wxHAS_INT64 |
41b0a113 RL |
551 | void wxDataOutputStream::Write64(wxUint64 i) |
552 | { | |
216a72f3 VZ |
553 | Write64(&i, 1); |
554 | } | |
41b0a113 | 555 | |
216a72f3 VZ |
556 | void wxDataOutputStream::Write64(wxInt64 i) |
557 | { | |
558 | Write64(&i, 1); | |
41b0a113 | 559 | } |
216a72f3 | 560 | #endif // wxHAS_INT64 |
41b0a113 | 561 | |
7b8bd818 | 562 | void wxDataOutputStream::Write32(wxUint32 i) |
cf447356 | 563 | { |
5a96d2f4 | 564 | wxUint32 i32; |
cf447356 | 565 | |
5a96d2f4 GL |
566 | if (m_be_order) |
567 | i32 = wxUINT32_SWAP_ON_LE(i); | |
568 | else | |
569 | i32 = wxUINT32_SWAP_ON_BE(i); | |
570 | m_output->Write(&i32, 4); | |
cf447356 GL |
571 | } |
572 | ||
7b8bd818 | 573 | void wxDataOutputStream::Write16(wxUint16 i) |
cf447356 | 574 | { |
5a96d2f4 | 575 | wxUint16 i16; |
cf447356 | 576 | |
5a96d2f4 GL |
577 | if (m_be_order) |
578 | i16 = wxUINT16_SWAP_ON_LE(i); | |
579 | else | |
580 | i16 = wxUINT16_SWAP_ON_BE(i); | |
581 | ||
582 | m_output->Write(&i16, 2); | |
cf447356 GL |
583 | } |
584 | ||
7b8bd818 | 585 | void wxDataOutputStream::Write8(wxUint8 i) |
cf447356 | 586 | { |
fae05df5 | 587 | m_output->Write(&i, 1); |
cf447356 GL |
588 | } |
589 | ||
3d4c6a21 | 590 | void wxDataOutputStream::WriteString(const wxString& string) |
eafc087e | 591 | { |
a99acbb0 | 592 | #if wxUSE_UNICODE |
d36c9347 | 593 | const wxWX2MBbuf buf = string.mb_str(*m_conv); |
a99acbb0 | 594 | #else |
c980c992 | 595 | const wxWX2MBbuf buf = string.mb_str(); |
a99acbb0 VS |
596 | #endif |
597 | size_t len = strlen(buf); | |
598 | Write32(len); | |
599 | if (len > 0) | |
600 | m_output->Write(buf, len); | |
cf447356 GL |
601 | } |
602 | ||
3d4c6a21 | 603 | void wxDataOutputStream::WriteDouble(double d) |
cf447356 | 604 | { |
789ab840 VZ |
605 | #if wxUSE_APPLE_IEEE |
606 | if ( m_useExtendedPrecision ) | |
607 | { | |
608 | char buf[10]; | |
609 | ||
610 | wxConvertToIeeeExtended(d, (wxInt8 *)buf); | |
611 | m_output->Write(buf, 10); | |
612 | } | |
613 | else | |
614 | #endif // wxUSE_APPLE_IEEE | |
615 | { | |
616 | Float64Data floatData; | |
cf447356 | 617 | |
789ab840 VZ |
618 | floatData.f = (wxFloat64)d; |
619 | ||
620 | if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) ) | |
621 | { | |
622 | Write32(floatData.i[0]); | |
623 | Write32(floatData.i[1]); | |
624 | } | |
625 | else | |
626 | { | |
627 | Write32(floatData.i[1]); | |
628 | Write32(floatData.i[0]); | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | void wxDataOutputStream::WriteFloat(float f) | |
634 | { | |
47d67540 | 635 | #if wxUSE_APPLE_IEEE |
789ab840 VZ |
636 | if ( m_useExtendedPrecision ) |
637 | { | |
638 | WriteDouble((double)f); | |
639 | } | |
640 | else | |
641 | #endif // wxUSE_APPLE_IEEE | |
642 | { | |
643 | Float32Data floatData; | |
644 | ||
645 | floatData.f = (wxFloat32)f; | |
646 | Write32(floatData.i); | |
647 | } | |
fae05df5 GL |
648 | } |
649 | ||
216a72f3 | 650 | #if wxHAS_INT64 |
53663be8 VZ |
651 | void wxDataOutputStream::Write64(const wxUint64 *buffer, size_t size) |
652 | { | |
216a72f3 VZ |
653 | #ifndef wxLongLong_t |
654 | DoWriteLL(buffer, size, m_output, m_be_order); | |
655 | #else | |
656 | DoWriteI64(buffer, size, m_output, m_be_order); | |
657 | #endif | |
658 | } | |
659 | ||
660 | void wxDataOutputStream::Write64(const wxInt64 *buffer, size_t size) | |
661 | { | |
662 | #ifndef wxLongLong_t | |
663 | DoWriteLL(buffer, size, m_output, m_be_order); | |
664 | #else | |
665 | DoWriteI64(buffer, size, m_output, m_be_order); | |
666 | #endif | |
53663be8 | 667 | } |
216a72f3 VZ |
668 | #endif // wxHAS_INT64 |
669 | ||
670 | #if defined(wxLongLong_t) && wxUSE_LONGLONG | |
671 | void wxDataOutputStream::Write64(const wxULongLong *buffer, size_t size) | |
672 | { | |
673 | DoWriteLL(buffer, size, m_output, m_be_order); | |
674 | } | |
675 | ||
676 | void wxDataOutputStream::Write64(const wxLongLong *buffer, size_t size) | |
677 | { | |
678 | DoWriteLL(buffer, size, m_output, m_be_order); | |
679 | } | |
680 | #endif // wxLongLong_t | |
681 | ||
682 | #if wxUSE_LONGLONG | |
683 | void wxDataOutputStream::WriteLL(const wxULongLong *buffer, size_t size) | |
684 | { | |
685 | DoWriteLL(buffer, size, m_output, m_be_order); | |
686 | } | |
687 | ||
688 | void wxDataOutputStream::WriteLL(const wxLongLong *buffer, size_t size) | |
689 | { | |
690 | DoWriteLL(buffer, size, m_output, m_be_order); | |
691 | } | |
692 | ||
693 | void wxDataOutputStream::WriteLL(const wxLongLong &ll) | |
694 | { | |
695 | WriteLL(&ll, 1); | |
696 | } | |
697 | ||
698 | void wxDataOutputStream::WriteLL(const wxULongLong &ll) | |
699 | { | |
700 | WriteLL(&ll, 1); | |
701 | } | |
702 | #endif // wxUSE_LONGLONG | |
53663be8 VZ |
703 | |
704 | void wxDataOutputStream::Write32(const wxUint32 *buffer, size_t size) | |
705 | { | |
706 | if (m_be_order) | |
707 | { | |
708 | for (wxUint32 i=0; i<size ;i++) | |
709 | { | |
710 | wxUint32 i32 = wxUINT32_SWAP_ON_LE(*buffer); | |
711 | buffer++; | |
712 | m_output->Write(&i32, 4); | |
713 | } | |
714 | } | |
715 | else | |
716 | { | |
717 | for (wxUint32 i=0; i<size ;i++) | |
718 | { | |
719 | wxUint32 i32 = wxUINT32_SWAP_ON_BE(*buffer); | |
720 | buffer++; | |
721 | m_output->Write(&i32, 4); | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | void wxDataOutputStream::Write16(const wxUint16 *buffer, size_t size) | |
727 | { | |
728 | if (m_be_order) | |
729 | { | |
730 | for (wxUint32 i=0; i<size ;i++) | |
731 | { | |
732 | wxUint16 i16 = wxUINT16_SWAP_ON_LE(*buffer); | |
733 | buffer++; | |
734 | m_output->Write(&i16, 2); | |
735 | } | |
736 | } | |
737 | else | |
738 | { | |
739 | for (wxUint32 i=0; i<size ;i++) | |
740 | { | |
741 | wxUint16 i16 = wxUINT16_SWAP_ON_BE(*buffer); | |
742 | buffer++; | |
743 | m_output->Write(&i16, 2); | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | void wxDataOutputStream::Write8(const wxUint8 *buffer, size_t size) | |
749 | { | |
750 | m_output->Write(buffer, size); | |
751 | } | |
752 | ||
753 | void wxDataOutputStream::WriteDouble(const double *buffer, size_t size) | |
754 | { | |
755 | for (wxUint32 i=0; i<size; i++) | |
756 | { | |
757 | WriteDouble(*(buffer++)); | |
758 | } | |
759 | } | |
760 | ||
789ab840 VZ |
761 | void wxDataOutputStream::WriteFloat(const float *buffer, size_t size) |
762 | { | |
763 | for (wxUint32 i=0; i<size; i++) | |
764 | { | |
765 | WriteFloat(*(buffer++)); | |
766 | } | |
767 | } | |
768 | ||
38caaa61 | 769 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string) |
fae05df5 GL |
770 | { |
771 | WriteString(string); | |
772 | return *this; | |
773 | } | |
774 | ||
775 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c) | |
776 | { | |
777 | Write8((wxUint8)c); | |
778 | return *this; | |
779 | } | |
780 | ||
781 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i) | |
782 | { | |
783 | Write16((wxUint16)i); | |
784 | return *this; | |
785 | } | |
786 | ||
787 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i) | |
788 | { | |
789 | Write32((wxUint32)i); | |
790 | return *this; | |
791 | } | |
792 | ||
793 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c) | |
794 | { | |
795 | Write8(c); | |
796 | return *this; | |
797 | } | |
798 | ||
799 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i) | |
800 | { | |
801 | Write16(i); | |
802 | return *this; | |
803 | } | |
804 | ||
805 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i) | |
806 | { | |
807 | Write32(i); | |
808 | return *this; | |
809 | } | |
810 | ||
216a72f3 | 811 | #if wxHAS_INT64 |
41b0a113 RL |
812 | wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i) |
813 | { | |
814 | Write64(i); | |
815 | return *this; | |
816 | } | |
817 | ||
216a72f3 VZ |
818 | wxDataOutputStream& wxDataOutputStream::operator<<(wxInt64 i) |
819 | { | |
820 | Write64(i); | |
821 | return *this; | |
822 | } | |
823 | #endif // wxHAS_INT64 | |
824 | ||
825 | #if defined(wxLongLong_t) && wxUSE_LONGLONG | |
826 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxULongLong &i) | |
827 | { | |
828 | WriteLL(i); | |
829 | return *this; | |
830 | } | |
831 | ||
832 | wxDataOutputStream& wxDataOutputStream::operator<<(const wxLongLong &i) | |
833 | { | |
834 | WriteLL(i); | |
835 | return *this; | |
836 | } | |
837 | #endif // wxLongLong_t | |
838 | ||
337bbb7a | 839 | wxDataOutputStream& wxDataOutputStream::operator<<(double d) |
fae05df5 | 840 | { |
337bbb7a | 841 | WriteDouble(d); |
fae05df5 GL |
842 | return *this; |
843 | } | |
844 | ||
845 | wxDataOutputStream& wxDataOutputStream::operator<<(float f) | |
846 | { | |
789ab840 | 847 | WriteFloat(f); |
fae05df5 | 848 | return *this; |
cf447356 | 849 | } |
ce4169a4 RR |
850 | |
851 | #endif | |
852 | // wxUSE_STREAMS |