ddnet/src/engine/config.cpp

149 lines
3.4 KiB
C++
Raw Normal View History

2007-06-01 11:08:33 +00:00
#include <baselib/system.h>
2007-06-03 16:22:21 +00:00
#include <baselib/stream/file.h>
#include <baselib/stream/line.h>
2007-06-01 12:02:27 +00:00
2007-06-01 11:08:33 +00:00
#include <cstring>
#include <cstdio>
2007-06-03 16:22:21 +00:00
#include <cstdlib>
2007-06-01 11:08:33 +00:00
#include "config.h"
configuration config;
2007-06-03 16:22:21 +00:00
using namespace baselib;
2007-06-01 11:08:33 +00:00
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-07-22 13:19:18 +00:00
void strip_spaces(char **p)
{
char *&s = *p;
while (*s == ' ')
++s;
char *end = s + strlen(s);
while (end > s && *(end - 1) == ' ')
*--end = 0;
}
2007-06-01 12:15:38 +00:00
void config_set(const char *line)
{
2007-07-22 13:19:18 +00:00
const char *c = strchr(line, '=');
if (c)
2007-06-01 12:15:38 +00:00
{
2007-07-22 13:19:18 +00:00
char var[256];
char val[256];
strcpy(val, c+1);
mem_copy(var, line, c - line);
var[c - line] = 0;
char *var_str = var;
char *val_str = val;
strip_spaces(&var_str);
strip_spaces(&val_str);
2007-06-01 12:15:38 +00:00
#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)
{
2007-08-05 15:58:26 +00:00
char full_path[1024];
2007-08-05 15:56:32 +00:00
if (filename[0] == '~')
{
char *home = getenv("HOME");
if (home)
{
sprintf(full_path, "%s%s", home, filename+1);
filename = full_path;
}
}
2007-06-01 11:08:33 +00:00
dbg_msg("config/load", "loading %s", filename);
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
file_stream file;
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
if (file.open_r(filename))
2007-06-01 12:02:27 +00:00
{
2007-06-03 16:22:21 +00:00
char *line;
line_stream lstream(&file);
while ((line = lstream.get_line()))
config_set(line);
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)
{
2007-08-05 15:58:26 +00:00
char full_path[1024];
2007-08-05 15:56:32 +00:00
if (filename[0] == '~')
{
char *home = getenv("HOME");
if (home)
{
sprintf(full_path, "%s%s", home, filename+1);
filename = full_path;
}
}
2007-06-01 11:17:10 +00:00
dbg_msg("config/save", "saving config to %s", filename);
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
file_stream file;
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
if (file.open_w(filename))
{
#if defined(CONF_FAMILY_WINDOWS)
const char newline[] = "\r\n";
#else
const char newline[] = "\n";
#endif
const int newline_len = sizeof(newline);
2007-06-03 16:22:21 +00:00
#define MACRO_CONFIG_INT(name,def,min,max) { char str[256]; sprintf(str, "%s=%i", #name, config.name); file.write(str, strlen(str)); file.write(newline, newline_len); }
#define MACRO_CONFIG_STR(name,len,def) { file.write(#name, strlen(#name)); file.write("=", 1); file.write(config.name, strlen(config.name)); file.write(newline, newline_len); }
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
#include "config_variables.h"
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
2007-06-01 12:02:27 +00:00
2007-06-03 16:22:21 +00:00
file.close();
}
2007-08-05 15:56:32 +00:00
else
dbg_msg("config/save", "couldn't open %s for writing. :(", filename);
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