Added parse_options
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user