]>
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"
27 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
32 wxDataInputStream::wxDataInputStream(wxInputStream
& s
, const wxMBConv
& conv
)
33 : m_input(&s
), m_be_order(false), m_conv(conv
.Clone())
35 wxDataInputStream::wxDataInputStream(wxInputStream
& s
)
36 : m_input(&s
), m_be_order(false)
41 wxDataInputStream::~wxDataInputStream()
45 #endif // wxUSE_UNICODE
49 wxUint64
wxDataInputStream::Read64()
57 wxUint32
wxDataInputStream::Read32()
61 m_input
->Read(&i32
, 4);
64 return wxUINT32_SWAP_ON_LE(i32
);
66 return wxUINT32_SWAP_ON_BE(i32
);
69 wxUint16
wxDataInputStream::Read16()
73 m_input
->Read(&i16
, 2);
76 return wxUINT16_SWAP_ON_LE(i16
);
78 return wxUINT16_SWAP_ON_BE(i16
);
81 wxUint8
wxDataInputStream::Read8()
85 m_input
->Read(&buf
, 1);
89 double wxDataInputStream::ReadDouble()
94 m_input
->Read(buf
, 10);
95 return wxConvertFromIeeeExtended((const wxInt8
*)buf
);
101 wxString
wxDataInputStream::ReadString()
110 wxCharBuffer
tmp(len
+ 1);
111 m_input
->Read(tmp
.data(), len
);
112 tmp
.data()[len
] = '\0';
113 wxString
ret(m_conv
->cMB2WX(tmp
.data()));
116 m_input
->Read( wxStringBuffer(ret
, len
), len
);
121 return wxEmptyString
;
128 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
131 unsigned char *pchBuffer
= new unsigned char[size
* 8];
132 // TODO: Check for overflow when size is of type uint and is > than 512m
133 input
->Read(pchBuffer
, size
* 8);
137 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
139 buffer
[uiIndex
] = 0l;
140 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
142 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
143 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
149 else // little endian
151 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
153 buffer
[uiIndex
] = 0l;
154 for ( unsigned ui
=0; ui
!=8; ++ui
)
155 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
156 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
164 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
167 unsigned char *pchBuffer
= new unsigned char[size
* 8];
171 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
173 DataType i64
= buffer
[uiIndex
];
174 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
176 pchBuffer
[idx_base
+ 7 - ui
] =
177 (unsigned char) (i64
.GetLo() & 255l);
184 else // little endian
186 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
188 DataType i64
= buffer
[uiIndex
];
189 for (unsigned ui
=0; ui
!=8; ++ui
)
191 pchBuffer
[idx_base
+ ui
] =
192 (unsigned char) (i64
.GetLo() & 255l);
200 // TODO: Check for overflow when size is of type uint and is > than 512m
201 output
->Write(pchBuffer
, size
* 8);
205 #endif // wxUSE_LONGLONG
211 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
214 unsigned char *pchBuffer
= (unsigned char*) buffer
;
215 // TODO: Check for overflow when size is of type uint and is > than 512m
216 input
->Read(pchBuffer
, size
* 8);
219 for ( wxUint32 i
= 0; i
< size
; i
++ )
221 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
225 else // little endian
227 for ( wxUint32 i
=0; i
<size
; i
++ )
229 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
237 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
242 for ( size_t i
= 0; i
< size
; i
++ )
244 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
246 output
->Write(&i64
, 8);
249 else // little endian
251 for ( size_t i
=0; i
< size
; i
++ )
253 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
255 output
->Write(&i64
, 8);
260 #endif // wxLongLong_t
264 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
267 DoReadLL(buffer
, size
, m_input
, m_be_order
);
269 DoReadI64(buffer
, size
, m_input
, m_be_order
);
273 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
276 DoReadLL(buffer
, size
, m_input
, m_be_order
);
278 DoReadI64(buffer
, size
, m_input
, m_be_order
);
281 #endif // wxHAS_INT64
283 #if defined(wxLongLong_t) && wxUSE_LONGLONG
284 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
286 DoReadLL(buffer
, size
, m_input
, m_be_order
);
289 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
291 DoReadLL(buffer
, size
, m_input
, m_be_order
);
293 #endif // wxLongLong_t
296 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
298 DoReadLL(buffer
, size
, m_input
, m_be_order
);
301 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
303 DoReadLL(buffer
, size
, m_input
, m_be_order
);
306 wxLongLong
wxDataInputStream::ReadLL(void)
309 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
312 #endif // wxUSE_LONGLONG
314 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
316 m_input
->Read(buffer
, size
* 4);
320 for (wxUint32 i
=0; i
<size
; i
++)
322 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
328 for (wxUint32 i
=0; i
<size
; i
++)
330 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
336 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
338 m_input
->Read(buffer
, size
* 2);
342 for (wxUint32 i
=0; i
<size
; i
++)
344 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
350 for (wxUint32 i
=0; i
<size
; i
++)
352 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
358 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
360 m_input
->Read(buffer
, size
);
363 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
365 for (wxUint32 i
=0; i
<size
; i
++)
367 *(buffer
++) = ReadDouble();
371 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
377 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
383 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
385 i
= (wxInt16
)Read16();
389 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
391 i
= (wxInt32
)Read32();
395 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
401 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
407 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
414 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
420 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
425 #endif // wxHAS_INT64
427 #if defined(wxLongLong_t) && wxUSE_LONGLONG
428 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
434 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
439 #endif // wxLongLong_t
441 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
447 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
449 f
= (float)ReadDouble();
453 // ---------------------------------------------------------------------------
454 // wxDataOutputStream
455 // ---------------------------------------------------------------------------
458 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
459 : m_output(&s
), m_be_order(false), m_conv(conv
.Clone())
461 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
462 : m_output(&s
), m_be_order(false)
467 wxDataOutputStream::~wxDataOutputStream()
471 #endif // wxUSE_UNICODE
475 void wxDataOutputStream::Write64(wxUint64 i
)
480 void wxDataOutputStream::Write64(wxInt64 i
)
484 #endif // wxHAS_INT64
486 void wxDataOutputStream::Write32(wxUint32 i
)
491 i32
= wxUINT32_SWAP_ON_LE(i
);
493 i32
= wxUINT32_SWAP_ON_BE(i
);
494 m_output
->Write(&i32
, 4);
497 void wxDataOutputStream::Write16(wxUint16 i
)
502 i16
= wxUINT16_SWAP_ON_LE(i
);
504 i16
= wxUINT16_SWAP_ON_BE(i
);
506 m_output
->Write(&i16
, 2);
509 void wxDataOutputStream::Write8(wxUint8 i
)
511 m_output
->Write(&i
, 1);
514 void wxDataOutputStream::WriteString(const wxString
& string
)
517 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
519 const wxWX2MBbuf buf
= string
.mb_str();
521 size_t len
= strlen(buf
);
524 m_output
->Write(buf
, len
);
527 void wxDataOutputStream::WriteDouble(double d
)
532 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
534 #if !defined(__VMS__) && !defined(__GNUG__)
535 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
539 m_output
->Write(buf
, 10);
543 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
546 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
548 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
552 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
555 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
557 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
560 #endif // wxHAS_INT64
562 #if defined(wxLongLong_t) && wxUSE_LONGLONG
563 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
565 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
568 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
570 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
572 #endif // wxLongLong_t
575 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
577 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
580 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
582 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
585 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
590 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
594 #endif // wxUSE_LONGLONG
596 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
600 for (wxUint32 i
=0; i
<size
;i
++)
602 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
604 m_output
->Write(&i32
, 4);
609 for (wxUint32 i
=0; i
<size
;i
++)
611 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
613 m_output
->Write(&i32
, 4);
618 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
622 for (wxUint32 i
=0; i
<size
;i
++)
624 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
626 m_output
->Write(&i16
, 2);
631 for (wxUint32 i
=0; i
<size
;i
++)
633 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
635 m_output
->Write(&i16
, 2);
640 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
642 m_output
->Write(buffer
, size
);
645 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
647 for (wxUint32 i
=0; i
<size
; i
++)
649 WriteDouble(*(buffer
++));
653 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
659 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
665 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
667 Write16((wxUint16
)i
);
671 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
673 Write32((wxUint32
)i
);
677 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
683 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
689 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
696 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
702 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
707 #endif // wxHAS_INT64
709 #if defined(wxLongLong_t) && wxUSE_LONGLONG
710 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
716 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
721 #endif // wxLongLong_t
723 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
729 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
731 WriteDouble((double)f
);