summaryrefslogtreecommitdiff
path: root/src/xmlnode.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-05-11 07:51:40 +0000
committerMike Buland <eichlan@xagasoft.com>2007-05-11 07:51:40 +0000
commit033c41ed57348abb3a418166b1fb39bfad3312de (patch)
tree72edbb0b7ff35ef35e4d533bca384b4f7c986942 /src/xmlnode.cpp
parentad92dc50b7cdf7cfe086f21d19442d03a90fd05d (diff)
downloadlibbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.gz
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.bz2
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.tar.xz
libbu++-033c41ed57348abb3a418166b1fb39bfad3312de.zip
Added a list template class, seems to work pretty well for now, I may have
forgotten proper cleanup in the deconstructor, but besides that you can do almost everything you need. I'll make a slist/stack next, probably with the same basic code, just a different structure (not doubley-linked). The xml system from old-libbu++ is almost completely converted, I was going to re-write it, but this seemed easier at first, it may not have been, we'll see. It almost parses everything again, and almost outputs again, and it does use streams now. The FString is partway to doing minimum chunk allocations, so that adding single-characters will be really fast up to the minimum chunk size. I also figured out how to add this optimization without any extra variables taking up space, and it's optional in the template, which is cool. You can specify the size of the blocks (default 256 bytes), if it's 0 then they'll be like the old FString, 1 chunk per operation. The next FString update should be allowing efficient removal from the begining of the string by faking it, and simply moving a secondary base pointer ahead, and then optimizing appends after that fact to simply move the existing data around if you shouldn't have to re-allocate (alla FlexBuf). The final fun addition that I'm planning is a simple switch in the template (boolean) that will switch an FString into a thread-safe mode without changing the interface or anything that you can do with them at all. It may increasing memory usage, but they should still be better than std::strings, and totally thread-safe. The best part of that is that if it's done with a boolean template parameter and if statements that only test that parameter controlling flow, the code that you don't want (threadsafe/non-threadsafe) won't be included at all post-optimization.
Diffstat (limited to 'src/xmlnode.cpp')
-rw-r--r--src/xmlnode.cpp106
1 files changed, 32 insertions, 74 deletions
diff --git a/src/xmlnode.cpp b/src/xmlnode.cpp
index b1ed9a9..96d5850 100644
--- a/src/xmlnode.cpp
+++ b/src/xmlnode.cpp
@@ -1,53 +1,15 @@
1#include "xmlnode.h" 1#include "xmlnode.h"
2#include "hashfunctionstring.h"
3 2
4XmlNode::XmlNode( const char *sName, XmlNode *pParent, const char *sContent ) : 3XmlNode::XmlNode( const Bu::FString &sName, XmlNode *pParent ) :
5 hProperties( new HashFunctionString(), 53, false ), 4 sName( sName ),
6 hChildren( new HashFunctionString(), 53, true ) 5 pParent( pParent )
7{ 6{
8 this->pParent = pParent;
9 if( sName != NULL )
10 {
11 setName( sName );
12 }
13 if( sContent != NULL )
14 {
15 this->sPreContent = new std::string( sContent );
16 }
17 else
18 {
19 this->sPreContent = NULL;
20 }
21 nCurContent = 0;
22} 7}
23 8
24XmlNode::~XmlNode() 9XmlNode::~XmlNode()
25{ 10{
26 for( int j = 0; j < lChildren.getSize(); j++ )
27 {
28 delete (XmlNode *)lChildren[j];
29 }
30 for( int j = 0; j < lPropNames.getSize(); j++ )
31 {
32 delete (std::string *)lPropNames[j];
33 }
34 for( int j = 0; j < lPropValues.getSize(); j++ )
35 {
36 delete (std::string *)lPropValues[j];
37 }
38 for( int j = 0; j < lPostContent.getSize(); j++ )
39 {
40 if( lPostContent[j] != NULL )
41 {
42 delete (std::string *)lPostContent[j];
43 }
44 }
45 if( sPreContent )
46 {
47 delete sPreContent;
48 }
49} 11}
50 12/*
51void XmlNode::setName( const char *sName ) 13void XmlNode::setName( const char *sName )
52{ 14{
53 if( pParent ) 15 if( pParent )
@@ -120,18 +82,18 @@ const char *XmlNode::getContent( int nIndex )
120 } 82 }
121 83
122 return NULL; 84 return NULL;
123} 85}*/
124 86
125XmlNode *XmlNode::addChild( const char *sName, const char *sContent ) 87XmlNode *XmlNode::addChild( const Bu::FString &sName )
126{ 88{
127 return addChild( new XmlNode( sName, this, sContent ) ); 89 return addChild( new XmlNode( sName, this ) );
128} 90}
129 91
130XmlNode *XmlNode::addChild( XmlNode *pNode ) 92XmlNode *XmlNode::addChild( XmlNode *pNode )
131{ 93{
132 lChildren.append( pNode ); 94 Child c = { typeNode };
133 lPostContent.append( NULL ); 95 c.pNode = pNode;
134 nCurContent++; 96 lChildren.append( c );
135 pNode->pParent = this; 97 pNode->pParent = this;
136 98
137 return pNode; 99 return pNode;
@@ -142,21 +104,16 @@ XmlNode *XmlNode::getParent()
142 return pParent; 104 return pParent;
143} 105}
144 106
145void XmlNode::addProperty( const char *sName, const char *sValue ) 107void XmlNode::addProperty( const Bu::FString &sName, const Bu::FString &sValue )
146{ 108{
147 std::string *pName = new std::string( sName ); 109 hProperties.insert( sName, sValue );
148 std::string *pValue = new std::string( sValue );
149
150 hProperties.insert( pName->c_str(), pValue->c_str() );
151 lPropNames.append( pName );
152 lPropValues.append( pValue );
153} 110}
154 111
155int XmlNode::getNumProperties() 112int XmlNode::getNumProperties()
156{ 113{
157 return lPropNames.getSize(); 114 return hProperties.size();
158} 115}
159 116/*
160const char *XmlNode::getPropertyName( int nIndex ) 117const char *XmlNode::getPropertyName( int nIndex )
161{ 118{
162 std::string *tmp = ((std::string *)lPropNames[nIndex]); 119 std::string *tmp = ((std::string *)lPropNames[nIndex]);
@@ -172,15 +129,12 @@ const char *XmlNode::getProperty( int nIndex )
172 return NULL; 129 return NULL;
173 return tmp->c_str(); 130 return tmp->c_str();
174} 131}
175 132*/
176const char *XmlNode::getProperty( const char *sName ) 133Bu::FString XmlNode::getProperty( const Bu::FString &sName )
177{ 134{
178 const char *tmp = (const char *)hProperties[sName]; 135 return hProperties[sName];
179 if( tmp == NULL )
180 return NULL;
181 return tmp;
182} 136}
183 137/*
184void XmlNode::deleteProperty( int nIndex ) 138void XmlNode::deleteProperty( int nIndex )
185{ 139{
186 hProperties.del( ((std::string *)lPropNames[nIndex])->c_str() ); 140 hProperties.del( ((std::string *)lPropNames[nIndex])->c_str() );
@@ -194,29 +148,33 @@ void XmlNode::deleteProperty( int nIndex )
194 148
195bool XmlNode::hasChildren() 149bool XmlNode::hasChildren()
196{ 150{
197 return lChildren.getSize()>0; 151 return hChildren.getSize()>0;
198} 152}*/
199 153
200int XmlNode::getNumChildren() 154int XmlNode::getNumChildren()
201{ 155{
202 return lChildren.getSize(); 156 return lChildren.getSize();
203} 157}
204 158/*
205XmlNode *XmlNode::getChild( int nIndex ) 159XmlNode *XmlNode::getChild( int nIndex )
206{ 160{
207 return (XmlNode *)lChildren[nIndex]; 161 return (XmlNode *)lChildren[nIndex];
208} 162}
209 163*/
210XmlNode *XmlNode::getChild( const char *sName, int nSkip ) 164XmlNode *XmlNode::getChild( const Bu::FString &sName, int nSkip )
211{ 165{
212 return (XmlNode *)hChildren.get( sName, nSkip ); 166 if( !hChildren.has( sName ) )
167 return NULL;
168
169 Bu::List<XmlNode *>::iterator i = hChildren[sName]->begin();
170 return *i;
213} 171}
214 172
215const char *XmlNode::getName() 173Bu::FString XmlNode::getName()
216{ 174{
217 return sName.c_str(); 175 return sName;
218} 176}
219 177/*
220void XmlNode::deleteNode( int nIndex, const char *sReplacementText ) 178void XmlNode::deleteNode( int nIndex, const char *sReplacementText )
221{ 179{
222 XmlNode *xRet = detatchNode( nIndex, sReplacementText ); 180 XmlNode *xRet = detatchNode( nIndex, sReplacementText );
@@ -442,4 +400,4 @@ void XmlNode::deleteNodeKeepChildren( int nIndex )
442void XmlNode::replaceNodeWithChildren( int nIndex, XmlNode *pNewNode ) 400void XmlNode::replaceNodeWithChildren( int nIndex, XmlNode *pNewNode )
443{ 401{
444} 402}
445 403*/