config/File: move code to ReadConfigParam()

This commit is contained in:
Max Kellermann 2015-01-21 23:23:56 +01:00
parent 6cdb2a4896
commit 84e74173de
1 changed files with 83 additions and 74 deletions

View File

@ -139,50 +139,17 @@ Append(config_param *&head, config_param *p)
} }
static bool static bool
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error) ReadConfigParam(ConfigData &config_data, BufferedReader &reader,
const char *name, ConfigOption o,
Tokenizer &tokenizer,
Error &error)
{ {
struct config_param *param;
while (true) {
char *line = reader.ReadLine();
if (line == nullptr)
return true;
const char *name, *value;
line = StripLeft(line);
if (*line == 0 || *line == CONF_COMMENT)
continue;
/* the first token in each line is the name, followed
by either the value or '{' */
Tokenizer tokenizer(line);
name = tokenizer.NextWord(error);
if (name == nullptr) {
assert(!tokenizer.IsEnd());
error.FormatPrefix("line %u: ", reader.GetLineNumber());
return false;
}
/* get the definition of that option, and check the
"repeatable" flag */
const ConfigOption o = ParseConfigOptionName(name);
if (o == ConfigOption::MAX) {
error.Format(config_file_domain,
"unrecognized parameter in config file at "
"line %u: %s\n",
reader.GetLineNumber(), name);
return false;
}
const unsigned i = unsigned(o); const unsigned i = unsigned(o);
const ConfigTemplate &option = config_templates[i]; const ConfigTemplate &option = config_templates[i];
config_param *&head = config_data.params[i]; config_param *&head = config_data.params[i];
if (head != nullptr && !option.repeatable) { if (head != nullptr && !option.repeatable) {
param = head; struct config_param *param = head;
error.Format(config_file_domain, error.Format(config_file_domain,
"config parameter \"%s\" is first defined " "config parameter \"%s\" is first defined "
"on line %d and redefined on line %u\n", "on line %d and redefined on line %u\n",
@ -193,6 +160,7 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
/* now parse the block or the value */ /* now parse the block or the value */
struct config_param *param;
if (option.block) { if (option.block) {
/* it's a block, call config_read_block() */ /* it's a block, call config_read_block() */
@ -203,7 +171,7 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
return false; return false;
} }
line = StripLeft(tokenizer.Rest() + 1); char *line = StripLeft(tokenizer.Rest() + 1);
if (*line != 0 && *line != CONF_COMMENT) { if (*line != 0 && *line != CONF_COMMENT) {
error.Format(config_file_domain, error.Format(config_file_domain,
"line %u: Unknown tokens after '{'", "line %u: Unknown tokens after '{'",
@ -218,7 +186,7 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
} else { } else {
/* a string value */ /* a string value */
value = tokenizer.NextString(error); const char *value = tokenizer.NextString(error);
if (value == nullptr) { if (value == nullptr) {
if (tokenizer.IsEnd()) if (tokenizer.IsEnd())
error.Format(config_file_domain, error.Format(config_file_domain,
@ -244,6 +212,47 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
} }
Append(head, param); Append(head, param);
return true;
}
static bool
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
{
while (true) {
char *line = reader.ReadLine();
if (line == nullptr)
return true;
line = StripLeft(line);
if (*line == 0 || *line == CONF_COMMENT)
continue;
/* the first token in each line is the name, followed
by either the value or '{' */
Tokenizer tokenizer(line);
const char *name = tokenizer.NextWord(error);
if (name == nullptr) {
assert(!tokenizer.IsEnd());
error.FormatPrefix("line %u: ", reader.GetLineNumber());
return false;
}
/* get the definition of that option, and check the
"repeatable" flag */
const ConfigOption o = ParseConfigOptionName(name);
if (o != ConfigOption::MAX) {
if (!ReadConfigParam(config_data, reader, name, o,
tokenizer, error))
return false;
} else {
error.Format(config_file_domain,
"unrecognized parameter in config file at "
"line %u: %s\n",
reader.GetLineNumber(), name);
return false;
}
} }
} }