/* 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;
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
XBT_DEBUG("Copy cfg set %p", tocopy);
*whereto = NULL;
- xbt_assert0(tocopy, "cannot copy NULL config");
+ xbt_assert(tocopy, "cannot copy NULL config");
xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
if (*cfg == NULL)
*cfg = xbt_cfg_new();
- xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
+ 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) {
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);
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.
* Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
* with type being one of 'string','int', 'peer' or 'double'.
*
- * @fixme: this does not allow to set the description
+ * FIXME: this does not allow to set the description
*/
void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
XBT_DEBUG("Register string '%s'", entry);
tok = strchr(entrycpy, ':');
- xbt_assert2(tok, "Invalid config element descriptor: %s%s",
+ xbt_assert(tok, "Invalid config element descriptor: %s%s",
entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
*(tok++) = '\0';
min = strtol(tok, &tok, 10);
- xbt_assert1(tok, "Invalid minimum in config element descriptor %s",
+ xbt_assert(tok, "Invalid minimum in config element descriptor %s",
entry);
- xbt_assert2(strcmp(tok, "_to_"),
+ xbt_assert(strcmp(tok, "_to_"),
"Invalid config element descriptor : %s%s",
entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
tok += strlen("_to_");
max = strtol(tok, &tok, 10);
- xbt_assert1(tok, "Invalid maximum in config element descriptor %s",
+ xbt_assert(tok, "Invalid maximum in config element descriptor %s",
entry);
- tok++;
- xbt_assert2(*tok == '_',
+ xbt_assert(*tok == '_',
"Invalid config element descriptor: %s%s", entry,
"; Should be <name>:<min nb>_to_<max nb>_<type>");
+ 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_assert2(type < xbt_cfgelm_type_count,
+ xbt_assert(type < xbt_cfgelm_type_count,
"Invalid type in config element descriptor: %s%s", entry,
"; Should be one of 'string', 'int', 'peer' or 'double'.");
char *name;
int size;
- xbt_assert0(cfg, "NULL config set.");
+ xbt_assert(cfg, "NULL config set.");
XBT_DEBUG("Check cfg set %p", cfg);
xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
size = xbt_dynar_length(variable->content);
if (variable->min > size) {
xbt_dict_cursor_free(&cursor);
- THROW4(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Config elem %s needs at least %d %s, but there is only %d values.",
name, variable->min, xbt_cfgelm_type_name[variable->type],
size);
if (variable->max > 0 && variable->max < size) {
xbt_dict_cursor_free(&cursor);
- THROW4(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Config elem %s accepts at most %d %s, but there is %d values.",
name, variable->max, xbt_cfgelm_type_name[variable->type],
size);
res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
if (!res) {
xbt_cfg_help(cfg);
- THROW1(not_found_error, 0,
+ THROWF(not_found_error, 0,
"No registered variable '%s' in this config set", name);
}
- xbt_assert3(type == xbt_cfgelm_any || res->type == type,
+ xbt_assert(type == xbt_cfgelm_any || res->type == type,
"You tried to access to the config element %s as an %s, but its type is %s.",
name,
xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
if (!variable)
- THROW1(not_found_error, 0,
+ THROWF(not_found_error, 0,
"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;
}
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;
TRY {
type = xbt_cfg_get_type(cfg, name);
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category == not_found_error) {
xbt_ex_free(e);
- THROW1(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't set the property '%s' since it's not registered",
name);
}
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);
}
}
* @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) {
- 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;
}
}
*(val++) = '\0';
- if (strcmp(name,"contexts/factory"))
+ if (strncmp(name, "contexts/", strlen("contexts/")))
XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
TRY {
- variable = xbt_dict_get((xbt_dict_t) cfg, name);
- }
- CATCH(e) {
- /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
- name = (char *) (optionlist_cpy - name + options);
+ xbt_cfg_set_as_string(cfg,name,val);
+ } CATCH_ANONYMOUS {
free(optionlist_cpy);
- if (e.category == not_found_error) {
- xbt_ex_free(e);
- THROW1(not_found_error, 0,
- "No registered variable corresponding to '%s'.", name);
- }
RETHROW;
}
+ }
+ free(optionlist_cpy);
+}
- TRY {
- switch (variable->type) {
- case xbt_cfgelm_string:
- xbt_cfg_set_string(cfg, name, val); /* throws */
- break;
-
- 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);
- }
-
- xbt_cfg_set_int(cfg, name, i); /* throws */
- break;
-
- 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;
-
- 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;
-
- default:
- THROW1(unknown_error, 0, "Type of config element %s is not valid.",
- name);
- }
+/** @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
+ */
+
+void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
+ xbt_ex_t e;
+
+ char *ret;
+ volatile xbt_cfgelm_t variable = NULL;
+ int i;
+ double d;
+ char *str, *val;
+
+
+ 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);
}
- CATCH(e) {
- free(optionlist_cpy);
- RETHROW;
+ RETHROW;
+ }
+
+ switch (variable->type) {
+ case xbt_cfgelm_string:
+ xbt_cfg_set_string(cfg, key, value); /* throws */
+ break;
+
+ case xbt_cfgelm_int:
+ i = strtol(value, &ret, 0);
+ if (value == NULL) {
+ xbt_die("Value of option %s not valid. Should be an integer", key);
}
+
+ xbt_cfg_set_int(cfg, key, i); /* throws */
+ break;
+
+ case xbt_cfgelm_double:
+ d = strtod(value, &ret);
+ if (value == NULL) {
+ 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 (val == NULL) {
+ 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
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
if (variable->max == 1) {
- if (variable->cb_rm && xbt_dynar_length(variable->content))
- (*variable->cb_rm) (name, 0);
+ if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
+ variable->cb_rm(name, 0);
xbt_dynar_set(variable->content, 0, &val);
} else {
if (variable->max
&& xbt_dynar_length(variable->content) ==
(unsigned long) variable->max)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot add value %d to the config element %s since it's already full (size=%d)",
val, name, variable->max);
}
if (variable->cb_set)
- (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
+ variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
variable->isdefault = 0;
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
if (variable->max == 1) {
- if (variable->cb_rm && xbt_dynar_length(variable->content))
- (*variable->cb_rm) (name, 0);
+ if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
+ variable->cb_rm(name, 0);
xbt_dynar_set(variable->content, 0, &val);
} else {
if (variable->max
&& xbt_dynar_length(variable->content) == variable->max)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot add value %f to the config element %s since it's already full (size=%d)",
val, name, variable->max);
}
if (variable->cb_set)
- (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
+ variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
variable->isdefault = 0;
}
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_length(variable->content)) {
+ if (!xbt_dynar_is_empty(variable->content)) {
if (variable->cb_rm)
- (*variable->cb_rm) (name, 0);
+ variable->cb_rm(name, 0);
else if (variable->type == xbt_cfgelm_string) {
char *sval = xbt_dynar_get_as(variable->content, 0, char *);
free(sval);
} else {
if (variable->max
&& xbt_dynar_length(variable->content) == variable->max)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot add value %s to the config element %s since it's already full (size=%d)",
name, val, variable->max);
}
if (variable->cb_set)
- (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
+ variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
variable->isdefault = 0;
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
if (variable->max == 1) {
- if (variable->cb_rm && xbt_dynar_length(variable->content))
- (*variable->cb_rm) (name, 0);
+ if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
+ variable->cb_rm(name, 0);
xbt_dynar_set(variable->content, 0, &val);
} else {
if (variable->max
&& xbt_dynar_length(variable->content) == variable->max)
- THROW4(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
peer, port, name, variable->max);
}
if (variable->cb_set)
- (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
+ variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
variable->isdefault = 0;
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
if (xbt_dynar_length(variable->content) == variable->min)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
val, name, variable->min);
xbt_dynar_foreach(variable->content, cpt, seen) {
if (seen == val) {
if (variable->cb_rm)
- (*variable->cb_rm) (name, cpt);
+ variable->cb_rm(name, cpt);
xbt_dynar_cursor_rm(variable->content, &cpt);
return;
}
}
- THROW2(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't remove the value %d of config element %s: value not found.",
val, name);
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
if (xbt_dynar_length(variable->content) == variable->min)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
val, name, variable->min);
if (seen == val) {
xbt_dynar_cursor_rm(variable->content, &cpt);
if (variable->cb_rm)
- (*variable->cb_rm) (name, cpt);
+ variable->cb_rm(name, cpt);
return;
}
}
- THROW2(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't remove the value %f of config element %s: value not found.",
val, name);
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
if (xbt_dynar_length(variable->content) == variable->min)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
name, val, variable->min);
xbt_dynar_foreach(variable->content, cpt, seen) {
if (!strcpy(seen, val)) {
if (variable->cb_rm)
- (*variable->cb_rm) (name, cpt);
+ variable->cb_rm(name, cpt);
xbt_dynar_cursor_rm(variable->content, &cpt);
return;
}
}
- THROW2(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't remove the value %s of config element %s: value not found.",
val, name);
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
if (xbt_dynar_length(variable->content) == variable->min)
- THROW4(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
peer, port, name, variable->min);
xbt_dynar_foreach(variable->content, cpt, seen) {
if (!strcpy(seen->name, peer) && seen->port == port) {
if (variable->cb_rm)
- (*variable->cb_rm) (name, cpt);
+ variable->cb_rm(name, cpt);
xbt_dynar_cursor_rm(variable->content, &cpt);
return;
}
}
- THROW3(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't remove the value %s:%d of config element %s: value not found.",
peer, port, name);
}
variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
if (xbt_dynar_length(variable->content) == variable->min)
- THROW3(mismatch_error, 0,
+ THROWF(mismatch_error, 0,
"Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
pos, name, variable->min);
if (variable->cb_rm)
- (*variable->cb_rm) (name, pos);
+ variable->cb_rm(name, pos);
xbt_dynar_remove_at(variable->content, pos, NULL);
}
TRY {
variable = xbt_dict_get((xbt_dict_t) cfg, name);
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category != not_found_error)
RETHROW;
xbt_ex_free(e);
- THROW1(not_found_error, 0,
+ THROWF(not_found_error, 0,
"Can't empty '%s' since this config element does not exist",
name);
}
unsigned int cpt;
void *ignored;
xbt_dynar_foreach(variable->content, cpt, ignored) {
- (*variable->cb_rm) (name, cpt);
+ variable->cb_rm(name, cpt);
}
}
xbt_dynar_reset(variable->content);
XBT_WARN
("You asked for the first value of the config element '%s', but there is %lu values\n",
name, xbt_dynar_length(variable->content));
- } else if (xbt_dynar_length(variable->content) == 0) {
+ } else if (xbt_dynar_is_empty(variable->content)) {
return NULL;
}
TRY {
variable = xbt_dict_get((xbt_dict_t) cfg, name);
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category == not_found_error) {
xbt_ex_free(e);
- THROW1(not_found_error, 0,
+ THROWF(not_found_error, 0,
"No registered variable %s in this config set", name);
}
RETHROW;
#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");
TRY {
xbt_cfg_set_parse(myset, "color:blue");
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category != not_found_error)
xbt_test_exception(e);
xbt_ex_free(e);