From 2b90449c30e4a420af4fff7e58588611d71f61fc Mon Sep 17 00:00:00 2001 From: David Date: Fri, 15 Jun 2007 18:54:59 +0000 Subject: david - wrote two silly unit tests... --- src/unit/taf.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/unit/taf.cpp (limited to 'src/unit/taf.cpp') diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp new file mode 100644 index 0000000..ab485d0 --- /dev/null +++ b/src/unit/taf.cpp @@ -0,0 +1,48 @@ +#include "unitsuite.h" +#include "file.h" +#include "tafreader.h" + +#include +#include + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("taf"); + addTest( Unit::read1 ); + } + + virtual ~Unit() + { + } + + void read1() + { +#define FN_TMP ("/tmp/tmpXXXXXXXX") + Bu::FString sFnTmp(FN_TMP); + Bu::File fOut = Bu::File::tempFile( sFnTmp, "wb" ); + const char *data = +"{test: name=\"Bob\"}" +; + fOut.write(data,strlen(data)); + fOut.close(); + + Bu::File fIn(sFnTmp.c_str(), "rb"); + Bu::TafReader tr(fIn); + + Bu::TafNode *tn = tr.getNode(); + unitTest( !strcmp("Bob", tn->getProperty("name").c_str()) ); + delete tn; + + unlink(sFnTmp.c_str()); +#undef FN_TMP + } +}; + +int main( int argc, char *argv[] ) +{ + return Unit().run( argc, argv ); +} + -- cgit v1.2.3 From c86c0cf088492e02431dffb1f860ff2f6faa492d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 27 Jun 2007 18:11:13 +0000 Subject: The taf system is new and improved. The writer works, we added C++ style comment blocks, and it retains the order of all nodes. --- src/tafnode.cpp | 138 ++++++++++++++++++++++++++++++++++++++++++------------ src/tafnode.h | 68 +++++++++++++++++++++++---- src/tafreader.cpp | 44 +++++++++++------ src/tafreader.h | 9 ++-- src/tafwriter.cpp | 42 ++++++++++++++++- src/tafwriter.h | 4 +- src/tests/taf.cpp | 13 +++-- src/unit/taf.cpp | 2 +- test.taf | 31 +++--------- 9 files changed, 260 insertions(+), 91 deletions(-) (limited to 'src/unit/taf.cpp') diff --git a/src/tafnode.cpp b/src/tafnode.cpp index b9a4a24..b7cf211 100644 --- a/src/tafnode.cpp +++ b/src/tafnode.cpp @@ -1,72 +1,148 @@ #include "tafnode.h" -Bu::TafNode::TafNode() +Bu::TafNode::TafNode( NodeType eType ) : + eType( eType ) { } Bu::TafNode::~TafNode() +{ +} + +const Bu::TafNode::NodeType Bu::TafNode::getType() const +{ + return eType; +} + +/* +const Bu::TafNode::PropList &Bu::TafNode::getProperties( const Bu::FString &sName ) const +{ + return hProp.get( sName ); +} + +const Bu::TafNode::NodeList &Bu::TafNode::getChildren( const Bu::FString &sName ) const +{ + return hChildren.get( sName ); +} + +const Bu::FString &Bu::TafNode::getProperty( const Bu::FString &sName ) const +{ + return getProperties( sName ).first(); +} + +const Bu::TafNode *Bu::TafNode::getChild( const Bu::FString &sName ) const +{ + return getChildren( sName ).first(); +} +*/ + +Bu::TafGroup::TafGroup( const Bu::FString &sName ) : + TafNode( typeGroup ), + sName( sName ) +{ +} + +Bu::TafGroup::~TafGroup() { //printf("Entering Bu::TafNode::~TafNode() \"%s\"\n", sName.getStr() ); - for( NodeHash::iterator i = hChildren.begin(); i != hChildren.end(); i++ ) + for( NodeList::iterator i = lChildren.begin(); i != lChildren.end(); i++ ) { - NodeList &l = i.getValue(); - for( NodeList::iterator k = l.begin(); k != l.end(); k++ ) - { - //printf("deleting: [%08X] %s\n", *k, "" );//(*k)->getName().getStr() ); - delete (*k); - } + delete (*i); } } -void Bu::TafNode::setProperty( Bu::FString sName, Bu::FString sValue ) +const Bu::FString &Bu::TafGroup::getName() const { - if( !hProp.has( sName ) ) + return sName; +} + +void Bu::TafGroup::addChild( Bu::TafNode *pNode ) +{ + switch( pNode->getType() ) { - hProp.insert( sName, PropList() ); + case typeGroup: + { + TafGroup *pGroup = (TafGroup *)pNode; + if( !hChildren.has( pGroup->getName() ) ) + hChildren.insert( pGroup->getName(), GroupList() ); + hChildren.get( pGroup->getName() ).append( pGroup ); + } + break; + + case typeProperty: + { + TafProperty *pProperty = (TafProperty *)pNode; + if( !hProp.has( pProperty->getName() ) ) + hProp.insert( pProperty->getName(), PropList() ); + hProp.get( pProperty->getName() ).append( pProperty->getValue() ); + } + break; + + case typeComment: + break; } - hProp.get( sName ).append( sValue ); + lChildren.append( pNode ); } -void Bu::TafNode::addChild( TafNode *pNode ) +const Bu::TafGroup::GroupList &Bu::TafGroup::getChildren( const Bu::FString &sName ) const { - if( !hChildren.has( pNode->getName() ) ) - { - hChildren.insert( pNode->getName(), NodeList() ); - } + return hChildren.get( sName ); +} - //printf("Appending \"%s\"\n", pNode->getName().getStr() ); - hChildren.get( pNode->getName() ).append( pNode ); - //printf("[%08X]\n", hChildren.get( pNode->getName() ).last() ); +const Bu::TafGroup::NodeList &Bu::TafGroup::getChildren() const +{ + return lChildren; } -const Bu::TafNode::PropList &Bu::TafNode::getProperties( const Bu::FString &sName ) const +const Bu::TafGroup *Bu::TafGroup::getChild( const Bu::FString &sName ) const { - return hProp.get( sName ); + return hChildren.get( sName ).first(); } -const Bu::TafNode::NodeList &Bu::TafNode::getChildren( const Bu::FString &sName ) const +const Bu::TafGroup::PropList &Bu::TafGroup::getProperties( const Bu::FString &sName ) const { - return hChildren.get( sName ); + return hProp.get( sName ); } -const Bu::FString &Bu::TafNode::getProperty( const Bu::FString &sName ) const +const Bu::FString &Bu::TafGroup::getProperty( const Bu::FString &sName ) const { - return getProperties( sName ).first(); + return hProp.get( sName ).first(); } -const Bu::TafNode *Bu::TafNode::getChild( const Bu::FString &sName ) const +Bu::TafProperty::TafProperty( const Bu::FString &sName, const Bu::FString &sValue ) : + TafNode( typeProperty ), + sName( sName ), + sValue( sValue ) { - return getChildren( sName ).first(); } -void Bu::TafNode::setName( const Bu::FString &sName ) +Bu::TafProperty::~TafProperty() { - this->sName = sName; } -const Bu::FString &Bu::TafNode::getName() const +const Bu::FString &Bu::TafProperty::getName() const { return sName; } +const Bu::FString &Bu::TafProperty::getValue() const +{ + return sValue; +} + +Bu::TafComment::TafComment( const Bu::FString &sText ) : + TafNode( typeComment ), + sText( sText ) +{ +} + +Bu::TafComment::~TafComment() +{ +} + +const Bu::FString &Bu::TafComment::getText() const +{ + return sText; +} + diff --git a/src/tafnode.h b/src/tafnode.h index 08f78e8..55a5123 100644 --- a/src/tafnode.h +++ b/src/tafnode.h @@ -14,29 +14,77 @@ namespace Bu class TafNode { public: - typedef Bu::List PropList; - typedef Bu::Hash PropHash; - typedef Bu::List NodeList; - typedef Bu::Hash NodeHash; + enum NodeType + { + typeGroup, + typeProperty, + typeComment + }; public: - TafNode(); + TafNode( NodeType eType ); virtual ~TafNode(); - void setName( const Bu::FString &sName ); + const NodeType getType() const; + + private: + NodeType eType; + }; + + class TafProperty; + class TafComment; + class TafGroup : public TafNode + { + public: + typedef Bu::List PropList; + typedef Bu::Hash PropHash; + typedef Bu::List GroupList; + typedef Bu::Hash GroupHash; + typedef Bu::List NodeList; + + TafGroup( const Bu::FString &sName ); + virtual ~TafGroup(); + const Bu::FString &getName() const; - void setProperty( Bu::FString sName, Bu::FString sValue ); const Bu::FString &getProperty( const Bu::FString &sName ) const; const PropList &getProperties( const Bu::FString &sName ) const; - const TafNode *getChild( const Bu::FString &sName ) const; - const NodeList &getChildren( const Bu::FString &sName ) const; + const TafGroup *getChild( const Bu::FString &sName ) const; + const GroupList &getChildren( const Bu::FString &sName ) const; void addChild( TafNode *pNode ); + const NodeList &getChildren() const; private: Bu::FString sName; PropHash hProp; - NodeHash hChildren; + GroupHash hChildren; + NodeList lChildren; + }; + + class TafProperty : public TafNode + { + public: + TafProperty( const Bu::FString &sName, const Bu::FString &sValue ); + virtual ~TafProperty(); + + const Bu::FString &getName() const; + const Bu::FString &getValue() const; + + private: + Bu::FString sName; + Bu::FString sValue; + }; + + class TafComment : public TafNode + { + public: + TafComment( const Bu::FString &sText ); + virtual ~TafComment(); + + const Bu::FString &getText() const; + + private: + Bu::FString sText; }; } diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 1187176..db465e9 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -8,6 +8,7 @@ Bu::TafReader::TafReader( Bu::Stream &sIn ) : c( 0 ), sIn( sIn ) { + next(); next(); } Bu::TafReader::~TafReader() @@ -15,60 +16,74 @@ Bu::TafReader::~TafReader() } -Bu::TafNode *Bu::TafReader::getNode() +Bu::TafGroup *Bu::TafReader::readGroup() { - if( c == 0 ) next(); - TafNode *pNode = new TafNode(); ws(); if( c != '{' ) throw TafException("Expected '{'"); next(); ws(); FString sName = readStr(); - pNode->setName( sName ); + TafGroup *pGroup = new TafGroup( sName ); next(); //printf("Node[%s]:\n", sName.getStr() ); - nodeContent( pNode ); + groupContent( pGroup ); if( c != '}' ) throw TafException("Expected '}'"); next(); - return pNode; + return pGroup; } -void Bu::TafReader::nodeContent( Bu::TafNode *pNode ) +void Bu::TafReader::groupContent( Bu::TafGroup *pGroup ) { for(;;) { ws(); if( c == '{' ) - pNode->addChild( getNode() ); + pGroup->addChild( readGroup() ); else if( c == '}' ) return; + else if( c == '/' && la == '*' ) + pGroup->addChild( readComment() ); else - nodeProperty( pNode ); + pGroup->addChild( readProperty() ); } } -void Bu::TafReader::nodeProperty( Bu::TafNode *pNode ) +Bu::TafProperty *Bu::TafReader::readProperty() { FString sName = readStr(); ws(); if( c != '=' ) { //printf(" %s (true)\n", sName.getStr() ); - pNode->setProperty( sName, "" ); - return; + return new Bu::TafProperty( sName, "" ); } next(); FString sValue = readStr(); - pNode->setProperty( sName, sValue ); + return new Bu::TafProperty( sName, sValue ); //printf(" %s = %s\n", sName.getStr(), sValue.getStr() ); } +Bu::TafComment *Bu::TafReader::readComment() +{ + next(); + FString sCmnt; + for(;;) + { + next(); + if( c == '*' && la == '/' ) + break; + sCmnt += c; + } + + return new TafComment( sCmnt ); +} + Bu::FString Bu::TafReader::readStr() { ws(); @@ -134,6 +149,7 @@ bool Bu::TafReader::isws() void Bu::TafReader::next() { - sIn.read( &c, 1 ); + c = la; + sIn.read( &la, 1 ); } diff --git a/src/tafreader.h b/src/tafreader.h index 47ae187..eeaafb3 100644 --- a/src/tafreader.h +++ b/src/tafreader.h @@ -17,16 +17,17 @@ namespace Bu TafReader( Bu::Stream &sIn ); virtual ~TafReader(); - Bu::TafNode *getNode(); + Bu::TafGroup *readGroup(); private: - void nodeContent( Bu::TafNode *pNode ); - void nodeProperty( Bu::TafNode *pNode ); + void groupContent( Bu::TafGroup *pNode ); + Bu::TafProperty *readProperty(); + Bu::TafComment *readComment(); void ws(); bool isws(); void next(); Bu::FString readStr(); - char c; + char c, la; Bu::Stream &sIn; }; } diff --git a/src/tafwriter.cpp b/src/tafwriter.cpp index ac42d3d..6b505ef 100644 --- a/src/tafwriter.cpp +++ b/src/tafwriter.cpp @@ -9,16 +9,54 @@ Bu::TafWriter::~TafWriter() { } -void Bu::TafWriter::writeNode( Bu::TafNode *pRoot ) +void Bu::TafWriter::writeGroup( const Bu::TafGroup *pRoot ) { sOut.write("{", 1 ); - writeString( pRoot->getName().getStr() ); + writeString( pRoot->getName() ); sOut.write(": ", 2 ); + const Bu::TafGroup::NodeList &nl = pRoot->getChildren(); + for( Bu::TafGroup::NodeList::const_iterator i = nl.begin(); i != nl.end(); i++ ) + { + switch( (*i)->getType() ) + { + case Bu::TafNode::typeGroup: + writeGroup( (Bu::TafGroup *)(*i) ); + break; + + case Bu::TafNode::typeProperty: + writeProperty( (Bu::TafProperty *)(*i) ); + break; + + case Bu::TafNode::typeComment: + writeComment( (Bu::TafComment *)(*i) ); + break; + } + } sOut.write("}", 1 ); } +void Bu::TafWriter::writeProperty( const Bu::TafProperty *pProp ) +{ + writeString( pProp->getName() ); + if( pProp->getValue().getStr() != NULL ) + { + sOut.write("=", 1 ); + writeString( pProp->getValue() ); + } + sOut.write(" ", 1 ); +} + +void Bu::TafWriter::writeComment( const Bu::TafComment *pComment ) +{ + sOut.write("/*", 2 ); + sOut.write( pComment->getText().getStr(), pComment->getText().getSize() ); + sOut.write("*/ ", 3 ); +} + void Bu::TafWriter::writeString( const Bu::FString &str ) { + if( str.getStr() == NULL ) + return; sOut.write("\"", 1 ); for( const char *s = str.getStr(); *s; s++ ) { diff --git a/src/tafwriter.h b/src/tafwriter.h index 4310e62..5f80504 100644 --- a/src/tafwriter.h +++ b/src/tafwriter.h @@ -17,9 +17,11 @@ namespace Bu TafWriter( Bu::Stream &sOut ); virtual ~TafWriter(); - void writeNode( Bu::TafNode *pRoot ); + void writeGroup( const Bu::TafGroup *pRoot ); private: + void writeProperty( const Bu::TafProperty *pProp ); + void writeComment( const Bu::TafComment *pComment ); void writeString( const Bu::FString &str ); Bu::Stream &sOut; }; diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp index d135e78..e3da120 100644 --- a/src/tests/taf.cpp +++ b/src/tests/taf.cpp @@ -1,4 +1,5 @@ #include "bu/tafreader.h" +#include "bu/tafwriter.h" #include "bu/file.h" int main() @@ -6,12 +7,18 @@ int main() Bu::File f("test.taf", "rb"); Bu::TafReader tr( f ); - Bu::TafNode *pNode = tr.getNode(); + Bu::TafGroup *pGroup = tr.readGroup(); - const Bu::TafNode *pStats = pNode->getChild("stats"); + const Bu::TafGroup *pStats = pGroup->getChild("stats"); printf("%s\n", pStats->getName().getStr() ); printf(" str = %s\n", pStats->getProperty("str").getStr() ); - delete pNode; + { + Bu::File fo("out.taf", "wb"); + Bu::TafWriter tw( fo ); + tw.writeGroup( pGroup ); + } + + delete pGroup; } diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp index ab485d0..5e0e914 100644 --- a/src/unit/taf.cpp +++ b/src/unit/taf.cpp @@ -32,7 +32,7 @@ public: Bu::File fIn(sFnTmp.c_str(), "rb"); Bu::TafReader tr(fIn); - Bu::TafNode *tn = tr.getNode(); + Bu::TafGroup *tn = tr.readGroup(); unitTest( !strcmp("Bob", tn->getProperty("name").c_str()) ); delete tn; diff --git a/test.taf b/test.taf index 045b042..1d4e7d1 100644 --- a/test.taf +++ b/test.taf @@ -1,26 +1,7 @@ -{player: - password = "aoeuaoeuao" - userclass = "implementor" - species = "human" - sex = "male" - active - startroom = "Salourn::Xagafinelle's Room" - {stats: str=14 dex=12 spd=12 enr=7 rea=12 wil=10 int=13 cha=14} - {hp: cur = 100 max = 100} - {en: cur = 100 max = 100} - attackrate = 30 - gold = 0 - {inventory: - {: count=1 id="Salourn::Dark Blade"} - {: count=1 id="Salourn::Dark Suit"} - {: count=3 id="Salourn::Small Fig"} - } - {aliases: - {: key="." value="say"} - {: key="," value="yell"} - {: key="li" value="lightning"} - } - description = "They appear to be rather average looking, not particularly +{"player": "password"="aoeuaoeuao" "userclass"="implementor" "species"="human" "sex"="male" "active" "startroom"="Salourn::Xagafinelle's Room" {"stats": "str"="14" "dex"="12" "spd"="12" "enr"="7" "rea"="12" "wil"="10" "int"="13" "cha"="14" }{"hp": "cur"="100" "max"="100" }{"en": "cur"="100" "max"="100" }"attackrate"="30" "gold"="0" + + /* Hey, the inventory is next...isn't that cool? Oooooh yeah! */ + +{"inventory": {: "count"="1" "id"="Salourn::Dark Blade" }{: "count"="1" "id"="Salourn::Dark Suit" }{: "count"="3" "id"="Salourn::Small Fig" }}{"aliases": {: "key"="." "value"="say" }{: "key"="," "value"="yell" }{: "key"="li" "value"="lightning" }}"description"="They appear to be rather average looking, not particularly tall or short, with facial features that are difficult to remember even - seconds after witnessing them." -} + seconds after witnessing them." } -- cgit v1.2.3