config/Param: add method With()
This commit is contained in:
		@@ -93,7 +93,7 @@ log_init_file(int line)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline LogLevel
 | 
					static inline LogLevel
 | 
				
			||||||
parse_log_level(const char *value, int line)
 | 
					parse_log_level(const char *value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (0 == strcmp(value, "default"))
 | 
						if (0 == strcmp(value, "default"))
 | 
				
			||||||
		return LogLevel::DEFAULT;
 | 
							return LogLevel::DEFAULT;
 | 
				
			||||||
@@ -102,8 +102,7 @@ parse_log_level(const char *value, int line)
 | 
				
			|||||||
	else if (0 == strcmp(value, "verbose"))
 | 
						else if (0 == strcmp(value, "verbose"))
 | 
				
			||||||
		return LogLevel::DEBUG;
 | 
							return LogLevel::DEBUG;
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		throw FormatRuntimeError("unknown log level \"%s\" at line %d",
 | 
							throw FormatRuntimeError("unknown log level \"%s\"", value);
 | 
				
			||||||
					 value, line);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -132,9 +131,12 @@ log_init(const ConfigData &config, bool verbose, bool use_stdout)
 | 
				
			|||||||
#else
 | 
					#else
 | 
				
			||||||
	if (verbose)
 | 
						if (verbose)
 | 
				
			||||||
		SetLogThreshold(LogLevel::DEBUG);
 | 
							SetLogThreshold(LogLevel::DEBUG);
 | 
				
			||||||
	else if (const auto ¶m = config.GetParam(ConfigOption::LOG_LEVEL))
 | 
						else
 | 
				
			||||||
		SetLogThreshold(parse_log_level(param->value.c_str(),
 | 
							SetLogThreshold(config.With(ConfigOption::LOG_LEVEL, [](const char *s){
 | 
				
			||||||
						param->line));
 | 
								return s != nullptr
 | 
				
			||||||
 | 
									? parse_log_level(s)
 | 
				
			||||||
 | 
									: LogLevel::DEFAULT;
 | 
				
			||||||
 | 
							}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (use_stdout) {
 | 
						if (use_stdout) {
 | 
				
			||||||
		out_fd = STDOUT_FILENO;
 | 
							out_fd = STDOUT_FILENO;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										60
									
								
								src/Main.cxx
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								src/Main.cxx
									
									
									
									
									
								
							@@ -282,20 +282,23 @@ initialize_decoder_and_player(Instance &instance,
 | 
				
			|||||||
	size_t buffer_size;
 | 
						size_t buffer_size;
 | 
				
			||||||
	param = config.GetParam(ConfigOption::AUDIO_BUFFER_SIZE);
 | 
						param = config.GetParam(ConfigOption::AUDIO_BUFFER_SIZE);
 | 
				
			||||||
	if (param != nullptr) {
 | 
						if (param != nullptr) {
 | 
				
			||||||
		char *test;
 | 
							buffer_size = param->With([](const char *s){
 | 
				
			||||||
		long tmp = strtol(param->value.c_str(), &test, 10);
 | 
								char *test;
 | 
				
			||||||
		if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
 | 
								long tmp = strtol(s, &test, 10);
 | 
				
			||||||
			throw FormatRuntimeError("buffer size \"%s\" is not a "
 | 
								if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
 | 
				
			||||||
						 "positive integer, line %i",
 | 
									throw FormatRuntimeError("buffer size \"%s\" is not a "
 | 
				
			||||||
						 param->value.c_str(), param->line);
 | 
												 "positive integer", s);
 | 
				
			||||||
		buffer_size = tmp * KILOBYTE;
 | 
								size_t result = tmp * KILOBYTE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (buffer_size < MIN_BUFFER_SIZE) {
 | 
								if (result < MIN_BUFFER_SIZE) {
 | 
				
			||||||
			FormatWarning(config_domain, "buffer size %lu is too small, using %lu bytes instead",
 | 
									FormatWarning(config_domain, "buffer size %lu is too small, using %lu bytes instead",
 | 
				
			||||||
				      (unsigned long)buffer_size,
 | 
										      (unsigned long)result,
 | 
				
			||||||
				      (unsigned long)MIN_BUFFER_SIZE);
 | 
										      (unsigned long)MIN_BUFFER_SIZE);
 | 
				
			||||||
			buffer_size = MIN_BUFFER_SIZE;
 | 
									result = MIN_BUFFER_SIZE;
 | 
				
			||||||
		}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return result;
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
	} else
 | 
						} else
 | 
				
			||||||
		buffer_size = DEFAULT_BUFFER_SIZE;
 | 
							buffer_size = DEFAULT_BUFFER_SIZE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -309,17 +312,12 @@ initialize_decoder_and_player(Instance &instance,
 | 
				
			|||||||
		config.GetPositive(ConfigOption::MAX_PLAYLIST_LENGTH,
 | 
							config.GetPositive(ConfigOption::MAX_PLAYLIST_LENGTH,
 | 
				
			||||||
				   DEFAULT_PLAYLIST_MAX_LENGTH);
 | 
									   DEFAULT_PLAYLIST_MAX_LENGTH);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	AudioFormat configured_audio_format = AudioFormat::Undefined();
 | 
						AudioFormat configured_audio_format = config.With(ConfigOption::AUDIO_OUTPUT_FORMAT, [](const char *s){
 | 
				
			||||||
	param = config.GetParam(ConfigOption::AUDIO_OUTPUT_FORMAT);
 | 
							if (s == nullptr)
 | 
				
			||||||
	if (param != nullptr) {
 | 
								return AudioFormat::Undefined();
 | 
				
			||||||
		try {
 | 
					
 | 
				
			||||||
			configured_audio_format = ParseAudioFormat(param->value.c_str(),
 | 
							return ParseAudioFormat(s, true);
 | 
				
			||||||
								   true);
 | 
						});
 | 
				
			||||||
		} catch (...) {
 | 
					 | 
				
			||||||
			std::throw_with_nested(FormatRuntimeError("error parsing line %i",
 | 
					 | 
				
			||||||
								  param->line));
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	instance.partitions.emplace_back(instance,
 | 
						instance.partitions.emplace_back(instance,
 | 
				
			||||||
					 "default",
 | 
										 "default",
 | 
				
			||||||
@@ -329,15 +327,11 @@ initialize_decoder_and_player(Instance &instance,
 | 
				
			|||||||
					 replay_gain_config);
 | 
										 replay_gain_config);
 | 
				
			||||||
	auto &partition = instance.partitions.back();
 | 
						auto &partition = instance.partitions.back();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	try {
 | 
						partition.replay_gain_mode = config.With(ConfigOption::REPLAYGAIN, [](const char *s){
 | 
				
			||||||
		param = config.GetParam(ConfigOption::REPLAYGAIN);
 | 
							return s != nullptr
 | 
				
			||||||
		if (param != nullptr)
 | 
								? FromString(s)
 | 
				
			||||||
			partition.replay_gain_mode =
 | 
								: ReplayGainMode::OFF;
 | 
				
			||||||
				FromString(param->value.c_str());
 | 
						});
 | 
				
			||||||
	} catch (...) {
 | 
					 | 
				
			||||||
		std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
 | 
					 | 
				
			||||||
							  param->line));
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
inline void
 | 
					inline void
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -82,44 +82,39 @@ static unsigned parsePermissions(const char *string)
 | 
				
			|||||||
void
 | 
					void
 | 
				
			||||||
initPermissions(const ConfigData &config)
 | 
					initPermissions(const ConfigData &config)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	unsigned permission;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	permission_default = PERMISSION_READ | PERMISSION_ADD |
 | 
						permission_default = PERMISSION_READ | PERMISSION_ADD |
 | 
				
			||||||
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
 | 
						    PERMISSION_CONTROL | PERMISSION_ADMIN;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (const auto ¶m : config.GetParamList(ConfigOption::PASSWORD)) {
 | 
						for (const auto ¶m : config.GetParamList(ConfigOption::PASSWORD)) {
 | 
				
			||||||
		permission_default = 0;
 | 
							permission_default = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const char *separator = strchr(param.value.c_str(),
 | 
							param.With([](const char *value){
 | 
				
			||||||
					       PERMISSION_PASSWORD_CHAR);
 | 
								const char *separator = strchr(value,
 | 
				
			||||||
 | 
											       PERMISSION_PASSWORD_CHAR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (separator == NULL)
 | 
								if (separator == NULL)
 | 
				
			||||||
			throw FormatRuntimeError("\"%c\" not found in password string "
 | 
									throw FormatRuntimeError("\"%c\" not found in password string",
 | 
				
			||||||
						 "\"%s\", line %i",
 | 
												 PERMISSION_PASSWORD_CHAR);
 | 
				
			||||||
						 PERMISSION_PASSWORD_CHAR,
 | 
					 | 
				
			||||||
						 param.value.c_str(),
 | 
					 | 
				
			||||||
						 param.line);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		std::string password(param.value.c_str(), separator);
 | 
								std::string password(value, separator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		permission = parsePermissions(separator + 1);
 | 
								unsigned permission = parsePermissions(separator + 1);
 | 
				
			||||||
 | 
								permission_passwords.insert(std::make_pair(std::move(password),
 | 
				
			||||||
		permission_passwords.insert(std::make_pair(std::move(password),
 | 
													   permission));
 | 
				
			||||||
							   permission));
 | 
							});
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const ConfigParam *param;
 | 
						config.With(ConfigOption::DEFAULT_PERMS, [](const char *value){
 | 
				
			||||||
	param = config.GetParam(ConfigOption::DEFAULT_PERMS);
 | 
							if (value != nullptr)
 | 
				
			||||||
 | 
								permission_default = parsePermissions(value);
 | 
				
			||||||
	if (param)
 | 
						});
 | 
				
			||||||
		permission_default = parsePermissions(param->value.c_str());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef HAVE_UN
 | 
					#ifdef HAVE_UN
 | 
				
			||||||
	param = config.GetParam(ConfigOption::LOCAL_PERMISSIONS);
 | 
						local_permissions = config.With(ConfigOption::LOCAL_PERMISSIONS, [](const char *value){
 | 
				
			||||||
	if (param != nullptr)
 | 
							return value != nullptr
 | 
				
			||||||
		local_permissions = parsePermissions(param->value.c_str());
 | 
								? parsePermissions(value)
 | 
				
			||||||
	else
 | 
								: permission_default;
 | 
				
			||||||
		local_permissions = permission_default;
 | 
						});
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,29 +43,22 @@ ParsePreamp(const char *s)
 | 
				
			|||||||
	return pow(10, f / 20.0);
 | 
						return pow(10, f / 20.0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static float
 | 
					 | 
				
			||||||
ParsePreamp(const ConfigParam &p)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	try {
 | 
					 | 
				
			||||||
		return ParsePreamp(p.value.c_str());
 | 
					 | 
				
			||||||
	} catch (...) {
 | 
					 | 
				
			||||||
		std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
 | 
					 | 
				
			||||||
							  p.line));
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
ReplayGainConfig
 | 
					ReplayGainConfig
 | 
				
			||||||
LoadReplayGainConfig(const ConfigData &config)
 | 
					LoadReplayGainConfig(const ConfigData &config)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	ReplayGainConfig replay_gain_config;
 | 
						ReplayGainConfig replay_gain_config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const auto *param = config.GetParam(ConfigOption::REPLAYGAIN_PREAMP);
 | 
						replay_gain_config.preamp = config.With(ConfigOption::REPLAYGAIN_PREAMP, [](const char *s){
 | 
				
			||||||
	if (param)
 | 
							return s != nullptr
 | 
				
			||||||
		replay_gain_config.preamp = ParsePreamp(*param);
 | 
								? ParsePreamp(s)
 | 
				
			||||||
 | 
								: 1.0;
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	param = config.GetParam(ConfigOption::REPLAYGAIN_MISSING_PREAMP);
 | 
						replay_gain_config.missing_preamp = config.With(ConfigOption::REPLAYGAIN_MISSING_PREAMP, [](const char *s){
 | 
				
			||||||
	if (param)
 | 
							return s != nullptr
 | 
				
			||||||
		replay_gain_config.missing_preamp = ParsePreamp(*param);
 | 
								? ParsePreamp(s)
 | 
				
			||||||
 | 
								: 1.0;
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	replay_gain_config.limit = config.GetBool(ConfigOption::REPLAYGAIN_LIMIT,
 | 
						replay_gain_config.limit = config.GetBool(ConfigOption::REPLAYGAIN_LIMIT,
 | 
				
			||||||
						  ReplayGainConfig::DEFAULT_LIMIT);
 | 
											  ReplayGainConfig::DEFAULT_LIMIT);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,6 +54,14 @@ struct ConfigData {
 | 
				
			|||||||
		return list.empty() ? nullptr : &list.front();
 | 
							return list.empty() ? nullptr : &list.front();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						template<typename F>
 | 
				
			||||||
 | 
						auto With(ConfigOption option, F &&f) const {
 | 
				
			||||||
 | 
							const auto *param = GetParam(option);
 | 
				
			||||||
 | 
							return param != nullptr
 | 
				
			||||||
 | 
								? param->With(std::forward<F>(f))
 | 
				
			||||||
 | 
								: f(nullptr);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	gcc_pure
 | 
						gcc_pure
 | 
				
			||||||
	const char *GetString(ConfigOption option,
 | 
						const char *GetString(ConfigOption option,
 | 
				
			||||||
			      const char *default_value=nullptr) const noexcept;
 | 
								      const char *default_value=nullptr) const noexcept;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -66,6 +66,19 @@ struct ConfigParam {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	[[noreturn]]
 | 
						[[noreturn]]
 | 
				
			||||||
	void ThrowWithNested() const;
 | 
						void ThrowWithNested() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Invoke a function with the configured value; if the
 | 
				
			||||||
 | 
						 * function throws, call ThrowWithNested().
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						template<typename F>
 | 
				
			||||||
 | 
						auto With(F &&f) const {
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
 | 
								return f(value.c_str());
 | 
				
			||||||
 | 
							} catch (...) {
 | 
				
			||||||
 | 
								ThrowWithNested();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user