tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

Read boolean uppercase

Open manuelk2017 opened this issue 7 years ago • 4 comments

Hello, I'm wondering if it would be possible to parse TRUE and FALSE, too.

It's in this function: bool XMLUtil::ToBool( const char* str, bool* value )

I changed it to this:

bool XMLUtil::ToBool( const char* str, bool* value )
{
    int ival = 0;
    if ( ToInt( str, &ival )) {
        *value = (ival==0) ? false : true;
        return true;
    }
    if ( StringEqual( str, "true" ) ) {
        *value = true;
        return true;
    }
    else if ( StringEqual( str, "false" ) ) {
        *value = false;
        return true;
    }
    else if ( StringEqual( str, "TRUE" ) ) {
        *value = true;
        return true;
    }
    else if ( StringEqual( str, "FALSE" ) ) {
        *value = false;
        return true;
    }
    return false;
}

Maybe you want to check this code in.

With kind regards

Manuel

manuelk2017 avatar Jul 19 '18 06:07 manuelk2017

There's a function "XMLUtil::SetBoolSerialization(const TCHAR* writeTrue, const TCHAR* writeFalse)". Try it.

slr69 avatar Jul 19 '18 09:07 slr69

I think this affects writing only. I want to read XML and values can be 1, TRUE or true (for example).

manuelk2017 avatar Jul 19 '18 09:07 manuelk2017

I think better way would be:

  1. set "XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse)".
  2. function "XMLUtil::ToBool" should consider "writeBoolTrue" and "writeBoolFalse" that were set above.

slr69 avatar Jul 19 '18 10:07 slr69

I have re-wrote "XMLUtil::ToBool" such way. bool XMLUtil::ToBool( const char* str, bool* value ) { int ival = 0; if ( ToInt( str, &ival )) { *value = (ival==0) ? false : true; return true; } if ( StringEqual( str, writeBoolTrue ) ) { *value = true; return true; } else if ( StringEqual( str, writeBoolFalse ) ) { *value = false; return true; } return false; }

Now if you call "tinyxml2::XMLUtil::SetBoolSerialization("tRuE_1", "fAlSe_0");" before any other calls I think you can image what will happen.

slr69 avatar Jul 19 '18 11:07 slr69