diff options
author | Max Kellermann <max@duempel.org> | 2015-01-21 22:13:44 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-01-21 23:56:33 +0100 |
commit | 4fa5538e2bed36903b403e1aaee2462d22b456dc (patch) | |
tree | 292b66e10e6b97e2363fde34a81c027a67d3a9fe /src/config/Block.cxx | |
parent | 84e74173de85a3897cfe67150297987f8c8bf52e (diff) | |
download | mpd-4fa5538e2bed36903b403e1aaee2462d22b456dc.tar.gz mpd-4fa5538e2bed36903b403e1aaee2462d22b456dc.tar.xz mpd-4fa5538e2bed36903b403e1aaee2462d22b456dc.zip |
config/Param: split block-specific attributes to new struct ConfigBlock
The old struct config_param remains only for top-level string options.
Diffstat (limited to '')
-rw-r--r-- | src/config/Block.cxx | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/config/Block.cxx b/src/config/Block.cxx index ba26c560f..a74903b10 100644 --- a/src/config/Block.cxx +++ b/src/config/Block.cxx @@ -20,8 +20,12 @@ #include "config.h" #include "Block.hxx" #include "ConfigParser.hxx" +#include "ConfigPath.hxx" #include "system/FatalError.hxx" +#include "fs/AllocatedPath.hxx" +#include "util/Error.hxx" +#include <assert.h> #include <stdlib.h> int @@ -57,3 +61,96 @@ BlockParam::GetBoolValue() const return value2; } + +ConfigBlock::~ConfigBlock() +{ + delete next; +} + +const BlockParam * +ConfigBlock::GetBlockParam(const char *name) const +{ + for (const auto &i : block_params) { + if (i.name == name) { + i.used = true; + return &i; + } + } + + return nullptr; +} + +const char * +ConfigBlock::GetBlockValue(const char *name, const char *default_value) const +{ + const BlockParam *bp = GetBlockParam(name); + if (bp == nullptr) + return default_value; + + return bp->value.c_str(); +} + +AllocatedPath +ConfigBlock::GetBlockPath(const char *name, const char *default_value, + Error &error) const +{ + assert(!error.IsDefined()); + + int line2 = line; + const char *s; + + const BlockParam *bp = GetBlockParam(name); + if (bp != nullptr) { + line2 = bp->line; + s = bp->value.c_str(); + } else { + if (default_value == nullptr) + return AllocatedPath::Null(); + + s = default_value; + } + + AllocatedPath path = ParsePath(s, error); + if (gcc_unlikely(path.IsNull())) + error.FormatPrefix("Invalid path in \"%s\" at line %i: ", + name, line2); + + return path; +} + +AllocatedPath +ConfigBlock::GetBlockPath(const char *name, Error &error) const +{ + return GetBlockPath(name, nullptr, error); +} + +int +ConfigBlock::GetBlockValue(const char *name, int default_value) const +{ + const BlockParam *bp = GetBlockParam(name); + if (bp == nullptr) + return default_value; + + return bp->GetIntValue(); +} + +unsigned +ConfigBlock::GetBlockValue(const char *name, unsigned default_value) const +{ + const BlockParam *bp = GetBlockParam(name); + if (bp == nullptr) + return default_value; + + return bp->GetUnsignedValue(); +} + +gcc_pure +bool +ConfigBlock::GetBlockValue(const char *name, bool default_value) const +{ + const BlockParam *bp = GetBlockParam(name); + if (bp == nullptr) + return default_value; + + return bp->GetBoolValue(); +} |