34 #ifndef TINYXML_INCLUDED
35 #define TINYXML_INCLUDED
38 #pragma warning( disable : 4530 )
39 #pragma warning( disable : 4786 )
49 #if defined( _DEBUG ) && !defined( DEBUG )
53 #if defined( DEBUG ) && defined( _MSC_VER )
55 #define TIXML_LOG OutputDebugString
57 #define TIXML_LOG printf
63 #define TIXML_STRING std::string
64 #define TIXML_ISTREAM std::istream
65 #define TIXML_OSTREAM std::ostream
68 #define TIXML_STRING TiXmlString
69 #define TIXML_OSTREAM TiXmlOutStream
82 class TiXmlDeclaration;
83 class TiXmlParsingData;
85 const int TIXML_MAJOR_VERSION = 2;
86 const int TIXML_MINOR_VERSION = 3;
87 const int TIXML_PATCH_VERSION = 4;
95 void Clear() { row = col = -1; }
114 TIXML_ENCODING_UNKNOWN,
116 TIXML_ENCODING_LEGACY
119 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
158 virtual void Print( FILE* cfile,
int depth )
const = 0;
189 int Row()
const {
return location.row + 1; }
190 int Column()
const {
return location.col + 1; }
192 void SetUserData(
void* user ) {
userData = user; }
193 void* GetUserData() {
return userData; }
197 static const int utf8ByteTable[256];
199 virtual const char* Parse(
const char* p,
200 TiXmlParsingData* data,
201 TiXmlEncoding encoding ) = 0;
207 TIXML_ERROR_OPENING_FILE,
208 TIXML_ERROR_OUT_OF_MEMORY,
209 TIXML_ERROR_PARSING_ELEMENT,
210 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
211 TIXML_ERROR_READING_ELEMENT_VALUE,
212 TIXML_ERROR_READING_ATTRIBUTES,
213 TIXML_ERROR_PARSING_EMPTY,
214 TIXML_ERROR_READING_END_TAG,
215 TIXML_ERROR_PARSING_UNKNOWN,
216 TIXML_ERROR_PARSING_COMMENT,
217 TIXML_ERROR_PARSING_DECLARATION,
218 TIXML_ERROR_DOCUMENT_EMPTY,
219 TIXML_ERROR_EMBEDDED_NULL,
221 TIXML_ERROR_STRING_COUNT
236 static const char* SkipWhiteSpace(
const char*, TiXmlEncoding encoding );
237 inline static bool IsWhiteSpace(
char c )
239 return ( isspace( (
unsigned char) c ) || c ==
'\n' || c ==
'\r' );
242 virtual void StreamOut (TIXML_OSTREAM *)
const = 0;
245 static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
246 static bool StreamTo( TIXML_ISTREAM * in,
int character, TIXML_STRING * tag );
253 static const char* ReadName(
const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
258 static const char* ReadText(
const char* in,
260 bool ignoreWhiteSpace,
263 TiXmlEncoding encoding );
266 static const char* GetEntity(
const char* in,
char* value,
int* length, TiXmlEncoding encoding );
270 inline static const char* GetChar(
const char* p,
char* _value,
int* length, TiXmlEncoding encoding )
273 if ( encoding == TIXML_ENCODING_UTF8 )
275 *length = utf8ByteTable[ *((
unsigned char*)p) ];
276 assert( *length >= 0 && *length < 5 );
286 return GetEntity( p, _value, length, encoding );
292 strncpy( _value, p, *length );
293 return p + (*length);
304 static void PutString(
const TIXML_STRING& str, TIXML_OSTREAM* out );
306 static void PutString(
const TIXML_STRING& str, TIXML_STRING* out );
311 static bool StringEqual(
const char* p,
314 TiXmlEncoding encoding );
316 static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
318 TiXmlCursor location;
325 static int IsAlpha(
unsigned char anyByte, TiXmlEncoding encoding );
326 static int IsAlphaNum(
unsigned char anyByte, TiXmlEncoding encoding );
327 inline static int ToLower(
int v, TiXmlEncoding encoding )
329 if ( encoding == TIXML_ENCODING_UTF8 )
331 if ( v < 128 )
return tolower( v );
339 static void ConvertUTF32ToUTF8(
unsigned long input,
char* output,
int* length );
342 TiXmlBase(
const TiXmlBase& );
343 void operator=(
const TiXmlBase& base );
348 unsigned int strLength;
354 MAX_ENTITY_LENGTH = 6
357 static Entity entity[ NUM_ENTITY ];
358 static bool condenseWhiteSpace;
435 const char *
Value()
const {
return value.c_str (); }
446 void SetValue(
const char * _value) { value = _value;}
449 void SetValue(
const std::string& _value )
452 StringToBuffer buf( _value );
453 SetValue( buf.buffer ? buf.buffer :
"" );
466 const TiXmlNode*
FirstChild(
const char * value )
const;
469 const TiXmlNode* LastChild()
const {
return lastChild; }
471 const TiXmlNode* LastChild(
const char * value )
const;
472 TiXmlNode* LastChild(
const char * value );
564 const TiXmlNode*
NextSibling(
const char * )
const;
603 virtual int Type()
const {
return type; }
642 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
646 TiXmlNode* Identify(
const char* start, TiXmlEncoding encoding );
649 const TIXML_STRING& SValue()
const {
return value ; }
654 TiXmlNode* firstChild;
655 TiXmlNode* lastChild;
663 TiXmlNode(
const TiXmlNode& );
664 void operator=(
const TiXmlNode& base );
688 TiXmlAttribute(
const std::string& _name,
const std::string& _value )
707 const char*
Name()
const {
return name.c_str (); }
708 const char*
Value()
const {
return value.c_str (); }
725 void SetName(
const char* _name ) { name = _name; }
726 void SetValue(
const char* _value ) { value = _value; }
732 void SetName(
const std::string& _name )
735 StringToBuffer buf( _name );
736 SetName ( buf.buffer ? buf.buffer :
"error" );
742 SetValue( buf.buffer ? buf.buffer :
"error" );
753 bool operator==(
const TiXmlAttribute& rhs )
const {
return rhs.name == name; }
754 bool operator<(
const TiXmlAttribute& rhs )
const {
return name < rhs.name; }
755 bool operator>(
const TiXmlAttribute& rhs )
const {
return name > rhs.name; }
760 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
763 virtual void Print( FILE* cfile,
int depth )
const;
765 virtual void StreamOut( TIXML_OSTREAM * out )
const;
768 void SetDocument( TiXmlDocument* doc ) { document = doc; }
774 TiXmlDocument* document;
803 const TiXmlAttribute* First()
const {
return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
804 TiXmlAttribute* First() {
return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
805 const TiXmlAttribute* Last()
const {
return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
806 TiXmlAttribute* Last() {
return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
845 const char*
Attribute(
const char* name )
const;
853 const char*
Attribute(
const char* name,
int* i )
const;
861 const char*
Attribute(
const char* name,
double* d )
const;
884 void SetAttribute(
const char* name,
const char * value );
887 const char*
Attribute(
const std::string& name )
const {
return Attribute( name.c_str() ); }
888 const char*
Attribute(
const std::string& name,
int* i )
const {
return Attribute( name.c_str(), i ); }
889 const char*
Attribute(
const std::string& name,
double* d )
const {
return Attribute( name.c_str(), d ); }
894 void SetAttribute(
const std::string& name,
const std::string& _value )
898 if ( n.buffer && v.buffer )
902 void SetAttribute(
const std::string& name,
int _value )
904 StringToBuffer n( name );
933 virtual TiXmlNode*
Clone()
const;
935 virtual void Print( FILE* cfile,
int depth )
const;
940 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
949 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
951 virtual void StreamOut( TIXML_OSTREAM * out )
const;
957 const char* ReadValue(
const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
961 TiXmlAttributeSet attributeSet;
978 virtual TiXmlNode*
Clone()
const;
980 virtual void Print( FILE* cfile,
int depth )
const;
985 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
992 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
994 virtual void StreamOut( TIXML_OSTREAM * out )
const;
1014 #ifdef TIXML_USE_STL
1015 TiXmlText(
const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
1022 TiXmlText(
const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT ) { copy.CopyTo(
this ); }
1023 void operator=(
const TiXmlText& base ) { base.CopyTo(
this ); }
1026 virtual void Print( FILE* cfile,
int depth )
const;
1028 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1032 virtual TiXmlNode*
Clone()
const;
1035 virtual void StreamOut ( TIXML_OSTREAM * out )
const;
1038 #ifdef TIXML_USE_STL
1039 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1065 #ifdef TIXML_USE_STL
1068 const std::string& _encoding,
1069 const std::string& _standalone );
1074 const char* _encoding,
1075 const char* _standalone );
1083 const char *
Version()
const {
return version.c_str (); }
1085 const char *
Encoding()
const {
return encoding.c_str (); }
1092 virtual void Print( FILE* cfile,
int depth )
const;
1094 virtual const char* Parse(
const char* p,
TiXmlParsingData* data, TiXmlEncoding encoding );
1099 #ifdef TIXML_USE_STL
1100 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1102 virtual void StreamOut ( TIXML_OSTREAM * out)
const;
1106 TIXML_STRING version;
1107 TIXML_STRING encoding;
1108 TIXML_STRING standalone;
1126 void operator=(
const TiXmlUnknown& copy ) { copy.CopyTo(
this ); }
1131 virtual void Print( FILE* cfile,
int depth )
const;
1133 virtual const char* Parse(
const char* p,
TiXmlParsingData* data, TiXmlEncoding encoding );
1138 #ifdef TIXML_USE_STL
1139 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1141 virtual void StreamOut ( TIXML_OSTREAM * out )
const;
1160 #ifdef TIXML_USE_STL
1174 bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1178 bool LoadFile(
const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1180 bool SaveFile(
const char * filename )
const;
1182 #ifdef TIXML_USE_STL
1183 bool LoadFile(
const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )
1186 return ( f.buffer && LoadFile( f.buffer, encoding ));
1191 return ( f.buffer && SaveFile( f.buffer ));
1199 virtual const char*
Parse(
const char* p,
TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1216 const char *
ErrorDesc()
const {
return errorDesc.c_str (); }
1255 int TabSize()
const {
return tabsize; }
1263 errorLocation.row = errorLocation.col = 0;
1271 virtual void Print( FILE* cfile,
int depth = 0 )
const;
1273 void SetError(
int err,
const char* errorLocation,
TiXmlParsingData* prevData, TiXmlEncoding encoding );
1276 virtual void StreamOut ( TIXML_OSTREAM * out)
const;
1279 #ifdef TIXML_USE_STL
1280 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1288 TIXML_STRING errorDesc;
1411 #ifdef TIXML_USE_STL
1415 TiXmlHandle Child(
const std::string& _value,
int index )
const {
return Child( _value.c_str(), index ); }
1440 #pragma warning( default : 4530 )
1441 #pragma warning( default : 4786 )
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given null terminated block of xml data.
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
TiXmlDeclaration()
Construct an empty declaration.
void SetDoubleValue(double value)
Set the value from a double.
bool NoChildren() const
Returns true if this node has no children.
void SetValue(const char *_value)
Set the value.
int Column() const
See Row()
bool SaveFile(const std::string &filename) const
< STL std::string version.
int IntValue() const
Return the value of this attribute, converted to an integer.
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
friend std::ostream & operator<<(std::ostream &out, const TiXmlNode &base)
An output stream operator, for every class.
virtual TiXmlNode * Clone() const
Creates a new Element and returns it - the returned element is a copy.
TiXmlElement(const char *in_value)
Construct an element.
int ErrorRow()
Returns the location (if known) of the error.
virtual void Print(FILE *cfile, int depth) const =0
All TinyXml classes can print themselves to a filestream.
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Add a new node related to this.
TiXmlUnknown * ToUnknown()
Cast to a more defined type. Will return null not of the requested type.
virtual TiXmlNode * Clone() const =0
Create an exact duplicate of this node and return it.
void SetValue(const char *_value)
Changes the value of the node.
TiXmlNode * LastChild(const std::string &_value)
STL std::string form.
virtual void Print(FILE *cfile, int depth) const
Print this declaration to a FILE stream.
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
void Clear()
Delete all the children of this node. Does not affect 'this'.
In correct XML the declaration is the first entry in the file.
TiXmlElement * NextSiblingElement(const std::string &_value)
STL std::string form.
static bool IsWhiteSpaceCondensed()
Return the current white space setting.
const TiXmlNode * LastChild(const std::string &_value) const
STL std::string form.
const TiXmlElement * FirstChildElement(const std::string &_value) const
STL std::string form.
void SetTabSize(int _tabsize)
By calling this method, with a tab size greater than 0, the row and column of each node and attribute...
const char * Version() const
Version. Will return an empty string if none was found.
const char * Encoding() const
Encoding. Will return an empty string if none was found.
An attribute is a name-value pair.
virtual TiXmlNode * Clone() const
Creates a copy of this Unknown and returns it.
const TiXmlNode * NextSibling() const
Navigate to a sibling node.
virtual void Print(FILE *cfile, int depth) const
Write this text object to a FILE stream.
void SetIntValue(int value)
Set the value from an integer.
const char * Value() const
The meaning of 'value' changes for the specific type of TiXmlNode.
TiXmlNode * Node() const
Return the handle as a TiXmlNode. This may return null.
const char * Name() const
Return the name of this attribute.
A TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thi...
const TiXmlNode * IterateChildren(const std::string &_value, const TiXmlNode *previous) const
STL std::string form.
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
const TiXmlDocument * ToDocument() const
Cast to a more defined type. Will return null not of the requested type.
const TiXmlUnknown * ToUnknown() const
Cast to a more defined type. Will return null not of the requested type.
virtual void Print(FILE *cfile, int depth) const
Print this Unknown to a FILE stream.
TiXmlDeclaration * ToDeclaration()
Cast to a more defined type. Will return null not of the requested type.
TiXmlBase is a base class for every class in TinyXml.
int ErrorCol()
The column where the error occured. See ErrorRow()
const TiXmlNode * FirstChild(const std::string &_value) const
STL std::string form.
TiXmlNode * IterateChildren(const std::string &_value, TiXmlNode *previous)
STL std::string form.
friend std::istream & operator>>(std::istream &in, TiXmlNode &base)
An input stream operator, for every class.
TiXmlUnknown * Unknown() const
Return the handle as a TiXmlUnknown. This may return null;.
const TiXmlAttribute * FirstAttribute() const
Access the first attribute in this element.
Always the top level node.
void SetAttribute(const std::string &name, const std::string &_value)
STL std::string form.
double DoubleValue() const
Return the value of this attribute, converted to a double.
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
Add a new node related to this.
const TiXmlComment * ToComment() const
Cast to a more defined type. Will return null not of the requested type.
int QueryDoubleAttribute(const char *name, double *value) const
QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
Any tag that tinyXml doesn't recognize is saved as an unknown.
TiXmlAttribute()
Construct an empty attribute.
void SetName(const char *_name)
Set the name of this attribute.
TiXmlText * ToText()
Cast to a more defined type. Will return null not of the requested type.
void SetValue(const std::string &_value)
STL std::string form.
const TiXmlDeclaration * ToDeclaration() const
Cast to a more defined type. Will return null not of the requested type.
The parent class for everything in the Document Object Model.
const char * Attribute(const char *name) const
Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
TiXmlText * Text() const
Return the handle as a TiXmlText. This may return null.
TiXmlHandle ChildElement(const char *value, int index) const
Return a handle to the "index" child element with the given name.
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Replace a child of this node.
const TiXmlAttribute * LastAttribute() const
Access the last attribute in this element.
int QueryDoubleValue(double *value) const
QueryDoubleValue examines the value string. See QueryIntValue().
virtual TiXmlNode * Clone() const
Create an exact duplicate of this node and return it.
const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null not of the requested type.
int QueryDoubleAttribute(const char *name, float *value) const
QueryFloatAttribute examines the attribute - see QueryIntAttribute().
void * userData
Field containing a generic user pointer.
const TiXmlElement * RootElement() const
Get the root element – the only top level element – of the document.
const char * Standalone() const
Is this a standalone document?
int QueryIntAttribute(const char *name, int *value) const
QueryIntAttribute examines the attribute - it is an alternative to the Attribute() method with richer...
NodeType
The types of XML nodes supported by TinyXml.
virtual TiXmlNode * Clone() const
Creates a copy of this Declaration and returns it.
The element is a container class.
int ErrorId() const
Generally, you probably want the error string ( ErrorDesc() ).
const TiXmlNode * PreviousSibling() const
Navigate to a sibling node.
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
const TiXmlNode * PreviousSibling(const std::string &_value) const
STL std::string form.
bool Error() const
If an error occurs, Error will be set to true.
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.
TiXmlNode * FirstChild(const std::string &_value)
STL std::string form.
TiXmlNode * NextSibling(const std::string &_value)
STL std::string form.
void SetDoubleAttribute(const char *name, double value)
Sets an attribute of name to a given value.
void RemoveAttribute(const std::string &name)
STL std::string form.
TiXmlHandle(TiXmlNode *node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
const TiXmlElement * NextSiblingElement(const std::string &_value) const
STL std::string form.
TiXmlNode * Parent()
One step up the DOM.
void RemoveAttribute(const char *name)
Deletes an attribute with the given name.
TiXmlDocument * ToDocument()
Cast to a more defined type. Will return null not of the requested type.
virtual void Print(FILE *cfile, int depth) const
All TinyXml classes can print themselves to a filestream.
void ClearError()
If you have handled the error, it can be reset with this call.
TiXmlHandle(const TiXmlHandle &ref)
Copy constructor.
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
TiXmlHandle Child(const char *value, int index) const
Return a handle to the "index" child with the given name.
const char * Value() const
Return the value of this attribute.
TiXmlDocument()
Create an empty document, that has no name.
virtual TiXmlNode * Clone() const
[internal use] Creates a new Element and returns it.
const TiXmlElement * NextSiblingElement() const
Convenience function to get through elements.
TiXmlNode * InsertAfterChild(TiXmlNode *afterThis, const TiXmlNode &addThis)
Add a new node related to this.
TiXmlElement * Element() const
Return the handle as a TiXmlElement. This may return null.
TiXmlHandle FirstChildElement() const
Return a handle to the first child element.
int QueryIntValue(int *value) const
QueryIntValue examines the value string.
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
An alternate way to walk the children of a node.
const TiXmlText * ToText() const
Cast to a more defined type. Will return null not of the requested type.
void SetAttribute(const char *name, const char *value)
Sets an attribute of name to a given value.
static void SetCondenseWhiteSpace(bool condense)
The world does not agree on whether white space should be kept or not.
TiXmlAttribute(const char *_name, const char *_value)
Construct an attribute with a name and value.
virtual void Print(FILE *cfile, int depth) const
All TinyXml classes can print themselves to a filestream.
const TiXmlDocument * GetDocument() const
Return a pointer to the Document this node lives in.
void Print() const
Dump the document to standard out.
TiXmlNode * PreviousSibling(const std::string &_value)
STL std::string form.
const TiXmlAttribute * Next() const
Get the next sibling attribute in the DOM. Returns null at end.
TiXmlText(const char *initValue)
Constructor.
TiXmlElement * FirstChildElement(const std::string &_value)
STL std::string form.
TiXmlHandle FirstChild() const
Return a handle to the first child node.
bool LoadFile(const std::string &filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
int Row() const
Return the position, in the original source file, of this node or attribute.
TiXmlComment * ToComment()
Cast to a more defined type. Will return null not of the requested type.
TiXmlNode * LastChild()
The last child of this node. Will be null if there are no children.
const TiXmlAttribute * Previous() const
Get the previous sibling attribute in the DOM. Returns null at beginning.
TiXmlElement * ToElement()
Cast to a more defined type. Will return null not of the requested type.
virtual int Type() const
Query the type (as an enumerated value, above) of this node.