From 119ca4b105b5c57a9efcfb675b3257e0512808b7 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 31 Jan 2025 08:52:19 -0400 Subject: [PATCH] MT#62053 add config file groups callback Add a callback type to read config groups and their contents based on a group prefix. Unused at this point. Change-Id: I7d9e043e96f48e599bc4da2d8ef4079559cb8b47 --- lib/auxlib.c | 34 ++++++++++++++++++++++++++++++++++ lib/auxlib.h | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/lib/auxlib.c b/lib/auxlib.c index 2f3c3d184..236721b2e 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -178,6 +178,36 @@ static void section_keys_callback(GKeyFile *kf, } } +static void file_groups_callback(GKeyFile *kf, + const char *group_prefix, + void (*callback)(const char *name, charp_ht, union rtpenging_config_callback_arg), + union rtpenging_config_callback_arg arg) +{ + if (!group_prefix) + return; + + size_t pref_len = strlen(group_prefix); + + g_autoptr(char_p) groups = g_key_file_get_groups(kf, NULL); + + for (char **group = groups; *group; group++) { + // check for groups starting with "PREFIX-.." + if (memcmp(*group, group_prefix, pref_len)) + continue; + char *ident = *group + pref_len; + if (*ident != '-') + continue; + ident++; + if (*ident == '\0') + continue; + + // read all keys and put them in a hash table + g_auto(charp_ht) ht = charp_ht_new(); + section_keys_callback(kf, *group, add_c_str_to_ht, ht); + callback(ident, ht, arg); + } +} + G_DEFINE_AUTOPTR_CLEANUP_FUNC(GOptionEntry, free) typedef char *char_p_shallow; G_DEFINE_AUTOPTR_CLEANUP_FUNC(char_p_shallow, g_free) @@ -394,6 +424,10 @@ void config_load_ext(int *argc, char ***argv, GOptionEntry *app_entries, const c case RCC_SECTION_KEYS: section_keys_callback(kf, *cb->section_keys.name, cb->section_keys.callback, cb->arg); continue; + + case RCC_FILE_GROUPS: + file_groups_callback(kf, *cb->file_groups.prefix, cb->file_groups.callback, cb->arg); + continue; } break; } diff --git a/lib/auxlib.h b/lib/auxlib.h index 31afe709a..3d8f40713 100644 --- a/lib/auxlib.h +++ b/lib/auxlib.h @@ -62,6 +62,7 @@ struct rtpenging_config_callback { enum { RCC_END = 0, RCC_SECTION_KEYS, + RCC_FILE_GROUPS, } type; union rtpenging_config_callback_arg arg; union { @@ -70,6 +71,11 @@ struct rtpenging_config_callback { void (*callback)(const char *key, char *value, union rtpenging_config_callback_arg); } section_keys; + struct { + char * const *prefix; + void (*callback)(const char *name, charp_ht, + union rtpenging_config_callback_arg); + } file_groups; }; };