Skip to content

Commit

Permalink
conf: add conf_get_float (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
juha-h authored Oct 18, 2024
1 parent 2a9fdae commit b9d11ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/re_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
27 changes: 27 additions & 0 deletions src/conf/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
8 changes: 7 additions & 1 deletion test/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
Expand Down

0 comments on commit b9d11ef

Please sign in to comment.