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++-libbu++/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 'c++-libbu++/src/object.cpp')
-rw-r--r-- | c++-libbu++/src/object.cpp | 503 |
1 files changed, 255 insertions, 248 deletions
diff --git a/c++-libbu++/src/object.cpp b/c++-libbu++/src/object.cpp index 15d7cb5..db7b80b 100644 --- a/c++-libbu++/src/object.cpp +++ b/c++-libbu++/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/object.h" | 8 | #include "gats/object.h" |
2 | 9 | ||
3 | #include "gats/integer.h" | 10 | #include "gats/integer.h" |
@@ -26,137 +33,137 @@ Gats::Object::~Object() | |||
26 | 33 | ||
27 | Gats::Object *Gats::Object::read( Bu::Stream &rIn ) | 34 | Gats::Object *Gats::Object::read( Bu::Stream &rIn ) |
28 | { | 35 | { |
29 | char buf; | 36 | char buf; |
30 | rIn.read( &buf, 1 ); | 37 | rIn.read( &buf, 1 ); |
31 | Object *pObj = NULL; | 38 | Object *pObj = NULL; |
32 | switch( buf ) | 39 | switch( buf ) |
33 | { | 40 | { |
34 | case 'i': | 41 | case 'i': |
35 | pObj = new Gats::Integer(); | 42 | pObj = new Gats::Integer(); |
36 | break; | 43 | break; |
37 | 44 | ||
38 | case 's': | 45 | case 's': |
39 | pObj = new Gats::String(); | 46 | pObj = new Gats::String(); |
40 | break; | 47 | break; |
41 | 48 | ||
42 | case '0': | 49 | case '0': |
43 | case '1': | 50 | case '1': |
44 | pObj = new Gats::Boolean(); | 51 | pObj = new Gats::Boolean(); |
45 | break; | 52 | break; |
46 | 53 | ||
47 | case 'l': | 54 | case 'l': |
48 | pObj = new Gats::List(); | 55 | pObj = new Gats::List(); |
49 | break; | 56 | break; |
50 | 57 | ||
51 | case 'd': | 58 | case 'd': |
52 | pObj = new Gats::Dictionary(); | 59 | pObj = new Gats::Dictionary(); |
53 | break; | 60 | break; |
54 | 61 | ||
55 | case 'f': // Normal floats | 62 | case 'f': // Normal floats |
56 | case 'F': // Special float values | 63 | case 'F': // Special float values |
57 | pObj = new Gats::Float(); | 64 | pObj = new Gats::Float(); |
58 | break; | 65 | break; |
59 | 66 | ||
60 | case 'n': | 67 | case 'n': |
61 | pObj = new Gats::Null(); | 68 | pObj = new Gats::Null(); |
62 | break; | 69 | break; |
63 | 70 | ||
64 | case 'e': | 71 | case 'e': |
65 | return NULL; | 72 | return NULL; |
66 | 73 | ||
67 | default: | 74 | default: |
68 | throw Bu::ExceptionBase("Invalid Gats type discovered: %c.", buf ); | 75 | throw Bu::ExceptionBase("Invalid Gats type discovered: %c.", buf ); |
69 | } | 76 | } |
70 | 77 | ||
71 | pObj->read( rIn, buf ); | 78 | pObj->read( rIn, buf ); |
72 | 79 | ||
73 | return pObj; | 80 | return pObj; |
74 | } | 81 | } |
75 | 82 | ||
76 | void Gats::Object::skipWs( Bu::String::const_iterator &i ) | 83 | void Gats::Object::skipWs( Bu::String::const_iterator &i ) |
77 | { | 84 | { |
78 | for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { } | 85 | for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { } |
79 | } | 86 | } |
80 | 87 | ||
81 | Bu::String Gats::Object::token( Bu::String::const_iterator &i ) | 88 | Bu::String Gats::Object::token( Bu::String::const_iterator &i ) |
82 | { | 89 | { |
83 | Bu::String sRet; | 90 | Bu::String sRet; |
84 | if( *i == '\"' ) | 91 | if( *i == '\"' ) |
85 | { | 92 | { |
86 | for( i++; i && *i != '\"' ; i++ ) | 93 | for( i++; i && *i != '\"' ; i++ ) |
87 | { | 94 | { |
88 | if( *i == '\\' ) | 95 | if( *i == '\\' ) |
89 | i++; | 96 | i++; |
90 | sRet += i; | 97 | sRet += i; |
91 | } | 98 | } |
92 | i++; | 99 | i++; |
93 | } | 100 | } |
94 | else | 101 | else |
95 | { | 102 | { |
96 | for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' && | 103 | for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' && |
97 | *i != ',' && *i != ']' && *i != '}' && *i != '[' && | 104 | *i != ',' && *i != ']' && *i != '}' && *i != '[' && |
98 | *i != '{'; i++ ) | 105 | *i != '{'; i++ ) |
99 | { | 106 | { |
100 | sRet += i; | 107 | sRet += i; |
101 | } | 108 | } |
102 | } | 109 | } |
103 | 110 | ||
104 | return sRet; | 111 | return sRet; |
105 | } | 112 | } |
106 | 113 | ||
107 | Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i ) | 114 | Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i ) |
108 | { | 115 | { |
109 | skipWs( i ); | 116 | skipWs( i ); |
110 | 117 | ||
111 | switch( *i ) | 118 | switch( *i ) |
112 | { | 119 | { |
113 | case '[': | 120 | case '[': |
114 | { | 121 | { |
115 | Gats::List *pLst = new Gats::List(); | 122 | Gats::List *pLst = new Gats::List(); |
116 | i++; | 123 | i++; |
117 | for(;;) | 124 | for(;;) |
118 | { | 125 | { |
119 | skipWs( i ); | 126 | skipWs( i ); |
120 | if( *i == ']' ) | 127 | if( *i == ']' ) |
121 | { | 128 | { |
122 | i++; | 129 | i++; |
123 | return pLst; | 130 | return pLst; |
124 | } | 131 | } |
125 | Gats::Object *pObj = strToGats( i ); | 132 | Gats::Object *pObj = strToGats( i ); |
126 | if( !pObj ) | 133 | if( !pObj ) |
127 | break; | 134 | break; |
128 | pLst->append( pObj ); | 135 | pLst->append( pObj ); |
129 | skipWs( i ); | 136 | skipWs( i ); |
130 | switch( *i ) | 137 | switch( *i ) |
131 | { | 138 | { |
132 | case ',': | 139 | case ',': |
133 | i++; | 140 | i++; |
134 | break; | 141 | break; |
135 | 142 | ||
136 | case ']': | 143 | case ']': |
137 | i++; | 144 | i++; |
138 | return pLst; | 145 | return pLst; |
139 | 146 | ||
140 | default: | 147 | default: |
141 | throw Bu::ExceptionBase("Invalid character found."); | 148 | throw Bu::ExceptionBase("Invalid character found."); |
142 | } | 149 | } |
143 | } | 150 | } |
144 | } | 151 | } |
145 | break; | 152 | break; |
146 | 153 | ||
147 | case '{': | 154 | case '{': |
148 | { | 155 | { |
149 | Gats::Dictionary *pDict = new Gats::Dictionary(); | 156 | Gats::Dictionary *pDict = new Gats::Dictionary(); |
150 | i++; | 157 | i++; |
151 | for(;;) | 158 | for(;;) |
152 | { | 159 | { |
153 | skipWs( i ); | 160 | skipWs( i ); |
154 | if( *i == '}' ) | 161 | if( *i == '}' ) |
155 | { | 162 | { |
156 | i++; | 163 | i++; |
157 | return pDict; | 164 | return pDict; |
158 | } | 165 | } |
159 | if( *i != '\"' ) | 166 | if( *i != '\"' ) |
160 | throw Bu::ExceptionBase("Keys must be quoted strings."); | 167 | throw Bu::ExceptionBase("Keys must be quoted strings."); |
161 | Bu::String sKey = token( i ); | 168 | Bu::String sKey = token( i ); |
162 | skipWs( i ); | 169 | skipWs( i ); |
@@ -187,153 +194,153 @@ Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i ) | |||
187 | break; | 194 | break; |
188 | 195 | ||
189 | case '\"': | 196 | case '\"': |
190 | return new Gats::String( token( i ) ); | 197 | return new Gats::String( token( i ) ); |
191 | break; | 198 | break; |
192 | 199 | ||
193 | case '0': | 200 | case '0': |
194 | case '1': | 201 | case '1': |
195 | case '2': | 202 | case '2': |
196 | case '3': | 203 | case '3': |
197 | case '4': | 204 | case '4': |
198 | case '5': | 205 | case '5': |
199 | case '6': | 206 | case '6': |
200 | case '7': | 207 | case '7': |
201 | case '8': | 208 | case '8': |
202 | case '9': | 209 | case '9': |
203 | case '.': | 210 | case '.': |
204 | case '+': | 211 | case '+': |
205 | case '-': | 212 | case '-': |
206 | { | 213 | { |
207 | Bu::String s = token( i ); | 214 | Bu::String s = token( i ); |
208 | int iSize = s.getSize(); | 215 | int iSize = s.getSize(); |
209 | if( s[iSize-1] == 'i' ) | 216 | if( s[iSize-1] == 'i' ) |
210 | { | 217 | { |
211 | return new Gats::Integer( | 218 | return new Gats::Integer( |
212 | strtoll( s.getStr(), NULL, 10 ) | 219 | strtoll( s.getStr(), NULL, 10 ) |
213 | ); | 220 | ); |
214 | } | 221 | } |
215 | else if( s[iSize-1] == 'f' ) | 222 | else if( s[iSize-1] == 'f' ) |
216 | { | 223 | { |
217 | return new Gats::Float( | 224 | return new Gats::Float( |
218 | strtod( s.getStr(), NULL ) | 225 | strtod( s.getStr(), NULL ) |
219 | ); | 226 | ); |
220 | } | 227 | } |
221 | else | 228 | else |
222 | { | 229 | { |
223 | for( Bu::String::iterator i = s.begin(); i; i++ ) | 230 | for( Bu::String::iterator i = s.begin(); i; i++ ) |
224 | { | 231 | { |
225 | if( *i == '.' ) | 232 | if( *i == '.' ) |
226 | return new Gats::Float( | 233 | return new Gats::Float( |
227 | strtod( s.getStr(), NULL ) | 234 | strtod( s.getStr(), NULL ) |
228 | ); | 235 | ); |
229 | } | 236 | } |
230 | return new Gats::Integer( | 237 | return new Gats::Integer( |
231 | strtoll( s.getStr(), NULL, 10 ) | 238 | strtoll( s.getStr(), NULL, 10 ) |
232 | ); | 239 | ); |
233 | } | 240 | } |
234 | } | 241 | } |
235 | break; | 242 | break; |
236 | 243 | ||
237 | default: | 244 | default: |
238 | { | 245 | { |
239 | Bu::String s = token( i ); | 246 | Bu::String s = token( i ); |
240 | int iSize = s.getSize(); | 247 | int iSize = s.getSize(); |
241 | // Test for explicit types first | 248 | // Test for explicit types first |
242 | if( iSize > 2 ) | 249 | if( iSize > 2 ) |
243 | { | 250 | { |
244 | if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' ) | 251 | if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' ) |
245 | { | 252 | { |
246 | } | 253 | } |
247 | else | 254 | else |
248 | { | 255 | { |
249 | Bu::String st = s.toLower(); | 256 | Bu::String st = s.toLower(); |
250 | if( st == "true" ) | 257 | if( st == "true" ) |
251 | { | 258 | { |
252 | return new Gats::Boolean( true ); | 259 | return new Gats::Boolean( true ); |
253 | } | 260 | } |
254 | else if( st == "false" ) | 261 | else if( st == "false" ) |
255 | { | 262 | { |
256 | return new Gats::Boolean( false ); | 263 | return new Gats::Boolean( false ); |
257 | } | 264 | } |
258 | else if( st == "null" ) | 265 | else if( st == "null" ) |
259 | { | 266 | { |
260 | return new Gats::Null(); | 267 | return new Gats::Null(); |
261 | } | 268 | } |
262 | } | 269 | } |
263 | } | 270 | } |
264 | } | 271 | } |
265 | break; | 272 | break; |
266 | } | 273 | } |
267 | 274 | ||
268 | return NULL; | 275 | return NULL; |
269 | } | 276 | } |
270 | 277 | ||
271 | Gats::Object *Gats::Object::strToGats( const Bu::String &sStr ) | 278 | Gats::Object *Gats::Object::strToGats( const Bu::String &sStr ) |
272 | { | 279 | { |
273 | Bu::String::const_iterator i = sStr.begin(); | 280 | Bu::String::const_iterator i = sStr.begin(); |
274 | 281 | ||
275 | return strToGats( i ); | 282 | return strToGats( i ); |
276 | } | 283 | } |
277 | 284 | ||
278 | Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) | 285 | Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) |
279 | { | 286 | { |
280 | switch( obj.getType() ) | 287 | switch( obj.getType() ) |
281 | { | 288 | { |
282 | case Gats::typeDictionary: | 289 | case Gats::typeDictionary: |
283 | return f << dynamic_cast<const Gats::Dictionary &>(obj); | 290 | return f << dynamic_cast<const Gats::Dictionary &>(obj); |
284 | 291 | ||
285 | case Gats::typeList: | 292 | case Gats::typeList: |
286 | return f << dynamic_cast<const Gats::List &>(obj); | 293 | return f << dynamic_cast<const Gats::List &>(obj); |
287 | 294 | ||
288 | case Gats::typeString: | 295 | case Gats::typeString: |
289 | return f << dynamic_cast<const Gats::String &>(obj); | 296 | return f << dynamic_cast<const Gats::String &>(obj); |
290 | 297 | ||
291 | case Gats::typeInteger: | 298 | case Gats::typeInteger: |
292 | return f << dynamic_cast<const Gats::Integer &>(obj); | 299 | return f << dynamic_cast<const Gats::Integer &>(obj); |
293 | 300 | ||
294 | case Gats::typeFloat: | 301 | case Gats::typeFloat: |
295 | return f << dynamic_cast<const Gats::Float &>(obj); | 302 | return f << dynamic_cast<const Gats::Float &>(obj); |
296 | 303 | ||
297 | case Gats::typeBoolean: | 304 | case Gats::typeBoolean: |
298 | return f << dynamic_cast<const Gats::Boolean &>(obj); | 305 | return f << dynamic_cast<const Gats::Boolean &>(obj); |
299 | 306 | ||
300 | case Gats::typeNull: | 307 | case Gats::typeNull: |
301 | return f << dynamic_cast<const Gats::Null &>(obj); | 308 | return f << dynamic_cast<const Gats::Null &>(obj); |
302 | 309 | ||
303 | default: | 310 | default: |
304 | return f << "***ERROR: Bad Gats type***"; | 311 | return f << "***ERROR: Bad Gats type***"; |
305 | } | 312 | } |
306 | } | 313 | } |
307 | 314 | ||
308 | Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t ) | 315 | Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t ) |
309 | { | 316 | { |
310 | switch( t ) | 317 | switch( t ) |
311 | { | 318 | { |
312 | case Gats::typeDictionary: return f << "dictionary"; | 319 | case Gats::typeDictionary: return f << "dictionary"; |
313 | case Gats::typeList: return f << "list"; | 320 | case Gats::typeList: return f << "list"; |
314 | case Gats::typeString: return f << "string"; | 321 | case Gats::typeString: return f << "string"; |
315 | case Gats::typeInteger: return f << "integer"; | 322 | case Gats::typeInteger: return f << "integer"; |
316 | case Gats::typeFloat: return f << "float"; | 323 | case Gats::typeFloat: return f << "float"; |
317 | case Gats::typeBoolean: return f << "boolean"; | 324 | case Gats::typeBoolean: return f << "boolean"; |
318 | case Gats::typeNull: return f << "null"; | 325 | case Gats::typeNull: return f << "null"; |
319 | } | 326 | } |
320 | 327 | ||
321 | return f << "***unknown***"; | 328 | return f << "***unknown***"; |
322 | } | 329 | } |
323 | 330 | ||
324 | const char *Gats::typeToStr( Gats::Type t ) | 331 | const char *Gats::typeToStr( Gats::Type t ) |
325 | { | 332 | { |
326 | switch( t ) | 333 | switch( t ) |
327 | { | 334 | { |
328 | case Gats::typeDictionary: return "dictionary"; | 335 | case Gats::typeDictionary: return "dictionary"; |
329 | case Gats::typeList: return "list"; | 336 | case Gats::typeList: return "list"; |
330 | case Gats::typeString: return "string"; | 337 | case Gats::typeString: return "string"; |
331 | case Gats::typeInteger: return "integer"; | 338 | case Gats::typeInteger: return "integer"; |
332 | case Gats::typeFloat: return "float"; | 339 | case Gats::typeFloat: return "float"; |
333 | case Gats::typeBoolean: return "boolean"; | 340 | case Gats::typeBoolean: return "boolean"; |
334 | case Gats::typeNull: return "null"; | 341 | case Gats::typeNull: return "null"; |
335 | } | 342 | } |
336 | 343 | ||
337 | return "***unknown***"; | 344 | return "***unknown***"; |
338 | } | 345 | } |
339 | 346 | ||