]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/datstrm.h"
24 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
29 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, wxMBConv
& conv
)
30 : m_input(&s
), m_be_order(false), m_conv(conv
)
32 wxDataInputStream::wxDataInputStream(wxInputStream
& s
)
33 : m_input(&s
), m_be_order(false)
38 wxUint64
wxDataInputStream::Read64()
42 m_input
->Read(&i64
, 8);
45 return wxUINT64_SWAP_ON_LE(i64
);
47 return wxUINT64_SWAP_ON_BE(i64
);
50 wxUint32
wxDataInputStream::Read32()
54 m_input
->Read(&i32
, 4);
57 return wxUINT32_SWAP_ON_LE(i32
);
59 return wxUINT32_SWAP_ON_BE(i32
);
62 wxUint16
wxDataInputStream::Read16()
66 m_input
->Read(&i16
, 2);
69 return wxUINT16_SWAP_ON_LE(i16
);
71 return wxUINT16_SWAP_ON_BE(i16
);
74 wxUint8
wxDataInputStream::Read8()
78 m_input
->Read(&buf
, 1);
82 double wxDataInputStream::ReadDouble()
87 m_input
->Read(buf
, 10);
88 return ConvertFromIeeeExtended((const wxInt8
*)buf
);
94 wxString
wxDataInputStream::ReadString()
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()));
109 m_input
->Read( wxStringBuffer(ret
, len
), len
);
114 return wxEmptyString
;
117 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
119 m_input
->Read(buffer
, size
* 8);
123 for (wxUint32 i
=0; i
<size
; i
++)
125 wxUint64 v
= wxUINT64_SWAP_ON_LE(*buffer
);
131 for (wxUint32 i
=0; i
<size
; i
++)
133 wxUint64 v
= wxUINT64_SWAP_ON_BE(*buffer
);
139 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
141 m_input
->Read(buffer
, size
* 4);
145 for (wxUint32 i
=0; i
<size
; i
++)
147 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
153 for (wxUint32 i
=0; i
<size
; i
++)
155 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
161 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
163 m_input
->Read(buffer
, size
* 2);
167 for (wxUint32 i
=0; i
<size
; i
++)
169 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
175 for (wxUint32 i
=0; i
<size
; i
++)
177 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
183 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
185 m_input
->Read(buffer
, size
);
188 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
190 for (wxUint32 i
=0; i
<size
; i
++)
192 *(buffer
++) = ReadDouble();
196 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
202 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
208 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
210 i
= (wxInt16
)Read16();
214 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
216 i
= (wxInt32
)Read32();
220 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
226 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
232 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
238 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
244 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
250 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
252 f
= (float)ReadDouble();
256 // ---------------------------------------------------------------------------
257 // wxDataOutputStream
258 // ---------------------------------------------------------------------------
261 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
262 : m_output(&s
), m_be_order(false), m_conv(conv
)
264 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
265 : m_output(&s
), m_be_order(false)
270 void wxDataOutputStream::Write64(wxUint64 i
)
275 i64
= wxUINT64_SWAP_ON_LE(i
);
277 i64
= wxUINT64_SWAP_ON_BE(i
);
278 m_output
->Write(&i64
, 8);
281 void wxDataOutputStream::Write32(wxUint32 i
)
286 i32
= wxUINT32_SWAP_ON_LE(i
);
288 i32
= wxUINT32_SWAP_ON_BE(i
);
289 m_output
->Write(&i32
, 4);
292 void wxDataOutputStream::Write16(wxUint16 i
)
297 i16
= wxUINT16_SWAP_ON_LE(i
);
299 i16
= wxUINT16_SWAP_ON_BE(i
);
301 m_output
->Write(&i16
, 2);
304 void wxDataOutputStream::Write8(wxUint8 i
)
306 m_output
->Write(&i
, 1);
309 void wxDataOutputStream::WriteString(const wxString
& string
)
312 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
314 const wxWX2MBbuf buf
= string
.mb_str();
316 size_t len
= strlen(buf
);
319 m_output
->Write(buf
, len
);
322 void wxDataOutputStream::WriteDouble(double d
)
327 ConvertToIeeeExtended(d
, (wxInt8
*)buf
);
329 #if !defined(__VMS__) && !defined(__GNUG__)
330 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
334 m_output
->Write(buf
, 10);
337 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
341 for (wxUint32 i
=0; i
<size
;i
++)
343 wxUint64 i64
= wxUINT64_SWAP_ON_LE(*buffer
);
345 m_output
->Write(&i64
, 8);
350 for (wxUint32 i
=0; i
<size
;i
++)
352 wxUint64 i64
= wxUINT64_SWAP_ON_BE(*buffer
);
354 m_output
->Write(&i64
, 8);
359 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
363 for (wxUint32 i
=0; i
<size
;i
++)
365 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
367 m_output
->Write(&i32
, 4);
372 for (wxUint32 i
=0; i
<size
;i
++)
374 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
376 m_output
->Write(&i32
, 4);
381 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
385 for (wxUint32 i
=0; i
<size
;i
++)
387 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
389 m_output
->Write(&i16
, 2);
394 for (wxUint32 i
=0; i
<size
;i
++)
396 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
398 m_output
->Write(&i16
, 2);
403 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
405 m_output
->Write(buffer
, size
);
408 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
410 for (wxUint32 i
=0; i
<size
; i
++)
412 WriteDouble(*(buffer
++));
416 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
418 Write32(wxStrlen(string
));
419 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
423 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
429 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
435 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
437 Write16((wxUint16
)i
);
441 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
443 Write32((wxUint32
)i
);
447 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
453 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
459 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
465 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
471 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
477 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
479 WriteDouble((double)f
);