libgoatvr

annotate src/opt.c @ 3:83292083070b

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