]>
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"
23 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
28 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, wxMBConv
& conv
)
29 : m_input(&s
), m_be_order(false), m_conv(conv
)
31 wxDataInputStream::wxDataInputStream(wxInputStream
& s
)
32 : m_input(&s
), m_be_order(false)
37 wxUint64
wxDataInputStream::Read64()
41 m_input
->Read(&i64
, 8);
44 return wxUINT64_SWAP_ON_LE(i64
);
46 return wxUINT64_SWAP_ON_BE(i64
);
49 wxUint32
wxDataInputStream::Read32()
53 m_input
->Read(&i32
, 4);
56 return wxUINT32_SWAP_ON_LE(i32
);
58 return wxUINT32_SWAP_ON_BE(i32
);
61 wxUint16
wxDataInputStream::Read16()
65 m_input
->Read(&i16
, 2);
68 return wxUINT16_SWAP_ON_LE(i16
);
70 return wxUINT16_SWAP_ON_BE(i16
);
73 wxUint8
wxDataInputStream::Read8()
77 m_input
->Read(&buf
, 1);
81 // Must be at global scope for VC++ 5
82 extern "C" double ConvertFromIeeeExtended(const unsigned char *bytes
);
84 double wxDataInputStream::ReadDouble()
89 m_input
->Read(buf
, 10);
90 return ConvertFromIeeeExtended((unsigned char *)buf
);
96 wxString
wxDataInputStream::ReadString()
105 wxCharBuffer
tmp(len
+ 1);
106 m_input
->Read(tmp
.data(), len
);
107 tmp
.data()[len
] = '\0';
108 wxString
ret(m_conv
.cMB2WX(tmp
.data()));
111 m_input
->Read( wxStringBuffer(ret
, len
), len
);
116 return wxEmptyString
;
119 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
121 m_input
->Read(buffer
, size
* 8);
125 for (wxUint32 i
=0; i
<size
; i
++)
127 wxUint64 v
= wxUINT64_SWAP_ON_LE(*buffer
);
133 for (wxUint32 i
=0; i
<size
; i
++)
135 wxUint64 v
= wxUINT64_SWAP_ON_BE(*buffer
);
141 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
143 m_input
->Read(buffer
, size
* 4);
147 for (wxUint32 i
=0; i
<size
; i
++)
149 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
155 for (wxUint32 i
=0; i
<size
; i
++)
157 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
163 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
165 m_input
->Read(buffer
, size
* 2);
169 for (wxUint32 i
=0; i
<size
; i
++)
171 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
177 for (wxUint32 i
=0; i
<size
; i
++)
179 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
185 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
187 m_input
->Read(buffer
, size
);
190 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
192 for (wxUint32 i
=0; i
<size
; i
++)
194 *(buffer
++) = ReadDouble();
198 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
204 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
210 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
212 i
= (wxInt16
)Read16();
216 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
218 i
= (wxInt32
)Read32();
222 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
228 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
234 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
240 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
246 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
252 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
254 f
= (float)ReadDouble();
258 // ---------------------------------------------------------------------------
259 // wxDataOutputStream
260 // ---------------------------------------------------------------------------
263 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
264 : m_output(&s
), m_be_order(false), m_conv(conv
)
266 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
267 : m_output(&s
), m_be_order(false)
272 void wxDataOutputStream::Write64(wxUint64 i
)
277 i64
= wxUINT64_SWAP_ON_LE(i
);
279 i64
= wxUINT64_SWAP_ON_BE(i
);
280 m_output
->Write(&i64
, 8);
283 void wxDataOutputStream::Write32(wxUint32 i
)
288 i32
= wxUINT32_SWAP_ON_LE(i
);
290 i32
= wxUINT32_SWAP_ON_BE(i
);
291 m_output
->Write(&i32
, 4);
294 void wxDataOutputStream::Write16(wxUint16 i
)
299 i16
= wxUINT16_SWAP_ON_LE(i
);
301 i16
= wxUINT16_SWAP_ON_BE(i
);
303 m_output
->Write(&i16
, 2);
306 void wxDataOutputStream::Write8(wxUint8 i
)
308 m_output
->Write(&i
, 1);
311 void wxDataOutputStream::WriteString(const wxString
& string
)
314 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
316 const wxWX2MBbuf buf
= string
.mb_str();
318 size_t len
= strlen(buf
);
321 m_output
->Write(buf
, len
);
324 // Must be at global scope for VC++ 5
325 extern "C" void ConvertToIeeeExtended(double num
, unsigned char *bytes
);
327 void wxDataOutputStream::WriteDouble(double d
)
332 ConvertToIeeeExtended(d
, (unsigned char *)buf
);
334 #if !defined(__VMS__) && !defined(__GNUG__)
335 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
339 m_output
->Write(buf
, 10);
342 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
346 for (wxUint32 i
=0; i
<size
;i
++)
348 wxUint64 i64
= wxUINT64_SWAP_ON_LE(*buffer
);
350 m_output
->Write(&i64
, 8);
355 for (wxUint32 i
=0; i
<size
;i
++)
357 wxUint64 i64
= wxUINT64_SWAP_ON_BE(*buffer
);
359 m_output
->Write(&i64
, 8);
364 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
368 for (wxUint32 i
=0; i
<size
;i
++)
370 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
372 m_output
->Write(&i32
, 4);
377 for (wxUint32 i
=0; i
<size
;i
++)
379 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
381 m_output
->Write(&i32
, 4);
386 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
390 for (wxUint32 i
=0; i
<size
;i
++)
392 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
394 m_output
->Write(&i16
, 2);
399 for (wxUint32 i
=0; i
<size
;i
++)
401 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
403 m_output
->Write(&i16
, 2);
408 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
410 m_output
->Write(buffer
, size
);
413 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
415 for (wxUint32 i
=0; i
<size
; i
++)
417 WriteDouble(*(buffer
++));
421 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
423 Write32(wxStrlen(string
));
424 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
428 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
434 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
440 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
442 Write16((wxUint16
)i
);
446 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
448 Write32((wxUint32
)i
);
452 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
458 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
464 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
470 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
476 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
482 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
484 WriteDouble((double)f
);