diff options
author | Max Kellermann <max@musicpd.org> | 2018-07-17 20:14:43 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2018-07-17 20:28:50 +0200 |
commit | a4eeaff63fcf6612b702ac093e43d8fddd5f57f0 (patch) | |
tree | f4cf53c365fe1893070b16999424be9ce1d87897 | |
parent | 7807ddae57b31d0e22722d8a562784d543a61b8e (diff) |
config/File: move duplicate code to ExpectValueAndEnd()
-rw-r--r-- | src/config/File.cxx | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/config/File.cxx b/src/config/File.cxx index 7320d5726..db7d94fb1 100644 --- a/src/config/File.cxx +++ b/src/config/File.cxx @@ -40,6 +40,22 @@ static constexpr char CONF_COMMENT = '#'; static constexpr Domain config_file_domain("config_file"); +/** + * Read a string value as the last token of a line. Throws on error. + */ +static auto +ExpectValueAndEnd(Tokenizer &tokenizer) +{ + auto value = tokenizer.NextString(); + if (!value) + throw std::runtime_error("Value missing"); + + if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) + throw std::runtime_error("Unknown tokens after value"); + + return value; +} + static void config_read_name_value(ConfigBlock &block, char *input, unsigned line) { @@ -48,19 +64,14 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line) const char *name = tokenizer.NextWord(); assert(name != nullptr); - const char *value = tokenizer.NextString(); - if (value == nullptr) - throw std::runtime_error("Value missing"); - - if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) - throw std::runtime_error("Unknown tokens after value"); + auto value = ExpectValueAndEnd(tokenizer); const BlockParam *bp = block.GetBlockParam(name); if (bp != nullptr) throw FormatRuntimeError("\"%s\" is duplicate, first defined on line %i", name, bp->line); - block.AddBlockParam(name, value, line); + block.AddBlockParam(name, std::move(value), line); } static ConfigBlock * @@ -171,14 +182,8 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader, /* now parse the block or the value */ - const char *value = tokenizer.NextString(); - if (value == nullptr) - throw std::runtime_error("Value missing"); - - if (!tokenizer.IsEnd() && tokenizer.CurrentChar() != CONF_COMMENT) - throw std::runtime_error("Unknown tokens after value"); - - auto *param = new ConfigParam(value, reader.GetLineNumber()); + auto *param = new ConfigParam(ExpectValueAndEnd(tokenizer), + reader.GetLineNumber()); Append(head, param); } |