conf: added config_get_positive()

This convenience function parses a configuration value into a positive
integer.  It aborts if parsing fails.
This commit is contained in:
Max Kellermann 2009-01-21 08:46:59 +01:00
parent 17222e9561
commit f11eb14c8a
2 changed files with 23 additions and 0 deletions

View File

@ -407,6 +407,26 @@ config_get_path(const char *name)
return param->value = path; return param->value = path;
} }
unsigned
config_get_positive(const char *name, unsigned default_value)
{
struct config_param *param = config_get_param(name);
long value;
char *endptr;
if (param == NULL)
return default_value;
value = strtol(param->value, &endptr, 0);
if (*endptr != 0)
g_error("Not a valid number in line %i", param->line);
if (value <= 0)
g_error("Not a positive number in line %i", param->line);
return (unsigned)value;
}
struct block_param * struct block_param *
getBlockParam(struct config_param * param, const char *name) getBlockParam(struct config_param * param, const char *name)
{ {

View File

@ -110,6 +110,9 @@ config_get_string(const char *name, const char *default_value);
const char * const char *
config_get_path(const char *name); config_get_path(const char *name);
unsigned
config_get_positive(const char *name, unsigned default_value);
struct block_param * struct block_param *
getBlockParam(struct config_param *param, const char *name); getBlockParam(struct config_param *param, const char *name);