From 15c36baefddbafc0e46f9b7e2c0c1254cc91d9f1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 17 Jul 2018 21:14:53 +0200 Subject: config/Data: add method FindBlock() --- src/config/Data.cxx | 20 ++++++++++++++++++++ src/config/Data.hxx | 4 ++++ src/config/Global.cxx | 13 +------------ 3 files changed, 25 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/config/Data.cxx b/src/config/Data.cxx index 2eee5e23e..f4535c599 100644 --- a/src/config/Data.cxx +++ b/src/config/Data.cxx @@ -21,6 +21,8 @@ #include "Data.hxx" #include "Param.hxx" #include "Block.hxx" +#include "system/FatalError.hxx" +#include "util/StringAPI.hxx" void ConfigData::Clear() @@ -35,3 +37,21 @@ ConfigData::Clear() i = nullptr; } } + +const ConfigBlock * +ConfigData::FindBlock(ConfigBlockOption option, + const char *key, const char *value) const noexcept +{ + for (const auto *block = GetBlock(option); + block != nullptr; block = block->next) { + const char *value2 = block->GetBlockValue(key); + if (value2 == nullptr) + FormatFatalError("block without '%s' in line %d", + key, block->line); + + if (StringIsEqual(value2, value)) + return block; + } + + return nullptr; +} diff --git a/src/config/Data.hxx b/src/config/Data.hxx index 11969231c..940440d7d 100644 --- a/src/config/Data.hxx +++ b/src/config/Data.hxx @@ -42,6 +42,10 @@ struct ConfigData { const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept { return blocks[size_t(option)]; } + + gcc_pure + const ConfigBlock *FindBlock(ConfigBlockOption option, + const char *key, const char *value) const noexcept; }; #endif diff --git a/src/config/Global.cxx b/src/config/Global.cxx index 547e5fa27..78ea175cf 100644 --- a/src/config/Global.cxx +++ b/src/config/Global.cxx @@ -89,18 +89,7 @@ config_get_block(ConfigBlockOption option) noexcept const ConfigBlock * config_find_block(ConfigBlockOption option, const char *key, const char *value) { - for (const auto *block = config_get_block(option); - block != nullptr; block = block->next) { - const char *value2 = block->GetBlockValue(key); - if (value2 == nullptr) - FormatFatalError("block without '%s' name in line %d", - key, block->line); - - if (strcmp(value2, value) == 0) - return block; - } - - return nullptr; + return config_data.FindBlock(option, key, value); } const char * -- cgit v1.2.3