U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1 | #include "tinyxml2.h"
|
| 2 |
|
| 3 | #include <string.h>
|
| 4 | #include <stdlib.h>
|
| 5 | #include <stdio.h>
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 6 | #include <ctype.h>
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 7 |
|
| 8 | using namespace tinyxml2;
|
| 9 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 10 | static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 11 | static const char LF = LINE_FEED;
|
| 12 | static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
|
| 13 | static const char CR = CARRIAGE_RETURN;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 14 | static const char SINGLE_QUOTE = '\'';
|
| 15 | static const char DOUBLE_QUOTE = '\"';
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 16 |
|
| 17 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 18 | // --------- CharBuffer ----------- //
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 19 | /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
|
| 20 | {
|
| 21 | size_t len = strlen( in );
|
| 22 | size_t size = len + sizeof( CharBuffer );
|
| 23 | CharBuffer* cb = (CharBuffer*) malloc( size );
|
| 24 | cb->length = len;
|
| 25 | strcpy( cb->mem, in );
|
| 26 | return cb;
|
| 27 | }
|
| 28 |
|
| 29 |
|
| 30 | /*static*/ void CharBuffer::Free( CharBuffer* cb )
|
| 31 | {
|
| 32 | free( cb );
|
| 33 | }
|
| 34 |
|
| 35 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 36 | const char* StrPair::GetStr()
|
| 37 | {
|
| 38 | if ( flags & NEEDS_FLUSH ) {
|
| 39 | *end = 0;
|
| 40 |
|
| 41 | if ( flags & ( NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION ) ) {
|
| 42 | char* p = start;
|
| 43 | char* q = start;
|
| 44 |
|
| 45 | while( p < end ) {
|
| 46 | if ( *p == CR ) {
|
| 47 | // CR-LF pair becomes LF
|
| 48 | // CR alone becomes LF
|
| 49 | // LF-CR becomes LF
|
| 50 | if ( *(p+1) == LF ) {
|
| 51 | p += 2;
|
| 52 | }
|
| 53 | else {
|
| 54 | ++p;
|
| 55 | }
|
| 56 | *q = LF;
|
| 57 | }
|
| 58 | else if ( *p == LF ) {
|
| 59 | if ( *(p+1) == CR ) {
|
| 60 | p += 2;
|
| 61 | }
|
| 62 | else {
|
| 63 | ++p;
|
| 64 | }
|
| 65 | *q = LF;
|
| 66 | }
|
| 67 | else {
|
| 68 | *q = *p;
|
| 69 | ++p;
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | flags = 0;
|
| 74 | }
|
| 75 | return start;
|
| 76 | }
|
| 77 |
|
| 78 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 79 | // --------- XMLBase ----------- //
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 80 | char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 81 | {
|
| 82 | TIXMLASSERT( endTag && *endTag );
|
| 83 |
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 84 | char* start = p;
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 85 | char endChar = *endTag;
|
| 86 | int length = strlen( endTag );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 87 |
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 88 | // Inner loop of text parsing.
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 89 | while ( *p ) {
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 90 | if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 91 | pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 92 | break;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 93 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 94 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 95 | return p;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 96 | }
|
| 97 |
|
| 98 |
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 99 | char* XMLBase::ParseName( char* p, StrPair* pair )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 100 | {
|
| 101 | char* start = p;
|
| 102 | char* nextTag = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 103 |
|
| 104 | start = p;
|
| 105 | if ( !start || !(*start) ) {
|
| 106 | return 0;
|
| 107 | }
|
| 108 |
|
| 109 | if ( !IsAlpha( *p ) ) {
|
| 110 | return 0;
|
| 111 | }
|
| 112 |
|
| 113 | while( *p && (
|
| 114 | IsAlphaNum( (unsigned char) *p )
|
| 115 | || *p == '_'
|
| 116 | || *p == '-'
|
| 117 | || *p == '.'
|
| 118 | || *p == ':' ))
|
| 119 | {
|
| 120 | ++p;
|
| 121 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 122 |
|
| 123 | if ( p > start ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 124 | pair->Set( start, p, 0 );
|
| 125 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 126 | }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 127 | return 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 128 | }
|
| 129 |
|
| 130 |
|
| 131 | char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
| 132 | {
|
| 133 | XMLNode* returnNode = 0;
|
| 134 |
|
| 135 | p = XMLNode::SkipWhiteSpace( p );
|
| 136 | if( !p || !*p || *p != '<' )
|
| 137 | {
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | // What is this thing?
|
| 142 | // - Elements start with a letter or underscore, but xml is reserved.
|
| 143 | // - Comments: <!--
|
| 144 | // - Decleration: <?xml
|
| 145 | // - Everthing else is unknown to tinyxml.
|
| 146 | //
|
| 147 |
|
| 148 | static const char* xmlHeader = { "<?xml" };
|
| 149 | static const char* commentHeader = { "<!--" };
|
| 150 | static const char* dtdHeader = { "<!" };
|
| 151 | static const char* cdataHeader = { "<![CDATA[" };
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 152 | static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 153 |
|
| 154 | static const int xmlHeaderLen = 5;
|
| 155 | static const int commentHeaderLen = 4;
|
| 156 | static const int dtdHeaderLen = 2;
|
| 157 | static const int cdataHeaderLen = 9;
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 158 | static const int elementHeaderLen = 1;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 159 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 160 | if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 161 | returnNode = new XMLComment( document );
|
| 162 | p += commentHeaderLen;
|
| 163 | }
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 164 | else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
| 165 | returnNode = new XMLElement( document );
|
| 166 | p += elementHeaderLen;
|
| 167 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 168 | else {
|
| 169 | TIXMLASSERT( 0 );
|
| 170 | }
|
| 171 |
|
| 172 | *node = returnNode;
|
| 173 | return p;
|
| 174 | }
|
| 175 |
|
| 176 |
|
| 177 | // --------- XMLNode ----------- //
|
| 178 |
|
| 179 | XMLNode::XMLNode( XMLDocument* doc ) :
|
| 180 | document( doc ),
|
| 181 | parent( 0 ),
|
| 182 | firstChild( 0 ), lastChild( 0 ),
|
| 183 | prev( 0 ), next( 0 )
|
| 184 | {
|
| 185 |
|
| 186 | }
|
| 187 |
|
| 188 |
|
| 189 | XMLNode::~XMLNode()
|
| 190 | {
|
| 191 | XMLNode* node=firstChild;
|
| 192 | while( node ) {
|
| 193 | XMLNode* temp = node->next;
|
| 194 | delete node;
|
| 195 | node = temp;
|
| 196 | }
|
| 197 | if ( prev ) {
|
| 198 | prev->next = next;
|
| 199 | }
|
| 200 | if ( next ) {
|
| 201 | next->prev = prev;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 |
|
| 206 | XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
|
| 207 | {
|
| 208 | if ( lastChild ) {
|
| 209 | TIXMLASSERT( firstChild );
|
| 210 | TIXMLASSERT( lastChild->next == 0 );
|
| 211 | lastChild->next = addThis;
|
| 212 | addThis->prev = lastChild;
|
| 213 | lastChild = addThis;
|
| 214 |
|
| 215 | addThis->parent = this;
|
| 216 | addThis->next = 0;
|
| 217 | }
|
| 218 | else {
|
| 219 | TIXMLASSERT( firstChild == 0 );
|
| 220 | firstChild = lastChild = addThis;
|
| 221 |
|
| 222 | addThis->parent = this;
|
| 223 | addThis->prev = 0;
|
| 224 | addThis->next = 0;
|
| 225 | }
|
| 226 | return addThis;
|
| 227 | }
|
| 228 |
|
| 229 |
|
| 230 | void XMLNode::Print( FILE* fp, int depth )
|
| 231 | {
|
| 232 | for( XMLNode* node = firstChild; node; node=node->next ) {
|
| 233 | node->Print( fp, depth );
|
| 234 | }
|
| 235 | }
|
| 236 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 237 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 238 | void XMLNode::PrintSpace( FILE* fp, int depth )
|
| 239 | {
|
| 240 | for( int i=0; i<depth; ++i ) {
|
| 241 | fprintf( fp, " " );
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 248 | // --------- XMLComment ---------- //
|
| 249 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 250 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 251 | {
|
| 252 | }
|
| 253 |
|
| 254 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 255 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 256 | {
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 257 | }
|
| 258 |
|
| 259 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 260 | void XMLComment::Print( FILE* fp, int depth )
|
| 261 | {
|
| 262 | XMLNode::Print( fp, depth );
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 263 | fprintf( fp, "<!--%s-->\n", value );
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 264 | }
|
| 265 |
|
| 266 |
|
| 267 | char* XMLComment::ParseDeep( char* p )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 268 | {
|
| 269 | // Comment parses as text.
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 270 | return ParseText( p, &value, "-->" );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 271 | }
|
| 272 |
|
| 273 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 274 | // --------- XMLAttribute ---------- //
|
| 275 | char* XMLAttribute::ParseDeep( char* p )
|
| 276 | {
|
| 277 | char endTag[2] = { *p, 0 };
|
| 278 | ++p;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 279 | p = ParseText( p, &value, endTag );
|
| 280 | if ( value.Empty() ) return 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 281 | return p;
|
| 282 | }
|
| 283 |
|
| 284 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 285 | void XMLAttribute::Print( FILE* cfile )
|
| 286 | {
|
| 287 | fprintf( cfile, "\"%s\"", value );
|
| 288 | }
|
| 289 |
|
| 290 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 291 | // --------- XMLElement ---------- //
|
| 292 | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 293 | closing( false ),
|
| 294 | rootAttribute( 0 ),
|
| 295 | lastAttribute( 0 )
|
| 296 | {
|
| 297 | }
|
| 298 |
|
| 299 |
|
| 300 | XMLElement::~XMLElement()
|
| 301 | {
|
| 302 | XMLAttribute* attribute = rootAttribute;
|
| 303 | while( attribute ) {
|
| 304 | XMLAttribute* next = attribute->next;
|
| 305 | delete attribute;
|
| 306 | attribute = next;
|
| 307 | }
|
| 308 |
|
| 309 | XMLNode* child = firstChild;
|
| 310 | while( child ) {
|
| 311 | XMLNode* next = child->next;
|
| 312 | delete child;
|
| 313 | child = next;
|
| 314 | }
|
| 315 | }
|
| 316 |
|
| 317 |
|
| 318 | char* XMLElement::ParseDeep( char* p )
|
| 319 | {
|
| 320 | // Read the element name.
|
| 321 | p = SkipWhiteSpace( p );
|
| 322 | if ( !p ) return 0;
|
| 323 | const char* start = p;
|
| 324 |
|
| 325 | // The closing element is the </element> form. It is
|
| 326 | // parsed just like a regular element then deleted from
|
| 327 | // the DOM.
|
| 328 | if ( *p == '/' ) {
|
| 329 | closing = true;
|
| 330 | ++p;
|
| 331 | }
|
| 332 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 333 | p = ParseName( p, &name );
|
| 334 | if ( name.Empty() ) return 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 335 |
|
| 336 | // Read the attributes.
|
| 337 | while( p ) {
|
| 338 | p = SkipWhiteSpace( p );
|
| 339 | if ( !p || !(*p) ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 340 | document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 341 | return 0;
|
| 342 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 343 |
|
| 344 | // attribute.
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 345 | if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 346 | XMLAttribute* attrib = new XMLAttribute( this );
|
| 347 | p = attrib->ParseDeep( p );
|
| 348 | if ( !p ) {
|
| 349 | delete attrib;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 350 | document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 351 | return 0;
|
| 352 | }
|
| 353 | if ( rootAttribute ) {
|
| 354 | TIXMLASSERT( lastAttribute );
|
| 355 | lastAttribute->next = attrib;
|
| 356 | lastAttribute = attrib;
|
| 357 | }
|
| 358 | else {
|
| 359 | rootAttribute = lastAttribute = attrib;
|
| 360 | }
|
| 361 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 362 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 363 | else if ( *p == '/' && *(p+1) == '>' ) {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 364 | if ( closing ) {
|
| 365 | document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
|
| 366 | return 0;
|
| 367 | }
|
| 368 | return p+2; // done; sealed element.
|
| 369 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 370 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 371 | else if ( *p == '>' ) {
|
| 372 | ++p;
|
| 373 | break;
|
| 374 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 375 | else {
|
| 376 | document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
|
| 377 | return 0;
|
| 378 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 379 | }
|
| 380 |
|
| 381 | while( p && *p ) {
|
| 382 | XMLNode* node = 0;
|
| 383 | p = Identify( document, p, &node );
|
| 384 | if ( p && node ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 385 | p = node->ParseDeep( p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 386 |
|
| 387 | XMLElement* element = node->ToElement();
|
| 388 | if ( element && element->Closing() ) {
|
| 389 | if ( StringEqual( element->Name(), this->Name() ) ) {
|
| 390 | // All good, this is closing tag.
|
| 391 | delete node;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 392 | }
|
| 393 | else {
|
| 394 | document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
|
| 395 | delete node;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame^] | 396 | p = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 397 | }
|
| 398 | return p;
|
| 399 | }
|
| 400 | else {
|
| 401 | this->InsertEndChild( node );
|
| 402 | }
|
| 403 | }
|
| 404 | }
|
| 405 | return 0;
|
| 406 | }
|
| 407 |
|
| 408 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 409 | void XMLElement::Print( FILE* cfile, int depth )
|
| 410 | {
|
| 411 | PrintSpace( cfile, depth );
|
| 412 | fprintf( cfile, "<%s", Name() );
|
| 413 |
|
| 414 | for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
|
| 415 | fprintf( cfile, " " );
|
| 416 | attrib->Print( cfile );
|
| 417 | }
|
| 418 |
|
| 419 | if ( firstChild ) {
|
| 420 | fprintf( cfile, ">/n" );
|
| 421 | for( XMLNode* node=firstChild; node; node=node->next ) {
|
| 422 | node->Print( cfile, depth+1 );
|
| 423 | }
|
| 424 | fprintf( cfile, "</%s>", Name() );
|
| 425 | }
|
| 426 | else {
|
| 427 | fprintf( cfile, "/>\n" );
|
| 428 | }
|
| 429 | }
|
| 430 |
|
| 431 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 432 | // --------- XMLDocument ----------- //
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 433 | XMLDocument::XMLDocument() :
|
| 434 | charBuffer( 0 )
|
| 435 | {
|
Lee Thomason | 85403d8 | 2012-01-11 15:55:05 -0800 | [diff] [blame] | 436 | root = new XMLNode( this );
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 437 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 438 |
|
| 439 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 440 | XMLDocument::~XMLDocument()
|
| 441 | {
|
| 442 | delete root;
|
| 443 | delete charBuffer;
|
| 444 | }
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 | bool XMLDocument::Parse( const char* p )
|
| 449 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 450 | charBuffer = CharBuffer::Construct( p );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 451 | XMLNode* node = 0;
|
Lee Thomason | 85403d8 | 2012-01-11 15:55:05 -0800 | [diff] [blame] | 452 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 453 | char* q = Identify( this, charBuffer->mem, &node );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 454 | if ( node ) {
|
| 455 | root->InsertEndChild( node );
|
| 456 | node->ParseDeep( q );
|
| 457 | return true;
|
| 458 | }
|
| 459 | return false;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 460 | }
|
| 461 |
|
| 462 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 463 | void XMLDocument::Print( FILE* fp, int depth )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 464 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 465 | for( XMLNode* node = root->firstChild; node; node=node->next ) {
|
| 466 | node->Print( fp, depth );
|
| 467 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 468 | }
|
| 469 |
|
| 470 |
|