added methods parseBool() and tryParseBool() to Poco::NumberParser + test unit

This commit is contained in:
Marian Krivos
2009-03-16 17:59:38 +00:00
parent 582d3853db
commit 02161a9f46
3 changed files with 80 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
#include "Poco/NumberParser.h"
#include "Poco/Exception.h"
#include "Poco/String.h"
#include <cstdio>
#include <cctype>
@@ -177,4 +178,59 @@ bool NumberParser::tryParseFloat(const std::string& s, double& value)
}
bool NumberParser::parseBool(const std::string& s)
{
bool result;
if (tryParseBool(s, result))
return result;
else
throw SyntaxException("Not a valid bool number", s);
}
bool NumberParser::tryParseBool(const std::string& s, bool& value)
{
int n;
if (NumberParser::tryParse(s, n))
{
value = (n != 0);
return true;
}
if (icompare(s, "true") == 0)
{
value = true;
return true;
}
else if (icompare(s, "yes") == 0)
{
value = true;
return true;
}
else if (icompare(s, "on") == 0)
{
value = true;
return true;
}
if (icompare(s, "false") == 0)
{
value = false;
return true;
}
else if (icompare(s, "no") == 0)
{
value = false;
return true;
}
else if (icompare(s, "off") == 0)
{
value = false;
return true;
}
return false;
}
} // namespace Poco