]>
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 /////////////////////////////////////////////////////////////////////////////
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 char *tmp
= new char[len
+ 1];
114 m_input
->Read(tmp
, len
);
116 wxString
ret( (const wxChar
*) m_conv
.cMB2WX(tmp
) );
120 m_input
->Read( ret
.GetWriteBuf(len
), len
);
126 return wxEmptyString
;
129 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
131 m_input
->Read(buffer
, size
* 8);
135 for (wxUint32 i
=0; i
<size
; i
++)
137 wxUint64 v
= wxUINT64_SWAP_ON_LE(*buffer
);
143 for (wxUint32 i
=0; i
<size
; i
++)
145 wxUint64 v
= wxUINT64_SWAP_ON_BE(*buffer
);
151 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
153 m_input
->Read(buffer
, size
* 4);
157 for (wxUint32 i
=0; i
<size
; i
++)
159 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
165 for (wxUint32 i
=0; i
<size
; i
++)
167 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
173 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
175 m_input
->Read(buffer
, size
* 2);
179 for (wxUint32 i
=0; i
<size
; i
++)
181 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
187 for (wxUint32 i
=0; i
<size
; i
++)
189 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
195 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
197 m_input
->Read(buffer
, size
);
200 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
202 for (wxUint32 i
=0; i
<size
; i
++)
204 *(buffer
++) = ReadDouble();
208 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
214 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
220 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
222 i
= (wxInt16
)Read16();
226 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
228 i
= (wxInt32
)Read32();
232 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
238 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
244 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
250 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
256 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
262 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
264 f
= (float)ReadDouble();
268 // ---------------------------------------------------------------------------
269 // wxDataOutputStream
270 // ---------------------------------------------------------------------------
273 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
274 : m_output(&s
), m_be_order(FALSE
), m_conv(conv
)
276 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
277 : m_output(&s
), m_be_order(FALSE
)
282 wxDataOutputStream::~wxDataOutputStream()
286 void wxDataOutputStream::Write64(wxUint64 i
)
291 i64
= wxUINT64_SWAP_ON_LE(i
);
293 i64
= wxUINT64_SWAP_ON_BE(i
);
294 m_output
->Write(&i64
, 8);
297 void wxDataOutputStream::Write32(wxUint32 i
)
302 i32
= wxUINT32_SWAP_ON_LE(i
);
304 i32
= wxUINT32_SWAP_ON_BE(i
);
305 m_output
->Write(&i32
, 4);
308 void wxDataOutputStream::Write16(wxUint16 i
)
313 i16
= wxUINT16_SWAP_ON_LE(i
);
315 i16
= wxUINT16_SWAP_ON_BE(i
);
317 m_output
->Write(&i16
, 2);
320 void wxDataOutputStream::Write8(wxUint8 i
)
322 m_output
->Write(&i
, 1);
325 void wxDataOutputStream::WriteString(const wxString
& string
)
328 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
330 const wxWX2MBbuf buf
= string
.mb_str();
332 size_t len
= strlen(buf
);
335 m_output
->Write(buf
, len
);
338 // Must be at global scope for VC++ 5
339 extern "C" void ConvertToIeeeExtended(double num
, unsigned char *bytes
);
341 void wxDataOutputStream::WriteDouble(double d
)
346 ConvertToIeeeExtended(d
, (unsigned char *)buf
);
349 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
353 m_output
->Write(buf
, 10);
356 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
360 for (wxUint32 i
=0; i
<size
;i
++)
362 wxUint64 i64
= wxUINT64_SWAP_ON_LE(*buffer
);
364 m_output
->Write(&i64
, 8);
369 for (wxUint32 i
=0; i
<size
;i
++)
371 wxUint64 i64
= wxUINT64_SWAP_ON_BE(*buffer
);
373 m_output
->Write(&i64
, 8);
378 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
382 for (wxUint32 i
=0; i
<size
;i
++)
384 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
386 m_output
->Write(&i32
, 4);
391 for (wxUint32 i
=0; i
<size
;i
++)
393 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
395 m_output
->Write(&i32
, 4);
400 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
404 for (wxUint32 i
=0; i
<size
;i
++)
406 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
408 m_output
->Write(&i16
, 2);
413 for (wxUint32 i
=0; i
<size
;i
++)
415 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
417 m_output
->Write(&i16
, 2);
422 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
424 m_output
->Write(buffer
, size
);
427 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
429 for (wxUint32 i
=0; i
<size
; i
++)
431 WriteDouble(*(buffer
++));
435 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
437 Write32(wxStrlen(string
));
438 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
442 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
448 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
454 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
456 Write16((wxUint16
)i
);
460 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
462 Write32((wxUint32
)i
);
466 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
472 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
478 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
484 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
490 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
496 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
498 WriteDouble((double)f
);