mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Merge pull request #1920 from H3rnand3zzz/cleanup/refactor-conf-string-list
Minor `config/conflists.c` refactoring
This commit is contained in:
commit
e8101aad6b
@ -35,6 +35,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
@ -42,46 +43,36 @@
|
||||
gboolean
|
||||
conf_string_list_add(GKeyFile* keyfile, const char* const group, const char* const key, const char* const item)
|
||||
{
|
||||
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);
|
||||
if (!item) {
|
||||
log_warning("Invalid parameter in `conf_string_list_add`: item is NULL. group=%s, key=%s", group, key);
|
||||
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));
|
||||
gsize length;
|
||||
auto_gcharv gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL);
|
||||
|
||||
// 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);
|
||||
}
|
||||
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;
|
||||
if (!list) {
|
||||
const gchar* new_list[2] = { item, NULL };
|
||||
g_key_file_set_string_list(keyfile, group, key, new_list, 1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
g_list_free_full(glist, g_free);
|
||||
// Check if item is already in the list
|
||||
for (gsize i = 0; i < length; ++i) {
|
||||
if (strcmp(list[i], item) == 0) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
// item found, mark as deleted
|
||||
GList* glist = NULL;
|
||||
gboolean deleted = FALSE;
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user