]>
git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
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 wxUint64
wxDataInputStream::Read64()
45 m_input
->Read(&i64
, 8);
48 return wxUINT64_SWAP_ON_LE(i64
);
50 return wxUINT64_SWAP_ON_BE(i64
);
53 wxUint32
wxDataInputStream::Read32()
57 m_input
->Read(&i32
, 4);
60 return wxUINT32_SWAP_ON_LE(i32
);
62 return wxUINT32_SWAP_ON_BE(i32
);
65 wxUint16
wxDataInputStream::Read16()
69 m_input
->Read(&i16
, 2);
72 return wxUINT16_SWAP_ON_LE(i16
);
74 return wxUINT16_SWAP_ON_BE(i16
);
77 wxUint8
wxDataInputStream::Read8()
81 m_input
->Read(&buf
, 1);
85 // Must be at global scope for VC++ 5
86 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes
);
88 double wxDataInputStream::ReadDouble()
93 m_input
->Read(buf
, 10);
94 return ConvertFromIeeeExtended((unsigned char *)buf
);
100 wxString
wxDataInputStream::ReadString()
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()));
115 m_input
->Read( wxStringBuffer(ret
, len
), len
);
120 return wxEmptyString
;
123 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
125 m_input
->Read(buffer
, size
* 8);
129 for (wxUint32 i
=0; i
<size
; i
++)
131 wxUint64 v
= wxUINT64_SWAP_ON_LE(*buffer
);
137 for (wxUint32 i
=0; i
<size
; i
++)
139 wxUint64 v
= wxUINT64_SWAP_ON_BE(*buffer
);
145 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
147 m_input
->Read(buffer
, size
* 4);
151 for (wxUint32 i
=0; i
<size
; i
++)
153 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
159 for (wxUint32 i
=0; i
<size
; i
++)
161 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
167 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
169 m_input
->Read(buffer
, size
* 2);
173 for (wxUint32 i
=0; i
<size
; i
++)
175 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
181 for (wxUint32 i
=0; i
<size
; i
++)
183 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
189 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
191 m_input
->Read(buffer
, size
);
194 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
196 for (wxUint32 i
=0; i
<size
; i
++)
198 *(buffer
++) = ReadDouble();
202 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
208 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
214 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
216 i
= (wxInt16
)Read16();
220 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
222 i
= (wxInt32
)Read32();
226 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
232 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
238 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
244 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
250 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
256 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
258 f
= (float)ReadDouble();
262 // ---------------------------------------------------------------------------
263 // wxDataOutputStream
264 // ---------------------------------------------------------------------------
267 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
268 : m_output(&s
), m_be_order(false), m_conv(conv
)
270 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
271 : m_output(&s
), m_be_order(false)
276 void wxDataOutputStream::Write64(wxUint64 i
)
281 i64
= wxUINT64_SWAP_ON_LE(i
);
283 i64
= wxUINT64_SWAP_ON_BE(i
);
284 m_output
->Write(&i64
, 8);
287 void wxDataOutputStream::Write32(wxUint32 i
)
292 i32
= wxUINT32_SWAP_ON_LE(i
);
294 i32
= wxUINT32_SWAP_ON_BE(i
);
295 m_output
->Write(&i32
, 4);
298 void wxDataOutputStream::Write16(wxUint16 i
)
303 i16
= wxUINT16_SWAP_ON_LE(i
);
305 i16
= wxUINT16_SWAP_ON_BE(i
);
307 m_output
->Write(&i16
, 2);
310 void wxDataOutputStream::Write8(wxUint8 i
)
312 m_output
->Write(&i
, 1);
315 void wxDataOutputStream::WriteString(const wxString
& string
)
318 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
320 const wxWX2MBbuf buf
= string
.mb_str();
322 size_t len
= strlen(buf
);
325 m_output
->Write(buf
, len
);
328 // Must be at global scope for VC++ 5
329 extern "C" void ConvertToIeeeExtended(double num
, unsigned char *bytes
);
331 void wxDataOutputStream::WriteDouble(double d
)
336 ConvertToIeeeExtended(d
, (unsigned char *)buf
);
338 #if !defined(__VMS__) && !defined(__GNUG__)
339 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
343 m_output
->Write(buf
, 10);
346 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
350 for (wxUint32 i
=0; i
<size
;i
++)
352 wxUint64 i64
= wxUINT64_SWAP_ON_LE(*buffer
);
354 m_output
->Write(&i64
, 8);
359 for (wxUint32 i
=0; i
<size
;i
++)
361 wxUint64 i64
= wxUINT64_SWAP_ON_BE(*buffer
);
363 m_output
->Write(&i64
, 8);
368 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
372 for (wxUint32 i
=0; i
<size
;i
++)
374 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
376 m_output
->Write(&i32
, 4);
381 for (wxUint32 i
=0; i
<size
;i
++)
383 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
385 m_output
->Write(&i32
, 4);
390 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
394 for (wxUint32 i
=0; i
<size
;i
++)
396 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
398 m_output
->Write(&i16
, 2);
403 for (wxUint32 i
=0; i
<size
;i
++)
405 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
407 m_output
->Write(&i16
, 2);
412 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
414 m_output
->Write(buffer
, size
);
417 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
419 for (wxUint32 i
=0; i
<size
; i
++)
421 WriteDouble(*(buffer
++));
425 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
427 Write32(wxStrlen(string
));
428 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
432 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
438 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
444 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
446 Write16((wxUint16
)i
);
450 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
452 Write32((wxUint32
)i
);
456 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
462 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
468 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
474 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
480 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
486 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
488 WriteDouble((double)f
);