]>
git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
1580055ca70174dfbe62b15d9ef733032bdbb6a0
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Data stream classes
4 // Author: Guilhem Lavaux
5 // Modified by: Mickael Gilabert
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "datstrm.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/datstrm.h"
27 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
32 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, wxMBConv
& conv
)
33 : m_input(&s
), m_be_order(FALSE
), m_conv(conv
)
35 wxDataInputStream::wxDataInputStream(wxInputStream
& s
)
36 : m_input(&s
), m_be_order(FALSE
)
41 wxDataInputStream::~wxDataInputStream()
45 wxUint64
wxDataInputStream::Read64()
49 m_input
->Read(&i64
, 8);
52 return wxUINT64_SWAP_ON_LE(i64
);
54 return wxUINT64_SWAP_ON_BE(i64
);
57 wxUint32
wxDataInputStream::Read32()
61 m_input
->Read(&i32
, 4);
64 return wxUINT32_SWAP_ON_LE(i32
);
66 return wxUINT32_SWAP_ON_BE(i32
);
69 wxUint16
wxDataInputStream::Read16()
73 m_input
->Read(&i16
, 2);
76 return wxUINT16_SWAP_ON_LE(i16
);
78 return wxUINT16_SWAP_ON_BE(i16
);
81 wxUint8
wxDataInputStream::Read8()
85 m_input
->Read(&buf
, 1);
89 // Must be at global scope for VC++ 5
90 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes
);
92 double wxDataInputStream::ReadDouble()
97 m_input
->Read(buf
, 10);
98 return ConvertFromIeeeExtended((unsigned char *)buf
);
104 wxString
wxDataInputStream::ReadString()
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()));
119 m_input
->Read( wxStringBuffer(ret
, len
), len
);
124 return wxEmptyString
;
127 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
129 m_input
->Read(buffer
, size
* 8);
133 for (wxUint32 i
=0; i
<size
; i
++)
135 wxUint64 v
= wxUINT64_SWAP_ON_LE(*buffer
);
141 for (wxUint32 i
=0; i
<size
; i
++)
143 wxUint64 v
= wxUINT64_SWAP_ON_BE(*buffer
);
149 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
151 m_input
->Read(buffer
, size
* 4);
155 for (wxUint32 i
=0; i
<size
; i
++)
157 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
163 for (wxUint32 i
=0; i
<size
; i
++)
165 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
171 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
173 m_input
->Read(buffer
, size
* 2);
177 for (wxUint32 i
=0; i
<size
; i
++)
179 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
185 for (wxUint32 i
=0; i
<size
; i
++)
187 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
193 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
195 m_input
->Read(buffer
, size
);
198 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
200 for (wxUint32 i
=0; i
<size
; i
++)
202 *(buffer
++) = ReadDouble();
206 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
212 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
218 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
220 i
= (wxInt16
)Read16();
224 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
226 i
= (wxInt32
)Read32();
230 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
236 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
242 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
248 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
254 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
260 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
262 f
= (float)ReadDouble();
266 // ---------------------------------------------------------------------------
267 // wxDataOutputStream
268 // ---------------------------------------------------------------------------
271 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
272 : m_output(&s
), m_be_order(FALSE
), m_conv(conv
)
274 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
275 : m_output(&s
), m_be_order(FALSE
)
280 wxDataOutputStream::~wxDataOutputStream()
284 void wxDataOutputStream::Write64(wxUint64 i
)
289 i64
= wxUINT64_SWAP_ON_LE(i
);
291 i64
= wxUINT64_SWAP_ON_BE(i
);
292 m_output
->Write(&i64
, 8);
295 void wxDataOutputStream::Write32(wxUint32 i
)
300 i32
= wxUINT32_SWAP_ON_LE(i
);
302 i32
= wxUINT32_SWAP_ON_BE(i
);
303 m_output
->Write(&i32
, 4);
306 void wxDataOutputStream::Write16(wxUint16 i
)
311 i16
= wxUINT16_SWAP_ON_LE(i
);
313 i16
= wxUINT16_SWAP_ON_BE(i
);
315 m_output
->Write(&i16
, 2);
318 void wxDataOutputStream::Write8(wxUint8 i
)
320 m_output
->Write(&i
, 1);
323 void wxDataOutputStream::WriteString(const wxString
& string
)
326 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
328 const wxWX2MBbuf buf
= string
.mb_str();
330 size_t len
= strlen(buf
);
333 m_output
->Write(buf
, len
);
336 // Must be at global scope for VC++ 5
337 extern "C" void ConvertToIeeeExtended(double num
, unsigned char *bytes
);
339 void wxDataOutputStream::WriteDouble(double d
)
344 ConvertToIeeeExtended(d
, (unsigned char *)buf
);
347 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
351 m_output
->Write(buf
, 10);
354 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
358 for (wxUint32 i
=0; i
<size
;i
++)
360 wxUint64 i64
= wxUINT64_SWAP_ON_LE(*buffer
);
362 m_output
->Write(&i64
, 8);
367 for (wxUint32 i
=0; i
<size
;i
++)
369 wxUint64 i64
= wxUINT64_SWAP_ON_BE(*buffer
);
371 m_output
->Write(&i64
, 8);
376 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
380 for (wxUint32 i
=0; i
<size
;i
++)
382 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
384 m_output
->Write(&i32
, 4);
389 for (wxUint32 i
=0; i
<size
;i
++)
391 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
393 m_output
->Write(&i32
, 4);
398 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
402 for (wxUint32 i
=0; i
<size
;i
++)
404 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
406 m_output
->Write(&i16
, 2);
411 for (wxUint32 i
=0; i
<size
;i
++)
413 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
415 m_output
->Write(&i16
, 2);
420 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
422 m_output
->Write(buffer
, size
);
425 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
427 for (wxUint32 i
=0; i
<size
; i
++)
429 WriteDouble(*(buffer
++));
433 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
435 Write32(wxStrlen(string
));
436 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
440 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
446 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
452 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
454 Write16((wxUint16
)i
);
458 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
460 Write32((wxUint32
)i
);
464 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
470 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
476 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
482 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
488 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
494 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
496 WriteDouble((double)f
);