34 FILE* TiXmlFOpen(
const char* filename,
const char* mode );
36 bool TiXmlBase::condenseWhiteSpace =
true;
39 FILE* TiXmlFOpen(
const char* filename,
const char* mode )
41 #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) 43 errno_t err = fopen_s( &fp, filename, mode );
48 return fopen( filename, mode );
56 while( i<(
int)str.length() )
58 unsigned char c = (
unsigned char) str[i];
61 && i < ( (
int)str.length() - 2 )
75 while ( i<(
int)str.length()-1 )
77 outString->append( str.c_str() + i, 1 );
85 outString->append( entity[0].str, entity[0].strLength );
90 outString->append( entity[1].str, entity[1].strLength );
95 outString->append( entity[2].str, entity[2].strLength );
100 outString->append( entity[3].str, entity[3].strLength );
103 else if ( c ==
'\'' )
105 outString->append( entity[4].str, entity[4].strLength );
114 #if defined(TIXML_SNPRINTF) 115 TIXML_SNPRINTF( buf,
sizeof(buf),
"&#x%02X;", (
unsigned) ( c & 0xff ) );
117 sprintf( buf,
"&#x%02X;", (
unsigned) ( c & 0xff ) );
122 outString->append( buf, (
int)strlen( buf ) );
129 *outString += (char) c;
136 TiXmlNode::TiXmlNode( NodeType _type ) :
TiXmlBase()
147 TiXmlNode::~TiXmlNode()
161 void TiXmlNode::CopyTo(
TiXmlNode* target )
const 165 target->location = location;
188 assert( node->parent == 0 || node->parent ==
this );
191 if ( node->
Type() == TiXmlNode::TINYXML_DOCUMENT )
195 GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
201 node->prev = lastChild;
205 lastChild->next = node;
216 if ( addThis.
Type() == TiXmlNode::TINYXML_DOCUMENT )
219 GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
226 return LinkEndChild( node );
232 if ( !beforeThis || beforeThis->parent !=
this ) {
235 if ( addThis.
Type() == TiXmlNode::TINYXML_DOCUMENT )
238 GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
247 node->next = beforeThis;
248 node->prev = beforeThis->prev;
249 if ( beforeThis->prev )
251 beforeThis->prev->next = node;
255 assert( firstChild == beforeThis );
258 beforeThis->prev = node;
265 if ( !afterThis || afterThis->parent !=
this ) {
268 if ( addThis.
Type() == TiXmlNode::TINYXML_DOCUMENT )
271 GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
280 node->prev = afterThis;
281 node->next = afterThis->next;
282 if ( afterThis->next )
284 afterThis->next->prev = node;
288 assert( lastChild == afterThis );
291 afterThis->next = node;
301 if ( replaceThis->parent !=
this )
308 document->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
316 node->next = replaceThis->next;
317 node->prev = replaceThis->prev;
319 if ( replaceThis->next )
320 replaceThis->next->prev = node;
324 if ( replaceThis->prev )
325 replaceThis->prev->next = node;
341 if ( removeThis->parent !=
this )
347 if ( removeThis->next )
348 removeThis->next->prev = removeThis->prev;
350 lastChild = removeThis->prev;
352 if ( removeThis->prev )
353 removeThis->prev->next = removeThis->next;
355 firstChild = removeThis->next;
364 for ( node = firstChild; node; node = node->next )
366 if ( strcmp( node->
Value(), _value ) == 0 )
373 const TiXmlNode* TiXmlNode::LastChild(
const char * _value )
const 376 for ( node = lastChild; node; node = node->prev )
378 if ( strcmp( node->
Value(), _value ) == 0 )
393 assert( previous->parent ==
this );
403 return FirstChild( val );
407 assert( previous->parent ==
this );
416 for ( node = next; node; node = node->next )
418 if ( strcmp( node->
Value(), _value ) == 0 )
428 for ( node = prev; node; node = node->prev )
430 if ( strcmp( node->
Value(), _value ) == 0 )
440 TIXML_STRING str( name );
447 attributeSet.Remove( node );
456 for ( node = FirstChild();
471 for ( node = FirstChild( _value );
486 for ( node = NextSibling();
501 for ( node = NextSibling( _value );
516 for( node =
this; node; node = node->parent )
528 firstChild = lastChild = 0;
535 :
TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
537 firstChild = lastChild = 0;
544 :
TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
546 firstChild = lastChild = 0;
559 TiXmlElement::~TiXmlElement()
565 void TiXmlElement::ClearThis()
568 while( attributeSet.First() )
571 attributeSet.Remove( node );
581 return node->
Value();
591 return &attrib->ValueStr();
600 const char* result = 0;
603 result = attrib->
Value();
616 const std::string* result = 0;
619 result = &attrib->ValueStr();
632 const char* result = 0;
635 result = attrib->
Value();
648 const std::string* result = 0;
651 result = &attrib->ValueStr();
665 return TIXML_NO_ATTRIBUTE;
674 return TIXML_NO_ATTRIBUTE;
678 *value = (unsigned)ival;
687 return TIXML_NO_ATTRIBUTE;
689 int result = TIXML_WRONG_TYPE;
690 if ( StringEqual( node->
Value(),
"true",
true, TIXML_ENCODING_UNKNOWN )
691 || StringEqual( node->
Value(),
"yes",
true, TIXML_ENCODING_UNKNOWN )
692 || StringEqual( node->
Value(),
"1",
true, TIXML_ENCODING_UNKNOWN ) )
695 result = TIXML_SUCCESS;
697 else if ( StringEqual( node->
Value(),
"false",
true, TIXML_ENCODING_UNKNOWN )
698 || StringEqual( node->
Value(),
"no",
true, TIXML_ENCODING_UNKNOWN )
699 || StringEqual( node->
Value(),
"0",
true, TIXML_ENCODING_UNKNOWN ) )
702 result = TIXML_SUCCESS;
714 return TIXML_NO_ATTRIBUTE;
724 return TIXML_NO_ATTRIBUTE;
734 return TIXML_NO_ATTRIBUTE;
804 for ( i=0; i<depth; i++ ) {
805 fprintf( cfile,
" " );
808 fprintf( cfile,
"<%s", value.c_str() );
811 for ( attrib = attributeSet.First(); attrib; attrib = attrib->
Next() )
813 fprintf( cfile,
" " );
814 attrib->Print( cfile, depth );
824 fprintf( cfile,
" />" );
826 else if ( firstChild == lastChild && firstChild->
ToText() )
828 fprintf( cfile,
">" );
829 firstChild->
Print( cfile, depth + 1 );
830 fprintf( cfile,
"</%s>", value.c_str() );
834 fprintf( cfile,
">" );
836 for ( node = firstChild; node; node=node->
NextSibling() )
840 fprintf( cfile,
"\n" );
842 node->
Print( cfile, depth+1 );
844 fprintf( cfile,
"\n" );
845 for( i=0; i<depth; ++i ) {
846 fprintf( cfile,
" " );
848 fprintf( cfile,
"</%s>", value.c_str() );
856 TiXmlNode::CopyTo( target );
861 for( attribute = attributeSet.First();
863 attribute = attribute->
Next() )
869 for ( node = firstChild; node; node = node->
NextSibling() )
877 if ( visitor->
VisitEnter( *
this, attributeSet.First() ) )
881 if ( !node->Accept( visitor ) )
906 return childText->
Value();
916 useMicrosoftBOM =
false;
923 useMicrosoftBOM =
false;
924 value = documentName;
933 useMicrosoftBOM =
false;
934 value = documentName;
967 TIXML_STRING filename( _filename );
971 FILE* file = TiXmlFOpen( value.c_str (),
"rb" );
975 bool result =
LoadFile( file, encoding );
981 SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
990 SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
1000 fseek( file, 0, SEEK_END );
1001 length = ftell( file );
1002 fseek( file, 0, SEEK_SET );
1007 SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
1032 char* buf =
new char[ length+1 ];
1035 if ( fread( buf, length, 1, file ) != 1 ) {
1037 SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
1052 const char* p = buf;
1054 const char CR = 0x0d;
1055 const char LF = 0x0a;
1059 assert( p < (buf+length) );
1060 assert( q <= (buf+length) );
1074 assert( q <= (buf+length) );
1077 Parse( buf, 0, encoding );
1087 FILE* fp = TiXmlFOpen( filename,
"w" );
1100 if ( useMicrosoftBOM )
1102 const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
1103 const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
1104 const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
1106 fputc( TIXML_UTF_LEAD_0, fp );
1107 fputc( TIXML_UTF_LEAD_1, fp );
1108 fputc( TIXML_UTF_LEAD_2, fp );
1111 return (ferror(fp) == 0);
1117 TiXmlNode::CopyTo( target );
1119 target->error = error;
1120 target->errorId = errorId;
1121 target->errorDesc = errorDesc;
1122 target->tabsize = tabsize;
1123 target->errorLocation = errorLocation;
1124 target->useMicrosoftBOM = useMicrosoftBOM;
1127 for ( node = firstChild; node; node = node->
NextSibling() )
1150 node->Print( cfile, depth );
1151 fprintf( cfile,
"\n" );
1162 if ( !node->Accept( visitor ) )
1174 if ( next->value.empty() && next->name.empty() )
1194 if ( prev->value.empty() && prev->name.empty() )
1217 if (value.find (
'\"') == TIXML_STRING::npos) {
1219 fprintf (cfile,
"%s=\"%s\"", n.c_str(), v.c_str() );
1222 (*str) += n; (*str) +=
"=\""; (*str) += v; (*str) +=
"\"";
1227 fprintf (cfile,
"%s='%s'", n.c_str(), v.c_str() );
1230 (*str) += n; (*str) +=
"='"; (*str) += v; (*str) +=
"'";
1238 if ( TIXML_SSCANF( value.c_str(),
"%d", ival ) == 1 )
1239 return TIXML_SUCCESS;
1240 return TIXML_WRONG_TYPE;
1245 if ( TIXML_SSCANF( value.c_str(),
"%lf", dval ) == 1 )
1246 return TIXML_SUCCESS;
1247 return TIXML_WRONG_TYPE;
1253 #if defined(TIXML_SNPRINTF) 1254 TIXML_SNPRINTF(buf,
sizeof(buf),
"%d", _value);
1256 sprintf (buf,
"%d", _value);
1264 #if defined(TIXML_SNPRINTF) 1265 TIXML_SNPRINTF( buf,
sizeof(buf),
"%g", _value);
1267 sprintf (buf,
"%g", _value);
1274 return atoi (value.c_str ());
1279 return atof (value.c_str ());
1285 copy.CopyTo(
this );
1292 base.CopyTo(
this );
1300 for (
int i=0; i<depth; i++ )
1302 fprintf( cfile,
" " );
1304 fprintf( cfile,
"<!--%s-->", value.c_str() );
1308 void TiXmlComment::CopyTo(
TiXmlComment* target )
const 1310 TiXmlNode::CopyTo( target );
1316 return visitor->
Visit( *
this );
1338 fprintf( cfile,
"\n" );
1339 for ( i=0; i<depth; i++ ) {
1340 fprintf( cfile,
" " );
1342 fprintf( cfile,
"<![CDATA[%s]]>\n", value.c_str() );
1346 TIXML_STRING buffer;
1348 fprintf( cfile,
"%s", buffer.c_str() );
1353 void TiXmlText::CopyTo(
TiXmlText* target )
const 1355 TiXmlNode::CopyTo( target );
1356 target->cdata = cdata;
1362 return visitor->
Visit( *
this );
1380 const char * _encoding,
1381 const char * _standalone )
1385 encoding = _encoding;
1386 standalone = _standalone;
1390 #ifdef TIXML_USE_STL 1392 const std::string& _encoding,
1393 const std::string& _standalone )
1394 :
TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
1397 encoding = _encoding;
1398 standalone = _standalone;
1404 :
TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
1406 copy.CopyTo(
this );
1413 copy.CopyTo(
this );
1418 void TiXmlDeclaration::Print( FILE* cfile,
int , TIXML_STRING* str )
const 1420 if ( cfile ) fprintf( cfile,
"<?xml " );
1421 if ( str ) (*str) +=
"<?xml ";
1423 if ( !version.empty() ) {
1424 if ( cfile ) fprintf (cfile,
"version=\"%s\" ", version.c_str ());
1425 if ( str ) { (*str) +=
"version=\""; (*str) += version; (*str) +=
"\" "; }
1427 if ( !encoding.empty() ) {
1428 if ( cfile ) fprintf (cfile,
"encoding=\"%s\" ", encoding.c_str ());
1429 if ( str ) { (*str) +=
"encoding=\""; (*str) += encoding; (*str) +=
"\" "; }
1431 if ( !standalone.empty() ) {
1432 if ( cfile ) fprintf (cfile,
"standalone=\"%s\" ", standalone.c_str ());
1433 if ( str ) { (*str) +=
"standalone=\""; (*str) += standalone; (*str) +=
"\" "; }
1435 if ( cfile ) fprintf( cfile,
"?>" );
1436 if ( str ) (*str) +=
"?>";
1442 TiXmlNode::CopyTo( target );
1444 target->version = version;
1445 target->encoding = encoding;
1446 target->standalone = standalone;
1452 return visitor->
Visit( *
this );
1470 for (
int i=0; i<depth; i++ )
1471 fprintf( cfile,
" " );
1472 fprintf( cfile,
"<%s>", value.c_str() );
1476 void TiXmlUnknown::CopyTo(
TiXmlUnknown* target )
const 1478 TiXmlNode::CopyTo( target );
1484 return visitor->
Visit( *
this );
1500 TiXmlAttributeSet::TiXmlAttributeSet()
1502 sentinel.next = &sentinel;
1503 sentinel.prev = &sentinel;
1507 TiXmlAttributeSet::~TiXmlAttributeSet()
1509 assert( sentinel.next == &sentinel );
1510 assert( sentinel.prev == &sentinel );
1516 #ifdef TIXML_USE_STL 1517 assert( !Find( TIXML_STRING( addMe->
Name() ) ) );
1519 assert( !Find( addMe->
Name() ) );
1522 addMe->next = &sentinel;
1523 addMe->prev = sentinel.prev;
1525 sentinel.prev->next = addMe;
1526 sentinel.prev = addMe;
1533 for( node = sentinel.next; node != &sentinel; node = node->next )
1535 if ( node == removeMe )
1537 node->prev->next = node->next;
1538 node->next->prev = node->prev;
1548 #ifdef TIXML_USE_STL 1549 TiXmlAttribute* TiXmlAttributeSet::Find(
const std::string& name )
const 1551 for(
TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
1553 if ( node->name == name )
1559 TiXmlAttribute* TiXmlAttributeSet::FindOrCreate(
const std::string& _name )
1572 TiXmlAttribute* TiXmlAttributeSet::Find(
const char* name )
const 1574 for(
TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
1576 if ( strcmp( node->name.c_str(), name ) == 0 )
1583 TiXmlAttribute* TiXmlAttributeSet::FindOrCreate(
const char* _name )
1595 #ifdef TIXML_USE_STL 1596 std::istream& operator>> (std::istream & in,
TiXmlNode & base)
1599 tag.reserve( 8 * 1000 );
1600 base.StreamIn( &in, &tag );
1602 base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
1608 #ifdef TIXML_USE_STL 1609 std::ostream& operator<< (std::ostream & out,
const TiXmlNode & base)
1614 out << printer.Str();
1620 std::string& operator<< (std::string& out,
const TiXmlNode& base )
1625 out.append( printer.Str() );
1770 buffer += element.
Value();
1772 for(
const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->
Next() )
1775 attrib->Print( 0, 0, &buffer );
1787 && element.LastChild() == element.
FirstChild()
1790 simpleTextPrint =
true;
1812 if ( simpleTextPrint )
1814 simpleTextPrint =
false;
1821 buffer += element.
Value();
1834 buffer +=
"<![CDATA[";
1835 buffer += text.
Value();
1839 else if ( simpleTextPrint )
1860 declaration.Print( 0, 0, &buffer );
1870 buffer += comment.
Value();
1881 buffer += unknown.
Value();
TiXmlHandle FirstChildElement() const
Return a handle to the first child element.
void SetDoubleAttribute(const char *name, double value)
virtual TiXmlNode * Clone() const
Creates a copy of this Declaration and returns it.
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
int QueryDoubleAttribute(const char *name, double *_value) const
QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
void SetDoubleValue(double _value)
Set the value from a double.
void RemoveAttribute(const char *name)
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.
virtual bool Accept(TiXmlVisitor *visitor) const
void SetIntValue(int _value)
Set the value from an integer.
TiXmlDocument()
Create an empty document, that has no name.
virtual TiXmlNode * Clone() const
Creates a copy of this Unknown and returns it.
const char * Value() const
int IntValue() const
Return the value of this attribute, converted to an integer.
int QueryIntAttribute(const char *name, int *_value) const
const char * Name() const
Return the name of this attribute.
const TiXmlAttribute * Previous() const
Get the previous sibling attribute in the DOM. Returns null at beginning.
virtual TiXmlNode * Clone() const
Creates a new Element and returns it - the returned element is a copy.
const TiXmlNode * PreviousSibling() const
Navigate to a sibling node.
virtual bool Accept(TiXmlVisitor *visitor) const
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
int QueryIntValue(int *_value) const
void SetName(const char *_name)
Set the name of this attribute.
int QueryBoolAttribute(const char *name, bool *_value) const
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
void Clear()
Delete all the children of this node. Does not affect 'this'.
virtual bool Accept(TiXmlVisitor *content) const
virtual bool VisitEnter(const TiXmlDocument &)
Visit a document.
const TiXmlElement * NextSiblingElement() const
TiXmlElement(const char *in_value)
Construct an element.
TiXmlDeclaration()
Construct an empty declaration.
virtual TiXmlNode * Clone() const
[internal use] Creates a new Element and returns it.
static void EncodeString(const TIXML_STRING &str, TIXML_STRING *out)
virtual TiXmlNode * Clone() const
void * userData
Field containing a generic user pointer.
const char * Attribute(const char *name) const
void SetValue(const char *_value)
Set the value.
virtual void Print(FILE *cfile, int depth) const
virtual bool Accept(TiXmlVisitor *content) const
TiXmlNode * InsertAfterChild(TiXmlNode *afterThis, const TiXmlNode &addThis)
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null if not of the requested type.
const TiXmlNode * NextSibling() const
Navigate to a sibling node.
virtual bool Visit(const TiXmlDeclaration &)
Visit a declaration.
virtual TiXmlNode * Clone() const =0
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
void SetAttribute(const char *name, const char *_value)
void SetValue(const char *_value)
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
virtual void Print(FILE *cfile, int depth) const =0
const char * GetText() const
virtual void Print(FILE *cfile, int depth) const
int QueryUnsignedAttribute(const char *name, unsigned *_value) const
QueryUnsignedAttribute examines the attribute - see QueryIntAttribute().
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
virtual const TiXmlDocument * ToDocument() const
Cast to a more defined type. Will return null if not of the requested type.
virtual bool Accept(TiXmlVisitor *content) const
int QueryDoubleValue(double *_value) const
QueryDoubleValue examines the value string. See QueryIntValue().
virtual bool Accept(TiXmlVisitor *visitor) const =0
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
TiXmlHandle FirstChild() const
Return a handle to the first child node.
virtual bool VisitExit(const TiXmlDocument &doc)
Visit a document.
TiXmlHandle Child(const char *value, int index) const
virtual bool VisitExit(const TiXmlDocument &)
Visit a document.
const char * Value() const
Return the value of this attribute.
const TiXmlAttribute * Next() const
Get the next sibling attribute in the DOM. Returns null at end.
const TiXmlDocument * GetDocument() const
virtual void Print(FILE *cfile, int depth) const
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
virtual bool VisitEnter(const TiXmlDocument &doc)
Visit a document.
virtual void Print(FILE *cfile, int depth) const
double DoubleValue() const
Return the value of this attribute, converted to a double.
bool CDATA() const
Queries whether this represents text using a CDATA section.
TiXmlHandle ChildElement(const char *value, int index) const
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
virtual bool Visit(const TiXmlDeclaration &declaration)
Visit a declaration.