diff options
Diffstat (limited to 'src/engine/config.cpp')
| -rw-r--r-- | src/engine/config.cpp | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/engine/config.cpp b/src/engine/config.cpp index f38be184..3db77e3a 100644 --- a/src/engine/config.cpp +++ b/src/engine/config.cpp @@ -1,10 +1,15 @@ #include <baselib/system.h> +#include <fstream> +#include <iostream> + #include <cstring> #include <cstdio> #include "config.h" +using namespace std; + configuration config; void config_reset() @@ -21,15 +26,53 @@ void config_reset() void config_load(const char *filename) { dbg_msg("config/load", "loading %s", filename); + + ifstream file(filename); + string line; + + if (file) + { + while (getline(file, line)) + { + int eq_index = line.find_first_of('='); + if (eq_index != string::npos) + { + string variable = line.substr(0, eq_index); + string value = line.substr(eq_index + 1); + + #define MACRO_CONFIG_INT(name,def,min,max) { if (strcmp(#name, variable.c_str()) == 0) config_set_ ## name (&config, atoi(value.c_str())); } + #define MACRO_CONFIG_STR(name,len,def) { if (strcmp(#name, variable.c_str()) == 0) { config_set_ ## name (&config, value.c_str()); cout << variable << '=' << value << endl; } } + + #include "config_variables.h" + + #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_STR + } + } + + file.close(); + } } void config_save(const char *filename) { dbg_msg("config/save", "saving config to %s", filename); + + ofstream file(filename); + + #define MACRO_CONFIG_INT(name,def,min,max) { file << # name << '=' << config.name << endl; } + #define MACRO_CONFIG_STR(name,len,def) { file << # name << '=' << config.name << endl; } + + #include "config_variables.h" + + #undef MACRO_CONFIG_INT + #undef MACRO_CONFIG_STR + + file.close(); } #define MACRO_CONFIG_INT(name,def,min,max) void config_set_ ## name (configuration *c, int val) { if (val < min) val = min; if (max != 0 && val > max) val = max; c->name = val; } -#define MACRO_CONFIG_STR(name,len,def) void config_set_ ## name (configuration *c, char *str) { strncpy(c->name, def, len-1); c->name[sizeof(c->name)-1] = 0; } +#define MACRO_CONFIG_STR(name,len,def) void config_set_ ## name (configuration *c, const char *str) { strncpy(c->name, str, len-1); c->name[sizeof(c->name)-1] = 0; cout << "config_str" << endl; } #include "config_variables.h" #undef MACRO_CONFIG_INT #undef MACRO_CONFIG_STR |