config/Param: add method GetPath()

Move code from config_parse_path().
This commit is contained in:
Max Kellermann 2016-10-28 11:45:12 +02:00
parent 5b2b4bf13c
commit 7a3415166e
5 changed files with 26 additions and 21 deletions

View File

@ -68,7 +68,7 @@ listen_add_config_param(unsigned int port,
if (0 == strcmp(param->value.c_str(), "any")) {
return listen_socket->AddPort(port, error_r);
} else if (param->value[0] == '/' || param->value[0] == '~') {
auto path = config_parse_path(param, error_r);
auto path = param->GetPath(error_r);
return !path.IsNull() &&
listen_socket->AddPath(std::move(path), error_r);
} else {

View File

@ -128,18 +128,7 @@ config_get_path(ConfigOption option, Error &error)
if (param == nullptr)
return AllocatedPath::Null();
return config_parse_path(param, error);
}
AllocatedPath
config_parse_path(const ConfigParam *param, Error & error)
{
AllocatedPath path = ParsePath(param->value.c_str(), error);
if (gcc_unlikely(path.IsNull()))
error.FormatPrefix("Invalid path at line %i: ",
param->line);
return path;
return param->GetPath(error);
}
unsigned

View File

@ -84,14 +84,6 @@ config_get_string(enum ConfigOption option, const char *default_value=nullptr);
AllocatedPath
config_get_path(enum ConfigOption option, Error &error);
/**
* Parse a configuration parameter as a path.
* If there is a tilde prefix, it is expanded. If the path could
* not be parsed, returns AllocatedPath::Null() and sets the error.
*/
AllocatedPath
config_parse_path(const ConfigParam *param, Error & error_r);
gcc_pure
unsigned
config_get_unsigned(enum ConfigOption option, unsigned default_value);

View File

@ -19,6 +19,9 @@
#include "config.h"
#include "Param.hxx"
#include "ConfigPath.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/Error.hxx"
ConfigParam::ConfigParam(const char *_value, int _line)
:next(nullptr), value(_value), line(_line), used(false) {}
@ -27,3 +30,14 @@ ConfigParam::~ConfigParam()
{
delete next;
}
AllocatedPath
ConfigParam::GetPath(Error &error) const
{
auto path = ParsePath(value.c_str(), error);
if (gcc_unlikely(path.IsNull()))
error.FormatPrefix("Invalid path at line %i: ", line);
return path;
}

View File

@ -25,6 +25,9 @@
#include <string>
class Error;
class AllocatedPath;
struct ConfigParam {
/**
* The next ConfigParam with the same name. The destructor
@ -62,6 +65,13 @@ struct ConfigParam {
bool IsNull() const {
return line < 0;
}
/**
* Parse the value as a path. If there is a tilde prefix, it
* is expanded. If the path could not be parsed, returns
* AllocatedPath::Null() and sets the error.
*/
AllocatedPath GetPath(Error &error) const;
};
#endif