X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/10c2f98a3951e534ac608fb801dd44f795733b82..76015a6bb79156d8af1b5e8b4b8e46b4e192d18f:/src/common/datstrm.cpp diff --git a/src/common/datstrm.cpp b/src/common/datstrm.cpp index 8a97eca767..c55f2134fb 100644 --- a/src/common/datstrm.cpp +++ b/src/common/datstrm.cpp @@ -24,35 +24,70 @@ #include "wx/math.h" #endif //WX_PRECOMP -// --------------------------------------------------------------------------- -// wxDataInputStream -// --------------------------------------------------------------------------- +namespace +{ -#if wxUSE_UNICODE -wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv) - : m_input(&s), m_be_order(false), m_conv(conv.Clone()) -#else -wxDataInputStream::wxDataInputStream(wxInputStream& s) - : m_input(&s), m_be_order(false) -#endif +// helper unions used to swap bytes of floats and doubles +union Float32Data { -} + wxFloat32 f; + wxUint32 i; +}; -wxDataInputStream::~wxDataInputStream() +union Float64Data { + wxFloat64 f; + wxUint32 i[2]; +}; + +} // anonymous namespace + +// ---------------------------------------------------------------------------- +// wxDataStreamBase +// ---------------------------------------------------------------------------- + +wxDataStreamBase::wxDataStreamBase(const wxMBConv& conv) #if wxUSE_UNICODE - delete m_conv; + : m_conv(conv.Clone()) #endif // wxUSE_UNICODE +{ + // It is unused in non-Unicode build, so suppress a warning there. + wxUnusedVar(conv); + + m_be_order = false; + + // For compatibility with the existing data files, we use extended + // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on. +#if wxUSE_APPLE_IEEE + m_useExtendedPrecision = true; +#endif // wxUSE_APPLE_IEEE } #if wxUSE_UNICODE -void wxDataInputStream::SetConv( const wxMBConv &conv ) +void wxDataStreamBase::SetConv( const wxMBConv &conv ) { delete m_conv; m_conv = conv.Clone(); } #endif +wxDataStreamBase::~wxDataStreamBase() +{ +#if wxUSE_UNICODE + delete m_conv; +#endif // wxUSE_UNICODE +} + +// --------------------------------------------------------------------------- +// wxDataInputStream +// --------------------------------------------------------------------------- + +wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv) + : wxDataStreamBase(conv), + m_input(&s) +{ +} + #if wxHAS_INT64 wxUint64 wxDataInputStream::Read64() { @@ -97,13 +132,48 @@ wxUint8 wxDataInputStream::Read8() double wxDataInputStream::ReadDouble() { #if wxUSE_APPLE_IEEE - char buf[10]; + if ( m_useExtendedPrecision ) + { + char buf[10]; - m_input->Read(buf, 10); - return wxConvertFromIeeeExtended((const wxInt8 *)buf); -#else - return 0.0; -#endif + m_input->Read(buf, 10); + return wxConvertFromIeeeExtended((const wxInt8 *)buf); + } + else +#endif // wxUSE_APPLE_IEEE + { + Float64Data floatData; + + if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) ) + { + floatData.i[0] = Read32(); + floatData.i[1] = Read32(); + } + else + { + floatData.i[1] = Read32(); + floatData.i[0] = Read32(); + } + + return static_cast(floatData.f); + } +} + +float wxDataInputStream::ReadFloat() +{ +#if wxUSE_APPLE_IEEE + if ( m_useExtendedPrecision ) + { + return (float)ReadDouble(); + } + else +#endif // wxUSE_APPLE_IEEE + { + Float32Data floatData; + + floatData.i = Read32(); + return static_cast(floatData.f); + } } wxString wxDataInputStream::ReadString() @@ -114,11 +184,10 @@ wxString wxDataInputStream::ReadString() if ( len > 0 ) { #if wxUSE_UNICODE - wxCharBuffer tmp(len + 1); + wxCharBuffer tmp(len); if ( tmp ) { m_input->Read(tmp.data(), len); - tmp.data()[len] = '\0'; ret = m_conv->cMB2WX(tmp.data()); } #else @@ -378,6 +447,14 @@ void wxDataInputStream::ReadDouble(double *buffer, size_t size) } } +void wxDataInputStream::ReadFloat(float *buffer, size_t size) +{ + for (wxUint32 i=0; i>(wxString& s) { s = ReadString(); @@ -448,15 +525,15 @@ wxDataInputStream& wxDataInputStream::operator>>(wxLongLong& i) } #endif // wxLongLong_t -wxDataInputStream& wxDataInputStream::operator>>(double& i) +wxDataInputStream& wxDataInputStream::operator>>(double& d) { - i = ReadDouble(); + d = ReadDouble(); return *this; } wxDataInputStream& wxDataInputStream::operator>>(float& f) { - f = (float)ReadDouble(); + f = ReadFloat(); return *this; } @@ -464,31 +541,12 @@ wxDataInputStream& wxDataInputStream::operator>>(float& f) // wxDataOutputStream // --------------------------------------------------------------------------- -#if wxUSE_UNICODE wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, const wxMBConv& conv) - : m_output(&s), m_be_order(false), m_conv(conv.Clone()) -#else -wxDataOutputStream::wxDataOutputStream(wxOutputStream& s) - : m_output(&s), m_be_order(false) -#endif + : wxDataStreamBase(conv), + m_output(&s) { } -wxDataOutputStream::~wxDataOutputStream() -{ -#if wxUSE_UNICODE - delete m_conv; -#endif // wxUSE_UNICODE -} - -#if wxUSE_UNICODE -void wxDataOutputStream::SetConv( const wxMBConv &conv ) -{ - delete m_conv; - m_conv = conv.Clone(); -} -#endif - #if wxHAS_INT64 void wxDataOutputStream::Write64(wxUint64 i) { @@ -544,17 +602,49 @@ void wxDataOutputStream::WriteString(const wxString& string) void wxDataOutputStream::WriteDouble(double d) { - char buf[10]; +#if wxUSE_APPLE_IEEE + if ( m_useExtendedPrecision ) + { + char buf[10]; + wxConvertToIeeeExtended(d, (wxInt8 *)buf); + m_output->Write(buf, 10); + } + else +#endif // wxUSE_APPLE_IEEE + { + Float64Data floatData; + + floatData.f = (wxFloat64)d; + + if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) ) + { + Write32(floatData.i[0]); + Write32(floatData.i[1]); + } + else + { + Write32(floatData.i[1]); + Write32(floatData.i[0]); + } + } +} + +void wxDataOutputStream::WriteFloat(float f) +{ #if wxUSE_APPLE_IEEE - wxConvertToIeeeExtended(d, (wxInt8 *)buf); -#else -#if !defined(__VMS__) && !defined(__GNUG__) -# pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!" -#endif - buf[0] = '\0'; -#endif - m_output->Write(buf, 10); + if ( m_useExtendedPrecision ) + { + WriteDouble((double)f); + } + else +#endif // wxUSE_APPLE_IEEE + { + Float32Data floatData; + + floatData.f = (wxFloat32)f; + Write32(floatData.i); + } } #if wxHAS_INT64 @@ -668,6 +758,14 @@ void wxDataOutputStream::WriteDouble(const double *buffer, size_t size) } } +void wxDataOutputStream::WriteFloat(const float *buffer, size_t size) +{ + for (wxUint32 i=0; i