ascii_to() utility for boolean values

Also, Genode::Arg was adapted to use the new utility for boolean tokens
and strings.

Issue #1648
This commit is contained in:
Emery Hemingway 2015-08-28 14:42:22 +02:00 committed by Christian Helmuth
parent ce354d6fd9
commit df0bbe0b0e
2 changed files with 33 additions and 21 deletions

View File

@ -139,32 +139,26 @@ class Genode::Arg
bool bool_value(bool default_value) const
{
/* check for known idents */
if (_value.type() == Token::IDENT) {
char *p = _value.start();
size_t l = _value.len();
bool result = default_value;
switch(_value.type()) {
if (!strcmp(p, "yes", l)) return true;
if (!strcmp(p, "true", l)) return true;
if (!strcmp(p, "on", l)) return true;
/* result is passed to 'ascii_to' by reference */
case Token::IDENT:;
if (ascii_to(_value.start(), result) == _value.len())
return result;
if (!strcmp(p, "no", l)) return false;
if (!strcmp(p, "false", l)) return false;
if (!strcmp(p, "off", l)) return false;
case Token::STRING:
if (ascii_to(_value.start()+1, result) == _value.len()-2)
return result;
/* saxony mode ;) */
if (!strcmp(p, "nu", l)) return true;
if (!strcmp(p, "nee", l)) return false;
default:
/* read values 0 (false) / !0 (true) */
unsigned long value;
int sign;
bool valid = read_ulong(&value, &sign);
return default_value;
return valid ? value : default_value;
}
/* read values 0 (false) / !0 (true) */
unsigned long value;
int sign;
bool valid = read_ulong(&value, &sign);
return valid ? value : default_value;
}
void key(char *dst, size_t dst_len) const

View File

@ -309,6 +309,24 @@ namespace Genode {
}
/**
* Read boolean value from string
*
* \return number of consumed characters
*/
inline size_t ascii_to(char const *s, bool &result)
{
if (!strcmp(s, "yes", 3)) { result = true; return 3; }
if (!strcmp(s, "true", 4)) { result = true; return 4; }
if (!strcmp(s, "on", 2)) { result = true; return 2; }
if (!strcmp(s, "no", 2)) { result = false; return 2; }
if (!strcmp(s, "false", 5)) { result = false; return 5; }
if (!strcmp(s, "off", 3)) { result = false; return 3; }
return 0;
}
/**
* Read unsigned long value from string
*