From cb0bb668eb948fe3348ffe8437d8b48f2d84b1f8 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 16 Jun 2010 11:05:10 +0200 Subject: [PATCH] Add support for retrieving float and double variables from the configuration. --- src/conf.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/conf.h | 2 ++ 2 files changed, 64 insertions(+) diff --git a/src/conf.c b/src/conf.c index 058b992328..160ee1f9e8 100644 --- a/src/conf.c +++ b/src/conf.c @@ -309,6 +309,68 @@ ConfGetBool(char *name, int *val) return 1; } +/** + * \brief Retrieve a configuration value as a double + * + * \param name Name of configuration parameter to get. + * \param val Pointer to an double that will be set the + * configuration value. + * + * \retval 1 will be returned if the name is found and was properly + * converted to a double, otherwise 0 will be returned. + */ +int +ConfGetDouble(char *name, double *val) +{ + char *strval; + double tmpdo; + char *endptr; + + if (ConfGet(name, &strval) == 0) + return 0; + + errno = 0; + tmpdo = strtod(strval, &endptr); + if (strval[0] == '\0' || *endptr != '\0') + return 0; + if (errno == ERANGE) + return 0; + + *val = tmpdo; + return 1; +} + +/** + * \brief Retrieve a configuration value as a float + * + * \param name Name of configuration parameter to get. + * \param val Pointer to an float that will be set the + * configuration value. + * + * \retval 1 will be returned if the name is found and was properly + * converted to a double, otherwise 0 will be returned. + */ +int +ConfGetFloat(char *name, float *val) +{ + char *strval; + double tmpfl; + char *endptr; + + if (ConfGet(name, &strval) == 0) + return 0; + + errno = 0; + tmpfl = strtof(strval, &endptr); + if (strval[0] == '\0' || *endptr != '\0') + return 0; + if (errno == ERANGE) + return 0; + + *val = tmpfl; + return 1; +} + /** * \brief Remove (and SCFree) the provided configuration node. */ diff --git a/src/conf.h b/src/conf.h index 8d15ad8c6c..20a71060a7 100644 --- a/src/conf.h +++ b/src/conf.h @@ -53,6 +53,8 @@ ConfNode *ConfGetRootNode(void); int ConfGet(char *name, char **vptr); int ConfGetInt(char *name, intmax_t *val); int ConfGetBool(char *name, int *val); +int ConfGetDouble(char *name, double *val); +int ConfGetFloat(char *name, float *val); int ConfSet(char *name, char *val, int allow_override); void ConfDump(void); void ConfNodeDump(ConfNode *node, const char *prefix);