]>
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"
24 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
29 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, const wxMBConv
& conv
)
30 : m_input(&s
), m_be_order(false), m_conv(conv
.Clone())
32 wxDataInputStream::wxDataInputStream(wxInputStream
& s
)
33 : m_input(&s
), m_be_order(false)
38 wxDataInputStream::~wxDataInputStream()
42 #endif // wxUSE_UNICODE
46 wxUint64
wxDataInputStream::Read64()
54 wxUint32
wxDataInputStream::Read32()
58 m_input
->Read(&i32
, 4);
61 return wxUINT32_SWAP_ON_LE(i32
);
63 return wxUINT32_SWAP_ON_BE(i32
);
66 wxUint16
wxDataInputStream::Read16()
70 m_input
->Read(&i16
, 2);
73 return wxUINT16_SWAP_ON_LE(i16
);
75 return wxUINT16_SWAP_ON_BE(i16
);
78 wxUint8
wxDataInputStream::Read8()
82 m_input
->Read(&buf
, 1);
86 double wxDataInputStream::ReadDouble()
91 m_input
->Read(buf
, 10);
92 return ConvertFromIeeeExtended((const wxInt8
*)buf
);
98 wxString
wxDataInputStream::ReadString()
107 wxCharBuffer
tmp(len
+ 1);
108 m_input
->Read(tmp
.data(), len
);
109 tmp
.data()[len
] = '\0';
110 wxString
ret(m_conv
->cMB2WX(tmp
.data()));
113 m_input
->Read( wxStringBuffer(ret
, len
), len
);
118 return wxEmptyString
;
125 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
128 unsigned char *pchBuffer
= new unsigned char[size
* 8];
129 // TODO: Check for overflow when size is of type uint and is > than 512m
130 input
->Read(pchBuffer
, size
* 8);
134 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
136 buffer
[uiIndex
] = 0l;
137 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
139 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
140 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
146 else // little endian
148 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
150 buffer
[uiIndex
] = 0l;
151 for ( unsigned ui
=0; ui
!=8; ++ui
)
152 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
153 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
161 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
164 unsigned char *pchBuffer
= new unsigned char[size
* 8];
168 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
170 DataType i64
= buffer
[uiIndex
];
171 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
173 pchBuffer
[idx_base
+ 7 - ui
] =
174 (unsigned char) (i64
.GetLo() & 255l);
181 else // little endian
183 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
185 DataType i64
= buffer
[uiIndex
];
186 for (unsigned ui
=0; ui
!=8; ++ui
)
188 pchBuffer
[idx_base
+ ui
] =
189 (unsigned char) (i64
.GetLo() & 255l);
197 // TODO: Check for overflow when size is of type uint and is > than 512m
198 output
->Write(pchBuffer
, size
* 8);
202 #endif // wxUSE_LONGLONG
208 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
211 unsigned char *pchBuffer
= (unsigned char*) buffer
;
212 // TODO: Check for overflow when size is of type uint and is > than 512m
213 input
->Read(pchBuffer
, size
* 8);
216 for ( wxUint32 i
= 0; i
< size
; i
++ )
218 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
222 else // little endian
224 for ( wxUint32 i
=0; i
<size
; i
++ )
226 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
234 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
239 for ( size_t i
= 0; i
< size
; i
++ )
241 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
243 output
->Write(&i64
, 8);
246 else // little endian
248 for ( size_t i
=0; i
< size
; i
++ )
250 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
252 output
->Write(&i64
, 8);
257 #endif // wxLongLong_t
261 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
264 DoReadLL(buffer
, size
, m_input
, m_be_order
);
266 DoReadI64(buffer
, size
, m_input
, m_be_order
);
270 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
273 DoReadLL(buffer
, size
, m_input
, m_be_order
);
275 DoReadI64(buffer
, size
, m_input
, m_be_order
);
278 #endif // wxHAS_INT64
280 #if defined(wxLongLong_t) && wxUSE_LONGLONG
281 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
283 DoReadLL(buffer
, size
, m_input
, m_be_order
);
286 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
288 DoReadLL(buffer
, size
, m_input
, m_be_order
);
290 #endif // wxLongLong_t
293 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
295 DoReadLL(buffer
, size
, m_input
, m_be_order
);
298 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
300 DoReadLL(buffer
, size
, m_input
, m_be_order
);
303 wxLongLong
wxDataInputStream::ReadLL(void)
306 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
309 #endif // wxUSE_LONGLONG
311 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
313 m_input
->Read(buffer
, size
* 4);
317 for (wxUint32 i
=0; i
<size
; i
++)
319 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
325 for (wxUint32 i
=0; i
<size
; i
++)
327 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
333 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
335 m_input
->Read(buffer
, size
* 2);
339 for (wxUint32 i
=0; i
<size
; i
++)
341 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
347 for (wxUint32 i
=0; i
<size
; i
++)
349 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
355 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
357 m_input
->Read(buffer
, size
);
360 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
362 for (wxUint32 i
=0; i
<size
; i
++)
364 *(buffer
++) = ReadDouble();
368 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
374 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
380 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
382 i
= (wxInt16
)Read16();
386 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
388 i
= (wxInt32
)Read32();
392 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
398 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
404 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
411 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
417 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
422 #endif // wxHAS_INT64
424 #if defined(wxLongLong_t) && wxUSE_LONGLONG
425 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
431 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
436 #endif // wxLongLong_t
438 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
444 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
446 f
= (float)ReadDouble();
450 // ---------------------------------------------------------------------------
451 // wxDataOutputStream
452 // ---------------------------------------------------------------------------
455 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
456 : m_output(&s
), m_be_order(false), m_conv(conv
.Clone())
458 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
459 : m_output(&s
), m_be_order(false)
464 wxDataOutputStream::~wxDataOutputStream()
468 #endif // wxUSE_UNICODE
472 void wxDataOutputStream::Write64(wxUint64 i
)
477 void wxDataOutputStream::Write64(wxInt64 i
)
481 #endif // wxHAS_INT64
483 void wxDataOutputStream::Write32(wxUint32 i
)
488 i32
= wxUINT32_SWAP_ON_LE(i
);
490 i32
= wxUINT32_SWAP_ON_BE(i
);
491 m_output
->Write(&i32
, 4);
494 void wxDataOutputStream::Write16(wxUint16 i
)
499 i16
= wxUINT16_SWAP_ON_LE(i
);
501 i16
= wxUINT16_SWAP_ON_BE(i
);
503 m_output
->Write(&i16
, 2);
506 void wxDataOutputStream::Write8(wxUint8 i
)
508 m_output
->Write(&i
, 1);
511 void wxDataOutputStream::WriteString(const wxString
& string
)
514 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
516 const wxWX2MBbuf buf
= string
.mb_str();
518 size_t len
= strlen(buf
);
521 m_output
->Write(buf
, len
);
524 void wxDataOutputStream::WriteDouble(double d
)
529 ConvertToIeeeExtended(d
, (wxInt8
*)buf
);
531 #if !defined(__VMS__) && !defined(__GNUG__)
532 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
536 m_output
->Write(buf
, 10);
540 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
543 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
545 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
549 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
552 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
554 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
557 #endif // wxHAS_INT64
559 #if defined(wxLongLong_t) && wxUSE_LONGLONG
560 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
562 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
565 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
567 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
569 #endif // wxLongLong_t
572 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
574 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
577 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
579 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
582 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
587 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
591 #endif // wxUSE_LONGLONG
593 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
597 for (wxUint32 i
=0; i
<size
;i
++)
599 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
601 m_output
->Write(&i32
, 4);
606 for (wxUint32 i
=0; i
<size
;i
++)
608 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
610 m_output
->Write(&i32
, 4);
615 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
619 for (wxUint32 i
=0; i
<size
;i
++)
621 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
623 m_output
->Write(&i16
, 2);
628 for (wxUint32 i
=0; i
<size
;i
++)
630 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
632 m_output
->Write(&i16
, 2);
637 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
639 m_output
->Write(buffer
, size
);
642 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
644 for (wxUint32 i
=0; i
<size
; i
++)
646 WriteDouble(*(buffer
++));
650 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxChar
*string
)
652 Write32(wxStrlen(string
));
653 m_output
->Write((const char *)string
, wxStrlen(string
)*sizeof(wxChar
));
657 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
663 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
669 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
671 Write16((wxUint16
)i
);
675 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
677 Write32((wxUint32
)i
);
681 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
687 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
693 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
700 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
706 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
711 #endif // wxHAS_INT64
713 #if defined(wxLongLong_t) && wxUSE_LONGLONG
714 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
720 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
725 #endif // wxLongLong_t
727 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
733 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
735 WriteDouble((double)f
);