Skip to content

Commit

Permalink
conf,test: add conf_get_i32 (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Mar 3, 2024
1 parent 7d1467c commit aefb21e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/re_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int conf_get(const struct conf *conf, const char *name, struct pl *pl);
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_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 @@ -225,6 +225,33 @@ int conf_get_u32(const struct conf *conf, const char *name, uint32_t *num)
}


/**
* Get the numeric signed 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_i32(const struct conf *conf, const char *name, int32_t *num)
{
struct pl pl;
int err;

if (!conf || !name || !num)
return EINVAL;

err = conf_get(conf, name, &pl);
if (err)
return err;

*num = pl_i32(&pl);

return 0;
}


/**
* Get the boolean value of a configuration item
*
Expand Down
19 changes: 14 additions & 5 deletions test/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@
#include <re.h>
#include "test.h"

#define DEBUG_MODULE "test_conf"
#define DEBUG_LEVEL 5
#include <re_dbg.h>


int test_conf(void)
{
static const char *cfg =
"string_val\trattarei\n"
"u32_val 42\n";
"u32_val 42\n"
"i32_val -23\n";
char str[256];
struct conf *conf;
struct pl pl;
uint32_t u32;
int32_t i32;
int err;

err = conf_alloc_buf(&conf, (uint8_t *)cfg, strlen(cfg));
if (err)
return err;

err = conf_get_str(conf, "string_val", str, sizeof(str));
if (err)
goto out;
TEST_ERR(err);
if (strcmp(str, "rattarei"))
goto badmsg;

err = conf_get_u32(conf, "u32_val", &u32);
if (u32 != 42)
goto badmsg;
TEST_ERR(err);
TEST_EQUALS(42, u32);

err = conf_get_i32(conf, "i32_val", &i32);
TEST_ERR(err);
TEST_EQUALS(-23, i32);

/* Non-existing parameters */
if (0 == conf_get(conf, "rattarei", &pl))
Expand Down

0 comments on commit aefb21e

Please sign in to comment.