conworlds

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