From a4eeaff63fcf6612b702ac093e43d8fddd5f57f0 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 17 Jul 2018 20:14:43 +0200 Subject: config/File: move duplicate code to ExpectValueAndEnd() --- src/config/File.cxx | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'src/config') 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); } -- cgit v1.2.3