1 /* config - Dictionnary where the type of each variable is provided. */
3 /* This is useful to build named structs, like option or property sets. */
5 /* Copyright (c) 2004-2014. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/sysdep.h"
15 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
24 xbt_cfg_t simgrid_config = NULL;
26 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
28 Both data and DTD are mixed, but fixing it now would prevent me to ever defend my thesis. */
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
42 /* actual content (could be an union or something) */
44 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
46 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
48 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
56 /* Internal stuff used in cache to free a variable */
57 static void xbt_cfgelm_free(void *data);
59 /* Retrieve the variable we'll modify */
60 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
62 /*----[ Memory management ]-----------------------------------------------*/
63 /** @brief Constructor
65 * Initialise a config set
67 xbt_cfg_t xbt_cfg_new(void)
69 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
72 /** \brief Copy an existing configuration set
74 * @param whereto the config set to be created
75 * @param tocopy the source data
77 * This only copy the registrations, not the actual content
79 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
81 xbt_dict_cursor_t cursor = NULL;
82 xbt_cfgelm_t variable = NULL;
85 XBT_DEBUG("Copy cfg set %p", tocopy);
87 xbt_assert(tocopy, "cannot copy NULL config");
89 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
90 xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->max, variable->cb_set);
94 /** @brief Destructor */
95 void xbt_cfg_free(xbt_cfg_t * cfg)
97 XBT_DEBUG("Frees cfg set %p", cfg);
98 xbt_dict_free((xbt_dict_t *) cfg);
101 /** @brief Dump a config set for debuging purpose
103 * @param name The name to give to this config set
104 * @param indent what to write at the beginning of each line (right number of spaces)
105 * @param cfg the config set
107 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
109 xbt_dict_t dict = (xbt_dict_t) cfg;
110 xbt_dict_cursor_t cursor = NULL;
111 xbt_cfgelm_t variable = NULL;
120 printf("%s>> Dumping of the config set '%s':\n", indent, name);
122 xbt_dict_foreach(dict, cursor, key, variable) {
123 printf("%s %s:", indent, key);
125 size = xbt_dynar_length(variable->content);
126 printf ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
127 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
129 switch (variable->type) {
131 for (i = 0; i < size; i++) {
132 ival = xbt_dynar_get_as(variable->content, i, int);
133 printf("%s %d\n", indent, ival);
136 case xbt_cfgelm_double:
137 for (i = 0; i < size; i++) {
138 dval = xbt_dynar_get_as(variable->content, i, double);
139 printf("%s %f\n", indent, dval);
142 case xbt_cfgelm_string:
143 for (i = 0; i < size; i++) {
144 sval = xbt_dynar_get_as(variable->content, i, char *);
145 printf("%s %s\n", indent, sval);
148 case xbt_cfgelm_boolean:
149 for (i = 0; i < size; i++) {
150 ival = xbt_dynar_get_as(variable->content, i, int);
151 printf("%s %d\n", indent, ival);
154 case xbt_cfgelm_alias:
158 printf("%s Invalid type!!\n", indent);
164 printf("%s<< End of the config set '%s'\n", indent, name);
167 xbt_dict_cursor_free(&cursor);
171 * free an config element
173 void xbt_cfgelm_free(void *data)
175 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
177 XBT_DEBUG("Frees cfgelm %p", c);
181 if (c->type != xbt_cfgelm_alias)
182 xbt_dynar_free(&(c->content));
186 /*----[ Registering stuff ]-----------------------------------------------*/
187 /** @brief Register an element within a config set
189 * @param cfg the config set
190 * @param name the name of the config element
191 * @param desc a description for this item (used by xbt_cfg_help())
192 * @param type the type of the config element
193 * @param min the minimum number of values for this config element
194 * @param max the maximum number of values for this config element
195 * @param cb_set callback function called when a value is set
197 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min,
198 int max, xbt_cfg_cb_t cb_set)
203 *cfg = xbt_cfg_new();
204 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
205 "type of %s not valid (%d should be between %d and %d)",
206 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
207 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
208 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
210 res = xbt_new(s_xbt_cfgelm_t, 1);
211 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
212 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
214 res->desc = xbt_strdup(desc);
218 res->cb_set = cb_set;
223 res->content = xbt_dynar_new(sizeof(int), NULL);
225 case xbt_cfgelm_double:
226 res->content = xbt_dynar_new(sizeof(double), NULL);
228 case xbt_cfgelm_string:
229 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
231 case xbt_cfgelm_boolean:
232 res->content = xbt_dynar_new(sizeof(int), NULL);
235 XBT_ERROR("%d is an invalid type code", (int)type);
238 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
241 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
242 xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_double,1,1,cb_set);
243 xbt_cfg_setdefault_double(name, default_value);
245 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
246 xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_int,1,1,cb_set);
247 xbt_cfg_setdefault_int(name, default_value);
249 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
250 xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_string,1,1,cb_set);
251 xbt_cfg_setdefault_string(name, default_value);
253 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
254 xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_boolean,1,1,cb_set);
255 xbt_cfg_setdefault_boolean(name, default_value);
258 void xbt_cfg_register_alias(const char *newname, const char *oldname)
260 if (simgrid_config == NULL)
261 simgrid_config = xbt_cfg_new();
263 xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
264 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
266 res = xbt_dict_get_or_null(simgrid_config, newname);
267 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
269 res = xbt_new0(s_xbt_cfgelm_t, 1);
270 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
272 res->desc = bprintf("Deprecated alias for %s",newname);
273 res->type = xbt_cfgelm_alias;
277 res->content = (xbt_dynar_t)newname;
279 xbt_dict_set(simgrid_config, oldname, res, NULL);
282 /** @brief Unregister an element from a config set.
284 * @param cfg the config set
285 * @param name the name of the element to be freed
287 * Note that it removes both the description and the actual content.
288 * Throws not_found when no such element exists.
290 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
292 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
293 xbt_dict_remove((xbt_dict_t) cfg, name);
297 * @brief Parse a string and register the stuff described.
299 * @param cfg the config set
300 * @param entry a string describing the element to register
302 * The string may consist in several variable descriptions separated by a space.
303 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
304 * with type being one of 'string','int' or 'double'.
306 * Note that this does not allow to set the description, so you should prefer the other interface
308 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
310 char *entrycpy = xbt_strdup(entry);
314 e_xbt_cfgelm_type_t type;
315 XBT_DEBUG("Register string '%s'", entry);
317 tok = strchr(entrycpy, ':');
318 xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
321 min = strtol(tok, &tok, 10);
322 xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
324 xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
325 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
326 tok += strlen("_to_");
328 max = strtol(tok, &tok, 10);
329 xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
331 xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
332 "; Should be <name>:<min nb>_to_<max nb>_<type>");
335 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
336 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
337 "; Should be one of 'string', 'int' or 'double'.");
339 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
341 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
344 /** @brief Displays the declared aliases and their description */
345 void xbt_cfg_aliases(xbt_cfg_t cfg)
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 )cfg, dict_cursor, name, variable)
354 xbt_dynar_push(names, &name);
355 xbt_dynar_sort_strings(names);
357 xbt_dynar_foreach(names, dynar_cursor, name) {
358 variable = xbt_dict_get((xbt_dict_t )cfg, name);
360 if (variable->type == xbt_cfgelm_alias)
361 printf(" %s: %s\n", name, variable->desc);
365 /** @brief Displays the declared options and their description */
366 void xbt_cfg_help(xbt_cfg_t cfg)
368 xbt_dict_cursor_t dict_cursor;
369 unsigned int dynar_cursor;
370 xbt_cfgelm_t variable;
372 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
374 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
375 xbt_dynar_push(names, &name);
376 xbt_dynar_sort_strings(names);
378 xbt_dynar_foreach(names, dynar_cursor, name) {
381 variable = xbt_dict_get((xbt_dict_t )cfg, name);
382 if (variable->type == xbt_cfgelm_alias)
385 printf(" %s: %s\n", name, variable->desc);
386 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
387 if (variable->min != 1 || variable->max != 1) {
388 printf("Arity: min:%d to max:", variable->min);
389 if (variable->max == 0)
390 printf("(no bound); ");
392 printf("%d; ", variable->max);
394 size = xbt_dynar_length(variable->content);
395 printf("Current value%s: ", (size <= 1 ? "" : "s"));
398 printf(size == 0 ? "n/a\n" : "{ ");
399 for (i = 0; i < size; i++) {
400 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
402 switch (variable->type) {
404 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
406 case xbt_cfgelm_double:
407 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
409 case xbt_cfgelm_string:
410 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
412 case xbt_cfgelm_boolean: {
413 int b = xbt_dynar_get_as(variable->content, i, int);
414 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
415 if (b == 0 || b == 1)
416 printf("'%s'%s", bs, sep);
418 printf("'%s/%d'%s", bs, b, sep);
422 printf("Invalid type!!%s", sep);
426 xbt_dynar_free(&names);
429 /** @brief Check that each variable have the right amount of values */
430 void xbt_cfg_check(xbt_cfg_t cfg)
432 xbt_dict_cursor_t cursor;
433 xbt_cfgelm_t variable;
437 xbt_assert(cfg, "NULL config set.");
438 XBT_DEBUG("Check cfg set %p", cfg);
440 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
441 if (variable->type == xbt_cfgelm_alias)
444 size = xbt_dynar_length(variable->content);
445 if (variable->min > size) {
446 xbt_dict_cursor_free(&cursor);
447 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
448 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
451 if (variable->isdefault && size > variable->min) {
452 xbt_dict_cursor_free(&cursor);
453 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
454 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
457 if (variable->max > 0 && variable->max < size) {
458 xbt_dict_cursor_free(&cursor);
459 THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
460 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
463 xbt_dict_cursor_free(&cursor);
466 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
468 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
470 // The user used the old name. Switch to the new one after a short warning
471 while (res && res->type == xbt_cfgelm_alias) {
472 const char* newname = (const char *)res->content;
473 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
474 res = xbt_cfgelm_get(cfg, newname, type);
480 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
483 xbt_assert(type == xbt_cfgelm_any || res->type == type,
484 "You tried to access to the config element %s as an %s, but its type is %s.",
485 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
489 /** @brief Get the type of this variable in that configuration set
491 * @param cfg the config set
492 * @param name the name of the element
494 * @return the type of the given element
496 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
498 xbt_cfgelm_t variable = NULL;
500 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
502 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
504 XBT_DEBUG("type in variable = %d", (int)variable->type);
505 return variable->type;
508 /*----[ Setting ]---------------------------------------------------------*/
509 /** @brief va_args version of xbt_cfg_set
511 * @param cfg config set to fill
512 * @param name variable name
513 * @param pa variable value
515 * Add some values to the config set.
517 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
522 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
527 type = xbt_cfg_get_type(cfg, name);
530 if (e.category == not_found_error) {
532 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
538 case xbt_cfgelm_string:
539 str = va_arg(pa, char *);
540 xbt_cfg_set_string(name, str);
544 xbt_cfg_set_int(name, i);
546 case xbt_cfgelm_double:
547 d = va_arg(pa, double);
548 xbt_cfg_set_double(name, d);
550 case xbt_cfgelm_boolean:
551 str = va_arg(pa, char *);
552 xbt_cfg_set_boolean(name, str);
555 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
559 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
561 * @param cfg config set to fill
562 * @param name variable name
563 * @param ... variable value
565 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
570 xbt_cfg_set_vargs(cfg, name, pa);
574 /** @brief Add values parsed from a string into a config set
576 * @param cfg config set to fill
577 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
578 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
579 * already registered variable, and [value] conforms to the data type under which this variable was registered.
581 * @todo This is a crude manual parser, it should be a proper lexer.
583 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
585 if (!options || !strlen(options)) { /* nothing to do */
588 char *optionlist_cpy = xbt_strdup(options);
590 XBT_DEBUG("List to parse and set:'%s'", options);
591 char *option = optionlist_cpy;
592 while (1) { /* breaks in the code */
596 int len = strlen(name);
597 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
600 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
601 XBT_DEBUG("Take %c.", *option);
604 if (option - name == len) {
605 XBT_DEBUG("Boundary=EOL");
606 option = NULL; /* don't do next iteration */
608 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
609 /* Pass the following blank chars */
611 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
612 /* fprintf(stderr,"Ignore a blank char.\n"); */
615 if (option - name == len - 1)
616 option = NULL; /* don't do next iteration */
618 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
620 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
625 char *val = strchr(name, ':');
626 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
627 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
630 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
631 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
634 xbt_cfg_set_as_string(name,val);
636 free(optionlist_cpy);
640 free(optionlist_cpy);
643 /** @brief Set the value of a variable, using the string representation of that value
645 * @param key name of the variable to modify
646 * @param value string representation of the value to set
648 * @return the first char after the parsed value in val
651 void *xbt_cfg_set_as_string(const char *key, const char *value) {
655 volatile xbt_cfgelm_t variable = NULL;
660 while (variable == NULL) {
661 variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
662 if (variable->type == xbt_cfgelm_alias) {
663 const char *newname = (const char*)variable->content;
664 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
670 if (e.category == not_found_error) {
672 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
677 switch (variable->type) {
678 case xbt_cfgelm_string:
679 xbt_cfg_set_string(key, value); /* throws */
682 i = strtol(value, &ret, 0);
684 xbt_die("Value of option %s not valid. Should be an integer", key);
686 xbt_cfg_set_int(key, i); /* throws */
688 case xbt_cfgelm_double:
689 d = strtod(value, &ret);
691 xbt_die("Value of option %s not valid. Should be a double", key);
693 xbt_cfg_set_double(key, d); /* throws */
695 case xbt_cfgelm_boolean:
696 xbt_cfg_set_boolean(key, value); /* throws */
697 ret = (char *)value + strlen(value);
700 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
706 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
708 * This is useful to change the default value of a variable while allowing
709 * users to override it with command line arguments
711 void xbt_cfg_setdefault_int(const char *name, int val)
713 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
715 if (variable->isdefault){
716 xbt_cfg_set_int(name, val);
717 variable->isdefault = 1;
719 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
722 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
724 * This is useful to change the default value of a variable while allowing
725 * users to override it with command line arguments
727 void xbt_cfg_setdefault_double(const char *name, double val)
729 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
731 if (variable->isdefault) {
732 xbt_cfg_set_double(name, val);
733 variable->isdefault = 1;
735 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
738 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
740 * This is useful to change the default value of a variable while allowing
741 * users to override it with command line arguments
743 void xbt_cfg_setdefault_string(const char *name, const char *val)
745 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
747 if (variable->isdefault){
748 xbt_cfg_set_string(name, val);
749 variable->isdefault = 1;
751 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
754 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
756 * This is useful to change the default value of a variable while allowing
757 * users to override it with command line arguments
759 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
761 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
763 if (variable->isdefault){
764 xbt_cfg_set_boolean(name, val);
765 variable->isdefault = 1;
768 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
771 /** @brief Set or add an integer value to \a name within \a cfg
773 * @param cfg the config set
774 * @param name the name of the variable
775 * @param val the value of the variable
777 void xbt_cfg_set_int(const char *name, int val)
779 XBT_VERB("Configuration setting: %s=%d", name, val);
780 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
782 if (variable->max == 1) {
783 xbt_dynar_set(variable->content, 0, &val);
785 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
786 THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
787 val, name, variable->max);
789 xbt_dynar_push(variable->content, &val);
792 if (variable->cb_set)
793 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
794 variable->isdefault = 0;
797 /** @brief Set or add a double value to \a name within \a cfg
799 * @param cfg the config set
800 * @param name the name of the variable
801 * @param val the doule to set
803 void xbt_cfg_set_double(const char *name, double val)
805 XBT_VERB("Configuration setting: %s=%f", name, val);
806 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
808 if (variable->max == 1) {
809 xbt_dynar_set(variable->content, 0, &val);
811 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
812 THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
813 val, name, variable->max);
815 xbt_dynar_push(variable->content, &val);
818 if (variable->cb_set)
819 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
820 variable->isdefault = 0;
823 /** @brief Set or add a string value to \a name within \a cfg
825 * @param cfg the config set
826 * @param name the name of the variable
827 * @param val the value to be added
830 void xbt_cfg_set_string(const char *name, const char *val)
832 char *newval = xbt_strdup(val);
834 XBT_VERB("Configuration setting: %s=%s", name, val);
835 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
837 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
838 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
840 if (variable->max == 1) {
841 if (!xbt_dynar_is_empty(variable->content)) {
842 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
846 xbt_dynar_set(variable->content, 0, &newval);
849 && xbt_dynar_length(variable->content) == variable->max)
850 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
851 name, val, variable->max);
853 xbt_dynar_push(variable->content, &newval);
856 if (variable->cb_set)
857 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
858 variable->isdefault = 0;
861 /** @brief Set or add a boolean value to \a name within \a cfg
863 * @param cfg the config set
864 * @param name the name of the variable
865 * @param val the value of the variable
867 void xbt_cfg_set_boolean(const char *name, const char *val)
871 XBT_VERB("Configuration setting: %s=%s", name, val);
872 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
874 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
875 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
879 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
884 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
885 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
888 if (variable->max == 1) {
889 xbt_dynar_set(variable->content, 0, &bval);
891 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
892 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
893 val, name, variable->max);
895 xbt_dynar_push(variable->content, &bval);
898 if (variable->cb_set)
899 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
900 variable->isdefault = 0;
903 /* ---- [ Removing ] ---- */
904 /** @brief Remove the provided \e val integer value from a variable
906 * @param cfg the config set
907 * @param name the name of the variable
908 * @param val the value to be removed
910 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
915 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
917 if (xbt_dynar_length(variable->content) == variable->min)
918 THROWF(mismatch_error, 0,
919 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
920 val, name, variable->min);
922 xbt_dynar_foreach(variable->content, cpt, seen) {
924 xbt_dynar_cursor_rm(variable->content, &cpt);
928 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
931 /** @brief Remove the provided \e val double value from a variable
933 * @param cfg the config set
934 * @param name the name of the variable
935 * @param val the value to be removed
937 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
942 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
944 if (xbt_dynar_length(variable->content) == variable->min)
945 THROWF(mismatch_error, 0,
946 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
947 val, name, variable->min);
949 xbt_dynar_foreach(variable->content, cpt, seen) {
951 xbt_dynar_cursor_rm(variable->content, &cpt);
956 THROWF(not_found_error, 0,"Can't remove the value %f of config element %s: value not found.", val, name);
959 /** @brief Remove the provided \e val string value from a variable
961 * @param cfg the config set
962 * @param name the name of the variable
963 * @param val the value of the string which will be removed
965 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
969 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
971 if (xbt_dynar_length(variable->content) == variable->min)
972 THROWF(mismatch_error, 0,
973 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
974 name, val, variable->min);
976 xbt_dynar_foreach(variable->content, cpt, seen) {
977 if (!strcpy(seen, val)) {
978 xbt_dynar_cursor_rm(variable->content, &cpt);
983 THROWF(not_found_error, 0, "Can't remove the value %s of config element %s: value not found.", val, name);
986 /** @brief Remove the provided \e val boolean value from a variable
988 * @param cfg the config set
989 * @param name the name of the variable
990 * @param val the value to be removed
992 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
996 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
998 if (xbt_dynar_length(variable->content) == variable->min)
999 THROWF(mismatch_error, 0,
1000 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1001 val, name, variable->min);
1003 xbt_dynar_foreach(variable->content, cpt, seen) {
1005 xbt_dynar_cursor_rm(variable->content, &cpt);
1010 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
1013 /** @brief Remove the \e pos th value from the provided variable */
1014 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1016 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1018 if (xbt_dynar_length(variable->content) == variable->min)
1019 THROWF(mismatch_error, 0,
1020 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1021 pos, name, variable->min);
1023 xbt_dynar_remove_at(variable->content, pos, NULL);
1026 /** @brief Remove all the values from a variable
1028 * @param cfg the config set
1029 * @param name the name of the variable
1031 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1033 xbt_cfgelm_t variable = NULL;
1037 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1039 if (e.category != not_found_error)
1043 THROWF(not_found_error, 0, "Can't empty '%s' since this option does not exist", name);
1047 xbt_dynar_reset(variable->content);
1050 /* Say if the value is the default value */
1051 int xbt_cfg_is_default_value(const char *name)
1053 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
1054 return variable->isdefault;
1057 /*----[ Getting ]---------------------------------------------------------*/
1058 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1060 * @param name the name of the variable
1062 * Returns the first value from the config set under the given name.
1063 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1065 int xbt_cfg_get_int(const char *name)
1067 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
1069 if (xbt_dynar_length(variable->content) > 1) {
1070 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1071 name, xbt_dynar_length(variable->content));
1074 return xbt_dynar_get_as(variable->content, 0, int);
1077 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1079 * @param cfg the config set
1080 * @param name the name of the variable
1082 * Returns the first value from the config set under the given name.
1083 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1085 double xbt_cfg_get_double(const char *name)
1087 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
1089 if (xbt_dynar_length(variable->content) > 1) {
1090 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
1091 name, xbt_dynar_length(variable->content));
1094 return xbt_dynar_get_as(variable->content, 0, double);
1097 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1099 * @param cfg the config set
1100 * @param name the name of the variable
1102 * Returns the first value from the config set under the given name.
1103 * If there is more than one value, it will issue a warning. Consider using
1104 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1106 * \warning the returned value is the actual content of the config set
1108 char *xbt_cfg_get_string(const char *name)
1110 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
1112 if (xbt_dynar_length(variable->content) > 1) {
1113 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
1114 name, xbt_dynar_length(variable->content));
1115 } else if (xbt_dynar_is_empty(variable->content)) {
1119 return xbt_dynar_get_as(variable->content, 0, char *);
1122 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1124 * @param cfg the config set
1125 * @param name the name of the variable
1127 * Returns the first value from the config set under the given name.
1128 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1130 int xbt_cfg_get_boolean(const char *name)
1132 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
1134 if (xbt_dynar_length(variable->content) > 1) {
1135 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1136 name, xbt_dynar_length(variable->content));
1139 return xbt_dynar_get_as(variable->content, 0, int);
1142 /** @brief Retrieve the dynar of all the values stored in a variable
1144 * @param cfg where to search in
1145 * @param name what to search for
1147 * Get the data stored in the config set.
1149 * \warning the returned value is the actual content of the config set
1151 xbt_dynar_t xbt_cfg_get_dynar(const char *name)
1153 xbt_cfgelm_t variable = NULL;
1157 variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
1159 if (e.category == not_found_error) {
1161 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1166 return variable->content;
1169 /** @brief Retrieve one of the integer value of a variable */
1170 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1172 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1173 return xbt_dynar_get_as(variable->content, pos, int);
1176 /** @brief Retrieve one of the double value of a variable */
1177 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1179 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1180 return xbt_dynar_get_as(variable->content, pos, double);
1183 /** @brief Retrieve one of the string value of a variable */
1184 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1186 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1187 return xbt_dynar_get_as(variable->content, pos, char *);
1190 /** @brief Retrieve one of the boolean value of a variable */
1191 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1193 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1194 return xbt_dynar_get_as(variable->content, pos, int);
1201 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1203 XBT_TEST_SUITE("config", "Configuration support");
1205 static xbt_cfg_t make_set()
1207 xbt_cfg_t set = NULL;
1209 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1210 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1211 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1212 xbt_cfg_register_str(&set, "user:1_to_10_string");
1215 } /* end_of_make_set */
1217 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1219 xbt_cfg_t set = make_set();
1220 xbt_test_add("Alloc and free a config set");
1221 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1226 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1228 xbt_cfg_t set = set = make_set();
1231 xbt_test_add("Having too few elements for speed");
1232 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1236 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1237 xbt_test_fail("Got an exception. msg=%s", e.msg);
1243 xbt_test_add("Having too much values of 'speed'");
1245 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1247 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1249 if (e.category != mismatch_error ||
1250 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1251 xbt_test_fail("Got an exception. msg=%s", e.msg);
1259 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1261 xbt_test_add("Get a single value");
1263 /* get_single_value */
1265 xbt_cfg_t myset = make_set();
1267 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1268 ival = xbt_cfg_get_int("speed");
1270 xbt_test_fail("Speed value = %d, I expected 42", ival);
1271 xbt_cfg_free(&myset);
1274 xbt_test_add("Get multiple values");
1276 /* get_multiple_value */
1278 xbt_cfg_t myset = make_set();
1280 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1281 xbt_cfg_set_parse(myset, "speed:42");
1282 xbt_cfg_check(myset);
1283 dyn = xbt_cfg_get_dynar("user");
1285 if (xbt_dynar_length(dyn) != 3)
1286 xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1288 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1289 xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1291 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1292 xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1294 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1295 xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1296 xbt_cfg_free(&myset);
1299 xbt_test_add("Access to a non-existant entry");
1301 /* non-existant_entry */
1302 xbt_cfg_t myset = make_set();
1306 xbt_cfg_set_parse(myset, "color:blue");
1308 if (e.category != not_found_error)
1309 xbt_test_exception(e);
1312 xbt_cfg_free(&myset);
1315 #endif /* SIMGRID_TEST */