]>
git.saurik.com Git - wxWidgets.git/blob - src/common/datstrm.cpp
861b46a8d7115062efb6fb089be332cb673b20bd
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"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 wxDataStreamBase::wxDataStreamBase(const wxMBConv
& conv
)
33 : m_conv(conv
.Clone())
34 #endif // wxUSE_UNICODE
36 // It is unused in non-Unicode build, so suppress a warning there.
43 void wxDataStreamBase::SetConv( const wxMBConv
&conv
)
46 m_conv
= conv
.Clone();
50 wxDataStreamBase::~wxDataStreamBase()
54 #endif // wxUSE_UNICODE
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, const wxMBConv
& conv
)
62 : wxDataStreamBase(conv
),
68 wxUint64
wxDataInputStream::Read64()
76 wxUint32
wxDataInputStream::Read32()
80 m_input
->Read(&i32
, 4);
83 return wxUINT32_SWAP_ON_LE(i32
);
85 return wxUINT32_SWAP_ON_BE(i32
);
88 wxUint16
wxDataInputStream::Read16()
92 m_input
->Read(&i16
, 2);
95 return wxUINT16_SWAP_ON_LE(i16
);
97 return wxUINT16_SWAP_ON_BE(i16
);
100 wxUint8
wxDataInputStream::Read8()
104 m_input
->Read(&buf
, 1);
108 double wxDataInputStream::ReadDouble()
113 m_input
->Read(buf
, 10);
114 return wxConvertFromIeeeExtended((const wxInt8
*)buf
);
120 wxString
wxDataInputStream::ReadString()
124 const size_t len
= Read32();
128 wxCharBuffer
tmp(len
);
131 m_input
->Read(tmp
.data(), len
);
132 ret
= m_conv
->cMB2WX(tmp
.data());
135 wxStringBuffer
buf(ret
, len
);
137 m_input
->Read(buf
, len
);
148 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
151 unsigned char *pchBuffer
= new unsigned char[size
* 8];
152 // TODO: Check for overflow when size is of type uint and is > than 512m
153 input
->Read(pchBuffer
, size
* 8);
157 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
159 buffer
[uiIndex
] = 0l;
160 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
162 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
163 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
169 else // little endian
171 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
173 buffer
[uiIndex
] = 0l;
174 for ( unsigned ui
=0; ui
!=8; ++ui
)
175 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
176 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
184 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
187 unsigned char *pchBuffer
= new unsigned char[size
* 8];
191 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
193 DataType i64
= buffer
[uiIndex
];
194 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
196 pchBuffer
[idx_base
+ 7 - ui
] =
197 (unsigned char) (i64
.GetLo() & 255l);
204 else // little endian
206 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
208 DataType i64
= buffer
[uiIndex
];
209 for (unsigned ui
=0; ui
!=8; ++ui
)
211 pchBuffer
[idx_base
+ ui
] =
212 (unsigned char) (i64
.GetLo() & 255l);
220 // TODO: Check for overflow when size is of type uint and is > than 512m
221 output
->Write(pchBuffer
, size
* 8);
225 #endif // wxUSE_LONGLONG
231 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
234 unsigned char *pchBuffer
= (unsigned char*) buffer
;
235 // TODO: Check for overflow when size is of type uint and is > than 512m
236 input
->Read(pchBuffer
, size
* 8);
239 for ( wxUint32 i
= 0; i
< size
; i
++ )
241 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
245 else // little endian
247 for ( wxUint32 i
=0; i
<size
; i
++ )
249 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
257 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
262 for ( size_t i
= 0; i
< size
; i
++ )
264 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
266 output
->Write(&i64
, 8);
269 else // little endian
271 for ( size_t i
=0; i
< size
; i
++ )
273 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
275 output
->Write(&i64
, 8);
280 #endif // wxLongLong_t
284 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
287 DoReadLL(buffer
, size
, m_input
, m_be_order
);
289 DoReadI64(buffer
, size
, m_input
, m_be_order
);
293 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
296 DoReadLL(buffer
, size
, m_input
, m_be_order
);
298 DoReadI64(buffer
, size
, m_input
, m_be_order
);
301 #endif // wxHAS_INT64
303 #if defined(wxLongLong_t) && wxUSE_LONGLONG
304 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
306 DoReadLL(buffer
, size
, m_input
, m_be_order
);
309 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
311 DoReadLL(buffer
, size
, m_input
, m_be_order
);
313 #endif // wxLongLong_t
316 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
318 DoReadLL(buffer
, size
, m_input
, m_be_order
);
321 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
323 DoReadLL(buffer
, size
, m_input
, m_be_order
);
326 wxLongLong
wxDataInputStream::ReadLL(void)
329 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
332 #endif // wxUSE_LONGLONG
334 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
336 m_input
->Read(buffer
, size
* 4);
340 for (wxUint32 i
=0; i
<size
; i
++)
342 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
348 for (wxUint32 i
=0; i
<size
; i
++)
350 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
356 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
358 m_input
->Read(buffer
, size
* 2);
362 for (wxUint32 i
=0; i
<size
; i
++)
364 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
370 for (wxUint32 i
=0; i
<size
; i
++)
372 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
378 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
380 m_input
->Read(buffer
, size
);
383 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
385 for (wxUint32 i
=0; i
<size
; i
++)
387 *(buffer
++) = ReadDouble();
391 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
397 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
403 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
405 i
= (wxInt16
)Read16();
409 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
411 i
= (wxInt32
)Read32();
415 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
421 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
427 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
434 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
440 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
445 #endif // wxHAS_INT64
447 #if defined(wxLongLong_t) && wxUSE_LONGLONG
448 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
454 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
459 #endif // wxLongLong_t
461 wxDataInputStream
& wxDataInputStream::operator>>(double& d
)
467 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
469 f
= (float)ReadDouble();
473 // ---------------------------------------------------------------------------
474 // wxDataOutputStream
475 // ---------------------------------------------------------------------------
477 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
478 : wxDataStreamBase(conv
),
484 void wxDataOutputStream::Write64(wxUint64 i
)
489 void wxDataOutputStream::Write64(wxInt64 i
)
493 #endif // wxHAS_INT64
495 void wxDataOutputStream::Write32(wxUint32 i
)
500 i32
= wxUINT32_SWAP_ON_LE(i
);
502 i32
= wxUINT32_SWAP_ON_BE(i
);
503 m_output
->Write(&i32
, 4);
506 void wxDataOutputStream::Write16(wxUint16 i
)
511 i16
= wxUINT16_SWAP_ON_LE(i
);
513 i16
= wxUINT16_SWAP_ON_BE(i
);
515 m_output
->Write(&i16
, 2);
518 void wxDataOutputStream::Write8(wxUint8 i
)
520 m_output
->Write(&i
, 1);
523 void wxDataOutputStream::WriteString(const wxString
& string
)
526 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
528 const wxWX2MBbuf buf
= string
.mb_str();
530 size_t len
= strlen(buf
);
533 m_output
->Write(buf
, len
);
536 void wxDataOutputStream::WriteDouble(double d
)
541 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
544 #if !defined(__VMS__) && !defined(__GNUG__)
546 # pragma message("wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!")
548 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
553 m_output
->Write(buf
, 10);
557 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
560 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
562 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
566 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
569 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
571 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
574 #endif // wxHAS_INT64
576 #if defined(wxLongLong_t) && wxUSE_LONGLONG
577 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
579 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
582 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
584 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
586 #endif // wxLongLong_t
589 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
591 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
594 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
596 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
599 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
604 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
608 #endif // wxUSE_LONGLONG
610 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
614 for (wxUint32 i
=0; i
<size
;i
++)
616 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
618 m_output
->Write(&i32
, 4);
623 for (wxUint32 i
=0; i
<size
;i
++)
625 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
627 m_output
->Write(&i32
, 4);
632 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
636 for (wxUint32 i
=0; i
<size
;i
++)
638 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
640 m_output
->Write(&i16
, 2);
645 for (wxUint32 i
=0; i
<size
;i
++)
647 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
649 m_output
->Write(&i16
, 2);
654 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
656 m_output
->Write(buffer
, size
);
659 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
661 for (wxUint32 i
=0; i
<size
; i
++)
663 WriteDouble(*(buffer
++));
667 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
673 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
679 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
681 Write16((wxUint16
)i
);
685 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
687 Write32((wxUint32
)i
);
691 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
697 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
703 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
710 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
716 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
721 #endif // wxHAS_INT64
723 #if defined(wxLongLong_t) && wxUSE_LONGLONG
724 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
730 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
735 #endif // wxLongLong_t
737 wxDataOutputStream
& wxDataOutputStream::operator<<(double d
)
743 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
745 WriteDouble((double)f
);