libgats /c++-libbu++/src/object.cpp

Language C++ Lines 381
MD5 Hash 076689f62f79b1fd9277742df4ad4ebc
Repository https://github.com/eichlan/libgats.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "gats/object.h"

#include "gats/integer.h"
#include "gats/float.h"
#include "gats/boolean.h"
#include "gats/string.h"
#include "gats/list.h"
#include "gats/dictionary.h"
#include "gats/null.h"

#include <stdlib.h>

#include <bu/formatter.h>
#include <bu/stream.h>

#include <bu/sio.h>
using namespace Bu;

Gats::Object::Object()
{
}

Gats::Object::~Object()
{
}

Gats::Object *Gats::Object::read( Bu::Stream &rIn )
{
    char buf;
    rIn.read( &buf, 1 );
    Object *pObj = NULL;
    switch( buf )
    {
        case 'i':
            pObj = new Gats::Integer();
            break;

        case 's':
            pObj = new Gats::String();
            break;

        case '0':
        case '1':
            pObj = new Gats::Boolean();
            break;

        case 'l':
            pObj = new Gats::List();
            break;

        case 'd':
            pObj = new Gats::Dictionary();
            break;

        case 'f':  // Normal floats
        case 'F':  // Special float values
            pObj = new Gats::Float();
            break;

        case 'n':
            pObj = new Gats::Null();
            break;

        case 'e':
            return NULL;

        default:
            throw Bu::ExceptionBase("Invalid Gats type discovered: %c.", buf );
    }

    pObj->read( rIn, buf );

    return pObj;
}

void Gats::Object::skipWs( Gats::Object::StrPos &i )
{
    for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n' || *i == '/'; i++ )
    {
        if( *i == '/' )
        {
            i++;
            if( *i == '/' )
                for(i++; *i != '\n' && *i != '\r'; i++ ) { }
            else if( *i == '*' )
            {
                for(i++; i; i++ )
                {
                    if( *i == '*' )
                    {
                        i++;
                        if( *i == '/' )
                        {
                            i++;
                            break;
                        }
                    }
                }
            }
            else
                return;
        }
    }
}

Bu::String Gats::Object::token( Gats::Object::StrPos &i )
{
    Bu::String sRet;
    if( *i == '\"' )
	{
		for( i++; i && *i != '\"' ; i++ )
        {
            if( *i == '\\' )
                i++;
            sRet += *i;
        }
        i++;
    }
    else
    {
        for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' &&
                *i != ',' && *i != ']' && *i != '}' && *i != '[' &&
                *i != '{' && *i != '/'; i++ )
        {
            sRet += *i;
        }
    }

    return sRet;
}

Bu::String::FormatProxy Gats::Object::posError( Gats::Object::StrPos &i, const Bu::String &msg )
{
    return msg.format( new Thrower(i) );
}

Gats::Object *Gats::Object::strToGats( Gats::Object::StrPos &i )
{
    skipWs( i );

    switch( *i )
    {
        case '[':
            {
                Gats::List *pLst = new Gats::List();
                i++;
                for(;;)
                {
                    skipWs( i );
                    if( *i == ']' )
                    {
                        i++;
                        return pLst;
                    }
                    Gats::Object *pObj = strToGats( i );
                    if( !pObj )
                        break;
                    pLst->append( pObj );
                    skipWs( i );
                    switch( *i )
                    {
                        case ',':
                            i++;
                            break;

                        case ']':
                            i++;
                            return pLst;

                        default:
                            posError(i, "Expected ',' or ']' but found '%1'.").
                                arg( *i );
                    }
                }
            }
            break;

        case '{':
            {
                Gats::Dictionary *pDict = new Gats::Dictionary();
                i++;
                for(;;)
                {
                    skipWs( i );
                    if( *i == '}' )
                    {
                        i++;
                        return pDict;
                    }
                    if( *i != '\"' )
                        posError(i, "Dictionary keys must be quoted strings.");
					Bu::String sKey = token( i );
					skipWs( i );
					if( *i != ':' )
                        posError(i, "Dictionary keys and values must be "
							"seperated with colons.");
                    StrPos ih( i );
					i++;
                    skipWs( i );
					Gats::Object *pObj = strToGats( i );
					if( !pObj )
						posError(ih, "Dictionary key has no value.");
					pDict->insert( sKey, pObj );
					skipWs( i );
					switch( *i )
					{
						case ',':
							i++;
							break;

						case '}':
							i++;
							return pDict;

						default:
                            posError(i, "Expected ',' or '}' but found '%1'.")
                                .arg( *i );
					}
				}
			}
			break;

		case '\"':
            return new Gats::String( token( i ) );
            break;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '.':
        case '+':
        case '-':
            {
                Bu::String s = token( i );
                int iSize = s.getSize();
                if( s[iSize-1] == 'i' )
                {
                    return new Gats::Integer(
                        strtoll( s.getStr(), NULL, 10 )
                        );
                }
                else if( s[iSize-1] == 'f' )
                {
                    return new Gats::Float(
                        strtod( s.getStr(), NULL )
                        );
                }
                else
                {
                    for( Bu::String::iterator i = s.begin(); i; i++ )
                    {
                        if( *i == '.' )
                            return new Gats::Float(
                                strtod( s.getStr(), NULL )
                                );
                    }
                    return new Gats::Integer(
                        strtoll( s.getStr(), NULL, 10 )
                        );
                }
            }
            break;

        default:
            {
                Bu::String s = token( i );
                int iSize = s.getSize();
                // Test for explicit types first
                if( iSize > 2 )
                {
                    if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' )
                    {
                    }
                    else
                    {
                        Bu::String st = s.toLower();
                        if( st == "true" )
                        {
                            return new Gats::Boolean( true );
                        }
                        else if( st == "false" )
                        {
                            return new Gats::Boolean( false );
                        }
                        else if( st == "null" )
                        {
                            return new Gats::Null();
                        }
                    }
                }
            }
            break;
    }

    return NULL;
}

Gats::Object *Gats::Object::strToGats( const Bu::String &sStr )
{
    StrPos i( sStr.begin() );

    return strToGats( i );
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj )
{
    switch( obj.getType() )
    {
        case Gats::typeDictionary:
            return f << dynamic_cast<const Gats::Dictionary &>(obj);
        
        case Gats::typeList:
            return f << dynamic_cast<const Gats::List &>(obj);
        
        case Gats::typeString:
            return f << dynamic_cast<const Gats::String &>(obj);
        
        case Gats::typeInteger:
            return f << dynamic_cast<const Gats::Integer &>(obj);
        
        case Gats::typeFloat:
            return f << dynamic_cast<const Gats::Float &>(obj);
        
        case Gats::typeBoolean:
            return f << dynamic_cast<const Gats::Boolean &>(obj);

        case Gats::typeNull:
            return f << dynamic_cast<const Gats::Null &>(obj);

        default:
            return f << "***ERROR: Bad Gats type***";
    }
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t )
{
    switch( t )
    {
        case Gats::typeDictionary:  return f << "dictionary";
        case Gats::typeList:        return f << "list";
        case Gats::typeString:      return f << "string";
        case Gats::typeInteger:     return f << "integer";
        case Gats::typeFloat:       return f << "float";
        case Gats::typeBoolean:     return f << "boolean";
        case Gats::typeNull:        return f << "null";
    }

    return f << "***unknown***";
}

const char *Gats::typeToStr( Gats::Type t )
{
    switch( t )
    {
        case Gats::typeDictionary:  return "dictionary";
        case Gats::typeList:        return "list";
        case Gats::typeString:      return "string";
        case Gats::typeInteger:     return "integer";
        case Gats::typeFloat:       return "float";
        case Gats::typeBoolean:     return "boolean";
        case Gats::typeNull:        return "null";
    }

    return "***unknown***";
}
Back to Top