X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/997eaf52ad0ae1eb34820b135c7ba884b2a17f3c..fabcad3489b65a4fbe2fdcce5c08a6b1be076471:/src/xbt/config.c diff --git a/src/xbt/config.c b/src/xbt/config.c index c149aaa27c..2b352b1569 100644 --- a/src/xbt/config.c +++ b/src/xbt/config.c @@ -33,7 +33,7 @@ typedef struct { /* Allowed type of the variable */ e_xbt_cfgelm_type_t type; int min, max; - int isdefault:1; + unsigned isdefault:1; /* Callbacks */ xbt_cfg_cb_t cb_set; @@ -64,7 +64,7 @@ static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, xbt_cfg_t xbt_cfg_new(void) { - return (xbt_cfg_t) xbt_dict_new(); + return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free); } /** \brief Copy an existing configuration set @@ -213,7 +213,7 @@ xbt_cfg_register(xbt_cfg_t * cfg, *cfg = xbt_cfg_new(); xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer, "type of %s not valid (%d should be between %d and %d)", - name, type, xbt_cfgelm_int, xbt_cfgelm_peer); + name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer); res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name); if (res) { @@ -223,7 +223,7 @@ xbt_cfg_register(xbt_cfg_t * cfg, res = xbt_new(s_xbt_cfgelm_t, 1); XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)", - name, desc, min, max, xbt_cfgelm_type_name[type], type, res, + name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg); res->desc = xbt_strdup(desc); @@ -260,10 +260,10 @@ xbt_cfg_register(xbt_cfg_t * cfg, break; default: - XBT_ERROR("%d is an invalide type code", type); + XBT_ERROR("%d is an invalide type code", (int)type); } - xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free); + xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL); } /** @brief Unregister an element from a config set. @@ -326,7 +326,7 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry) "; Should be :_to__"); tok++; - for (type = 0; + for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++); xbt_assert(type < xbt_cfgelm_type_count, @@ -370,48 +370,37 @@ void xbt_cfg_help(xbt_cfg_t cfg) else printf("Arity: min:%d to max:%d; ", variable->min, variable->max); } - printf("Current value: "); size = xbt_dynar_length(variable->content); + printf("Current value%s: ", (size <= 1 ? "" : "s")); - switch (variable->type) { - int ival; - char *sval; - double dval; - xbt_peer_t hval; + if (size != 1) + printf(size == 0 ? "n/a\n" : "{ "); + for (i = 0; i < size; i++) { + const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n")); - case xbt_cfgelm_int: - for (i = 0; i < size; i++) { - ival = xbt_dynar_get_as(variable->content, i, int); - printf("%s%d\n", (i == 0 ? "" : " "), ival); - } - break; + switch (variable->type) { + case xbt_cfgelm_int: + printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep); + break; - case xbt_cfgelm_double: - for (i = 0; i < size; i++) { - dval = xbt_dynar_get_as(variable->content, i, double); - printf("%s%f\n", (i == 0 ? "" : " "), dval); - } - break; + case xbt_cfgelm_double: + printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep); + break; - case xbt_cfgelm_string: - for (i = 0; i < size; i++) { - sval = xbt_dynar_get_as(variable->content, i, char *); - printf("%s'%s'\n", (i == 0 ? "" : " "), sval); - } - break; + case xbt_cfgelm_string: + printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep); + break; - case xbt_cfgelm_peer: - for (i = 0; i < size; i++) { - hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t); - printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name, - hval->port); + case xbt_cfgelm_peer: { + xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t); + printf("%s:%d%s", hval->name, hval->port, sep); + break; } - break; - default: - printf("Invalid type!!\n"); + default: + printf("Invalid type!!%s", sep); + } } - } xbt_dynar_free(&names); @@ -490,7 +479,7 @@ e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) "Can't get the type of '%s' since this variable does not exist", name); - XBT_INFO("type in variable = %d", variable->type); + XBT_DEBUG("type in variable = %d", (int)variable->type); return variable->type; } @@ -509,7 +498,7 @@ void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) char *str; int i; double d; - e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */ + e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */ xbt_ex_t e; @@ -549,7 +538,7 @@ void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) break; default: - xbt_die("Config element variable %s not valid (type=%d)", name, type); + xbt_die("Config element variable %s not valid (type=%d)", name, (int)type); } } @@ -581,21 +570,14 @@ void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) * @todo This is a crude manual parser, it should be a proper lexer. */ -void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) -{ - xbt_ex_t e; - - int i; - double d; - char *str; +void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) { - volatile xbt_cfgelm_t variable = NULL; char *optionlist_cpy; char *option, *name, *val; int len; - XBT_IN(""); + XBT_IN(); if (!options || !strlen(options)) { /* nothing to do */ return; } @@ -651,89 +633,97 @@ void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) } *(val++) = '\0'; - if (strncmp(name, "contexts/", strlen("contexts/"))) + if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path"))) XBT_INFO("Configuration change: Set '%s' to '%s'", name, val); TRY { - variable = xbt_dict_get((xbt_dict_t) cfg, name); - } - CATCH(e) { - if (e.category == not_found_error) { - xbt_ex_free(e); - TRY { - THROWF(not_found_error, 0, - "No registered variable corresponding to '%s'.", name); - } - TRY_CLEANUP { - /* name points into optionlist_cpy, it cannot be freed before */ - free(optionlist_cpy); - } - CATCH_ANONYMOUS { - RETHROW; - } - } + xbt_cfg_set_as_string(cfg,name,val); + } CATCH_ANONYMOUS { free(optionlist_cpy); RETHROW; } + } + free(optionlist_cpy); +} - TRY { - switch (variable->type) { - case xbt_cfgelm_string: - xbt_cfg_set_string(cfg, name, val); /* throws */ - break; +/** @brief Set the value of a variable, using the string representation of that value + * + * @arg cfg config set to modify + * @arg key name of the variable to modify + * @arg value string representation of the value to set + * + * @return the first char after the parsed value in val + */ - case xbt_cfgelm_int: - i = strtol(val, &val, 0); - if (val == NULL) { - free(optionlist_cpy); - xbt_die("Value of option %s not valid. Should be an integer", name); - } +void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) { + xbt_ex_t e; - xbt_cfg_set_int(cfg, name, i); /* throws */ - break; + char *ret; + volatile xbt_cfgelm_t variable = NULL; + int i; + double d; + char *str, *val; - case xbt_cfgelm_double: - d = strtod(val, &val); - if (val == NULL) { - free(optionlist_cpy); - xbt_die("Value of option %s not valid. Should be a double", name); - } - xbt_cfg_set_double(cfg, name, d); /* throws */ - break; + TRY { + variable = xbt_dict_get((xbt_dict_t) cfg, key); + } + CATCH(e) { + if (e.category == not_found_error) { + xbt_ex_free(e); + THROWF(not_found_error, 0, + "No registered variable corresponding to '%s'.", key); + } + RETHROW; + } - case xbt_cfgelm_peer: - str = val; - val = strchr(val, ':'); - if (!val) { - free(optionlist_cpy); - xbt_die("Value of option %s not valid. Should be an peer (machine:port)", - name); - } - - *(val++) = '\0'; - i = strtol(val, &val, 0); - if (val == NULL) { - free(optionlist_cpy); - xbt_die("Value of option %s not valid. Should be an peer (machine:port)", - name); - } - - xbt_cfg_set_peer(cfg, name, str, i); /* throws */ - break; + switch (variable->type) { + case xbt_cfgelm_string: + xbt_cfg_set_string(cfg, key, value); /* throws */ + break; - default: - THROWF(unknown_error, 0, "Type of config element %s is not valid.", - name); - } + case xbt_cfgelm_int: + i = strtol(value, &ret, 0); + if (ret == value) { + xbt_die("Value of option %s not valid. Should be an integer", key); } - CATCH_ANONYMOUS { - free(optionlist_cpy); - RETHROW; + + xbt_cfg_set_int(cfg, key, i); /* throws */ + break; + + case xbt_cfgelm_double: + d = strtod(value, &ret); + if (ret == value) { + xbt_die("Value of option %s not valid. Should be a double", key); + } + + xbt_cfg_set_double(cfg, key, d); /* throws */ + break; + + case xbt_cfgelm_peer: + val = xbt_strdup(value); + str = val; + val = strchr(val, ':'); + if (!val) { + xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key); + } + + *(val++) = '\0'; + i = strtol(val, &ret, 0); + if (ret == val) { + xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key); } + + xbt_cfg_set_peer(cfg, key, str, i); /* throws */ + free(val); + break; + + default: + THROWF(unknown_error, 0, "Type of config element %s is not valid.", key); + break; } - free(optionlist_cpy); + return ret; } /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet @@ -899,7 +889,7 @@ void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val) variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string); XBT_DEBUG("Variable: %d to %d %s (=%d) @%p", variable->min, variable->max, - xbt_cfgelm_type_name[variable->type], variable->type, variable); + xbt_cfgelm_type_name[variable->type], (int)variable->type, variable); if (variable->max == 1) { if (!xbt_dynar_is_empty(variable->content)) { @@ -1359,8 +1349,7 @@ xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos, #include "xbt.h" #include "xbt/ex.h" -XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg); -XBT_LOG_DEFAULT_CATEGORY(xbt_cfg); +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg); XBT_TEST_SUITE("config", "Configuration support");