From 82da0238a8171cf8bba6bb1c82d5abba207a10aa Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 31 Mar 2012 17:34:11 +0000 Subject: Gats for QT is here. It's a pretty basic conversion right now, it doesn't support debugging formatting (Bu::sio), it doesn't support gats-text string parsing, and the exceptions need to be fixed to be real exceptions. The basic functions have been renamed to match the Qt API and we use QT types for everything (QHash, QList, QByteArray). It needs more testing, but it's a great start. --- c++-qt/src/object.cpp | 310 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 c++-qt/src/object.cpp (limited to 'c++-qt/src/object.cpp') diff --git a/c++-qt/src/object.cpp b/c++-qt/src/object.cpp new file mode 100644 index 0000000..3955ced --- /dev/null +++ b/c++-qt/src/object.cpp @@ -0,0 +1,310 @@ +#include "gats-qt/object.h" + +#include "gats-qt/integer.h" +#include "gats-qt/float.h" +#include "gats-qt/boolean.h" +#include "gats-qt/string.h" +#include "gats-qt/list.h" +#include "gats-qt/dictionary.h" + +#include + +#include + +Gats::Object::Object() +{ +} + +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 '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++ ) { } +} + +QByteArray Gats::Object::token( QByteArray::const_iterator &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; +} + +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 != '\"' ) + throw "PUT GOOD EXCEPTION HERE"; + QByteArray sKey = token( i ); + skipWs( i ); + if( *i != ':' ) + throw "PUT GOOD EXCEPTION HERE"; + "seperated with colons."); + i++; + Gats::Object *pObj = strToGats( i ); + if( !pObj ) + throw "PUT GOOD EXCEPTION HERE"; + pDict->insert( sKey, pObj ); + skipWs( i ); + switch( *i ) + { + case ',': + i++; + break; + + case '}': + i++; + return pDict; + + default: + throw "PUT GOOD EXCEPTION HERE"; + } + } + } + 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; +} + +Gats::Object *Gats::Object::strToGats( const QByteArray &sStr ) +{ + QByteArray::const_iterator 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(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***"; +} +*/ +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***"; +} + -- cgit v1.2.3