From 45969caad2bbc2f0adb381c77d47e36c6ae5493c Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 15 Nov 2018 20:21:32 +0100 Subject: [PATCH] introduce ec_config_count --- libecoli/ecoli_config.c | 19 +++++++++++++++++++ libecoli/ecoli_config.h | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/libecoli/ecoli_config.c b/libecoli/ecoli_config.c index 0698f07..66d9232 100644 --- a/libecoli/ecoli_config.c +++ b/libecoli/ecoli_config.c @@ -360,6 +360,25 @@ ec_config_list(void) return value; } +ssize_t ec_config_count(const struct ec_config *config) +{ + const struct ec_config *child; + ssize_t n; + + switch (config->type) { + case EC_CONFIG_TYPE_LIST: + n = 0; + TAILQ_FOREACH(child, &config->list, next) + n++; + return n; + case EC_CONFIG_TYPE_DICT: + // XXX todo + default: + errno = EINVAL; + return -1; + } +} + const struct ec_config_schema * ec_config_schema_lookup(const struct ec_config_schema *schema, const char *key) diff --git a/libecoli/ecoli_config.h b/libecoli/ecoli_config.h index 9d7c628..6e06a0b 100644 --- a/libecoli/ecoli_config.h +++ b/libecoli/ecoli_config.h @@ -265,6 +265,16 @@ int ec_config_list_add(struct ec_config *list, struct ec_config *value); */ int ec_config_list_del(struct ec_config *list, struct ec_config *config); +/** + * Count the number of elements in a list or dict. + * + * @param config + * The configuration that must be a list or a dict. + * @return + * The number of elements, or -1 on error (errno is set). + */ +ssize_t ec_config_count(const struct ec_config *config); + /** * Validate a configuration. * -- 2.39.5