From ce310c9a864419205c122f4a22a0959790d94ab3 Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Wed, 6 Nov 2024 15:19:06 +0100 Subject: [PATCH] odict: add odict_pl_add() (#1208) --- include/re_odict.h | 2 ++ src/odict/entry.c | 14 ++++++++++++++ test/odict.c | 33 +++++++++++++++++++++++++++++++++ test/test.c | 1 + test/test.h | 1 + 5 files changed, 51 insertions(+) diff --git a/include/re_odict.h b/include/re_odict.h index e0809c805..bdac0c5fc 100644 --- a/include/re_odict.h +++ b/include/re_odict.h @@ -29,6 +29,8 @@ int odict_debug(struct re_printf *pf, const struct odict *o); int odict_entry_add(struct odict *o, const char *key, int type, ...); +int odict_pl_add(struct odict *od, const char *key, + const struct pl *val); void odict_entry_del(struct odict *o, const char *key); int odict_entry_debug(struct re_printf *pf, const struct odict_entry *e); bool odict_compare(const struct odict *dict1, const struct odict *dict2, diff --git a/src/odict/entry.c b/src/odict/entry.c index c5ce4c7e2..5a62f2f99 100644 --- a/src/odict/entry.c +++ b/src/odict/entry.c @@ -107,6 +107,20 @@ int odict_entry_add(struct odict *o, const char *key, } +int odict_pl_add(struct odict *od, const char *key, + const struct pl *val) +{ + char *str; + int err = pl_strdup(&str, val); + if (err) + return err; + + err = odict_entry_add(od, key, ODICT_STRING, str); + mem_deref(str); + return err; +} + + void odict_entry_del(struct odict *o, const char *key) { mem_deref((struct odict_entry *)odict_lookup(o, key)); diff --git a/test/odict.c b/test/odict.c index ae1e2694d..9d1a13c32 100644 --- a/test/odict.c +++ b/test/odict.c @@ -188,6 +188,39 @@ int test_odict(void) } +int test_odict_pl(void) +{ + struct odict *od = NULL; + const struct odict_entry *e; + int err; + + static struct pl pl=PL("liajsdoiausdoaudoaisudaoisdjal"); + + err = odict_alloc(&od, 64); + if (err) + goto out; + + /* add pl */ + err = odict_pl_add(od, "pl1", &pl); + TEST_ERR(err); + + e = odict_lookup(od, "pl1"); + TEST_ASSERT(e != NULL); + + TEST_STRCMP(pl.p, pl.l, + odict_entry_str(e), str_len(odict_entry_str(e))); + + mem_deref((struct odict_entry *) e); + /* entry should not exist anymore */ + e = (struct odict_entry *)odict_lookup(od, "pl1"); + TEST_ASSERT(e == NULL); + + out: + mem_deref(od); + return err; +} + + int test_odict_array(void) { struct odict *arr = NULL; diff --git a/test/test.c b/test/test.c index cab2eaba0..fcbe2b4e6 100644 --- a/test/test.c +++ b/test/test.c @@ -147,6 +147,7 @@ static const struct test tests[] = { TEST(test_mqueue), TEST(test_odict), TEST(test_odict_array), + TEST(test_odict_pl), TEST(test_pcp), TEST(test_remain), TEST(test_re_assert_se), diff --git a/test/test.h b/test/test.h index aa04d92b3..303b84166 100644 --- a/test/test.h +++ b/test/test.h @@ -252,6 +252,7 @@ int test_net_if(void); int test_net_dst_source_addr_get(void); int test_odict(void); int test_odict_array(void); +int test_odict_pl(void); int test_pcp(void); int test_trice_cand(void); int test_trice_candpair(void);