ddnet/src/engine/config.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

2007-06-01 11:08:33 +00:00
#include <baselib/system.h>
2007-06-01 12:02:27 +00:00
#include <fstream>
#include <iostream>
2007-06-01 11:08:33 +00:00
#include <cstring>
#include <cstdio>
#include "config.h"
2007-06-01 12:02:27 +00:00
using namespace std;
2007-06-01 11:08:33 +00:00
configuration config;
void config_reset()
{
#define MACRO_CONFIG_INT(name,def,min,max) config.name = def;
#define MACRO_CONFIG_STR(name,len,def) strncpy(config.name, def, len);
2007-06-01 11:17:10 +00:00
#include "config_variables.h"
2007-06-01 11:08:33 +00:00
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
}
2007-06-01 12:15:38 +00:00
void config_set(const char *line)
{
char var_str[128];
char *val_str = strchr(line, '=');
if (val_str)
{
memcpy(var_str, line, val_str - line);
var_str[val_str - line] = 0;
++val_str;
#define MACRO_CONFIG_INT(name,def,min,max) { if (strcmp(#name, var_str) == 0) config_set_ ## name (&config, atoi(val_str)); }
#define MACRO_CONFIG_STR(name,len,def) { if (strcmp(#name, var_str) == 0) { config_set_ ## name (&config, val_str); } }
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
}
}
2007-06-01 11:08:33 +00:00
void config_load(const char *filename)
{
dbg_msg("config/load", "loading %s", filename);
2007-06-01 12:02:27 +00:00
ifstream file(filename);
string line;
if (file)
{
while (getline(file, line))
2007-06-01 12:15:38 +00:00
config_set(line.c_str());
2007-06-01 12:02:27 +00:00
file.close();
}
2007-06-01 11:08:33 +00:00
}
2007-06-01 11:17:10 +00:00
void config_save(const char *filename)
{
dbg_msg("config/save", "saving config to %s", filename);
2007-06-01 12:02:27 +00:00
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();
2007-06-01 11:17:10 +00:00
}
2007-06-01 12:24:13 +00:00
#define MACRO_CONFIG_INT(name,def,min,max) int config_get_ ## name (configuration *c) { return c->name; }
#define MACRO_CONFIG_STR(name,len,def) const char *config_get_ ## name (configuration *c) { return c->name; }
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
2007-06-01 11:17:10 +00:00
#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; }
2007-06-01 12:04:36 +00:00
#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; }
2007-06-01 11:17:10 +00:00
#include "config_variables.h"
2007-06-01 11:08:33 +00:00
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR