nuclear@8: #include nuclear@8: #include nuclear@8: #include nuclear@8: #include nuclear@8: #include "opt.h" nuclear@8: #include "rbtree.h" nuclear@8: nuclear@8: static void opt_del_func(void *opt, void *cls) nuclear@8: { nuclear@8: free(opt); nuclear@8: } nuclear@8: nuclear@8: void *create_options(void) nuclear@8: { nuclear@8: struct rbtree *db = rb_create(RB_KEY_STRING); nuclear@8: rb_set_delete_func(db, opt_del_func, 0); nuclear@8: return db; nuclear@8: } nuclear@8: nuclear@8: void destroy_options(void *optdb) nuclear@8: { nuclear@8: rb_destroy(optdb); nuclear@8: } nuclear@8: nuclear@8: void set_option_int(void *optdb, const char *key, int val) nuclear@8: { nuclear@8: struct option *opt = malloc(sizeof *opt); nuclear@8: if(!opt) { nuclear@8: fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno)); nuclear@8: return; nuclear@8: } nuclear@8: opt->type = OTYPE_INT; nuclear@8: opt->ival = val; nuclear@8: opt->fval = (float)val; nuclear@8: nuclear@8: if(rb_insert(optdb, (void*)key, opt) == -1) { nuclear@8: fprintf(stderr, "failed to set option: %s\n", key); nuclear@8: } nuclear@8: } nuclear@8: nuclear@8: void set_option_float(void *optdb, const char *key, float val) nuclear@8: { nuclear@8: struct option *opt = malloc(sizeof *opt); nuclear@8: if(!opt) { nuclear@8: fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno)); nuclear@8: return; nuclear@8: } nuclear@8: opt->type = OTYPE_FLOAT; nuclear@8: opt->fval = val; nuclear@8: opt->ival = (int)val; nuclear@8: nuclear@8: if(rb_insert(optdb, (void*)key, opt) == -1) { nuclear@8: fprintf(stderr, "failed to set option: %s\n", key); nuclear@8: } nuclear@8: } nuclear@8: nuclear@8: int get_option_int(void *optdb, const char *key, int *val) nuclear@8: { nuclear@8: struct option *opt = rb_find(optdb, (void*)key); nuclear@8: if(!opt) { nuclear@8: *val = 0; nuclear@8: return -1; nuclear@8: } nuclear@8: *val = opt->ival; nuclear@8: return 0; nuclear@8: } nuclear@8: nuclear@8: int get_option_float(void *optdb, const char *key, float *val) nuclear@8: { nuclear@8: struct option *opt = rb_find(optdb, (void*)key); nuclear@8: if(!opt) { nuclear@8: *val = 0.0f; nuclear@8: return -1; nuclear@8: } nuclear@8: *val = opt->fval; nuclear@8: return 0; nuclear@8: }