1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Simplified parse_options to take gchar**

This commit is contained in:
James Booth 2014-04-15 00:13:32 +01:00
parent 79088d0150
commit 4425aba1f2
4 changed files with 24 additions and 56 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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

View File

@ -508,9 +508,7 @@ void
parse_options_when_none_returns_empty_hasmap(void **state)
{
gchar *args[] = { "cmd1", "cmd2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gchar *keys[] = { "opt1", NULL };
gboolean res = FALSE;
@ -521,16 +519,13 @@ parse_options_when_none_returns_empty_hasmap(void **state)
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt1_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gchar *keys[] = { "opt1", NULL };
gboolean res = TRUE;
@ -540,16 +535,13 @@ parse_options_when_opt1_no_val_sets_error(void **state)
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_one_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
gchar *keys[] = { "opt1", NULL };
gboolean res = FALSE;
@ -561,17 +553,13 @@ parse_options_when_one_returns_map(void **state)
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt2_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
gchar *keys[] = { "opt1", "opt2", NULL };
gboolean res = TRUE;
@ -581,17 +569,13 @@ parse_options_when_opt2_no_val_sets_error(void **state)
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_two_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
gchar *keys[] = { "opt1", "opt2", NULL };
gboolean res = FALSE;
@ -605,18 +589,13 @@ parse_options_when_two_returns_map(void **state)
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_opt3_no_val_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gchar *keys[] = { "opt1", "opt2", "opt3", NULL };
gboolean res = TRUE;
@ -626,18 +605,13 @@ parse_options_when_opt3_no_val_sets_error(void **state)
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_three_returns_map(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gchar *keys[] = { "opt1", "opt2", "opt3", NULL };
gboolean res = FALSE;
@ -653,18 +627,13 @@ parse_options_when_three_returns_map(void **state)
assert_true(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_when_unknown_opt_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "oops", "val2", "opt3", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gchar *keys[] = { "opt1", "opt2", "opt3", NULL };
gboolean res = TRUE;
@ -674,18 +643,13 @@ parse_options_when_unknown_opt_sets_error(void **state)
assert_false(res);
options_destroy(options);
g_list_free(keys);
}
void
parse_options_with_duplicated_option_sets_error(void **state)
{
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt1", "val3", NULL };
GList *keys = NULL;
keys = g_list_append(keys, "opt1");
keys = g_list_append(keys, "opt2");
keys = g_list_append(keys, "opt3");
gchar *keys[] = { "opt1", "opt2", "opt3", NULL };
gboolean res = TRUE;
@ -695,5 +659,4 @@ parse_options_with_duplicated_option_sets_error(void **state)
assert_false(res);
options_destroy(options);
g_list_free(keys);
}