From b9d11ef065a44e856c60709aad2cf0aae2496bf2 Mon Sep 17 00:00:00 2001 From: juha-h Date: Fri, 18 Oct 2024 15:58:22 +0300 Subject: [PATCH] conf: add conf_get_float (#1198) --- include/re_conf.h | 1 + src/conf/conf.c | 27 +++++++++++++++++++++++++++ test/conf.c | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/re_conf.h b/include/re_conf.h index 1db507d5a..f702bba7f 100644 --- a/include/re_conf.h +++ b/include/re_conf.h @@ -16,6 +16,7 @@ int conf_get_str(const struct conf *conf, const char *name, char *str, size_t size); int conf_get_u32(const struct conf *conf, const char *name, uint32_t *num); int conf_get_i32(const struct conf *conf, const char *name, int32_t *num); +int conf_get_float(const struct conf *conf, const char *name, double *num); int conf_get_bool(const struct conf *conf, const char *name, bool *val); int conf_apply(const struct conf *conf, const char *name, conf_h *ch, void *arg); diff --git a/src/conf/conf.c b/src/conf/conf.c index 2fe1a2417..22b72567d 100644 --- a/src/conf/conf.c +++ b/src/conf/conf.c @@ -252,6 +252,33 @@ int conf_get_i32(const struct conf *conf, const char *name, int32_t *num) } +/** + * Get the numeric floating point value of a configuration item + * + * @param conf Configuration object + * @param name Name of config item key + * @param num Returned numeric value of config item, if present + * + * @return 0 if success, otherwise errorcode + */ +int conf_get_float(const struct conf *conf, const char *name, double *num) +{ + struct pl opt; + int err; + + if (!conf || !name || !num) + return EINVAL; + + err = conf_get(conf, name, &opt); + if (err) + return err; + + *num = pl_float(&opt); + + return 0; +} + + /** * Get the boolean value of a configuration item * diff --git a/test/conf.c b/test/conf.c index 4437b7118..dca33990c 100644 --- a/test/conf.c +++ b/test/conf.c @@ -17,12 +17,14 @@ int test_conf(void) static const char *cfg = "string_val\trattarei\n" "u32_val 42\n" - "i32_val -23\n"; + "i32_val -23\n" + "float_val 1.5\n"; char str[256]; struct conf *conf; struct pl pl; uint32_t u32; int32_t i32; + double fl; int err; err = conf_alloc_buf(&conf, (uint8_t *)cfg, strlen(cfg)); @@ -42,6 +44,10 @@ int test_conf(void) TEST_ERR(err); TEST_EQUALS(-23, i32); + err = conf_get_float(conf, "float_val", &fl); + TEST_ERR(err); + TEST_EQUALS(1.5, fl); + /* Non-existing parameters */ if (0 == conf_get(conf, "rattarei", &pl)) goto badmsg;