mirror of
https://github.com/genodelabs/genode.git
synced 2025-03-11 06:54:18 +00:00
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:
parent
ce354d6fd9
commit
df0bbe0b0e
@ -139,32 +139,26 @@ class Genode::Arg
|
|||||||
|
|
||||||
bool bool_value(bool default_value) const
|
bool bool_value(bool default_value) const
|
||||||
{
|
{
|
||||||
/* check for known idents */
|
bool result = default_value;
|
||||||
if (_value.type() == Token::IDENT) {
|
switch(_value.type()) {
|
||||||
char *p = _value.start();
|
|
||||||
size_t l = _value.len();
|
|
||||||
|
|
||||||
if (!strcmp(p, "yes", l)) return true;
|
/* result is passed to 'ascii_to' by reference */
|
||||||
if (!strcmp(p, "true", l)) return true;
|
case Token::IDENT:;
|
||||||
if (!strcmp(p, "on", l)) return true;
|
if (ascii_to(_value.start(), result) == _value.len())
|
||||||
|
return result;
|
||||||
|
|
||||||
if (!strcmp(p, "no", l)) return false;
|
case Token::STRING:
|
||||||
if (!strcmp(p, "false", l)) return false;
|
if (ascii_to(_value.start()+1, result) == _value.len()-2)
|
||||||
if (!strcmp(p, "off", l)) return false;
|
return result;
|
||||||
|
|
||||||
/* saxony mode ;) */
|
default:
|
||||||
if (!strcmp(p, "nu", l)) return true;
|
/* read values 0 (false) / !0 (true) */
|
||||||
if (!strcmp(p, "nee", l)) return false;
|
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
|
void key(char *dst, size_t dst_len) const
|
||||||
|
@ -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
|
* Read unsigned long value from string
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user