From 1cf7973245b3331600a21c17eeee853e1cb3e3ec Mon Sep 17 00:00:00 2001 From: John Hernandez <129467592+H3rnand3zzz@users.noreply.github.com> Date: Fri, 10 Nov 2023 12:01:44 +0100 Subject: [PATCH] Minor `config/conflists.c` refactoring Refactor functions `conf_string_list_add`, `conf_string_list_remove`: simplify the logic, improve readability and avoid unnecessary allocations. --- src/config/conflists.c | 79 ++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/src/config/conflists.c b/src/config/conflists.c index 1d0cb5395..4032878ee 100644 --- a/src/config/conflists.c +++ b/src/config/conflists.c @@ -35,6 +35,7 @@ #include "config.h" #include "common.h" +#include "log.h" #include #include @@ -42,46 +43,36 @@ gboolean conf_string_list_add(GKeyFile* keyfile, const char* const group, const char* const key, const char* const item) { + if (!item) { + log_warning("Invalid parameter in `conf_string_list_add`: item is NULL. group=%s, key=%s", group, key); + return FALSE; + } + gsize length; auto_gcharv gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL); - GList* glist = NULL; - // list found - if (list) { - int i = 0; - for (i = 0; i < length; i++) { - // item already in list, exit function - if (strcmp(list[i], item) == 0) { - g_list_free_full(glist, g_free); - return FALSE; - } - // add item to our g_list - glist = g_list_append(glist, strdup(list[i])); - } - - // item not found, add to our g_list - glist = g_list_append(glist, strdup(item)); + if (!list) { + const gchar* new_list[2] = { item, NULL }; + g_key_file_set_string_list(keyfile, group, key, new_list, 1); + return TRUE; + } - // create the new list entry - const gchar* new_list[g_list_length(glist) + 1]; - GList* curr = glist; - i = 0; - while (curr) { - new_list[i++] = curr->data; - curr = g_list_next(curr); + // Check if item is already in the list + for (gsize i = 0; i < length; ++i) { + if (strcmp(list[i], item) == 0) { + return FALSE; } - new_list[i] = NULL; - g_key_file_set_string_list(keyfile, group, key, new_list, g_list_length(glist)); - - // list not found - } else { - const gchar* new_list[2]; - new_list[0] = item; - new_list[1] = NULL; - g_key_file_set_string_list(keyfile, group, key, new_list, 1); } - g_list_free_full(glist, g_free); + // Add item to the existing list + gchar** new_list = g_new(gchar*, length + 2); + for (gsize i = 0; i < length; ++i) { + new_list[i] = g_strdup(list[i]); + } + new_list[length] = g_strdup(item); + new_list[length + 1] = NULL; + + g_key_file_set_string_list(keyfile, group, key, (const gchar* const*)new_list, length + 1); return TRUE; } @@ -92,41 +83,37 @@ conf_string_list_remove(GKeyFile* keyfile, const char* const group, const char* gsize length; auto_gcharv gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL); - gboolean deleted = FALSE; if (!list) { return FALSE; } - int i = 0; + GList* glist = NULL; + gboolean deleted = FALSE; - for (i = 0; i < length; i++) { - // item found, mark as deleted + for (int i = 0; i < length; i++) { if (strcmp(list[i], item) == 0) { deleted = TRUE; - } else { - // add item to our g_list - glist = g_list_append(glist, strdup(list[i])); + continue; } + glist = g_list_append(glist, strdup(list[i])); } if (deleted) { if (g_list_length(glist) == 0) { g_key_file_remove_key(keyfile, group, key, NULL); } else { - // create the new list entry const gchar* new_list[g_list_length(glist) + 1]; - GList* curr = glist; - i = 0; - while (curr) { + int i = 0; + + for (GList* curr = glist; curr; curr = g_list_next(curr)) { new_list[i++] = curr->data; - curr = g_list_next(curr); } + new_list[i] = NULL; g_key_file_set_string_list(keyfile, group, key, new_list, g_list_length(glist)); } } g_list_free_full(glist, g_free); - return deleted; }