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