2015-01-21 20:45:34 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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 "config.h"
|
|
|
|
#include "Block.hxx"
|
|
|
|
#include "ConfigParser.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "ConfigPath.hxx"
|
2015-01-21 20:45:34 +01:00
|
|
|
#include "system/FatalError.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
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
#include <assert.h>
|
2015-01-21 20:45:34 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
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)
|
2015-01-21 20:45:34 +01:00
|
|
|
FormatFatalError("Not a valid number in line %i", line);
|
|
|
|
|
|
|
|
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)
|
2015-01-21 20:45:34 +01:00
|
|
|
FormatFatalError("Not a valid number in line %i", line);
|
|
|
|
|
|
|
|
return (unsigned)value2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-21 21:12:54 +01:00
|
|
|
BlockParam::GetBoolValue() const
|
2015-01-21 20:45:34 +01:00
|
|
|
{
|
|
|
|
bool value2;
|
|
|
|
if (!get_bool(value.c_str(), &value2))
|
|
|
|
FormatFatalError("%s is not a boolean value (yes, true, 1) or "
|
|
|
|
"(no, false, 0) on line %i\n",
|
|
|
|
name.c_str(), line);
|
|
|
|
|
|
|
|
return value2;
|
|
|
|
}
|
2015-01-21 22:13:44 +01:00
|
|
|
|
|
|
|
ConfigBlock::~ConfigBlock()
|
|
|
|
{
|
|
|
|
delete next;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return AllocatedPath::Null();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConfigBlock::GetBlockValue(const char *name, bool default_value) const
|
|
|
|
{
|
|
|
|
const BlockParam *bp = GetBlockParam(name);
|
|
|
|
if (bp == nullptr)
|
|
|
|
return default_value;
|
|
|
|
|
|
|
|
return bp->GetBoolValue();
|
|
|
|
}
|