From d534a56d95bca7bdd812be024d9eacba4734e2b7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 9 Nov 2012 17:20:11 +0000 Subject: Many changes: tabconv'd the C++ code, added a license, BSD, and docs. --- c++-qt/src/object.cpp | 463 +++++++++++++++++++++++++------------------------- 1 file changed, 235 insertions(+), 228 deletions(-) (limited to 'c++-qt/src/object.cpp') 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 @@ +/* + * Copyright (C) 2007-2012 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-qt/object.h" #include "gats-qt/integer.h" @@ -22,126 +29,126 @@ Gats::Object::~Object() Gats::Object *Gats::Object::read( QIODevice &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 "Invalid Gats type discovered: "; - } - - pObj->read( rIn, buf ); - - return pObj; + 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 "Invalid Gats type discovered: "; + } + + pObj->read( rIn, buf ); + + return pObj; } /* void Gats::Object::skipWs( QByteArray::const_iterator &i ) { - for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { } + for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { } } QByteArray Gats::Object::token( QByteArray::const_iterator &i ) { - QByteArray sRet; - if( *i == '\"' ) + QByteArray 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++ ) - { - sRet += i; - } - } - - return sRet; + { + if( *i == '\\' ) + i++; + sRet += i; + } + i++; + } + else + { + for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' && + *i != ',' && *i != ']' && *i != '}' && *i != '[' && + *i != '{'; i++ ) + { + sRet += i; + } + } + + return sRet; } Gats::Object *Gats::Object::strToGats( QByteArray::const_iterator &i ) { - skipWs( i ); - - switch( *i ) - { - case '[': - { - Gats::List *pLst = new Gats::List(); - i++; - for(;;) - { - Gats::Object *pObj = strToGats( i ); - if( !pObj ) - break; - pLst->append( pObj ); - skipWs( i ); - switch( *i ) - { - case ',': - i++; - break; - - case ']': - i++; - return pLst; - - default: - throw "PUT GOOD EXCEPTION HERE"; - } - } - } - break; - - case '{': - { - Gats::Dictionary *pDict = new Gats::Dictionary(); - i++; - for(;;) - { - skipWs( i ); - if( *i != '\"' ) + skipWs( i ); + + switch( *i ) + { + case '[': + { + Gats::List *pLst = new Gats::List(); + i++; + for(;;) + { + Gats::Object *pObj = strToGats( i ); + if( !pObj ) + break; + pLst->append( pObj ); + skipWs( i ); + switch( *i ) + { + case ',': + i++; + break; + + case ']': + i++; + return pLst; + + default: + throw "PUT GOOD EXCEPTION HERE"; + } + } + } + break; + + case '{': + { + Gats::Dictionary *pDict = new Gats::Dictionary(); + i++; + for(;;) + { + skipWs( i ); + if( *i != '\"' ) throw "PUT GOOD EXCEPTION HERE"; QByteArray sKey = token( i ); skipWs( i ); @@ -172,144 +179,144 @@ Gats::Object *Gats::Object::strToGats( QByteArray::const_iterator &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 '-': - { - QByteArray 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( QByteArray::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: - { - QByteArray 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 - { - QByteArray st = s.toLower(); - if( st == "true" ) - { - return new Gats::Boolean( true ); - } - else if( st == "false" ) - { - return new Gats::Boolean( false ); - } - } - } - } - break; - } - - return NULL; + 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 '-': + { + QByteArray 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( QByteArray::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: + { + QByteArray 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 + { + QByteArray st = s.toLower(); + if( st == "true" ) + { + return new Gats::Boolean( true ); + } + else if( st == "false" ) + { + return new Gats::Boolean( false ); + } + } + } + } + break; + } + + return NULL; } Gats::Object *Gats::Object::strToGats( const QByteArray &sStr ) { - QByteArray::const_iterator i = sStr.begin(); + QByteArray::const_iterator i = sStr.begin(); - return strToGats( i ); + return strToGats( i ); } Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) { - switch( obj.getType() ) - { - case Gats::typeDictionary: - return f << dynamic_cast(obj); - - case Gats::typeList: - return f << dynamic_cast(obj); - - case Gats::typeString: - return f << dynamic_cast(obj); - - case Gats::typeInteger: - return f << dynamic_cast(obj); - - case Gats::typeFloat: - return f << dynamic_cast(obj); - - case Gats::typeBoolean: - return f << dynamic_cast(obj); - - default: - return f << "***ERROR: Bad Gats type***"; - } + switch( obj.getType() ) + { + case Gats::typeDictionary: + return f << dynamic_cast(obj); + + case Gats::typeList: + return f << dynamic_cast(obj); + + case Gats::typeString: + return f << dynamic_cast(obj); + + case Gats::typeInteger: + return f << dynamic_cast(obj); + + case Gats::typeFloat: + return f << dynamic_cast(obj); + + case Gats::typeBoolean: + return f << dynamic_cast(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"; - } - - return f << "***unknown***"; + 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"; + } + + 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"; - } - - return "***unknown***"; + 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"; + } + + return "***unknown***"; } -- cgit v1.2.3