#include <ecoli_strvec.h>
#include <ecoli_keyval.h>
#include <ecoli_log.h>
+#include <ecoli_config.h>
#include <ecoli_test.h>
#include <ecoli_node.h>
return type;
}
+ errno = ENOENT;
return NULL;
}
int ec_node_type_register(struct ec_node_type *type)
{
- if (ec_node_type_lookup(type->name) != NULL)
- return -EEXIST;
- if (type->size < sizeof(struct ec_node))
- return -EINVAL;
+ EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
+
+ if (ec_node_type_lookup(type->name) != NULL) {
+ errno = EEXIST;
+ return -1;
+ }
TAILQ_INSERT_TAIL(&node_type_list, type, next);
if (node->type != NULL && node->type->free_priv != NULL)
node->type->free_priv(node);
- ec_free(node->children);
ec_free(node->id);
ec_free(node->desc);
ec_keyval_free(node->attrs);
+ ec_config_free(node->config);
ec_free(node);
}
size_t ec_node_get_children_count(const struct ec_node *node)
{
- return node->n_children;
+ if (node->type->get_children_count == NULL)
+ return 0;
+ return node->type->get_children_count(node);
}
struct ec_node *
ec_node_get_child(const struct ec_node *node, size_t i)
{
- if (i >= ec_node_get_children_count(node))
+ if (node->type->get_child == NULL)
return NULL;
- return node->children[i];
+ return node->type->get_child(node, i);
}
-int ec_node_add_child(struct ec_node *node, struct ec_node *child)
+int
+ec_node_set_config(struct ec_node *node, struct ec_config *config)
{
- struct ec_node **children = NULL;
- size_t n;
-
- if (node == NULL || child == NULL) {
+ if (node->type->schema == NULL) {
errno = EINVAL;
goto fail;
}
-
- n = node->n_children;
- children = ec_realloc(node->children,
- (n + 1) * sizeof(child));
- if (children == NULL)
+ if (ec_config_validate(config, node->type->schema,
+ node->type->schema_len) < 0)
goto fail;
+ if (node->type->set_config != NULL) {
+ if (node->type->set_config(node, config) < 0)
+ goto fail;
+ }
- children[n] = child;
- node->children = children;
- node->n_children = n + 1;
+ ec_config_free(node->config);
+ node->config = config;
return 0;
fail:
- ec_free(children);
+ ec_config_free(config);
return -1;
}
-#if 0 /* later */
-int ec_node_del_child(struct ec_node *node, struct ec_node *child)
+const struct ec_config *ec_node_get_config(struct ec_node *node)
{
- size_t i, n;
-
- if (node == NULL || child == NULL)
- goto fail;
-
- n = node->n_children;
- for (i = 0; i < n; i++) {
- if (node->children[i] != child)
- continue;
- memcpy(&node->children[i], &node->children[i+1],
- (n - i - 1) * sizeof(child));
- return 0;
- }
-
-fail:
- errno = EINVAL;
- return -1;
+ return node->config;
}
-#endif
struct ec_node *ec_node_find(struct ec_node *node, const char *id)
{
if (id != NULL && node_id != NULL && !strcmp(node_id, id))
return node;
- n = node->n_children;
+ n = ec_node_get_children_count(node);
for (i = 0; i < n; i++) {
- child = node->children[i];
+ child = ec_node_get_child(node, i);
ret = ec_node_find(child, id);
if (ret != NULL)
return ret;
fprintf(out, "%*s" "type=%s id=%s %p\n",
(int)indent * 4, "", typename, id, node);
- n = node->n_children;
+ n = ec_node_get_children_count(node);
for (i = 0; i < n; i++) {
- child = node->children[i];
+ child = ec_node_get_child(node, i);
__ec_node_dump(out, child, indent + 1);
}
}
errno = EINVAL;
return -1;
}
+
return 0;
}
fclose(f);
free(buf);
+ assert(errno != 0);
return -1;
}
/* LCOV_EXCL_STOP */