conworlds

view src/vr/opt.c @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents
children 90abf4b93cc9
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(void *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 printf("stored %s -> %p\n", key, opt);
40 }
42 void set_option_float(void *optdb, const char *key, float val)
43 {
44 struct option *opt = malloc(sizeof *opt);
45 if(!opt) {
46 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
47 return;
48 }
49 opt->type = OTYPE_FLOAT;
50 opt->fval = val;
51 opt->ival = (int)val;
53 if(rb_insert(optdb, (void*)key, opt) == -1) {
54 fprintf(stderr, "failed to set option: %s\n", key);
55 }
56 printf("stored %s -> %p\n", key, opt);
57 }
59 int get_option_int(void *optdb, const char *key, int *val)
60 {
61 struct option *opt = rb_find(optdb, (void*)key);
62 if(!opt) {
63 *val = 0;
64 return -1;
65 }
66 printf("got %s -> %p\n", key, opt);
67 *val = opt->ival;
68 return 0;
69 }
71 int get_option_float(void *optdb, const char *key, float *val)
72 {
73 struct option *opt = rb_find(optdb, (void*)key);
74 if(!opt) {
75 *val = 0.0f;
76 return -1;
77 }
78 printf("got %s -> %p\n", key, opt);
79 *val = opt->fval;
80 return 0;
81 }