Skip to content

Commit

Permalink
Merge pull request #1920 from H3rnand3zzz/cleanup/refactor-conf-strin…
Browse files Browse the repository at this point in the history
…g-list

Minor `config/conflists.c` refactoring
  • Loading branch information
jubalh authored Nov 13, 2023
2 parents 80ab050 + 1cf7973 commit e8101aa
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions src/config/conflists.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,44 @@

#include "config.h"
#include "common.h"
#include "log.h"

#include <string.h>
#include <glib.h>

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;
}
Expand All @@ -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;
}

0 comments on commit e8101aa

Please sign in to comment.