From 8945b7bf33e33ddd82229be50dd134907ac93fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 11 Jun 2024 10:15:03 +0200 Subject: [PATCH] core: add function hdata_count --- src/core/core-hdata.c | 21 +++++++++++++++++++++ src/core/core-hdata.h | 1 + tests/unit/core/test-core-hdata.cpp | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/core/core-hdata.c b/src/core/core-hdata.c index c3f4d3ad3..10e9720d1 100644 --- a/src/core/core-hdata.c +++ b/src/core/core-hdata.c @@ -656,6 +656,27 @@ end: return ret_pointer; } +/* + * Returns number of item in this hdata, starting at "pointer". + */ + +int +hdata_count (struct t_hdata *hdata, void *pointer) +{ + int count; + + if (!hdata || !pointer) + return 0; + + count = 0; + while (pointer) + { + count++; + pointer = hdata_move (hdata, pointer, 1); + } + return count; +} + /* * Extracts index from name of a variable. * diff --git a/src/core/core-hdata.h b/src/core/core-hdata.h index e25514807..fd953c69d 100644 --- a/src/core/core-hdata.h +++ b/src/core/core-hdata.h @@ -122,6 +122,7 @@ extern void *hdata_search (struct t_hdata *hdata, void *pointer, const char *search, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options, int move); +extern int hdata_count (struct t_hdata *hdata, void *pointer); extern void hdata_get_index_and_name (const char *name, int *index, const char **ptr_name); extern char hdata_char (struct t_hdata *hdata, void *pointer, diff --git a/tests/unit/core/test-core-hdata.cpp b/tests/unit/core/test-core-hdata.cpp index d6f048e16..597f50748 100644 --- a/tests/unit/core/test-core-hdata.cpp +++ b/tests/unit/core/test-core-hdata.cpp @@ -1728,6 +1728,21 @@ TEST(CoreHdataWithList, Search) hashtable_free (extra_vars); } +/* + * Tests functions: + * hdata_count + */ + +TEST(CoreHdataWithList, Count) +{ + LONGS_EQUAL(0, hdata_count (NULL, NULL)); + LONGS_EQUAL(0, hdata_count (ptr_hdata, NULL)); + LONGS_EQUAL(0, hdata_count (NULL, items)); + + LONGS_EQUAL(2, hdata_count (ptr_hdata, items)); + LONGS_EQUAL(1, hdata_count (ptr_hdata, ptr_item2)); +} + /* * Tests functions: * hdata_get_index_and_name