[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [3078] Now a node can store attributes.
From: |
Giuseppe Scrivano |
Subject: |
[myserver-commit] [3078] Now a node can store attributes. |
Date: |
Mon, 04 May 2009 18:34:13 +0000 |
Revision: 3078
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3078
Author: gscrivano
Date: 2009-05-04 18:34:12 +0000 (Mon, 04 May 2009)
Log Message:
-----------
Now a node can store attributes.
Modified Paths:
--------------
trunk/myserver/src/conf/nodetree.cpp
trunk/myserver/src/conf/xml_conf.cpp
trunk/myserver/tests/test_nodetree.cpp
Modified: trunk/myserver/src/conf/nodetree.cpp
===================================================================
--- trunk/myserver/src/conf/nodetree.cpp 2009-05-03 20:44:54 UTC (rev
3077)
+++ trunk/myserver/src/conf/nodetree.cpp 2009-05-04 18:34:12 UTC (rev
3078)
@@ -19,6 +19,7 @@
#include <stdlib.h>
#include <string.h>
#include <string>
+#include <map>
using namespace std;
@@ -30,12 +31,14 @@
{
value = NULL;
children = NULL;
+ attrs = NULL;
}
NodeTree (T& v)
{
value = new T (v);
children = NULL;
+ attrs = NULL;
}
~NodeTree ()
@@ -52,9 +55,10 @@
}
if (value)
- {
- delete value;
- }
+ delete value;
+
+ if (attrs)
+ delete attrs;
}
void setValue (T *v)
@@ -70,6 +74,29 @@
children->push_back (child);
}
+ void addAttr (string &name, T value)
+ {
+ if (attrs == NULL)
+ attrs = new list<pair<string, T> > ();
+
+ attrs->push_back (pair<string, T> (name, value));
+ }
+
+ T* getAttr (string &name)
+ {
+ if (attrs == NULL)
+ return NULL;
+ for (typename list<pair<string, T> >::iterator it = attrs->begin ();
+ it != attrs->end ();
+ it++)
+ {
+ if ((*it).first.compare (name) == 0)
+ return &((*it).second);
+ }
+
+ return NULL;
+ }
+
bool isLeaf ()
{
return children == NULL;
@@ -86,5 +113,6 @@
}
protected:
list<NodeTree<T>*> *children;
+ list<pair<string, T> > *attrs;
T *value;
};
Modified: trunk/myserver/src/conf/xml_conf.cpp
===================================================================
--- trunk/myserver/src/conf/xml_conf.cpp 2009-05-03 20:44:54 UTC (rev
3077)
+++ trunk/myserver/src/conf/xml_conf.cpp 2009-05-04 18:34:12 UTC (rev
3078)
@@ -48,6 +48,7 @@
for(; lcur; lcur = lcur->next)
if (lcur->name && !xmlStrcmp (lcur->name, (const xmlChar *)"DEFINE"))
{
+ NodeTree<string> *node = new NodeTree<string> ();
const char *name = NULL;
const char *value = NULL;
@@ -56,14 +57,19 @@
if (!xmlStrcmp (attrs->name, (const xmlChar *)"name") &&
attrs->children && attrs->children->content)
name = (const char*)attrs->children->content;
-
- if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") &&
+ else if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") &&
attrs->children && attrs->children->content)
value = (const char*)attrs->children->content;
+ else
+ {
+ string attr ((const char*)attrs->name);
+ string valueAttr ((const char*)attrs->children->content);
+ node->addAttr (attr, valueAttr);
+ }
}
string *v = value ? new string((const char*)value) : NULL;
- NodeTree<string> *node = new NodeTree<string> ();
+
node->setValue (v);
if(name)
Modified: trunk/myserver/tests/test_nodetree.cpp
===================================================================
--- trunk/myserver/tests/test_nodetree.cpp 2009-05-03 20:44:54 UTC (rev
3077)
+++ trunk/myserver/tests/test_nodetree.cpp 2009-05-04 18:34:12 UTC (rev
3078)
@@ -28,39 +28,55 @@
{
CPPUNIT_TEST_SUITE( TestNodeTree );
CPPUNIT_TEST( testCreateDestroy );
+ CPPUNIT_TEST( testAttributes );
CPPUNIT_TEST_SUITE_END();
public:
+ NodeTree<string> *node;
+
void setUp()
{
-
+ node = new NodeTree<string> ();
}
void tearDown()
{
+ delete node;
+ }
+ void testAttributes ()
+ {
+ string name ("my_attrib");
+ string value ("foo.bar");
+ string *value2;
+
+ value2 = node->getAttr (name);
+ CPPUNIT_ASSERT_EQUAL (value2, (string*)NULL);
+
+ node->addAttr (name, value);
+
+ value2 = node->getAttr (name);
+ CPPUNIT_ASSERT (value2);
+
+ CPPUNIT_ASSERT_EQUAL (value2->compare (value), 0);
}
void testCreateDestroy ()
{
- NodeTree<string> *c = new NodeTree<string> ();
-
for (int j = 0; j < 5; j++)
{
- NodeTree<string> *cc = new NodeTree<string> ();
- c->addChild (cc);
+ NodeTree<string> *child = new NodeTree<string> ();
+ node->addChild (child);
for (int i = 0; i < 10; i++)
{
string s = "just a string";
- cc->addChild (new NodeTree<string> (s));
+ child->addChild (new NodeTree<string> (s));
}
}
- CPPUNIT_ASSERT_EQUAL (countLeafNodes (c), 50);
-
- delete c;
+ CPPUNIT_ASSERT_EQUAL (countLeafNodes (node), 50);
}
protected:
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [3078] Now a node can store attributes.,
Giuseppe Scrivano <=