From a51a1cde3621d682bc70e8771d48a8af797bd24e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 13 Feb 2016 16:06:36 +0100 Subject: utils: add nm_utils_parse_debug_string() to replace g_parse_debug_string() g_parse_debug_string() interprets the string "help" special and accepts an "all" tag to invert the result. We don't want that. --- src/NetworkManagerUtils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/NetworkManagerUtils.c') diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 9e3928f72..5379c5403 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -3627,3 +3627,65 @@ nm_utils_g_value_set_strv (GValue *value, GPtrArray *strings) g_value_take_boxed (value, strv); } + +/*****************************************************************************/ + +static gboolean +debug_key_matches (const gchar *key, + const gchar *token, + guint length) +{ + /* may not call GLib functions: see note in g_parse_debug_string() */ + for (; length; length--, key++, token++) { + char k = (*key == '_') ? '-' : g_ascii_tolower (*key ); + char t = (*token == '_') ? '-' : g_ascii_tolower (*token); + + if (k != t) + return FALSE; + } + + return *key == '\0'; +} + +/** + * nm_utils_parse_debug_string: + * @string: the string to parse + * @keys: the debug keys + * @nkeys: number of entires in @keys + * + * Similar to g_parse_debug_string(), but does not special + * case "help" or "all". + * + * Returns: the flags + */ +guint +nm_utils_parse_debug_string (const char *string, + const GDebugKey *keys, + guint nkeys) +{ + guint i; + guint result = 0; + const char *q; + + if (string == NULL) + return 0; + + while (*string) { + q = strpbrk (string, ":;, \t"); + if (!q) + q = string + strlen (string); + + for (i = 0; i < nkeys; i++) { + if (debug_key_matches (keys[i].key, string, q - string)) + result |= keys[i].value; + } + + string = q; + if (*string) + string++; + } + + return result; +} + + -- cgit v1.2.3