EC_LOG_TYPE_REGISTER(config);
-#ifndef EC_COUNT_OF //XXX
-#define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
- ((size_t)(!(sizeof(x) % sizeof(0[x])))))
-#endif
-
static int
__ec_config_dump(FILE *out, const char *key, const struct ec_config *config,
size_t indent);
return NULL;
}
-static int
-ec_config_set_schema(struct ec_config *dict,
- const struct ec_config_schema *schema,
- size_t schema_len)
-{
- if (dict->type != EC_CONFIG_TYPE_DICT) {
- errno = EINVAL;
- goto fail;
- }
-
- if (ec_config_schema_validate(schema, schema_len) < 0)
- goto fail;
-
- dict->schema = schema;
- dict->schema_len = schema_len;
-
- return 0;
-
-fail:
- return -1;
-}
-
struct ec_config *
-ec_config_dict(const struct ec_config_schema *schema,
- size_t schema_len)
+ec_config_dict(void)
{
struct ec_config *value = NULL;
struct ec_keyval *dict = NULL;
value->type = EC_CONFIG_TYPE_DICT;
value->dict = dict;
- if (ec_config_set_schema(value, schema, schema_len) < 0)
- goto fail;
-
return value;
fail:
}
int
-ec_config_validate(const struct ec_config *dict)
+ec_config_validate(const struct ec_config *dict,
+ const struct ec_config_schema *schema,
+ size_t schema_len)
{
- if (dict->type != EC_CONFIG_TYPE_DICT) {
+ if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
errno = EINVAL;
goto fail;
}
- if (dict->schema == NULL || dict->schema_len == 0) {
- errno = ENOENT;
- goto fail;
- }
- if (ec_config_dict_validate(dict->dict, dict->schema,
- dict->schema_len) < 0)
+ if (ec_config_dict_validate(dict->dict, schema, schema_len) < 0)
goto fail;
return 0
}
struct ec_config *
-ec_config_get(struct ec_config *config, const char *key)
+ec_config_get(const struct ec_config *config, const char *key)
{
if (config == NULL)
return NULL;
}
/* value is consumed */
-int ec_config_set(struct ec_config *config, const char *key,
+int ec_config_dict_set(struct ec_config *config, const char *key,
struct ec_config *value)
{
void (*free_cb)(struct ec_config *) = ec_config_free;
return -1;
}
+int ec_config_dict_del(struct ec_config *config, const char *key)
+{
+ if (config == NULL || key == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (config->type != EC_CONFIG_TYPE_DICT) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ return ec_keyval_del(config->dict, key);
+}
+
/* value is consumed */
int
-ec_config_add(struct ec_config *list,
+ec_config_list_add(struct ec_config *list,
struct ec_config *value)
{
- if (list->type != EC_CONFIG_TYPE_LIST) {
+ if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
errno = EINVAL;
goto fail;
}
return -1;
}
-void ec_config_del(struct ec_config *list, struct ec_config *config)
+int ec_config_list_del(struct ec_config *list, struct ec_config *config)
{
+ if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
+ errno = EINVAL;
+ return -1;
+ }
+
TAILQ_REMOVE(&list->list, config, next);
ec_config_free(config);
+ return 0;
}
static int
ec_config_schema_dump(stdout, sch_baseconfig,
EC_COUNT_OF(sch_baseconfig));
- config = ec_config_dict(sch_baseconfig, EC_COUNT_OF(sch_baseconfig));
+ config = ec_config_dict();
if (config == NULL)
goto fail;
- ret = ec_config_set(config, "my_bool", ec_config_bool(true));
+ ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
value = ec_config_get(config, "my_bool");
testres |= EC_TEST_CHECK(
value->boolean == true,
"unexpected boolean value");
- ret = ec_config_set(config, "my_int", ec_config_i64(1234));
+ ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
value = ec_config_get(config, "my_int");
testres |= EC_TEST_CHECK(
"unexpected int value");
testres |= EC_TEST_CHECK(
- ec_config_validate(config) == 0,
+ ec_config_validate(config, sch_baseconfig,
+ EC_COUNT_OF(sch_baseconfig)) == 0,
"cannot validate config\n");
- ret = ec_config_set(config, "my_string", ec_config_string("toto"));
+ ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
value = ec_config_get(config, "my_string");
testres |= EC_TEST_CHECK(
if (list == NULL)
goto fail;
- subconfig = ec_config_dict(sch_dict, EC_COUNT_OF(sch_dict));
+ subconfig = ec_config_dict();
if (subconfig == NULL)
goto fail;
- ret = ec_config_set(subconfig, "my_int", ec_config_i64(1));
+ ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
value = ec_config_get(subconfig, "my_int");
testres |= EC_TEST_CHECK(
value->i64 == 1,
"unexpected int value");
- ret = ec_config_set(subconfig, "my_int2", ec_config_i64(2));
+ ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
value = ec_config_get(subconfig, "my_int2");
testres |= EC_TEST_CHECK(
"unexpected int value");
testres |= EC_TEST_CHECK(
- ec_config_validate(subconfig) == 0,
+ ec_config_validate(subconfig, sch_dict,
+ EC_COUNT_OF(sch_dict)) == 0,
"cannot validate subconfig\n");
- ret = ec_config_add(list, subconfig);
+ ret = ec_config_list_add(list, subconfig);
subconfig = NULL; /* freed */
testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
- subconfig = ec_config_dict(sch_dict, EC_COUNT_OF(sch_dict));
+ subconfig = ec_config_dict();
if (subconfig == NULL)
goto fail;
- ret = ec_config_set(subconfig, "my_int", ec_config_i64(3));
+ ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
value = ec_config_get(subconfig, "my_int");
testres |= EC_TEST_CHECK(
value->i64 == 3,
"unexpected int value");
- ret = ec_config_set(subconfig, "my_int2", ec_config_i64(4));
+ ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
value = ec_config_get(subconfig, "my_int2");
testres |= EC_TEST_CHECK(
"unexpected int value");
testres |= EC_TEST_CHECK(
- ec_config_validate(subconfig) == 0,
+ ec_config_validate(subconfig, sch_dict,
+ EC_COUNT_OF(sch_dict)) == 0,
"cannot validate subconfig\n");
- ret = ec_config_add(list, subconfig);
+ ret = ec_config_list_add(list, subconfig);
subconfig = NULL; /* freed */
testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
- ret = ec_config_set(config, "my_dictlist", list);
+ ret = ec_config_dict_set(config, "my_dictlist", list);
list = NULL;
testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
testres |= EC_TEST_CHECK(
- ec_config_validate(config) == 0,
+ ec_config_validate(config, sch_baseconfig,
+ EC_COUNT_OF(sch_baseconfig)) == 0,
"cannot validate config\n");
list_ = ec_config_get(config, "my_dictlist");
ec_config_dump(stdout, config);
/* remove the first element */
- ec_config_del(list_, ec_config_list_first(list_));
+ ec_config_list_del(list_, ec_config_list_first(list_));
testres |= EC_TEST_CHECK(
- ec_config_validate(config) == 0,
+ ec_config_validate(config, sch_baseconfig,
+ EC_COUNT_OF(sch_baseconfig)) == 0,
"cannot validate config\n");
-#if 0
- value.type = EC_CONFIG_TYPE_NODE;
- value.node = ec_node_clone(node);
- ret = ec_config_set(config, "node", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot set node");
- pvalue = ec_config_get(config, "node");
- testres |= EC_TEST_CHECK(
- pvalue != NULL &&
- ec_config_cmp(pvalue, &value) == 0,
- "unexpected node value");
-
- subconfig = ec_config(dict, EC_COUNT_OF(dict));
- if (subconfig == NULL)
- goto fail;
-
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 4321;
- ret = ec_config_set(subconfig, "int", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
- pvalue = ec_config_get(subconfig, "int");
- testres |= EC_TEST_CHECK(
- pvalue != NULL &&
- ec_config_cmp(pvalue, &value) == 0,
- "unexpected int value");
-
- value.type = EC_CONFIG_TYPE_DICT;
- value.dict = subconfig;
- subconfig = NULL; /* will be freed when freeing config */
- ret = ec_config_set(config, "dict", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot set dict");
- pvalue = ec_config_get(config, "dict");
- testres |= EC_TEST_CHECK(
- pvalue != NULL &&
- ec_config_cmp(pvalue, &value) == 0,
- "unexpected dict value");
-
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 4321;
- pvalue = ec_config_get(
- ec_config_get(config, "dict")->dict, "int");
- testres |= EC_TEST_CHECK(
- pvalue != NULL &&
- ec_config_cmp(pvalue, &value) == 0,
- "unexpected int value");
-
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 1;
- ret = ec_config_add(config, "intlist", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot add int in list");
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 2;
- ret = ec_config_add(config, "intlist", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot add int in list");
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 3;
- ret = ec_config_add(config, "intlist", value);
- testres |= EC_TEST_CHECK(ret == 0, "cannot add int in list");
-
- value.type = EC_CONFIG_TYPE_INT64;
- value.i64 = 4321;
- ret = ec_config_set(config, "invalid", value);
- testres |= EC_TEST_CHECK(ret < 0,
- "should not be able to set invalid key");
- pvalue = ec_config_get(config, "invalid");
- testres |= EC_TEST_CHECK(pvalue == NULL,
- "invalid key returned a value");
-
-#endif
ec_config_dump(stdout, config);
ec_config_free(list);
#ifndef ECOLI_CONFIG_
#define ECOLI_CONFIG_
+#include <sys/queue.h>
#include <stdbool.h>
#include <stdint.h>
+#ifndef EC_COUNT_OF //XXX
+#define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
+ ((size_t)(!(sizeof(x) % sizeof(0[x])))))
+#endif
+
struct ec_config;
struct ec_keyval;
* Next in list, only valid if type is list.
*/
TAILQ_ENTRY(ec_config) next;
-
- /** Associated schema. Only valid if type is dict. */
- const struct ec_config_schema *schema;
- size_t schema_len; /**< Schema length. */
};
/* schema */
/**
* Create a hash table configuration value.
*
- * @param schema
- * Optional pointer to the first element of the schema array. Set
- * it to NULL if no schema should be attached.
- * @param schema_len
- * Length of the schema array. Set to 0 if no schema.
* @return
- * The configuration object containing an empty hash table, or NULL on
+ * A configuration object containing an empty hash table, or NULL on
* error (errno is set).
*/
-struct ec_config *ec_config_dict(const struct ec_config_schema *schema,
- size_t schema_len);
+struct ec_config *ec_config_dict(void);
/**
* Create a list configuration value.
* @param value
* The value configuration to add in the list. The value object
* will be freed when freeing the list object. On error, the
- * value object is freed.
+ * value object is also freed.
* @return
* 0 on success, else -1 (errno is set).
*/
-int ec_config_add(struct ec_config *list, struct ec_config *value);
+int ec_config_list_add(struct ec_config *list, struct ec_config *value);
-void ec_config_free(struct ec_config *config);
+/**
+ * Remove an element from a list.
+ *
+ * The element is freed and should not be accessed.
+ *
+ * @param list
+ * The list configuration.
+ * @param config
+ * The element to remove from the list.
+ * @return
+ * 0 on success, -1 on error (errno is set).
+ */
+int ec_config_list_del(struct ec_config *list, struct ec_config *config);
-int ec_config_validate(const struct ec_config *dict);
+/**
+ * Validate a configuration.
+ *
+ * @param dict
+ * A hash table configuration to validate.
+ * @param schema
+ * Pointer to the first element of the schema array.
+ * @param schema_len
+ * Length of the schema array.
+ * @return
+ * 0 on success, -1 on error (errno is set).
+ */
+int ec_config_validate(const struct ec_config *dict,
+ const struct ec_config_schema *schema,
+ size_t schema_len);
-/* value is consumed */
-int ec_config_set(struct ec_config *dict, const char *key,
+/**
+ * Set a value in a hash table configuration
+ *
+ * @param dict
+ * A hash table configuration to validate.
+ * @param key
+ * The key to update.
+ * @param value
+ * The value to set. The value object will be freed when freeing the
+ * dict object. On error, the value object is also freed.
+ * @return
+ * 0 on success, -1 on error (errno is set).
+ */
+int ec_config_dict_set(struct ec_config *dict, const char *key,
struct ec_config *value);
+/**
+ * Remove an element from a hash table configuration.
+ *
+ * The element is freed and should not be accessed.
+ *
+ * @param dict
+ * A hash table configuration to validate.
+ * @param key
+ * The key of the configuration to delete.
+ * @return
+ * 0 on success, -1 on error (errno is set).
+ */
+int ec_config_dict_del(struct ec_config *config, const char *key);
+
/**
* Compare two configurations.
*/
/**
* Get configuration value.
*/
-struct ec_config *ec_config_get(struct ec_config *config,
+struct ec_config *ec_config_get(const struct ec_config *config,
const char *key);
/**
struct ec_config *
ec_config_list_next(struct ec_config *list, struct ec_config *config);
-/**
- * Remove an element from a list.
- *
- * The element is freed and should not be accessed.
- *
- * @param list
- * The list configuration.
- * @param config
- * The element to remove from the list.
- */
-void ec_config_del(struct ec_config *list, struct ec_config *config);
-
/**
* Free a configuration.
*