aboutsummaryrefslogtreecommitdiff
path: root/c++-qt/src/object.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c++-qt/src/object.cpp')
-rw-r--r--c++-qt/src/object.cpp310
1 files changed, 310 insertions, 0 deletions
diff --git a/c++-qt/src/object.cpp b/c++-qt/src/object.cpp
new file mode 100644
index 0000000..3955ced
--- /dev/null
+++ b/c++-qt/src/object.cpp
@@ -0,0 +1,310 @@
1#include "gats-qt/object.h"
2
3#include "gats-qt/integer.h"
4#include "gats-qt/float.h"
5#include "gats-qt/boolean.h"
6#include "gats-qt/string.h"
7#include "gats-qt/list.h"
8#include "gats-qt/dictionary.h"
9
10#include <stdlib.h>
11
12#include <QIODevice>
13
14Gats::Object::Object()
15{
16}
17
18Gats::Object::~Object()
19{
20}
21
22Gats::Object *Gats::Object::read( QIODevice &rIn )
23{
24 char buf;
25 rIn.read( &buf, 1 );
26 Object *pObj = NULL;
27 switch( buf )
28 {
29 case 'i':
30 pObj = new Gats::Integer();
31 break;
32
33 case 's':
34 pObj = new Gats::String();
35 break;
36
37 case '0':
38 case '1':
39 pObj = new Gats::Boolean();
40 break;
41
42 case 'l':
43 pObj = new Gats::List();
44 break;
45
46 case 'd':
47 pObj = new Gats::Dictionary();
48 break;
49
50 case 'f': // Normal floats
51 case 'F': // Special float values
52 pObj = new Gats::Float();
53 break;
54
55 case 'e':
56 return NULL;
57
58 default:
59 throw "Invalid Gats type discovered: ";
60 }
61
62 pObj->read( rIn, buf );
63
64 return pObj;
65}
66/*
67void Gats::Object::skipWs( QByteArray::const_iterator &i )
68{
69 for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { }
70}
71
72QByteArray Gats::Object::token( QByteArray::const_iterator &i )
73{
74 QByteArray sRet;
75 if( *i == '\"' )
76 {
77 for( i++; i && *i != '\"' ; i++ )
78 {
79 if( *i == '\\' )
80 i++;
81 sRet += i;
82 }
83 i++;
84 }
85 else
86 {
87 for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' &&
88 *i != ',' && *i != ']' && *i != '}' && *i != '[' &&
89 *i != '{'; i++ )
90 {
91 sRet += i;
92 }
93 }
94
95 return sRet;
96}
97
98Gats::Object *Gats::Object::strToGats( QByteArray::const_iterator &i )
99{
100 skipWs( i );
101
102 switch( *i )
103 {
104 case '[':
105 {
106 Gats::List *pLst = new Gats::List();
107 i++;
108 for(;;)
109 {
110 Gats::Object *pObj = strToGats( i );
111 if( !pObj )
112 break;
113 pLst->append( pObj );
114 skipWs( i );
115 switch( *i )
116 {
117 case ',':
118 i++;
119 break;
120
121 case ']':
122 i++;
123 return pLst;
124
125 default:
126 throw "PUT GOOD EXCEPTION HERE";
127 }
128 }
129 }
130 break;
131
132 case '{':
133 {
134 Gats::Dictionary *pDict = new Gats::Dictionary();
135 i++;
136 for(;;)
137 {
138 skipWs( i );
139 if( *i != '\"' )
140 throw "PUT GOOD EXCEPTION HERE";
141 QByteArray sKey = token( i );
142 skipWs( i );
143 if( *i != ':' )
144 throw "PUT GOOD EXCEPTION HERE";
145 "seperated with colons.");
146 i++;
147 Gats::Object *pObj = strToGats( i );
148 if( !pObj )
149 throw "PUT GOOD EXCEPTION HERE";
150 pDict->insert( sKey, pObj );
151 skipWs( i );
152 switch( *i )
153 {
154 case ',':
155 i++;
156 break;
157
158 case '}':
159 i++;
160 return pDict;
161
162 default:
163 throw "PUT GOOD EXCEPTION HERE";
164 }
165 }
166 }
167 break;
168
169 case '\"':
170 return new Gats::String( token( i ) );
171 break;
172
173 case '0':
174 case '1':
175 case '2':
176 case '3':
177 case '4':
178 case '5':
179 case '6':
180 case '7':
181 case '8':
182 case '9':
183 case '.':
184 case '+':
185 case '-':
186 {
187 QByteArray s = token( i );
188 int iSize = s.getSize();
189 if( s[iSize-1] == 'i' )
190 {
191 return new Gats::Integer(
192 strtoll( s.getStr(), NULL, 10 )
193 );
194 }
195 else if( s[iSize-1] == 'f' )
196 {
197 return new Gats::Float(
198 strtod( s.getStr(), NULL )
199 );
200 }
201 else
202 {
203 for( QByteArray::iterator i = s.begin(); i; i++ )
204 {
205 if( *i == '.' )
206 return new Gats::Float(
207 strtod( s.getStr(), NULL )
208 );
209 }
210 return new Gats::Integer(
211 strtoll( s.getStr(), NULL, 10 )
212 );
213 }
214 }
215 break;
216
217 default:
218 {
219 QByteArray s = token( i );
220 int iSize = s.getSize();
221 // Test for explicit types first
222 if( iSize > 2 )
223 {
224 if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' )
225 {
226 }
227 else
228 {
229 QByteArray st = s.toLower();
230 if( st == "true" )
231 {
232 return new Gats::Boolean( true );
233 }
234 else if( st == "false" )
235 {
236 return new Gats::Boolean( false );
237 }
238 }
239 }
240 }
241 break;
242 }
243
244 return NULL;
245}
246
247Gats::Object *Gats::Object::strToGats( const QByteArray &sStr )
248{
249 QByteArray::const_iterator i = sStr.begin();
250
251 return strToGats( i );
252}
253
254Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj )
255{
256 switch( obj.getType() )
257 {
258 case Gats::typeDictionary:
259 return f << dynamic_cast<const Gats::Dictionary &>(obj);
260
261 case Gats::typeList:
262 return f << dynamic_cast<const Gats::List &>(obj);
263
264 case Gats::typeString:
265 return f << dynamic_cast<const Gats::String &>(obj);
266
267 case Gats::typeInteger:
268 return f << dynamic_cast<const Gats::Integer &>(obj);
269
270 case Gats::typeFloat:
271 return f << dynamic_cast<const Gats::Float &>(obj);
272
273 case Gats::typeBoolean:
274 return f << dynamic_cast<const Gats::Boolean &>(obj);
275
276 default:
277 return f << "***ERROR: Bad Gats type***";
278 }
279}
280
281Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t )
282{
283 switch( t )
284 {
285 case Gats::typeDictionary: return f << "dictionary";
286 case Gats::typeList: return f << "list";
287 case Gats::typeString: return f << "string";
288 case Gats::typeInteger: return f << "integer";
289 case Gats::typeFloat: return f << "float";
290 case Gats::typeBoolean: return f << "boolean";
291 }
292
293 return f << "***unknown***";
294}
295*/
296const char *Gats::typeToStr( Gats::Type t )
297{
298 switch( t )
299 {
300 case Gats::typeDictionary: return "dictionary";
301 case Gats::typeList: return "list";
302 case Gats::typeString: return "string";
303 case Gats::typeInteger: return "integer";
304 case Gats::typeFloat: return "float";
305 case Gats::typeBoolean: return "boolean";
306 }
307
308 return "***unknown***";
309}
310