From 82da0238a8171cf8bba6bb1c82d5abba207a10aa Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 31 Mar 2012 17:34:11 +0000 Subject: Gats for QT is here. It's a pretty basic conversion right now, it doesn't support debugging formatting (Bu::sio), it doesn't support gats-text string parsing, and the exceptions need to be fixed to be real exceptions. The basic functions have been renamed to match the Qt API and we use QT types for everything (QHash, QList, QByteArray). It needs more testing, but it's a great start. --- c++-qt/src/float.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 c++-qt/src/float.cpp (limited to 'c++-qt/src/float.cpp') diff --git a/c++-qt/src/float.cpp b/c++-qt/src/float.cpp new file mode 100644 index 0000000..a82edec --- /dev/null +++ b/c++-qt/src/float.cpp @@ -0,0 +1,122 @@ +#include "gats-qt/float.h" +#include "gats-qt/integer.h" + +#include + +Gats::Float::Float() : + fVal( 0.0 ) +{ +} + +Gats::Float::Float( double f ) : + fVal( f ) +{ +} + +Gats::Float::~Float() +{ +} + +void Gats::Float::write( QIODevice &rOut ) const +{ + if( fVal == 0.0 ) + { + if( signbit( fVal ) ) + rOut.write("FZ", 2 ); + else + rOut.write("Fz", 2 ); + } + else if( !isfinite( fVal ) ) + { + if( isnan( fVal ) ) + { + if( signbit( fVal ) ) + rOut.write("FN", 2 ); + else + rOut.write("Fn", 2 ); + } + else + { + if( signbit( fVal ) ) + rOut.write("FI", 2 ); + else + rOut.write("Fi", 2 ); + } + } + else + { + rOut.write("f", 1 ); + double d = fVal; + bool bNeg = false; + int64_t iScale=0; + if( signbit( d ) ) + { + bNeg = true; + d = -d; + } + + iScale = log( d ) / 0x1.62e42fefa39efp+2; // log( 256.0 ) + if( iScale < 0 ) iScale--; + d /= pow( 256.0, iScale ); + + QByteArray s; + s += (uint8_t)(d); + d -= (int)d; + for( int j = 0; j < 150 && d; j++ ) + { + d = d*256.0; + s += (uint8_t)d; + d -= (int)d; + } + Gats::Integer::writePackedInt( rOut, bNeg?-s.size():s.size() ); + rOut.write( s.constData(), s.size() ); + Gats::Integer::writePackedInt( rOut, iScale ); + } +} + +void Gats::Float::read( QIODevice &rIn, char cType ) +{ + if( cType == 'F' ) + { + char buf; + rIn.read( &buf, 1 ); + switch( buf ) + { + case 'N': fVal = -NAN; break; + case 'n': fVal = NAN; break; + case 'I': fVal = -INFINITY; break; + case 'i': fVal = INFINITY; break; + case 'Z': fVal = -0.0; break; + case 'z': fVal = 0.0; break; + } + } + else if( cType == 'f' ) + { + int64_t iStr; + Gats::Integer::readPackedInt( rIn, iStr ); + bool bNeg = false; + if( iStr < 0 ) + { + bNeg = true; + iStr = -iStr; + } + QByteArray s( iStr, '\0' ); + rIn.read( s.data(), iStr ); + fVal = 0.0; + for( int j = iStr-1; j > 0; j-- ) + { + fVal = (fVal+(uint8_t)s[j])*0x1p-8; + } + fVal += (uint8_t)s[0]; + int64_t iScale; + Gats::Integer::readPackedInt( rIn, iScale ); + fVal *= pow( 256.0, iScale ); + if( bNeg ) fVal = -fVal; + } +} +/* +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Float &flt ) +{ + return f << "(float) " << flt.getValue(); +}*/ + -- cgit v1.2.3