ref: bc8517a05648054df17e556d4116e63a20fef75a
parent: f149adaeb7f6ea23aa09a8777a966420815bada1
author: Simon Howard <[email protected]>
date: Tue Mar 25 18:38:11 EDT 2014
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.
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -1627,14 +1627,55 @@
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 @@
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 @@
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;
+ strparm[strlen(strparm)-1] = '\0';
+ }
- 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;
-
- 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);
}
@@ -1861,6 +1872,72 @@
variable->location = 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
--- 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);