Skip to content

Commit

Permalink
Cleanup g_strfreev() to auto_gcharv
Browse files Browse the repository at this point in the history
Include some additional minor cleanups
  • Loading branch information
H3rnand3zzz committed Jul 13, 2023
1 parent 029f1ca commit 865a056
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 234 deletions.
9 changes: 2 additions & 7 deletions src/command/cmd_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -3210,25 +3210,23 @@ _form_field_autocomplete(ProfWin* window, const char* const input, gboolean prev
return NULL;
}

gchar** split = g_strsplit(input, " ", 0);
auto_gcharv gchar** split = g_strsplit(input, " ", 0);

if (g_strv_length(split) == 3) {
char* field_tag = split[0] + 1;
if (form_tag_exists(form, field_tag)) {
form_field_type_t field_type = form_get_field_type(form, field_tag);
Autocomplete value_ac = form_get_value_ac(form, field_tag);
;

GString* beginning = g_string_new(split[0]);
g_string_append(beginning, " ");
g_string_append(beginning, split[1]);

if (((g_strcmp0(split[1], "add") == 0) || (g_strcmp0(split[1], "remove") == 0))
&& field_type == FIELD_LIST_MULTI) {
found = autocomplete_param_with_ac(input, beginning->str, value_ac, TRUE, previous);

} else if ((g_strcmp0(split[1], "remove") == 0) && field_type == FIELD_TEXT_MULTI) {
found = autocomplete_param_with_ac(input, beginning->str, value_ac, TRUE, previous);

} else if ((g_strcmp0(split[1], "remove") == 0) && field_type == FIELD_JID_MULTI) {
found = autocomplete_param_with_ac(input, beginning->str, value_ac, TRUE, previous);
}
Expand All @@ -3241,7 +3239,6 @@ _form_field_autocomplete(ProfWin* window, const char* const input, gboolean prev
if (form_tag_exists(form, field_tag)) {
form_field_type_t field_type = form_get_field_type(form, field_tag);
Autocomplete value_ac = form_get_value_ac(form, field_tag);
;

switch (field_type) {
case FIELD_BOOLEAN:
Expand All @@ -3261,8 +3258,6 @@ _form_field_autocomplete(ProfWin* window, const char* const input, gboolean prev
}
}

g_strfreev(split);

return found;
}

Expand Down
10 changes: 3 additions & 7 deletions src/command/cmd_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2768,15 +2768,14 @@ _cmd_index(const Command* cmd)
index_source = g_string_append(index_source, " ");
}

gchar** tokens = g_str_tokenize_and_fold(index_source->str, NULL, NULL);
auto_gcharv gchar** tokens = g_str_tokenize_and_fold(index_source->str, NULL, NULL);
g_string_free(index_source, TRUE);

GString* index = g_string_new("");
for (int i = 0; i < g_strv_length(tokens); i++) {
index = g_string_append(index, tokens[i]);
index = g_string_append(index, " ");
}
g_strfreev(tokens);

return g_string_free(index, FALSE);
}
Expand All @@ -2786,7 +2785,7 @@ cmd_search_index_any(char* term)
{
GList* results = NULL;

gchar** processed_terms = g_str_tokenize_and_fold(term, NULL, NULL);
auto_gcharv gchar** processed_terms = g_str_tokenize_and_fold(term, NULL, NULL);
int terms_len = g_strv_length(processed_terms);

for (int i = 0; i < terms_len; i++) {
Expand All @@ -2802,8 +2801,6 @@ cmd_search_index_any(char* term)
g_list_free(index_keys);
}

g_strfreev(processed_terms);

return results;
}

Expand All @@ -2812,7 +2809,7 @@ cmd_search_index_all(char* term)
{
GList* results = NULL;

gchar** terms = g_str_tokenize_and_fold(term, NULL, NULL);
auto_gcharv gchar** terms = g_str_tokenize_and_fold(term, NULL, NULL);
int terms_len = g_strv_length(terms);

GList* commands = g_hash_table_get_keys(search_index);
Expand All @@ -2833,7 +2830,6 @@ cmd_search_index_all(char* term)
}

g_list_free(commands);
g_strfreev(terms);

return results;
}
Expand Down
27 changes: 9 additions & 18 deletions src/command/cmd_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,8 @@ cmd_connect(ProfWin* window, const char* const command, gchar** args)
gboolean
cmd_account_list(ProfWin* window, const char* const command, gchar** args)
{
gchar** accounts = accounts_get_list();
auto_gcharv gchar** accounts = accounts_get_list();
cons_show_account_list(accounts);
g_strfreev(accounts);

return TRUE;
}
Expand Down Expand Up @@ -4839,9 +4838,8 @@ cmd_bookmark_ignore(ProfWin* window, const char* const command, gchar** args)
// `/bookmark ignore` lists them
if (args[1] == NULL) {
gsize len = 0;
gchar** list = bookmark_ignore_list(&len);
auto_gcharv gchar** list = bookmark_ignore_list(&len);
cons_show_bookmarks_ignore(list, len);
g_strfreev(list);
return TRUE;
}

Expand Down Expand Up @@ -8479,26 +8477,24 @@ _cmd_execute(ProfWin* window, const char* const command, const char* const inp)
{
if (g_str_has_prefix(command, "/field") && window->type == WIN_CONFIG) {
gboolean result = FALSE;
gchar** args = parse_args_with_freetext(inp, 1, 2, &result);
auto_gcharv gchar** args = parse_args_with_freetext(inp, 1, 2, &result);
if (!result) {
win_println(window, THEME_DEFAULT, "!", "Invalid command, see /form help");
result = TRUE;
} else {
gchar** tokens = g_strsplit(inp, " ", 2);
auto_gcharv gchar** tokens = g_strsplit(inp, " ", 2);
char* field = tokens[0] + 1;
result = cmd_form_field(window, field, args);
g_strfreev(tokens);
}

g_strfreev(args);
return result;
}

Command* cmd = cmd_get(command);
gboolean result = FALSE;

if (cmd) {
gchar** args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
auto_gcharv gchar** args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
if (result == FALSE) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
return TRUE;
Expand All @@ -8507,20 +8503,17 @@ _cmd_execute(ProfWin* window, const char* const command, const char* const inp)
int i = 0;
while (cmd->sub_funcs[i].cmd) {
if (g_strcmp0(args[0], (char*)cmd->sub_funcs[i].cmd) == 0) {
result = cmd->sub_funcs[i].func(window, command, args);
goto out;
return cmd->sub_funcs[i].func(window, command, args);
}
i++;
}
}
if (!cmd->func) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
result = TRUE;
goto out;
return TRUE;
}
result = cmd->func(window, command, args);
out:
g_strfreev(args);

return result;
} else if (plugins_run_command(inp)) {
return TRUE;
Expand Down Expand Up @@ -9543,15 +9536,13 @@ _url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* p
void
_url_external_method(const char* cmd_template, const char* url, gchar* filename)
{
gchar** argv = format_call_external_argv(cmd_template, url, filename);
auto_gcharv gchar** argv = format_call_external_argv(cmd_template, url, filename);

if (!call_external(argv)) {
cons_show_error("Unable to call external executable for url: check the logs for more information.");
} else {
cons_show("URL '%s' has been called with '%s'.", url, cmd_template);
}

g_strfreev(argv);
}

gboolean
Expand Down
25 changes: 8 additions & 17 deletions src/config/accounts.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,14 @@ 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]);
if (g_key_file_get_boolean(accounts, account_names[i], "enabled", NULL)) {
autocomplete_add(enabled_ac, account_names[i]);
}
}

g_strfreev(account_names);
}

void
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
61 changes: 30 additions & 31 deletions src/config/conflists.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/

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

#include <string.h>
#include <glib.h>
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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));
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);
g_list_free_full(glist, g_free);

return deleted;
}
Loading

0 comments on commit 865a056

Please sign in to comment.