From efcbdb7a0347b4399cbabacf3cbea432eeafb17b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 17 May 2011 01:24:51 +0000 Subject: Gats::Object now has a strToGats function, it's pretty slick, it takes a string and produces a fully formed gats tree. Also, gatscon now can interact with a server directly. --- src/object.cpp | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) (limited to 'src/object.cpp') diff --git a/src/object.cpp b/src/object.cpp index af81017..da77ff8 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -7,9 +7,14 @@ #include "gats/list.h" #include "gats/dictionary.h" +#include + #include #include +#include +using namespace Bu; + Gats::Object::Object() { } @@ -63,6 +68,193 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn ) return pObj; } +void Gats::Object::skipWs( Bu::String::const_iterator &i ) +{ + for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { } +} + +Bu::String Gats::Object::token( Bu::String::const_iterator &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++ ) + { + sRet += i; + } + } + + return sRet; +} + +Gats::Object *Gats::Object::strToGats( Bu::String::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 Bu::ExceptionBase("Invalid character found."); + } + } + } + break; + + case '{': + { + Gats::Dictionary *pDict = new Gats::Dictionary(); + i++; + for(;;) + { + skipWs( i ); + if( *i != '\"' ) + throw Bu::ExceptionBase("Keys must be quoted strings."); + Bu::String sKey = token( i ); + skipWs( i ); + if( *i != ':' ) + throw Bu::ExceptionBase("Keys and values must be " + "seperated with colons."); + i++; + Gats::Object *pObj = strToGats( i ); + if( !pObj ) + throw Bu::ExceptionBase("No value object found."); + pDict->insert( sKey, pObj ); + skipWs( i ); + switch( *i ) + { + case ',': + i++; + break; + + case '}': + i++; + return pDict; + + default: + throw Bu::ExceptionBase("Invalid character found."); + } + } + } + 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 ); + } + } + } + } + break; + } + + return NULL; +} + +Gats::Object *Gats::Object::strToGats( const Bu::String &sStr ) +{ + Bu::String::const_iterator i = sStr.begin(); + + return strToGats( i ); +} + Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) { switch( obj.getType() ) -- cgit v1.2.3