diff options
Diffstat (limited to '')
| -rw-r--r-- | c++-qt/src/gatsstream.cpp | 195 |
1 files changed, 101 insertions, 94 deletions
diff --git a/c++-qt/src/gatsstream.cpp b/c++-qt/src/gatsstream.cpp index 920b2d6..f49fdb5 100644 --- a/c++-qt/src/gatsstream.cpp +++ b/c++-qt/src/gatsstream.cpp | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
| 3 | * | ||
| 4 | * This file is part of the libgats library and is released under the | ||
| 5 | * terms of the license contained in the file LICENSE. | ||
| 6 | */ | ||
| 7 | |||
| 1 | #include "gats-qt/gatsstream.h" | 8 | #include "gats-qt/gatsstream.h" |
| 2 | #include "gats-qt/object.h" | 9 | #include "gats-qt/object.h" |
| 3 | 10 | ||
| @@ -10,7 +17,7 @@ | |||
| 10 | #include <QBuffer> | 17 | #include <QBuffer> |
| 11 | 18 | ||
| 12 | Gats::GatsStream::GatsStream( QIODevice &rStream ) : | 19 | Gats::GatsStream::GatsStream( QIODevice &rStream ) : |
| 13 | rStream( rStream ) | 20 | rStream( rStream ) |
| 14 | { | 21 | { |
| 15 | } | 22 | } |
| 16 | 23 | ||
| @@ -20,110 +27,110 @@ Gats::GatsStream::~GatsStream() | |||
| 20 | 27 | ||
| 21 | Gats::Object *Gats::GatsStream::readObject() | 28 | Gats::Object *Gats::GatsStream::readObject() |
| 22 | { | 29 | { |
| 23 | char buf[1500]; | 30 | char buf[1500]; |
| 24 | 31 | ||
| 25 | do | 32 | do |
| 26 | { | 33 | { |
| 27 | if( qbRead.size() < 5 ) | 34 | if( qbRead.size() < 5 ) |
| 28 | { | 35 | { |
| 29 | int iRead = rStream.read( buf, 5-qbRead.size() ); | 36 | int iRead = rStream.read( buf, 5-qbRead.size() ); |
| 30 | qbRead.append( buf, iRead ); | 37 | qbRead.append( buf, iRead ); |
| 31 | 38 | ||
| 32 | if( qbRead.size() < 5 ) | 39 | if( qbRead.size() < 5 ) |
| 33 | return NULL; | 40 | return NULL; |
| 34 | } | 41 | } |
| 35 | } while( !skipReadNulls() ); | 42 | } while( !skipReadNulls() ); |
| 36 | 43 | ||
| 37 | uint8_t uVer; | 44 | uint8_t uVer; |
| 38 | uVer = qbRead[0]; | 45 | uVer = qbRead[0]; |
| 39 | 46 | ||
| 40 | int32_t iSize; | 47 | int32_t iSize; |
| 41 | memcpy( &iSize, qbRead.constData()+1, 4 ); | 48 | memcpy( &iSize, qbRead.constData()+1, 4 ); |
| 42 | iSize = ntohl( iSize ); | 49 | iSize = ntohl( iSize ); |
| 43 | while( qbRead.size() < iSize ) | 50 | while( qbRead.size() < iSize ) |
| 44 | { | 51 | { |
| 45 | int32_t iRead = iSize - qbRead.size(); | 52 | int32_t iRead = iSize - qbRead.size(); |
| 46 | if( iRead > 1500 ) | 53 | if( iRead > 1500 ) |
| 47 | iRead = 1500; | 54 | iRead = 1500; |
| 48 | int32_t iReal = rStream.read( buf, iRead ); | 55 | int32_t iReal = rStream.read( buf, iRead ); |
| 49 | qbRead.append( buf, iReal ); | 56 | qbRead.append( buf, iReal ); |
| 50 | if( iReal < iRead ) | 57 | if( iReal < iRead ) |
| 51 | { | 58 | { |
| 52 | return NULL; | 59 | return NULL; |
| 53 | } | 60 | } |
| 54 | } | 61 | } |
| 55 | 62 | ||
| 56 | if( qbRead.size() < iSize ) | 63 | if( qbRead.size() < iSize ) |
| 57 | { | 64 | { |
| 58 | return NULL; | 65 | return NULL; |
| 59 | } | 66 | } |
| 60 | 67 | ||
| 61 | QBuffer rTmp( &qbRead ); | 68 | QBuffer rTmp( &qbRead ); |
| 62 | rTmp.open( QIODevice::ReadOnly ); | 69 | rTmp.open( QIODevice::ReadOnly ); |
| 63 | rTmp.seek( 5 ); | 70 | rTmp.seek( 5 ); |
| 64 | Gats::Object *pObj = Gats::Object::read( rTmp ); | 71 | Gats::Object *pObj = Gats::Object::read( rTmp ); |
| 65 | qbRead.clear(); | 72 | qbRead.clear(); |
| 66 | 73 | ||
| 67 | emit objectRead( pObj ); | 74 | emit objectRead( pObj ); |
| 68 | 75 | ||
| 69 | return pObj; | 76 | return pObj; |
| 70 | } | 77 | } |
| 71 | 78 | ||
| 72 | void Gats::GatsStream::readAllObjects() | 79 | void Gats::GatsStream::readAllObjects() |
| 73 | { | 80 | { |
| 74 | while( readObject() ) { } | 81 | while( readObject() ) { } |
| 75 | } | 82 | } |
| 76 | 83 | ||
| 77 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) | 84 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) |
| 78 | { | 85 | { |
| 79 | QIODevice *pTmp; | 86 | QIODevice *pTmp; |
| 80 | if( rStream.isSequential() ) | 87 | if( rStream.isSequential() ) |
| 81 | { | 88 | { |
| 82 | pTmp = new QBuffer(); | 89 | pTmp = new QBuffer(); |
| 83 | pTmp->open( QIODevice::WriteOnly ); | 90 | pTmp->open( QIODevice::WriteOnly ); |
| 84 | } | 91 | } |
| 85 | else | 92 | else |
| 86 | { | 93 | { |
| 87 | pTmp = &rStream; | 94 | pTmp = &rStream; |
| 88 | } | 95 | } |
| 89 | 96 | ||
| 90 | uint8_t uBuf = 1; | 97 | uint8_t uBuf = 1; |
| 91 | uint32_t iSize = 0; | 98 | uint32_t iSize = 0; |
| 92 | pTmp->write( (const char *)&uBuf, 1 ); | 99 | pTmp->write( (const char *)&uBuf, 1 ); |
| 93 | uint64_t iSizePos = pTmp->pos(); | 100 | uint64_t iSizePos = pTmp->pos(); |
| 94 | pTmp->write( (const char *)&iSize, 4 ); | 101 | pTmp->write( (const char *)&iSize, 4 ); |
| 95 | pObject->write( *pTmp ); | 102 | pObject->write( *pTmp ); |
| 96 | iSize = htonl( pTmp->pos() ); | 103 | iSize = htonl( pTmp->pos() ); |
| 97 | uint64_t iEndPos = pTmp->pos(); | 104 | uint64_t iEndPos = pTmp->pos(); |
| 98 | pTmp->seek( iSizePos ); | 105 | pTmp->seek( iSizePos ); |
| 99 | pTmp->write( (const char *)&iSize, 4 ); | 106 | pTmp->write( (const char *)&iSize, 4 ); |
| 100 | 107 | ||
| 101 | if( rStream.isSequential() ) | 108 | if( rStream.isSequential() ) |
| 102 | { | 109 | { |
| 103 | pTmp->close(); | 110 | pTmp->close(); |
| 104 | rStream.write( ((QBuffer *)pTmp)->data() ); | 111 | rStream.write( ((QBuffer *)pTmp)->data() ); |
| 105 | delete pTmp; | 112 | delete pTmp; |
| 106 | } | 113 | } |
| 107 | else | 114 | else |
| 108 | { | 115 | { |
| 109 | pTmp->seek( iSizePos ); | 116 | pTmp->seek( iSizePos ); |
| 110 | } | 117 | } |
| 111 | } | 118 | } |
| 112 | 119 | ||
| 113 | bool Gats::GatsStream::skipReadNulls() | 120 | bool Gats::GatsStream::skipReadNulls() |
| 114 | { | 121 | { |
| 115 | bool bHaveSeeked = false; | 122 | bool bHaveSeeked = false; |
| 116 | for(;;) | 123 | for(;;) |
| 117 | { | 124 | { |
| 118 | if( qbRead.size() == 0 ) | 125 | if( qbRead.size() == 0 ) |
| 119 | return false; | 126 | return false; |
| 120 | if( qbRead.at(0) != 0 ) | 127 | if( qbRead.at(0) != 0 ) |
| 121 | return !bHaveSeeked; //true; | 128 | return !bHaveSeeked; //true; |
| 122 | else | 129 | else |
| 123 | { | 130 | { |
| 124 | qbRead.remove( 0, 1 ); | 131 | qbRead.remove( 0, 1 ); |
| 125 | bHaveSeeked = true; | 132 | bHaveSeeked = true; |
| 126 | } | 133 | } |
| 127 | } | 134 | } |
| 128 | } | 135 | } |
| 129 | 136 | ||
