aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tafnode.cpp138
-rw-r--r--src/tafnode.h68
-rw-r--r--src/tafreader.cpp44
-rw-r--r--src/tafreader.h9
-rw-r--r--src/tafwriter.cpp42
-rw-r--r--src/tafwriter.h4
-rw-r--r--src/tests/taf.cpp13
-rw-r--r--src/unit/taf.cpp2
-rw-r--r--test.taf31
9 files changed, 260 insertions, 91 deletions
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 @@
1#include "tafnode.h" 1#include "tafnode.h"
2 2
3Bu::TafNode::TafNode() 3Bu::TafNode::TafNode( NodeType eType ) :
4 eType( eType )
4{ 5{
5} 6}
6 7
7Bu::TafNode::~TafNode() 8Bu::TafNode::~TafNode()
8{ 9{
10}
11
12const Bu::TafNode::NodeType Bu::TafNode::getType() const
13{
14 return eType;
15}
16
17/*
18const Bu::TafNode::PropList &Bu::TafNode::getProperties( const Bu::FString &sName ) const
19{
20 return hProp.get( sName );
21}
22
23const Bu::TafNode::NodeList &Bu::TafNode::getChildren( const Bu::FString &sName ) const
24{
25 return hChildren.get( sName );
26}
27
28const Bu::FString &Bu::TafNode::getProperty( const Bu::FString &sName ) const
29{
30 return getProperties( sName ).first();
31}
32
33const Bu::TafNode *Bu::TafNode::getChild( const Bu::FString &sName ) const
34{
35 return getChildren( sName ).first();
36}
37*/
38
39Bu::TafGroup::TafGroup( const Bu::FString &sName ) :
40 TafNode( typeGroup ),
41 sName( sName )
42{
43}
44
45Bu::TafGroup::~TafGroup()
46{
9 //printf("Entering Bu::TafNode::~TafNode() \"%s\"\n", sName.getStr() ); 47 //printf("Entering Bu::TafNode::~TafNode() \"%s\"\n", sName.getStr() );
10 for( NodeHash::iterator i = hChildren.begin(); i != hChildren.end(); i++ ) 48 for( NodeList::iterator i = lChildren.begin(); i != lChildren.end(); i++ )
11 { 49 {
12 NodeList &l = i.getValue(); 50 delete (*i);
13 for( NodeList::iterator k = l.begin(); k != l.end(); k++ )
14 {
15 //printf("deleting: [%08X] %s\n", *k, "" );//(*k)->getName().getStr() );
16 delete (*k);
17 }
18 } 51 }
19} 52}
20 53
21void Bu::TafNode::setProperty( Bu::FString sName, Bu::FString sValue ) 54const Bu::FString &Bu::TafGroup::getName() const
22{ 55{
23 if( !hProp.has( sName ) ) 56 return sName;
57}
58
59void Bu::TafGroup::addChild( Bu::TafNode *pNode )
60{
61 switch( pNode->getType() )
24 { 62 {
25 hProp.insert( sName, PropList() ); 63 case typeGroup:
64 {
65 TafGroup *pGroup = (TafGroup *)pNode;
66 if( !hChildren.has( pGroup->getName() ) )
67 hChildren.insert( pGroup->getName(), GroupList() );
68 hChildren.get( pGroup->getName() ).append( pGroup );
69 }
70 break;
71
72 case typeProperty:
73 {
74 TafProperty *pProperty = (TafProperty *)pNode;
75 if( !hProp.has( pProperty->getName() ) )
76 hProp.insert( pProperty->getName(), PropList() );
77 hProp.get( pProperty->getName() ).append( pProperty->getValue() );
78 }
79 break;
80
81 case typeComment:
82 break;
26 } 83 }
27 84
28 hProp.get( sName ).append( sValue ); 85 lChildren.append( pNode );
29} 86}
30 87
31void Bu::TafNode::addChild( TafNode *pNode ) 88const Bu::TafGroup::GroupList &Bu::TafGroup::getChildren( const Bu::FString &sName ) const
32{ 89{
33 if( !hChildren.has( pNode->getName() ) ) 90 return hChildren.get( sName );
34 { 91}
35 hChildren.insert( pNode->getName(), NodeList() );
36 }
37 92
38 //printf("Appending \"%s\"\n", pNode->getName().getStr() ); 93const Bu::TafGroup::NodeList &Bu::TafGroup::getChildren() const
39 hChildren.get( pNode->getName() ).append( pNode ); 94{
40 //printf("[%08X]\n", hChildren.get( pNode->getName() ).last() ); 95 return lChildren;
41} 96}
42 97
43const Bu::TafNode::PropList &Bu::TafNode::getProperties( const Bu::FString &sName ) const 98const Bu::TafGroup *Bu::TafGroup::getChild( const Bu::FString &sName ) const
44{ 99{
45 return hProp.get( sName ); 100 return hChildren.get( sName ).first();
46} 101}
47 102
48const Bu::TafNode::NodeList &Bu::TafNode::getChildren( const Bu::FString &sName ) const 103const Bu::TafGroup::PropList &Bu::TafGroup::getProperties( const Bu::FString &sName ) const
49{ 104{
50 return hChildren.get( sName ); 105 return hProp.get( sName );
51} 106}
52 107
53const Bu::FString &Bu::TafNode::getProperty( const Bu::FString &sName ) const 108const Bu::FString &Bu::TafGroup::getProperty( const Bu::FString &sName ) const
54{ 109{
55 return getProperties( sName ).first(); 110 return hProp.get( sName ).first();
56} 111}
57 112
58const Bu::TafNode *Bu::TafNode::getChild( const Bu::FString &sName ) const 113Bu::TafProperty::TafProperty( const Bu::FString &sName, const Bu::FString &sValue ) :
114 TafNode( typeProperty ),
115 sName( sName ),
116 sValue( sValue )
59{ 117{
60 return getChildren( sName ).first();
61} 118}
62 119
63void Bu::TafNode::setName( const Bu::FString &sName ) 120Bu::TafProperty::~TafProperty()
64{ 121{
65 this->sName = sName;
66} 122}
67 123
68const Bu::FString &Bu::TafNode::getName() const 124const Bu::FString &Bu::TafProperty::getName() const
69{ 125{
70 return sName; 126 return sName;
71} 127}
72 128
129const Bu::FString &Bu::TafProperty::getValue() const
130{
131 return sValue;
132}
133
134Bu::TafComment::TafComment( const Bu::FString &sText ) :
135 TafNode( typeComment ),
136 sText( sText )
137{
138}
139
140Bu::TafComment::~TafComment()
141{
142}
143
144const Bu::FString &Bu::TafComment::getText() const
145{
146 return sText;
147}
148
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
14 class TafNode 14 class TafNode
15 { 15 {
16 public: 16 public:
17 typedef Bu::List<Bu::FString> PropList; 17 enum NodeType
18 typedef Bu::Hash<Bu::FString, PropList> PropHash; 18 {
19 typedef Bu::List<TafNode *> NodeList; 19 typeGroup,
20 typedef Bu::Hash<Bu::FString, NodeList> NodeHash; 20 typeProperty,
21 typeComment
22 };
21 23
22 public: 24 public:
23 TafNode(); 25 TafNode( NodeType eType );
24 virtual ~TafNode(); 26 virtual ~TafNode();
25 27
26 void setName( const Bu::FString &sName ); 28 const NodeType getType() const;
29
30 private:
31 NodeType eType;
32 };
33
34 class TafProperty;
35 class TafComment;
36 class TafGroup : public TafNode
37 {
38 public:
39 typedef Bu::List<Bu::FString> PropList;
40 typedef Bu::Hash<Bu::FString, PropList> PropHash;
41 typedef Bu::List<class Bu::TafGroup *> GroupList;
42 typedef Bu::Hash<Bu::FString, GroupList> GroupHash;
43 typedef Bu::List<class Bu::TafNode *> NodeList;
44
45 TafGroup( const Bu::FString &sName );
46 virtual ~TafGroup();
47
27 const Bu::FString &getName() const; 48 const Bu::FString &getName() const;
28 49
29 void setProperty( Bu::FString sName, Bu::FString sValue );
30 const Bu::FString &getProperty( const Bu::FString &sName ) const; 50 const Bu::FString &getProperty( const Bu::FString &sName ) const;
31 const PropList &getProperties( const Bu::FString &sName ) const; 51 const PropList &getProperties( const Bu::FString &sName ) const;
32 const TafNode *getChild( const Bu::FString &sName ) const; 52 const TafGroup *getChild( const Bu::FString &sName ) const;
33 const NodeList &getChildren( const Bu::FString &sName ) const; 53 const GroupList &getChildren( const Bu::FString &sName ) const;
34 void addChild( TafNode *pNode ); 54 void addChild( TafNode *pNode );
55 const NodeList &getChildren() const;
35 56
36 private: 57 private:
37 Bu::FString sName; 58 Bu::FString sName;
38 PropHash hProp; 59 PropHash hProp;
39 NodeHash hChildren; 60 GroupHash hChildren;
61 NodeList lChildren;
62 };
63
64 class TafProperty : public TafNode
65 {
66 public:
67 TafProperty( const Bu::FString &sName, const Bu::FString &sValue );
68 virtual ~TafProperty();
69
70 const Bu::FString &getName() const;
71 const Bu::FString &getValue() const;
72
73 private:
74 Bu::FString sName;
75 Bu::FString sValue;
76 };
77
78 class TafComment : public TafNode
79 {
80 public:
81 TafComment( const Bu::FString &sText );
82 virtual ~TafComment();
83
84 const Bu::FString &getText() const;
85
86 private:
87 Bu::FString sText;
40 }; 88 };
41} 89}
42 90
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 ) :
8 c( 0 ), 8 c( 0 ),
9 sIn( sIn ) 9 sIn( sIn )
10{ 10{
11 next(); next();
11} 12}
12 13
13Bu::TafReader::~TafReader() 14Bu::TafReader::~TafReader()
@@ -15,60 +16,74 @@ Bu::TafReader::~TafReader()
15 16
16} 17}
17 18
18Bu::TafNode *Bu::TafReader::getNode() 19Bu::TafGroup *Bu::TafReader::readGroup()
19{ 20{
20 if( c == 0 ) next();
21 TafNode *pNode = new TafNode();
22 ws(); 21 ws();
23 if( c != '{' ) 22 if( c != '{' )
24 throw TafException("Expected '{'"); 23 throw TafException("Expected '{'");
25 next(); 24 next();
26 ws(); 25 ws();
27 FString sName = readStr(); 26 FString sName = readStr();
28 pNode->setName( sName ); 27 TafGroup *pGroup = new TafGroup( sName );
29 next(); 28 next();
30 //printf("Node[%s]:\n", sName.getStr() ); 29 //printf("Node[%s]:\n", sName.getStr() );
31 30
32 nodeContent( pNode ); 31 groupContent( pGroup );
33 32
34 if( c != '}' ) 33 if( c != '}' )
35 throw TafException("Expected '}'"); 34 throw TafException("Expected '}'");
36 35
37 next(); 36 next();
38 37
39 return pNode; 38 return pGroup;
40} 39}
41 40
42void Bu::TafReader::nodeContent( Bu::TafNode *pNode ) 41void Bu::TafReader::groupContent( Bu::TafGroup *pGroup )
43{ 42{
44 for(;;) 43 for(;;)
45 { 44 {
46 ws(); 45 ws();
47 if( c == '{' ) 46 if( c == '{' )
48 pNode->addChild( getNode() ); 47 pGroup->addChild( readGroup() );
49 else if( c == '}' ) 48 else if( c == '}' )
50 return; 49 return;
50 else if( c == '/' && la == '*' )
51 pGroup->addChild( readComment() );
51 else 52 else
52 nodeProperty( pNode ); 53 pGroup->addChild( readProperty() );
53 } 54 }
54} 55}
55 56
56void Bu::TafReader::nodeProperty( Bu::TafNode *pNode ) 57Bu::TafProperty *Bu::TafReader::readProperty()
57{ 58{
58 FString sName = readStr(); 59 FString sName = readStr();
59 ws(); 60 ws();
60 if( c != '=' ) 61 if( c != '=' )
61 { 62 {
62 //printf(" %s (true)\n", sName.getStr() ); 63 //printf(" %s (true)\n", sName.getStr() );
63 pNode->setProperty( sName, "" ); 64 return new Bu::TafProperty( sName, "" );
64 return;
65 } 65 }
66 next(); 66 next();
67 FString sValue = readStr(); 67 FString sValue = readStr();
68 pNode->setProperty( sName, sValue ); 68 return new Bu::TafProperty( sName, sValue );
69 //printf(" %s = %s\n", sName.getStr(), sValue.getStr() ); 69 //printf(" %s = %s\n", sName.getStr(), sValue.getStr() );
70} 70}
71 71
72Bu::TafComment *Bu::TafReader::readComment()
73{
74 next();
75 FString sCmnt;
76 for(;;)
77 {
78 next();
79 if( c == '*' && la == '/' )
80 break;
81 sCmnt += c;
82 }
83
84 return new TafComment( sCmnt );
85}
86
72Bu::FString Bu::TafReader::readStr() 87Bu::FString Bu::TafReader::readStr()
73{ 88{
74 ws(); 89 ws();
@@ -134,6 +149,7 @@ bool Bu::TafReader::isws()
134 149
135void Bu::TafReader::next() 150void Bu::TafReader::next()
136{ 151{
137 sIn.read( &c, 1 ); 152 c = la;
153 sIn.read( &la, 1 );
138} 154}
139 155
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
17 TafReader( Bu::Stream &sIn ); 17 TafReader( Bu::Stream &sIn );
18 virtual ~TafReader(); 18 virtual ~TafReader();
19 19
20 Bu::TafNode *getNode(); 20 Bu::TafGroup *readGroup();
21 21
22 private: 22 private:
23 void nodeContent( Bu::TafNode *pNode ); 23 void groupContent( Bu::TafGroup *pNode );
24 void nodeProperty( Bu::TafNode *pNode ); 24 Bu::TafProperty *readProperty();
25 Bu::TafComment *readComment();
25 void ws(); 26 void ws();
26 bool isws(); 27 bool isws();
27 void next(); 28 void next();
28 Bu::FString readStr(); 29 Bu::FString readStr();
29 char c; 30 char c, la;
30 Bu::Stream &sIn; 31 Bu::Stream &sIn;
31 }; 32 };
32} 33}
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()
9{ 9{
10} 10}
11 11
12void Bu::TafWriter::writeNode( Bu::TafNode *pRoot ) 12void Bu::TafWriter::writeGroup( const Bu::TafGroup *pRoot )
13{ 13{
14 sOut.write("{", 1 ); 14 sOut.write("{", 1 );
15 writeString( pRoot->getName().getStr() ); 15 writeString( pRoot->getName() );
16 sOut.write(": ", 2 ); 16 sOut.write(": ", 2 );
17 const Bu::TafGroup::NodeList &nl = pRoot->getChildren();
18 for( Bu::TafGroup::NodeList::const_iterator i = nl.begin(); i != nl.end(); i++ )
19 {
20 switch( (*i)->getType() )
21 {
22 case Bu::TafNode::typeGroup:
23 writeGroup( (Bu::TafGroup *)(*i) );
24 break;
25
26 case Bu::TafNode::typeProperty:
27 writeProperty( (Bu::TafProperty *)(*i) );
28 break;
29
30 case Bu::TafNode::typeComment:
31 writeComment( (Bu::TafComment *)(*i) );
32 break;
33 }
34 }
17 sOut.write("}", 1 ); 35 sOut.write("}", 1 );
18} 36}
19 37
38void Bu::TafWriter::writeProperty( const Bu::TafProperty *pProp )
39{
40 writeString( pProp->getName() );
41 if( pProp->getValue().getStr() != NULL )
42 {
43 sOut.write("=", 1 );
44 writeString( pProp->getValue() );
45 }
46 sOut.write(" ", 1 );
47}
48
49void Bu::TafWriter::writeComment( const Bu::TafComment *pComment )
50{
51 sOut.write("/*", 2 );
52 sOut.write( pComment->getText().getStr(), pComment->getText().getSize() );
53 sOut.write("*/ ", 3 );
54}
55
20void Bu::TafWriter::writeString( const Bu::FString &str ) 56void Bu::TafWriter::writeString( const Bu::FString &str )
21{ 57{
58 if( str.getStr() == NULL )
59 return;
22 sOut.write("\"", 1 ); 60 sOut.write("\"", 1 );
23 for( const char *s = str.getStr(); *s; s++ ) 61 for( const char *s = str.getStr(); *s; s++ )
24 { 62 {
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
17 TafWriter( Bu::Stream &sOut ); 17 TafWriter( Bu::Stream &sOut );
18 virtual ~TafWriter(); 18 virtual ~TafWriter();
19 19
20 void writeNode( Bu::TafNode *pRoot ); 20 void writeGroup( const Bu::TafGroup *pRoot );
21 21
22 private: 22 private:
23 void writeProperty( const Bu::TafProperty *pProp );
24 void writeComment( const Bu::TafComment *pComment );
23 void writeString( const Bu::FString &str ); 25 void writeString( const Bu::FString &str );
24 Bu::Stream &sOut; 26 Bu::Stream &sOut;
25 }; 27 };
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 @@
1#include "bu/tafreader.h" 1#include "bu/tafreader.h"
2#include "bu/tafwriter.h"
2#include "bu/file.h" 3#include "bu/file.h"
3 4
4int main() 5int main()
@@ -6,12 +7,18 @@ int main()
6 Bu::File f("test.taf", "rb"); 7 Bu::File f("test.taf", "rb");
7 Bu::TafReader tr( f ); 8 Bu::TafReader tr( f );
8 9
9 Bu::TafNode *pNode = tr.getNode(); 10 Bu::TafGroup *pGroup = tr.readGroup();
10 11
11 const Bu::TafNode *pStats = pNode->getChild("stats"); 12 const Bu::TafGroup *pStats = pGroup->getChild("stats");
12 printf("%s\n", pStats->getName().getStr() ); 13 printf("%s\n", pStats->getName().getStr() );
13 printf(" str = %s\n", pStats->getProperty("str").getStr() ); 14 printf(" str = %s\n", pStats->getProperty("str").getStr() );
14 15
15 delete pNode; 16 {
17 Bu::File fo("out.taf", "wb");
18 Bu::TafWriter tw( fo );
19 tw.writeGroup( pGroup );
20 }
21
22 delete pGroup;
16} 23}
17 24
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:
32 Bu::File fIn(sFnTmp.c_str(), "rb"); 32 Bu::File fIn(sFnTmp.c_str(), "rb");
33 Bu::TafReader tr(fIn); 33 Bu::TafReader tr(fIn);
34 34
35 Bu::TafNode *tn = tr.getNode(); 35 Bu::TafGroup *tn = tr.readGroup();
36 unitTest( !strcmp("Bob", tn->getProperty("name").c_str()) ); 36 unitTest( !strcmp("Bob", tn->getProperty("name").c_str()) );
37 delete tn; 37 delete tn;
38 38
diff --git a/test.taf b/test.taf
index 045b042..1d4e7d1 100644
--- a/test.taf
+++ b/test.taf
@@ -1,26 +1,7 @@
1{player: 1{"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"
2 password = "aoeuaoeuao" 2
3 userclass = "implementor" 3 /* Hey, the inventory is next...isn't that cool? Oooooh yeah! */
4 species = "human" 4
5 sex = "male" 5{"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
6 active
7 startroom = "Salourn::Xagafinelle's Room"
8 {stats: str=14 dex=12 spd=12 enr=7 rea=12 wil=10 int=13 cha=14}
9 {hp: cur = 100 max = 100}
10 {en: cur = 100 max = 100}
11 attackrate = 30
12 gold = 0
13 {inventory:
14 {: count=1 id="Salourn::Dark Blade"}
15 {: count=1 id="Salourn::Dark Suit"}
16 {: count=3 id="Salourn::Small Fig"}
17 }
18 {aliases:
19 {: key="." value="say"}
20 {: key="," value="yell"}
21 {: key="li" value="lightning"}
22 }
23 description = "They appear to be rather average looking, not particularly
24 tall or short, with facial features that are difficult to remember even 6 tall or short, with facial features that are difficult to remember even
25 seconds after witnessing them." 7 seconds after witnessing them." }
26}