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