conf: use GLib instead of utils.h/log.h

This commit is contained in:
Max Kellermann 2008-12-28 19:54:49 +01:00
parent 859aac7242
commit 1914e11466

View File

@ -17,15 +17,14 @@
*/ */
#include "conf.h" #include "conf.h"
#include "log.h"
#include "utils.h" #include "utils.h"
#include "buffer2array.h" #include "buffer2array.h"
#include "list.h" #include "list.h"
#include "path.h" #include "path.h"
#include "os_compat.h" #include "os_compat.h"
#include <glib.h>
#define MAX_STRING_SIZE MPD_PATH_MAX+80 #define MAX_STRING_SIZE MPD_PATH_MAX+80
#define CONF_COMMENT '#' #define CONF_COMMENT '#'
@ -62,12 +61,12 @@ static int get_bool(const char *value)
static ConfigParam *newConfigParam(char *value, int line) static ConfigParam *newConfigParam(char *value, int line)
{ {
ConfigParam *ret = xmalloc(sizeof(ConfigParam)); ConfigParam *ret = g_new(ConfigParam, 1);
if (!value) if (!value)
ret->value = NULL; ret->value = NULL;
else else
ret->value = xstrdup(value); ret->value = g_strdup(value);
ret->line = line; ret->line = line;
@ -101,7 +100,7 @@ static void freeConfigParam(ConfigParam * param)
static ConfigEntry *newConfigEntry(int repeatable, int block) static ConfigEntry *newConfigEntry(int repeatable, int block)
{ {
ConfigEntry *ret = xmalloc(sizeof(ConfigEntry)); ConfigEntry *ret = g_new(ConfigEntry, 1);
ret->mask = 0; ret->mask = 0;
ret->configParamList = ret->configParamList =
@ -126,7 +125,7 @@ static void registerConfigParam(const char *name, int repeatable, int block)
ConfigEntry *entry; ConfigEntry *entry;
if (findInList(configEntriesList, name, NULL)) if (findInList(configEntriesList, name, NULL))
FATAL("config parameter \"%s\" already registered\n", name); g_error("config parameter \"%s\" already registered\n", name);
entry = newConfigEntry(repeatable, block); entry = newConfigEntry(repeatable, block);
@ -192,13 +191,13 @@ static void addBlockParam(ConfigParam * param, char *name, char *value,
{ {
param->numberOfBlockParams++; param->numberOfBlockParams++;
param->blockParams = xrealloc(param->blockParams, param->blockParams = g_realloc(param->blockParams,
param->numberOfBlockParams * param->numberOfBlockParams *
sizeof(BlockParam)); sizeof(BlockParam));
param->blockParams[param->numberOfBlockParams - 1].name = xstrdup(name); param->blockParams[param->numberOfBlockParams - 1].name = g_strdup(name);
param->blockParams[param->numberOfBlockParams - 1].value = param->blockParams[param->numberOfBlockParams - 1].value =
xstrdup(value); g_strdup(value);
param->blockParams[param->numberOfBlockParams - 1].line = line; param->blockParams[param->numberOfBlockParams - 1].line = line;
} }
@ -234,17 +233,17 @@ static ConfigParam *readConfigBlock(FILE * fp, int *count, char *string)
} }
if (2 != argsMinusComment) { if (2 != argsMinusComment) {
FATAL("improperly formatted config file at line %i:" g_error("improperly formatted config file at line %i:"
" %s\n", *count, string); " %s\n", *count, string);
} }
if (0 == strcmp(array[0], CONF_BLOCK_BEGIN) || if (0 == strcmp(array[0], CONF_BLOCK_BEGIN) ||
0 == strcmp(array[1], CONF_BLOCK_BEGIN) || 0 == strcmp(array[1], CONF_BLOCK_BEGIN) ||
0 == strcmp(array[0], CONF_BLOCK_END) || 0 == strcmp(array[0], CONF_BLOCK_END) ||
0 == strcmp(array[1], CONF_BLOCK_END)) { 0 == strcmp(array[1], CONF_BLOCK_END)) {
FATAL("improperly formatted config file at line %i: %s\n" g_error("improperly formatted config file at line %i: %s "
"in block beginning at line %i\n", "in block beginning at line %i\n",
*count, string, ret->line); *count, string, ret->line);
} }
addBlockParam(ret, array[0], array[1], *count); addBlockParam(ret, array[0], array[1], *count);
@ -266,8 +265,8 @@ void readConf(const char *file)
ConfigParam *param; ConfigParam *param;
if (!(fp = fopen(file, "r"))) { if (!(fp = fopen(file, "r"))) {
FATAL("problems opening file %s for reading: %s\n", file, g_error("problems opening file %s for reading: %s\n",
strerror(errno)); file, strerror(errno));
} }
while (fgets(string, MAX_STRING_SIZE, fp)) { while (fgets(string, MAX_STRING_SIZE, fp)) {
@ -289,13 +288,13 @@ void readConf(const char *file)
} }
if (2 != argsMinusComment) { if (2 != argsMinusComment) {
FATAL("improperly formatted config file at line %i:" g_error("improperly formatted config file at line %i:"
" %s\n", count, string); " %s\n", count, string);
} }
if (!findInList(configEntriesList, array[0], &voidPtr)) { if (!findInList(configEntriesList, array[0], &voidPtr)) {
FATAL("unrecognized parameter in config file at line " g_error("unrecognized parameter in config file at "
"%i: %s\n", count, string); "line %i: %s\n", count, string);
} }
entry = (ConfigEntry *) voidPtr; entry = (ConfigEntry *) voidPtr;
@ -303,15 +302,15 @@ void readConf(const char *file)
if (!(entry->mask & CONF_REPEATABLE_MASK) && if (!(entry->mask & CONF_REPEATABLE_MASK) &&
entry->configParamList->numberOfNodes) { entry->configParamList->numberOfNodes) {
param = entry->configParamList->firstNode->data; param = entry->configParamList->firstNode->data;
FATAL("config parameter \"%s\" is first defined on line " g_error("config parameter \"%s\" is first defined on "
"%i and redefined on line %i\n", array[0], "line %i and redefined on line %i\n",
param->line, count); array[0], param->line, count);
} }
if (entry->mask & CONF_BLOCK_MASK) { if (entry->mask & CONF_BLOCK_MASK) {
if (0 != strcmp(array[1], CONF_BLOCK_BEGIN)) { if (0 != strcmp(array[1], CONF_BLOCK_BEGIN)) {
FATAL("improperly formatted config file at " g_error("improperly formatted config file at "
"line %i: %s\n", count, string); "line %i: %s\n", count, string);
} }
param = readConfigBlock(fp, &count, string); param = readConfigBlock(fp, &count, string);
} else } else
@ -371,9 +370,9 @@ BlockParam *getBlockParam(ConfigParam * param, const char *name)
for (i = 0; i < param->numberOfBlockParams; i++) { for (i = 0; i < param->numberOfBlockParams; i++) {
if (0 == strcmp(name, param->blockParams[i].name)) { if (0 == strcmp(name, param->blockParams[i].name)) {
if (ret) { if (ret) {
ERROR("\"%s\" first defined on line %i, and " g_warning("\"%s\" first defined on line %i, and "
"redefined on line %i\n", name, "redefined on line %i\n", name,
ret->line, param->blockParams[i].line); ret->line, param->blockParams[i].line);
} }
ret = param->blockParams + i; ret = param->blockParams + i;
} }
@ -388,14 +387,15 @@ ConfigParam *parseConfigFilePath(const char *name, int force)
char *path; char *path;
if (!param && force) if (!param && force)
FATAL("config parameter \"%s\" not found\n", name); g_error("config parameter \"%s\" not found\n", name);
if (!param) if (!param)
return NULL; return NULL;
path = parsePath(param->value); path = parsePath(param->value);
if (!path) if (!path)
FATAL("error parsing \"%s\" at line %i\n", name, param->line); g_error("error parsing \"%s\" at line %i\n",
name, param->line);
free(param->value); free(param->value);
param->value = path; param->value = path;
@ -413,9 +413,9 @@ int getBoolConfigParam(const char *name, int force)
ret = get_bool(param->value); ret = get_bool(param->value);
if (force && ret == CONF_BOOL_INVALID) if (force && ret == CONF_BOOL_INVALID)
FATAL("%s is not a boolean value (yes, true, 1) or " g_error("%s is not a boolean value (yes, true, 1) or "
"(no, false, 0) on line %i\n", "(no, false, 0) on line %i\n",
name, param->line); name, param->line);
return ret; return ret;
} }
@ -439,8 +439,9 @@ int getBoolBlockParam(ConfigParam *param, const char *name, int force)
ret = get_bool(bp->value); ret = get_bool(bp->value);
if (force && ret == CONF_BOOL_INVALID) if (force && ret == CONF_BOOL_INVALID)
FATAL("%s is not a boolean value (yes, true, 1) or " g_error("%s is not a boolean value (yes, true, 1) or "
"(no, false, 0) on line %i\n", bp->value, bp->line); "(no, false, 0) on line %i\n",
bp->value, bp->line);
return ret; return ret;
} }