Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 1865 lines (1549 sloc) 36.371 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 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 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 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"


bool TiXmlBase::condenseWhiteSpace = true;

void TiXmlBase::PutString( const TIXML_STRING& str, TIXML_STRING* outString )
{
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;
}
}
}


TiXmlNode::TiXmlNode( NodeType _type ) : TiXmlBase()
{
parent = 0;
type = _type;
firstChild = 0;
lastChild = 0;
prev = 0;
next = 0;
}


TiXmlNode::~TiXmlNode()
{
TiXmlNode* node = firstChild;
TiXmlNode* temp = 0;

while ( node )
{
temp = node;
node = node->next;
delete temp;
}
}


void TiXmlNode::CopyTo( TiXmlNode* target ) const
{
target->SetValue (value.c_str() );
target->userData = userData;
}


void TiXmlNode::Clear()
{
TiXmlNode* node = firstChild;
TiXmlNode* temp = 0;

while ( node )
{
temp = node;
node = node->next;
delete temp;
}

firstChild = 0;
lastChild = 0;
}


TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node )
{
assert( node->parent == 0 || node->parent == this );
assert( node->GetDocument() == 0 || node->GetDocument() == this->GetDocument() );

if ( node->Type() == TiXmlNode::DOCUMENT )
{
delete node;
if ( GetDocument() ) GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
return 0;
}

node->parent = this;

node->prev = lastChild;
node->next = 0;

if ( lastChild )
lastChild->next = node;
else
firstChild = node; // it was an empty list.

lastChild = node;
return node;
}


TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis )
{
if ( addThis.Type() == TiXmlNode::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;

return LinkEndChild( node );
}


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


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


TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )
{
if ( replaceThis->parent != this )
return 0;

TiXmlNode* node = withThis.Clone();
if ( !node )
return 0;

node->next = replaceThis->next;
node->prev = replaceThis->prev;

if ( replaceThis->next )
replaceThis->next->prev = node;
else
lastChild = node;

if ( replaceThis->prev )
replaceThis->prev->next = node;
else
firstChild = node;

delete replaceThis;
node->parent = this;
return node;
}


bool TiXmlNode::RemoveChild( TiXmlNode* removeThis )
{
if ( removeThis->parent != this )
{
assert( 0 );
return false;
}

if ( removeThis->next )
removeThis->next->prev = removeThis->prev;
else
lastChild = removeThis->prev;

if ( removeThis->prev )
removeThis->prev->next = removeThis->next;
else
firstChild = removeThis->next;

delete removeThis;
return true;
}

const TiXmlNode* TiXmlNode::FirstChild( const char * _value ) const
{
const TiXmlNode* node;
for ( node = firstChild; node; node = node->next )
{
if ( strcmp( node->Value(), _value ) == 0 )
return node;
}
return 0;
}


const TiXmlNode* TiXmlNode::LastChild( const char * _value ) const
{
const TiXmlNode* node;
for ( node = lastChild; node; node = node->prev )
{
if ( strcmp( node->Value(), _value ) == 0 )
return node;
}
return 0;
}


const TiXmlNode* TiXmlNode::IterateChildren( const TiXmlNode* previous ) const
{
if ( !previous )
{
return FirstChild();
}
else
{
assert( previous->parent == this );
return previous->NextSibling();
}
}


const TiXmlNode* TiXmlNode::IterateChildren( const char * val, const TiXmlNode* previous ) const
{
if ( !previous )
{
return FirstChild( val );
}
else
{
assert( previous->parent == this );
return previous->NextSibling( val );
}
}


const TiXmlNode* TiXmlNode::NextSibling( const char * _value ) const
{
const TiXmlNode* node;
for ( node = next; node; node = node->next )
{
if ( strcmp( node->Value(), _value ) == 0 )
return node;
}
return 0;
}


const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const
{
const TiXmlNode* node;
for ( node = prev; node; node = node->prev )
{
if ( strcmp( node->Value(), _value ) == 0 )
return node;
}
return 0;
}


void TiXmlElement::RemoveAttribute( const char * name )
{
#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;
}
}

const TiXmlElement* TiXmlNode::FirstChildElement() const
{
const TiXmlNode* node;

for ( node = FirstChild();
node;
node = node->NextSibling() )
{
if ( node->ToElement() )
return node->ToElement();
}
return 0;
}


const TiXmlElement* TiXmlNode::FirstChildElement( const char * _value ) const
{
const TiXmlNode* node;

for ( node = FirstChild( _value );
node;
node = node->NextSibling( _value ) )
{
if ( node->ToElement() )
return node->ToElement();
}
return 0;
}


const TiXmlElement* TiXmlNode::NextSiblingElement() const
{
const TiXmlNode* node;

for ( node = NextSibling();
node;
node = node->NextSibling() )
{
if ( node->ToElement() )
return node->ToElement();
}
return 0;
}


const TiXmlElement* TiXmlNode::NextSiblingElement( const char * _value ) const
{
const TiXmlNode* node;

for ( node = NextSibling( _value );
node;
node = node->NextSibling( _value ) )
{
if ( node->ToElement() )
return node->ToElement();
}
return 0;
}


const TiXmlDocument* TiXmlNode::GetDocument() const
{
const TiXmlNode* node;

for( node = this; node; node = node->parent )
{
if ( node->ToDocument() )
return node->ToDocument();
}
return 0;
}


TiXmlElement::TiXmlElement (const char * _value)
: TiXmlNode( TiXmlNode::ELEMENT )
{
firstChild = lastChild = 0;
value = _value;
}


#ifdef TIXML_USE_STL
TiXmlElement::TiXmlElement( const std::string& _value )
: TiXmlNode( TiXmlNode::ELEMENT )
{
firstChild = lastChild = 0;
value = _value;
}
#endif


TiXmlElement::TiXmlElement( const TiXmlElement& copy)
: TiXmlNode( TiXmlNode::ELEMENT )
{
firstChild = lastChild = 0;
copy.CopyTo( this );
}


void TiXmlElement::operator=( const TiXmlElement& base )
{
ClearThis();
base.CopyTo( this );
}


TiXmlElement::~TiXmlElement()
{
ClearThis();
}


void TiXmlElement::ClearThis()
{
Clear();
while( attributeSet.First() )
{
TiXmlAttribute* node = attributeSet.First();
attributeSet.Remove( node );
delete node;
}
}


const char* TiXmlElement::Attribute( const char* name ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( node )
return node->Value();
return 0;
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( node )
return &node->ValueStr();
return 0;
}
#endif


const char* TiXmlElement::Attribute( const char* name, int* i ) const
{
const char* s = Attribute( name );
if ( i )
{
if ( s ) {
*i = atoi( s );
}
else {
*i = 0;
}
}
return s;
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
{
const std::string* s = Attribute( name );
if ( i )
{
if ( s ) {
*i = atoi( s->c_str() );
}
else {
*i = 0;
}
}
return s;
}
#endif


const char* TiXmlElement::Attribute( const char* name, double* d ) const
{
const char* s = Attribute( name );
if ( d )
{
if ( s ) {
*d = atof( s );
}
else {
*d = 0;
}
}
return s;
}


#ifdef TIXML_USE_STL
const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const
{
const std::string* s = Attribute( name );
if ( d )
{
if ( s ) {
*d = atof( s->c_str() );
}
else {
*d = 0;
}
}
return s;
}
#endif


int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
return node->QueryIntValue( ival );
}


#ifdef TIXML_USE_STL
int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
return node->QueryIntValue( ival );
}
#endif


int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
return node->QueryDoubleValue( dval );
}


#ifdef TIXML_USE_STL
int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const
{
const TiXmlAttribute* node = attributeSet.Find( name );
if ( !node )
return TIXML_NO_ATTRIBUTE;
return node->QueryDoubleValue( dval );
}
#endif


void TiXmlElement::SetAttribute( const char * name, int val )
{
char buf[64];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF( buf, sizeof(buf), "%d", val );
#else
sprintf( buf, "%d", val );
#endif
SetAttribute( name, buf );
}


#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& name, int val )
{
   std::ostringstream oss;
   oss << val;
   SetAttribute( name, oss.str() );
}
#endif


void TiXmlElement::SetDoubleAttribute( const char * name, double val )
{
char buf[256];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF( buf, sizeof(buf), "%f", val );
#else
sprintf( buf, "%f", val );
#endif
SetAttribute( name, buf );
}


void TiXmlElement::SetAttribute( const char * cname, const char * cvalue )
{
#ifdef TIXML_USE_STL
TIXML_STRING _name( cname );
TIXML_STRING _value( cvalue );
#else
const char* _name = cname;
const char* _value = cvalue;
#endif

TiXmlAttribute* node = attributeSet.Find( _name );
if ( node )
{
node->SetValue( _value );
return;
}

TiXmlAttribute* attrib = new TiXmlAttribute( cname, cvalue );
if ( attrib )
{
attributeSet.Add( attrib );
}
else
{
TiXmlDocument* document = GetDocument();
if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, TIXML_ENCODING_UNKNOWN );
}
}


#ifdef TIXML_USE_STL
void TiXmlElement::SetAttribute( const std::string& name, const std::string& _value )
{
TiXmlAttribute* node = attributeSet.Find( name );
if ( node )
{
node->SetValue( _value );
return;
}

TiXmlAttribute* attrib = new TiXmlAttribute( name, _value );
if ( attrib )
{
attributeSet.Add( attrib );
}
else
{
TiXmlDocument* document = GetDocument();
if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY, 0, 0, TIXML_ENCODING_UNKNOWN );
}
}
#endif


void TiXmlElement::Print( FILE* cfile, int depth ) const
{
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() );
}
}


void TiXmlElement::CopyTo( TiXmlElement* target ) const
{
// superclass:
TiXmlNode::CopyTo( target );

// 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() );
}

TiXmlNode* node = 0;
for ( node = firstChild; node; node = node->NextSibling() )
{
target->LinkEndChild( node->Clone() );
}
}

bool TiXmlElement::Accept( TiXmlVisitor* visitor ) const
{
if ( visitor->VisitEnter( *this, attributeSet.First() ) )
{
for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
{
if ( !node->Accept( visitor ) )
break;
}
}
return visitor->VisitExit( *this );
}


TiXmlNode* TiXmlElement::Clone() const
{
TiXmlElement* clone = new TiXmlElement( Value() );
if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


const char* TiXmlElement::GetText() const
{
const TiXmlNode* child = this->FirstChild();
if ( child ) {
const TiXmlText* childText = child->ToText();
if ( childText ) {
return childText->Value();
}
}
return 0;
}


TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::DOCUMENT )
{
tabsize = 4;
useMicrosoftBOM = false;
ClearError();
}

TiXmlDocument::TiXmlDocument( const char * documentName ) : TiXmlNode( TiXmlNode::DOCUMENT )
{
tabsize = 4;
useMicrosoftBOM = false;
value = documentName;
ClearError();
}


#ifdef TIXML_USE_STL
TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::DOCUMENT )
{
tabsize = 4;
useMicrosoftBOM = false;
    value = documentName;
ClearError();
}
#endif


TiXmlDocument::TiXmlDocument( const TiXmlDocument& copy ) : TiXmlNode( TiXmlNode::DOCUMENT )
{
copy.CopyTo( this );
}


void TiXmlDocument::operator=( const TiXmlDocument& copy )
{
Clear();
copy.CopyTo( this );
}


bool TiXmlDocument::LoadFile( TiXmlEncoding encoding )
{
// See STL_STRING_BUG below.
//StringToBuffer buf( value );

return LoadFile( Value(), encoding );
}


bool TiXmlDocument::SaveFile() const
{
// See STL_STRING_BUG below.
// StringToBuffer buf( value );
//
// if ( buf.buffer && SaveFile( buf.buffer ) )
// return true;
//
// return false;
return SaveFile( Value() );
}

bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding )
{
// There was a really terrifying little bug here. The code:
// value = filename
// in the STL case, cause the assignment method of the std::string to
// be called. What is strange, is that the std::string had the same
// address as it's c_str() method, and so bad things happen. Looks
// like a bug in the Microsoft STL implementation.
// Add an extra string to avoid the crash.
TIXML_STRING filename( _filename );
value = filename;

// reading in binary mode so that tinyxml can normalize the EOL
FILE* file = fopen( value.c_str (), "rb" );

if ( file )
{
bool result = LoadFile( file, encoding );
fclose( file );
return result;
}
else
{
SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
return false;
}
}

bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
{
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;
}

// If we have a file, assume it is all one big XML file, and read it in.
// The document parser may decide the document ends sooner than the entire file, however.
TIXML_STRING data;
data.reserve( length );

// 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;
}

const char* lastPos = buf;
const char* p = buf;

buf[length] = 0;
while( *p ) {
assert( p < (buf+length) );
if ( *p == 0xa ) {
// Newline character. No special rules for this. Append all the characters
// since the last string, and include the newline.
data.append( lastPos, (p-lastPos+1) ); // append, include the newline
++p; // move past the newline
lastPos = p; // and point to the new buffer (may be 0)
assert( p <= (buf+length) );
}
else if ( *p == 0xd ) {
// Carriage return. Append what we have so far, then
// handle moving forward in the buffer.
if ( (p-lastPos) > 0 ) {
data.append( lastPos, p-lastPos ); // do not add the CR
}
data += (char)0xa; // a proper newline

if ( *(p+1) == 0xa ) {
// Carriage return - new line sequence
p += 2;
lastPos = p;
assert( p <= (buf+length) );
}
else {
// it was followed by something else...that is presumably characters again.
++p;
lastPos = p;
assert( p <= (buf+length) );
}
}
else {
++p;
}
}
// Handle any left over characters.
if ( p-lastPos ) {
data.append( lastPos, p-lastPos );
}
delete [] buf;
buf = 0;

Parse( data.c_str(), 0, encoding );

if ( Error() )
        return false;
    else
return true;
}


bool TiXmlDocument::SaveFile( const char * filename ) const
{
// The old c stuff lives on...
FILE* fp = fopen( filename, "w" );
if ( fp )
{
bool result = SaveFile( fp );
fclose( fp );
return result;
}
return false;
}


bool TiXmlDocument::SaveFile( FILE* fp ) const
{
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;

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);
}


void TiXmlDocument::CopyTo( TiXmlDocument* target ) const
{
TiXmlNode::CopyTo( target );

target->error = error;
target->errorDesc = errorDesc.c_str ();

TiXmlNode* node = 0;
for ( node = firstChild; node; node = node->NextSibling() )
{
target->LinkEndChild( node->Clone() );
}
}


TiXmlNode* TiXmlDocument::Clone() const
{
TiXmlDocument* clone = new TiXmlDocument();
if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


void TiXmlDocument::Print( FILE* cfile, int depth ) const
{
assert( cfile );
for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
{
node->Print( cfile, depth );
fprintf( cfile, "\n" );
}
}


bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const
{
if ( visitor->VisitEnter( *this ) )
{
for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
{
if ( !node->Accept( visitor ) )
break;
}
}
return visitor->VisitExit( *this );
}


const TiXmlAttribute* TiXmlAttribute::Next() const
{
// 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;
}

/*
TiXmlAttribute* TiXmlAttribute::Next()
{
// 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;
}
*/

const TiXmlAttribute* TiXmlAttribute::Previous() const
{
// 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;
}

/*
TiXmlAttribute* TiXmlAttribute::Previous()
{
// 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;
}
*/

void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
{
TIXML_STRING n, v;

PutString( name, &n );
PutString( 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) += "'";
}
}
}


int TiXmlAttribute::QueryIntValue( int* ival ) const
{
if ( sscanf( value.c_str(), "%d", ival ) == 1 )
return TIXML_SUCCESS;
return TIXML_WRONG_TYPE;
}

int TiXmlAttribute::QueryDoubleValue( double* dval ) const
{
if ( sscanf( value.c_str(), "%lf", dval ) == 1 )
return TIXML_SUCCESS;
return TIXML_WRONG_TYPE;
}

void TiXmlAttribute::SetIntValue( int _value )
{
char buf [64];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
#else
sprintf (buf, "%d", _value);
#endif
SetValue (buf);
}

void TiXmlAttribute::SetDoubleValue( double _value )
{
char buf [256];
#if defined(TIXML_SNPRINTF)
TIXML_SNPRINTF( buf, sizeof(buf), "%lf", _value);
#else
sprintf (buf, "%lf", _value);
#endif
SetValue (buf);
}

int TiXmlAttribute::IntValue() const
{
return atoi (value.c_str ());
}

double TiXmlAttribute::DoubleValue() const
{
return atof (value.c_str ());
}


TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::COMMENT )
{
copy.CopyTo( this );
}


void TiXmlComment::operator=( const TiXmlComment& base )
{
Clear();
base.CopyTo( this );
}


void TiXmlComment::Print( FILE* cfile, int depth ) const
{
assert( cfile );
for ( int i=0; i<depth; i++ )
{
fprintf( cfile, " " );
}
fprintf( cfile, "<!--%s-->", value.c_str() );
}


void TiXmlComment::CopyTo( TiXmlComment* target ) const
{
TiXmlNode::CopyTo( target );
}


bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const
{
return visitor->Visit( *this );
}


TiXmlNode* TiXmlComment::Clone() const
{
TiXmlComment* clone = new TiXmlComment();

if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


void TiXmlText::Print( FILE* cfile, int depth ) const
{
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;
PutString( value, &buffer );
fprintf( cfile, "%s", buffer.c_str() );
}
}


void TiXmlText::CopyTo( TiXmlText* target ) const
{
TiXmlNode::CopyTo( target );
target->cdata = cdata;
}


bool TiXmlText::Accept( TiXmlVisitor* visitor ) const
{
return visitor->Visit( *this );
}


TiXmlNode* TiXmlText::Clone() const
{
TiXmlText* clone = 0;
clone = new TiXmlText( "" );

if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


TiXmlDeclaration::TiXmlDeclaration( const char * _version,
const char * _encoding,
const char * _standalone )
: TiXmlNode( TiXmlNode::DECLARATION )
{
version = _version;
encoding = _encoding;
standalone = _standalone;
}


#ifdef TIXML_USE_STL
TiXmlDeclaration::TiXmlDeclaration( const std::string& _version,
const std::string& _encoding,
const std::string& _standalone )
: TiXmlNode( TiXmlNode::DECLARATION )
{
version = _version;
encoding = _encoding;
standalone = _standalone;
}
#endif


TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy )
: TiXmlNode( TiXmlNode::DECLARATION )
{
copy.CopyTo( this );
}


void TiXmlDeclaration::operator=( const TiXmlDeclaration& copy )
{
Clear();
copy.CopyTo( this );
}


void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
{
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) += "?>";
}


void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const
{
TiXmlNode::CopyTo( target );

target->version = version;
target->encoding = encoding;
target->standalone = standalone;
}


bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const
{
return visitor->Visit( *this );
}


TiXmlNode* TiXmlDeclaration::Clone() const
{
TiXmlDeclaration* clone = new TiXmlDeclaration();

if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


void TiXmlUnknown::Print( FILE* cfile, int depth ) const
{
for ( int i=0; i<depth; i++ )
fprintf( cfile, " " );
fprintf( cfile, "<%s>", value.c_str() );
}


void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const
{
TiXmlNode::CopyTo( target );
}


bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const
{
return visitor->Visit( *this );
}


TiXmlNode* TiXmlUnknown::Clone() const
{
TiXmlUnknown* clone = new TiXmlUnknown();

if ( !clone )
return 0;

CopyTo( clone );
return clone;
}


TiXmlAttributeSet::TiXmlAttributeSet()
{
sentinel.next = &sentinel;
sentinel.prev = &sentinel;
}


TiXmlAttributeSet::~TiXmlAttributeSet()
{
assert( sentinel.next == &sentinel );
assert( sentinel.prev == &sentinel );
}


void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
{
#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

addMe->next = &sentinel;
addMe->prev = sentinel.prev;

sentinel.prev->next = addMe;
sentinel.prev = addMe;
}

void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
{
TiXmlAttribute* node;

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.
}


#ifdef TIXML_USE_STL
const TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
{
for( const TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
{
if ( node->name == name )
return node;
}
return 0;
}

/*
TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name )
{
for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
{
if ( node->name == name )
return node;
}
return 0;
}
*/
#endif


const TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const
{
for( const TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
{
if ( strcmp( node->name.c_str(), name ) == 0 )
return node;
}
return 0;
}

/*
TiXmlAttribute* TiXmlAttributeSet::Find( const char* name )
{
for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
{
if ( strcmp( node->name.c_str(), name ) == 0 )
return node;
}
return 0;
}
*/

#ifdef TIXML_USE_STL
std::istream& operator>> (std::istream & in, TiXmlNode & base)
{
TIXML_STRING tag;
tag.reserve( 8 * 1000 );
base.StreamIn( &in, &tag );

base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
return in;
}
#endif


#ifdef TIXML_USE_STL
std::ostream& operator<< (std::ostream & out, const TiXmlNode & base)
{
TiXmlPrinter printer;
printer.SetStreamPrinting();
base.Accept( &printer );
out << printer.Str();

return out;
}


std::string& operator<< (std::string& out, const TiXmlNode& base )
{
TiXmlPrinter printer;
printer.SetStreamPrinting();
base.Accept( &printer );
out.append( printer.Str() );

return out;
}
#endif


TiXmlHandle TiXmlHandle::FirstChild() const
{
if ( node )
{
TiXmlNode* child = node->FirstChild();
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::FirstChild( const char * value ) const
{
if ( node )
{
TiXmlNode* child = node->FirstChild( value );
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::FirstChildElement() const
{
if ( node )
{
TiXmlElement* child = node->FirstChildElement();
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::FirstChildElement( const char * value ) const
{
if ( node )
{
TiXmlElement* child = node->FirstChildElement( value );
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::Child( int count ) const
{
if ( node )
{
int i;
TiXmlNode* child = node->FirstChild();
for ( i=0;
child && i<count;
child = child->NextSibling(), ++i )
{
// nothing
}
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::Child( const char* value, int count ) const
{
if ( node )
{
int i;
TiXmlNode* child = node->FirstChild( value );
for ( i=0;
child && i<count;
child = child->NextSibling( value ), ++i )
{
// nothing
}
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::ChildElement( int count ) const
{
if ( node )
{
int i;
TiXmlElement* child = node->FirstChildElement();
for ( i=0;
child && i<count;
child = child->NextSiblingElement(), ++i )
{
// nothing
}
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


TiXmlHandle TiXmlHandle::ChildElement( const char* value, int count ) const
{
if ( node )
{
int i;
TiXmlElement* child = node->FirstChildElement( value );
for ( i=0;
child && i<count;
child = child->NextSiblingElement( value ), ++i )
{
// nothing
}
if ( child )
return TiXmlHandle( child );
}
return TiXmlHandle( 0 );
}


bool TiXmlPrinter::VisitEnter( const TiXmlDocument& )
{
return true;
}

bool TiXmlPrinter::VisitExit( const TiXmlDocument& )
{
return true;
}

bool TiXmlPrinter::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
{
DoIndent();
buffer += "<";
buffer += element.Value();

for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
{
buffer += " ";
attrib->Print( 0, 0, &buffer );
}

if ( !element.FirstChild() )
{
buffer += " />";
DoLineBreak();
}
else
{
buffer += ">";
if ( element.FirstChild()->ToText()
&& element.LastChild() == element.FirstChild()
&& element.FirstChild()->ToText()->CDATA() == false )
{
simpleTextPrint = true;
// no DoLineBreak()!
}
else
{
DoLineBreak();
}
}
++depth;
return true;
}


bool TiXmlPrinter::VisitExit( const TiXmlElement& element )
{
--depth;
if ( !element.FirstChild() )
{
// nothing.
}
else
{
if ( simpleTextPrint )
{
simpleTextPrint = false;
}
else
{
DoIndent();
}
buffer += "</";
buffer += element.Value();
buffer += ">";
DoLineBreak();
}
return true;
}


bool TiXmlPrinter::Visit( const TiXmlText& text )
{
if ( text.CDATA() )
{
DoIndent();
buffer += "<![CDATA[";
buffer += text.Value();
buffer += "]]>";
DoLineBreak();
}
else if ( simpleTextPrint )
{
buffer += text.Value();
}
else
{
DoIndent();
buffer += text.Value();
DoLineBreak();
}
return true;
}


bool TiXmlPrinter::Visit( const TiXmlDeclaration& declaration )
{
DoIndent();
declaration.Print( 0, 0, &buffer );
DoLineBreak();
return true;
}


bool TiXmlPrinter::Visit( const TiXmlComment& comment )
{
DoIndent();
buffer += "<!--";
buffer += comment.Value();
buffer += "-->";
DoLineBreak();
return true;
}


bool TiXmlPrinter::Visit( const TiXmlUnknown& unknown )
{
DoIndent();
buffer += "<";
buffer += unknown.Value();
buffer += ">";
DoLineBreak();
return true;
}
Something went wrong with that request. Please try again.