]>
git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/datstrm.cpp
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"
30 // helper unions used to swap bytes of floats and doubles
43 } // anonymous namespace
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxDataStreamBase::wxDataStreamBase(const wxMBConv
& conv
)
51 : m_conv(conv
.Clone())
52 #endif // wxUSE_UNICODE
54 // It is unused in non-Unicode build, so suppress a warning there.
59 // For compatibility with the existing data files, we use extended
60 // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on.
62 m_useExtendedPrecision
= true;
63 #endif // wxUSE_APPLE_IEEE
67 void wxDataStreamBase::SetConv( const wxMBConv
&conv
)
70 m_conv
= conv
.Clone();
74 wxDataStreamBase::~wxDataStreamBase()
78 #endif // wxUSE_UNICODE
81 // ---------------------------------------------------------------------------
83 // ---------------------------------------------------------------------------
85 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, const wxMBConv
& conv
)
86 : wxDataStreamBase(conv
),
92 wxUint64
wxDataInputStream::Read64()
100 wxUint32
wxDataInputStream::Read32()
104 m_input
->Read(&i32
, 4);
107 return wxUINT32_SWAP_ON_LE(i32
);
109 return wxUINT32_SWAP_ON_BE(i32
);
112 wxUint16
wxDataInputStream::Read16()
116 m_input
->Read(&i16
, 2);
119 return wxUINT16_SWAP_ON_LE(i16
);
121 return wxUINT16_SWAP_ON_BE(i16
);
124 wxUint8
wxDataInputStream::Read8()
128 m_input
->Read(&buf
, 1);
132 double wxDataInputStream::ReadDouble()
135 if ( m_useExtendedPrecision
)
139 m_input
->Read(buf
, 10);
140 return wxConvertFromIeeeExtended((const wxInt8
*)buf
);
143 #endif // wxUSE_APPLE_IEEE
145 Float64Data floatData
;
147 if ( m_be_order
== (wxBYTE_ORDER
== wxBIG_ENDIAN
) )
149 floatData
.i
[0] = Read32();
150 floatData
.i
[1] = Read32();
154 floatData
.i
[1] = Read32();
155 floatData
.i
[0] = Read32();
158 return static_cast<double>(floatData
.f
);
162 float wxDataInputStream::ReadFloat()
165 if ( m_useExtendedPrecision
)
167 return (float)ReadDouble();
170 #endif // wxUSE_APPLE_IEEE
172 Float32Data floatData
;
174 floatData
.i
= Read32();
175 return static_cast<float>(floatData
.f
);
179 wxString
wxDataInputStream::ReadString()
183 const size_t len
= Read32();
187 wxCharBuffer
tmp(len
);
190 m_input
->Read(tmp
.data(), len
);
191 ret
= m_conv
->cMB2WX(tmp
.data());
194 wxStringBuffer
buf(ret
, len
);
196 m_input
->Read(buf
, len
);
207 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
210 unsigned char *pchBuffer
= new unsigned char[size
* 8];
211 // TODO: Check for overflow when size is of type uint and is > than 512m
212 input
->Read(pchBuffer
, size
* 8);
216 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
218 buffer
[uiIndex
] = 0l;
219 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
221 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
222 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
228 else // little endian
230 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
232 buffer
[uiIndex
] = 0l;
233 for ( unsigned ui
=0; ui
!=8; ++ui
)
234 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
235 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
243 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
246 unsigned char *pchBuffer
= new unsigned char[size
* 8];
250 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
252 DataType i64
= buffer
[uiIndex
];
253 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
255 pchBuffer
[idx_base
+ 7 - ui
] =
256 (unsigned char) (i64
.GetLo() & 255l);
263 else // little endian
265 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
267 DataType i64
= buffer
[uiIndex
];
268 for (unsigned ui
=0; ui
!=8; ++ui
)
270 pchBuffer
[idx_base
+ ui
] =
271 (unsigned char) (i64
.GetLo() & 255l);
279 // TODO: Check for overflow when size is of type uint and is > than 512m
280 output
->Write(pchBuffer
, size
* 8);
284 #endif // wxUSE_LONGLONG
290 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
293 unsigned char *pchBuffer
= (unsigned char*) buffer
;
294 // TODO: Check for overflow when size is of type uint and is > than 512m
295 input
->Read(pchBuffer
, size
* 8);
298 for ( wxUint32 i
= 0; i
< size
; i
++ )
300 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
304 else // little endian
306 for ( wxUint32 i
=0; i
<size
; i
++ )
308 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
316 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
321 for ( size_t i
= 0; i
< size
; i
++ )
323 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
325 output
->Write(&i64
, 8);
328 else // little endian
330 for ( size_t i
=0; i
< size
; i
++ )
332 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
334 output
->Write(&i64
, 8);
339 #endif // wxLongLong_t
343 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
346 DoReadLL(buffer
, size
, m_input
, m_be_order
);
348 DoReadI64(buffer
, size
, m_input
, m_be_order
);
352 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
355 DoReadLL(buffer
, size
, m_input
, m_be_order
);
357 DoReadI64(buffer
, size
, m_input
, m_be_order
);
360 #endif // wxHAS_INT64
362 #if defined(wxLongLong_t) && wxUSE_LONGLONG
363 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
365 DoReadLL(buffer
, size
, m_input
, m_be_order
);
368 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
370 DoReadLL(buffer
, size
, m_input
, m_be_order
);
372 #endif // wxLongLong_t
375 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
377 DoReadLL(buffer
, size
, m_input
, m_be_order
);
380 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
382 DoReadLL(buffer
, size
, m_input
, m_be_order
);
385 wxLongLong
wxDataInputStream::ReadLL(void)
388 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
391 #endif // wxUSE_LONGLONG
393 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
395 m_input
->Read(buffer
, size
* 4);
399 for (wxUint32 i
=0; i
<size
; i
++)
401 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
407 for (wxUint32 i
=0; i
<size
; i
++)
409 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
415 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
417 m_input
->Read(buffer
, size
* 2);
421 for (wxUint32 i
=0; i
<size
; i
++)
423 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
429 for (wxUint32 i
=0; i
<size
; i
++)
431 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
437 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
439 m_input
->Read(buffer
, size
);
442 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
444 for (wxUint32 i
=0; i
<size
; i
++)
446 *(buffer
++) = ReadDouble();
450 void wxDataInputStream::ReadFloat(float *buffer
, size_t size
)
452 for (wxUint32 i
=0; i
<size
; i
++)
454 *(buffer
++) = ReadFloat();
458 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
464 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
470 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
472 i
= (wxInt16
)Read16();
476 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
478 i
= (wxInt32
)Read32();
482 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
488 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
494 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
501 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
507 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
512 #endif // wxHAS_INT64
514 #if defined(wxLongLong_t) && wxUSE_LONGLONG
515 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
521 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
526 #endif // wxLongLong_t
528 wxDataInputStream
& wxDataInputStream::operator>>(double& d
)
534 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
540 // ---------------------------------------------------------------------------
541 // wxDataOutputStream
542 // ---------------------------------------------------------------------------
544 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
545 : wxDataStreamBase(conv
),
551 void wxDataOutputStream::Write64(wxUint64 i
)
556 void wxDataOutputStream::Write64(wxInt64 i
)
560 #endif // wxHAS_INT64
562 void wxDataOutputStream::Write32(wxUint32 i
)
567 i32
= wxUINT32_SWAP_ON_LE(i
);
569 i32
= wxUINT32_SWAP_ON_BE(i
);
570 m_output
->Write(&i32
, 4);
573 void wxDataOutputStream::Write16(wxUint16 i
)
578 i16
= wxUINT16_SWAP_ON_LE(i
);
580 i16
= wxUINT16_SWAP_ON_BE(i
);
582 m_output
->Write(&i16
, 2);
585 void wxDataOutputStream::Write8(wxUint8 i
)
587 m_output
->Write(&i
, 1);
590 void wxDataOutputStream::WriteString(const wxString
& string
)
593 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
595 const wxWX2MBbuf buf
= string
.mb_str();
597 size_t len
= strlen(buf
);
600 m_output
->Write(buf
, len
);
603 void wxDataOutputStream::WriteDouble(double d
)
606 if ( m_useExtendedPrecision
)
610 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
611 m_output
->Write(buf
, 10);
614 #endif // wxUSE_APPLE_IEEE
616 Float64Data floatData
;
618 floatData
.f
= (wxFloat64
)d
;
620 if ( m_be_order
== (wxBYTE_ORDER
== wxBIG_ENDIAN
) )
622 Write32(floatData
.i
[0]);
623 Write32(floatData
.i
[1]);
627 Write32(floatData
.i
[1]);
628 Write32(floatData
.i
[0]);
633 void wxDataOutputStream::WriteFloat(float f
)
636 if ( m_useExtendedPrecision
)
638 WriteDouble((double)f
);
641 #endif // wxUSE_APPLE_IEEE
643 Float32Data floatData
;
645 floatData
.f
= (wxFloat32
)f
;
646 Write32(floatData
.i
);
651 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
654 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
656 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
660 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
663 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
665 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
668 #endif // wxHAS_INT64
670 #if defined(wxLongLong_t) && wxUSE_LONGLONG
671 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
673 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
676 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
678 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
680 #endif // wxLongLong_t
683 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
685 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
688 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
690 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
693 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
698 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
702 #endif // wxUSE_LONGLONG
704 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
708 for (wxUint32 i
=0; i
<size
;i
++)
710 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
712 m_output
->Write(&i32
, 4);
717 for (wxUint32 i
=0; i
<size
;i
++)
719 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
721 m_output
->Write(&i32
, 4);
726 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
730 for (wxUint32 i
=0; i
<size
;i
++)
732 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
734 m_output
->Write(&i16
, 2);
739 for (wxUint32 i
=0; i
<size
;i
++)
741 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
743 m_output
->Write(&i16
, 2);
748 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
750 m_output
->Write(buffer
, size
);
753 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
755 for (wxUint32 i
=0; i
<size
; i
++)
757 WriteDouble(*(buffer
++));
761 void wxDataOutputStream::WriteFloat(const float *buffer
, size_t size
)
763 for (wxUint32 i
=0; i
<size
; i
++)
765 WriteFloat(*(buffer
++));
769 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
775 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
781 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
783 Write16((wxUint16
)i
);
787 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
789 Write32((wxUint32
)i
);
793 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
799 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
805 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
812 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
818 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
823 #endif // wxHAS_INT64
825 #if defined(wxLongLong_t) && wxUSE_LONGLONG
826 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
832 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
837 #endif // wxLongLong_t
839 wxDataOutputStream
& wxDataOutputStream::operator<<(double d
)
845 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)