Merge branch 'master' of https://github.com/leethomason/tinyxml2
Conflicts:
tinyxml2.cpp
xmltest.cpp
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index cb26e00..6081856 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -411,6 +411,12 @@
}
+void XMLUtil::ToStr( long long v, char* buffer, int bufferSize )
+{
+ TIXML_SNPRINTF( buffer, bufferSize, "%lld", v );
+}
+
+
void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
@@ -446,6 +452,14 @@
return false;
}
+bool XMLUtil::ToLongLong( const char* str, long long* value )
+{
+ if ( TIXML_SSCANF( str, "%lld", value ) == 1 ) {
+ return true;
+ }
+ return false;
+}
+
bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
{
if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
@@ -1166,6 +1180,14 @@
}
+void XMLAttribute::SetAttribute( long long v )
+{
+ char buf[BUF_SIZE];
+ XMLUtil::ToStr( v, buf, BUF_SIZE );
+ _value.SetStr( buf );
+}
+
+
void XMLAttribute::SetAttribute( unsigned v )
{
char buf[BUF_SIZE];
@@ -1311,6 +1333,41 @@
}
+void XMLElement::SetBoolFirstChild( bool inBool )
+{
+ if( FirstChild() && FirstChild()->ToElement()
+ && (strcmp(FirstChild()->Value(),"true") == 0 || strcmp(FirstChild()->Value(),"false") == 0) ) {
+ FirstChild()->SetValue( inBool ? "true" : "false" );
+ }
+ else if( !FirstChild() ) {
+ XMLElement* theText = GetDocument()->NewElement( inBool ? "true" : "false" );
+ InsertFirstChild( theText );
+ }
+}
+
+
+XMLError XMLElement::QueryBoolFirstChild( bool *outBool )
+{
+ if ( FirstChild() )
+ {
+ if ( FirstChild()->ToElement() )
+ {
+ bool isTrue = strcmp( FirstChild()->Value(), "true" ) == 0;
+ bool isFalse = strcmp( FirstChild()->Value(), "false" ) == 0;
+ if( !isTrue && !isFalse )
+ return XML_CAN_NOT_CONVERT_TEXT;
+
+ *outBool = isTrue;
+ return XML_SUCCESS;
+ }
+ else
+ return XML_NO_ELEMENT_NODE;
+ }
+ else
+ return XML_NO_ELEMENT_NODE;
+}
+
+
XMLError XMLElement::QueryIntText( int* ival ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
@@ -1324,6 +1381,19 @@
}
+XMLError XMLElement::QueryLongLongText( long long* ival ) const
+{
+ if ( FirstChild() && FirstChild()->ToText() ) {
+ const char* t = FirstChild()->ToText()->Value();
+ if ( XMLUtil::ToLongLong( t, ival ) ) {
+ return XML_SUCCESS;
+ }
+ return XML_CAN_NOT_CONVERT_TEXT;
+ }
+ return XML_NO_TEXT_NODE;
+}
+
+
XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
diff --git a/tinyxml2.h b/tinyxml2.h
index 41f1c70..f9269a1 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -540,6 +540,7 @@
// converts primitive types to strings
static void ToStr( int v, char* buffer, int bufferSize );
+ static void ToStr( long long v, char* buffer, int bufferSize );
static void ToStr( unsigned v, char* buffer, int bufferSize );
static void ToStr( bool v, char* buffer, int bufferSize );
static void ToStr( float v, char* buffer, int bufferSize );
@@ -547,6 +548,7 @@
// converts strings to primitive types
static bool ToInt( const char* str, int* value );
+ static bool ToLongLong( const char* str, long long* value );
static bool ToUnsigned( const char* str, unsigned* value );
static bool ToBool( const char* str, bool* value );
static bool ToFloat( const char* str, float* value );
@@ -1014,7 +1016,8 @@
XML_ERROR_PARSING,
XML_CAN_NOT_CONVERT_TEXT,
- XML_NO_TEXT_NODE
+ XML_NO_TEXT_NODE,
+ XML_NO_ELEMENT_NODE
};
@@ -1092,6 +1095,8 @@
/// Set the attribute to value.
void SetAttribute( int value );
/// Set the attribute to value.
+ void SetAttribute( long long value );
+ /// Set the attribute to value.
void SetAttribute( unsigned value );
/// Set the attribute to value.
void SetAttribute( bool value );
@@ -1307,6 +1312,11 @@
a->SetAttribute( value );
}
/// Sets the named attribute to value.
+ void SetAttribute( const char* name, long long value ) {
+ XMLAttribute* a = FindOrCreateAttribute( name );
+ a->SetAttribute( value );
+ }
+ /// Sets the named attribute to value.
void SetAttribute( const char* name, unsigned value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
@@ -1415,6 +1425,74 @@
/// Convenince method for setting text inside and element. See SetText() for important limitations.
void SetText( float value );
+ /// Sets the text to the given number.
+ void SetText( int inNum );
+
+ /// Sets the text to the given number.
+ void SetText( unsigned inNum );
+
+ /// Sets the text to the given double.
+ void SetText( double inNum );
+
+ /// Sets the text to the given float.
+ void SetText( float inNum );
+
+ /// Sets the text to the given long long.
+ void SetText( long long inNum );
+
+ /// Convenience for QueryIntText when you don't care if the text won't convert.
+ int IntText()
+ {
+ int i = 0;
+ QueryIntText( &i );
+ return i;
+ }
+
+ /// Convenience for QueryLongLongText when you don't care if the text won't convert.
+ long long LongLongText()
+ {
+ long long i = 0;
+ QueryLongLongText( &i );
+ return i;
+ }
+
+ /// Convenience for QueryUnsignedText when you don't care if the text won't convert.
+ unsigned UnsignedText()
+ {
+ unsigned i = 0;
+ QueryUnsignedText( &i );
+ return i;
+ }
+
+ /// Convenience for QueryDoubleText when you don't care if the text won't convert.
+ double DoubleText()
+ {
+ double i = 0;
+ QueryDoubleText( &i );
+ return i;
+ }
+
+ /// Convenience for QueryFloatText when you don't care if the text won't convert.
+ float FloatText()
+ {
+ float i = 0;
+ QueryFloatText( &i );
+ return i;
+ }
+
+ /// Adds a sub-element equivalent to the given boolean.
+ void SetBoolFirstChild( bool inBool );
+
+ /// Looks for a <true /> or <false /> as the first child and returns the corresponding bool.
+ bool BoolFirstChild()
+ {
+ bool b = false;
+ QueryBoolFirstChild(&b);
+ return b;
+ }
+
+ XMLError QueryBoolFirstChild( bool *outBool );
+
/**
Convenience method to query the value of a child text node. This is probably best
shown by example. Given you have a document is this form:
@@ -1443,6 +1521,8 @@
*/
XMLError QueryIntText( int* ival ) const;
/// See QueryIntText()
+ XMLError QueryLongLongText( long long* ival ) const;
+ /// See QueryIntText()
XMLError QueryUnsignedText( unsigned* uval ) const;
/// See QueryIntText()
XMLError QueryBoolText( bool* bval ) const;
@@ -1465,6 +1545,8 @@
virtual bool ShallowEqual( const XMLNode* compare ) const;
private:
+ enum { BUF_SIZE = 200 };
+
XMLElement( XMLDocument* doc );
virtual ~XMLElement();
XMLElement( const XMLElement& ); // not supported