]>
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
);
120 m_input
->Read(tmp
.data(), len
);
121 ret
= m_conv
->cMB2WX(tmp
.data());
124 wxStringBuffer
buf(ret
, len
);
126 m_input
->Read(buf
, len
);
137 void DoReadLL(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
140 unsigned char *pchBuffer
= new unsigned char[size
* 8];
141 // TODO: Check for overflow when size is of type uint and is > than 512m
142 input
->Read(pchBuffer
, size
* 8);
146 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
148 buffer
[uiIndex
] = 0l;
149 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
151 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
152 DataType((unsigned long) pchBuffer
[idx_base
+ ui
]);
158 else // little endian
160 for ( size_t uiIndex
=0; uiIndex
!=size
; ++uiIndex
)
162 buffer
[uiIndex
] = 0l;
163 for ( unsigned ui
=0; ui
!=8; ++ui
)
164 buffer
[uiIndex
] = buffer
[uiIndex
] * 256l +
165 DataType((unsigned long) pchBuffer
[idx_base
+ 7 - ui
]);
173 static void DoWriteLL(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
176 unsigned char *pchBuffer
= new unsigned char[size
* 8];
180 for ( size_t uiIndex
= 0; uiIndex
!= size
; ++uiIndex
)
182 DataType i64
= buffer
[uiIndex
];
183 for ( unsigned ui
= 0; ui
!= 8; ++ui
)
185 pchBuffer
[idx_base
+ 7 - ui
] =
186 (unsigned char) (i64
.GetLo() & 255l);
193 else // little endian
195 for ( size_t uiIndex
=0; uiIndex
!= size
; ++uiIndex
)
197 DataType i64
= buffer
[uiIndex
];
198 for (unsigned ui
=0; ui
!=8; ++ui
)
200 pchBuffer
[idx_base
+ ui
] =
201 (unsigned char) (i64
.GetLo() & 255l);
209 // TODO: Check for overflow when size is of type uint and is > than 512m
210 output
->Write(pchBuffer
, size
* 8);
214 #endif // wxUSE_LONGLONG
220 void DoReadI64(T
*buffer
, size_t size
, wxInputStream
*input
, bool be_order
)
223 unsigned char *pchBuffer
= (unsigned char*) buffer
;
224 // TODO: Check for overflow when size is of type uint and is > than 512m
225 input
->Read(pchBuffer
, size
* 8);
228 for ( wxUint32 i
= 0; i
< size
; i
++ )
230 DataType v
= wxUINT64_SWAP_ON_LE(*buffer
);
234 else // little endian
236 for ( wxUint32 i
=0; i
<size
; i
++ )
238 DataType v
= wxUINT64_SWAP_ON_BE(*buffer
);
246 void DoWriteI64(const T
*buffer
, size_t size
, wxOutputStream
*output
, bool be_order
)
251 for ( size_t i
= 0; i
< size
; i
++ )
253 DataType i64
= wxUINT64_SWAP_ON_LE(*buffer
);
255 output
->Write(&i64
, 8);
258 else // little endian
260 for ( size_t i
=0; i
< size
; i
++ )
262 DataType i64
= wxUINT64_SWAP_ON_BE(*buffer
);
264 output
->Write(&i64
, 8);
269 #endif // wxLongLong_t
273 void wxDataInputStream::Read64(wxUint64
*buffer
, size_t size
)
276 DoReadLL(buffer
, size
, m_input
, m_be_order
);
278 DoReadI64(buffer
, size
, m_input
, m_be_order
);
282 void wxDataInputStream::Read64(wxInt64
*buffer
, size_t size
)
285 DoReadLL(buffer
, size
, m_input
, m_be_order
);
287 DoReadI64(buffer
, size
, m_input
, m_be_order
);
290 #endif // wxHAS_INT64
292 #if defined(wxLongLong_t) && wxUSE_LONGLONG
293 void wxDataInputStream::Read64(wxULongLong
*buffer
, size_t size
)
295 DoReadLL(buffer
, size
, m_input
, m_be_order
);
298 void wxDataInputStream::Read64(wxLongLong
*buffer
, size_t size
)
300 DoReadLL(buffer
, size
, m_input
, m_be_order
);
302 #endif // wxLongLong_t
305 void wxDataInputStream::ReadLL(wxULongLong
*buffer
, size_t size
)
307 DoReadLL(buffer
, size
, m_input
, m_be_order
);
310 void wxDataInputStream::ReadLL(wxLongLong
*buffer
, size_t size
)
312 DoReadLL(buffer
, size
, m_input
, m_be_order
);
315 wxLongLong
wxDataInputStream::ReadLL(void)
318 DoReadLL(&ll
, (size_t)1, m_input
, m_be_order
);
321 #endif // wxUSE_LONGLONG
323 void wxDataInputStream::Read32(wxUint32
*buffer
, size_t size
)
325 m_input
->Read(buffer
, size
* 4);
329 for (wxUint32 i
=0; i
<size
; i
++)
331 wxUint32 v
= wxUINT32_SWAP_ON_LE(*buffer
);
337 for (wxUint32 i
=0; i
<size
; i
++)
339 wxUint32 v
= wxUINT32_SWAP_ON_BE(*buffer
);
345 void wxDataInputStream::Read16(wxUint16
*buffer
, size_t size
)
347 m_input
->Read(buffer
, size
* 2);
351 for (wxUint32 i
=0; i
<size
; i
++)
353 wxUint16 v
= wxUINT16_SWAP_ON_LE(*buffer
);
359 for (wxUint32 i
=0; i
<size
; i
++)
361 wxUint16 v
= wxUINT16_SWAP_ON_BE(*buffer
);
367 void wxDataInputStream::Read8(wxUint8
*buffer
, size_t size
)
369 m_input
->Read(buffer
, size
);
372 void wxDataInputStream::ReadDouble(double *buffer
, size_t size
)
374 for (wxUint32 i
=0; i
<size
; i
++)
376 *(buffer
++) = ReadDouble();
380 wxDataInputStream
& wxDataInputStream::operator>>(wxString
& s
)
386 wxDataInputStream
& wxDataInputStream::operator>>(wxInt8
& c
)
392 wxDataInputStream
& wxDataInputStream::operator>>(wxInt16
& i
)
394 i
= (wxInt16
)Read16();
398 wxDataInputStream
& wxDataInputStream::operator>>(wxInt32
& i
)
400 i
= (wxInt32
)Read32();
404 wxDataInputStream
& wxDataInputStream::operator>>(wxUint8
& c
)
410 wxDataInputStream
& wxDataInputStream::operator>>(wxUint16
& i
)
416 wxDataInputStream
& wxDataInputStream::operator>>(wxUint32
& i
)
423 wxDataInputStream
& wxDataInputStream::operator>>(wxUint64
& i
)
429 wxDataInputStream
& wxDataInputStream::operator>>(wxInt64
& i
)
434 #endif // wxHAS_INT64
436 #if defined(wxLongLong_t) && wxUSE_LONGLONG
437 wxDataInputStream
& wxDataInputStream::operator>>(wxULongLong
& i
)
443 wxDataInputStream
& wxDataInputStream::operator>>(wxLongLong
& i
)
448 #endif // wxLongLong_t
450 wxDataInputStream
& wxDataInputStream::operator>>(double& i
)
456 wxDataInputStream
& wxDataInputStream::operator>>(float& f
)
458 f
= (float)ReadDouble();
462 // ---------------------------------------------------------------------------
463 // wxDataOutputStream
464 // ---------------------------------------------------------------------------
467 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
, const wxMBConv
& conv
)
468 : m_output(&s
), m_be_order(false), m_conv(conv
.Clone())
470 wxDataOutputStream::wxDataOutputStream(wxOutputStream
& s
)
471 : m_output(&s
), m_be_order(false)
476 wxDataOutputStream::~wxDataOutputStream()
480 #endif // wxUSE_UNICODE
484 void wxDataOutputStream::SetConv( const wxMBConv
&conv
)
487 m_conv
= conv
.Clone();
492 void wxDataOutputStream::Write64(wxUint64 i
)
497 void wxDataOutputStream::Write64(wxInt64 i
)
501 #endif // wxHAS_INT64
503 void wxDataOutputStream::Write32(wxUint32 i
)
508 i32
= wxUINT32_SWAP_ON_LE(i
);
510 i32
= wxUINT32_SWAP_ON_BE(i
);
511 m_output
->Write(&i32
, 4);
514 void wxDataOutputStream::Write16(wxUint16 i
)
519 i16
= wxUINT16_SWAP_ON_LE(i
);
521 i16
= wxUINT16_SWAP_ON_BE(i
);
523 m_output
->Write(&i16
, 2);
526 void wxDataOutputStream::Write8(wxUint8 i
)
528 m_output
->Write(&i
, 1);
531 void wxDataOutputStream::WriteString(const wxString
& string
)
534 const wxWX2MBbuf buf
= string
.mb_str(*m_conv
);
536 const wxWX2MBbuf buf
= string
.mb_str();
538 size_t len
= strlen(buf
);
541 m_output
->Write(buf
, len
);
544 void wxDataOutputStream::WriteDouble(double d
)
549 wxConvertToIeeeExtended(d
, (wxInt8
*)buf
);
552 #if !defined(__VMS__) && !defined(__GNUG__)
554 # pragma message("wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!")
556 # pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
561 m_output
->Write(buf
, 10);
565 void wxDataOutputStream::Write64(const wxUint64
*buffer
, size_t size
)
568 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
570 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
574 void wxDataOutputStream::Write64(const wxInt64
*buffer
, size_t size
)
577 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
579 DoWriteI64(buffer
, size
, m_output
, m_be_order
);
582 #endif // wxHAS_INT64
584 #if defined(wxLongLong_t) && wxUSE_LONGLONG
585 void wxDataOutputStream::Write64(const wxULongLong
*buffer
, size_t size
)
587 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
590 void wxDataOutputStream::Write64(const wxLongLong
*buffer
, size_t size
)
592 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
594 #endif // wxLongLong_t
597 void wxDataOutputStream::WriteLL(const wxULongLong
*buffer
, size_t size
)
599 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
602 void wxDataOutputStream::WriteLL(const wxLongLong
*buffer
, size_t size
)
604 DoWriteLL(buffer
, size
, m_output
, m_be_order
);
607 void wxDataOutputStream::WriteLL(const wxLongLong
&ll
)
612 void wxDataOutputStream::WriteLL(const wxULongLong
&ll
)
616 #endif // wxUSE_LONGLONG
618 void wxDataOutputStream::Write32(const wxUint32
*buffer
, size_t size
)
622 for (wxUint32 i
=0; i
<size
;i
++)
624 wxUint32 i32
= wxUINT32_SWAP_ON_LE(*buffer
);
626 m_output
->Write(&i32
, 4);
631 for (wxUint32 i
=0; i
<size
;i
++)
633 wxUint32 i32
= wxUINT32_SWAP_ON_BE(*buffer
);
635 m_output
->Write(&i32
, 4);
640 void wxDataOutputStream::Write16(const wxUint16
*buffer
, size_t size
)
644 for (wxUint32 i
=0; i
<size
;i
++)
646 wxUint16 i16
= wxUINT16_SWAP_ON_LE(*buffer
);
648 m_output
->Write(&i16
, 2);
653 for (wxUint32 i
=0; i
<size
;i
++)
655 wxUint16 i16
= wxUINT16_SWAP_ON_BE(*buffer
);
657 m_output
->Write(&i16
, 2);
662 void wxDataOutputStream::Write8(const wxUint8
*buffer
, size_t size
)
664 m_output
->Write(buffer
, size
);
667 void wxDataOutputStream::WriteDouble(const double *buffer
, size_t size
)
669 for (wxUint32 i
=0; i
<size
; i
++)
671 WriteDouble(*(buffer
++));
675 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxString
& string
)
681 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt8 c
)
687 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt16 i
)
689 Write16((wxUint16
)i
);
693 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt32 i
)
695 Write32((wxUint32
)i
);
699 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint8 c
)
705 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint16 i
)
711 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint32 i
)
718 wxDataOutputStream
& wxDataOutputStream::operator<<(wxUint64 i
)
724 wxDataOutputStream
& wxDataOutputStream::operator<<(wxInt64 i
)
729 #endif // wxHAS_INT64
731 #if defined(wxLongLong_t) && wxUSE_LONGLONG
732 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxULongLong
&i
)
738 wxDataOutputStream
& wxDataOutputStream::operator<<(const wxLongLong
&i
)
743 #endif // wxLongLong_t
745 wxDataOutputStream
& wxDataOutputStream::operator<<(double f
)
751 wxDataOutputStream
& wxDataOutputStream::operator<<(float f
)
753 WriteDouble((double)f
);