mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-31 00:24:51 +00:00
util/arg_string.h: set_arg_string
Convenience function to set x="y" style arguments. Issue #1861
This commit is contained in:
parent
23c1d4c66d
commit
723d9e7784
@ -277,14 +277,17 @@ class Genode::Arg_string
|
||||
* Add new argument
|
||||
*/
|
||||
static bool add_arg(char *args, unsigned args_len,
|
||||
const char *key, const char *value)
|
||||
const char *key, const char *value,
|
||||
Token::Type type = Token::Type::IDENT)
|
||||
{
|
||||
if (!args || !key || !value) return false;
|
||||
|
||||
unsigned old_len = strlen(args);
|
||||
|
||||
/* check if args string has enough capacity */
|
||||
if (old_len + strlen(key) + strlen(value) + 2 > args_len)
|
||||
if ((type == Token::Type::STRING
|
||||
&& old_len + strlen(key) + strlen(value) + 4 > args_len)
|
||||
|| (old_len + strlen(key) + strlen(value) + 2 > args_len))
|
||||
return false;
|
||||
|
||||
args += old_len;
|
||||
@ -292,7 +295,10 @@ class Genode::Arg_string
|
||||
if (old_len)
|
||||
args = _append(args, ", ");
|
||||
|
||||
_append(_append(_append(args, key), "="), value);
|
||||
if (type == Token::Type::STRING)
|
||||
_append(_append(_append(_append(args, key), "=\""), value), "\"");
|
||||
else
|
||||
_append(_append(_append(args, key), "="), value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -316,6 +322,16 @@ class Genode::Arg_string
|
||||
snprintf(buf, sizeof(buf), "%d", value);
|
||||
return remove_arg(args, key) && add_arg(args, args_len, key, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign new string argument
|
||||
*/
|
||||
static bool set_arg_string(char *args, unsigned args_len,
|
||||
const char *key, const char *value)
|
||||
{
|
||||
return remove_arg(args, key)
|
||||
&& add_arg(args, args_len, key, value, Token::Type::STRING);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* _INCLUDE__UTIL__ARG_STRING_H_ */
|
||||
|
Loading…
x
Reference in New Issue
Block a user