]>
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
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/datstrm.h"
29 // helper unions used to swap bytes of floats and doubles
42 } // anonymous namespace
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxDataStreamBase::wxDataStreamBase(const wxMBConv
& conv
)
50 : m_conv(conv
.Clone())
51 #endif // wxUSE_UNICODE
53 // It is unused in non-Unicode build, so suppress a warning there.
58 // For compatibility with the existing data files, we use extended
59 // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on.
61 m_useExtendedPrecision
= true;
62 #endif // wxUSE_APPLE_IEEE
66 void wxDataStreamBase::SetConv( const wxMBConv
&conv
)
69 m_conv
= conv
.Clone();
73 wxDataStreamBase::~wxDataStreamBase()
77 #endif // wxUSE_UNICODE
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
84 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, const wxMBConv
& conv
)
85 : wxDataStreamBase(conv
),
91 wxUint64
wxDataInputStream::Read64()
99 wxUint32
wxDataInputStream::Read32()
103 m_input
->Read(&i32
, 4);
106 return wxUINT32_SWAP_ON_LE(i32
);
108 return wxUINT32_SWAP_ON_BE(i32
);
111 wxUint16
wxDataInputStream::Read16()
115 m_input
->Read(&i16
, 2);
118 return wxUINT16_SWAP_ON_LE(i16
);
120 return wxUINT16_SWAP_ON_BE(i16
);
123 wxUint8
wxDataInputStream::Read8()
127 m_input
->Read(&buf
, 1);
131 double wxDataInputStream::ReadDouble()
134 if ( m_useExtendedPrecision
)
138 m_input
->Read(buf
, 10);
139 return wxConvertFromIeeeExtended((const wxInt8
*)buf
);
142 #endif // wxUSE_APPLE_IEEE
144 Float64Data floatData
;
146 if ( m_be_order
== (wxBYTE_ORDER
== wxBIG_ENDIAN
) )
148 floatData
.i
[0] = Read32();
149 floatData
.i
[1] = Read32();
153 floatData
.i
[1] = Read32();
154 floatData
.i
[0] = Read32();
157 return static_cast<double>(floatData
.f
);
161 float wxDataInputStream::ReadFloat()
164 if ( m_useExtendedPrecision
)
166 return (float)ReadDouble();
169 #endif // wxUSE_APPLE_IEEE
171 Float32Data floatData
;
173 floatData
.i
= Read32();
174 return static_cast<float>(floatData
.f
);
178 wxString
wxDataInputStream::ReadString()
182 const size_t len
= Read32();
186 wxCharBuffer
tmp(len
);
189 m_input
->Read(tmp
.data(), len
);
190 ret
= m_conv
->cMB2WX(tmp
.data());
193 wxStringBuffer
buf(ret
, len
);
195 m_input
->Read(buf
, len
);
206 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
209 unsigned char *pchBuffer
= new unsigned char[size
* 8];
210 // TODO: Check for overflow when size is of type uint and is > than 512m
211 input
->Read(pchBuffer
, size
* 8);
215 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
217 buffer
[uiIndex
] = 0l;
218 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
220 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
221 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
227 else // little endian
229 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
231 buffer
[uiIndex
] = 0l;
232 for ( unsigned ui
=0; ui
!=8; ++ui
)
233 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
234 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
242 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
245 unsigned char *pchBuffer
= new unsigned char[size
* 8];
249 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
251 DataType i64
= buffer
[uiIndex
];
252 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
254 pchBuffer
[idx_base
+ 7 - ui
] =
255 (unsigned char) (i64
.GetLo() & 255l);
262 else // little endian
264 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
266 DataType i64
= buffer
[uiIndex
];
267 for (unsigned ui
=0; ui
!=8; ++ui
)
269 pchBuffer
[idx_base
+ ui
] =
270 (unsigned char) (i64
.GetLo() & 255l);
278 // TODO: Check for overflow when size is of type uint and is > than 512m
279 output
->Write(pchBuffer
, size
* 8);
283 #endif // wxUSE_LONGLONG
289 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
292 unsigned char *pchBuffer
= (unsigned char*) buffer
;
293 // TODO: Check for overflow when size is of type uint and is > than 512m
294 input
->Read(pchBuffer
, size
* 8);
297 for ( wxUint32 i
= 0; i
< size
; i
++ )
299 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
303 else // little endian
305 for ( wxUint32 i
=0; i
<size
; i
++ )
307 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
315 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
320 for ( size_t i
= 0; i
< size
; i
++ )
322 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
324 output
->Write(&i64
, 8);
327 else // little endian
329 for ( size_t i
=0; i
< size
; i
++ )
331 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
333 output
->Write(&i64
, 8);
338 #endif // wxLongLong_t
342 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
345 DoReadLL(buffer
, size
, m_input
, m_be_order
);
347 DoReadI64(buffer
, size
, m_input
, m_be_order
);
351 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
354 DoReadLL(buffer
, size
, m_input
, m_be_order
);
356 DoReadI64(buffer
, size
, m_input
, m_be_order
);
359 #endif // wxHAS_INT64
361 #if defined(wxLongLong_t) && wxUSE_LONGLONG
362 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
364 DoReadLL(buffer
, size
, m_input
, m_be_order
);
367 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
369 DoReadLL(buffer
, size
, m_input
, m_be_order
);
371 #endif // wxLongLong_t
374 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
376 DoReadLL(buffer
, size
, m_input
, m_be_order
);
379 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
381 DoReadLL(buffer
, size
, m_input
, m_be_order
);
384 wxLongLong
wxDataInputStream::ReadLL(void)
387 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
390 #endif // wxUSE_LONGLONG
392 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
394 m_input
->Read(buffer
, size
* 4);
398 for (wxUint32 i
=0; i
<size
; i
++)
400 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
406 for (wxUint32 i
=0; i
<size
; i
++)
408 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
414 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
416 m_input
->Read(buffer
, size
* 2);
420 for (wxUint32 i
=0; i
<size
; i
++)
422 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
428 for (wxUint32 i
=0; i
<size
; i
++)
430 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
436 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
438 m_input
->Read(buffer
, size
);
441 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
443 for (wxUint32 i
=0; i
<size
; i
++)
445 *(buffer
++) = ReadDouble();
449 void wxDataInputStream::ReadFloat(float *buffer
, size_t size
)
451 for (wxUint32 i
=0; i
<size
; i
++)
453 *(buffer
++) = ReadFloat();
457 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
463 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
469 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
471 i
= (wxInt16
)Read16();
475 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
477 i
= (wxInt32
)Read32();
481 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
487 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
493 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
500 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
506 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
511 #endif // wxHAS_INT64
513 #if defined(wxLongLong_t) && wxUSE_LONGLONG
514 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
520 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
525 #endif // wxLongLong_t
527 wxDataInputStream
& wxDataInputStream::operator>>(double& d
)
533 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
539 // ---------------------------------------------------------------------------
540 // wxDataOutputStream
541 // ---------------------------------------------------------------------------
543 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
544 : wxDataStreamBase(conv
),
550 void wxDataOutputStream::Write64(wxUint64 i
)
555 void wxDataOutputStream::Write64(wxInt64 i
)
559 #endif // wxHAS_INT64
561 void wxDataOutputStream::Write32(wxUint32 i
)
566 i32
= wxUINT32_SWAP_ON_LE(i
);
568 i32
= wxUINT32_SWAP_ON_BE(i
);
569 m_output
->Write(&i32
, 4);
572 void wxDataOutputStream::Write16(wxUint16 i
)
577 i16
= wxUINT16_SWAP_ON_LE(i
);
579 i16
= wxUINT16_SWAP_ON_BE(i
);
581 m_output
->Write(&i16
, 2);
584 void wxDataOutputStream::Write8(wxUint8 i
)
586 m_output
->Write(&i
, 1);
589 void wxDataOutputStream::WriteString(const wxString
& string
)
592 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
594 const wxWX2MBbuf buf
= string
.mb_str();
596 size_t len
= strlen(buf
);
599 m_output
->Write(buf
, len
);
602 void wxDataOutputStream::WriteDouble(double d
)
605 if ( m_useExtendedPrecision
)
609 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
610 m_output
->Write(buf
, 10);
613 #endif // wxUSE_APPLE_IEEE
615 Float64Data floatData
;
617 floatData
.f
= (wxFloat64
)d
;
619 if ( m_be_order
== (wxBYTE_ORDER
== wxBIG_ENDIAN
) )
621 Write32(floatData
.i
[0]);
622 Write32(floatData
.i
[1]);
626 Write32(floatData
.i
[1]);
627 Write32(floatData
.i
[0]);
632 void wxDataOutputStream::WriteFloat(float f
)
635 if ( m_useExtendedPrecision
)
637 WriteDouble((double)f
);
640 #endif // wxUSE_APPLE_IEEE
642 Float32Data floatData
;
644 floatData
.f
= (wxFloat32
)f
;
645 Write32(floatData
.i
);
650 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
653 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
655 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
659 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
662 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
664 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
667 #endif // wxHAS_INT64
669 #if defined(wxLongLong_t) && wxUSE_LONGLONG
670 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
672 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
675 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
677 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
679 #endif // wxLongLong_t
682 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
684 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
687 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
689 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
692 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
697 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
701 #endif // wxUSE_LONGLONG
703 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
707 for (wxUint32 i
=0; i
<size
;i
++)
709 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
711 m_output
->Write(&i32
, 4);
716 for (wxUint32 i
=0; i
<size
;i
++)
718 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
720 m_output
->Write(&i32
, 4);
725 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
729 for (wxUint32 i
=0; i
<size
;i
++)
731 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
733 m_output
->Write(&i16
, 2);
738 for (wxUint32 i
=0; i
<size
;i
++)
740 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
742 m_output
->Write(&i16
, 2);
747 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
749 m_output
->Write(buffer
, size
);
752 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
754 for (wxUint32 i
=0; i
<size
; i
++)
756 WriteDouble(*(buffer
++));
760 void wxDataOutputStream::WriteFloat(const float *buffer
, size_t size
)
762 for (wxUint32 i
=0; i
<size
; i
++)
764 WriteFloat(*(buffer
++));
768 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
774 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
780 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
782 Write16((wxUint16
)i
);
786 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
788 Write32((wxUint32
)i
);
792 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
798 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
804 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
811 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
817 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
822 #endif // wxHAS_INT64
824 #if defined(wxLongLong_t) && wxUSE_LONGLONG
825 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
831 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
836 #endif // wxLongLong_t
838 wxDataOutputStream
& wxDataOutputStream::operator<<(double d
)
844 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)