Extract common parts of wxData{In,Out}putStream in a common base class.
[wxWidgets.git] / src / common / datstrm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/datstrm.cpp
3 // Purpose: Data stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mickael Gilabert
6 // Created: 28/06/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_STREAMS
20
21 #include "wx/datstrm.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/math.h"
25 #endif //WX_PRECOMP
26
27 // ----------------------------------------------------------------------------
28 // wxDataStreamBase
29 // ----------------------------------------------------------------------------
30
31 wxDataStreamBase::wxDataStreamBase(const wxMBConv& conv)
32 #if wxUSE_UNICODE
33 : m_conv(conv.Clone())
34 #endif // wxUSE_UNICODE
35 {
36 // It is unused in non-Unicode build, so suppress a warning there.
37 wxUnusedVar(conv);
38
39 m_be_order = false;
40 }
41
42 #if wxUSE_UNICODE
43 void wxDataStreamBase::SetConv( const wxMBConv &conv )
44 {
45 delete m_conv;
46 m_conv = conv.Clone();
47 }
48 #endif
49
50 wxDataStreamBase::~wxDataStreamBase()
51 {
52 #if wxUSE_UNICODE
53 delete m_conv;
54 #endif // wxUSE_UNICODE
55 }
56
57 // ---------------------------------------------------------------------------
58 // wxDataInputStream
59 // ---------------------------------------------------------------------------
60
61 wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv)
62 : wxDataStreamBase(conv),
63 m_input(&s)
64 {
65 }
66
67 #if wxHAS_INT64
68 wxUint64 wxDataInputStream::Read64()
69 {
70 wxUint64 tmp;
71 Read64(&tmp, 1);
72 return tmp;
73 }
74 #endif // wxHAS_INT64
75
76 wxUint32 wxDataInputStream::Read32()
77 {
78 wxUint32 i32;
79
80 m_input->Read(&i32, 4);
81
82 if (m_be_order)
83 return wxUINT32_SWAP_ON_LE(i32);
84 else
85 return wxUINT32_SWAP_ON_BE(i32);
86 }
87
88 wxUint16 wxDataInputStream::Read16()
89 {
90 wxUint16 i16;
91
92 m_input->Read(&i16, 2);
93
94 if (m_be_order)
95 return wxUINT16_SWAP_ON_LE(i16);
96 else
97 return wxUINT16_SWAP_ON_BE(i16);
98 }
99
100 wxUint8 wxDataInputStream::Read8()
101 {
102 wxUint8 buf;
103
104 m_input->Read(&buf, 1);
105 return (wxUint8)buf;
106 }
107
108 double wxDataInputStream::ReadDouble()
109 {
110 #if wxUSE_APPLE_IEEE
111 char buf[10];
112
113 m_input->Read(buf, 10);
114 return wxConvertFromIeeeExtended((const wxInt8 *)buf);
115 #else
116 return 0.0;
117 #endif
118 }
119
120 wxString wxDataInputStream::ReadString()
121 {
122 wxString ret;
123
124 const size_t len = Read32();
125 if ( len > 0 )
126 {
127 #if wxUSE_UNICODE
128 wxCharBuffer tmp(len);
129 if ( tmp )
130 {
131 m_input->Read(tmp.data(), len);
132 ret = m_conv->cMB2WX(tmp.data());
133 }
134 #else
135 wxStringBuffer buf(ret, len);
136 if ( buf )
137 m_input->Read(buf, len);
138 #endif
139 }
140
141 return ret;
142 }
143
144 #if wxUSE_LONGLONG
145
146 template <class T>
147 static
148 void DoReadLL(T *buffer, size_t size, wxInputStream *input, bool be_order)
149 {
150 typedef T DataType;
151 unsigned char *pchBuffer = new unsigned char[size * 8];
152 // TODO: Check for overflow when size is of type uint and is > than 512m
153 input->Read(pchBuffer, size * 8);
154 size_t idx_base = 0;
155 if ( be_order )
156 {
157 for ( size_t uiIndex = 0; uiIndex != size; ++uiIndex )
158 {
159 buffer[uiIndex] = 0l;
160 for ( unsigned ui = 0; ui != 8; ++ui )
161 {
162 buffer[uiIndex] = buffer[uiIndex] * 256l +
163 DataType((unsigned long) pchBuffer[idx_base + ui]);
164 }
165
166 idx_base += 8;
167 }
168 }
169 else // little endian
170 {
171 for ( size_t uiIndex=0; uiIndex!=size; ++uiIndex )
172 {
173 buffer[uiIndex] = 0l;
174 for ( unsigned ui=0; ui!=8; ++ui )
175 buffer[uiIndex] = buffer[uiIndex] * 256l +
176 DataType((unsigned long) pchBuffer[idx_base + 7 - ui]);
177 idx_base += 8;
178 }
179 }
180 delete[] pchBuffer;
181 }
182
183 template <class T>
184 static void DoWriteLL(const T *buffer, size_t size, wxOutputStream *output, bool be_order)
185 {
186 typedef T DataType;
187 unsigned char *pchBuffer = new unsigned char[size * 8];
188 size_t idx_base = 0;
189 if ( be_order )
190 {
191 for ( size_t uiIndex = 0; uiIndex != size; ++uiIndex )
192 {
193 DataType i64 = buffer[uiIndex];
194 for ( unsigned ui = 0; ui != 8; ++ui )
195 {
196 pchBuffer[idx_base + 7 - ui] =
197 (unsigned char) (i64.GetLo() & 255l);
198 i64 >>= 8l;
199 }
200
201 idx_base += 8;
202 }
203 }
204 else // little endian
205 {
206 for ( size_t uiIndex=0; uiIndex != size; ++uiIndex )
207 {
208 DataType i64 = buffer[uiIndex];
209 for (unsigned ui=0; ui!=8; ++ui)
210 {
211 pchBuffer[idx_base + ui] =
212 (unsigned char) (i64.GetLo() & 255l);
213 i64 >>= 8l;
214 }
215
216 idx_base += 8;
217 }
218 }
219
220 // TODO: Check for overflow when size is of type uint and is > than 512m
221 output->Write(pchBuffer, size * 8);
222 delete[] pchBuffer;
223 }
224
225 #endif // wxUSE_LONGLONG
226
227 #ifdef wxLongLong_t
228
229 template <class T>
230 static
231 void DoReadI64(T *buffer, size_t size, wxInputStream *input, bool be_order)
232 {
233 typedef T DataType;
234 unsigned char *pchBuffer = (unsigned char*) buffer;
235 // TODO: Check for overflow when size is of type uint and is > than 512m
236 input->Read(pchBuffer, size * 8);
237 if ( be_order )
238 {
239 for ( wxUint32 i = 0; i < size; i++ )
240 {
241 DataType v = wxUINT64_SWAP_ON_LE(*buffer);
242 *(buffer++) = v;
243 }
244 }
245 else // little endian
246 {
247 for ( wxUint32 i=0; i<size; i++ )
248 {
249 DataType v = wxUINT64_SWAP_ON_BE(*buffer);
250 *(buffer++) = v;
251 }
252 }
253 }
254
255 template <class T>
256 static
257 void DoWriteI64(const T *buffer, size_t size, wxOutputStream *output, bool be_order)
258 {
259 typedef T DataType;
260 if ( be_order )
261 {
262 for ( size_t i = 0; i < size; i++ )
263 {
264 DataType i64 = wxUINT64_SWAP_ON_LE(*buffer);
265 buffer++;
266 output->Write(&i64, 8);
267 }
268 }
269 else // little endian
270 {
271 for ( size_t i=0; i < size; i++ )
272 {
273 DataType i64 = wxUINT64_SWAP_ON_BE(*buffer);
274 buffer++;
275 output->Write(&i64, 8);
276 }
277 }
278 }
279
280 #endif // wxLongLong_t
281
282
283 #if wxHAS_INT64
284 void wxDataInputStream::Read64(wxUint64 *buffer, size_t size)
285 {
286 #ifndef wxLongLong_t
287 DoReadLL(buffer, size, m_input, m_be_order);
288 #else
289 DoReadI64(buffer, size, m_input, m_be_order);
290 #endif
291 }
292
293 void wxDataInputStream::Read64(wxInt64 *buffer, size_t size)
294 {
295 #ifndef wxLongLong_t
296 DoReadLL(buffer, size, m_input, m_be_order);
297 #else
298 DoReadI64(buffer, size, m_input, m_be_order);
299 #endif
300 }
301 #endif // wxHAS_INT64
302
303 #if defined(wxLongLong_t) && wxUSE_LONGLONG
304 void wxDataInputStream::Read64(wxULongLong *buffer, size_t size)
305 {
306 DoReadLL(buffer, size, m_input, m_be_order);
307 }
308
309 void wxDataInputStream::Read64(wxLongLong *buffer, size_t size)
310 {
311 DoReadLL(buffer, size, m_input, m_be_order);
312 }
313 #endif // wxLongLong_t
314
315 #if wxUSE_LONGLONG
316 void wxDataInputStream::ReadLL(wxULongLong *buffer, size_t size)
317 {
318 DoReadLL(buffer, size, m_input, m_be_order);
319 }
320
321 void wxDataInputStream::ReadLL(wxLongLong *buffer, size_t size)
322 {
323 DoReadLL(buffer, size, m_input, m_be_order);
324 }
325
326 wxLongLong wxDataInputStream::ReadLL(void)
327 {
328 wxLongLong ll;
329 DoReadLL(&ll, (size_t)1, m_input, m_be_order);
330 return ll;
331 }
332 #endif // wxUSE_LONGLONG
333
334 void wxDataInputStream::Read32(wxUint32 *buffer, size_t size)
335 {
336 m_input->Read(buffer, size * 4);
337
338 if (m_be_order)
339 {
340 for (wxUint32 i=0; i<size; i++)
341 {
342 wxUint32 v = wxUINT32_SWAP_ON_LE(*buffer);
343 *(buffer++) = v;
344 }
345 }
346 else
347 {
348 for (wxUint32 i=0; i<size; i++)
349 {
350 wxUint32 v = wxUINT32_SWAP_ON_BE(*buffer);
351 *(buffer++) = v;
352 }
353 }
354 }
355
356 void wxDataInputStream::Read16(wxUint16 *buffer, size_t size)
357 {
358 m_input->Read(buffer, size * 2);
359
360 if (m_be_order)
361 {
362 for (wxUint32 i=0; i<size; i++)
363 {
364 wxUint16 v = wxUINT16_SWAP_ON_LE(*buffer);
365 *(buffer++) = v;
366 }
367 }
368 else
369 {
370 for (wxUint32 i=0; i<size; i++)
371 {
372 wxUint16 v = wxUINT16_SWAP_ON_BE(*buffer);
373 *(buffer++) = v;
374 }
375 }
376 }
377
378 void wxDataInputStream::Read8(wxUint8 *buffer, size_t size)
379 {
380 m_input->Read(buffer, size);
381 }
382
383 void wxDataInputStream::ReadDouble(double *buffer, size_t size)
384 {
385 for (wxUint32 i=0; i<size; i++)
386 {
387 *(buffer++) = ReadDouble();
388 }
389 }
390
391 wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
392 {
393 s = ReadString();
394 return *this;
395 }
396
397 wxDataInputStream& wxDataInputStream::operator>>(wxInt8& c)
398 {
399 c = (wxInt8)Read8();
400 return *this;
401 }
402
403 wxDataInputStream& wxDataInputStream::operator>>(wxInt16& i)
404 {
405 i = (wxInt16)Read16();
406 return *this;
407 }
408
409 wxDataInputStream& wxDataInputStream::operator>>(wxInt32& i)
410 {
411 i = (wxInt32)Read32();
412 return *this;
413 }
414
415 wxDataInputStream& wxDataInputStream::operator>>(wxUint8& c)
416 {
417 c = Read8();
418 return *this;
419 }
420
421 wxDataInputStream& wxDataInputStream::operator>>(wxUint16& i)
422 {
423 i = Read16();
424 return *this;
425 }
426
427 wxDataInputStream& wxDataInputStream::operator>>(wxUint32& i)
428 {
429 i = Read32();
430 return *this;
431 }
432
433 #if wxHAS_INT64
434 wxDataInputStream& wxDataInputStream::operator>>(wxUint64& i)
435 {
436 i = Read64();
437 return *this;
438 }
439
440 wxDataInputStream& wxDataInputStream::operator>>(wxInt64& i)
441 {
442 i = Read64();
443 return *this;
444 }
445 #endif // wxHAS_INT64
446
447 #if defined(wxLongLong_t) && wxUSE_LONGLONG
448 wxDataInputStream& wxDataInputStream::operator>>(wxULongLong& i)
449 {
450 i = ReadLL();
451 return *this;
452 }
453
454 wxDataInputStream& wxDataInputStream::operator>>(wxLongLong& i)
455 {
456 i = ReadLL();
457 return *this;
458 }
459 #endif // wxLongLong_t
460
461 wxDataInputStream& wxDataInputStream::operator>>(double& i)
462 {
463 i = ReadDouble();
464 return *this;
465 }
466
467 wxDataInputStream& wxDataInputStream::operator>>(float& f)
468 {
469 f = (float)ReadDouble();
470 return *this;
471 }
472
473 // ---------------------------------------------------------------------------
474 // wxDataOutputStream
475 // ---------------------------------------------------------------------------
476
477 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, const wxMBConv& conv)
478 : wxDataStreamBase(conv),
479 m_output(&s)
480 {
481 }
482
483 #if wxHAS_INT64
484 void wxDataOutputStream::Write64(wxUint64 i)
485 {
486 Write64(&i, 1);
487 }
488
489 void wxDataOutputStream::Write64(wxInt64 i)
490 {
491 Write64(&i, 1);
492 }
493 #endif // wxHAS_INT64
494
495 void wxDataOutputStream::Write32(wxUint32 i)
496 {
497 wxUint32 i32;
498
499 if (m_be_order)
500 i32 = wxUINT32_SWAP_ON_LE(i);
501 else
502 i32 = wxUINT32_SWAP_ON_BE(i);
503 m_output->Write(&i32, 4);
504 }
505
506 void wxDataOutputStream::Write16(wxUint16 i)
507 {
508 wxUint16 i16;
509
510 if (m_be_order)
511 i16 = wxUINT16_SWAP_ON_LE(i);
512 else
513 i16 = wxUINT16_SWAP_ON_BE(i);
514
515 m_output->Write(&i16, 2);
516 }
517
518 void wxDataOutputStream::Write8(wxUint8 i)
519 {
520 m_output->Write(&i, 1);
521 }
522
523 void wxDataOutputStream::WriteString(const wxString& string)
524 {
525 #if wxUSE_UNICODE
526 const wxWX2MBbuf buf = string.mb_str(*m_conv);
527 #else
528 const wxWX2MBbuf buf = string.mb_str();
529 #endif
530 size_t len = strlen(buf);
531 Write32(len);
532 if (len > 0)
533 m_output->Write(buf, len);
534 }
535
536 void wxDataOutputStream::WriteDouble(double d)
537 {
538 char buf[10];
539
540 #if wxUSE_APPLE_IEEE
541 wxConvertToIeeeExtended(d, (wxInt8 *)buf);
542 #else
543 wxUnusedVar(d);
544 #if !defined(__VMS__) && !defined(__GNUG__)
545 #ifdef _MSC_VER
546 # pragma message("wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!")
547 #else
548 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
549 #endif
550 #endif
551 buf[0] = '\0';
552 #endif
553 m_output->Write(buf, 10);
554 }
555
556 #if wxHAS_INT64
557 void wxDataOutputStream::Write64(const wxUint64 *buffer, size_t size)
558 {
559 #ifndef wxLongLong_t
560 DoWriteLL(buffer, size, m_output, m_be_order);
561 #else
562 DoWriteI64(buffer, size, m_output, m_be_order);
563 #endif
564 }
565
566 void wxDataOutputStream::Write64(const wxInt64 *buffer, size_t size)
567 {
568 #ifndef wxLongLong_t
569 DoWriteLL(buffer, size, m_output, m_be_order);
570 #else
571 DoWriteI64(buffer, size, m_output, m_be_order);
572 #endif
573 }
574 #endif // wxHAS_INT64
575
576 #if defined(wxLongLong_t) && wxUSE_LONGLONG
577 void wxDataOutputStream::Write64(const wxULongLong *buffer, size_t size)
578 {
579 DoWriteLL(buffer, size, m_output, m_be_order);
580 }
581
582 void wxDataOutputStream::Write64(const wxLongLong *buffer, size_t size)
583 {
584 DoWriteLL(buffer, size, m_output, m_be_order);
585 }
586 #endif // wxLongLong_t
587
588 #if wxUSE_LONGLONG
589 void wxDataOutputStream::WriteLL(const wxULongLong *buffer, size_t size)
590 {
591 DoWriteLL(buffer, size, m_output, m_be_order);
592 }
593
594 void wxDataOutputStream::WriteLL(const wxLongLong *buffer, size_t size)
595 {
596 DoWriteLL(buffer, size, m_output, m_be_order);
597 }
598
599 void wxDataOutputStream::WriteLL(const wxLongLong &ll)
600 {
601 WriteLL(&ll, 1);
602 }
603
604 void wxDataOutputStream::WriteLL(const wxULongLong &ll)
605 {
606 WriteLL(&ll, 1);
607 }
608 #endif // wxUSE_LONGLONG
609
610 void wxDataOutputStream::Write32(const wxUint32 *buffer, size_t size)
611 {
612 if (m_be_order)
613 {
614 for (wxUint32 i=0; i<size ;i++)
615 {
616 wxUint32 i32 = wxUINT32_SWAP_ON_LE(*buffer);
617 buffer++;
618 m_output->Write(&i32, 4);
619 }
620 }
621 else
622 {
623 for (wxUint32 i=0; i<size ;i++)
624 {
625 wxUint32 i32 = wxUINT32_SWAP_ON_BE(*buffer);
626 buffer++;
627 m_output->Write(&i32, 4);
628 }
629 }
630 }
631
632 void wxDataOutputStream::Write16(const wxUint16 *buffer, size_t size)
633 {
634 if (m_be_order)
635 {
636 for (wxUint32 i=0; i<size ;i++)
637 {
638 wxUint16 i16 = wxUINT16_SWAP_ON_LE(*buffer);
639 buffer++;
640 m_output->Write(&i16, 2);
641 }
642 }
643 else
644 {
645 for (wxUint32 i=0; i<size ;i++)
646 {
647 wxUint16 i16 = wxUINT16_SWAP_ON_BE(*buffer);
648 buffer++;
649 m_output->Write(&i16, 2);
650 }
651 }
652 }
653
654 void wxDataOutputStream::Write8(const wxUint8 *buffer, size_t size)
655 {
656 m_output->Write(buffer, size);
657 }
658
659 void wxDataOutputStream::WriteDouble(const double *buffer, size_t size)
660 {
661 for (wxUint32 i=0; i<size; i++)
662 {
663 WriteDouble(*(buffer++));
664 }
665 }
666
667 wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string)
668 {
669 WriteString(string);
670 return *this;
671 }
672
673 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt8 c)
674 {
675 Write8((wxUint8)c);
676 return *this;
677 }
678
679 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt16 i)
680 {
681 Write16((wxUint16)i);
682 return *this;
683 }
684
685 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt32 i)
686 {
687 Write32((wxUint32)i);
688 return *this;
689 }
690
691 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint8 c)
692 {
693 Write8(c);
694 return *this;
695 }
696
697 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint16 i)
698 {
699 Write16(i);
700 return *this;
701 }
702
703 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint32 i)
704 {
705 Write32(i);
706 return *this;
707 }
708
709 #if wxHAS_INT64
710 wxDataOutputStream& wxDataOutputStream::operator<<(wxUint64 i)
711 {
712 Write64(i);
713 return *this;
714 }
715
716 wxDataOutputStream& wxDataOutputStream::operator<<(wxInt64 i)
717 {
718 Write64(i);
719 return *this;
720 }
721 #endif // wxHAS_INT64
722
723 #if defined(wxLongLong_t) && wxUSE_LONGLONG
724 wxDataOutputStream& wxDataOutputStream::operator<<(const wxULongLong &i)
725 {
726 WriteLL(i);
727 return *this;
728 }
729
730 wxDataOutputStream& wxDataOutputStream::operator<<(const wxLongLong &i)
731 {
732 WriteLL(i);
733 return *this;
734 }
735 #endif // wxLongLong_t
736
737 wxDataOutputStream& wxDataOutputStream::operator<<(double f)
738 {
739 WriteDouble(f);
740 return *this;
741 }
742
743 wxDataOutputStream& wxDataOutputStream::operator<<(float f)
744 {
745 WriteDouble((double)f);
746 return *this;
747 }
748
749 #endif
750 // wxUSE_STREAMS