Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions tinyxml2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,8 @@ void XMLElement::SetText( double v )

XMLError XMLElement::QueryIntText( int* ival ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if ( t ) {
if ( XMLUtil::ToInt( t, ival ) ) {
return XML_SUCCESS;
}
Expand All @@ -1790,8 +1790,8 @@ XMLError XMLElement::QueryIntText( int* ival ) const

XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if ( t ) {
if ( XMLUtil::ToUnsigned( t, uval ) ) {
return XML_SUCCESS;
}
Expand All @@ -1803,8 +1803,8 @@ XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const

XMLError XMLElement::QueryInt64Text(int64_t* ival) const
{
if (FirstChild() && FirstChild()->ToText()) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if (t) {
if (XMLUtil::ToInt64(t, ival)) {
return XML_SUCCESS;
}
Expand All @@ -1816,8 +1816,8 @@ XMLError XMLElement::QueryInt64Text(int64_t* ival) const

XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const
{
if(FirstChild() && FirstChild()->ToText()) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if(t) {
if(XMLUtil::ToUnsigned64(t, uval)) {
return XML_SUCCESS;
}
Expand All @@ -1829,8 +1829,8 @@ XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const

XMLError XMLElement::QueryBoolText( bool* bval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if ( t ) {
if ( XMLUtil::ToBool( t, bval ) ) {
return XML_SUCCESS;
}
Expand All @@ -1842,8 +1842,8 @@ XMLError XMLElement::QueryBoolText( bool* bval ) const

XMLError XMLElement::QueryDoubleText( double* dval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if ( t ) {
if ( XMLUtil::ToDouble( t, dval ) ) {
return XML_SUCCESS;
}
Expand All @@ -1855,8 +1855,8 @@ XMLError XMLElement::QueryDoubleText( double* dval ) const

XMLError XMLElement::QueryFloatText( float* fval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
const char* t = GetText();
if ( t ) {
if ( XMLUtil::ToFloat( t, fval ) ) {
return XML_SUCCESS;
}
Expand Down
35 changes: 33 additions & 2 deletions xmltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1736,8 +1736,13 @@ int main( int argc, const char ** argv )
}

// QueryXYZText
{
const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
const char* queryTextCases[] = {
"<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>",
"<point><x><!--comment-->1.2</x><y><!--comment-->1</y><valid><!--comment-->true</valid></point>",
"<point><x><!--a--><!--b--><![CDATA[1.2]]></x><y><!--a--><!--b--><![CDATA[1]]></y><valid><!--a--><!--b--><![CDATA[true]]></valid></point>"
};
for ( unsigned i = 0; i < sizeof(queryTextCases) / sizeof(queryTextCases[0]); ++i ) {
const char* xml = queryTextCases[i];
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse points", false, doc.Error() );
Expand All @@ -1758,6 +1763,18 @@ int main( int argc, const char ** argv )
XMLTest( "QueryUnsignedText", static_cast<unsigned>(1), unsignedValue, false );
}

{
int64_t intValue = 0;
XMLTest( "QueryInt64Text result", XML_SUCCESS, pointElement->FirstChildElement( "y" )->QueryInt64Text( &intValue ) );
XMLTest( "QueryInt64Text", static_cast<int64_t>(1), intValue );
}

{
uint64_t unsignedValue = 0;
XMLTest( "QueryUnsigned64Text result", XML_SUCCESS, pointElement->FirstChildElement( "y" )->QueryUnsigned64Text( &unsignedValue ) );
XMLTest( "QueryUnsigned64Text", static_cast<uint64_t>(1), unsignedValue );
}

{
float floatValue = 0;
XMLError queryResult = pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
Expand All @@ -1780,6 +1797,20 @@ int main( int argc, const char ** argv )
}
}

const char* queryTextErrorCases[] = {
"<value><!--comment-->invalid</value>",
"<value><!--comment--></value>",
"<value><!--comment--><child/>42</value>"
};
for ( unsigned i = 0; i < sizeof(queryTextErrorCases) / sizeof(queryTextErrorCases[0]); ++i ) {
XMLDocument doc;
doc.Parse( queryTextErrorCases[i] );
int value = 42;
XMLTest( "QueryIntText error", i == 0 ? XML_CAN_NOT_CONVERT_TEXT : XML_NO_TEXT_NODE,
doc.RootElement()->QueryIntText( &value ) );
XMLTest( "QueryIntText preserves output on error", 42, value );
}

{
const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
XMLDocument doc;
Expand Down