util/debug: add an enable parsing helper

This allows to parse something like :

 ENV_VAR=+a,-b

which will enable a and disable b.

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Danylo Piliaiev <dpiliaiev@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16717>
This commit is contained in:
Lionel Landwerlin 2022-05-22 15:46:48 +03:00 committed by Marge Bot
parent e666089082
commit 4192747737
2 changed files with 46 additions and 0 deletions

View File

@ -53,6 +53,48 @@ parse_debug_string(const char *debug,
return flag;
}
uint64_t
parse_enable_string(const char *debug,
uint64_t default_value,
const struct debug_control *control)
{
uint64_t flag = default_value;
if (debug != NULL) {
for (; control->string != NULL; control++) {
if (!strcmp(debug, "all")) {
flag |= control->flag;
} else {
const char *s = debug;
unsigned n;
for (; n = strcspn(s, ", "), *s; s += MAX2(1, n)) {
bool enable;
if (s[0] == '+') {
enable = true;
s++; n--;
} else if (s[0] == '-') {
enable = false;
s++; n--;
} else {
enable = true;
}
if (strlen(control->string) == n &&
!strncmp(control->string, s, n)) {
if (enable)
flag |= control->flag;
else
flag &= ~control->flag;
}
}
}
}
}
return flag;
}
bool
comma_separated_list_contains(const char *list, const char *s)
{

View File

@ -39,6 +39,10 @@ struct debug_control {
uint64_t
parse_debug_string(const char *debug,
const struct debug_control *control);
uint64_t
parse_enable_string(const char *debug,
uint64_t default_value,
const struct debug_control *control);
bool
comma_separated_list_contains(const char *list, const char *s);
bool