1 /* Copyright (c) 2004-2014,2016. The SimGrid Team.
2 * All rights reserved. */
10 #include <xbt/config.h>
11 #include <xbt/config.hpp>
13 #include "xbt/sysdep.h"
16 #include "xbt/dynar.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
23 XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL;
27 // We could define some range/iterator for the num values.
29 void increment(e_xbt_cfgelm_type_t& type)
31 typedef std::underlying_type<e_xbt_cfgelm_type_t>::type underlying_type;
32 type = (e_xbt_cfgelm_type_t) ((underlying_type) type + 1);
37 /* xbt_cfgelm_t: the typedef corresponding to a config variable. */
43 /* Allowed type of the variable */
44 e_xbt_cfgelm_type_t type;
50 /* Advanced callbacks */
51 xbt_cfg_cb_ext_t cb_set_ext;
53 xbt_cfg_cb_free_t cb_set_free;
55 /* actual content (could be an union or something) */
57 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
59 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
61 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
69 /* Internal stuff used in cache to free a variable */
70 static void xbt_cfgelm_free(void *data);
72 /* Retrieve the variable we'll modify */
73 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
75 /*----[ Memory management ]-----------------------------------------------*/
76 /** @brief Constructor
78 * Initialise a config set
80 xbt_cfg_t xbt_cfg_new(void)
82 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
85 /** @brief Destructor */
86 void xbt_cfg_free(xbt_cfg_t * cfg)
88 XBT_DEBUG("Frees cfg set %p", cfg);
89 xbt_dict_free((xbt_dict_t *) cfg);
92 /** @brief Dump a config set for debuging purpose
94 * @param name The name to give to this config set
95 * @param indent what to write at the beginning of each line (right number of spaces)
96 * @param cfg the config set
98 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
100 xbt_dict_t dict = (xbt_dict_t) cfg;
101 xbt_dict_cursor_t cursor = NULL;
102 xbt_cfgelm_t variable = NULL;
111 printf("%s>> Dumping of the config set '%s':\n", indent, name);
113 xbt_dict_foreach(dict, cursor, key, variable) {
114 printf("%s %s:", indent, key);
116 size = xbt_dynar_length(variable->content);
117 printf ("%s. Actual size=%d. postset=%p\n",
118 xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
120 switch (variable->type) {
122 for (i = 0; i < size; i++) {
123 ival = xbt_dynar_get_as(variable->content, i, int);
124 printf("%s %d\n", indent, ival);
127 case xbt_cfgelm_double:
128 for (i = 0; i < size; i++) {
129 dval = xbt_dynar_get_as(variable->content, i, double);
130 printf("%s %f\n", indent, dval);
133 case xbt_cfgelm_string:
134 for (i = 0; i < size; i++) {
135 sval = xbt_dynar_get_as(variable->content, i, char *);
136 printf("%s %s\n", indent, sval);
139 case xbt_cfgelm_boolean:
140 for (i = 0; i < size; i++) {
141 ival = xbt_dynar_get_as(variable->content, i, int);
142 printf("%s %d\n", indent, ival);
145 case xbt_cfgelm_alias:
149 printf("%s Invalid type!!\n", indent);
155 printf("%s<< End of the config set '%s'\n", indent, name);
158 xbt_dict_cursor_free(&cursor);
162 * free an config element
164 void xbt_cfgelm_free(void *data)
166 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
168 XBT_DEBUG("Frees cfgelm %p", c);
172 c->cb_set_free(c->cb_set_data);
174 if (c->type != xbt_cfgelm_alias)
175 xbt_dynar_free(&(c->content));
179 /*----[ Registering stuff ]-----------------------------------------------*/
180 /** @brief Register an element within a config set
182 * @param cfg the config set
183 * @param name the name of the config element
184 * @param desc a description for this item (used by xbt_cfg_help())
185 * @param type the type of the config element
186 * @param cb_set callback function called when a value is set
188 static void xbt_cfg_register(
189 xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type,
191 xbt_cfg_cb_ext_t cb_set_ext, void* cb_set_data, xbt_cfg_cb_free_t cb_set_free)
194 *cfg = xbt_cfg_new();
195 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
196 "type of %s not valid (%d should be between %d and %d)",
197 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
199 xbt_cfgelm_t res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
200 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
202 res = xbt_new(s_xbt_cfgelm_t, 1);
203 XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
204 name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
206 res->desc = xbt_strdup(desc);
208 res->cb_set = cb_set;
209 res->cb_set_ext = cb_set_ext;
210 res->cb_set_data = cb_set_data;
211 res->cb_set_free = cb_set_free;
216 res->content = xbt_dynar_new(sizeof(int), NULL);
218 case xbt_cfgelm_double:
219 res->content = xbt_dynar_new(sizeof(double), NULL);
221 case xbt_cfgelm_string:
222 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
224 case xbt_cfgelm_boolean:
225 res->content = xbt_dynar_new(sizeof(int), NULL);
228 XBT_ERROR("%d is an invalid type code", (int)type);
231 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
236 void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc){
237 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set, NULL, NULL, NULL);
238 xbt_cfg_setdefault_double(name, default_value);
240 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) {
241 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set, NULL, NULL, NULL);
242 xbt_cfg_setdefault_int(name, default_value);
244 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc){
245 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set, NULL, NULL, NULL);
246 xbt_cfg_setdefault_string(name, default_value);
248 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc){
249 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set, NULL, NULL, NULL);
250 xbt_cfg_setdefault_boolean(name, default_value);
253 /** Register a config with an extended callback
255 * @param name Name of the flag
256 * @param desc Description of the flag
257 * @param type Type of the flag
258 * @param cb Extended callback
259 * @param data Data associated with the callback
260 * @param data_free Function used to free the callback data (or NULL)
262 void xbt_cfg_register_ext(const char *name, const char *desc, e_xbt_cfgelm_type_t type,
263 xbt_cfg_cb_ext_t cb, void* data, xbt_cfg_cb_free_t data_free)
265 xbt_cfg_register(&simgrid_config, name, desc, type, NULL, cb, data, data_free);
268 void xbt_cfg_register_alias(const char *newname, const char *oldname)
270 if (simgrid_config == NULL)
271 simgrid_config = xbt_cfg_new();
273 xbt_cfgelm_t res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) simgrid_config, oldname);
274 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
276 res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) simgrid_config, newname);
277 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
279 res = xbt_new0(s_xbt_cfgelm_t, 1);
280 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
282 res->desc = bprintf("Deprecated alias for %s",newname);
283 res->type = xbt_cfgelm_alias;
285 res->content = (xbt_dynar_t)newname;
287 xbt_dict_set((xbt_dict_t) simgrid_config, oldname, res, NULL);
291 * @brief Parse a string and register the stuff described.
293 * @param cfg the config set
294 * @param entry a string describing the element to register
296 * The string may consist in several variable descriptions separated by a space.
297 * Each of them must use the following syntax: \<name\>:\<type\>
298 * with type being one of 'string','int','bool' or 'double'.
300 * Note that this does not allow to set the description, so you should prefer the other interface
302 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
304 char *entrycpy = xbt_strdup(entry);
307 e_xbt_cfgelm_type_t type;
308 XBT_DEBUG("Register string '%s'", entry);
310 tok = strchr(entrycpy, ':');
311 xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
314 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); increment(type));
315 xbt_assert(type < xbt_cfgelm_type_count,
316 "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
318 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, NULL, NULL, NULL);
320 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
323 /** @brief Displays the declared aliases and their description */
324 void xbt_cfg_aliases(void)
326 xbt_dict_cursor_t dict_cursor;
327 unsigned int dynar_cursor;
328 xbt_cfgelm_t variable;
330 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
332 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
333 xbt_dynar_push(names, &name);
334 xbt_dynar_sort_strings(names);
336 xbt_dynar_foreach(names, dynar_cursor, name) {
337 variable = (xbt_cfgelm_t) xbt_dict_get((xbt_dict_t )simgrid_config, name);
339 if (variable->type == xbt_cfgelm_alias)
340 printf(" %s: %s\n", name, variable->desc);
344 /** @brief Displays the declared options and their description */
345 void xbt_cfg_help(void)
347 xbt_dict_cursor_t dict_cursor;
348 unsigned int dynar_cursor;
349 xbt_cfgelm_t variable;
351 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
353 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
354 xbt_dynar_push(names, &name);
355 xbt_dynar_sort_strings(names);
357 xbt_dynar_foreach(names, dynar_cursor, name) {
359 variable = (xbt_cfgelm_t) xbt_dict_get((xbt_dict_t )simgrid_config, name);
360 if (variable->type == xbt_cfgelm_alias)
363 printf(" %s: %s\n", name, variable->desc);
364 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
365 size = xbt_dynar_length(variable->content);
366 printf("Current value: ");
369 printf(size == 0 ? "n/a\n" : "{ ");
370 for (int i = 0; i < size; i++) {
371 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
373 switch (variable->type) {
375 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
377 case xbt_cfgelm_double:
378 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
380 case xbt_cfgelm_string:
381 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
383 case xbt_cfgelm_boolean: {
384 int b = xbt_dynar_get_as(variable->content, i, int);
385 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
386 if (b == 0 || b == 1)
387 printf("'%s'%s", bs, sep);
389 printf("'%s/%d'%s", bs, b, sep);
393 printf("Invalid type!!%s", sep);
398 xbt_dynar_free(&names);
401 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
403 xbt_cfgelm_t res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) cfg, name);
405 // The user used the old name. Switch to the new one after a short warning
406 while (res && res->type == xbt_cfgelm_alias) {
407 const char* newname = (const char *)res->content;
408 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
409 res = xbt_cfgelm_get(cfg, newname, type);
415 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
418 xbt_assert(type == xbt_cfgelm_any || res->type == type,
419 "You tried to access to the config element %s as an %s, but its type is %s.",
420 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
424 /** @brief Get the type of this variable in that configuration set
426 * @param cfg the config set
427 * @param name the name of the element
429 * @return the type of the given element
431 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
433 xbt_cfgelm_t variable = NULL;
435 variable = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) cfg, name);
437 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
439 XBT_DEBUG("type in variable = %d", (int)variable->type);
440 return variable->type;
443 /*----[ Setting ]---------------------------------------------------------*/
444 /** @brief va_args version of xbt_cfg_set
446 * @param cfg config set to fill
447 * @param name variable name
448 * @param pa variable value
450 * Add some values to the config set.
452 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
457 e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
462 type = xbt_cfg_get_type(cfg, name);
465 if (e.category == not_found_error) {
467 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
473 case xbt_cfgelm_string:
474 str = va_arg(pa, char *);
475 xbt_cfg_set_string(name, str);
479 xbt_cfg_set_int(name, i);
481 case xbt_cfgelm_double:
482 d = va_arg(pa, double);
483 xbt_cfg_set_double(name, d);
485 case xbt_cfgelm_boolean:
486 str = va_arg(pa, char *);
487 xbt_cfg_set_boolean(name, str);
490 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
494 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
496 * @param cfg config set to fill
497 * @param name variable name
498 * @param ... variable value
500 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
505 xbt_cfg_set_vargs(cfg, name, pa);
509 /** @brief Add values parsed from a string into a config set
511 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
512 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
513 * already registered variable, and [value] conforms to the data type under which this variable was registered.
515 * @todo This is a crude manual parser, it should be a proper lexer.
517 void xbt_cfg_set_parse(const char *options)
519 if (!options || !strlen(options)) { /* nothing to do */
522 char *optionlist_cpy = xbt_strdup(options);
524 XBT_DEBUG("List to parse and set:'%s'", options);
525 char *option = optionlist_cpy;
526 while (1) { /* breaks in the code */
530 int len = strlen(name);
531 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
534 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
535 XBT_DEBUG("Take %c.", *option);
538 if (option - name == len) {
539 XBT_DEBUG("Boundary=EOL");
540 option = NULL; /* don't do next iteration */
542 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
543 /* Pass the following blank chars */
545 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
546 /* fprintf(stderr,"Ignore a blank char.\n"); */
549 if (option - name == len - 1)
550 option = NULL; /* don't do next iteration */
552 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
554 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
559 char *val = strchr(name, ':');
560 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
561 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
564 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
565 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
568 xbt_cfg_set_as_string(name,val);
570 free(optionlist_cpy);
574 free(optionlist_cpy);
577 /** @brief Set the value of a variable, using the string representation of that value
579 * @param key name of the variable to modify
580 * @param value string representation of the value to set
582 * @return the first char after the parsed value in val
585 void *xbt_cfg_set_as_string(const char *key, const char *value) {
589 volatile xbt_cfgelm_t variable = NULL;
594 while (variable == NULL) {
595 variable = (xbt_cfgelm_t) xbt_dict_get((xbt_dict_t) simgrid_config, key);
596 if (variable->type == xbt_cfgelm_alias) {
597 const char *newname = (const char*)variable->content;
598 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
604 if (e.category == not_found_error) {
606 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
611 switch (variable->type) {
612 case xbt_cfgelm_string:
613 xbt_cfg_set_string(key, value); /* throws */
616 i = strtol(value, &ret, 0);
618 xbt_die("Value of option %s not valid. Should be an integer", key);
620 xbt_cfg_set_int(key, i); /* throws */
622 case xbt_cfgelm_double:
623 d = strtod(value, &ret);
625 xbt_die("Value of option %s not valid. Should be a double", key);
627 xbt_cfg_set_double(key, d); /* throws */
629 case xbt_cfgelm_boolean:
630 xbt_cfg_set_boolean(key, value); /* throws */
631 ret = (char *)value + strlen(value);
634 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
640 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
642 * This is useful to change the default value of a variable while allowing
643 * users to override it with command line arguments
645 void xbt_cfg_setdefault_int(const char *name, int val)
647 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
649 if (variable->isdefault){
650 xbt_cfg_set_int(name, val);
651 variable->isdefault = 1;
653 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
656 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
658 * This is useful to change the default value of a variable while allowing
659 * users to override it with command line arguments
661 void xbt_cfg_setdefault_double(const char *name, double val)
663 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
665 if (variable->isdefault) {
666 xbt_cfg_set_double(name, val);
667 variable->isdefault = 1;
669 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
672 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
674 * This is useful to change the default value of a variable while allowing
675 * users to override it with command line arguments
677 void xbt_cfg_setdefault_string(const char *name, const char *val)
679 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
681 if (variable->isdefault){
682 xbt_cfg_set_string(name, val);
683 variable->isdefault = 1;
685 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
688 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
690 * This is useful to change the default value of a variable while allowing
691 * users to override it with command line arguments
693 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
695 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
697 if (variable->isdefault){
698 xbt_cfg_set_boolean(name, val);
699 variable->isdefault = 1;
702 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
705 /** @brief Set an integer value to \a name within \a cfg
707 * @param name the name of the variable
708 * @param val the value of the variable
710 void xbt_cfg_set_int(const char *name, int val)
712 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
714 xbt_dynar_set(variable->content, 0, &val);
716 if (variable->cb_set)
717 variable->cb_set(name);
718 if (variable->cb_set_ext)
719 variable->cb_set_ext(name, variable->cb_set_data);
720 variable->isdefault = 0;
723 /** @brief Set or add a double value to \a name within \a cfg
725 * @param name the name of the variable
726 * @param val the double to set
728 void xbt_cfg_set_double(const char *name, double val)
730 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
732 xbt_dynar_set(variable->content, 0, &val);
734 if (variable->cb_set)
735 variable->cb_set(name);
736 if (variable->cb_set_ext)
737 variable->cb_set_ext(name, variable->cb_set_data);
738 variable->isdefault = 0;
741 /** @brief Set or add a string value to \a name within \a cfg
743 * @param cfg the config set
744 * @param name the name of the variable
745 * @param val the value to be added
748 void xbt_cfg_set_string(const char *name, const char *val)
750 char *newval = xbt_strdup(val);
751 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
753 if (!xbt_dynar_is_empty(variable->content)) {
754 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
758 xbt_dynar_set(variable->content, 0, &newval);
760 if (variable->cb_set)
761 variable->cb_set(name);
762 if (variable->cb_set_ext)
763 variable->cb_set_ext(name, variable->cb_set_data);
764 variable->isdefault = 0;
767 /** @brief Set or add a boolean value to \a name within \a cfg
769 * @param name the name of the variable
770 * @param val the value of the variable
772 void xbt_cfg_set_boolean(const char *name, const char *val)
775 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
777 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
778 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
782 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
787 xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
788 xbt_dynar_set(variable->content, 0, &bval);
790 if (variable->cb_set)
791 variable->cb_set(name);
792 if (variable->cb_set_ext)
793 variable->cb_set_ext(name, variable->cb_set_data);
794 variable->isdefault = 0;
798 /* Say if the value is the default value */
799 int xbt_cfg_is_default_value(const char *name)
801 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
802 return variable->isdefault;
805 /*----[ Getting ]---------------------------------------------------------*/
806 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
808 * @param name the name of the variable
810 * Returns the first value from the config set under the given name.
811 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
813 int xbt_cfg_get_int(const char *name)
815 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
817 if (xbt_dynar_length(variable->content) > 1) {
818 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
819 name, xbt_dynar_length(variable->content));
822 return xbt_dynar_get_as(variable->content, 0, int);
825 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
827 * @param cfg the config set
828 * @param name the name of the variable
830 * Returns the first value from the config set under the given name.
831 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
833 double xbt_cfg_get_double(const char *name)
835 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
837 if (xbt_dynar_length(variable->content) > 1) {
838 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
839 name, xbt_dynar_length(variable->content));
842 return xbt_dynar_get_as(variable->content, 0, double);
845 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
847 * @param cfg the config set
848 * @param name the name of the variable
850 * Returns the first value from the config set under the given name.
851 * If there is more than one value, it will issue a warning. Consider using
852 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
854 * \warning the returned value is the actual content of the config set
856 char *xbt_cfg_get_string(const char *name)
858 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
860 if (xbt_dynar_length(variable->content) > 1) {
861 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
862 name, xbt_dynar_length(variable->content));
863 } else if (xbt_dynar_is_empty(variable->content)) {
867 return xbt_dynar_get_as(variable->content, 0, char *);
870 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
872 * @param cfg the config set
873 * @param name the name of the variable
875 * Returns the first value from the config set under the given name.
876 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
878 int xbt_cfg_get_boolean(const char *name)
880 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
882 if (xbt_dynar_length(variable->content) > 1) {
883 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
884 name, xbt_dynar_length(variable->content));
887 return xbt_dynar_get_as(variable->content, 0, int);
893 static void callCallback(const char* name, void* data)
895 (*(std::function<void(const char*)>*) data)(name);
898 static void freeCallback(void* data)
900 delete (std::function<void(const char*)>*) data;
903 void registerConfig(const char* name, const char* description,
904 e_xbt_cfgelm_type_t type,
905 std::function<void(const char*)> callback)
907 std::function<void(const char*)>* code
908 = new std::function<void(const char*)>(std::move(callback));
909 xbt_cfg_register_ext(name, description, type,
910 callCallback, code, freeCallback);
920 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
922 XBT_TEST_SUITE("config", "Configuration support");
924 static xbt_cfg_t make_set()
926 xbt_cfg_t set = NULL;
928 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
929 xbt_cfg_register_str(&set, "speed:int");
930 xbt_cfg_register_str(&set, "peername:string");
931 xbt_cfg_register_str(&set, "user:string");
934 } /* end_of_make_set */
936 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
938 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
940 simgrid_config = make_set();
941 xbt_test_add("Alloc and free a config set");
942 xbt_cfg_set_parse("peername:veloce user:bidule");
943 xbt_cfg_free(&simgrid_config);
946 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
948 simgrid_config = make_set();
949 xbt_test_add("Get a single value");
951 /* get_single_value */
954 xbt_cfg_set_parse("peername:toto:42 speed:42");
955 ival = xbt_cfg_get_int("speed");
957 xbt_test_fail("Speed value = %d, I expected 42", ival);
960 xbt_test_add("Access to a non-existant entry");
965 xbt_cfg_set_parse("color:blue");
967 if (e.category != not_found_error)
968 xbt_test_exception(e);
972 xbt_cfg_free(&simgrid_config);
974 #endif /* SIMGRID_TEST */