2015-01-21 21:50:02 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2015-01-21 21:50:02 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Data.hxx"
|
2018-07-17 22:18:15 +02:00
|
|
|
#include "Parser.hxx"
|
|
|
|
#include "fs/AllocatedPath.hxx"
|
2018-07-17 21:18:18 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2018-07-17 21:14:53 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2015-01-21 21:50:02 +01:00
|
|
|
|
2018-07-17 22:18:15 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-01-21 21:50:02 +01:00
|
|
|
void
|
|
|
|
ConfigData::Clear()
|
|
|
|
{
|
2018-07-18 11:03:19 +02:00
|
|
|
for (auto &i : params)
|
|
|
|
i.clear();
|
2015-01-21 22:13:44 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
for (auto &i : blocks)
|
|
|
|
i.clear();
|
2015-01-21 21:50:02 +01:00
|
|
|
}
|
2018-07-17 21:14:53 +02:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
template<typename T>
|
|
|
|
gcc_pure
|
|
|
|
static auto
|
|
|
|
FindLast(const std::forward_list<T> &list)
|
2018-07-18 10:17:45 +02:00
|
|
|
{
|
2018-07-18 11:03:19 +02:00
|
|
|
auto i = list.before_begin();
|
|
|
|
while (std::next(i) != list.end())
|
|
|
|
++i;
|
|
|
|
return i;
|
|
|
|
}
|
2018-07-18 10:17:45 +02:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
template<typename T>
|
|
|
|
static auto
|
|
|
|
Append(std::forward_list<T> &list, T &&src)
|
|
|
|
{
|
2020-02-03 01:06:22 +01:00
|
|
|
return list.emplace_after(FindLast(list), std::forward<T>(src));
|
2018-07-18 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigData::AddParam(ConfigOption option,
|
2018-07-18 11:21:59 +02:00
|
|
|
ConfigParam &¶m) noexcept
|
2018-07-18 10:17:45 +02:00
|
|
|
{
|
2018-07-18 11:21:59 +02:00
|
|
|
Append(GetParamList(option), std::move(param));
|
2018-07-18 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 22:18:15 +02:00
|
|
|
const char *
|
|
|
|
ConfigData::GetString(ConfigOption option,
|
|
|
|
const char *default_value) const noexcept
|
|
|
|
{
|
|
|
|
const auto *param = GetParam(option);
|
|
|
|
if (param == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return param->value.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatedPath
|
|
|
|
ConfigData::GetPath(ConfigOption option) const
|
|
|
|
{
|
|
|
|
const auto *param = GetParam(option);
|
|
|
|
if (param == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return param->GetPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
ConfigData::GetUnsigned(ConfigOption option, unsigned default_value) const
|
|
|
|
{
|
|
|
|
const auto *param = GetParam(option);
|
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (param == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
const char *const s = param->value.c_str();
|
|
|
|
value = strtol(s, &endptr, 0);
|
|
|
|
if (endptr == s || *endptr != 0 || value < 0)
|
|
|
|
throw FormatRuntimeError("Not a valid non-negative number in line %i",
|
|
|
|
param->line);
|
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
ConfigData::GetPositive(ConfigOption option, unsigned default_value) const
|
|
|
|
{
|
|
|
|
const auto *param = GetParam(option);
|
|
|
|
long value;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if (param == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
const char *const s = param->value.c_str();
|
|
|
|
value = strtol(s, &endptr, 0);
|
|
|
|
if (endptr == s || *endptr != 0)
|
|
|
|
throw FormatRuntimeError("Not a valid number in line %i",
|
|
|
|
param->line);
|
|
|
|
|
|
|
|
if (value <= 0)
|
|
|
|
throw FormatRuntimeError("Not a positive number in line %i",
|
|
|
|
param->line);
|
|
|
|
|
|
|
|
return (unsigned)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConfigData::GetBool(ConfigOption option, bool default_value) const
|
|
|
|
{
|
2019-05-29 22:31:00 +02:00
|
|
|
return With(option, [default_value](const char *s){
|
|
|
|
return s != nullptr
|
|
|
|
? ParseBool(s)
|
|
|
|
: default_value;
|
|
|
|
});
|
2018-07-17 22:18:15 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
ConfigBlock &
|
2018-07-18 10:17:45 +02:00
|
|
|
ConfigData::AddBlock(ConfigBlockOption option,
|
2018-07-18 11:21:59 +02:00
|
|
|
ConfigBlock &&block) noexcept
|
2018-07-18 10:17:45 +02:00
|
|
|
{
|
2018-07-18 11:21:59 +02:00
|
|
|
return *Append(GetBlockList(option), std::move(block));
|
2018-07-18 10:17:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:14:53 +02:00
|
|
|
const ConfigBlock *
|
|
|
|
ConfigData::FindBlock(ConfigBlockOption option,
|
2018-07-17 21:18:18 +02:00
|
|
|
const char *key, const char *value) const
|
2018-07-17 21:14:53 +02:00
|
|
|
{
|
2018-07-18 11:03:19 +02:00
|
|
|
for (const auto &block : GetBlockList(option)) {
|
|
|
|
const char *value2 = block.GetBlockValue(key);
|
2018-07-17 21:14:53 +02:00
|
|
|
if (value2 == nullptr)
|
2018-07-17 21:18:18 +02:00
|
|
|
throw FormatRuntimeError("block without '%s' in line %d",
|
2018-07-18 11:03:19 +02:00
|
|
|
key, block.line);
|
2018-07-17 21:14:53 +02:00
|
|
|
|
|
|
|
if (StringIsEqual(value2, value))
|
2018-07-18 11:03:19 +02:00
|
|
|
return █
|
2018-07-17 21:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-18 09:42:56 +02:00
|
|
|
|
|
|
|
ConfigBlock &
|
|
|
|
ConfigData::MakeBlock(ConfigBlockOption option,
|
|
|
|
const char *key, const char *value)
|
|
|
|
{
|
|
|
|
auto *block = const_cast<ConfigBlock *>(FindBlock(option, key, value));
|
|
|
|
if (block == nullptr) {
|
2018-07-18 11:21:59 +02:00
|
|
|
ConfigBlock new_block;
|
|
|
|
new_block.AddBlockParam(key, value);
|
2018-07-18 11:03:19 +02:00
|
|
|
block = &AddBlock(option, std::move(new_block));
|
2018-07-18 09:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *block;
|
|
|
|
}
|