Make C configuration even more insane

David thought we weren't using enough void pointers.
This commit is contained in:
Michael Spang 2009-01-17 20:23:44 -05:00
parent 6aec4b3c25
commit e2bf0997e2
1 changed files with 33 additions and 37 deletions

View File

@ -14,27 +14,15 @@
#undef CONFIG_STR
#undef CONFIG_INT
struct config_var_str {
struct config_var {
const char *name;
char **p;
void *p;
enum { CONFIG_TYPE_STR, CONFIG_TYPE_INT } type;
};
struct config_var_int {
const char *name;
long *p;
};
#define CONFIG_STR(x) {#x, &x},
#define CONFIG_INT(x)
static struct config_var_str str_vars[] = {
#include "config-vars.h"
};
#undef CONFIG_STR
#undef CONFIG_INT
#define CONFIG_STR(x)
#define CONFIG_INT(x) {#x, &x},
static struct config_var_int int_vars[] = {
#define CONFIG_STR(x) {#x, &x, CONFIG_TYPE_STR },
#define CONFIG_INT(x) {#x, &x, CONFIG_TYPE_INT },
static struct config_var config_vars[] = {
#include "config-vars.h"
};
#undef CONFIG_STR
@ -43,17 +31,20 @@ static struct config_var_int int_vars[] = {
void config_var(char *var, char *val) {
int i;
for (i = 0; i < sizeof(str_vars)/sizeof(*str_vars); i++) {
if (!strcmp(var, str_vars[i].name)) {
if (*str_vars[i].p)
free(*str_vars[i].p);
*str_vars[i].p = xstrdup(val);
}
}
for (i = 0; i < sizeof(int_vars)/sizeof(*int_vars); i++) {
if (!strcmp(var, int_vars[i].name)) {
*int_vars[i].p = config_long(var, val);
for (i = 0; i < sizeof(config_vars)/sizeof(*config_vars); i++) {
if (!strcmp(var, config_vars[i].name)) {
switch (config_vars[i].type) {
case CONFIG_TYPE_STR:
if (*(char **)config_vars[i].p)
free(*(char **)config_vars[i].p);
*(char **)config_vars[i].p = xstrdup(val);
break;
case CONFIG_TYPE_INT:
*(long *)config_vars[i].p = config_long(var, val);
break;
default:
fatal("unknown config var type %d", config_vars[i].type);
}
}
}
}
@ -63,13 +54,18 @@ void configure() {
config_parse(CONFIG_FILE);
for (i = 0; i < sizeof(str_vars)/sizeof(*str_vars); i++) {
if (*str_vars[i].p == DEF_STR)
badconf("undefined string variable: %s", str_vars[i].name);
}
for (i = 0; i < sizeof(int_vars)/sizeof(*int_vars); i++) {
if (*int_vars[i].p == DEF_INT)
badconf("undefined integer variable: %s", int_vars[i].name);
for (i = 0; i < sizeof(config_vars)/sizeof(*config_vars); i++) {
switch (config_vars[i].type) {
case CONFIG_TYPE_STR:
if (*(char **)config_vars[i].p == DEF_STR)
badconf("undefined string variable: %s", config_vars[i].name);
break;
case CONFIG_TYPE_INT:
if (*(long *)config_vars[i].p == DEF_INT)
badconf("undefined integer variable: %s", config_vars[i].name);
break;
default:
fatal("unknown config var type %d", config_vars[i].type);
}
}
}