tinyxml.cpp 36.7 KB
Newer Older
Ulrich Kemloh's avatar
Ulrich Kemloh committed
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
/*
www.sourceforge.net/projects/tinyxml
Original code by Lee Thomason (www.grinninglizard.com)

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.

Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.
*/

#include <ctype.h>

#ifdef TIXML_USE_STL
#include <sstream>
#include <iostream>
#endif

#include "tinyxml.h"

FILE* TiXmlFOpen( const char* filename, const char* mode );

bool TiXmlBase::condenseWhiteSpace = true;

// Microsoft compiler security
FILE* TiXmlFOpen( const char* filename, const char* mode )
{
41 42 43 44 45 46 47 48 49
	#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
		FILE* fp = 0;
		errno_t err = fopen_s( &fp, filename, mode );
		if ( !err && fp )
			return fp;
		return 0;
	#else
		return fopen( filename, mode );
	#endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
50 51 52 53
}

void TiXmlBase::EncodeString( const TIXML_STRING& str, TIXML_STRING* outString )
{
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
	int i=0;

	while( i<(int)str.length() )
	{
		unsigned char c = (unsigned char) str[i];

		if (    c == '&' 
		     && i < ( (int)str.length() - 2 )
			 && str[i+1] == '#'
			 && str[i+2] == 'x' )
		{
			// Hexadecimal character reference.
			// Pass through unchanged.
			// &#xA9;	-- copyright symbol, for example.
			//
			// The -1 is a bug fix from Rob Laveaux. It keeps
			// an overflow from happening if there is no ';'.
			// There are actually 2 ways to exit this loop -
			// while fails (error case) and break (semicolon found).
			// However, there is no mechanism (currently) for
			// this function to return an error.
			while ( i<(int)str.length()-1 )
			{
				outString->append( str.c_str() + i, 1 );
				++i;
				if ( str[i] == ';' )
					break;
			}
		}
		else if ( c == '&' )
		{
			outString->append( entity[0].str, entity[0].strLength );
			++i;
		}
		else if ( c == '<' )
		{
			outString->append( entity[1].str, entity[1].strLength );
			++i;
		}
		else if ( c == '>' )
		{
			outString->append( entity[2].str, entity[2].strLength );
			++i;
		}
		else if ( c == '\"' )
		{
			outString->append( entity[3].str, entity[3].strLength );
			++i;
		}
		else if ( c == '\'' )
		{
			outString->append( entity[4].str, entity[4].strLength );
			++i;
		}
		else if ( c < 32 )
		{
			// Easy pass at non-alpha/numeric/symbol
			// Below 32 is symbolic.
			char buf[ 32 ];
			
			#if defined(TIXML_SNPRINTF)		
				TIXML_SNPRINTF( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) );
			#else
				sprintf( buf, "&#x%02X;", (unsigned) ( c & 0xff ) );
			#endif		

			//*ME:	warning C4267: convert 'size_t' to 'int'
			//*ME:	Int-Cast to make compiler happy ...
			outString->append( buf, (int)strlen( buf ) );
			++i;
		}
		else
		{
			//char realc = (char) c;
			//outString->append( &realc, 1 );
			*outString += (char) c;	// somewhat more efficient function call.
			++i;
		}
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
133 134 135 136 137
}


TiXmlNode::TiXmlNode( NodeType _type ) : TiXmlBase()
{
138 139 140 141 142 143
	parent = 0;
	type = _type;
	firstChild = 0;
	lastChild = 0;
	prev = 0;
	next = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
144 145 146 147 148
}


TiXmlNode::~TiXmlNode()
{
149 150
	TiXmlNode* node = firstChild;
	TiXmlNode* temp = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
151

152 153 154 155 156 157
	while ( node )
	{
		temp = node;
		node = node->next;
		delete temp;
	}	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
158 159 160 161 162
}


void TiXmlNode::CopyTo( TiXmlNode* target ) const
{
163 164 165
	target->SetValue (value.c_str() );
	target->userData = userData; 
	target->location = location;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
166 167 168 169 170
}


void TiXmlNode::Clear()
{
171 172
	TiXmlNode* node = firstChild;
	TiXmlNode* temp = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
173

174 175 176 177 178 179
	while ( node )
	{
		temp = node;
		node = node->next;
		delete temp;
	}	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
180

181 182
	firstChild = 0;
	lastChild = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
183 184 185 186 187
}


TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node )
{
188 189
	assert( node->parent == 0 || node->parent == this );
	assert( node->GetDocument() == 0 || node->GetDocument() == this->GetDocument() );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
190

191 192 193 194 195 196 197
	if ( node->Type() == TiXmlNode::TINYXML_DOCUMENT )
	{
		delete node;
		if ( GetDocument() ) 
			GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
198

199
	node->parent = this;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
200

201 202
	node->prev = lastChild;
	node->next = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
203

204 205 206 207
	if ( lastChild )
		lastChild->next = node;
	else
		firstChild = node;			// it was an empty list.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
208

209 210
	lastChild = node;
	return node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
211 212 213 214 215
}


TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis )
{
216 217 218 219 220 221 222 223 224
	if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
	{
		if ( GetDocument() ) 
			GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}
	TiXmlNode* node = addThis.Clone();
	if ( !node )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
225

226
	return LinkEndChild( node );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
227 228 229 230
}


TiXmlNode* TiXmlNode::InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis )
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
{	
	if ( !beforeThis || beforeThis->parent != this ) {
		return 0;
	}
	if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
	{
		if ( GetDocument() ) 
			GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}

	TiXmlNode* node = addThis.Clone();
	if ( !node )
		return 0;
	node->parent = this;

	node->next = beforeThis;
	node->prev = beforeThis->prev;
	if ( beforeThis->prev )
	{
		beforeThis->prev->next = node;
	}
	else
	{
		assert( firstChild == beforeThis );
		firstChild = node;
	}
	beforeThis->prev = node;
	return node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
260 261 262 263 264
}


TiXmlNode* TiXmlNode::InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis )
{
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
	if ( !afterThis || afterThis->parent != this ) {
		return 0;
	}
	if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
	{
		if ( GetDocument() ) 
			GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}

	TiXmlNode* node = addThis.Clone();
	if ( !node )
		return 0;
	node->parent = this;

	node->prev = afterThis;
	node->next = afterThis->next;
	if ( afterThis->next )
	{
		afterThis->next->prev = node;
	}
	else
	{
		assert( lastChild == afterThis );
		lastChild = node;
	}
	afterThis->next = node;
	return node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
293 294 295 296 297
}


TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )
{
298 299
	if ( !replaceThis )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
300

301 302
	if ( replaceThis->parent != this )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
303

304 305 306 307 308 309 310
	if ( withThis.ToDocument() ) {
		// A document can never be a child.	Thanks to Noam.
		TiXmlDocument* document = GetDocument();
		if ( document ) 
			document->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return 0;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
311

312 313 314
	TiXmlNode* node = withThis.Clone();
	if ( !node )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
315

316 317
	node->next = replaceThis->next;
	node->prev = replaceThis->prev;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
318

319 320 321 322
	if ( replaceThis->next )
		replaceThis->next->prev = node;
	else
		lastChild = node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
323

324 325 326 327
	if ( replaceThis->prev )
		replaceThis->prev->next = node;
	else
		firstChild = node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
328

329 330 331
	delete replaceThis;
	node->parent = this;
	return node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
332 333 334 335 336
}


bool TiXmlNode::RemoveChild( TiXmlNode* removeThis )
{
337 338 339
	if ( !removeThis ) {
		return false;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
340

341 342 343 344 345
	if ( removeThis->parent != this )
	{	
		assert( 0 );
		return false;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
346

347 348 349 350
	if ( removeThis->next )
		removeThis->next->prev = removeThis->prev;
	else
		lastChild = removeThis->prev;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
351

352 353 354 355
	if ( removeThis->prev )
		removeThis->prev->next = removeThis->next;
	else
		firstChild = removeThis->next;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
356

357 358
	delete removeThis;
	return true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
359 360 361 362
}

const TiXmlNode* TiXmlNode::FirstChild( const char * _value ) const
{
363 364 365 366 367 368 369
	const TiXmlNode* node;
	for ( node = firstChild; node; node = node->next )
	{
		if ( strcmp( node->Value(), _value ) == 0 )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
370 371 372 373 374
}


const TiXmlNode* TiXmlNode::LastChild( const char * _value ) const
{
375 376 377 378 379 380 381
	const TiXmlNode* node;
	for ( node = lastChild; node; node = node->prev )
	{
		if ( strcmp( node->Value(), _value ) == 0 )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
382 383 384 385 386
}


const TiXmlNode* TiXmlNode::IterateChildren( const TiXmlNode* previous ) const
{
387 388 389 390 391 392 393 394 395
	if ( !previous )
	{
		return FirstChild();
	}
	else
	{
		assert( previous->parent == this );
		return previous->NextSibling();
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
396 397 398 399 400
}


const TiXmlNode* TiXmlNode::IterateChildren( const char * val, const TiXmlNode* previous ) const
{
401 402 403 404 405 406 407 408 409
	if ( !previous )
	{
		return FirstChild( val );
	}
	else
	{
		assert( previous->parent == this );
		return previous->NextSibling( val );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
410 411 412
}


413
const TiXmlNode* TiXmlNode::NextSibling( const char * _value ) const 
Ulrich Kemloh's avatar
Ulrich Kemloh committed
414
{
415 416 417 418 419 420 421
	const TiXmlNode* node;
	for ( node = next; node; node = node->next )
	{
		if ( strcmp( node->Value(), _value ) == 0 )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
422 423 424 425 426
}


const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const
{
427 428 429 430 431 432 433
	const TiXmlNode* node;
	for ( node = prev; node; node = node->prev )
	{
		if ( strcmp( node->Value(), _value ) == 0 )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
434 435 436 437 438
}


void TiXmlElement::RemoveAttribute( const char * name )
{
439 440 441 442 443 444 445 446 447 448 449
    #ifdef TIXML_USE_STL
	TIXML_STRING str( name );
	TiXmlAttribute* node = attributeSet.Find( str );
	#else
	TiXmlAttribute* node = attributeSet.Find( name );
	#endif
	if ( node )
	{
		attributeSet.Remove( node );
		delete node;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
450 451 452 453
}

const TiXmlElement* TiXmlNode::FirstChildElement() const
{
454
	const TiXmlNode* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
455

456 457 458 459 460 461 462 463
	for (	node = FirstChild();
			node;
			node = node->NextSibling() )
	{
		if ( node->ToElement() )
			return node->ToElement();
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
464 465 466 467 468
}


const TiXmlElement* TiXmlNode::FirstChildElement( const char * _value ) const
{
469
	const TiXmlNode* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
470

471 472 473 474 475 476 477 478
	for (	node = FirstChild( _value );
			node;
			node = node->NextSibling( _value ) )
	{
		if ( node->ToElement() )
			return node->ToElement();
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
479 480 481 482 483
}


const TiXmlElement* TiXmlNode::NextSiblingElement() const
{
484
	const TiXmlNode* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
485

486 487 488 489 490 491 492 493
	for (	node = NextSibling();
			node;
			node = node->NextSibling() )
	{
		if ( node->ToElement() )
			return node->ToElement();
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
494 495 496 497 498
}


const TiXmlElement* TiXmlNode::NextSiblingElement( const char * _value ) const
{
499
	const TiXmlNode* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
500

501 502 503 504 505 506 507 508
	for (	node = NextSibling( _value );
			node;
			node = node->NextSibling( _value ) )
	{
		if ( node->ToElement() )
			return node->ToElement();
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
509 510 511 512 513
}


const TiXmlDocument* TiXmlNode::GetDocument() const
{
514
	const TiXmlNode* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
515

516 517 518 519 520 521
	for( node = this; node; node = node->parent )
	{
		if ( node->ToDocument() )
			return node->ToDocument();
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
522 523 524 525
}


TiXmlElement::TiXmlElement (const char * _value)
526
	: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
527
{
528 529
	firstChild = lastChild = 0;
	value = _value;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
530 531 532 533
}


#ifdef TIXML_USE_STL
534 535
TiXmlElement::TiXmlElement( const std::string& _value ) 
	: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
536
{
537 538
	firstChild = lastChild = 0;
	value = _value;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
539 540 541 542 543
}
#endif


TiXmlElement::TiXmlElement( const TiXmlElement& copy)
544
	: TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
545
{
546 547
	firstChild = lastChild = 0;
	copy.CopyTo( this );	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
548 549 550 551 552
}


TiXmlElement& TiXmlElement::operator=( const TiXmlElement& base )
{
553 554 555
	ClearThis();
	base.CopyTo( this );
	return *this;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
556 557 558 559 560
}


TiXmlElement::~TiXmlElement()
{
561
	ClearThis();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
562 563 564 565 566
}


void TiXmlElement::ClearThis()
{
567 568 569 570 571 572 573
	Clear();
	while( attributeSet.First() )
	{
		TiXmlAttribute* node = attributeSet.First();
		attributeSet.Remove( node );
		delete node;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
574 575 576 577 578
}


const char* TiXmlElement::Attribute( const char* name ) const
{
579 580 581 582
	const TiXmlAttribute* node = attributeSet.Find( name );
	if ( node )
		return node->Value();
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
583 584 585 586 587 588
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name ) const
{
589 590 591 592
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	if ( attrib )
		return &attrib->ValueStr();
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
593 594 595 596 597 598
}
#endif


const char* TiXmlElement::Attribute( const char* name, int* i ) const
{
599 600
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	const char* result = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
601

602 603 604 605 606 607 608
	if ( attrib ) {
		result = attrib->Value();
		if ( i ) {
			attrib->QueryIntValue( i );
		}
	}
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
609 610 611 612 613 614
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
{
615 616
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	const std::string* result = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
617

618 619 620 621 622 623 624
	if ( attrib ) {
		result = &attrib->ValueStr();
		if ( i ) {
			attrib->QueryIntValue( i );
		}
	}
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
625 626 627 628 629 630
}
#endif


const char* TiXmlElement::Attribute( const char* name, double* d ) const
{
631 632
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	const char* result = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
633

634 635 636 637 638 639 640
	if ( attrib ) {
		result = attrib->Value();
		if ( d ) {
			attrib->QueryDoubleValue( d );
		}
	}
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
641 642 643 644 645 646
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const
{
647 648
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	const std::string* result = 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
649

650 651 652 653 654 655 656
	if ( attrib ) {
		result = &attrib->ValueStr();
		if ( d ) {
			attrib->QueryDoubleValue( d );
		}
	}
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
657 658 659 660 661 662
}
#endif


int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const
{
663 664 665 666
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	if ( !attrib )
		return TIXML_NO_ATTRIBUTE;
	return attrib->QueryIntValue( ival );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
667 668 669
}


670
int TiXmlElement::QueryUnsignedAttribute( const char* name, unsigned* vvalue ) const
Ulrich Kemloh's avatar
Ulrich Kemloh committed
671
{
672 673 674
	const TiXmlAttribute* node = attributeSet.Find( name );
	if ( !node )
		return TIXML_NO_ATTRIBUTE;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
675

676 677
	int ival = 0;
	int result = node->QueryIntValue( &ival );
678
	*vvalue = (unsigned)ival;
679
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
680 681 682 683 684
}


int TiXmlElement::QueryBoolAttribute( const char* name, bool* bval ) const
{
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	const TiXmlAttribute* node = attributeSet.Find( name );
	if ( !node )
		return TIXML_NO_ATTRIBUTE;
	
	int result = TIXML_WRONG_TYPE;
	if (    StringEqual( node->Value(), "true", true, TIXML_ENCODING_UNKNOWN ) 
		 || StringEqual( node->Value(), "yes", true, TIXML_ENCODING_UNKNOWN ) 
		 || StringEqual( node->Value(), "1", true, TIXML_ENCODING_UNKNOWN ) ) 
	{
		*bval = true;
		result = TIXML_SUCCESS;
	}
	else if (    StringEqual( node->Value(), "false", true, TIXML_ENCODING_UNKNOWN ) 
			  || StringEqual( node->Value(), "no", true, TIXML_ENCODING_UNKNOWN ) 
			  || StringEqual( node->Value(), "0", true, TIXML_ENCODING_UNKNOWN ) ) 
	{
		*bval = false;
		result = TIXML_SUCCESS;
	}
	return result;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
705 706 707 708 709 710 711
}



#ifdef TIXML_USE_STL
int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const
{
712 713 714 715
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	if ( !attrib )
		return TIXML_NO_ATTRIBUTE;
	return attrib->QueryIntValue( ival );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
716 717 718 719 720 721
}
#endif


int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const
{
722 723 724 725
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	if ( !attrib )
		return TIXML_NO_ATTRIBUTE;
	return attrib->QueryDoubleValue( dval );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
726 727 728 729 730 731
}


#ifdef TIXML_USE_STL
int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const
{
732 733 734 735
	const TiXmlAttribute* attrib = attributeSet.Find( name );
	if ( !attrib )
		return TIXML_NO_ATTRIBUTE;
	return attrib->QueryDoubleValue( dval );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
736 737 738 739 740
}
#endif


void TiXmlElement::SetAttribute( const char * name, int val )
741 742 743 744 745
{	
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
	if ( attrib ) {
		attrib->SetIntValue( val );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
746 747 748 749 750
}


#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& name, int val )
751 752 753 754 755
{	
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
	if ( attrib ) {
		attrib->SetIntValue( val );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
756 757 758 759 760
}
#endif


void TiXmlElement::SetDoubleAttribute( const char * name, double val )
761 762 763 764 765
{	
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
	if ( attrib ) {
		attrib->SetDoubleValue( val );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
766 767 768 769 770
}


#ifdef TIXML_USE_STL
void TiXmlElement::SetDoubleAttribute( const std::string& name, double val )
771 772 773 774 775
{	
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
	if ( attrib ) {
		attrib->SetDoubleValue( val );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
776
}
777
#endif 
Ulrich Kemloh's avatar
Ulrich Kemloh committed
778 779 780 781


void TiXmlElement::SetAttribute( const char * cname, const char * cvalue )
{
782 783 784 785
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( cname );
	if ( attrib ) {
		attrib->SetValue( cvalue );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
786 787 788 789 790 791
}


#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& _name, const std::string& _value )
{
792 793 794 795
	TiXmlAttribute* attrib = attributeSet.FindOrCreate( _name );
	if ( attrib ) {
		attrib->SetValue( _value );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
796 797 798 799 800 801
}
#endif


void TiXmlElement::Print( FILE* cfile, int depth ) const
{
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	int i;
	assert( cfile );
	for ( i=0; i<depth; i++ ) {
		fprintf( cfile, "    " );
	}

	fprintf( cfile, "<%s", value.c_str() );

	const TiXmlAttribute* attrib;
	for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
	{
		fprintf( cfile, " " );
		attrib->Print( cfile, depth );
	}

	// There are 3 different formatting approaches:
	// 1) An element without children is printed as a <foo /> node
	// 2) An element with only a text child is printed as <foo> text </foo>
	// 3) An element with children is printed on multiple lines.
	TiXmlNode* node;
	if ( !firstChild )
	{
		fprintf( cfile, " />" );
	}
	else if ( firstChild == lastChild && firstChild->ToText() )
	{
		fprintf( cfile, ">" );
		firstChild->Print( cfile, depth + 1 );
		fprintf( cfile, "</%s>", value.c_str() );
	}
	else
	{
		fprintf( cfile, ">" );

		for ( node = firstChild; node; node=node->NextSibling() )
		{
			if ( !node->ToText() )
			{
				fprintf( cfile, "\n" );
			}
			node->Print( cfile, depth+1 );
		}
		fprintf( cfile, "\n" );
		for( i=0; i<depth; ++i ) {
			fprintf( cfile, "    " );
		}
		fprintf( cfile, "</%s>", value.c_str() );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
850 851 852 853 854
}


void TiXmlElement::CopyTo( TiXmlElement* target ) const
{
855 856
	// superclass:
	TiXmlNode::CopyTo( target );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
857

858 859 860 861 862 863 864 865 866
	// Element class: 
	// Clone the attributes, then clone the children.
	const TiXmlAttribute* attribute = 0;
	for(	attribute = attributeSet.First();
	attribute;
	attribute = attribute->Next() )
	{
		target->SetAttribute( attribute->Name(), attribute->Value() );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
867

868 869 870 871 872
	TiXmlNode* node = 0;
	for ( node = firstChild; node; node = node->NextSibling() )
	{
		target->LinkEndChild( node->Clone() );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
873 874 875 876
}

bool TiXmlElement::Accept( TiXmlVisitor* visitor ) const
{
877 878 879 880 881 882 883 884 885
	if ( visitor->VisitEnter( *this, attributeSet.First() ) ) 
	{
		for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
		{
			if ( !node->Accept( visitor ) )
				break;
		}
	}
	return visitor->VisitExit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
886 887 888 889 890
}


TiXmlNode* TiXmlElement::Clone() const
{
891 892 893
	TiXmlElement* clone = new TiXmlElement( Value() );
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
894

895 896
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
897 898 899 900 901
}


const char* TiXmlElement::GetText() const
{
902 903 904 905 906 907 908 909
	const TiXmlNode* child = this->FirstChild();
	if ( child ) {
		const TiXmlText* childText = child->ToText();
		if ( childText ) {
			return childText->Value();
		}
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
910 911 912 913 914
}


TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
{
915 916 917
	tabsize = 4;
	useMicrosoftBOM = false;
	ClearError();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
918 919 920 921
}

TiXmlDocument::TiXmlDocument( const char * documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
{
922 923 924 925
	tabsize = 4;
	useMicrosoftBOM = false;
	value = documentName;
	ClearError();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
926 927 928 929 930 931
}


#ifdef TIXML_USE_STL
TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
{
932 933 934 935
	tabsize = 4;
	useMicrosoftBOM = false;
    value = documentName;
	ClearError();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
936 937 938 939 940 941
}
#endif


TiXmlDocument::TiXmlDocument( const TiXmlDocument& copy ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
{
942
	copy.CopyTo( this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
943 944 945 946 947
}


TiXmlDocument& TiXmlDocument::operator=( const TiXmlDocument& copy )
{
948 949 950
	Clear();
	copy.CopyTo( this );
	return *this;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
951 952 953 954 955
}


bool TiXmlDocument::LoadFile( TiXmlEncoding encoding )
{
956
	return LoadFile( Value(), encoding );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
957 958 959 960 961
}


bool TiXmlDocument::SaveFile() const
{
962
	return SaveFile( Value() );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
963 964 965 966
}

bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding )
{
967 968
	TIXML_STRING filename( _filename );
	value = filename;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
969

970 971
	// reading in binary mode so that tinyxml can normalize the EOL
	FILE* file = TiXmlFOpen( value.c_str (), "rb" );	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
972

973 974 975 976 977 978 979 980 981 982 983
	if ( file )
	{
		bool result = LoadFile( file, encoding );
		fclose( file );
		return result;
	}
	else
	{
		SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
984 985 986 987
}

bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
{
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	if ( !file ) 
	{
		SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Delete the existing data:
	Clear();
	location.Clear();

	// Get the file size, so we can pre-allocate the string. HUGE speed impact.
	long length = 0;
	fseek( file, 0, SEEK_END );
	length = ftell( file );
	fseek( file, 0, SEEK_SET );

	// Strange case, but good to handle up front.
	if ( length <= 0 )
	{
		SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Subtle bug here. TinyXml did use fgets. But from the XML spec:
	// 2.11 End-of-Line Handling
	// <snip>
	// <quote>
	// ...the XML processor MUST behave as if it normalized all line breaks in external 
	// parsed entities (including the document entity) on input, before parsing, by translating 
	// both the two-character sequence #xD #xA and any #xD that is not followed by #xA to 
	// a single #xA character.
	// </quote>
	//
	// It is not clear fgets does that, and certainly isn't clear it works cross platform. 
	// Generally, you expect fgets to translate from the convention of the OS to the c/unix
	// convention, and not work generally.

	/*
	while( fgets( buf, sizeof(buf), file ) )
	{
		data += buf;
	}
	*/

	char* buf = new char[ length+1 ];
	buf[0] = 0;

	if ( fread( buf, length, 1, file ) != 1 ) {
		delete [] buf;
		SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Process the buffer in place to normalize new lines. (See comment above.)
	// Copies from the 'p' to 'q' pointer, where p can advance faster if
	// a newline-carriage return is hit.
	//
	// Wikipedia:
	// Systems based on ASCII or a compatible character set use either LF  (Line feed, '\n', 0x0A, 10 in decimal) or 
	// CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
	//		* LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
    //		* CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
    //		* CR:    Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9

	const char* p = buf;	// the read head
	char* q = buf;			// the write head
	const char CR = 0x0d;
	const char LF = 0x0a;

	buf[length] = 0;
	while( *p ) {
		assert( p < (buf+length) );
		assert( q <= (buf+length) );
		assert( q <= p );

		if ( *p == CR ) {
			*q++ = LF;
			p++;
			if ( *p == LF ) {		// check for CR+LF (and skip LF)
				p++;
			}
		}
		else {
			*q++ = *p++;
		}
	}
	assert( q <= (buf+length) );
	*q = 0;

	Parse( buf, 0, encoding );

	delete [] buf;
	return !Error();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1081 1082 1083 1084 1085
}


bool TiXmlDocument::SaveFile( const char * filename ) const
{
1086 1087 1088 1089 1090 1091 1092 1093 1094
	// The old c stuff lives on...
	FILE* fp = TiXmlFOpen( filename, "w" );
	if ( fp )
	{
		bool result = SaveFile( fp );
		fclose( fp );
		return result;
	}
	return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1095 1096 1097 1098 1099
}


bool TiXmlDocument::SaveFile( FILE* fp ) const
{
1100 1101 1102 1103 1104
	if ( useMicrosoftBOM ) 
	{
		const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
		const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
		const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1105

1106 1107 1108 1109 1110 1111
		fputc( TIXML_UTF_LEAD_0, fp );
		fputc( TIXML_UTF_LEAD_1, fp );
		fputc( TIXML_UTF_LEAD_2, fp );
	}
	Print( fp, 0 );
	return (ferror(fp) == 0);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1112 1113 1114 1115 1116
}


void TiXmlDocument::CopyTo( TiXmlDocument* target ) const
{
1117
	TiXmlNode::CopyTo( target );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1118

1119 1120 1121 1122 1123 1124
	target->error = error;
	target->errorId = errorId;
	target->errorDesc = errorDesc;
	target->tabsize = tabsize;
	target->errorLocation = errorLocation;
	target->useMicrosoftBOM = useMicrosoftBOM;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1125

1126 1127 1128 1129 1130
	TiXmlNode* node = 0;
	for ( node = firstChild; node; node = node->NextSibling() )
	{
		target->LinkEndChild( node->Clone() );
	}	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1131 1132 1133 1134 1135
}


TiXmlNode* TiXmlDocument::Clone() const
{
1136 1137 1138
	TiXmlDocument* clone = new TiXmlDocument();
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1139

1140 1141
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1142 1143 1144 1145 1146
}


void TiXmlDocument::Print( FILE* cfile, int depth ) const
{
1147 1148 1149 1150 1151 1152
	assert( cfile );
	for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
	{
		node->Print( cfile, depth );
		fprintf( cfile, "\n" );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1153 1154 1155 1156 1157
}


bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const
{
1158 1159 1160 1161 1162 1163 1164 1165 1166
	if ( visitor->VisitEnter( *this ) )
	{
		for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
		{
			if ( !node->Accept( visitor ) )
				break;
		}
	}
	return visitor->VisitExit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1167 1168 1169 1170 1171
}


const TiXmlAttribute* TiXmlAttribute::Next() const
{
1172 1173 1174 1175 1176
	// We are using knowledge of the sentinel. The sentinel
	// have a value or name.
	if ( next->value.empty() && next->name.empty() )
		return 0;
	return next;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1177 1178 1179 1180 1181
}

/*
TiXmlAttribute* TiXmlAttribute::Next()
{
1182 1183 1184 1185 1186
	// We are using knowledge of the sentinel. The sentinel
	// have a value or name.
	if ( next->value.empty() && next->name.empty() )
		return 0;
	return next;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1187 1188 1189 1190 1191
}
*/

const TiXmlAttribute* TiXmlAttribute::Previous() const
{
1192 1193 1194 1195 1196
	// We are using knowledge of the sentinel. The sentinel
	// have a value or name.
	if ( prev->value.empty() && prev->name.empty() )
		return 0;
	return prev;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1197 1198 1199 1200 1201
}

/*
TiXmlAttribute* TiXmlAttribute::Previous()
{
1202 1203 1204 1205 1206
	// We are using knowledge of the sentinel. The sentinel
	// have a value or name.
	if ( prev->value.empty() && prev->name.empty() )
		return 0;
	return prev;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1207 1208 1209 1210 1211
}
*/

void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
{
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
	TIXML_STRING n, v;

	EncodeString( name, &n );
	EncodeString( value, &v );

	if (value.find ('\"') == TIXML_STRING::npos) {
		if ( cfile ) {
			fprintf (cfile, "%s=\"%s\"", n.c_str(), v.c_str() );
		}
		if ( str ) {
			(*str) += n; (*str) += "=\""; (*str) += v; (*str) += "\"";
		}
	}
	else {
		if ( cfile ) {
			fprintf (cfile, "%s='%s'", n.c_str(), v.c_str() );
		}
		if ( str ) {
			(*str) += n; (*str) += "='"; (*str) += v; (*str) += "'";
		}
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1233 1234 1235 1236 1237
}


int TiXmlAttribute::QueryIntValue( int* ival ) const
{
1238 1239 1240
	if ( TIXML_SSCANF( value.c_str(), "%d", ival ) == 1 )
		return TIXML_SUCCESS;
	return TIXML_WRONG_TYPE;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1241 1242 1243 1244
}

int TiXmlAttribute::QueryDoubleValue( double* dval ) const
{
1245 1246 1247
	if ( TIXML_SSCANF( value.c_str(), "%lf", dval ) == 1 )
		return TIXML_SUCCESS;
	return TIXML_WRONG_TYPE;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1248 1249 1250 1251
}

void TiXmlAttribute::SetIntValue( int _value )
{
1252 1253 1254 1255 1256 1257 1258
	char buf [64];
	#if defined(TIXML_SNPRINTF)		
		TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
	#else
		sprintf (buf, "%d", _value);
	#endif
	SetValue (buf);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1259 1260 1261 1262
}

void TiXmlAttribute::SetDoubleValue( double _value )
{
1263 1264 1265 1266 1267 1268 1269
	char buf [256];
	#if defined(TIXML_SNPRINTF)		
		TIXML_SNPRINTF( buf, sizeof(buf), "%g", _value);
	#else
		sprintf (buf, "%g", _value);
	#endif
	SetValue (buf);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1270 1271 1272 1273
}

int TiXmlAttribute::IntValue() const
{
1274
	return atoi (value.c_str ());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1275 1276 1277 1278
}

double  TiXmlAttribute::DoubleValue() const
{
1279
	return atof (value.c_str ());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1280 1281 1282 1283 1284
}


TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT )
{
1285
	copy.CopyTo( this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1286 1287 1288 1289 1290
}


TiXmlComment& TiXmlComment::operator=( const TiXmlComment& base )
{
1291 1292 1293
	Clear();
	base.CopyTo( this );
	return *this;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1294 1295 1296 1297 1298
}


void TiXmlComment::Print( FILE* cfile, int depth ) const
{
1299 1300 1301 1302 1303 1304
	assert( cfile );
	for ( int i=0; i<depth; i++ )
	{
		fprintf( cfile,  "    " );
	}
	fprintf( cfile, "<!--%s-->", value.c_str() );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1305 1306 1307 1308 1309
}


void TiXmlComment::CopyTo( TiXmlComment* target ) const
{
1310
	TiXmlNode::CopyTo( target );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1311 1312 1313 1314 1315
}


bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const
{
1316
	return visitor->Visit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1317 1318 1319 1320 1321
}


TiXmlNode* TiXmlComment::Clone() const
{
1322
	TiXmlComment* clone = new TiXmlComment();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1323

1324 1325
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1326

1327 1328
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1329 1330 1331 1332 1333
}


void TiXmlText::Print( FILE* cfile, int depth ) const
{
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	assert( cfile );
	if ( cdata )
	{
		int i;
		fprintf( cfile, "\n" );
		for ( i=0; i<depth; i++ ) {
			fprintf( cfile, "    " );
		}
		fprintf( cfile, "<![CDATA[%s]]>\n", value.c_str() );	// unformatted output
	}
	else
	{
		TIXML_STRING buffer;
		EncodeString( value, &buffer );
		fprintf( cfile, "%s", buffer.c_str() );
	}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1350 1351 1352 1353 1354
}


void TiXmlText::CopyTo( TiXmlText* target ) const
{
1355 1356
	TiXmlNode::CopyTo( target );
	target->cdata = cdata;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1357 1358 1359 1360 1361
}


bool TiXmlText::Accept( TiXmlVisitor* visitor ) const
{
1362
	return visitor->Visit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1363 1364 1365 1366
}


TiXmlNode* TiXmlText::Clone() const
1367 1368 1369
{	
	TiXmlText* clone = 0;
	clone = new TiXmlText( "" );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1370

1371 1372
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1373

1374 1375
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1376 1377 1378 1379
}


TiXmlDeclaration::TiXmlDeclaration( const char * _version,
1380 1381 1382
									const char * _encoding,
									const char * _standalone )
	: TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1383
{
1384 1385 1386
	version = _version;
	encoding = _encoding;
	standalone = _standalone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1387 1388 1389 1390
}


#ifdef TIXML_USE_STL
1391 1392 1393 1394
TiXmlDeclaration::TiXmlDeclaration(	const std::string& _version,
									const std::string& _encoding,
									const std::string& _standalone )
	: TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1395
{
1396 1397 1398
	version = _version;
	encoding = _encoding;
	standalone = _standalone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1399 1400 1401 1402 1403
}
#endif


TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy )
1404
	: TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1405
{
1406
	copy.CopyTo( this );	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1407 1408 1409 1410 1411
}


TiXmlDeclaration& TiXmlDeclaration::operator=( const TiXmlDeclaration& copy )
{
1412 1413 1414
	Clear();
	copy.CopyTo( this );
	return *this;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1415 1416 1417 1418 1419
}


void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
{
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	if ( cfile ) fprintf( cfile, "<?xml " );
	if ( str )	 (*str) += "<?xml ";

	if ( !version.empty() ) {
		if ( cfile ) fprintf (cfile, "version=\"%s\" ", version.c_str ());
		if ( str ) { (*str) += "version=\""; (*str) += version; (*str) += "\" "; }
	}
	if ( !encoding.empty() ) {
		if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding.c_str ());
		if ( str ) { (*str) += "encoding=\""; (*str) += encoding; (*str) += "\" "; }
	}
	if ( !standalone.empty() ) {
		if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone.c_str ());
		if ( str ) { (*str) += "standalone=\""; (*str) += standalone; (*str) += "\" "; }
	}
	if ( cfile ) fprintf( cfile, "?>" );
	if ( str )	 (*str) += "?>";
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1437 1438 1439 1440 1441
}


void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const
{
1442
	TiXmlNode::CopyTo( target );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1443

1444 1445 1446
	target->version = version;
	target->encoding = encoding;
	target->standalone = standalone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1447 1448 1449 1450 1451
}


bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const
{
1452
	return visitor->Visit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1453 1454 1455 1456
}


TiXmlNode* TiXmlDeclaration::Clone() const
1457 1458
{	
	TiXmlDeclaration* clone = new TiXmlDeclaration();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1459

1460 1461
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1462

1463 1464
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1465 1466 1467 1468 1469
}


void TiXmlUnknown::Print( FILE* cfile, int depth ) const
{
1470 1471 1472
	for ( int i=0; i<depth; i++ )
		fprintf( cfile, "    " );
	fprintf( cfile, "<%s>", value.c_str() );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1473 1474 1475 1476 1477
}


void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const
{
1478
	TiXmlNode::CopyTo( target );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1479 1480 1481 1482 1483
}


bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const
{
1484
	return visitor->Visit( *this );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1485 1486 1487 1488 1489
}


TiXmlNode* TiXmlUnknown::Clone() const
{
1490
	TiXmlUnknown* clone = new TiXmlUnknown();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1491

1492 1493
	if ( !clone )
		return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1494

1495 1496
	CopyTo( clone );
	return clone;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1497 1498 1499 1500 1501
}


TiXmlAttributeSet::TiXmlAttributeSet()
{
1502 1503
	sentinel.next = &sentinel;
	sentinel.prev = &sentinel;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1504 1505 1506 1507 1508
}


TiXmlAttributeSet::~TiXmlAttributeSet()
{
1509 1510
	assert( sentinel.next == &sentinel );
	assert( sentinel.prev == &sentinel );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1511 1512 1513 1514 1515
}


void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
{
1516 1517 1518 1519 1520
    #ifdef TIXML_USE_STL
	assert( !Find( TIXML_STRING( addMe->Name() ) ) );	// Shouldn't be multiply adding to the set.
	#else
	assert( !Find( addMe->Name() ) );	// Shouldn't be multiply adding to the set.
	#endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1521

1522 1523
	addMe->next = &sentinel;
	addMe->prev = sentinel.prev;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1524

1525 1526
	sentinel.prev->next = addMe;
	sentinel.prev      = addMe;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1527 1528 1529 1530
}

void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
{
1531
	TiXmlAttribute* node;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1532

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	for( node = sentinel.next; node != &sentinel; node = node->next )
	{
		if ( node == removeMe )
		{
			node->prev->next = node->next;
			node->next->prev = node->prev;
			node->next = 0;
			node->prev = 0;
			return;
		}
	}
	assert( 0 );		// we tried to remove a non-linked attribute.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1545 1546 1547 1548 1549 1550
}


#ifdef TIXML_USE_STL
TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
{
1551 1552 1553 1554 1555 1556
	for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
	{
		if ( node->name == name )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1557 1558 1559 1560
}

TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const std::string& _name )
{
1561 1562 1563 1564 1565 1566 1567
	TiXmlAttribute* attrib = Find( _name );
	if ( !attrib ) {
		attrib = new TiXmlAttribute();
		Add( attrib );
		attrib->SetName( _name );
	}
	return attrib;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1568 1569 1570 1571 1572 1573
}
#endif


TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const
{
1574 1575 1576 1577 1578 1579
	for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
	{
		if ( strcmp( node->name.c_str(), name ) == 0 )
			return node;
	}
	return 0;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1580 1581 1582 1583 1584
}


TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const char* _name )
{
1585 1586 1587 1588 1589 1590 1591
	TiXmlAttribute* attrib = Find( _name );
	if ( !attrib ) {
		attrib = new TiXmlAttribute();
		Add( attrib );
		attrib->SetName( _name );
	}
	return attrib;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1592 1593 1594
}


1595
#ifdef TIXML_USE_STL	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1596 1597
std::istream& operator>> (std::istream & in, TiXmlNode & base)
{
1598 1599 1600
	TIXML_STRING tag;
	tag.reserve( 8 * 1000 );
	base.StreamIn( &in, &tag );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1601

1602 1603
	base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
	return in;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1604 1605 1606 1607
}
#endif


1608
#ifdef TIXML_USE_STL	
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1609 1610
std::ostream& operator<< (std::ostream & out, const TiXmlNode & base)
{
1611 1612 1613 1614
	TiXmlPrinter printer;
	printer.SetStreamPrinting();
	base.Accept( &printer );
	out << printer.Str();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1615

1616
	return out;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1617 1618 1619 1620 1621
}


std::string& operator<< (std::string& out, const TiXmlNode& base )
{
1622 1623 1624 1625
	TiXmlPrinter printer;
	printer.SetStreamPrinting();
	base.Accept( &printer );
	out.append( printer.Str() );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1626