aboutsummaryrefslogtreecommitdiff
path: root/src/xmlnode.cpp
blob: 96d585062ddcd6cf134ea0b203c226dd7faace42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "xmlnode.h"

XmlNode::XmlNode( const Bu::FString &sName, XmlNode *pParent ) :
	sName( sName ),
	pParent( pParent )
{
}

XmlNode::~XmlNode()
{
}
/*
void XmlNode::setName( const char *sName )
{
	if( pParent )
	{
		if( this->sName.size() == 0 )
		{
			// We're not in the hash yet, so add us
			this->sName = sName;
			pParent->hChildren.insert( this->sName.c_str(), this );
		}
		else
		{
			// Slightly more tricky, delete us, then add us...
			pParent->hChildren.del( this->sName.c_str() );
			this->sName = sName;
			pParent->hChildren.insert( this->sName.c_str(), this );
		}
	}
	else
	{
		// If we have no parent, then just set the name string, we don't need
		// to worry about hashing.
		this->sName = sName;
	}
}

void XmlNode::setContent( const char *sContent, int nIndex )
{
	if( nIndex == -1 )
	{
		nIndex = nCurContent;
	}
	if( nIndex == 0 )
	{
		if( this->sPreContent )
		{
			delete this->sPreContent;
		}
	
		this->sPreContent = new std::string( sContent );
	}
	else
	{
		nIndex--;
		if( lPostContent[nIndex] )
		{
			delete (std::string *)lPostContent[nIndex];
		}

		lPostContent.setAt( nIndex, new std::string( sContent ) );
	}
}

const char *XmlNode::getContent( int nIndex )
{
	if( nIndex == 0 )
	{
		if( sPreContent )
		{
			return sPreContent->c_str();
		}
	}
	else
	{
		nIndex--;
		if( lPostContent[nIndex] )
		{
			return ((std::string *)lPostContent[nIndex])->c_str();
		}
	}

	return NULL;
}*/

XmlNode *XmlNode::addChild( const Bu::FString &sName )
{
	return addChild( new XmlNode( sName, this ) );
}

XmlNode *XmlNode::addChild( XmlNode *pNode )
{
	Child c = { typeNode };
	c.pNode = pNode;
	lChildren.append( c );
	pNode->pParent = this;

	return pNode;
}

XmlNode *XmlNode::getParent()
{
	return pParent;
}

void XmlNode::addProperty( const Bu::FString &sName, const Bu::FString &sValue )
{
	hProperties.insert( sName, sValue );
}

int XmlNode::getNumProperties()
{
	return hProperties.size();
}
/*
const char *XmlNode::getPropertyName( int nIndex )
{
	std::string *tmp = ((std::string *)lPropNames[nIndex]);
	if( tmp == NULL )
		return NULL;
	return tmp->c_str();
}

const char *XmlNode::getProperty( int nIndex )
{
	std::string *tmp = ((std::string *)lPropValues[nIndex]);
	if( tmp == NULL )
		return NULL;
	return tmp->c_str();
}
*/
Bu::FString XmlNode::getProperty( const Bu::FString &sName )
{
	return hProperties[sName];
}
/*
void XmlNode::deleteProperty( int nIndex )
{
	hProperties.del( ((std::string *)lPropNames[nIndex])->c_str() );

	delete (std::string *)lPropNames[nIndex];
	delete (std::string *)lPropValues[nIndex];

	lPropNames.deleteAt( nIndex );
	lPropValues.deleteAt( nIndex );
}

bool XmlNode::hasChildren()
{
	return hChildren.getSize()>0;
}*/

int XmlNode::getNumChildren()
{
	return lChildren.getSize();
}
/*
XmlNode *XmlNode::getChild( int nIndex )
{
	return (XmlNode *)lChildren[nIndex];
}
*/
XmlNode *XmlNode::getChild( const Bu::FString &sName, int nSkip )
{
	if( !hChildren.has( sName ) )
		return NULL;

	Bu::List<XmlNode *>::iterator i = hChildren[sName]->begin();
	return *i;
}

Bu::FString XmlNode::getName()
{
	return sName;
}
/*
void XmlNode::deleteNode( int nIndex, const char *sReplacementText )
{
	XmlNode *xRet = detatchNode( nIndex, sReplacementText );

	if( xRet != NULL )
	{
		delete xRet;
	}
}

XmlNode *XmlNode::detatchNode( int nIndex, const char *sReplacementText )
{
	if( nIndex < 0 || nIndex >= lChildren.getSize() )
		return NULL;
	
	// The real trick when deleteing a node isn't actually deleting it, it's
	// reforming the content around the node that's now missing...hmmm...
	
	if( nIndex == 0 )
	{
		// If the index is zero we have to deal with the pre-content
		if( sReplacementText )
		{
			if( sPreContent == NULL )
			{
				sPreContent = new std::string( sReplacementText );
			}
			else
			{
				*sPreContent += sReplacementText;
			}
		}
		if( lPostContent.getSize() > 0 )
		{
			if( lPostContent[0] != NULL )
			{
				if( sPreContent == NULL )
				{
					sPreContent = new std::string(
						((std::string *)lPostContent[0])->c_str()
						);
				}
				else
				{
					*sPreContent +=
						((std::string *)lPostContent[0])->c_str();
				}
			}
			delete (std::string *)lPostContent[0];
			lPostContent.deleteAt( 0 );
		}
	}
	else
	{
		int nCont = nIndex-1;
		// If it's above zero we deal with the post-content only
		if( sReplacementText )
		{
			if( lPostContent[nCont] == NULL )
			{
				lPostContent.setAt( nCont, new std::string( sReplacementText ) );
			}
			else
			{
				*((std::string *)lPostContent[nCont]) += sReplacementText;
			}
		}
		if( lPostContent.getSize() > nIndex )
		{
			if( lPostContent[nIndex] != NULL )
			{
				if( lPostContent[nCont] == NULL )
				{
					lPostContent.setAt( nCont, new std::string(
						((std::string *)lPostContent[nIndex])->c_str()
						) );
				}
				else
				{
					*((std::string *)lPostContent[nCont]) +=
						((std::string *)lPostContent[nIndex])->c_str();
				}
			}
			delete (std::string *)lPostContent[nIndex];
			lPostContent.deleteAt( nIndex );
		}
	}

	XmlNode *xRet = (XmlNode *)lChildren[nIndex];
	hChildren.del( ((XmlNode *)lChildren[nIndex])->getName() );
	lChildren.deleteAt( nIndex );
	
	return xRet;
}

void XmlNode::replaceNode( int nIndex, XmlNode *pNewNode )
{
	if( nIndex < 0 || nIndex >= lChildren.getSize() )
		return; //TODO: throw an exception
	
	delete (XmlNode *)lChildren[nIndex];
	lChildren.setAt( nIndex, pNewNode );
	pNewNode->pParent = this;
}

XmlNode *XmlNode::getCopy()
{
	XmlNode *pNew = new XmlNode();

	pNew->sName = sName;
	if( sPreContent )
	{
		pNew->sPreContent = new std::string( sPreContent->c_str() );
	}
	else
	{
		pNew->sPreContent = NULL;
	}
	pNew->nCurContent = 0;
	
	int nSize = lPostContent.getSize();
	pNew->lPostContent.setSize( nSize );
	for( int j = 0; j < nSize; j++ )
	{
		if( lPostContent[j] )
		{
			pNew->lPostContent.setAt(
				j, new std::string(
					((std::string *)lPostContent[j])->c_str()
					)
				);
		}
		else
		{
			pNew->lPostContent.setAt( j, NULL );
		}
	}

	nSize = lChildren.getSize();
	pNew->lChildren.setSize( nSize );
	for( int j = 0; j < nSize; j++ )
	{
		XmlNode *pChild = ((XmlNode *)lChildren[j])->getCopy();
		pNew->lChildren.setAt( j, pChild );
		pChild->pParent = pNew;
		pNew->hChildren.insert( pChild->getName(), pChild );
	}

	nSize = lPropNames.getSize();
	pNew->lPropNames.setSize( nSize );
	pNew->lPropValues.setSize( nSize );
	for( int j = 0; j < nSize; j++ )
	{
		std::string *pProp = new std::string( ((std::string *)lPropNames[j])->c_str() );
		std::string *pVal = new std::string( ((std::string *)lPropValues[j])->c_str() );
		pNew->lPropNames.setAt( j, pProp );
		pNew->lPropValues.setAt( j, pVal );
		pNew->hProperties.insert( pProp->c_str(), pVal->c_str() );
		pNew->nCurContent++;
	}

	return pNew;
}

void XmlNode::deleteNodeKeepChildren( int nIndex )
{
	// This is a tricky one...we need to do some patching to keep things all
	// even...
	XmlNode *xRet = (XmlNode *)lChildren[nIndex];

	if( xRet == NULL )
	{
		return;
	}
	else
	{
		if( getContent( nIndex ) )
		{
			std::string sBuf( getContent( nIndex ) );
			sBuf += xRet->getContent( 0 );
			setContent( sBuf.c_str(), nIndex );
		}
		else
		{
			setContent( xRet->getContent( 0 ), nIndex );
		}
		
		int nSize = xRet->lChildren.getSize();
		for( int j = 0; j < nSize; j++ )
		{
			XmlNode *pCopy = ((XmlNode *)xRet->lChildren[j])->getCopy();
			pCopy->pParent = this;
			lChildren.insertBefore( pCopy, nIndex+j );
		
			if( xRet->lPostContent[j] )
			{
				lPostContent.insertBefore(
					new std::string( ((std::string *)xRet->lPostContent[j])->c_str() ),
					nIndex+j
					);
			}
			else
			{
				lPostContent.insertBefore( NULL, nIndex+j );
			}
		}
		
		if( getContent( nIndex+nSize ) )
		{
			//SString sBuf( getContent( nIndex+nSize ) );
			//sBuf.catfrom( xRet->getContent( nSize ) );
			//setContent( sBuf, nIndex+nSize );
		}
		else
		{
			setContent( xRet->getContent( nSize ), nIndex+nSize );
		}

		deleteNode( nIndex+nSize );
	}
}

void XmlNode::replaceNodeWithChildren( int nIndex, XmlNode *pNewNode )
{
}
*/