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