| 1 | /* |
| 2 | www.sourceforge.net/projects/tinyxml |
| 3 | Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com) |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any |
| 7 | damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any |
| 10 | purpose, including commercial applications, and to alter it and |
| 11 | redistribute it freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must |
| 14 | not claim that you wrote the original software. If you use this |
| 15 | software in a product, an acknowledgment in the product documentation |
| 16 | would be appreciated but is not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and |
| 19 | must not be misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source |
| 22 | distribution. |
| 23 | */ |
| 24 | |
| 25 | #include <cctype> |
| 26 | #include <cstring> // Added by neoneurone on 27th november, 2007 |
| 27 | |
| 28 | #ifdef TIXML_USE_STL |
| 29 | #include <sstream> |
| 30 | #include <iostream> |
| 31 | #endif |
| 32 | |
| 33 | #include "tinyxml.h" |
| 34 | |
| 35 | |
| 36 | bool TiXmlBase::condenseWhiteSpace = true; |
| 37 | |
| 38 | void TiXmlBase::PutString( const TIXML_STRING& str, TIXML_STRING* outString ) |
| 39 | { |
| 40 | int i=0; |
| 41 | |
| 42 | while( i<(int)str.length() ) |
| 43 | { |
| 44 | unsigned char c = (unsigned char) str[i]; |
| 45 | |
| 46 | if ( c == '&' |
| 47 | && i < ( (int)str.length() - 2 ) |
| 48 | && str[i+1] == '#' |
| 49 | && str[i+2] == 'x' ) |
| 50 | { |
| 51 | // Hexadecimal character reference. |
| 52 | // Pass through unchanged. |
| 53 | // © -- copyright symbol, for example. |
| 54 | // |
| 55 | // The -1 is a bug fix from Rob Laveaux. It keeps |
| 56 | // an overflow from happening if there is no ';'. |
| 57 | // There are actually 2 ways to exit this loop - |
| 58 | // while fails (error case) and break (semicolon found). |
| 59 | // However, there is no mechanism (currently) for |
| 60 | // this function to return an error. |
| 61 | while ( i<(int)str.length()-1 ) |
| 62 | { |
| 63 | outString->append( str.c_str() + i, 1 ); |
| 64 | ++i; |
| 65 | if ( str[i] == ';' ) |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | else if ( c == '&' ) |
| 70 | { |
| 71 | outString->append( entity[0].str, entity[0].strLength ); |
| 72 | ++i; |
| 73 | } |
| 74 | else if ( c == '<' ) |
| 75 | { |
| 76 | outString->append( entity[1].str, entity[1].strLength ); |
| 77 | ++i; |
| 78 | } |
| 79 | else if ( c == '>' ) |
| 80 | { |
| 81 | outString->append( entity[2].str, entity[2].strLength ); |
| 82 | ++i; |
| 83 | } |
| 84 | else if ( c == '\"' ) |
| 85 | { |
| 86 | outString->append( entity[3].str, entity[3].strLength ); |
| 87 | ++i; |
| 88 | } |
| 89 | else if ( c == '\'' ) |
| 90 | { |
| 91 | outString->append( entity[4].str, entity[4].strLength ); |
| 92 | ++i; |
| 93 | } |
| 94 | else if ( c < 32 ) |
| 95 | { |
| 96 | // Easy pass at non-alpha/numeric/symbol |
| 97 | // Below 32 is symbolic. |
| 98 | char buf[ 32 ]; |
| 99 | |
| 100 | #if defined(TIXML_SNPRINTF) |
| 101 | TIXML_SNPRINTF( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) ); |
| 102 | #else |
| 103 | sprintf( buf, "&#x%02X;", (unsigned) ( c & 0xff ) ); |
| 104 | #endif |
| 105 | |
| 106 | //*ME: warning C4267: convert 'size_t' to 'int' |
| 107 | //*ME: Int-Cast to make compiler happy ... |
| 108 | outString->append( buf, (int)strlen( buf ) ); |
| 109 | ++i; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | //char realc = (char) c; |
| 114 | //outString->append( &realc, 1 ); |
| 115 | *outString += (char) c; // somewhat more efficient function call. |
| 116 | ++i; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | TiXmlNode::TiXmlNode( NodeType _type ) : TiXmlBase() |
| 123 | { |
| 124 | parent = 0; |
| 125 | type = _type; |
| 126 | firstChild = 0; |
| 127 | lastChild = 0; |
| 128 | prev = 0; |
| 129 | next = 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | TiXmlNode::~TiXmlNode() |
| 134 | { |
| 135 | TiXmlNode* node = firstChild; |
| 136 | TiXmlNode* temp = 0; |
| 137 | |
| 138 | while ( node ) |
| 139 | { |
| 140 | temp = node; |
| 141 | node = node->next; |
| 142 | delete temp; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void TiXmlNode::CopyTo( TiXmlNode* target ) const |
| 148 | { |
| 149 | target->SetValue (value.c_str() ); |
| 150 | target->userData = userData; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void TiXmlNode::Clear() |
| 155 | { |
| 156 | TiXmlNode* node = firstChild; |
| 157 | TiXmlNode* temp = 0; |
| 158 | |
| 159 | while ( node ) |
| 160 | { |
| 161 | temp = node; |
| 162 | node = node->next; |
| 163 | delete temp; |
| 164 | } |
| 165 | |
| 166 | firstChild = 0; |
| 167 | lastChild = 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node ) |
| 172 | { |
| 173 | assert( node->parent == 0 || node->parent == this ); |
| 174 | assert( node->GetDocument() == 0 || node->GetDocument() == this->GetDocument() ); |
| 175 | |
| 176 | if ( node->Type() == TiXmlNode::DOCUMENT ) |
| 177 | { |
| 178 | delete node; |
| 179 | if ( GetDocument() ) GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | node->parent = this; |
| 184 | |
| 185 | node->prev = lastChild; |
| 186 | node->next = 0; |
| 187 | |
| 188 | if ( lastChild ) |
| 189 | lastChild->next = node; |
| 190 | else |
| 191 | firstChild = node; // it was an empty list. |
| 192 | |
| 193 | lastChild = node; |
| 194 | return node; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis ) |
| 199 | { |
| 200 | if ( addThis.Type() == TiXmlNode::DOCUMENT ) |
| 201 | { |
| 202 | if ( GetDocument() ) GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 203 | return 0; |
| 204 | } |
| 205 | TiXmlNode* node = addThis.Clone(); |
| 206 | if ( !node ) |
| 207 | return 0; |
| 208 | |
| 209 | return LinkEndChild( node ); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | TiXmlNode* TiXmlNode::InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis ) |
| 214 | { |
| 215 | if ( !beforeThis || beforeThis->parent != this ) { |
| 216 | return 0; |
| 217 | } |
| 218 | if ( addThis.Type() == TiXmlNode::DOCUMENT ) |
| 219 | { |
| 220 | if ( GetDocument() ) GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | TiXmlNode* node = addThis.Clone(); |
| 225 | if ( !node ) |
| 226 | return 0; |
| 227 | node->parent = this; |
| 228 | |
| 229 | node->next = beforeThis; |
| 230 | node->prev = beforeThis->prev; |
| 231 | if ( beforeThis->prev ) |
| 232 | { |
| 233 | beforeThis->prev->next = node; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | assert( firstChild == beforeThis ); |
| 238 | firstChild = node; |
| 239 | } |
| 240 | beforeThis->prev = node; |
| 241 | return node; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | TiXmlNode* TiXmlNode::InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis ) |
| 246 | { |
| 247 | if ( !afterThis || afterThis->parent != this ) { |
| 248 | return 0; |
| 249 | } |
| 250 | if ( addThis.Type() == TiXmlNode::DOCUMENT ) |
| 251 | { |
| 252 | if ( GetDocument() ) GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | TiXmlNode* node = addThis.Clone(); |
| 257 | if ( !node ) |
| 258 | return 0; |
| 259 | node->parent = this; |
| 260 | |
| 261 | node->prev = afterThis; |
| 262 | node->next = afterThis->next; |
| 263 | if ( afterThis->next ) |
| 264 | { |
| 265 | afterThis->next->prev = node; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | assert( lastChild == afterThis ); |
| 270 | lastChild = node; |
| 271 | } |
| 272 | afterThis->next = node; |
| 273 | return node; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis ) |
| 278 | { |
| 279 | if ( replaceThis->parent != this ) |
| 280 | return 0; |
| 281 | |
| 282 | TiXmlNode* node = withThis.Clone(); |
| 283 | if ( !node ) |
| 284 | return 0; |
| 285 | |
| 286 | node->next = replaceThis->next; |
| 287 | node->prev = replaceThis->prev; |
| 288 | |
| 289 | if ( replaceThis->next ) |
| 290 | replaceThis->next->prev = node; |
| 291 | else |
| 292 | lastChild = node; |
| 293 | |
| 294 | if ( replaceThis->prev ) |
| 295 | replaceThis->prev->next = node; |
| 296 | else |
| 297 | firstChild = node; |
| 298 | |
| 299 | delete replaceThis; |
| 300 | node->parent = this; |
| 301 | return node; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | bool TiXmlNode::RemoveChild( TiXmlNode* removeThis ) |
| 306 | { |
| 307 | if ( removeThis->parent != this ) |
| 308 | { |
| 309 | assert( 0 ); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | if ( removeThis->next ) |
| 314 | removeThis->next->prev = removeThis->prev; |
| 315 | else |
| 316 | lastChild = removeThis->prev; |
| 317 | |
| 318 | if ( removeThis->prev ) |
| 319 | removeThis->prev->next = removeThis->next; |
| 320 | else |
| 321 | firstChild = removeThis->next; |
| 322 | |
| 323 | delete removeThis; |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | const TiXmlNode* TiXmlNode::FirstChild( const char * _value ) const |
| 328 | { |
| 329 | const TiXmlNode* node; |
| 330 | for ( node = firstChild; node; node = node->next ) |
| 331 | { |
| 332 | if ( strcmp( node->Value(), _value ) == 0 ) |
| 333 | return node; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | const TiXmlNode* TiXmlNode::LastChild( const char * _value ) const |
| 340 | { |
| 341 | const TiXmlNode* node; |
| 342 | for ( node = lastChild; node; node = node->prev ) |
| 343 | { |
| 344 | if ( strcmp( node->Value(), _value ) == 0 ) |
| 345 | return node; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | const TiXmlNode* TiXmlNode::IterateChildren( const TiXmlNode* previous ) const |
| 352 | { |
| 353 | if ( !previous ) |
| 354 | { |
| 355 | return FirstChild(); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | assert( previous->parent == this ); |
| 360 | return previous->NextSibling(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | |
| 365 | const TiXmlNode* TiXmlNode::IterateChildren( const char * val, const TiXmlNode* previous ) const |
| 366 | { |
| 367 | if ( !previous ) |
| 368 | { |
| 369 | return FirstChild( val ); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | assert( previous->parent == this ); |
| 374 | return previous->NextSibling( val ); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
| 379 | const TiXmlNode* TiXmlNode::NextSibling( const char * _value ) const |
| 380 | { |
| 381 | const TiXmlNode* node; |
| 382 | for ( node = next; node; node = node->next ) |
| 383 | { |
| 384 | if ( strcmp( node->Value(), _value ) == 0 ) |
| 385 | return node; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const |
| 392 | { |
| 393 | const TiXmlNode* node; |
| 394 | for ( node = prev; node; node = node->prev ) |
| 395 | { |
| 396 | if ( strcmp( node->Value(), _value ) == 0 ) |
| 397 | return node; |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void TiXmlElement::RemoveAttribute( const char * name ) |
| 404 | { |
| 405 | #ifdef TIXML_USE_STL |
| 406 | TIXML_STRING str( name ); |
| 407 | TiXmlAttribute* node = attributeSet.Find( str ); |
| 408 | #else |
| 409 | TiXmlAttribute* node = attributeSet.Find( name ); |
| 410 | #endif |
| 411 | if ( node ) |
| 412 | { |
| 413 | attributeSet.Remove( node ); |
| 414 | delete node; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | const TiXmlElement* TiXmlNode::FirstChildElement() const |
| 419 | { |
| 420 | const TiXmlNode* node; |
| 421 | |
| 422 | for ( node = FirstChild(); |
| 423 | node; |
| 424 | node = node->NextSibling() ) |
| 425 | { |
| 426 | if ( node->ToElement() ) |
| 427 | return node->ToElement(); |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | const TiXmlElement* TiXmlNode::FirstChildElement( const char * _value ) const |
| 434 | { |
| 435 | const TiXmlNode* node; |
| 436 | |
| 437 | for ( node = FirstChild( _value ); |
| 438 | node; |
| 439 | node = node->NextSibling( _value ) ) |
| 440 | { |
| 441 | if ( node->ToElement() ) |
| 442 | return node->ToElement(); |
| 443 | } |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | const TiXmlElement* TiXmlNode::NextSiblingElement() const |
| 449 | { |
| 450 | const TiXmlNode* node; |
| 451 | |
| 452 | for ( node = NextSibling(); |
| 453 | node; |
| 454 | node = node->NextSibling() ) |
| 455 | { |
| 456 | if ( node->ToElement() ) |
| 457 | return node->ToElement(); |
| 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | const TiXmlElement* TiXmlNode::NextSiblingElement( const char * _value ) const |
| 464 | { |
| 465 | const TiXmlNode* node; |
| 466 | |
| 467 | for ( node = NextSibling( _value ); |
| 468 | node; |
| 469 | node = node->NextSibling( _value ) ) |
| 470 | { |
| 471 | if ( node->ToElement() ) |
| 472 | return node->ToElement(); |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | const TiXmlDocument* TiXmlNode::GetDocument() const |
| 479 | { |
| 480 | const TiXmlNode* node; |
| 481 | |
| 482 | for( node = this; node; node = node->parent ) |
| 483 | { |
| 484 | if ( node->ToDocument() ) |
| 485 | return node->ToDocument(); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | TiXmlElement::TiXmlElement (const char * _value) |
| 492 | : TiXmlNode( TiXmlNode::ELEMENT ) |
| 493 | { |
| 494 | firstChild = lastChild = 0; |
| 495 | value = _value; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | #ifdef TIXML_USE_STL |
| 500 | TiXmlElement::TiXmlElement( const std::string& _value ) |
| 501 | : TiXmlNode( TiXmlNode::ELEMENT ) |
| 502 | { |
| 503 | firstChild = lastChild = 0; |
| 504 | value = _value; |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | |
| 509 | TiXmlElement::TiXmlElement( const TiXmlElement& copy) |
| 510 | : TiXmlNode( TiXmlNode::ELEMENT ) |
| 511 | { |
| 512 | firstChild = lastChild = 0; |
| 513 | copy.CopyTo( this ); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | void TiXmlElement::operator=( const TiXmlElement& base ) |
| 518 | { |
| 519 | ClearThis(); |
| 520 | base.CopyTo( this ); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | TiXmlElement::~TiXmlElement() |
| 525 | { |
| 526 | ClearThis(); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | void TiXmlElement::ClearThis() |
| 531 | { |
| 532 | Clear(); |
| 533 | while( attributeSet.First() ) |
| 534 | { |
| 535 | TiXmlAttribute* node = attributeSet.First(); |
| 536 | attributeSet.Remove( node ); |
| 537 | delete node; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | |
| 542 | const char* TiXmlElement::Attribute( const char* name ) const |
| 543 | { |
| 544 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 545 | if ( node ) |
| 546 | return node->Value(); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | #ifdef TIXML_USE_STL |
| 552 | const std::string* TiXmlElement::Attribute( const std::string& name ) const |
| 553 | { |
| 554 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 555 | if ( node ) |
| 556 | return &node->ValueStr(); |
| 557 | return 0; |
| 558 | } |
| 559 | #endif |
| 560 | |
| 561 | |
| 562 | const char* TiXmlElement::Attribute( const char* name, int* i ) const |
| 563 | { |
| 564 | const char* s = Attribute( name ); |
| 565 | if ( i ) |
| 566 | { |
| 567 | if ( s ) { |
| 568 | *i = atoi( s ); |
| 569 | } |
| 570 | else { |
| 571 | *i = 0; |
| 572 | } |
| 573 | } |
| 574 | return s; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | #ifdef TIXML_USE_STL |
| 579 | const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const |
| 580 | { |
| 581 | const std::string* s = Attribute( name ); |
| 582 | if ( i ) |
| 583 | { |
| 584 | if ( s ) { |
| 585 | *i = atoi( s->c_str() ); |
| 586 | } |
| 587 | else { |
| 588 | *i = 0; |
| 589 | } |
| 590 | } |
| 591 | return s; |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | |
| 596 | const char* TiXmlElement::Attribute( const char* name, double* d ) const |
| 597 | { |
| 598 | const char* s = Attribute( name ); |
| 599 | if ( d ) |
| 600 | { |
| 601 | if ( s ) { |
| 602 | *d = atof( s ); |
| 603 | } |
| 604 | else { |
| 605 | *d = 0; |
| 606 | } |
| 607 | } |
| 608 | return s; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | #ifdef TIXML_USE_STL |
| 613 | const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const |
| 614 | { |
| 615 | const std::string* s = Attribute( name ); |
| 616 | if ( d ) |
| 617 | { |
| 618 | if ( s ) { |
| 619 | *d = atof( s->c_str() ); |
| 620 | } |
| 621 | else { |
| 622 | *d = 0; |
| 623 | } |
| 624 | } |
| 625 | return s; |
| 626 | } |
| 627 | #endif |
| 628 | |
| 629 | |
| 630 | int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const |
| 631 | { |
| 632 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 633 | if ( !node ) |
| 634 | return TIXML_NO_ATTRIBUTE; |
| 635 | return node->QueryIntValue( ival ); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | #ifdef TIXML_USE_STL |
| 640 | int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const |
| 641 | { |
| 642 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 643 | if ( !node ) |
| 644 | return TIXML_NO_ATTRIBUTE; |
| 645 | return node->QueryIntValue( ival ); |
| 646 | } |
| 647 | #endif |
| 648 | |
| 649 | |
| 650 | int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const |
| 651 | { |
| 652 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 653 | if ( !node ) |
| 654 | return TIXML_NO_ATTRIBUTE; |
| 655 | return node->QueryDoubleValue( dval ); |
| 656 | } |
| 657 | |
| 658 | |
| 659 | #ifdef TIXML_USE_STL |
| 660 | int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const |
| 661 | { |
| 662 | const TiXmlAttribute* node = attributeSet.Find( name ); |
| 663 | if ( !node ) |
| 664 | return TIXML_NO_ATTRIBUTE; |
| 665 | return node->QueryDoubleValue( dval ); |
| 666 | } |
| 667 | #endif |
| 668 | |
| 669 | |
| 670 | void TiXmlElement::SetAttribute( const char * name, int val ) |
| 671 | { |
| 672 | char buf[64]; |
| 673 | #if defined(TIXML_SNPRINTF) |
| 674 | TIXML_SNPRINTF( buf, sizeof(buf), "%d", val ); |
| 675 | #else |
| 676 | sprintf( buf, "%d", val ); |
| 677 | #endif |
| 678 | SetAttribute( name, buf ); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | #ifdef TIXML_USE_STL |
| 683 | void TiXmlElement::SetAttribute( const std::string& name, int val ) |
| 684 | { |
| 685 | std::ostringstream oss; |
| 686 | oss << val; |
| 687 | SetAttribute( name, oss.str() ); |
| 688 | } |
| 689 | #endif |
| 690 | |
| 691 | |
| 692 | void TiXmlElement::SetDoubleAttribute( const char * name, double val ) |
| 693 | { |
| 694 | char buf[256]; |
| 695 | #if defined(TIXML_SNPRINTF) |
| 696 | TIXML_SNPRINTF( buf, sizeof(buf), "%f", val ); |
| 697 | #else |
| 698 | sprintf( buf, "%f", val ); |
| 699 | #endif |
| 700 | SetAttribute( name, buf ); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | void TiXmlElement::SetAttribute( const char * cname, const char * cvalue ) |
| 705 | { |
| 706 | #ifdef TIXML_USE_STL |
| 707 | TIXML_STRING _name( cname ); |
| 708 | TIXML_STRING _value( cvalue ); |
| 709 | #else |
| 710 | const char* _name = cname; |
| 711 | const char* _value = cvalue; |
| 712 | #endif |
| 713 | |
| 714 | TiXmlAttribute* node = attributeSet.Find( _name ); |
| 715 | if ( node ) |
| 716 | { |
| 717 | node->SetValue( _value ); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | TiXmlAttribute* attrib = new TiXmlAttribute( cname, cvalue ); |
| 722 | if ( attrib ) |
| 723 | { |
| 724 | attributeSet.Add( attrib ); |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | TiXmlDocument* document = GetDocument(); |
| 729 | if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | |
| 734 | #ifdef TIXML_USE_STL |
| 735 | void TiXmlElement::SetAttribute( const std::string& name, const std::string& _value ) |
| 736 | { |
| 737 | TiXmlAttribute* node = attributeSet.Find( name ); |
| 738 | if ( node ) |
| 739 | { |
| 740 | node->SetValue( _value ); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | TiXmlAttribute* attrib = new TiXmlAttribute( name, _value ); |
| 745 | if ( attrib ) |
| 746 | { |
| 747 | attributeSet.Add( attrib ); |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | TiXmlDocument* document = GetDocument(); |
| 752 | if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 753 | } |
| 754 | } |
| 755 | #endif |
| 756 | |
| 757 | |
| 758 | void TiXmlElement::Print( FILE* cfile, int depth ) const |
| 759 | { |
| 760 | int i; |
| 761 | assert( cfile ); |
| 762 | for ( i=0; i<depth; i++ ) { |
| 763 | fprintf( cfile, " " ); |
| 764 | } |
| 765 | |
| 766 | fprintf( cfile, "<%s", value.c_str() ); |
| 767 | |
| 768 | const TiXmlAttribute* attrib; |
| 769 | for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) |
| 770 | { |
| 771 | fprintf( cfile, " " ); |
| 772 | attrib->Print( cfile, depth ); |
| 773 | } |
| 774 | |
| 775 | // There are 3 different formatting approaches: |
| 776 | // 1) An element without children is printed as a <foo /> node |
| 777 | // 2) An element with only a text child is printed as <foo> text </foo> |
| 778 | // 3) An element with children is printed on multiple lines. |
| 779 | TiXmlNode* node; |
| 780 | if ( !firstChild ) |
| 781 | { |
| 782 | fprintf( cfile, " />" ); |
| 783 | } |
| 784 | else if ( firstChild == lastChild && firstChild->ToText() ) |
| 785 | { |
| 786 | fprintf( cfile, ">" ); |
| 787 | firstChild->Print( cfile, depth + 1 ); |
| 788 | fprintf( cfile, "</%s>", value.c_str() ); |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | fprintf( cfile, ">" ); |
| 793 | |
| 794 | for ( node = firstChild; node; node=node->NextSibling() ) |
| 795 | { |
| 796 | if ( !node->ToText() ) |
| 797 | { |
| 798 | fprintf( cfile, "\n" ); |
| 799 | } |
| 800 | node->Print( cfile, depth+1 ); |
| 801 | } |
| 802 | fprintf( cfile, "\n" ); |
| 803 | for( i=0; i<depth; ++i ) { |
| 804 | fprintf( cfile, " " ); |
| 805 | } |
| 806 | fprintf( cfile, "</%s>", value.c_str() ); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | |
| 811 | void TiXmlElement::CopyTo( TiXmlElement* target ) const |
| 812 | { |
| 813 | // superclass: |
| 814 | TiXmlNode::CopyTo( target ); |
| 815 | |
| 816 | // Element class: |
| 817 | // Clone the attributes, then clone the children. |
| 818 | const TiXmlAttribute* attribute = 0; |
| 819 | for( attribute = attributeSet.First(); |
| 820 | attribute; |
| 821 | attribute = attribute->Next() ) |
| 822 | { |
| 823 | target->SetAttribute( attribute->Name(), attribute->Value() ); |
| 824 | } |
| 825 | |
| 826 | TiXmlNode* node = 0; |
| 827 | for ( node = firstChild; node; node = node->NextSibling() ) |
| 828 | { |
| 829 | target->LinkEndChild( node->Clone() ); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | bool TiXmlElement::Accept( TiXmlVisitor* visitor ) const |
| 834 | { |
| 835 | if ( visitor->VisitEnter( *this, attributeSet.First() ) ) |
| 836 | { |
| 837 | for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() ) |
| 838 | { |
| 839 | if ( !node->Accept( visitor ) ) |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | return visitor->VisitExit( *this ); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | TiXmlNode* TiXmlElement::Clone() const |
| 848 | { |
| 849 | TiXmlElement* clone = new TiXmlElement( Value() ); |
| 850 | if ( !clone ) |
| 851 | return 0; |
| 852 | |
| 853 | CopyTo( clone ); |
| 854 | return clone; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | const char* TiXmlElement::GetText() const |
| 859 | { |
| 860 | const TiXmlNode* child = this->FirstChild(); |
| 861 | if ( child ) { |
| 862 | const TiXmlText* childText = child->ToText(); |
| 863 | if ( childText ) { |
| 864 | return childText->Value(); |
| 865 | } |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::DOCUMENT ) |
| 872 | { |
| 873 | tabsize = 4; |
| 874 | useMicrosoftBOM = false; |
| 875 | ClearError(); |
| 876 | } |
| 877 | |
| 878 | TiXmlDocument::TiXmlDocument( const char * documentName ) : TiXmlNode( TiXmlNode::DOCUMENT ) |
| 879 | { |
| 880 | tabsize = 4; |
| 881 | useMicrosoftBOM = false; |
| 882 | value = documentName; |
| 883 | ClearError(); |
| 884 | } |
| 885 | |
| 886 | |
| 887 | #ifdef TIXML_USE_STL |
| 888 | TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::DOCUMENT ) |
| 889 | { |
| 890 | tabsize = 4; |
| 891 | useMicrosoftBOM = false; |
| 892 | value = documentName; |
| 893 | ClearError(); |
| 894 | } |
| 895 | #endif |
| 896 | |
| 897 | |
| 898 | TiXmlDocument::TiXmlDocument( const TiXmlDocument& copy ) : TiXmlNode( TiXmlNode::DOCUMENT ) |
| 899 | { |
| 900 | copy.CopyTo( this ); |
| 901 | } |
| 902 | |
| 903 | |
| 904 | void TiXmlDocument::operator=( const TiXmlDocument& copy ) |
| 905 | { |
| 906 | Clear(); |
| 907 | copy.CopyTo( this ); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | bool TiXmlDocument::LoadFile( TiXmlEncoding encoding ) |
| 912 | { |
| 913 | // See STL_STRING_BUG below. |
| 914 | //StringToBuffer buf( value ); |
| 915 | |
| 916 | return LoadFile( Value(), encoding ); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | bool TiXmlDocument::SaveFile() const |
| 921 | { |
| 922 | // See STL_STRING_BUG below. |
| 923 | // StringToBuffer buf( value ); |
| 924 | // |
| 925 | // if ( buf.buffer && SaveFile( buf.buffer ) ) |
| 926 | // return true; |
| 927 | // |
| 928 | // return false; |
| 929 | return SaveFile( Value() ); |
| 930 | } |
| 931 | |
| 932 | bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding ) |
| 933 | { |
| 934 | // There was a really terrifying little bug here. The code: |
| 935 | // value = filename |
| 936 | // in the STL case, cause the assignment method of the std::string to |
| 937 | // be called. What is strange, is that the std::string had the same |
| 938 | // address as it's c_str() method, and so bad things happen. Looks |
| 939 | // like a bug in the Microsoft STL implementation. |
| 940 | // Add an extra string to avoid the crash. |
| 941 | TIXML_STRING filename( _filename ); |
| 942 | value = filename; |
| 943 | |
| 944 | // reading in binary mode so that tinyxml can normalize the EOL |
| 945 | FILE* file = fopen( value.c_str (), "rb" ); |
| 946 | |
| 947 | if ( file ) |
| 948 | { |
| 949 | bool result = LoadFile( file, encoding ); |
| 950 | fclose( file ); |
| 951 | return result; |
| 952 | } |
| 953 | else |
| 954 | { |
| 955 | SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 956 | return false; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding ) |
| 961 | { |
| 962 | if ( !file ) |
| 963 | { |
| 964 | SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | // Delete the existing data: |
| 969 | Clear(); |
| 970 | location.Clear(); |
| 971 | |
| 972 | // Get the file size, so we can pre-allocate the string. HUGE speed impact. |
| 973 | long length = 0; |
| 974 | fseek( file, 0, SEEK_END ); |
| 975 | length = ftell( file ); |
| 976 | fseek( file, 0, SEEK_SET ); |
| 977 | |
| 978 | // Strange case, but good to handle up front. |
| 979 | if ( length == 0 ) |
| 980 | { |
| 981 | SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | // If we have a file, assume it is all one big XML file, and read it in. |
| 986 | // The document parser may decide the document ends sooner than the entire file, however. |
| 987 | TIXML_STRING data; |
| 988 | data.reserve( length ); |
| 989 | |
| 990 | // Subtle bug here. TinyXml did use fgets. But from the XML spec: |
| 991 | // 2.11 End-of-Line Handling |
| 992 | // <snip> |
| 993 | // <quote> |
| 994 | // ...the XML processor MUST behave as if it normalized all line breaks in external |
| 995 | // parsed entities (including the document entity) on input, before parsing, by translating |
| 996 | // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to |
| 997 | // a single #xA character. |
| 998 | // </quote> |
| 999 | // |
| 1000 | // It is not clear fgets does that, and certainly isn't clear it works cross platform. |
| 1001 | // Generally, you expect fgets to translate from the convention of the OS to the c/unix |
| 1002 | // convention, and not work generally. |
| 1003 | |
| 1004 | /* |
| 1005 | while( fgets( buf, sizeof(buf), file ) ) |
| 1006 | { |
| 1007 | data += buf; |
| 1008 | } |
| 1009 | */ |
| 1010 | |
| 1011 | char* buf = new char[ length+1 ]; |
| 1012 | buf[0] = 0; |
| 1013 | |
| 1014 | if ( fread( buf, length, 1, file ) != 1 ) { |
| 1015 | delete [] buf; |
| 1016 | SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN ); |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | const char* lastPos = buf; |
| 1021 | const char* p = buf; |
| 1022 | |
| 1023 | buf[length] = 0; |
| 1024 | while( *p ) { |
| 1025 | assert( p < (buf+length) ); |
| 1026 | if ( *p == 0xa ) { |
| 1027 | // Newline character. No special rules for this. Append all the characters |
| 1028 | // since the last string, and include the newline. |
| 1029 | data.append( lastPos, (p-lastPos+1) ); // append, include the newline |
| 1030 | ++p; // move past the newline |
| 1031 | lastPos = p; // and point to the new buffer (may be 0) |
| 1032 | assert( p <= (buf+length) ); |
| 1033 | } |
| 1034 | else if ( *p == 0xd ) { |
| 1035 | // Carriage return. Append what we have so far, then |
| 1036 | // handle moving forward in the buffer. |
| 1037 | if ( (p-lastPos) > 0 ) { |
| 1038 | data.append( lastPos, p-lastPos ); // do not add the CR |
| 1039 | } |
| 1040 | data += (char)0xa; // a proper newline |
| 1041 | |
| 1042 | if ( *(p+1) == 0xa ) { |
| 1043 | // Carriage return - new line sequence |
| 1044 | p += 2; |
| 1045 | lastPos = p; |
| 1046 | assert( p <= (buf+length) ); |
| 1047 | } |
| 1048 | else { |
| 1049 | // it was followed by something else...that is presumably characters again. |
| 1050 | ++p; |
| 1051 | lastPos = p; |
| 1052 | assert( p <= (buf+length) ); |
| 1053 | } |
| 1054 | } |
| 1055 | else { |
| 1056 | ++p; |
| 1057 | } |
| 1058 | } |
| 1059 | // Handle any left over characters. |
| 1060 | if ( p-lastPos ) { |
| 1061 | data.append( lastPos, p-lastPos ); |
| 1062 | } |
| 1063 | delete [] buf; |
| 1064 | buf = 0; |
| 1065 | |
| 1066 | Parse( data.c_str(), 0, encoding ); |
| 1067 | |
| 1068 | if ( Error() ) |
| 1069 | return false; |
| 1070 | else |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | bool TiXmlDocument::SaveFile( const char * filename ) const |
| 1076 | { |
| 1077 | // The old c stuff lives on... |
| 1078 | FILE* fp = fopen( filename, "w" ); |
| 1079 | if ( fp ) |
| 1080 | { |
| 1081 | bool result = SaveFile( fp ); |
| 1082 | fclose( fp ); |
| 1083 | return result; |
| 1084 | } |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | bool TiXmlDocument::SaveFile( FILE* fp ) const |
| 1090 | { |
| 1091 | if ( useMicrosoftBOM ) |
| 1092 | { |
| 1093 | const unsigned char TIXML_UTF_LEAD_0 = 0xefU; |
| 1094 | const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; |
| 1095 | const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; |
| 1096 | |
| 1097 | fputc( TIXML_UTF_LEAD_0, fp ); |
| 1098 | fputc( TIXML_UTF_LEAD_1, fp ); |
| 1099 | fputc( TIXML_UTF_LEAD_2, fp ); |
| 1100 | } |
| 1101 | Print( fp, 0 ); |
| 1102 | return (ferror(fp) == 0); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | void TiXmlDocument::CopyTo( TiXmlDocument* target ) const |
| 1107 | { |
| 1108 | TiXmlNode::CopyTo( target ); |
| 1109 | |
| 1110 | target->error = error; |
| 1111 | target->errorDesc = errorDesc.c_str (); |
| 1112 | |
| 1113 | TiXmlNode* node = 0; |
| 1114 | for ( node = firstChild; node; node = node->NextSibling() ) |
| 1115 | { |
| 1116 | target->LinkEndChild( node->Clone() ); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | TiXmlNode* TiXmlDocument::Clone() const |
| 1122 | { |
| 1123 | TiXmlDocument* clone = new TiXmlDocument(); |
| 1124 | if ( !clone ) |
| 1125 | return 0; |
| 1126 | |
| 1127 | CopyTo( clone ); |
| 1128 | return clone; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | void TiXmlDocument::Print( FILE* cfile, int depth ) const |
| 1133 | { |
| 1134 | assert( cfile ); |
| 1135 | for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() ) |
| 1136 | { |
| 1137 | node->Print( cfile, depth ); |
| 1138 | fprintf( cfile, "\n" ); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const |
| 1144 | { |
| 1145 | if ( visitor->VisitEnter( *this ) ) |
| 1146 | { |
| 1147 | for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() ) |
| 1148 | { |
| 1149 | if ( !node->Accept( visitor ) ) |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | return visitor->VisitExit( *this ); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | const TiXmlAttribute* TiXmlAttribute::Next() const |
| 1158 | { |
| 1159 | // We are using knowledge of the sentinel. The sentinel |
| 1160 | // have a value or name. |
| 1161 | if ( next->value.empty() && next->name.empty() ) |
| 1162 | return 0; |
| 1163 | return next; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | TiXmlAttribute* TiXmlAttribute::Next() |
| 1168 | { |
| 1169 | // We are using knowledge of the sentinel. The sentinel |
| 1170 | // have a value or name. |
| 1171 | if ( next->value.empty() && next->name.empty() ) |
| 1172 | return 0; |
| 1173 | return next; |
| 1174 | } |
| 1175 | */ |
| 1176 | |
| 1177 | const TiXmlAttribute* TiXmlAttribute::Previous() const |
| 1178 | { |
| 1179 | // We are using knowledge of the sentinel. The sentinel |
| 1180 | // have a value or name. |
| 1181 | if ( prev->value.empty() && prev->name.empty() ) |
| 1182 | return 0; |
| 1183 | return prev; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | TiXmlAttribute* TiXmlAttribute::Previous() |
| 1188 | { |
| 1189 | // We are using knowledge of the sentinel. The sentinel |
| 1190 | // have a value or name. |
| 1191 | if ( prev->value.empty() && prev->name.empty() ) |
| 1192 | return 0; |
| 1193 | return prev; |
| 1194 | } |
| 1195 | */ |
| 1196 | |
| 1197 | void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const |
| 1198 | { |
| 1199 | TIXML_STRING n, v; |
| 1200 | |
| 1201 | PutString( name, &n ); |
| 1202 | PutString( value, &v ); |
| 1203 | |
| 1204 | if (value.find ('\"') == TIXML_STRING::npos) { |
| 1205 | if ( cfile ) { |
| 1206 | fprintf (cfile, "%s=\"%s\"", n.c_str(), v.c_str() ); |
| 1207 | } |
| 1208 | if ( str ) { |
| 1209 | (*str) += n; (*str) += "=\""; (*str) += v; (*str) += "\""; |
| 1210 | } |
| 1211 | } |
| 1212 | else { |
| 1213 | if ( cfile ) { |
| 1214 | fprintf (cfile, "%s='%s'", n.c_str(), v.c_str() ); |
| 1215 | } |
| 1216 | if ( str ) { |
| 1217 | (*str) += n; (*str) += "='"; (*str) += v; (*str) += "'"; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | int TiXmlAttribute::QueryIntValue( int* ival ) const |
| 1224 | { |
| 1225 | if ( sscanf( value.c_str(), "%d", ival ) == 1 ) |
| 1226 | return TIXML_SUCCESS; |
| 1227 | return TIXML_WRONG_TYPE; |
| 1228 | } |
| 1229 | |
| 1230 | int TiXmlAttribute::QueryDoubleValue( double* dval ) const |
| 1231 | { |
| 1232 | if ( sscanf( value.c_str(), "%lf", dval ) == 1 ) |
| 1233 | return TIXML_SUCCESS; |
| 1234 | return TIXML_WRONG_TYPE; |
| 1235 | } |
| 1236 | |
| 1237 | void TiXmlAttribute::SetIntValue( int _value ) |
| 1238 | { |
| 1239 | char buf [64]; |
| 1240 | #if defined(TIXML_SNPRINTF) |
| 1241 | TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value); |
| 1242 | #else |
| 1243 | sprintf (buf, "%d", _value); |
| 1244 | #endif |
| 1245 | SetValue (buf); |
| 1246 | } |
| 1247 | |
| 1248 | void TiXmlAttribute::SetDoubleValue( double _value ) |
| 1249 | { |
| 1250 | char buf [256]; |
| 1251 | #if defined(TIXML_SNPRINTF) |
| 1252 | TIXML_SNPRINTF( buf, sizeof(buf), "%f", _value); |
| 1253 | //Old version which causes the warning: ISO C++ does not support %lf printf format |
| 1254 | // TIXML_SNPRINTF( buf, sizeof(buf), "%lf", _value); |
| 1255 | #else |
| 1256 | sprintf (buf, "%lf", _value); |
| 1257 | #endif |
| 1258 | SetValue (buf); |
| 1259 | } |
| 1260 | |
| 1261 | int TiXmlAttribute::IntValue() const |
| 1262 | { |
| 1263 | return atoi (value.c_str ()); |
| 1264 | } |
| 1265 | |
| 1266 | double TiXmlAttribute::DoubleValue() const |
| 1267 | { |
| 1268 | return atof (value.c_str ()); |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::COMMENT ) |
| 1273 | { |
| 1274 | copy.CopyTo( this ); |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | void TiXmlComment::operator=( const TiXmlComment& base ) |
| 1279 | { |
| 1280 | Clear(); |
| 1281 | base.CopyTo( this ); |
| 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | void TiXmlComment::Print( FILE* cfile, int depth ) const |
| 1286 | { |
| 1287 | assert( cfile ); |
| 1288 | for ( int i=0; i<depth; i++ ) |
| 1289 | { |
| 1290 | fprintf( cfile, " " ); |
| 1291 | } |
| 1292 | fprintf( cfile, "<!--%s-->", value.c_str() ); |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | void TiXmlComment::CopyTo( TiXmlComment* target ) const |
| 1297 | { |
| 1298 | TiXmlNode::CopyTo( target ); |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const |
| 1303 | { |
| 1304 | return visitor->Visit( *this ); |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | TiXmlNode* TiXmlComment::Clone() const |
| 1309 | { |
| 1310 | TiXmlComment* clone = new TiXmlComment(); |
| 1311 | |
| 1312 | if ( !clone ) |
| 1313 | return 0; |
| 1314 | |
| 1315 | CopyTo( clone ); |
| 1316 | return clone; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | void TiXmlText::Print( FILE* cfile, int depth ) const |
| 1321 | { |
| 1322 | assert( cfile ); |
| 1323 | if ( cdata ) |
| 1324 | { |
| 1325 | int i; |
| 1326 | fprintf( cfile, "\n" ); |
| 1327 | for ( i=0; i<depth; i++ ) { |
| 1328 | fprintf( cfile, " " ); |
| 1329 | } |
| 1330 | fprintf( cfile, "<![CDATA[%s]]>\n", value.c_str() ); // unformatted output |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | TIXML_STRING buffer; |
| 1335 | PutString( value, &buffer ); |
| 1336 | fprintf( cfile, "%s", buffer.c_str() ); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | void TiXmlText::CopyTo( TiXmlText* target ) const |
| 1342 | { |
| 1343 | TiXmlNode::CopyTo( target ); |
| 1344 | target->cdata = cdata; |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | bool TiXmlText::Accept( TiXmlVisitor* visitor ) const |
| 1349 | { |
| 1350 | return visitor->Visit( *this ); |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | TiXmlNode* TiXmlText::Clone() const |
| 1355 | { |
| 1356 | TiXmlText* clone = 0; |
| 1357 | clone = new TiXmlText( "" ); |
| 1358 | |
| 1359 | if ( !clone ) |
| 1360 | return 0; |
| 1361 | |
| 1362 | CopyTo( clone ); |
| 1363 | return clone; |
| 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | TiXmlDeclaration::TiXmlDeclaration( const char * _version, |
| 1368 | const char * _encoding, |
| 1369 | const char * _standalone ) |
| 1370 | : TiXmlNode( TiXmlNode::DECLARATION ) |
| 1371 | { |
| 1372 | version = _version; |
| 1373 | encoding = _encoding; |
| 1374 | standalone = _standalone; |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | #ifdef TIXML_USE_STL |
| 1379 | TiXmlDeclaration::TiXmlDeclaration( const std::string& _version, |
| 1380 | const std::string& _encoding, |
| 1381 | const std::string& _standalone ) |
| 1382 | : TiXmlNode( TiXmlNode::DECLARATION ) |
| 1383 | { |
| 1384 | version = _version; |
| 1385 | encoding = _encoding; |
| 1386 | standalone = _standalone; |
| 1387 | } |
| 1388 | #endif |
| 1389 | |
| 1390 | |
| 1391 | TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy ) |
| 1392 | : TiXmlNode( TiXmlNode::DECLARATION ) |
| 1393 | { |
| 1394 | copy.CopyTo( this ); |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | void TiXmlDeclaration::operator=( const TiXmlDeclaration& copy ) |
| 1399 | { |
| 1400 | Clear(); |
| 1401 | copy.CopyTo( this ); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const |
| 1406 | { |
| 1407 | if ( cfile ) fprintf( cfile, "<?xml " ); |
| 1408 | if ( str ) (*str) += "<?xml "; |
| 1409 | |
| 1410 | if ( !version.empty() ) { |
| 1411 | if ( cfile ) fprintf (cfile, "version=\"%s\" ", version.c_str ()); |
| 1412 | if ( str ) { (*str) += "version=\""; (*str) += version; (*str) += "\" "; } |
| 1413 | } |
| 1414 | if ( !encoding.empty() ) { |
| 1415 | if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding.c_str ()); |
| 1416 | if ( str ) { (*str) += "encoding=\""; (*str) += encoding; (*str) += "\" "; } |
| 1417 | } |
| 1418 | if ( !standalone.empty() ) { |
| 1419 | if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone.c_str ()); |
| 1420 | if ( str ) { (*str) += "standalone=\""; (*str) += standalone; (*str) += "\" "; } |
| 1421 | } |
| 1422 | if ( cfile ) fprintf( cfile, "?>" ); |
| 1423 | if ( str ) (*str) += "?>"; |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const |
| 1428 | { |
| 1429 | TiXmlNode::CopyTo( target ); |
| 1430 | |
| 1431 | target->version = version; |
| 1432 | target->encoding = encoding; |
| 1433 | target->standalone = standalone; |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const |
| 1438 | { |
| 1439 | return visitor->Visit( *this ); |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | TiXmlNode* TiXmlDeclaration::Clone() const |
| 1444 | { |
| 1445 | TiXmlDeclaration* clone = new TiXmlDeclaration(); |
| 1446 | |
| 1447 | if ( !clone ) |
| 1448 | return 0; |
| 1449 | |
| 1450 | CopyTo( clone ); |
| 1451 | return clone; |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | void TiXmlUnknown::Print( FILE* cfile, int depth ) const |
| 1456 | { |
| 1457 | for ( int i=0; i<depth; i++ ) |
| 1458 | fprintf( cfile, " " ); |
| 1459 | fprintf( cfile, "<%s>", value.c_str() ); |
| 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const |
| 1464 | { |
| 1465 | TiXmlNode::CopyTo( target ); |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const |
| 1470 | { |
| 1471 | return visitor->Visit( *this ); |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | TiXmlNode* TiXmlUnknown::Clone() const |
| 1476 | { |
| 1477 | TiXmlUnknown* clone = new TiXmlUnknown(); |
| 1478 | |
| 1479 | if ( !clone ) |
| 1480 | return 0; |
| 1481 | |
| 1482 | CopyTo( clone ); |
| 1483 | return clone; |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | TiXmlAttributeSet::TiXmlAttributeSet() |
| 1488 | { |
| 1489 | sentinel.next = &sentinel; |
| 1490 | sentinel.prev = &sentinel; |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | TiXmlAttributeSet::~TiXmlAttributeSet() |
| 1495 | { |
| 1496 | assert( sentinel.next == &sentinel ); |
| 1497 | assert( sentinel.prev == &sentinel ); |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | void TiXmlAttributeSet::Add( TiXmlAttribute* addMe ) |
| 1502 | { |
| 1503 | #ifdef TIXML_USE_STL |
| 1504 | assert( !Find( TIXML_STRING( addMe->Name() ) ) ); // Shouldn't be multiply adding to the set. |
| 1505 | #else |
| 1506 | assert( !Find( addMe->Name() ) ); // Shouldn't be multiply adding to the set. |
| 1507 | #endif |
| 1508 | |
| 1509 | addMe->next = &sentinel; |
| 1510 | addMe->prev = sentinel.prev; |
| 1511 | |
| 1512 | sentinel.prev->next = addMe; |
| 1513 | sentinel.prev = addMe; |
| 1514 | } |
| 1515 | |
| 1516 | void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe ) |
| 1517 | { |
| 1518 | TiXmlAttribute* node; |
| 1519 | |
| 1520 | for( node = sentinel.next; node != &sentinel; node = node->next ) |
| 1521 | { |
| 1522 | if ( node == removeMe ) |
| 1523 | { |
| 1524 | node->prev->next = node->next; |
| 1525 | node->next->prev = node->prev; |
| 1526 | node->next = 0; |
| 1527 | node->prev = 0; |
| 1528 | return; |
| 1529 | } |
| 1530 | } |
| 1531 | assert( 0 ); // we tried to remove a non-linked attribute. |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | #ifdef TIXML_USE_STL |
| 1536 | const TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const |
| 1537 | { |
| 1538 | for( const TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next ) |
| 1539 | { |
| 1540 | if ( node->name == name ) |
| 1541 | return node; |
| 1542 | } |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) |
| 1548 | { |
| 1549 | for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next ) |
| 1550 | { |
| 1551 | if ( node->name == name ) |
| 1552 | return node; |
| 1553 | } |
| 1554 | return 0; |
| 1555 | } |
| 1556 | */ |
| 1557 | #endif |
| 1558 | |
| 1559 | |
| 1560 | const TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const |
| 1561 | { |
| 1562 | for( const TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next ) |
| 1563 | { |
| 1564 | if ( strcmp( node->name.c_str(), name ) == 0 ) |
| 1565 | return node; |
| 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) |
| 1572 | { |
| 1573 | for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next ) |
| 1574 | { |
| 1575 | if ( strcmp( node->name.c_str(), name ) == 0 ) |
| 1576 | return node; |
| 1577 | } |
| 1578 | return 0; |
| 1579 | } |
| 1580 | */ |
| 1581 | |
| 1582 | #ifdef TIXML_USE_STL |
| 1583 | std::istream& operator>> (std::istream & in, TiXmlNode & base) |
| 1584 | { |
| 1585 | TIXML_STRING tag; |
| 1586 | tag.reserve( 8 * 1000 ); |
| 1587 | base.StreamIn( &in, &tag ); |
| 1588 | |
| 1589 | base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING ); |
| 1590 | return in; |
| 1591 | } |
| 1592 | #endif |
| 1593 | |
| 1594 | |
| 1595 | #ifdef TIXML_USE_STL |
| 1596 | std::ostream& operator<< (std::ostream & out, const TiXmlNode & base) |
| 1597 | { |
| 1598 | TiXmlPrinter printer; |
| 1599 | printer.SetStreamPrinting(); |
| 1600 | base.Accept( &printer ); |
| 1601 | out << printer.Str(); |
| 1602 | |
| 1603 | return out; |
| 1604 | } |
| 1605 | |
| 1606 | |
| 1607 | std::string& operator<< (std::string& out, const TiXmlNode& base ) |
| 1608 | { |
| 1609 | TiXmlPrinter printer; |
| 1610 | printer.SetStreamPrinting(); |
| 1611 | base.Accept( &printer ); |
| 1612 | out.append( printer.Str() ); |
| 1613 | |
| 1614 | return out; |
| 1615 | } |
| 1616 | #endif |
| 1617 | |
| 1618 | |
| 1619 | TiXmlHandle TiXmlHandle::FirstChild() const |
| 1620 | { |
| 1621 | if ( node ) |
| 1622 | { |
| 1623 | TiXmlNode* child = node->FirstChild(); |
| 1624 | if ( child ) |
| 1625 | return TiXmlHandle( child ); |
| 1626 | } |
| 1627 | return TiXmlHandle( 0 ); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | TiXmlHandle TiXmlHandle::FirstChild( const char * value ) const |
| 1632 | { |
| 1633 | if ( node ) |
| 1634 | { |
| 1635 | TiXmlNode* child = node->FirstChild( value ); |
| 1636 | if ( child ) |
| 1637 | return TiXmlHandle( child ); |
| 1638 | } |
| 1639 | return TiXmlHandle( 0 ); |
| 1640 | } |
| 1641 | |
| 1642 | |
| 1643 | TiXmlHandle TiXmlHandle::FirstChildElement() const |
| 1644 | { |
| 1645 | if ( node ) |
| 1646 | { |
| 1647 | TiXmlElement* child = node->FirstChildElement(); |
| 1648 | if ( child ) |
| 1649 | return TiXmlHandle( child ); |
| 1650 | } |
| 1651 | return TiXmlHandle( 0 ); |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | TiXmlHandle TiXmlHandle::FirstChildElement( const char * value ) const |
| 1656 | { |
| 1657 | if ( node ) |
| 1658 | { |
| 1659 | TiXmlElement* child = node->FirstChildElement( value ); |
| 1660 | if ( child ) |
| 1661 | return TiXmlHandle( child ); |
| 1662 | } |
| 1663 | return TiXmlHandle( 0 ); |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | TiXmlHandle TiXmlHandle::Child( int count ) const |
| 1668 | { |
| 1669 | if ( node ) |
| 1670 | { |
| 1671 | int i; |
| 1672 | TiXmlNode* child = node->FirstChild(); |
| 1673 | for ( i=0; |
| 1674 | child && i<count; |
| 1675 | child = child->NextSibling(), ++i ) |
| 1676 | { |
| 1677 | // nothing |
| 1678 | } |
| 1679 | if ( child ) |
| 1680 | return TiXmlHandle( child ); |
| 1681 | } |
| 1682 | return TiXmlHandle( 0 ); |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | TiXmlHandle TiXmlHandle::Child( const char* value, int count ) const |
| 1687 | { |
| 1688 | if ( node ) |
| 1689 | { |
| 1690 | int i; |
| 1691 | TiXmlNode* child = node->FirstChild( value ); |
| 1692 | for ( i=0; |
| 1693 | child && i<count; |
| 1694 | child = child->NextSibling( value ), ++i ) |
| 1695 | { |
| 1696 | // nothing |
| 1697 | } |
| 1698 | if ( child ) |
| 1699 | return TiXmlHandle( child ); |
| 1700 | } |
| 1701 | return TiXmlHandle( 0 ); |
| 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | TiXmlHandle TiXmlHandle::ChildElement( int count ) const |
| 1706 | { |
| 1707 | if ( node ) |
| 1708 | { |
| 1709 | int i; |
| 1710 | TiXmlElement* child = node->FirstChildElement(); |
| 1711 | for ( i=0; |
| 1712 | child && i<count; |
| 1713 | child = child->NextSiblingElement(), ++i ) |
| 1714 | { |
| 1715 | // nothing |
| 1716 | } |
| 1717 | if ( child ) |
| 1718 | return TiXmlHandle( child ); |
| 1719 | } |
| 1720 | return TiXmlHandle( 0 ); |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | TiXmlHandle TiXmlHandle::ChildElement( const char* value, int count ) const |
| 1725 | { |
| 1726 | if ( node ) |
| 1727 | { |
| 1728 | int i; |
| 1729 | TiXmlElement* child = node->FirstChildElement( value ); |
| 1730 | for ( i=0; |
| 1731 | child && i<count; |
| 1732 | child = child->NextSiblingElement( value ), ++i ) |
| 1733 | { |
| 1734 | // nothing |
| 1735 | } |
| 1736 | if ( child ) |
| 1737 | return TiXmlHandle( child ); |
| 1738 | } |
| 1739 | return TiXmlHandle( 0 ); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | bool TiXmlPrinter::VisitEnter( const TiXmlDocument& ) |
| 1744 | { |
| 1745 | return true; |
| 1746 | } |
| 1747 | |
| 1748 | bool TiXmlPrinter::VisitExit( const TiXmlDocument& ) |
| 1749 | { |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | bool TiXmlPrinter::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute ) |
| 1754 | { |
| 1755 | DoIndent(); |
| 1756 | buffer += "<"; |
| 1757 | buffer += element.Value(); |
| 1758 | |
| 1759 | for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() ) |
| 1760 | { |
| 1761 | buffer += " "; |
| 1762 | attrib->Print( 0, 0, &buffer ); |
| 1763 | } |
| 1764 | |
| 1765 | if ( !element.FirstChild() ) |
| 1766 | { |
| 1767 | buffer += " />"; |
| 1768 | DoLineBreak(); |
| 1769 | } |
| 1770 | else |
| 1771 | { |
| 1772 | buffer += ">"; |
| 1773 | if ( element.FirstChild()->ToText() |
| 1774 | && element.LastChild() == element.FirstChild() |
| 1775 | && element.FirstChild()->ToText()->CDATA() == false ) |
| 1776 | { |
| 1777 | simpleTextPrint = true; |
| 1778 | // no DoLineBreak()! |
| 1779 | } |
| 1780 | else |
| 1781 | { |
| 1782 | DoLineBreak(); |
| 1783 | } |
| 1784 | } |
| 1785 | ++depth; |
| 1786 | return true; |
| 1787 | } |
| 1788 | |
| 1789 | |
| 1790 | bool TiXmlPrinter::VisitExit( const TiXmlElement& element ) |
| 1791 | { |
| 1792 | --depth; |
| 1793 | if ( !element.FirstChild() ) |
| 1794 | { |
| 1795 | // nothing. |
| 1796 | } |
| 1797 | else |
| 1798 | { |
| 1799 | if ( simpleTextPrint ) |
| 1800 | { |
| 1801 | simpleTextPrint = false; |
| 1802 | } |
| 1803 | else |
| 1804 | { |
| 1805 | DoIndent(); |
| 1806 | } |
| 1807 | buffer += "</"; |
| 1808 | buffer += element.Value(); |
| 1809 | buffer += ">"; |
| 1810 | DoLineBreak(); |
| 1811 | } |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | bool TiXmlPrinter::Visit( const TiXmlText& text ) |
| 1817 | { |
| 1818 | if ( text.CDATA() ) |
| 1819 | { |
| 1820 | DoIndent(); |
| 1821 | buffer += "<![CDATA["; |
| 1822 | buffer += text.Value(); |
| 1823 | buffer += "]]>"; |
| 1824 | DoLineBreak(); |
| 1825 | } |
| 1826 | else if ( simpleTextPrint ) |
| 1827 | { |
| 1828 | buffer += text.Value(); |
| 1829 | } |
| 1830 | else |
| 1831 | { |
| 1832 | DoIndent(); |
| 1833 | buffer += text.Value(); |
| 1834 | DoLineBreak(); |
| 1835 | } |
| 1836 | return true; |
| 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | bool TiXmlPrinter::Visit( const TiXmlDeclaration& declaration ) |
| 1841 | { |
| 1842 | DoIndent(); |
| 1843 | declaration.Print( 0, 0, &buffer ); |
| 1844 | DoLineBreak(); |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | bool TiXmlPrinter::Visit( const TiXmlComment& comment ) |
| 1850 | { |
| 1851 | DoIndent(); |
| 1852 | buffer += "<!--"; |
| 1853 | buffer += comment.Value(); |
| 1854 | buffer += "-->"; |
| 1855 | DoLineBreak(); |
| 1856 | return true; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | bool TiXmlPrinter::Visit( const TiXmlUnknown& unknown ) |
| 1861 | { |
| 1862 | DoIndent(); |
| 1863 | buffer += "<"; |
| 1864 | buffer += unknown.Value(); |
| 1865 | buffer += ">"; |
| 1866 | DoLineBreak(); |
| 1867 | return true; |
| 1868 | } |
| 1869 |