]>
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 void wxDataInputStream::SetConv( const wxMBConv
&conv
)
52 m_conv
= conv
.Clone();
57 wxUint64
wxDataInputStream::Read64()
65 wxUint32
wxDataInputStream::Read32()
69 m_input
->Read(&i32
, 4);
72 return wxUINT32_SWAP_ON_LE(i32
);
74 return wxUINT32_SWAP_ON_BE(i32
);
77 wxUint16
wxDataInputStream::Read16()
81 m_input
->Read(&i16
, 2);
84 return wxUINT16_SWAP_ON_LE(i16
);
86 return wxUINT16_SWAP_ON_BE(i16
);
89 wxUint8
wxDataInputStream::Read8()
93 m_input
->Read(&buf
, 1);
97 double wxDataInputStream::ReadDouble()
102 m_input
->Read(buf
, 10);
103 return wxConvertFromIeeeExtended((const wxInt8
*)buf
);
109 wxString
wxDataInputStream::ReadString()
113 const size_t len
= Read32();
117 wxCharBuffer
tmp(len
+ 1);
120 m_input
->Read(tmp
.data(), len
);
121 tmp
.data()[len
] = '\0';
122 ret
= m_conv
->cMB2WX(tmp
.data());
125 wxStringBuffer
buf(ret
, len
);
127 m_input
->Read(buf
, len
);
138 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
141 unsigned char *pchBuffer
= new unsigned char[size
* 8];
142 // TODO: Check for overflow when size is of type uint and is > than 512m
143 input
->Read(pchBuffer
, size
* 8);
147 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
149 buffer
[uiIndex
] = 0l;
150 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
152 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
153 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
159 else // little endian
161 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
163 buffer
[uiIndex
] = 0l;
164 for ( unsigned ui
=0; ui
!=8; ++ui
)
165 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
166 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
174 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
177 unsigned char *pchBuffer
= new unsigned char[size
* 8];
181 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
183 DataType i64
= buffer
[uiIndex
];
184 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
186 pchBuffer
[idx_base
+ 7 - ui
] =
187 (unsigned char) (i64
.GetLo() & 255l);
194 else // little endian
196 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
198 DataType i64
= buffer
[uiIndex
];
199 for (unsigned ui
=0; ui
!=8; ++ui
)
201 pchBuffer
[idx_base
+ ui
] =
202 (unsigned char) (i64
.GetLo() & 255l);
210 // TODO: Check for overflow when size is of type uint and is > than 512m
211 output
->Write(pchBuffer
, size
* 8);
215 #endif // wxUSE_LONGLONG
221 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
224 unsigned char *pchBuffer
= (unsigned char*) buffer
;
225 // TODO: Check for overflow when size is of type uint and is > than 512m
226 input
->Read(pchBuffer
, size
* 8);
229 for ( wxUint32 i
= 0; i
< size
; i
++ )
231 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
235 else // little endian
237 for ( wxUint32 i
=0; i
<size
; i
++ )
239 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
247 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
252 for ( size_t i
= 0; i
< size
; i
++ )
254 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
256 output
->Write(&i64
, 8);
259 else // little endian
261 for ( size_t i
=0; i
< size
; i
++ )
263 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
265 output
->Write(&i64
, 8);
270 #endif // wxLongLong_t
274 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
277 DoReadLL(buffer
, size
, m_input
, m_be_order
);
279 DoReadI64(buffer
, size
, m_input
, m_be_order
);
283 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
286 DoReadLL(buffer
, size
, m_input
, m_be_order
);
288 DoReadI64(buffer
, size
, m_input
, m_be_order
);
291 #endif // wxHAS_INT64
293 #if defined(wxLongLong_t) && wxUSE_LONGLONG
294 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
296 DoReadLL(buffer
, size
, m_input
, m_be_order
);
299 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
301 DoReadLL(buffer
, size
, m_input
, m_be_order
);
303 #endif // wxLongLong_t
306 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
308 DoReadLL(buffer
, size
, m_input
, m_be_order
);
311 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
313 DoReadLL(buffer
, size
, m_input
, m_be_order
);
316 wxLongLong
wxDataInputStream::ReadLL(void)
319 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
322 #endif // wxUSE_LONGLONG
324 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
326 m_input
->Read(buffer
, size
* 4);
330 for (wxUint32 i
=0; i
<size
; i
++)
332 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
338 for (wxUint32 i
=0; i
<size
; i
++)
340 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
346 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
348 m_input
->Read(buffer
, size
* 2);
352 for (wxUint32 i
=0; i
<size
; i
++)
354 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
360 for (wxUint32 i
=0; i
<size
; i
++)
362 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
368 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
370 m_input
->Read(buffer
, size
);
373 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
375 for (wxUint32 i
=0; i
<size
; i
++)
377 *(buffer
++) = ReadDouble();
381 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
387 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
393 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
395 i
= (wxInt16
)Read16();
399 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
401 i
= (wxInt32
)Read32();
405 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
411 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
417 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
424 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
430 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
435 #endif // wxHAS_INT64
437 #if defined(wxLongLong_t) && wxUSE_LONGLONG
438 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
444 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
449 #endif // wxLongLong_t
451 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
457 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
459 f
= (float)ReadDouble();
463 // ---------------------------------------------------------------------------
464 // wxDataOutputStream
465 // ---------------------------------------------------------------------------
468 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
469 : m_output(&s
), m_be_order(false), m_conv(conv
.Clone())
471 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
472 : m_output(&s
), m_be_order(false)
477 wxDataOutputStream::~wxDataOutputStream()
481 #endif // wxUSE_UNICODE
485 void wxDataOutputStream::SetConv( const wxMBConv
&conv
)
488 m_conv
= conv
.Clone();
493 void wxDataOutputStream::Write64(wxUint64 i
)
498 void wxDataOutputStream::Write64(wxInt64 i
)
502 #endif // wxHAS_INT64
504 void wxDataOutputStream::Write32(wxUint32 i
)
509 i32
= wxUINT32_SWAP_ON_LE(i
);
511 i32
= wxUINT32_SWAP_ON_BE(i
);
512 m_output
->Write(&i32
, 4);
515 void wxDataOutputStream::Write16(wxUint16 i
)
520 i16
= wxUINT16_SWAP_ON_LE(i
);
522 i16
= wxUINT16_SWAP_ON_BE(i
);
524 m_output
->Write(&i16
, 2);
527 void wxDataOutputStream::Write8(wxUint8 i
)
529 m_output
->Write(&i
, 1);
532 void wxDataOutputStream::WriteString(const wxString
& string
)
535 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
537 const wxWX2MBbuf buf
= string
.mb_str();
539 size_t len
= strlen(buf
);
542 m_output
->Write(buf
, len
);
545 void wxDataOutputStream::WriteDouble(double d
)
550 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
552 #if !defined(__VMS__) && !defined(__GNUG__)
553 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
557 m_output
->Write(buf
, 10);
561 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
564 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
566 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
570 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
573 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
575 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
578 #endif // wxHAS_INT64
580 #if defined(wxLongLong_t) && wxUSE_LONGLONG
581 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
583 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
586 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
588 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
590 #endif // wxLongLong_t
593 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
595 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
598 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
600 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
603 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
608 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
612 #endif // wxUSE_LONGLONG
614 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
618 for (wxUint32 i
=0; i
<size
;i
++)
620 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
622 m_output
->Write(&i32
, 4);
627 for (wxUint32 i
=0; i
<size
;i
++)
629 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
631 m_output
->Write(&i32
, 4);
636 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
640 for (wxUint32 i
=0; i
<size
;i
++)
642 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
644 m_output
->Write(&i16
, 2);
649 for (wxUint32 i
=0; i
<size
;i
++)
651 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
653 m_output
->Write(&i16
, 2);
658 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
660 m_output
->Write(buffer
, size
);
663 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
665 for (wxUint32 i
=0; i
<size
; i
++)
667 WriteDouble(*(buffer
++));
671 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
677 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
683 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
685 Write16((wxUint16
)i
);
689 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
691 Write32((wxUint32
)i
);
695 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
701 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
707 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
714 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
720 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
725 #endif // wxHAS_INT64
727 #if defined(wxLongLong_t) && wxUSE_LONGLONG
728 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
734 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
739 #endif // wxLongLong_t
741 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
747 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
749 WriteDouble((double)f
);