]>
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)
39 wxUint64
wxDataInputStream::Read64()
47 wxUint32
wxDataInputStream::Read32()
51 m_input
->Read(&i32
, 4);
54 return wxUINT32_SWAP_ON_LE(i32
);
56 return wxUINT32_SWAP_ON_BE(i32
);
59 wxUint16
wxDataInputStream::Read16()
63 m_input
->Read(&i16
, 2);
66 return wxUINT16_SWAP_ON_LE(i16
);
68 return wxUINT16_SWAP_ON_BE(i16
);
71 wxUint8
wxDataInputStream::Read8()
75 m_input
->Read(&buf
, 1);
79 double wxDataInputStream::ReadDouble()
84 m_input
->Read(buf
, 10);
85 return ConvertFromIeeeExtended((const wxInt8
*)buf
);
91 wxString
wxDataInputStream::ReadString()
100 wxCharBuffer
tmp(len
+ 1);
101 m_input
->Read(tmp
.data(), len
);
102 tmp
.data()[len
] = '\0';
103 wxString
ret(m_conv
.cMB2WX(tmp
.data()));
106 m_input
->Read( wxStringBuffer(ret
, len
), len
);
111 return wxEmptyString
;
118 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
121 unsigned char *pchBuffer
= new unsigned char[size
* 8];
122 // TODO: Check for overflow when size is of type uint and is > than 512m
123 input
->Read(pchBuffer
, size
* 8);
127 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
129 buffer
[uiIndex
] = 0l;
130 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
132 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
133 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
139 else // little endian
141 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
143 buffer
[uiIndex
] = 0l;
144 for ( unsigned ui
=0; ui
!=8; ++ui
)
145 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
146 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
154 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
157 unsigned char *pchBuffer
= new unsigned char[size
* 8];
161 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
163 DataType i64
= buffer
[uiIndex
];
164 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
166 pchBuffer
[idx_base
+ 7 - ui
] =
167 (unsigned char) (i64
.GetLo() & 255l);
174 else // little endian
176 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
178 DataType i64
= buffer
[uiIndex
];
179 for (unsigned ui
=0; ui
!=8; ++ui
)
181 pchBuffer
[idx_base
+ ui
] =
182 (unsigned char) (i64
.GetLo() & 255l);
190 // TODO: Check for overflow when size is of type uint and is > than 512m
191 output
->Write(pchBuffer
, size
* 8);
195 #endif // wxUSE_LONGLONG
201 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
204 unsigned char *pchBuffer
= (unsigned char*) buffer
;
205 // TODO: Check for overflow when size is of type uint and is > than 512m
206 input
->Read(pchBuffer
, size
* 8);
209 for ( wxUint32 i
= 0; i
< size
; i
++ )
211 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
215 else // little endian
217 for ( wxUint32 i
=0; i
<size
; i
++ )
219 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
227 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
232 for ( size_t i
= 0; i
< size
; i
++ )
234 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
236 output
->Write(&i64
, 8);
239 else // little endian
241 for ( size_t i
=0; i
< size
; i
++ )
243 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
245 output
->Write(&i64
, 8);
250 #endif // wxLongLong_t
254 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
257 DoReadLL(buffer
, size
, m_input
, m_be_order
);
259 DoReadI64(buffer
, size
, m_input
, m_be_order
);
263 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
266 DoReadLL(buffer
, size
, m_input
, m_be_order
);
268 DoReadI64(buffer
, size
, m_input
, m_be_order
);
271 #endif // wxHAS_INT64
273 #if defined(wxLongLong_t) && wxUSE_LONGLONG
274 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
276 DoReadLL(buffer
, size
, m_input
, m_be_order
);
279 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
281 DoReadLL(buffer
, size
, m_input
, m_be_order
);
283 #endif // wxLongLong_t
286 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
288 DoReadLL(buffer
, size
, m_input
, m_be_order
);
291 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
293 DoReadLL(buffer
, size
, m_input
, m_be_order
);
296 wxLongLong
wxDataInputStream::ReadLL(void)
299 DoReadLL(&ll
, 1, m_input
, m_be_order
);
302 #endif // wxUSE_LONGLONG
304 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
306 m_input
->Read(buffer
, size
* 4);
310 for (wxUint32 i
=0; i
<size
; i
++)
312 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
318 for (wxUint32 i
=0; i
<size
; i
++)
320 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
326 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
328 m_input
->Read(buffer
, size
* 2);
332 for (wxUint32 i
=0; i
<size
; i
++)
334 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
340 for (wxUint32 i
=0; i
<size
; i
++)
342 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
348 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
350 m_input
->Read(buffer
, size
);
353 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
355 for (wxUint32 i
=0; i
<size
; i
++)
357 *(buffer
++) = ReadDouble();
361 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
367 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
373 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
375 i
= (wxInt16
)Read16();
379 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
381 i
= (wxInt32
)Read32();
385 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
391 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
397 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
404 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
410 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
415 #endif // wxHAS_INT64
417 #if defined(wxLongLong_t) && wxUSE_LONGLONG
418 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
424 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
429 #endif // wxLongLong_t
431 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
437 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
439 f
= (float)ReadDouble();
443 // ---------------------------------------------------------------------------
444 // wxDataOutputStream
445 // ---------------------------------------------------------------------------
448 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, wxMBConv
& conv
)
449 : m_output(&s
), m_be_order(false), m_conv(conv
)
451 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
452 : m_output(&s
), m_be_order(false)
458 void wxDataOutputStream::Write64(wxUint64 i
)
463 void wxDataOutputStream::Write64(wxInt64 i
)
467 #endif // wxHAS_INT64
469 void wxDataOutputStream::Write32(wxUint32 i
)
474 i32
= wxUINT32_SWAP_ON_LE(i
);
476 i32
= wxUINT32_SWAP_ON_BE(i
);
477 m_output
->Write(&i32
, 4);
480 void wxDataOutputStream::Write16(wxUint16 i
)
485 i16
= wxUINT16_SWAP_ON_LE(i
);
487 i16
= wxUINT16_SWAP_ON_BE(i
);
489 m_output
->Write(&i16
, 2);
492 void wxDataOutputStream::Write8(wxUint8 i
)
494 m_output
->Write(&i
, 1);
497 void wxDataOutputStream::WriteString(const wxString
& string
)
500 const wxWX2MBbuf buf
= string
.mb_str(m_conv
);
502 const wxWX2MBbuf buf
= string
.mb_str();
504 size_t len
= strlen(buf
);
507 m_output
->Write(buf
, len
);
510 void wxDataOutputStream::WriteDouble(double d
)
515 ConvertToIeeeExtended(d
, (wxInt8
*)buf
);
517 #if !defined(__VMS__) && !defined(__GNUG__)
518 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
522 m_output
->Write(buf
, 10);
526 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
529 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
531 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
535 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
538 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
540 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
543 #endif // wxHAS_INT64
545 #if defined(wxLongLong_t) && wxUSE_LONGLONG
546 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
548 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
551 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
553 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
555 #endif // wxLongLong_t
558 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
560 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
563 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
565 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
568 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
573 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
577 #endif // wxUSE_LONGLONG
579 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
583 for (wxUint32 i
=0; i
<size
;i
++)
585 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
587 m_output
->Write(&i32
, 4);
592 for (wxUint32 i
=0; i
<size
;i
++)
594 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
596 m_output
->Write(&i32
, 4);
601 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
605 for (wxUint32 i
=0; i
<size
;i
++)
607 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
609 m_output
->Write(&i16
, 2);
614 for (wxUint32 i
=0; i
<size
;i
++)
616 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
618 m_output
->Write(&i16
, 2);
623 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
625 m_output
->Write(buffer
, size
);
628 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
630 for (wxUint32 i
=0; i
<size
; i
++)
632 WriteDouble(*(buffer
++));
636 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
638 Write32(wxStrlen(string
));
639 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
643 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
649 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
655 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
657 Write16((wxUint16
)i
);
661 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
663 Write32((wxUint32
)i
);
667 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
673 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
679 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
686 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
692 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
697 #endif // wxHAS_INT64
699 #if defined(wxLongLong_t) && wxUSE_LONGLONG
700 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
706 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
711 #endif // wxLongLong_t
713 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
719 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
721 WriteDouble((double)f
);