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