conworlds

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vr/opt.c	Fri Aug 22 16:55:16 2014 +0300
     1.3 @@ -0,0 +1,81 @@
     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(void *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 +	printf("stored %s -> %p\n", key, opt);
    1.43 +}
    1.44 +
    1.45 +void set_option_float(void *optdb, const char *key, float val)
    1.46 +{
    1.47 +	struct option *opt = malloc(sizeof *opt);
    1.48 +	if(!opt) {
    1.49 +		fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
    1.50 +		return;
    1.51 +	}
    1.52 +	opt->type = OTYPE_FLOAT;
    1.53 +	opt->fval = val;
    1.54 +	opt->ival = (int)val;
    1.55 +
    1.56 +	if(rb_insert(optdb, (void*)key, opt) == -1) {
    1.57 +		fprintf(stderr, "failed to set option: %s\n", key);
    1.58 +	}
    1.59 +	printf("stored %s -> %p\n", key, opt);
    1.60 +}
    1.61 +
    1.62 +int get_option_int(void *optdb, const char *key, int *val)
    1.63 +{
    1.64 +	struct option *opt = rb_find(optdb, (void*)key);
    1.65 +	if(!opt) {
    1.66 +		*val = 0;
    1.67 +		return -1;
    1.68 +	}
    1.69 +	printf("got %s -> %p\n", key, opt);
    1.70 +	*val = opt->ival;
    1.71 +	return 0;
    1.72 +}
    1.73 +
    1.74 +int get_option_float(void *optdb, const char *key, float *val)
    1.75 +{
    1.76 +	struct option *opt = rb_find(optdb, (void*)key);
    1.77 +	if(!opt) {
    1.78 +		*val = 0.0f;
    1.79 +		return -1;
    1.80 +	}
    1.81 +	printf("got %s -> %p\n", key, opt);
    1.82 +	*val = opt->fval;
    1.83 +	return 0;
    1.84 +}