From 4425aba1f218cdaeacc607a69287db4ad951b2d0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 15 Apr 2014 00:13:32 +0100 Subject: Simplified parse_options to take gchar** --- src/command/commands.c | 10 ++-------- src/tools/parser.c | 13 ++++++++++++- src/tools/parser.h | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/command/commands.c b/src/command/commands.c index 63ecb48e..2daa82d1 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -68,9 +68,7 @@ cmd_connect(gchar **args, struct cmd_help_t help) cons_show("You are either connected already, or a login is in process."); result = TRUE; } else { - GList *opt_keys = NULL; - opt_keys = g_list_append(opt_keys, "server"); - opt_keys = g_list_append(opt_keys, "port"); + gchar *opt_keys[] = { "server", "port", NULL }; gboolean parsed; GHashTable *options = parse_options(args, 1, opt_keys, &parsed); @@ -93,7 +91,6 @@ cmd_connect(gchar **args, struct cmd_help_t help) } options_destroy(options); - g_list_free(opt_keys); char *user = args[0]; char *lower = g_utf8_strdown(user, -1); @@ -1572,9 +1569,7 @@ cmd_join(gchar **args, struct cmd_help_t help) } // Additional args supplied - GList *opt_keys = NULL; - opt_keys = g_list_append(opt_keys, "nick"); - opt_keys = g_list_append(opt_keys, "password"); + gchar *opt_keys[] = { "nick", "password", NULL }; gboolean parsed; GHashTable *options = parse_options(args, 1, opt_keys, &parsed); @@ -1588,7 +1583,6 @@ cmd_join(gchar **args, struct cmd_help_t help) passwd = g_hash_table_lookup(options, "password"); options_destroy(options); - g_list_free(opt_keys); // In the case that a nick wasn't provided by the optional args... if (nick == NULL) { diff --git a/src/tools/parser.c b/src/tools/parser.c index 6db522aa..ff00b1e8 100644 --- a/src/tools/parser.c +++ b/src/tools/parser.c @@ -374,14 +374,21 @@ get_start(char *string, int tokens) } GHashTable * -parse_options(gchar **args, int start, GList *keys, gboolean *res) +parse_options(gchar **args, int start, gchar **opt_keys, gboolean *res) { + GList *keys = NULL; + int i; + for (i = 0; i < g_strv_length(opt_keys); i++) { + keys = g_list_append(keys, opt_keys[i]); + } + GHashTable *options = NULL; // no options found, success if (args[start] == NULL) { options = g_hash_table_new(g_str_hash, g_str_equal); *res = TRUE; + g_list_free(keys); return options; } @@ -392,24 +399,28 @@ parse_options(gchar **args, int start, GList *keys, gboolean *res) // check if option valid if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) { *res = FALSE; + g_list_free(keys); return options; } // check if duplicate if (g_list_find_custom(found_keys, args[curr], (GCompareFunc)g_strcmp0) != NULL) { *res = FALSE; + g_list_free(keys); return options; } // check value given if (args[curr+1] == NULL) { *res = FALSE; + g_list_free(keys); return options; } found_keys = g_list_append(found_keys, args[curr]); } g_list_free(found_keys); + g_list_free(keys); // create map options = g_hash_table_new(g_str_hash, g_str_equal); diff --git a/src/tools/parser.h b/src/tools/parser.h index aef3ad37..10ee627c 100644 --- a/src/tools/parser.h +++ b/src/tools/parser.h @@ -29,7 +29,7 @@ gchar** parse_args(const char * const inp, int min, int max, gboolean *result); gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result); int count_tokens(char *string); char* get_start(char *string, int tokens); -GHashTable* parse_options(gchar **args, int start, GList *keys, gboolean *res); +GHashTable* parse_options(gchar **args, int start, gchar **keys, gboolean *res); void options_destroy(GHashTable *options); #endif \ No newline at end of file -- cgit 1.4.1-2-gfad0