ConfigGlobal.cxx 4.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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 "ConfigGlobal.hxx"
#include "ConfigParser.hxx"
23
#include "Data.hxx"
24
#include "Param.hxx"
25
#include "Block.hxx"
26
#include "ConfigFile.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "ConfigPath.hxx"
28
#include "ConfigError.hxx"
29
#include "fs/Path.hxx"
30
#include "fs/AllocatedPath.hxx"
31
#include "system/FatalError.hxx"
32
#include "Log.hxx"
33

34
#include <stdlib.h>
35 36 37 38 39

static ConfigData config_data;

void config_global_finish(void)
{
40
	config_data.Clear();
41 42 43 44 45 46
}

void config_global_init(void)
{
}

47 48
void
ReadConfigFile(Path path)
49
{
50
	return ReadConfigFile(config_data, path);
51 52 53
}

static void
54
Check(const ConfigBlock &block)
55
{
56 57
	if (!block.used)
		/* this whole block was not queried at all -
58 59 60 61
		   the feature might be disabled at compile time?
		   Silently ignore it here. */
		return;

62
	for (const auto &i : block.block_params) {
63
		if (!i.used)
64 65 66
			FormatWarning(config_domain,
				      "option '%s' on line %i was not recognized",
				      i.name.c_str(), i.line);
67 68 69 70 71
	}
}

void config_global_check(void)
{
72 73 74
	for (auto i : config_data.blocks)
		for (const auto *p = i; p != nullptr; p = p->next)
			Check(*p);
75 76
}

77
const ConfigParam *
78
config_get_param(ConfigOption option) noexcept
79
{
80
	auto *param = config_data.params[unsigned(option)];
81 82
	if (param != nullptr)
		param->used = true;
83 84 85
	return param;
}

86
const ConfigBlock *
87
config_get_block(ConfigBlockOption option) noexcept
88 89 90 91 92 93 94 95 96
{
	ConfigBlock *block = config_data.blocks[unsigned(option)];
	if (block != nullptr)
		block->used = true;
	return block;
}

const ConfigBlock *
config_find_block(ConfigBlockOption option, const char *key, const char *value)
97
{
98 99 100
	for (const auto *block = config_get_block(option);
	     block != nullptr; block = block->next) {
		const char *value2 = block->GetBlockValue(key);
101 102
		if (value2 == nullptr)
			FormatFatalError("block without '%s' name in line %d",
103
					 key, block->line);
104 105

		if (strcmp(value2, value) == 0)
106
			return block;
107 108 109 110 111
	}

	return nullptr;
}

112
const char *
113
config_get_string(ConfigOption option, const char *default_value) noexcept
114
{
115
	const auto *param = config_get_param(option);
116

117
	if (param == nullptr)
118 119
		return default_value;

120
	return param->value.c_str();
121 122
}

123
AllocatedPath
124
config_get_path(ConfigOption option)
125
{
126
	const auto *param = config_get_param(option);
127
	if (param == nullptr)
128
		return AllocatedPath::Null();
129

130
	return param->GetPath();
131 132 133 134 135
}

unsigned
config_get_unsigned(ConfigOption option, unsigned default_value)
{
136
	const auto *param = config_get_param(option);
137 138 139
	long value;
	char *endptr;

140
	if (param == nullptr)
141 142
		return default_value;

143
	value = strtol(param->value.c_str(), &endptr, 0);
144
	if (*endptr != 0 || value < 0)
145 146
		FormatFatalError("Not a valid non-negative number in line %i",
				 param->line);
147 148 149 150 151 152 153

	return (unsigned)value;
}

unsigned
config_get_positive(ConfigOption option, unsigned default_value)
{
154
	const auto *param = config_get_param(option);
155 156 157
	long value;
	char *endptr;

158
	if (param == nullptr)
159 160
		return default_value;

161
	value = strtol(param->value.c_str(), &endptr, 0);
162
	if (*endptr != 0)
163
		FormatFatalError("Not a valid number in line %i", param->line);
164 165

	if (value <= 0)
166 167
		FormatFatalError("Not a positive number in line %i",
				 param->line);
168 169 170 171 172 173 174

	return (unsigned)value;
}

bool
config_get_bool(ConfigOption option, bool default_value)
{
175
	const auto *param = config_get_param(option);
176 177
	bool success, value;

178
	if (param == nullptr)
179 180
		return default_value;

181
	success = get_bool(param->value.c_str(), &value);
182
	if (!success)
183 184 185
		FormatFatalError("Expected boolean value (yes, true, 1) or "
				 "(no, false, 0) on line %i\n",
				 param->line);
186 187 188

	return value;
}