Cleanup g_strfreev() to auto_gcharv

Include some additional minor cleanups
This commit is contained in:
John Hernandez
2023-07-13 16:31:31 +02:00
parent 029f1caa52
commit 865a056315
16 changed files with 165 additions and 234 deletions

View File

@@ -78,7 +78,7 @@ accounts_load(void)
// create the logins searchable list for autocompletion
gsize naccounts;
gchar** account_names = g_key_file_get_groups(accounts, &naccounts);
auto_gcharv gchar** account_names = g_key_file_get_groups(accounts, &naccounts);
for (gsize i = 0; i < naccounts; i++) {
autocomplete_add(all_ac, account_names[i]);
@@ -86,8 +86,6 @@ accounts_load(void)
autocomplete_add(enabled_ac, account_names[i]);
}
}
g_strfreev(account_names);
}
void
@@ -245,30 +243,27 @@ accounts_get_account(const char* const name)
gsize length;
GList* otr_manual = NULL;
gchar** manual = g_key_file_get_string_list(accounts, name, "otr.manual", &length, NULL);
auto_gcharv gchar** manual = g_key_file_get_string_list(accounts, name, "otr.manual", &length, NULL);
if (manual) {
for (int i = 0; i < length; i++) {
otr_manual = g_list_append(otr_manual, strdup(manual[i]));
}
g_strfreev(manual);
}
GList* otr_opportunistic = NULL;
gchar** opportunistic = g_key_file_get_string_list(accounts, name, "otr.opportunistic", &length, NULL);
auto_gcharv gchar** opportunistic = g_key_file_get_string_list(accounts, name, "otr.opportunistic", &length, NULL);
if (opportunistic) {
for (int i = 0; i < length; i++) {
otr_opportunistic = g_list_append(otr_opportunistic, strdup(opportunistic[i]));
}
g_strfreev(opportunistic);
}
GList* otr_always = NULL;
gchar** always = g_key_file_get_string_list(accounts, name, "otr.always", &length, NULL);
auto_gcharv gchar** always = g_key_file_get_string_list(accounts, name, "otr.always", &length, NULL);
if (always) {
for (int i = 0; i < length; i++) {
otr_always = g_list_append(otr_always, strdup(always[i]));
}
g_strfreev(always);
}
gchar* omemo_policy = NULL;
@@ -277,39 +272,35 @@ accounts_get_account(const char* const name)
}
GList* omemo_enabled = NULL;
gchar** omemo_enabled_list = g_key_file_get_string_list(accounts, name, "omemo.enabled", &length, NULL);
auto_gcharv gchar** omemo_enabled_list = g_key_file_get_string_list(accounts, name, "omemo.enabled", &length, NULL);
if (omemo_enabled_list) {
for (int i = 0; i < length; i++) {
omemo_enabled = g_list_append(omemo_enabled, strdup(omemo_enabled_list[i]));
}
g_strfreev(omemo_enabled_list);
}
GList* omemo_disabled = NULL;
gchar** omemo_disabled_list = g_key_file_get_string_list(accounts, name, "omemo.disabled", &length, NULL);
auto_gcharv gchar** omemo_disabled_list = g_key_file_get_string_list(accounts, name, "omemo.disabled", &length, NULL);
if (omemo_disabled_list) {
for (int i = 0; i < length; i++) {
omemo_disabled = g_list_append(omemo_disabled, strdup(omemo_disabled_list[i]));
}
g_strfreev(omemo_disabled_list);
}
GList* ox_enabled = NULL;
gchar** ox_enabled_list = g_key_file_get_string_list(accounts, name, "ox.enabled", &length, NULL);
auto_gcharv gchar** ox_enabled_list = g_key_file_get_string_list(accounts, name, "ox.enabled", &length, NULL);
if (ox_enabled_list) {
for (int i = 0; i < length; i++) {
ox_enabled = g_list_append(ox_enabled, strdup(ox_enabled_list[i]));
}
g_strfreev(ox_enabled_list);
}
GList* pgp_enabled = NULL;
gchar** pgp_enabled_list = g_key_file_get_string_list(accounts, name, "pgp.enabled", &length, NULL);
auto_gcharv gchar** pgp_enabled_list = g_key_file_get_string_list(accounts, name, "pgp.enabled", &length, NULL);
if (pgp_enabled_list) {
for (int i = 0; i < length; i++) {
pgp_enabled = g_list_append(pgp_enabled, strdup(pgp_enabled_list[i]));
}
g_strfreev(pgp_enabled_list);
}
gchar* pgp_keyid = NULL;

View File

@@ -34,6 +34,7 @@
*/
#include "config.h"
#include "common.h"
#include <string.h>
#include <glib.h>
@@ -42,7 +43,7 @@ gboolean
conf_string_list_add(GKeyFile* keyfile, const char* const group, const char* const key, const char* const item)
{
gsize length;
gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL);
auto_gcharv gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL);
GList* glist = NULL;
// list found
@@ -81,7 +82,6 @@ conf_string_list_add(GKeyFile* keyfile, const char* const group, const char* con
g_key_file_set_string_list(keyfile, group, key, new_list, 1);
}
g_strfreev(list);
g_list_free_full(glist, g_free);
return TRUE;
@@ -91,44 +91,43 @@ gboolean
conf_string_list_remove(GKeyFile* keyfile, const char* const group, const char* const key, const char* const item)
{
gsize length;
gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL);
auto_gcharv gchar** list = g_key_file_get_string_list(keyfile, group, key, &length, NULL);
gboolean deleted = FALSE;
if (list) {
int i = 0;
GList* glist = NULL;
if (!list) {
return FALSE;
}
int i = 0;
GList* glist = NULL;
for (i = 0; i < length; i++) {
// item found, mark as deleted
if (strcmp(list[i], item) == 0) {
deleted = TRUE;
} else {
// add item to our g_list
glist = g_list_append(glist, strdup(list[i]));
}
for (i = 0; i < length; i++) {
// item found, mark as deleted
if (strcmp(list[i], item) == 0) {
deleted = TRUE;
} else {
// add item to our g_list
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) {
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);
}
g_strfreev(list);
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) {
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;
}

View File

@@ -216,12 +216,11 @@ _prefs_load(void)
room_trigger_ac = autocomplete_new();
gsize len = 0;
gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
auto_gcharv gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
for (int i = 0; i < len; i++) {
autocomplete_add(room_trigger_ac, triggers[i]);
}
g_strfreev(triggers);
}
/* Clean up after _prefs_load() */
@@ -333,7 +332,7 @@ prefs_message_get_triggers(const char* const message)
auto_gchar gchar* message_lower = g_utf8_strdown(message, -1);
gsize len = 0;
gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
auto_gcharv gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
for (int i = 0; i < len; i++) {
auto_gchar gchar* trigger_lower = g_utf8_strdown(triggers[i], -1);
@@ -342,8 +341,6 @@ prefs_message_get_triggers(const char* const message)
}
}
g_strfreev(triggers);
return result;
}
@@ -862,12 +859,6 @@ prefs_remove_plugin(const char* const name)
_save_prefs();
}
void
prefs_free_plugins(gchar** plugins)
{
g_strfreev(plugins);
}
void
prefs_set_occupants_size(gint value)
{
@@ -1316,14 +1307,12 @@ prefs_get_room_notify_triggers(void)
{
GList* result = NULL;
gsize len = 0;
gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
auto_gcharv gchar** triggers = g_key_file_get_string_list(prefs, PREF_GROUP_NOTIFICATIONS, "room.trigger.list", &len, NULL);
for (int i = 0; i < len; i++) {
result = g_list_append(result, strdup(triggers[i]));
}
g_strfreev(triggers);
return result;
}
@@ -1665,7 +1654,7 @@ prefs_get_aliases(void)
} else {
GList* result = NULL;
gsize len;
gchar** keys = g_key_file_get_keys(prefs, PREF_GROUP_ALIAS, &len, NULL);
auto_gcharv gchar** keys = g_key_file_get_keys(prefs, PREF_GROUP_ALIAS, &len, NULL);
for (int i = 0; i < len; i++) {
char* name = keys[i];
@@ -1680,8 +1669,6 @@ prefs_get_aliases(void)
}
}
g_strfreev(keys);
return result;
}
}

View File

@@ -253,7 +253,6 @@ gint prefs_get_autoxa_time(void);
void prefs_set_autoxa_time(gint value);
gchar** prefs_get_plugins(void);
void prefs_free_plugins(gchar** plugins);
void prefs_add_plugin(const char* const name);
void prefs_remove_plugin(const char* const name);

View File

@@ -71,12 +71,11 @@ tlscerts_init(void)
certs_ac = autocomplete_new();
gsize len = 0;
gchar** groups = g_key_file_get_groups(tlscerts, &len);
auto_gcharv gchar** groups = g_key_file_get_groups(tlscerts, &len);
for (int i = 0; i < g_strv_length(groups); i++) {
autocomplete_add(certs_ac, groups[i]);
}
g_strfreev(groups);
current_fp = NULL;
}
@@ -116,7 +115,7 @@ tlscerts_list(void)
{
GList* res = NULL;
gsize len = 0;
gchar** groups = g_key_file_get_groups(tlscerts, &len);
auto_gcharv gchar** groups = g_key_file_get_groups(tlscerts, &len);
for (int i = 0; i < g_strv_length(groups); i++) {
char* fingerprint = strdup(groups[i]);
@@ -135,10 +134,6 @@ tlscerts_list(void)
res = g_list_append(res, cert);
}
if (groups) {
g_strfreev(groups);
}
return res;
}
@@ -178,9 +173,9 @@ tlscerts_new(const char* const fingerprint, int version, const char* const seria
cert->pem = strdup(pem);
}
gchar** fields = g_strsplit(subjectname, "/", 0);
auto_gcharv gchar** fields = g_strsplit(subjectname, "/", 0);
for (int i = 0; i < g_strv_length(fields); i++) {
gchar** keyval = g_strsplit(fields[i], "=", 2);
auto_gcharv gchar** keyval = g_strsplit(fields[i], "=", 2);
if (g_strv_length(keyval) == 2) {
if ((g_strcmp0(keyval[0], "C") == 0) || (g_strcmp0(keyval[0], "countryName") == 0)) {
cert->subject_country = strdup(keyval[1]);
@@ -207,13 +202,11 @@ tlscerts_new(const char* const fingerprint, int version, const char* const seria
cert->subject_email = strdup(keyval[1]);
}
}
g_strfreev(keyval);
}
g_strfreev(fields);
fields = g_strsplit(issuername, "/", 0);
for (int i = 0; i < g_strv_length(fields); i++) {
gchar** keyval = g_strsplit(fields[i], "=", 2);
auto_gcharv gchar** fields2 = g_strsplit(issuername, "/", 0);
for (int i = 0; i < g_strv_length(fields2); i++) {
auto_gcharv gchar** keyval = g_strsplit(fields2[i], "=", 2);
if (g_strv_length(keyval) == 2) {
if ((g_strcmp0(keyval[0], "C") == 0) || (g_strcmp0(keyval[0], "countryName") == 0)) {
cert->issuer_country = strdup(keyval[1]);
@@ -240,9 +233,7 @@ tlscerts_new(const char* const fingerprint, int version, const char* const seria
cert->issuer_email = strdup(keyval[1]);
}
}
g_strfreev(keyval);
}
g_strfreev(fields);
return cert;
}