2015-01-21 20:45:34 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2015-01-21 20:45:34 +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 "Block.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "Parser.hxx"
|
|
|
|
#include "Path.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2016-10-28 23:08:42 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2015-01-21 20:45:34 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-05-29 22:33:51 +02:00
|
|
|
void
|
|
|
|
BlockParam::ThrowWithNested() const
|
|
|
|
{
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Error in setting \"%s\" on line %i",
|
|
|
|
name.c_str(), line));
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:45:34 +01:00
|
|
|
int
|
2015-01-21 21:12:54 +01:00
|
|
|
BlockParam::GetIntValue() const
|
2015-01-21 20:45:34 +01:00
|
|
|
{
|
2018-01-02 17:23:10 +01:00
|
|
|
const char *const s = value.c_str();
|
2015-01-21 20:45:34 +01:00
|
|
|
char *endptr;
|
2018-01-02 17:23:10 +01:00
|
|
|
long value2 = strtol(s, &endptr, 0);
|
|
|
|
if (endptr == s || *endptr != 0)
|
2018-07-17 22:23:04 +02:00
|
|
|
throw FormatRuntimeError("Not a valid number in line %i", line);
|
2015-01-21 20:45:34 +01:00
|
|
|
|
|
|
|
return value2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2015-01-21 21:12:54 +01:00
|
|
|
BlockParam::GetUnsignedValue() const
|
2015-01-21 20:45:34 +01:00
|
|
|
{
|
2018-01-02 17:23:10 +01:00
|
|
|
const char *const s = value.c_str();
|
2015-01-21 20:45:34 +01:00
|
|
|
char *endptr;
|
2018-01-02 17:23:10 +01:00
|
|
|
unsigned long value2 = strtoul(s, &endptr, 0);
|
|
|
|
if (endptr == s || *endptr != 0)
|
2018-07-17 22:23:04 +02:00
|
|
|
throw FormatRuntimeError("Not a valid number in line %i", line);
|
2015-01-21 20:45:34 +01:00
|
|
|
|
|
|
|
return (unsigned)value2;
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:25 +01:00
|
|
|
unsigned
|
|
|
|
BlockParam::GetPositiveValue() const
|
|
|
|
{
|
|
|
|
const char *const s = value.c_str();
|
|
|
|
char *endptr;
|
|
|
|
unsigned long value2 = strtoul(s, &endptr, 0);
|
|
|
|
if (endptr == s || *endptr != 0)
|
2018-07-17 22:23:04 +02:00
|
|
|
throw FormatRuntimeError("Not a valid number in line %i", line);
|
2018-01-02 17:22:25 +01:00
|
|
|
|
|
|
|
if (value2 <= 0)
|
2018-07-17 22:23:04 +02:00
|
|
|
throw FormatRuntimeError("Number in line %i must be positive", line);
|
2018-01-02 17:22:25 +01:00
|
|
|
|
|
|
|
return (unsigned)value2;
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:45:34 +01:00
|
|
|
bool
|
2015-01-21 21:12:54 +01:00
|
|
|
BlockParam::GetBoolValue() const
|
2015-01-21 20:45:34 +01:00
|
|
|
{
|
2019-05-29 22:31:00 +02:00
|
|
|
return With(ParseBool);
|
2015-01-21 20:45:34 +01:00
|
|
|
}
|
2015-01-21 22:13:44 +01:00
|
|
|
|
|
|
|
const BlockParam *
|
2017-05-08 14:44:49 +02:00
|
|
|
ConfigBlock::GetBlockParam(const char *name) const noexcept
|
2015-01-21 22:13:44 +01:00
|
|
|
{
|
|
|
|
for (const auto &i : block_params) {
|
|
|
|
if (i.name == name) {
|
|
|
|
i.used = true;
|
|
|
|
return &i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
ConfigBlock::GetBlockValue(const char *name,
|
|
|
|
const char *default_value) const noexcept
|
2015-01-21 22:13:44 +01:00
|
|
|
{
|
|
|
|
const BlockParam *bp = GetBlockParam(name);
|
|
|
|
if (bp == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return bp->value.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocatedPath
|
2016-10-28 23:08:42 +02:00
|
|
|
ConfigBlock::GetPath(const char *name, const char *default_value) const
|
2015-01-21 22:13:44 +01:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
const BlockParam *bp = GetBlockParam(name);
|
|
|
|
if (bp != nullptr) {
|
|
|
|
s = bp->value.c_str();
|
|
|
|
} else {
|
|
|
|
if (default_value == nullptr)
|
2018-01-17 12:17:41 +01:00
|
|
|
return nullptr;
|
2015-01-21 22:13:44 +01:00
|
|
|
|
|
|
|
s = default_value;
|
|
|
|
}
|
|
|
|
|
2016-11-07 09:07:50 +01:00
|
|
|
return ParsePath(s);
|
2015-01-21 22:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:22:25 +01:00
|
|
|
unsigned
|
|
|
|
ConfigBlock::GetPositiveValue(const char *name, unsigned default_value) const
|
|
|
|
{
|
|
|
|
const auto *param = GetBlockParam(name);
|
|
|
|
if (param == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return param->GetPositiveValue();
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
bool
|
|
|
|
ConfigBlock::GetBlockValue(const char *name, bool default_value) const
|
|
|
|
{
|
|
|
|
const BlockParam *bp = GetBlockParam(name);
|
|
|
|
if (bp == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return bp->GetBoolValue();
|
|
|
|
}
|