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/gatsstream.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 c++-qt/src/gatsstream.cpp (limited to 'c++-qt/src/gatsstream.cpp') diff --git a/c++-qt/src/gatsstream.cpp b/c++-qt/src/gatsstream.cpp new file mode 100644 index 0000000..d32b2b2 --- /dev/null +++ b/c++-qt/src/gatsstream.cpp @@ -0,0 +1,116 @@ +#include "gats-qt/gatsstream.h" +#include "gats-qt/object.h" + +#ifdef WIN32 +#include +#else +#include +#endif + +#include + +Gats::GatsStream::GatsStream( QIODevice &rStream ) : + rStream( rStream ) +{ +} + +Gats::GatsStream::~GatsStream() +{ +} + +Gats::Object *Gats::GatsStream::readObject() +{ + char buf[1500]; + + do + { + if( qbRead.size() < 5 ) + { + int iRead = rStream.read( buf, 5-qbRead.size() ); + qbRead.append( buf, iRead ); + + if( qbRead.size() < 5 ) + return NULL; + } + } while( !skipReadNulls() ); + + uint8_t uVer; + uVer = qbRead[0]; + + int32_t iSize; + memcpy( &iSize, qbRead.constData()+1, 4 ); + iSize = ntohl( iSize ); + while( qbRead.size() < iSize ) + { + int32_t iRead = iSize - qbRead.size(); + if( iRead > 1500 ) + iRead = 1500; + int32_t iReal = rStream.read( buf, iRead ); + qbRead.append( buf, iReal ); + if( iReal < iRead ) + { + return NULL; + } + } + + if( qbRead.size() < iSize ) + { + return NULL; + } + + QBuffer rTmp( &qbRead ); + rTmp.open( QIODevice::ReadOnly ); + rTmp.seek( 5 ); + Gats::Object *pObj = Gats::Object::read( rTmp ); + + return pObj; +} + +void Gats::GatsStream::writeObject( Gats::Object *pObject ) +{ + QIODevice *pTmp; + if( rStream.isSequential() ) + { + pTmp = new QBuffer(); + pTmp->open( QIODevice::WriteOnly ); + } + else + { + pTmp = &rStream; + } + + uint8_t uBuf = 1; + uint32_t iSize = 0; + pTmp->write( (const char *)&uBuf, 1 ); + uint64_t iSizePos = pTmp->pos(); + pTmp->write( (const char *)&iSize, 4 ); + pObject->write( *pTmp ); + iSize = htonl( pTmp->pos() ); + pTmp->seek( iSizePos ); + pTmp->write( (const char *)&iSize, 4 ); + pTmp->close(); + + if( rStream.isSequential() ) + { + rStream.write( ((QBuffer *)pTmp)->data() ); + delete pTmp; + } +} + +bool Gats::GatsStream::skipReadNulls() +{ + bool bHaveSeeked = false; + for(;;) + { + if( qbRead.size() == 0 ) + return false; + if( qbRead.at(0) != 0 ) + return !bHaveSeeked; //true; + else + { + qbRead.remove( 0, 1 ); + bHaveSeeked = true; + } + } +} + -- cgit v1.2.3