2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2007-06-01 12:02:27 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-06-01 11:08:33 +00:00
|
|
|
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "e_system.h"
|
|
|
|
#include "e_config.h"
|
2007-06-01 11:08:33 +00:00
|
|
|
|
|
|
|
|
2007-09-09 18:21:14 +00:00
|
|
|
/* buffered stream for reading lines, should perhaps be something smaller */
|
2007-08-22 07:52:33 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char buffer[4*1024];
|
|
|
|
unsigned buffer_pos;
|
|
|
|
unsigned buffer_size;
|
|
|
|
unsigned buffer_max_size;
|
|
|
|
IOHANDLE io;
|
|
|
|
} LINEREADER;
|
|
|
|
|
|
|
|
void linereader_init(LINEREADER *lr, IOHANDLE io)
|
|
|
|
{
|
|
|
|
lr->buffer_max_size = 4*1024;
|
|
|
|
lr->buffer_size = 0;
|
|
|
|
lr->buffer_pos = 0;
|
|
|
|
lr->io = io;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *linereader_get(LINEREADER *lr)
|
|
|
|
{
|
|
|
|
unsigned line_start = lr->buffer_pos;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(lr->buffer_pos >= lr->buffer_size)
|
|
|
|
{
|
2007-09-09 18:21:14 +00:00
|
|
|
/* fetch more */
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2007-09-09 18:21:14 +00:00
|
|
|
/* move the remaining part to the front */
|
2007-10-06 17:01:06 +00:00
|
|
|
unsigned read;
|
2007-08-22 07:52:33 +00:00
|
|
|
unsigned left = lr->buffer_size - line_start;
|
2007-10-06 17:01:06 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
if(line_start > lr->buffer_size)
|
|
|
|
left = 0;
|
|
|
|
if(left)
|
|
|
|
mem_move(lr->buffer, &lr->buffer[line_start], left);
|
|
|
|
lr->buffer_pos = left;
|
|
|
|
|
2007-09-09 18:21:14 +00:00
|
|
|
/* fill the buffer */
|
2007-10-06 17:01:06 +00:00
|
|
|
read = io_read(lr->io, &lr->buffer[lr->buffer_pos], lr->buffer_max_size-lr->buffer_pos);
|
2007-08-22 07:52:33 +00:00
|
|
|
lr->buffer_size = left + read;
|
|
|
|
line_start = 0;
|
|
|
|
|
|
|
|
if(!read)
|
|
|
|
{
|
|
|
|
if(left)
|
|
|
|
{
|
2007-09-09 18:21:14 +00:00
|
|
|
lr->buffer[left] = 0; /* return the last line */
|
2007-08-22 07:52:33 +00:00
|
|
|
lr->buffer_pos = left;
|
|
|
|
lr->buffer_size = left;
|
|
|
|
return lr->buffer;
|
|
|
|
}
|
|
|
|
else
|
2007-09-09 18:21:14 +00:00
|
|
|
return 0x0; /* we are done! */
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(lr->buffer[lr->buffer_pos] == '\n' || lr->buffer[lr->buffer_pos] == '\r')
|
|
|
|
{
|
2007-09-09 18:21:14 +00:00
|
|
|
/* line found */
|
2007-08-22 07:52:33 +00:00
|
|
|
lr->buffer[lr->buffer_pos] = 0;
|
|
|
|
lr->buffer_pos++;
|
|
|
|
return &lr->buffer[line_start];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lr->buffer_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIGURATION config;
|
2007-06-03 16:22:21 +00:00
|
|
|
|
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-12-15 10:24:49 +00:00
|
|
|
#include "e_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)
|
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
char *s = *p;
|
2007-10-06 17:01:06 +00:00
|
|
|
char *end;
|
2007-07-22 13:19:18 +00:00
|
|
|
|
|
|
|
while (*s == ' ')
|
|
|
|
++s;
|
|
|
|
|
2007-10-06 17:01:06 +00:00
|
|
|
end = s + strlen(s);
|
2007-07-22 13:19:18 +00:00
|
|
|
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];
|
2007-10-06 17:01:06 +00:00
|
|
|
char *var_str = var;
|
|
|
|
char *val_str = val;
|
2007-07-22 13:19:18 +00:00
|
|
|
|
|
|
|
strcpy(val, c+1);
|
|
|
|
|
|
|
|
mem_copy(var, line, c - line);
|
|
|
|
var[c - line] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
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); } }
|
|
|
|
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "e_config_variables.h"
|
2007-06-01 12:15:38 +00:00
|
|
|
|
|
|
|
#undef MACRO_CONFIG_INT
|
|
|
|
#undef MACRO_CONFIG_STR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 11:08:33 +00:00
|
|
|
void config_load(const char *filename)
|
|
|
|
{
|
2007-10-06 17:01:06 +00:00
|
|
|
IOHANDLE file;
|
2007-06-01 11:08:33 +00:00
|
|
|
dbg_msg("config/load", "loading %s", filename);
|
2007-10-06 17:01:06 +00:00
|
|
|
file = io_open(filename, IOFLAG_READ);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
if(file)
|
2007-06-01 12:02:27 +00:00
|
|
|
{
|
2007-06-03 16:22:21 +00:00
|
|
|
char *line;
|
2007-08-22 07:52:33 +00:00
|
|
|
LINEREADER lr;
|
|
|
|
linereader_init(&lr, file);
|
2007-06-03 16:22:21 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
while ((line = linereader_get(&lr)))
|
2007-06-03 16:22:21 +00:00
|
|
|
config_set(line);
|
2007-06-01 12:02:27 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
io_close(file);
|
2007-06-01 12:02:27 +00:00
|
|
|
}
|
2007-06-01 11:08:33 +00:00
|
|
|
}
|
|
|
|
|
2007-06-01 11:17:10 +00:00
|
|
|
void config_save(const char *filename)
|
|
|
|
{
|
2007-10-06 17:01:06 +00:00
|
|
|
IOHANDLE file;
|
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-10-06 17:01:06 +00:00
|
|
|
file = io_open(filename, IOFLAG_WRITE);
|
2007-06-01 12:02:27 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
if(file)
|
2007-06-03 16:22:21 +00:00
|
|
|
{
|
2007-08-10 11:22:47 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
const char newline[] = "\r\n";
|
|
|
|
#else
|
|
|
|
const char newline[] = "\n";
|
|
|
|
#endif
|
2007-08-14 18:37:16 +00:00
|
|
|
const int newline_len = sizeof(newline)-1;
|
2007-06-03 16:22:21 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#define MACRO_CONFIG_INT(name,def,min,max) { char str[256]; sprintf(str, "%s=%i%s", #name, config.name, newline); io_write(file, str, strlen(str)); }
|
|
|
|
#define MACRO_CONFIG_STR(name,len,def) { io_write(file, #name, strlen(#name)); io_write(file, "=", 1); io_write(file, config.name, strlen(config.name)); io_write(file, newline, newline_len); }
|
2007-06-01 12:02:27 +00:00
|
|
|
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "e_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-08-22 07:52:33 +00:00
|
|
|
io_close(file);
|
2007-06-03 16:22:21 +00:00
|
|
|
}
|
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-08-22 07:52:33 +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; }
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "e_config_variables.h"
|
2007-06-01 12:24:13 +00:00
|
|
|
#undef MACRO_CONFIG_INT
|
|
|
|
#undef MACRO_CONFIG_STR
|
|
|
|
|
2007-11-18 12:03:59 +00:00
|
|
|
#define MACRO_CONFIG_INT(name,def,min,max) void config_set_ ## name (CONFIGURATION *c, int val) { if(min != max) { if (val < min) val = min; if (max != 0 && val > max) val = max; } c->name = val; }
|
2007-08-22 07:52:33 +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-12-15 10:24:49 +00:00
|
|
|
#include "e_config_variables.h"
|
2007-06-01 11:08:33 +00:00
|
|
|
#undef MACRO_CONFIG_INT
|
|
|
|
#undef MACRO_CONFIG_STR
|