From b39121c6a682eb8ae5efd29a875bd7c098185f04 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 20 Feb 2015 00:31:09 -0500 Subject: Refactor config file API. The config file API previously relied on binding config variables using M_BindVariable() which took a void pointer. It occurred to me that if used on a boolean variable, this would be erroneous, but the void pointer would make it impossible to tell. Split this into separate M_Bind{Foo}Variable() functions based on type, which allows for proper type checking on the pointers that are passed. Vaguely related to #509. --- src/m_config.c | 64 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'src/m_config.c') diff --git a/src/m_config.c b/src/m_config.c index 083d298a..ad539a7f 100644 --- a/src/m_config.c +++ b/src/m_config.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "config.h" @@ -64,7 +65,11 @@ typedef struct char *name; // Pointer to the location in memory of the variable - void *location; + union { + int *i; + char **s; + float *f; + } location; // Type of the variable default_type_t type; @@ -93,7 +98,7 @@ typedef struct } default_collection_t; #define CONFIG_VARIABLE_GENERIC(name, type) \ - { #name, NULL, type, 0, 0, false } + { #name, {NULL}, type, 0, 0, false } #define CONFIG_VARIABLE_KEY(name) \ CONFIG_VARIABLE_GENERIC(name, DEFAULT_KEY) @@ -1646,7 +1651,7 @@ static void SaveDefaultCollection(default_collection_t *collection) // the possibility of screwing up the user's config // file - v = * (int *) defaults[i].location; + v = *defaults[i].location.i; if (v == KEY_RSHIFT) { @@ -1687,19 +1692,19 @@ static void SaveDefaultCollection(default_collection_t *collection) break; case DEFAULT_INT: - fprintf(f, "%i", * (int *) defaults[i].location); + fprintf(f, "%i", *defaults[i].location.i); break; case DEFAULT_INT_HEX: - fprintf(f, "0x%x", * (int *) defaults[i].location); + fprintf(f, "0x%x", *defaults[i].location.i); break; case DEFAULT_FLOAT: - fprintf(f, "%f", * (float *) defaults[i].location); + fprintf(f, "%f", *defaults[i].location.f); break; case DEFAULT_STRING: - fprintf(f,"\"%s\"", * (char **) (defaults[i].location)); + fprintf(f,"\"%s\"", *defaults[i].location.s); break; } @@ -1732,12 +1737,12 @@ static void SetVariable(default_t *def, char *value) switch (def->type) { case DEFAULT_STRING: - * (char **) def->location = M_StringDuplicate(value); + *def->location.s = M_StringDuplicate(value); break; case DEFAULT_INT: case DEFAULT_INT_HEX: - * (int *) def->location = ParseIntParameter(value); + *def->location.i = ParseIntParameter(value); break; case DEFAULT_KEY: @@ -1757,11 +1762,11 @@ static void SetVariable(default_t *def, char *value) } def->original_translated = intparm; - * (int *) def->location = intparm; + *def->location.i = intparm; break; case DEFAULT_FLOAT: - * (float *) def->location = (float) atof(value); + *def->location.f = (float) atof(value); break; } } @@ -1957,13 +1962,38 @@ static default_t *GetDefaultForName(char *name) // Bind a variable to a given configuration file variable, by name. // -void M_BindVariable(char *name, void *location) +void M_BindIntVariable(char *name, int *location) { default_t *variable; variable = GetDefaultForName(name); + assert(variable->type == DEFAULT_INT + || variable->type == DEFAULT_INT_HEX + || variable->type == DEFAULT_KEY); - variable->location = location; + variable->location.i = location; + variable->bound = true; +} + +void M_BindFloatVariable(char *name, float *location) +{ + default_t *variable; + + variable = GetDefaultForName(name); + assert(variable->type == DEFAULT_FLOAT); + + variable->location.f = location; + variable->bound = true; +} + +void M_BindStringVariable(char *name, char **location) +{ + default_t *variable; + + variable = GetDefaultForName(name); + assert(variable->type == DEFAULT_STRING); + + variable->location.s = location; variable->bound = true; } @@ -2000,10 +2030,10 @@ int M_GetIntVariable(char *name) return 0; } - return *((int *) variable->location); + return *variable->location.i; } -const char *M_GetStrVariable(char *name) +const char *M_GetStringVariable(char *name) { default_t *variable; @@ -2015,7 +2045,7 @@ const char *M_GetStrVariable(char *name) return NULL; } - return *((const char **) variable->location); + return *variable->location.s; } float M_GetFloatVariable(char *name) @@ -2030,7 +2060,7 @@ float M_GetFloatVariable(char *name) return 0; } - return *((float *) variable->location); + return *variable->location.f; } // Get the path to the default configuration dir to use, if NULL -- cgit v1.2.3