]>
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()
105 const size_t len
= Read32();
109 wxCharBuffer
tmp(len
+ 1);
112 m_input
->Read(tmp
.data(), len
);
113 tmp
.data()[len
] = '\0';
114 ret
= m_conv
->cMB2WX(tmp
.data());
117 wxStringBuffer
buf(ret
, len
);
119 m_input
->Read(buf
, len
);
130 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
133 unsigned char *pchBuffer
= new unsigned char[size
* 8];
134 // TODO: Check for overflow when size is of type uint and is > than 512m
135 input
->Read(pchBuffer
, size
* 8);
139 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
141 buffer
[uiIndex
] = 0l;
142 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
144 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
145 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
151 else // little endian
153 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
155 buffer
[uiIndex
] = 0l;
156 for ( unsigned ui
=0; ui
!=8; ++ui
)
157 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
158 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
166 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
169 unsigned char *pchBuffer
= new unsigned char[size
* 8];
173 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
175 DataType i64
= buffer
[uiIndex
];
176 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
178 pchBuffer
[idx_base
+ 7 - ui
] =
179 (unsigned char) (i64
.GetLo() & 255l);
186 else // little endian
188 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
190 DataType i64
= buffer
[uiIndex
];
191 for (unsigned ui
=0; ui
!=8; ++ui
)
193 pchBuffer
[idx_base
+ ui
] =
194 (unsigned char) (i64
.GetLo() & 255l);
202 // TODO: Check for overflow when size is of type uint and is > than 512m
203 output
->Write(pchBuffer
, size
* 8);
207 #endif // wxUSE_LONGLONG
213 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
216 unsigned char *pchBuffer
= (unsigned char*) buffer
;
217 // TODO: Check for overflow when size is of type uint and is > than 512m
218 input
->Read(pchBuffer
, size
* 8);
221 for ( wxUint32 i
= 0; i
< size
; i
++ )
223 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
227 else // little endian
229 for ( wxUint32 i
=0; i
<size
; i
++ )
231 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
239 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
244 for ( size_t i
= 0; i
< size
; i
++ )
246 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
248 output
->Write(&i64
, 8);
251 else // little endian
253 for ( size_t i
=0; i
< size
; i
++ )
255 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
257 output
->Write(&i64
, 8);
262 #endif // wxLongLong_t
266 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
269 DoReadLL(buffer
, size
, m_input
, m_be_order
);
271 DoReadI64(buffer
, size
, m_input
, m_be_order
);
275 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
278 DoReadLL(buffer
, size
, m_input
, m_be_order
);
280 DoReadI64(buffer
, size
, m_input
, m_be_order
);
283 #endif // wxHAS_INT64
285 #if defined(wxLongLong_t) && wxUSE_LONGLONG
286 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
288 DoReadLL(buffer
, size
, m_input
, m_be_order
);
291 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
293 DoReadLL(buffer
, size
, m_input
, m_be_order
);
295 #endif // wxLongLong_t
298 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
300 DoReadLL(buffer
, size
, m_input
, m_be_order
);
303 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
305 DoReadLL(buffer
, size
, m_input
, m_be_order
);
308 wxLongLong
wxDataInputStream::ReadLL(void)
311 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
314 #endif // wxUSE_LONGLONG
316 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
318 m_input
->Read(buffer
, size
* 4);
322 for (wxUint32 i
=0; i
<size
; i
++)
324 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
330 for (wxUint32 i
=0; i
<size
; i
++)
332 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
338 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
340 m_input
->Read(buffer
, size
* 2);
344 for (wxUint32 i
=0; i
<size
; i
++)
346 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
352 for (wxUint32 i
=0; i
<size
; i
++)
354 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
360 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
362 m_input
->Read(buffer
, size
);
365 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
367 for (wxUint32 i
=0; i
<size
; i
++)
369 *(buffer
++) = ReadDouble();
373 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
379 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
385 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
387 i
= (wxInt16
)Read16();
391 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
393 i
= (wxInt32
)Read32();
397 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
403 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
409 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
416 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
422 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
427 #endif // wxHAS_INT64
429 #if defined(wxLongLong_t) && wxUSE_LONGLONG
430 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
436 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
441 #endif // wxLongLong_t
443 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
449 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
451 f
= (float)ReadDouble();
455 // ---------------------------------------------------------------------------
456 // wxDataOutputStream
457 // ---------------------------------------------------------------------------
460 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
461 : m_output(&s
), m_be_order(false), m_conv(conv
.Clone())
463 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
464 : m_output(&s
), m_be_order(false)
469 wxDataOutputStream::~wxDataOutputStream()
473 #endif // wxUSE_UNICODE
477 void wxDataOutputStream::Write64(wxUint64 i
)
482 void wxDataOutputStream::Write64(wxInt64 i
)
486 #endif // wxHAS_INT64
488 void wxDataOutputStream::Write32(wxUint32 i
)
493 i32
= wxUINT32_SWAP_ON_LE(i
);
495 i32
= wxUINT32_SWAP_ON_BE(i
);
496 m_output
->Write(&i32
, 4);
499 void wxDataOutputStream::Write16(wxUint16 i
)
504 i16
= wxUINT16_SWAP_ON_LE(i
);
506 i16
= wxUINT16_SWAP_ON_BE(i
);
508 m_output
->Write(&i16
, 2);
511 void wxDataOutputStream::Write8(wxUint8 i
)
513 m_output
->Write(&i
, 1);
516 void wxDataOutputStream::WriteString(const wxString
& string
)
519 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
521 const wxWX2MBbuf buf
= string
.mb_str();
523 size_t len
= strlen(buf
);
526 m_output
->Write(buf
, len
);
529 void wxDataOutputStream::WriteDouble(double d
)
534 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
536 #if !defined(__VMS__) && !defined(__GNUG__)
537 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
541 m_output
->Write(buf
, 10);
545 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
548 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
550 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
554 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
557 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
559 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
562 #endif // wxHAS_INT64
564 #if defined(wxLongLong_t) && wxUSE_LONGLONG
565 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
567 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
570 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
572 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
574 #endif // wxLongLong_t
577 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
579 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
582 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
584 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
587 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
592 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
596 #endif // wxUSE_LONGLONG
598 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
602 for (wxUint32 i
=0; i
<size
;i
++)
604 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
606 m_output
->Write(&i32
, 4);
611 for (wxUint32 i
=0; i
<size
;i
++)
613 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
615 m_output
->Write(&i32
, 4);
620 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
624 for (wxUint32 i
=0; i
<size
;i
++)
626 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
628 m_output
->Write(&i16
, 2);
633 for (wxUint32 i
=0; i
<size
;i
++)
635 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
637 m_output
->Write(&i16
, 2);
642 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
644 m_output
->Write(buffer
, size
);
647 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
649 for (wxUint32 i
=0; i
<size
; i
++)
651 WriteDouble(*(buffer
++));
655 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
661 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
667 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
669 Write16((wxUint16
)i
);
673 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
675 Write32((wxUint32
)i
);
679 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
685 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
691 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
698 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
704 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
709 #endif // wxHAS_INT64
711 #if defined(wxLongLong_t) && wxUSE_LONGLONG
712 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
718 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
723 #endif // wxLongLong_t
725 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
731 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
733 WriteDouble((double)f
);