libgoatvr
diff src/opt.c @ 0:ded3d0a74e19
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 29 Aug 2014 03:45:25 +0300 |
parents | |
children | 437fe32ac633 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/opt.c Fri Aug 29 03:45:25 2014 +0300 1.3 @@ -0,0 +1,77 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "opt.h" 1.9 +#include "rbtree.h" 1.10 + 1.11 +static void opt_del_func(struct rbnode *opt, void *cls) 1.12 +{ 1.13 + free(opt); 1.14 +} 1.15 + 1.16 +void *create_options(void) 1.17 +{ 1.18 + struct rbtree *db = rb_create(RB_KEY_STRING); 1.19 + rb_set_delete_func(db, opt_del_func, 0); 1.20 + return db; 1.21 +} 1.22 + 1.23 +void destroy_options(void *optdb) 1.24 +{ 1.25 + rb_destroy(optdb); 1.26 +} 1.27 + 1.28 +void set_option_int(void *optdb, const char *key, int val) 1.29 +{ 1.30 + struct option *opt = malloc(sizeof *opt); 1.31 + if(!opt) { 1.32 + fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno)); 1.33 + return; 1.34 + } 1.35 + opt->type = OTYPE_INT; 1.36 + opt->ival = val; 1.37 + opt->fval = (float)val; 1.38 + 1.39 + if(rb_insert(optdb, (void*)key, opt) == -1) { 1.40 + fprintf(stderr, "failed to set option: %s\n", key); 1.41 + } 1.42 +} 1.43 + 1.44 +void set_option_float(void *optdb, const char *key, float val) 1.45 +{ 1.46 + struct option *opt = malloc(sizeof *opt); 1.47 + if(!opt) { 1.48 + fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno)); 1.49 + return; 1.50 + } 1.51 + opt->type = OTYPE_FLOAT; 1.52 + opt->fval = val; 1.53 + opt->ival = (int)val; 1.54 + 1.55 + if(rb_insert(optdb, (void*)key, opt) == -1) { 1.56 + fprintf(stderr, "failed to set option: %s\n", key); 1.57 + } 1.58 +} 1.59 + 1.60 +int get_option_int(void *optdb, const char *key, int *val) 1.61 +{ 1.62 + struct option *opt = rb_find(optdb, (void*)key); 1.63 + if(!opt) { 1.64 + *val = 0; 1.65 + return -1; 1.66 + } 1.67 + *val = opt->ival; 1.68 + return 0; 1.69 +} 1.70 + 1.71 +int get_option_float(void *optdb, const char *key, float *val) 1.72 +{ 1.73 + struct option *opt = rb_find(optdb, (void*)key); 1.74 + if(!opt) { 1.75 + *val = 0.0f; 1.76 + return -1; 1.77 + } 1.78 + *val = opt->fval; 1.79 + return 0; 1.80 +}