diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-03-31 17:34:11 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-03-31 17:34:11 +0000 |
commit | 82da0238a8171cf8bba6bb1c82d5abba207a10aa (patch) | |
tree | 83ff9b022b658e7a056fc85f132937b8bda4c1c3 /c++-qt/src/gatsstream.cpp | |
parent | 7dd5c386611e31930e7ccfb83cb585df27696881 (diff) | |
download | libgats-82da0238a8171cf8bba6bb1c82d5abba207a10aa.tar.gz libgats-82da0238a8171cf8bba6bb1c82d5abba207a10aa.tar.bz2 libgats-82da0238a8171cf8bba6bb1c82d5abba207a10aa.tar.xz libgats-82da0238a8171cf8bba6bb1c82d5abba207a10aa.zip |
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.
Diffstat (limited to '')
-rw-r--r-- | c++-qt/src/gatsstream.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
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 @@ | |||
1 | #include "gats-qt/gatsstream.h" | ||
2 | #include "gats-qt/object.h" | ||
3 | |||
4 | #ifdef WIN32 | ||
5 | #include <winsock2.h> | ||
6 | #else | ||
7 | #include <arpa/inet.h> | ||
8 | #endif | ||
9 | |||
10 | #include <QBuffer> | ||
11 | |||
12 | Gats::GatsStream::GatsStream( QIODevice &rStream ) : | ||
13 | rStream( rStream ) | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Gats::GatsStream::~GatsStream() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | Gats::Object *Gats::GatsStream::readObject() | ||
22 | { | ||
23 | char buf[1500]; | ||
24 | |||
25 | do | ||
26 | { | ||
27 | if( qbRead.size() < 5 ) | ||
28 | { | ||
29 | int iRead = rStream.read( buf, 5-qbRead.size() ); | ||
30 | qbRead.append( buf, iRead ); | ||
31 | |||
32 | if( qbRead.size() < 5 ) | ||
33 | return NULL; | ||
34 | } | ||
35 | } while( !skipReadNulls() ); | ||
36 | |||
37 | uint8_t uVer; | ||
38 | uVer = qbRead[0]; | ||
39 | |||
40 | int32_t iSize; | ||
41 | memcpy( &iSize, qbRead.constData()+1, 4 ); | ||
42 | iSize = ntohl( iSize ); | ||
43 | while( qbRead.size() < iSize ) | ||
44 | { | ||
45 | int32_t iRead = iSize - qbRead.size(); | ||
46 | if( iRead > 1500 ) | ||
47 | iRead = 1500; | ||
48 | int32_t iReal = rStream.read( buf, iRead ); | ||
49 | qbRead.append( buf, iReal ); | ||
50 | if( iReal < iRead ) | ||
51 | { | ||
52 | return NULL; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | if( qbRead.size() < iSize ) | ||
57 | { | ||
58 | return NULL; | ||
59 | } | ||
60 | |||
61 | QBuffer rTmp( &qbRead ); | ||
62 | rTmp.open( QIODevice::ReadOnly ); | ||
63 | rTmp.seek( 5 ); | ||
64 | Gats::Object *pObj = Gats::Object::read( rTmp ); | ||
65 | |||
66 | return pObj; | ||
67 | } | ||
68 | |||
69 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) | ||
70 | { | ||
71 | QIODevice *pTmp; | ||
72 | if( rStream.isSequential() ) | ||
73 | { | ||
74 | pTmp = new QBuffer(); | ||
75 | pTmp->open( QIODevice::WriteOnly ); | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | pTmp = &rStream; | ||
80 | } | ||
81 | |||
82 | uint8_t uBuf = 1; | ||
83 | uint32_t iSize = 0; | ||
84 | pTmp->write( (const char *)&uBuf, 1 ); | ||
85 | uint64_t iSizePos = pTmp->pos(); | ||
86 | pTmp->write( (const char *)&iSize, 4 ); | ||
87 | pObject->write( *pTmp ); | ||
88 | iSize = htonl( pTmp->pos() ); | ||
89 | pTmp->seek( iSizePos ); | ||
90 | pTmp->write( (const char *)&iSize, 4 ); | ||
91 | pTmp->close(); | ||
92 | |||
93 | if( rStream.isSequential() ) | ||
94 | { | ||
95 | rStream.write( ((QBuffer *)pTmp)->data() ); | ||
96 | delete pTmp; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | bool Gats::GatsStream::skipReadNulls() | ||
101 | { | ||
102 | bool bHaveSeeked = false; | ||
103 | for(;;) | ||
104 | { | ||
105 | if( qbRead.size() == 0 ) | ||
106 | return false; | ||
107 | if( qbRead.at(0) != 0 ) | ||
108 | return !bHaveSeeked; //true; | ||
109 | else | ||
110 | { | ||
111 | qbRead.remove( 0, 1 ); | ||
112 | bHaveSeeked = true; | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||