From 3cbd1038e20dcb2b25db4e74666ec21766642729 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 6 Apr 2022 23:45:42 -0700 Subject: toString works. It does indent automatically. I figure, you know, if you're converting to a string then a big part of the reason is so you can read it. In that case, why make it optional? --- c++-qt/gats-qt/boolean.h | 2 ++ c++-qt/gats-qt/dictionary.h | 2 ++ c++-qt/gats-qt/float.h | 2 ++ c++-qt/gats-qt/integer.h | 2 ++ c++-qt/gats-qt/list.h | 2 ++ c++-qt/gats-qt/null.h | 2 ++ c++-qt/gats-qt/object.h | 2 ++ c++-qt/gats-qt/string.h | 2 ++ c++-qt/src/boolean.cpp | 8 +++++++- c++-qt/src/dictionary.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ c++-qt/src/float.cpp | 6 ++++++ c++-qt/src/gatsstream.cpp | 7 ++++--- c++-qt/src/integer.cpp | 7 ++++++- c++-qt/src/list.cpp | 28 ++++++++++++++++++++++++++- c++-qt/src/null.cpp | 7 ++++++- c++-qt/src/object.cpp | 1 + c++-qt/src/string.cpp | 26 ++++++++++++++++++++++++- 17 files changed, 144 insertions(+), 8 deletions(-) diff --git a/c++-qt/gats-qt/boolean.h b/c++-qt/gats-qt/boolean.h index e6d10af..07dcbc3 100644 --- a/c++-qt/gats-qt/boolean.h +++ b/c++-qt/gats-qt/boolean.h @@ -31,6 +31,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; private: bool bVal; diff --git a/c++-qt/gats-qt/dictionary.h b/c++-qt/gats-qt/dictionary.h index b1b6051..6f9e82a 100644 --- a/c++-qt/gats-qt/dictionary.h +++ b/c++-qt/gats-qt/dictionary.h @@ -30,6 +30,8 @@ namespace Gats virtual Type getType() const { return typeDictionary; } virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; void insert( const QByteArray &sKey, const char *s ); void insert( const QByteArray &sKey, const QByteArray &s ); diff --git a/c++-qt/gats-qt/float.h b/c++-qt/gats-qt/float.h index 5a1ff4b..453d000 100644 --- a/c++-qt/gats-qt/float.h +++ b/c++-qt/gats-qt/float.h @@ -28,6 +28,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; private: double fVal; diff --git a/c++-qt/gats-qt/integer.h b/c++-qt/gats-qt/integer.h index 3427682..2f08d43 100644 --- a/c++-qt/gats-qt/integer.h +++ b/c++-qt/gats-qt/integer.h @@ -33,6 +33,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; template static void readPackedInt( QIODevice &rStream, itype &rOut ) diff --git a/c++-qt/gats-qt/list.h b/c++-qt/gats-qt/list.h index 27c17c3..f87ddd7 100644 --- a/c++-qt/gats-qt/list.h +++ b/c++-qt/gats-qt/list.h @@ -29,6 +29,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; void append( const char *s ); void append( const QByteArray &s ); diff --git a/c++-qt/gats-qt/null.h b/c++-qt/gats-qt/null.h index 974ae09..7bf309a 100644 --- a/c++-qt/gats-qt/null.h +++ b/c++-qt/gats-qt/null.h @@ -27,6 +27,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; }; }; diff --git a/c++-qt/gats-qt/object.h b/c++-qt/gats-qt/object.h index 6d602de..8d64c38 100644 --- a/c++-qt/gats-qt/object.h +++ b/c++-qt/gats-qt/object.h @@ -42,6 +42,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const=0; virtual void read( QIODevice &rIn, char cType )=0; + + virtual QString toString( int iIndent=0 ) const = 0; static Object *read( QIODevice &rIn ); // static Object *strToGats( const &sStr ); diff --git a/c++-qt/gats-qt/string.h b/c++-qt/gats-qt/string.h index 1e87db5..443c5a8 100644 --- a/c++-qt/gats-qt/string.h +++ b/c++-qt/gats-qt/string.h @@ -31,6 +31,8 @@ namespace Gats virtual void write( QIODevice &rOut ) const; virtual void read( QIODevice &rIn, char cType ); + + virtual QString toString( int iIndent=0 ) const; private: }; diff --git a/c++-qt/src/boolean.cpp b/c++-qt/src/boolean.cpp index bd9bca1..8e7317a 100644 --- a/c++-qt/src/boolean.cpp +++ b/c++-qt/src/boolean.cpp @@ -40,7 +40,7 @@ void Gats::Boolean::write( QIODevice &rOut ) const } } -void Gats::Boolean::read( QIODevice &rIn, char cType ) +void Gats::Boolean::read( QIODevice & /*rIn*/, char cType ) { if( cType == '1' ) { @@ -51,6 +51,12 @@ void Gats::Boolean::read( QIODevice &rIn, char cType ) bVal = false; } } + +QString Gats::Boolean::toString( int /*iIndent*/ ) const +{ + return bVal?"true":"false"; +} + /* Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Boolean &b ) { diff --git a/c++-qt/src/dictionary.cpp b/c++-qt/src/dictionary.cpp index 10ec6c5..1d666ac 100644 --- a/c++-qt/src/dictionary.cpp +++ b/c++-qt/src/dictionary.cpp @@ -73,6 +73,52 @@ void Gats::Dictionary::read( QIODevice &rIn, char /*cType*/ ) } } +QString Gats::Dictionary::toString( int iIndent ) const +{ + if( count() == 0 ) + return "{ }"; + QString sRet("{"); + iIndent++; + for( const_iterator i = begin(); i != end(); i++ ) + { + if( i == begin() ) + { + sRet += "\n"; + } + else + { + sRet += ",\n"; + } + for( int j = 0; j < iIndent; j++ ) sRet += " "; + sRet += '"'; + for( QByteArray::const_iterator iS = i.key().begin(); + iS != i.key().end(); iS++ ) + { + switch( *iS ) + { + case '\\': + sRet += "\\"; + break; + + case '\"': + sRet += "\\\""; + break; + + default: + sRet += *iS; + break; + } + } + sRet += "\": "; + sRet += i.value()->toString( iIndent ); + } + iIndent--; + sRet += "\n"; + for( int j = 0; j < iIndent; j++ ) sRet += " "; + sRet += "}"; + return sRet; +} + void Gats::Dictionary::insert( const QByteArray &sKey, char i ) { ((QHash *)this)->insert( diff --git a/c++-qt/src/float.cpp b/c++-qt/src/float.cpp index 7f0d1a3..d4ec76e 100644 --- a/c++-qt/src/float.cpp +++ b/c++-qt/src/float.cpp @@ -126,6 +126,12 @@ void Gats::Float::read( QIODevice &rIn, char cType ) if( bNeg ) fVal = -fVal; } } + +QString Gats::Float::toString( int /*iIndent*/ ) const +{ + return QString("%1").arg( fVal ); +} + /* Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Float &flt ) { diff --git a/c++-qt/src/gatsstream.cpp b/c++-qt/src/gatsstream.cpp index cb6b53c..36b2ba8 100644 --- a/c++-qt/src/gatsstream.cpp +++ b/c++-qt/src/gatsstream.cpp @@ -42,8 +42,9 @@ Gats::Object *Gats::GatsStream::readObject() } } while( !skipReadNulls() ); - uint8_t uVer; - uVer = qbRead[0]; + // There's only one version, so...we don't need to do anything with this + //uint8_t uVer; + //uVer = qbRead[0]; int32_t iSize; memcpy( &iSize, qbRead.constData()+1, 4 ); @@ -102,7 +103,7 @@ void Gats::GatsStream::writeObject( Gats::Object *pObject ) pTmp->write( (const char *)&iSize, 4 ); pObject->write( *pTmp ); iSize = htonl( pTmp->pos() ); - uint64_t iEndPos = pTmp->pos(); + //uint64_t iEndPos = pTmp->pos(); pTmp->seek( iSizePos ); pTmp->write( (const char *)&iSize, 4 ); diff --git a/c++-qt/src/integer.cpp b/c++-qt/src/integer.cpp index 967eb30..1c665b4 100644 --- a/c++-qt/src/integer.cpp +++ b/c++-qt/src/integer.cpp @@ -32,10 +32,15 @@ void Gats::Integer::write( QIODevice &rOut ) const writePackedInt( rOut, iVal ); } -void Gats::Integer::read( QIODevice &rIn, char cType ) +void Gats::Integer::read( QIODevice &rIn, char /*cType*/ ) { readPackedInt( rIn, iVal ); } + +QString Gats::Integer::toString( int /*iIndent*/ ) const +{ + return QString("%1").arg( iVal ); +} /* Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Integer &i ) { diff --git a/c++-qt/src/list.cpp b/c++-qt/src/list.cpp index fae51d1..cca7c46 100644 --- a/c++-qt/src/list.cpp +++ b/c++-qt/src/list.cpp @@ -48,7 +48,7 @@ void Gats::List::write( QIODevice &rOut ) const rOut.write("e", 1 ); } -void Gats::List::read( QIODevice &rIn, char cType ) +void Gats::List::read( QIODevice &rIn, char /*cType*/ ) { for(;;) { @@ -59,6 +59,32 @@ void Gats::List::read( QIODevice &rIn, char cType ) } } +QString Gats::List::toString( int iIndent ) const +{ + if( count() == 0 ) + return "[ ]"; + QString sRet("["); + iIndent++; + for( const_iterator i = begin(); i != end(); i++ ) + { + if( i != begin() ) + { + sRet += ",\n"; + } + else + { + sRet += "\n"; + } + for( int j = 0; j < iIndent; j++ ) sRet += " "; + sRet += (*i)->toString( iIndent ); + } + sRet += "\n"; + iIndent--; + for( int j = 0; j < iIndent; j++ ) sRet += " "; + sRet += ']'; + return sRet; +} + void Gats::List::append( const char *s ) { QList::append( new Gats::String( s ) ); diff --git a/c++-qt/src/null.cpp b/c++-qt/src/null.cpp index 8bc87a6..5e3a19c 100644 --- a/c++-qt/src/null.cpp +++ b/c++-qt/src/null.cpp @@ -27,9 +27,14 @@ void Gats::Null::write( QIODevice &rOut ) const rOut.write("n", 1 ); } -void Gats::Null::read( QIODevice &rIn, char cType ) +void Gats::Null::read( QIODevice & /*rIn*/, char /*cType*/ ) { } + +QString Gats::Null::toString( int /*iIndent*/ ) const +{ + return "null"; +} /* Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Null &b ) { diff --git a/c++-qt/src/object.cpp b/c++-qt/src/object.cpp index 8af203f..b0f0f12 100644 --- a/c++-qt/src/object.cpp +++ b/c++-qt/src/object.cpp @@ -88,6 +88,7 @@ const char *Gats::typeToStr( Gats::Type t ) case Gats::typeInteger: return "integer"; case Gats::typeFloat: return "float"; case Gats::typeBoolean: return "boolean"; + case Gats::typeNull: return "null"; } return "***unknown***"; diff --git a/c++-qt/src/string.cpp b/c++-qt/src/string.cpp index 1eddcf6..fd6cad1 100644 --- a/c++-qt/src/string.cpp +++ b/c++-qt/src/string.cpp @@ -57,13 +57,37 @@ void Gats::String::write( QIODevice &rOut ) const rOut.write( constData(), iSize ); } -void Gats::String::read( QIODevice &rIn, char cType ) +void Gats::String::read( QIODevice &rIn, char /*cType*/ ) { uint32_t iSize; Gats::Integer::readPackedInt( rIn, iSize ); fill( '\0', iSize ); rIn.read( data(), iSize ); } + +QString Gats::String::toString( int /*iIndent*/ ) const +{ + QString sRet("\""); + for( const_iterator iS = begin(); iS != end(); iS++ ) + { + switch( *iS ) + { + case '\\': + sRet += "\\"; + break; + + case '\"': + sRet += "\\\""; + break; + + default: + sRet += *iS; + break; + } + } + sRet += "\""; + return sRet; +} /* Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::String &s ) { -- cgit v1.2.3