From 3e69d6b71ed57964df92f172d8f7a041e798a2e3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 14 Apr 2014 23:36:00 +0100 Subject: Refactored cmd_join to use parse_options --- src/command/commands.c | 48 +++++++++++++++--------------------------------- src/tools/parser.c | 4 ++-- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index c03d294d..51334934 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1551,7 +1551,6 @@ cmd_join(gchar **args, struct cmd_help_t help) return TRUE; } - int num_args = g_strv_length(args); char *room = NULL; char *nick = NULL; char *passwd = NULL; @@ -1572,40 +1571,23 @@ cmd_join(gchar **args, struct cmd_help_t help) } // Additional args supplied - if (num_args > 1) { - char *opt1 = args[1]; - char *opt1val = args[2]; - char *opt2 = args[3]; - char *opt2val = args[4]; - if (opt1 != NULL) { - if (opt1val == NULL) { - cons_show("Usage: %s", help.usage); - cons_show(""); - return TRUE; - } - if (strcmp(opt1, "nick") == 0) { - nick = opt1val; - } else if (strcmp(opt1, "password") == 0) { - passwd = opt1val; - } else { - cons_show("Usage: %s", help.usage); - cons_show(""); - return TRUE; - } - if (opt2 != NULL) { - if (strcmp(opt2, "nick") == 0) { - nick = opt2val; - } else if (strcmp(opt2, "password") == 0) { - passwd = opt2val; - } else { - cons_show("Usage: %s", help.usage); - cons_show(""); - return TRUE; - } - } - } + GList *opt_keys = NULL; + opt_keys = g_list_append(opt_keys, "nick"); + opt_keys = g_list_append(opt_keys, "password"); + gboolean parsed; + + GHashTable *options = parse_options(args, 1, opt_keys, &parsed); + if (!parsed) { + cons_show("Usage: %s", help.usage); + cons_show(""); + return TRUE; } + nick = g_hash_table_lookup(options, "nick"); + passwd = g_hash_table_lookup(options, "password"); + + options_destroy(options); + // In the case that a nick wasn't provided by the optional args... if (nick == NULL) { nick = account->muc_nick; diff --git a/src/tools/parser.c b/src/tools/parser.c index b4403d06..6db522aa 100644 --- a/src/tools/parser.c +++ b/src/tools/parser.c @@ -390,13 +390,13 @@ parse_options(gchar **args, int start, GList *keys, gboolean *res) GList *found_keys = NULL; for (curr = start; curr < g_strv_length(args); curr+= 2) { // check if option valid - if (g_list_find(keys, args[curr]) == NULL) { + if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) { *res = FALSE; return options; } // check if duplicate - if (g_list_find(found_keys, args[curr]) != NULL) { + if (g_list_find_custom(found_keys, args[curr], (GCompareFunc)g_strcmp0) != NULL) { *res = FALSE; return options; } -- cgit 1.4.1-2-gfad0