From 8df98932b111bf1cdd219d47f3e418761d38d664 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 18 Jul 2018 10:17:45 +0200 Subject: [PATCH] config/Data: add methods AddParam(), AddBlock() --- src/config/Data.cxx | 40 +++++++++++++++++++++++++++ src/config/Data.hxx | 7 +++++ src/config/File.cxx | 67 ++++++++++++--------------------------------- 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/config/Data.cxx b/src/config/Data.cxx index 2c1967f07..cd43e7cff 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -42,6 +42,26 @@ ConfigData::Clear() } } +gcc_nonnull_all +static void +Append(ConfigParam *&head, ConfigParam *p) +{ + assert(p->next == nullptr); + + auto **i = &head; + while (*i != nullptr) + i = &(*i)->next; + + *i = p; +} + +void +ConfigData::AddParam(ConfigOption option, + std::unique_ptr param) noexcept +{ + Append(params[size_t(option)], param.release()); +} + const char * ConfigData::GetString(ConfigOption option, const char *default_value) const noexcept @@ -123,6 +143,26 @@ ConfigData::GetBool(ConfigOption option, bool default_value) const return value; } +gcc_nonnull_all +static void +Append(ConfigBlock *&head, ConfigBlock *p) +{ + assert(p->next == nullptr); + + auto **i = &head; + while (*i != nullptr) + i = &(*i)->next; + + *i = p; +} + +void +ConfigData::AddBlock(ConfigBlockOption option, + std::unique_ptr block) noexcept +{ + Append(blocks[size_t(option)], block.release()); +} + const ConfigBlock * ConfigData::FindBlock(ConfigBlockOption option, const char *key, const char *value) const diff --git a/src/config/Data.hxx b/src/config/Data.hxx index 7d21a8767..65ec0af84 100644 --- a/src/config/Data.hxx +++ b/src/config/Data.hxx @@ -24,6 +24,7 @@ #include #include +#include struct ConfigParam; struct ConfigBlock; @@ -35,6 +36,9 @@ struct ConfigData { void Clear(); + void AddParam(ConfigOption option, + std::unique_ptr param) noexcept; + gcc_pure const ConfigParam *GetParam(ConfigOption option) const noexcept { return params[size_t(option)]; @@ -77,6 +81,9 @@ struct ConfigData { bool GetBool(ConfigOption option, bool default_value) const; + void AddBlock(ConfigBlockOption option, + std::unique_ptr block) noexcept; + gcc_pure const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept { return blocks[size_t(option)]; diff --git a/src/config/File.cxx b/src/config/File.cxx index db7d94fb1..7610cecf9 100644 --- a/src/config/File.cxx +++ b/src/config/File.cxx @@ -74,7 +74,7 @@ config_read_name_value(ConfigBlock &block, char *input, unsigned line) block.AddBlockParam(name, std::move(value), line); } -static ConfigBlock * +static std::unique_ptr config_read_block(BufferedReader &reader) { std::unique_ptr block(new ConfigBlock(reader.GetLineNumber())); @@ -96,7 +96,7 @@ config_read_block(BufferedReader &reader) if (*line != 0 && *line != CONF_COMMENT) throw std::runtime_error("Unknown tokens after '}'"); - return block.release(); + return block; } /* parse name and value */ @@ -106,19 +106,6 @@ config_read_block(BufferedReader &reader) } } -gcc_nonnull_all -static void -Append(ConfigBlock *&head, ConfigBlock *p) -{ - assert(p->next == nullptr); - - auto **i = &head; - while (*i != nullptr) - i = &(*i)->next; - - *i = p; -} - static void ReadConfigBlock(ConfigData &config_data, BufferedReader &reader, const char *name, ConfigBlockOption o, @@ -126,15 +113,13 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader, { const unsigned i = unsigned(o); const ConfigTemplate &option = config_block_templates[i]; - ConfigBlock *&head = config_data.blocks[i]; - if (head != nullptr && !option.repeatable) { - ConfigBlock *block = head; - throw FormatRuntimeError("config parameter \"%s\" is first defined " - "on line %d and redefined on line %u\n", - name, block->line, - reader.GetLineNumber()); - } + if (!option.repeatable) + if (const auto *block = config_data.GetBlock(o)) + throw FormatRuntimeError("config parameter \"%s\" is first defined " + "on line %d and redefined on line %u\n", + name, block->line, + reader.GetLineNumber()); /* now parse the block or the value */ @@ -145,22 +130,7 @@ ReadConfigBlock(ConfigData &config_data, BufferedReader &reader, if (*line != 0 && *line != CONF_COMMENT) throw std::runtime_error("Unknown tokens after '{'"); - auto *param = config_read_block(reader); - assert(param != nullptr); - Append(head, param); -} - -gcc_nonnull_all -static void -Append(ConfigParam *&head, ConfigParam *p) -{ - assert(p->next == nullptr); - - auto **i = &head; - while (*i != nullptr) - i = &(*i)->next; - - *i = p; + config_data.AddBlock(o, config_read_block(reader)); } static void @@ -170,21 +140,18 @@ ReadConfigParam(ConfigData &config_data, BufferedReader &reader, { const unsigned i = unsigned(o); const ConfigTemplate &option = config_param_templates[i]; - auto *&head = config_data.params[i]; - if (head != nullptr && !option.repeatable) { - auto *param = head; - throw FormatRuntimeError("config parameter \"%s\" is first defined " - "on line %d and redefined on line %u\n", - name, param->line, - reader.GetLineNumber()); - } + if (!option.repeatable) + if (const auto *param = config_data.GetParam(o)) + throw FormatRuntimeError("config parameter \"%s\" is first defined " + "on line %d and redefined on line %u\n", + name, param->line, + reader.GetLineNumber()); /* now parse the block or the value */ - auto *param = new ConfigParam(ExpectValueAndEnd(tokenizer), - reader.GetLineNumber()); - Append(head, param); + config_data.AddParam(o, std::make_unique(ExpectValueAndEnd(tokenizer), + reader.GetLineNumber())); } static void