vrchess

view src/vr/opt.c @ 10:e3f0ca1d008a

added preliminary OpenHMD module
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 20:11:15 +0300
parents 90abf4b93cc9
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "opt.h"
6 #include "rbtree.h"
8 static void opt_del_func(struct rbnode *opt, void *cls)
9 {
10 free(opt);
11 }
13 void *create_options(void)
14 {
15 struct rbtree *db = rb_create(RB_KEY_STRING);
16 rb_set_delete_func(db, opt_del_func, 0);
17 return db;
18 }
20 void destroy_options(void *optdb)
21 {
22 rb_destroy(optdb);
23 }
25 void set_option_int(void *optdb, const char *key, int val)
26 {
27 struct option *opt = malloc(sizeof *opt);
28 if(!opt) {
29 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
30 return;
31 }
32 opt->type = OTYPE_INT;
33 opt->ival = val;
34 opt->fval = (float)val;
36 if(rb_insert(optdb, (void*)key, opt) == -1) {
37 fprintf(stderr, "failed to set option: %s\n", key);
38 }
39 }
41 void set_option_float(void *optdb, const char *key, float val)
42 {
43 struct option *opt = malloc(sizeof *opt);
44 if(!opt) {
45 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
46 return;
47 }
48 opt->type = OTYPE_FLOAT;
49 opt->fval = val;
50 opt->ival = (int)val;
52 if(rb_insert(optdb, (void*)key, opt) == -1) {
53 fprintf(stderr, "failed to set option: %s\n", key);
54 }
55 }
57 int get_option_int(void *optdb, const char *key, int *val)
58 {
59 struct option *opt = rb_find(optdb, (void*)key);
60 if(!opt) {
61 *val = 0;
62 return -1;
63 }
64 *val = opt->ival;
65 return 0;
66 }
68 int get_option_float(void *optdb, const char *key, float *val)
69 {
70 struct option *opt = rb_find(optdb, (void*)key);
71 if(!opt) {
72 *val = 0.0f;
73 return -1;
74 }
75 *val = opt->fval;
76 return 0;
77 }