introduce ec_config_count
authorOlivier Matz <zer0@droids-corp.org>
Thu, 15 Nov 2018 19:21:32 +0000 (20:21 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 15 Nov 2018 19:21:39 +0000 (20:21 +0100)
libecoli/ecoli_config.c
libecoli/ecoli_config.h

index 0698f07..66d9232 100644 (file)
@@ -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)
index 9d7c628..6e06a0b 100644 (file)
@@ -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.
  *