config/Data: add method FindBlock()

This commit is contained in:
Max Kellermann 2018-07-17 21:14:53 +02:00
parent 33deb84aa1
commit 15c36baefd
3 changed files with 25 additions and 12 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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 *