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