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