options stuff

personal/stbuehler/wip
Thomas Porzelt 15 years ago
parent b74a2ba5d3
commit 16f2068fad
  1. 39
      src/options.c
  2. 18
      src/options.h

@ -57,7 +57,7 @@ void option_free(option* opt) {
g_string_free(opt->value.opt_string, TRUE);
break;
case OPTION_LIST:
for (i=0; i<opt->value.opt_list->len; i++)
for (i = 0; i < opt->value.opt_list->len; i++)
option_free(g_array_index(opt->value.opt_list, option *, i));
g_array_free(opt->value.opt_list, FALSE);
break;
@ -68,3 +68,40 @@ void option_free(option* opt) {
opt->type = OPTION_NONE;
g_slice_free(option, opt);
}
gboolean option_register(GString *name, option *opt) {
guint *ndx;
ndx = g_slice_new(guint);
/* check if not already registered */
if (option_index(name, ndx))
return FALSE;
g_array_append_val(options, opt);
*ndx = options->len;
g_hash_table_insert(options_hash, (gpointer) name, (gpointer) ndx);
return TRUE;
}
gboolean option_unregister(GString *name) {
UNUSED(name);
assert(NULL == "does this even make sense?");
}
gboolean option_index(GString *name, guint *ndx) {
guint *val;
val = (guint *) g_hash_table_lookup(options_hash, (gconstpointer) name);
if (val == NULL)
return FALSE;
*ndx = *val;
return TRUE;
}

@ -11,6 +11,11 @@ typedef struct option_mark option_mark;
#include "base.h"
/* registered options */
GArray *options;
/* hash table to speed up lookup of options by name */
GHashTable *options_hash;
struct option {
option_type type;
union {
@ -24,9 +29,9 @@ struct option {
} value;
};
struct option_mark {
size_t ndx;
gpointer value;
struct option_set {
guint ndx;
option opt;
};
LI_API option* option_new_bool(gboolean val);
@ -36,4 +41,11 @@ LI_API option* option_new_list();
LI_API option* option_new_hash();
LI_API void option_free(option* opt);
/* registers an option */
LI_API gboolean option_register(GString *name, option *opt);
/* unregisters an option */
LI_API gboolean option_unregister(GString *name);
/* retrieves the index of a previously registered option. returns TRUE if option was found, FALSE otherwise */
LI_API gboolean option_index(GString *name, guint *index);
#endif

Loading…
Cancel
Save