X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_config.h;h=96e47b322cdcc2454effbcee229426e1d1cf276a;hb=41be2fa5bc7520b4d9ba2028aa151ddfac8e62ed;hp=42a71d5104507c9bff2dc6c6c3e7de1374632344;hpb=bc0ad6be8236af765acdee9727d991debcf012d7;p=protos%2Flibecoli.git diff --git a/lib/ecoli_config.h b/lib/ecoli_config.h index 42a71d5..96e47b3 100644 --- a/lib/ecoli_config.h +++ b/lib/ecoli_config.h @@ -5,8 +5,15 @@ #ifndef ECOLI_CONFIG_ #define ECOLI_CONFIG_ +#include #include #include +#include + +#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; @@ -70,10 +77,6 @@ struct ec_config { * 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 */ @@ -107,6 +110,16 @@ void ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema, /* config */ +/** + * Get the type of the configuration. + * + * @param config + * The configuration. + * @return + * The configuration type. + */ +enum ec_config_type ec_config_get_type(const struct ec_config *config); + /** * Create a boolean configuration value. * @@ -164,17 +177,11 @@ struct ec_config *ec_config_node(struct ec_node *node); /** * 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. @@ -193,20 +200,72 @@ struct ec_config *ec_config_list(void); * @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. */ @@ -216,7 +275,7 @@ int ec_config_cmp(const struct ec_config *config1, /** * Get configuration value. */ -struct ec_config *ec_config_get(struct ec_config *config, +struct ec_config *ec_config_dict_get(const struct ec_config *config, const char *key); /** @@ -249,18 +308,6 @@ struct ec_config *ec_config_list_first(struct ec_config *list); 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. * @@ -278,6 +325,17 @@ void ec_config_free(struct ec_config *config); int ec_config_cmp(const struct ec_config *value1, const struct ec_config *value2); +/** + * Duplicate a configuration. + * + * @param config + * The configuration to duplicate. + * @return + * The duplicated configuration, or NULL on error (errno is set). + */ +struct ec_config * +ec_config_dup(const struct ec_config *config); + /** * Dump a configuration. *