conf: use the tokenizer library

This commit is contained in:
Max Kellermann 2009-07-19 15:11:37 +02:00
parent 1745e68795
commit 16ff44ad30
2 changed files with 93 additions and 73 deletions

View File

@ -649,7 +649,7 @@ test_read_conf_CPPFLAGS = $(AM_CPPFLAGS) \
test_read_conf_LDADD = $(MPD_LIBS) \ test_read_conf_LDADD = $(MPD_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_conf_SOURCES = test/read_conf.c \ test_read_conf_SOURCES = test/read_conf.c \
src/conf.c src/buffer2array.c src/utils.c src/conf.c src/tokenizer.c src/utils.c
test_run_input_CPPFLAGS = $(AM_CPPFLAGS) \ test_run_input_CPPFLAGS = $(AM_CPPFLAGS) \
$(ARCHIVE_CFLAGS) \ $(ARCHIVE_CFLAGS) \
@ -659,7 +659,7 @@ test_run_input_LDADD = $(MPD_LIBS) \
$(INPUT_LIBS) \ $(INPUT_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_input_SOURCES = test/run_input.c \ test_run_input_SOURCES = test/run_input.c \
src/conf.c src/buffer2array.c src/utils.c \ src/conf.c src/tokenizer.c src/utils.c \
src/tag.c src/tag_pool.c src/tag_save.c \ src/tag.c src/tag_pool.c src/tag_save.c \
$(ARCHIVE_SRC) \ $(ARCHIVE_SRC) \
$(INPUT_SRC) $(INPUT_SRC)
@ -674,7 +674,7 @@ test_run_decoder_LDADD = $(MPD_LIBS) \
$(INPUT_LIBS) $(DECODER_LIBS) \ $(INPUT_LIBS) $(DECODER_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_decoder_SOURCES = test/run_decoder.c \ test_run_decoder_SOURCES = test/run_decoder.c \
src/conf.c src/buffer2array.c src/utils.c src/log.c \ src/conf.c src/tokenizer.c src/utils.c src/log.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
src/replay_gain.c \ src/replay_gain.c \
src/uri.c \ src/uri.c \
@ -693,7 +693,7 @@ test_read_tags_LDADD = $(MPD_LIBS) \
$(INPUT_LIBS) $(DECODER_LIBS) \ $(INPUT_LIBS) $(DECODER_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_tags_SOURCES = test/read_tags.c \ test_read_tags_SOURCES = test/read_tags.c \
src/conf.c src/buffer2array.c src/utils.c src/log.c \ src/conf.c src/tokenizer.c src/utils.c src/log.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
src/replay_gain.c \ src/replay_gain.c \
src/uri.c \ src/uri.c \
@ -709,7 +709,7 @@ test_run_filter_LDADD = $(MPD_LIBS) \
test_run_filter_SOURCES = test/run_filter.c \ test_run_filter_SOURCES = test/run_filter.c \
src/filter_plugin.c \ src/filter_plugin.c \
src/filter_registry.c \ src/filter_registry.c \
src/conf.c src/buffer2array.c src/utils.c \ src/conf.c src/tokenizer.c src/utils.c \
src/pcm_volume.c src/pcm_convert.c \ src/pcm_volume.c src/pcm_convert.c \
src/pcm_format.c src/pcm_channels.c src/pcm_dither.c \ src/pcm_format.c src/pcm_channels.c src/pcm_dither.c \
src/pcm_resample.c src/pcm_resample_fallback.c \ src/pcm_resample.c src/pcm_resample_fallback.c \
@ -723,7 +723,7 @@ endif
if ENABLE_ENCODER if ENABLE_ENCODER
noinst_PROGRAMS += test/run_encoder noinst_PROGRAMS += test/run_encoder
test_run_encoder_SOURCES = test/run_encoder.c \ test_run_encoder_SOURCES = test/run_encoder.c \
src/conf.c src/buffer2array.c \ src/conf.c src/tokenizer.c \
src/utils.c \ src/utils.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
src/audio_parser.c \ src/audio_parser.c \
@ -747,7 +747,7 @@ test_run_output_LDADD = $(MPD_LIBS) \
$(OUTPUT_LIBS) \ $(OUTPUT_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_output_SOURCES = test/run_output.c \ test_run_output_SOURCES = test/run_output.c \
src/conf.c src/buffer2array.c src/utils.c src/log.c \ src/conf.c src/tokenizer.c src/utils.c src/log.c \
src/audio_parser.c \ src/audio_parser.c \
src/timer.c \ src/timer.c \
src/tag.c src/tag_pool.c \ src/tag.c src/tag_pool.c \
@ -772,7 +772,7 @@ test_read_mixer_LDADD = $(MPD_LIBS) \
$(OUTPUT_LIBS) \ $(OUTPUT_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_mixer_SOURCES = test/read_mixer.c \ test_read_mixer_SOURCES = test/read_mixer.c \
src/conf.c src/buffer2array.c src/utils.c src/log.c \ src/conf.c src/tokenizer.c src/utils.c src/log.c \
src/mixer_control.c src/mixer_api.c \ src/mixer_control.c src/mixer_api.c \
src/filter_plugin.c \ src/filter_plugin.c \
src/filter/volume_filter_plugin.c \ src/filter/volume_filter_plugin.c \

View File

@ -19,7 +19,7 @@
#include "conf.h" #include "conf.h"
#include "utils.h" #include "utils.h"
#include "buffer2array.h" #include "tokenizer.h"
#include "path.h" #include "path.h"
#include <glib.h> #include <glib.h>
@ -36,10 +36,6 @@
#define MAX_STRING_SIZE MPD_PATH_MAX+80 #define MAX_STRING_SIZE MPD_PATH_MAX+80
#define CONF_COMMENT '#' #define CONF_COMMENT '#'
#define CONF_BLOCK_BEGIN "{"
#define CONF_BLOCK_END "}"
#define CONF_LINE_TOKEN_MAX 3
struct config_entry { struct config_entry {
const char *const name; const char *const name;
@ -250,60 +246,59 @@ config_read_block(FILE *fp, int *count, char *string)
{ {
struct config_param *ret = config_new_param(NULL, *count); struct config_param *ret = config_new_param(NULL, *count);
int i; while (true) {
int numberOfArgs; char *line;
int argsMinusComment; const char *name, *value;
GError *error = NULL;
while (fgets(string, MAX_STRING_SIZE, fp)) { line = fgets(string, MAX_STRING_SIZE, fp);
char *array[CONF_LINE_TOKEN_MAX] = { NULL }; if (line == NULL)
g_error("Expected '}' before end-of-file");
(*count)++; (*count)++;
line = g_strchug(line);
numberOfArgs = buffer2array(string, array, CONF_LINE_TOKEN_MAX); if (*line == 0 || *line == CONF_COMMENT)
for (i = 0; i < numberOfArgs; i++) {
if (array[i][0] == CONF_COMMENT)
break;
}
argsMinusComment = i;
if (0 == argsMinusComment) {
continue; continue;
}
if (1 == argsMinusComment && if (*line == '}') {
0 == strcmp(array[0], CONF_BLOCK_END)) { /* end of this block; return from the function
break; (and from this "while" loop) */
}
if (2 != argsMinusComment) { line = g_strchug(line + 1);
g_error("improperly formatted config file at line %i:" if (*line != 0 && *line != CONF_COMMENT)
" %s\n", *count, string); g_error("line %i: Unknown tokens after '}'",
} *count);
if (0 == strcmp(array[0], CONF_BLOCK_BEGIN) ||
0 == strcmp(array[1], CONF_BLOCK_BEGIN) ||
0 == strcmp(array[0], CONF_BLOCK_END) ||
0 == strcmp(array[1], CONF_BLOCK_END)) {
g_error("improperly formatted config file at line %i: %s "
"in block beginning at line %i\n",
*count, string, ret->line);
}
config_add_block_param(ret, array[0], array[1], *count);
}
return ret; return ret;
} }
/* parse name and value */
name = tokenizer_next_word(&line, &error);
if (name == NULL) {
assert(*line != 0);
g_error("line %i: %s", *count, error->message);
}
value = tokenizer_next_string(&line, &error);
if (value == NULL) {
if (*line == 0)
g_error("line %i: Value missing", *count);
else
g_error("line %i: %s", *count, error->message);
}
if (*line != 0 && *line != CONF_COMMENT)
g_error("line %i: Unknown tokens after value", *count);
config_add_block_param(ret, name, value, *count);
}
}
void config_read_file(const char *file) void config_read_file(const char *file)
{ {
FILE *fp; FILE *fp;
char string[MAX_STRING_SIZE + 1]; char string[MAX_STRING_SIZE + 1];
int i;
int numberOfArgs;
int argsMinusComment;
int count = 0; int count = 0;
struct config_entry *entry; struct config_entry *entry;
struct config_param *param; struct config_param *param;
@ -316,48 +311,73 @@ void config_read_file(const char *file)
} }
while (fgets(string, MAX_STRING_SIZE, fp)) { while (fgets(string, MAX_STRING_SIZE, fp)) {
char *array[CONF_LINE_TOKEN_MAX] = { NULL }; char *line;
const char *name, *value;
GError *error = NULL;
count++; count++;
numberOfArgs = buffer2array(string, array, CONF_LINE_TOKEN_MAX); line = g_strchug(string);
if (*line == 0 || *line == CONF_COMMENT)
for (i = 0; i < numberOfArgs; i++) {
if (array[i][0] == CONF_COMMENT)
break;
}
argsMinusComment = i;
if (0 == argsMinusComment) {
continue; continue;
/* the first token in each line is the name, followed
by either the value or '{' */
name = tokenizer_next_word(&line, &error);
if (name == NULL) {
assert(*line != 0);
g_error("line %i: %s", count, error->message);
} }
if (2 != argsMinusComment) { /* get the definition of that option, and check the
g_error("improperly formatted config file at line %i:" "repeatable" flag */
" %s\n", count, string);
}
entry = config_entry_get(array[0]); entry = config_entry_get(name);
if (entry == NULL) if (entry == NULL)
g_error("unrecognized parameter in config file at " g_error("unrecognized parameter in config file at "
"line %i: %s\n", count, string); "line %i: %s\n", count, name);
if (entry->params != NULL && !entry->repeatable) { if (entry->params != NULL && !entry->repeatable) {
param = entry->params->data; param = entry->params->data;
g_error("config parameter \"%s\" is first defined on " g_error("config parameter \"%s\" is first defined on "
"line %i and redefined on line %i\n", "line %i and redefined on line %i\n",
array[0], param->line, count); name, param->line, count);
} }
/* now parse the block or the value */
if (entry->block) { if (entry->block) {
if (0 != strcmp(array[1], CONF_BLOCK_BEGIN)) { /* it's a block, call config_read_block() */
g_error("improperly formatted config file at "
"line %i: %s\n", count, string); if (*line != '{')
} g_error("line %i: '{' expected", count);
line = g_strchug(line + 1);
if (*line != 0 && *line != CONF_COMMENT)
g_error("line %i: Unknown tokens after '{'",
count);
param = config_read_block(fp, &count, string); param = config_read_block(fp, &count, string);
} else } else {
param = config_new_param(array[1], count); /* a string value */
value = tokenizer_next_string(&line, &error);
if (value == NULL) {
if (*line == 0)
g_error("line %i: Value missing",
count);
else
g_error("line %i: %s", count,
error->message);
}
if (*line != 0 && *line != CONF_COMMENT)
g_error("line %i: Unknown tokens after value",
count);
param = config_new_param(value, count);
}
entry->params = g_slist_append(entry->params, param); entry->params = g_slist_append(entry->params, param);
} }