From 8d77930eceba6841673605f7df5083657a0ecd28 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 14 Apr 2014 22:48:18 +0100 Subject: Added parse_options --- src/tools/parser.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/tools/parser.h | 4 +++- 2 files changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/tools/parser.c b/src/tools/parser.c index 932fd9aa..375572e0 100644 --- a/src/tools/parser.c +++ b/src/tools/parser.c @@ -372,3 +372,43 @@ get_start(char *string, int tokens) return result_str; } + +GHashTable * +parse_options(gchar **args, int start, GList *keys, gboolean *res) +{ + GHashTable *options = NULL; + + // no options found, success + if (args[start] == NULL) { + options = g_hash_table_new(g_str_hash, g_str_equal); + *res = TRUE; + return options; + } + + // check each option is valid and has value, failure if not + int curr; + for (curr = start; curr < g_strv_length(args); curr+= 2) { + if ((g_list_find(keys, args[curr]) == NULL) || (args[curr+1] == NULL)) { + *res = FALSE; + return options; + } + } + + // create map + options = g_hash_table_new(g_str_hash, g_str_equal); + *res = TRUE; + for (curr = start; curr < g_strv_length(args); curr+=2) { + g_hash_table_insert(options, args[curr], args[curr+1]); + } + + return options; +} + +void +options_destroy(GHashTable *options) +{ + if (options != NULL) { + g_hash_table_destroy(options); + options = NULL; + } +} \ No newline at end of file diff --git a/src/tools/parser.h b/src/tools/parser.h index f03fca81..aef3ad37 100644 --- a/src/tools/parser.h +++ b/src/tools/parser.h @@ -29,5 +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); +void options_destroy(GHashTable *options); -#endif +#endif \ No newline at end of file -- cgit 1.4.1-2-gfad0