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
238 static const char* SkipWhiteSpace(
const char*, TiXmlEncoding encoding );
239 inline static bool IsWhiteSpace(
char c )
241 return ( isspace( (
unsigned char) c ) || c ==
'\n' || c ==
'\r' );
244 virtual void StreamOut (TIXML_OSTREAM *)
const = 0;
247 static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
248 static bool StreamTo( TIXML_ISTREAM * in,
int character, TIXML_STRING * tag );
255 static const char* ReadName(
const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
260 static const char* ReadText(
const char* in,
262 bool ignoreWhiteSpace,
265 TiXmlEncoding encoding );
268 static const char* GetEntity(
const char* in,
char* value,
int* length, TiXmlEncoding encoding );
272 inline static const char* GetChar(
const char* p,
char* _value,
int* length, TiXmlEncoding encoding )
275 if ( encoding == TIXML_ENCODING_UTF8 )
277 *length = utf8ByteTable[ *((
unsigned char*)p) ];
278 assert( *length >= 0 && *length < 5 );
288 return GetEntity( p, _value, length, encoding );
294 strncpy( _value, p, *length );
295 return p + (*length);
306 static void PutString(
const TIXML_STRING& str, TIXML_OSTREAM* out );
308 static void PutString(
const TIXML_STRING& str, TIXML_STRING* out );
313 static bool StringEqual(
const char* p,
316 TiXmlEncoding encoding );
318 static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
320 TiXmlCursor location{};
327 static int IsAlpha(
unsigned char anyByte, TiXmlEncoding encoding );
328 static int IsAlphaNum(
unsigned char anyByte, TiXmlEncoding encoding );
329 inline static int ToLower(
int v, TiXmlEncoding encoding )
331 if ( encoding == TIXML_ENCODING_UTF8 )
333 if ( v < 128 )
return tolower( v );
341 static void ConvertUTF32ToUTF8(
unsigned long input,
char* output,
int* length );
344 TiXmlBase(
const TiXmlBase& );
345 void operator=(
const TiXmlBase& base );
350 unsigned int strLength;
356 MAX_ENTITY_LENGTH = 6
359 static Entity entity[ NUM_ENTITY ];
360 static bool condenseWhiteSpace;
437 const char *
Value()
const {
return value.c_str (); }
448 void SetValue(
const char * _value) { value = _value;}
451 void SetValue(
const std::string& _value )
454 StringToBuffer buf( _value );
455 SetValue( buf.buffer ? buf.buffer :
"" );
468 const TiXmlNode*
FirstChild(
const char * value )
const;
471 const TiXmlNode* LastChild()
const {
return lastChild; }
473 const TiXmlNode* LastChild(
const char * value )
const;
474 TiXmlNode* LastChild(
const char * value );
566 const TiXmlNode*
NextSibling(
const char * )
const;
605 virtual int Type()
const {
return type; }
644 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
648 TiXmlNode* Identify(
const char* start, TiXmlEncoding encoding );
651 const TIXML_STRING& SValue()
const {
return value ; }
656 TiXmlNode* firstChild{};
657 TiXmlNode* lastChild{};
659 TIXML_STRING value{};
665 TiXmlNode(
const TiXmlNode& );
666 void operator=(
const TiXmlNode& base );
690 TiXmlAttribute(
const std::string& _name,
const std::string& _value )
709 const char*
Name()
const {
return name.c_str (); }
710 const char*
Value()
const {
return value.c_str (); }
727 void SetName(
const char* _name ) { name = _name; }
728 void SetValue(
const char* _value ) { value = _value; }
734 void SetName(
const std::string& _name )
737 StringToBuffer buf( _name );
738 SetName ( buf.buffer ? buf.buffer :
"error" );
744 SetValue( buf.buffer ? buf.buffer :
"error" );
755 bool operator==(
const TiXmlAttribute& rhs )
const {
return rhs.name == name; }
756 bool operator<(
const TiXmlAttribute& rhs )
const {
return name < rhs.name; }
757 bool operator>(
const TiXmlAttribute& rhs )
const {
return name > rhs.name; }
762 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
765 virtual void Print( FILE* cfile,
int depth )
const;
767 virtual void StreamOut( TIXML_OSTREAM * out )
const;
770 void SetDocument( TiXmlDocument* doc ) { document = doc; }
776 TiXmlDocument* document{};
778 TIXML_STRING value{};
805 const TiXmlAttribute* First()
const {
return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
806 TiXmlAttribute* First() {
return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
807 const TiXmlAttribute* Last()
const {
return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
808 TiXmlAttribute* Last() {
return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
847 const char*
Attribute(
const char* name )
const;
855 const char*
Attribute(
const char* name,
int* i )
const;
863 const char*
Attribute(
const char* name,
double* d )
const;
886 void SetAttribute(
const char* name,
const char * value );
889 const char*
Attribute(
const std::string& name )
const {
return Attribute( name.c_str() ); }
890 const char*
Attribute(
const std::string& name,
int* i )
const {
return Attribute( name.c_str(), i ); }
891 const char*
Attribute(
const std::string& name,
double* d )
const {
return Attribute( name.c_str(), d ); }
896 void SetAttribute(
const std::string& name,
const std::string& _value )
900 if ( n.buffer && v.buffer )
904 void SetAttribute(
const std::string& name,
int _value )
906 StringToBuffer n( name );
935 virtual TiXmlNode*
Clone()
const;
937 virtual void Print( FILE* cfile,
int depth )
const;
942 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
951 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
953 virtual void StreamOut( TIXML_OSTREAM * out )
const;
959 const char* ReadValue(
const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
963 TiXmlAttributeSet attributeSet{};
980 virtual TiXmlNode*
Clone()
const;
982 virtual void Print( FILE* cfile,
int depth )
const;
987 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
994 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
996 virtual void StreamOut( TIXML_OSTREAM * out )
const;
1016 #ifdef TIXML_USE_STL
1017 TiXmlText(
const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
1024 TiXmlText(
const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT ) { copy.CopyTo(
this ); }
1028 virtual void Print( FILE* cfile,
int depth )
const;
1030 virtual const char* Parse(
const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1034 virtual TiXmlNode*
Clone()
const;
1037 virtual void StreamOut ( TIXML_OSTREAM * out )
const;
1040 #ifdef TIXML_USE_STL
1041 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1067 #ifdef TIXML_USE_STL
1070 const std::string& _encoding,
1071 const std::string& _standalone );
1076 const char* _encoding,
1077 const char* _standalone );
1085 const char *
Version()
const {
return version.c_str (); }
1087 const char *
Encoding()
const {
return encoding.c_str (); }
1094 virtual void Print( FILE* cfile,
int depth )
const;
1096 virtual const char* Parse(
const char* p,
TiXmlParsingData* data, TiXmlEncoding encoding );
1101 #ifdef TIXML_USE_STL
1102 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1104 virtual void StreamOut ( TIXML_OSTREAM * out)
const;
1108 TIXML_STRING version{};
1109 TIXML_STRING encoding{};
1110 TIXML_STRING standalone{};
1133 virtual void Print( FILE* cfile,
int depth )
const;
1135 virtual const char* Parse(
const char* p,
TiXmlParsingData* data, TiXmlEncoding encoding );
1140 #ifdef TIXML_USE_STL
1141 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1143 virtual void StreamOut ( TIXML_OSTREAM * out )
const;
1162 #ifdef TIXML_USE_STL
1176 bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1180 bool LoadFile(
const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1182 bool SaveFile(
const char * filename )
const;
1184 #ifdef TIXML_USE_STL
1185 bool LoadFile(
const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )
1188 return ( f.buffer && LoadFile( f.buffer, encoding ));
1193 return ( f.buffer && SaveFile( f.buffer ));
1201 virtual const char*
Parse(
const char* p,
TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1218 const char *
ErrorDesc()
const {
return errorDesc.c_str (); }
1257 int TabSize()
const {
return tabsize; }
1265 errorLocation.row = errorLocation.col = 0;
1273 virtual void Print( FILE* cfile,
int depth = 0 )
const;
1275 void SetError(
int err,
const char* errorLocation,
TiXmlParsingData* prevData, TiXmlEncoding encoding );
1278 virtual void StreamOut ( TIXML_OSTREAM * out)
const;
1281 #ifdef TIXML_USE_STL
1282 virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1290 TIXML_STRING errorDesc{};
1292 TiXmlCursor errorLocation{};
1413 #ifdef TIXML_USE_STL
1417 TiXmlHandle Child(
const std::string& _value,
int index )
const {
return Child( _value.c_str(), index ); }
1442 #pragma warning( default : 4530 )
1443 #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.
int QueryDoubleAttribute(const char *name, float *val) const
QueryFloatAttribute examines the attribute - see QueryIntAttribute().
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.
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.
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.
TiXmlHandle(TiXmlNode *aNode)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
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.