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