summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Howard2014-03-25 22:38:11 -0400
committerSimon Howard2014-03-25 22:38:11 -0400
commitbc8517a05648054df17e556d4116e63a20fef75a (patch)
tree48f62450355e68390e0edcab02b77c4e04c7f275
parentf149adaeb7f6ea23aa09a8777a966420815bada1 (diff)
downloadchocolate-doom-bc8517a05648054df17e556d4116e63a20fef75a.tar.gz
chocolate-doom-bc8517a05648054df17e556d4116e63a20fef75a.tar.bz2
chocolate-doom-bc8517a05648054df17e556d4116e63a20fef75a.zip
config: Add API to get/set config variables.
It is useful to be able to set variables through an API provided by the config module; this means that it is possible for one module to set config variables of another in a more loosely-coupled way.
-rw-r--r--src/m_config.c179
-rw-r--r--src/m_config.h6
2 files changed, 134 insertions, 51 deletions
diff --git a/src/m_config.c b/src/m_config.c
index 89d24eaf..9593de57 100644
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -1627,14 +1627,55 @@ static int ParseIntParameter(char *strparm)
return parm;
}
+static void SetVariable(default_t *def, char *value)
+{
+ int intparm;
+
+ // parameter found
+
+ switch (def->type)
+ {
+ case DEFAULT_STRING:
+ * (char **) def->location = strdup(value);
+ break;
+
+ case DEFAULT_INT:
+ case DEFAULT_INT_HEX:
+ * (int *) def->location = ParseIntParameter(value);
+ break;
+
+ case DEFAULT_KEY:
+
+ // translate scancodes read from config
+ // file (save the old value in untranslated)
+
+ intparm = ParseIntParameter(value);
+ def->untranslated = intparm;
+ if (intparm >= 0 && intparm < 128)
+ {
+ intparm = scantokey[intparm];
+ }
+ else
+ {
+ intparm = 0;
+ }
+
+ def->original_translated = intparm;
+ * (int *) def->location = intparm;
+ break;
+
+ case DEFAULT_FLOAT:
+ * (float *) def->location = (float) atof(value);
+ break;
+ }
+}
+
static void LoadDefaultCollection(default_collection_t *collection)
{
- default_t *def;
FILE *f;
+ default_t *def;
char defname[80];
char strparm[100];
- char *s;
- int intparm;
// read the file in, overriding any set defaults
f = fopen(collection->filename, "r");
@@ -1646,26 +1687,18 @@ static void LoadDefaultCollection(default_collection_t *collection)
return;
}
-
+
while (!feof(f))
{
- if (fscanf (f, "%79s %[^\n]\n", defname, strparm) != 2)
+ if (fscanf(f, "%79s %99[^\n]\n", defname, strparm) != 2)
{
// This line doesn't match
-
+
continue;
}
- // Strip off trailing non-printable characters (\r characters
- // from DOS text files)
-
- while (strlen(strparm) > 0 && !isprint(strparm[strlen(strparm)-1]))
- {
- strparm[strlen(strparm)-1] = '\0';
- }
-
// Find the setting in the list
-
+
def = SearchCollection(collection, defname);
if (def == NULL || !def->bound)
@@ -1676,47 +1709,25 @@ static void LoadDefaultCollection(default_collection_t *collection)
continue;
}
- // parameter found
+ // Strip off trailing non-printable characters (\r characters
+ // from DOS text files)
- switch (def->type)
+ while (strlen(strparm) > 0 && !isprint(strparm[strlen(strparm)-1]))
{
- case DEFAULT_STRING:
- s = strdup(strparm + 1);
- s[strlen(s) - 1] = '\0';
- * (char **) def->location = s;
- break;
-
- case DEFAULT_INT:
- case DEFAULT_INT_HEX:
- * (int *) def->location = ParseIntParameter(strparm);
- break;
-
- case DEFAULT_KEY:
-
- // translate scancodes read from config
- // file (save the old value in untranslated)
-
- intparm = ParseIntParameter(strparm);
- def->untranslated = intparm;
- if (intparm >= 0 && intparm < 128)
- {
- intparm = scantokey[intparm];
- }
- else
- {
- intparm = 0;
- }
-
- def->original_translated = intparm;
- * (int *) def->location = intparm;
- break;
+ strparm[strlen(strparm)-1] = '\0';
+ }
- case DEFAULT_FLOAT:
- * (float *) def->location = (float) atof(strparm);
- break;
+ // Surrounded by quotes? If so, remove them.
+ if (strlen(strparm) >= 2
+ && strparm[0] == '"' && strparm[strlen(strparm) - 1] == '"')
+ {
+ strparm[strlen(strparm) - 1] = '\0';
+ memmove(strparm, strparm + 1, sizeof(strparm) - 1);
}
+
+ SetVariable(def, strparm);
}
-
+
fclose (f);
}
@@ -1863,6 +1874,72 @@ void M_BindVariable(char *name, void *location)
variable->bound = true;
}
+// Set the value of a particular variable; an API function for other
+// parts of the program to assign values to config variables by name.
+
+boolean M_SetVariable(char *name, char *value)
+{
+ default_t *variable;
+
+ variable = GetDefaultForName(name);
+
+ if (variable == NULL || !variable->bound)
+ {
+ return false;
+ }
+
+ SetVariable(variable, value);
+
+ return true;
+}
+
+// Get the value of a variable.
+
+int M_GetIntVariable(char *name)
+{
+ default_t *variable;
+
+ variable = GetDefaultForName(name);
+
+ if (variable == NULL || !variable->bound
+ || (variable->type != DEFAULT_INT && variable->type != DEFAULT_INT_HEX))
+ {
+ return 0;
+ }
+
+ return *((int *) variable->location);
+}
+
+const char *M_GetStrVariable(char *name)
+{
+ default_t *variable;
+
+ variable = GetDefaultForName(name);
+
+ if (variable == NULL || !variable->bound
+ || variable->type != DEFAULT_STRING)
+ {
+ return NULL;
+ }
+
+ return *((const char **) variable->location);
+}
+
+float M_GetFloatVariable(char *name)
+{
+ default_t *variable;
+
+ variable = GetDefaultForName(name);
+
+ if (variable == NULL || !variable->bound
+ || variable->type != DEFAULT_FLOAT)
+ {
+ return 0;
+ }
+
+ return *((float *) variable->location);
+}
+
// Get the path to the default configuration dir to use, if NULL
// is passed to M_SetConfigDir.
diff --git a/src/m_config.h b/src/m_config.h
index 999b50ae..7de6e995 100644
--- a/src/m_config.h
+++ b/src/m_config.h
@@ -28,11 +28,17 @@
#ifndef __M_CONFIG__
#define __M_CONFIG__
+#include "doomtype.h"
+
void M_LoadDefaults(void);
void M_SaveDefaults(void);
void M_SaveDefaultsAlternate(char *main, char *extra);
void M_SetConfigDir(char *dir);
void M_BindVariable(char *name, void *variable);
+boolean M_SetVariable(char *name, char *value);
+int M_GetIntVariable(char *name);
+const char *M_GetStrVariable(char *name);
+float M_GetFloatVariable(char *name);
void M_SetConfigFilenames(char *main_config, char *extra_config);
char *M_GetSaveGameDir(char *iwadname);