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_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
33 /* Allowed type of the variable */
34 e_xbt_cfgelm_type_t type;
43 (cannot be an union because type peer used to use both str and i, but it could be converted now) */
45 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
47 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
48 { "int", "double", "string", "boolean", "any" };
50 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
58 /* Internal stuff used in cache to free a variable */
59 static void xbt_cfgelm_free(void *data);
61 /* Retrieve the variable we'll modify */
62 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
63 e_xbt_cfgelm_type_t type);
65 /*----[ Memory management ]-----------------------------------------------*/
67 /** @brief Constructor
69 * Initialise a config set
73 xbt_cfg_t xbt_cfg_new(void)
75 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
78 /** \brief Copy an existing configuration set
80 * @param whereto the config set to be created
81 * @param tocopy the source data
83 * This only copy the registrations, not the actual content
86 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
88 xbt_dict_cursor_t cursor = NULL;
89 xbt_cfgelm_t variable = NULL;
92 XBT_DEBUG("Copy cfg set %p", tocopy);
94 xbt_assert(tocopy, "cannot copy NULL config");
96 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
97 xbt_cfg_register(whereto, name, variable->desc, variable->type,
98 variable->min, variable->max, variable->cb_set,
103 /** @brief Destructor */
104 void xbt_cfg_free(xbt_cfg_t * cfg)
106 XBT_DEBUG("Frees cfg set %p", cfg);
107 xbt_dict_free((xbt_dict_t *) cfg);
110 /** @brief Dump a config set for debuging purpose
112 * @param name The name to give to this config set
113 * @param indent what to write at the begining of each line (right number of spaces)
114 * @param cfg the config set
116 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
118 xbt_dict_t dict = (xbt_dict_t) cfg;
119 xbt_dict_cursor_t cursor = NULL;
120 xbt_cfgelm_t variable = NULL;
129 printf("%s>> Dumping of the config set '%s':\n", indent, name);
130 xbt_dict_foreach(dict, cursor, key, variable) {
132 printf("%s %s:", indent, key);
134 size = xbt_dynar_length(variable->content);
136 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
137 variable->min, variable->max,
138 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
141 switch (variable->type) {
144 for (i = 0; i < size; i++) {
145 ival = xbt_dynar_get_as(variable->content, i, int);
146 printf("%s %d\n", indent, ival);
150 case xbt_cfgelm_double:
151 for (i = 0; i < size; i++) {
152 dval = xbt_dynar_get_as(variable->content, i, double);
153 printf("%s %f\n", indent, dval);
157 case xbt_cfgelm_string:
158 for (i = 0; i < size; i++) {
159 sval = xbt_dynar_get_as(variable->content, i, char *);
160 printf("%s %s\n", indent, sval);
164 case xbt_cfgelm_boolean:
165 for (i = 0; i < size; i++) {
166 ival = xbt_dynar_get_as(variable->content, i, int);
167 printf("%s %d\n", indent, ival);
172 printf("%s Invalid type!!\n", indent);
178 printf("%s<< End of the config set '%s'\n", indent, name);
181 xbt_dict_cursor_free(&cursor);
186 * free an config element
189 void xbt_cfgelm_free(void *data)
191 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
193 XBT_DEBUG("Frees cfgelm %p", c);
197 xbt_dynar_free(&(c->content));
201 /*----[ Registering stuff ]-----------------------------------------------*/
203 /** @brief Register an element within a config set
205 * @param cfg the config set
206 * @param name the name of the config element
207 * @param desc a description for this item (used by xbt_cfg_help())
208 * @param type the type of the config element
209 * @param min the minimum number of values for this config element
210 * @param max the maximum number of values for this config element
211 * @param cb_set callback function called when a value is set
212 * @param cb_rm callback function called when a value is removed
215 void xbt_cfg_register(xbt_cfg_t * cfg,
216 const char *name, const char *desc,
217 e_xbt_cfgelm_type_t type, int min,
218 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
223 *cfg = xbt_cfg_new();
224 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
225 "type of %s not valid (%d should be between %d and %d)",
226 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
227 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
230 XBT_WARN("Config elem %s registered twice.", name);
231 /* Will be removed by the insertion of the new one */
234 res = xbt_new(s_xbt_cfgelm_t, 1);
235 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
236 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
239 res->desc = xbt_strdup(desc);
243 res->cb_set = cb_set;
249 res->content = xbt_dynar_new(sizeof(int), NULL);
252 case xbt_cfgelm_double:
253 res->content = xbt_dynar_new(sizeof(double), NULL);
256 case xbt_cfgelm_string:
257 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
260 case xbt_cfgelm_boolean:
261 res->content = xbt_dynar_new(sizeof(int), NULL);
265 XBT_ERROR("%d is an invalid type code", (int)type);
268 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
271 /** @brief Unregister an element from a config set.
273 * @param cfg the config set
274 * @param name the name of the element to be freed
276 * Note that it removes both the description and the actual content.
277 * Throws not_found when no such element exists.
280 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
282 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
283 xbt_dict_remove((xbt_dict_t) cfg, name);
287 * @brief Parse a string and register the stuff described.
289 * @param cfg the config set
290 * @param entry a string describing the element to register
292 * The string may consist in several variable descriptions separated by a space.
293 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
294 * with type being one of 'string','int' or 'double'.
296 * FIXME: this does not allow to set the description
299 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
301 char *entrycpy = xbt_strdup(entry);
305 e_xbt_cfgelm_type_t type;
306 XBT_DEBUG("Register string '%s'", entry);
308 tok = strchr(entrycpy, ':');
309 xbt_assert(tok, "Invalid config element descriptor: %s%s",
310 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
313 min = strtol(tok, &tok, 10);
314 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
317 xbt_assert(strcmp(tok, "_to_"),
318 "Invalid config element descriptor : %s%s",
319 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
320 tok += strlen("_to_");
322 max = strtol(tok, &tok, 10);
323 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
326 xbt_assert(*tok == '_',
327 "Invalid config element descriptor: %s%s", entry,
328 "; Should be <name>:<min nb>_to_<max nb>_<type>");
331 for (type = (e_xbt_cfgelm_type_t)0;
332 type < xbt_cfgelm_type_count
333 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
334 xbt_assert(type < xbt_cfgelm_type_count,
335 "Invalid type in config element descriptor: %s%s", entry,
336 "; Should be one of 'string', 'int' or 'double'.");
338 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
340 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
343 /** @brief Displays the declared options and their description */
344 void xbt_cfg_help(xbt_cfg_t cfg)
346 xbt_dict_cursor_t dict_cursor;
347 unsigned int dynar_cursor;
348 xbt_cfgelm_t variable;
350 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
352 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
353 xbt_dynar_push(names, &name);
355 xbt_dynar_sort_strings(names);
357 xbt_dynar_foreach(names, dynar_cursor, name) {
360 variable = xbt_dict_get((xbt_dict_t )cfg, name);
362 printf(" %s: %s\n", name, variable->desc);
363 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
364 if (variable->min != 1 || variable->max != 1) {
365 printf("Arity: min:%d to max:", variable->min);
366 if (variable->max == 0)
367 printf("(no bound); ");
369 printf("%d; ", variable->max);
371 size = xbt_dynar_length(variable->content);
372 printf("Current value%s: ", (size <= 1 ? "" : "s"));
375 printf(size == 0 ? "n/a\n" : "{ ");
376 for (i = 0; i < size; i++) {
377 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
379 switch (variable->type) {
381 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
384 case xbt_cfgelm_double:
385 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
388 case xbt_cfgelm_string:
389 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
392 case xbt_cfgelm_boolean: {
393 int b = xbt_dynar_get_as(variable->content, i, int);
394 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
395 : xbt_cfgelm_boolean_values[0].false_val;
396 if (b == 0 || b == 1)
397 printf("'%s'%s", bs, sep);
399 printf("'%s/%d'%s", bs, b, sep);
404 printf("Invalid type!!%s", sep);
409 xbt_dynar_free(&names);
412 /** @brief Check that each variable have the right amount of values */
413 void xbt_cfg_check(xbt_cfg_t cfg)
415 xbt_dict_cursor_t cursor;
416 xbt_cfgelm_t variable;
420 xbt_assert(cfg, "NULL config set.");
421 XBT_DEBUG("Check cfg set %p", cfg);
423 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
424 size = xbt_dynar_length(variable->content);
425 if (variable->min > size) {
426 xbt_dict_cursor_free(&cursor);
427 THROWF(mismatch_error, 0,
428 "Config elem %s needs at least %d %s, but there is only %d values.",
429 name, variable->min, xbt_cfgelm_type_name[variable->type],
433 if (variable->isdefault && size > variable->min) {
434 xbt_dict_cursor_free(&cursor);
435 THROWF(mismatch_error, 0,
436 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
437 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
440 if (variable->max > 0 && variable->max < size) {
441 xbt_dict_cursor_free(&cursor);
442 THROWF(mismatch_error, 0,
443 "Config elem %s accepts at most %d %s, but there is %d values.",
444 name, variable->max, xbt_cfgelm_type_name[variable->type],
449 xbt_dict_cursor_free(&cursor);
452 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
454 e_xbt_cfgelm_type_t type)
456 xbt_cfgelm_t res = NULL;
458 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
461 THROWF(not_found_error, 0,
462 "No registered variable '%s' in this config set. It is possible that this "\
463 "configuration option has been renamed; please read the file ChangeLog carefully!", name);
466 xbt_assert(type == xbt_cfgelm_any || res->type == type,
467 "You tried to access to the config element %s as an %s, but its type is %s.",
469 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
474 /** @brief Get the type of this variable in that configuration set
476 * @param cfg the config set
477 * @param name the name of the element
479 * @return the type of the given element
482 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
485 xbt_cfgelm_t variable = NULL;
487 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
489 THROWF(not_found_error, 0,
490 "Can't get the type of '%s' since this variable does not exist",
493 XBT_DEBUG("type in variable = %d", (int)variable->type);
495 return variable->type;
498 /*----[ Setting ]---------------------------------------------------------*/
499 /** @brief va_args version of xbt_cfg_set
501 * @param cfg config set to fill
502 * @param name variable name
503 * @param pa variable value
505 * Add some values to the config set.
507 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
512 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
517 type = xbt_cfg_get_type(cfg, name);
520 if (e.category == not_found_error) {
522 THROWF(not_found_error, 0,
523 "Can't set the property '%s' since it's not registered",
530 case xbt_cfgelm_string:
531 str = va_arg(pa, char *);
532 xbt_cfg_set_string(cfg, name, str);
537 xbt_cfg_set_int(cfg, name, i);
540 case xbt_cfgelm_double:
541 d = va_arg(pa, double);
542 xbt_cfg_set_double(cfg, name, d);
545 case xbt_cfgelm_boolean:
546 str = va_arg(pa, char *);
547 xbt_cfg_set_boolean(cfg, name, str);
551 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
555 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
557 * @param cfg config set to fill
558 * @param name variable name
559 * @param ... variable value
562 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
567 xbt_cfg_set_vargs(cfg, name, pa);
571 /** @brief Add values parsed from a string into a config set
573 * @param cfg config set to fill
574 * @param options a string containing the content to add to the config set. This
575 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
576 * like "[name]:[value]" where [name] is the name of an already registred
577 * variable, and [value] conforms to the data type under which this variable was
580 * @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 char *optionlist_cpy;
586 char *option, *name, *val;
591 if (!options || !strlen(options)) { /* nothing to do */
594 optionlist_cpy = xbt_strdup(options);
596 XBT_DEBUG("List to parse and set:'%s'", options);
597 option = optionlist_cpy;
598 while (1) { /* breaks in the code */
604 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
605 name, len, (long) (option - name));
608 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
609 && *option != '\t' && *option != ',') {
610 XBT_DEBUG("Take %c.", *option);
613 if (option - name == len) {
614 XBT_DEBUG("Boundary=EOL");
615 option = NULL; /* don't do next iteration */
618 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
619 *option, len, (long) (option - name));
621 /* Pass the following blank chars */
623 while (option - name < (len - 1) &&
624 (*option == ' ' || *option == '\n' || *option == '\t')) {
625 /* fprintf(stderr,"Ignore a blank char.\n"); */
628 if (option - name == len - 1)
629 option = NULL; /* don't do next iteration */
631 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
633 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
638 val = strchr(name, ':');
640 /* don't free(optionlist_cpy) here, 'name' points inside it */
641 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
646 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
647 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
650 xbt_cfg_set_as_string(cfg,name,val);
652 free(optionlist_cpy);
656 free(optionlist_cpy);
659 /** @brief Set the value of a variable, using the string representation of that value
661 * @param cfg config set to modify
662 * @param key name of the variable to modify
663 * @param value string representation of the value to set
665 * @return the first char after the parsed value in val
668 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
672 volatile xbt_cfgelm_t variable = NULL;
677 variable = xbt_dict_get((xbt_dict_t) cfg, key);
680 if (e.category == not_found_error) {
682 THROWF(not_found_error, 0,
683 "No registered variable corresponding to '%s'.", key);
688 switch (variable->type) {
689 case xbt_cfgelm_string:
690 xbt_cfg_set_string(cfg, key, value); /* throws */
694 i = strtol(value, &ret, 0);
696 xbt_die("Value of option %s not valid. Should be an integer", key);
699 xbt_cfg_set_int(cfg, key, i); /* throws */
702 case xbt_cfgelm_double:
703 d = strtod(value, &ret);
705 xbt_die("Value of option %s not valid. Should be a double", key);
708 xbt_cfg_set_double(cfg, key, d); /* throws */
711 case xbt_cfgelm_boolean:
712 xbt_cfg_set_boolean(cfg, key, value); /* throws */
713 ret = (char *)value + strlen(value);
717 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
724 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
726 * This is useful to change the default value of a variable while allowing
727 * users to override it with command line arguments
729 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
731 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
733 if (variable->isdefault){
734 xbt_cfg_set_int(cfg, name, val);
735 variable->isdefault = 1;
739 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
743 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
745 * This is useful to change the default value of a variable while allowing
746 * users to override it with command line arguments
748 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
750 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
752 if (variable->isdefault) {
753 xbt_cfg_set_double(cfg, name, val);
754 variable->isdefault = 1;
758 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
762 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
764 * This is useful to change the default value of a variable while allowing
765 * users to override it with command line arguments
767 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
770 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
772 if (variable->isdefault){
773 xbt_cfg_set_string(cfg, name, val);
774 variable->isdefault = 1;
778 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
783 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
785 * This is useful to change the default value of a variable while allowing
786 * users to override it with command line arguments
788 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
790 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
792 if (variable->isdefault){
793 xbt_cfg_set_boolean(cfg, name, val);
794 variable->isdefault = 1;
798 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
802 /** @brief Set or add an integer value to \a name within \a cfg
804 * @param cfg the config set
805 * @param name the name of the variable
806 * @param val the value of the variable
808 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
810 xbt_cfgelm_t variable;
812 XBT_VERB("Configuration setting: %s=%d", name, val);
813 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
815 if (variable->max == 1) {
816 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
817 variable->cb_rm(name, 0);
819 xbt_dynar_set(variable->content, 0, &val);
822 && xbt_dynar_length(variable->content) ==
823 (unsigned long) variable->max)
824 THROWF(mismatch_error, 0,
825 "Cannot add value %d to the config element %s since it's already full (size=%d)",
826 val, name, variable->max);
828 xbt_dynar_push(variable->content, &val);
831 if (variable->cb_set)
832 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
833 variable->isdefault = 0;
836 /** @brief Set or add a double value to \a name within \a cfg
838 * @param cfg the config set
839 * @param name the name of the variable
840 * @param val the doule to set
843 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
845 xbt_cfgelm_t variable;
847 XBT_VERB("Configuration setting: %s=%f", name, val);
848 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
850 if (variable->max == 1) {
851 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
852 variable->cb_rm(name, 0);
854 xbt_dynar_set(variable->content, 0, &val);
857 && xbt_dynar_length(variable->content) == variable->max)
858 THROWF(mismatch_error, 0,
859 "Cannot add value %f to the config element %s since it's already full (size=%d)",
860 val, name, variable->max);
862 xbt_dynar_push(variable->content, &val);
865 if (variable->cb_set)
866 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
867 variable->isdefault = 0;
870 /** @brief Set or add a string value to \a name within \a cfg
872 * @param cfg the config set
873 * @param name the name of the variable
874 * @param val the value to be added
878 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
880 xbt_cfgelm_t variable;
881 char *newval = xbt_strdup(val);
883 XBT_VERB("Configuration setting: %s=%s", name, val);
884 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
885 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
886 variable->min, variable->max,
887 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
889 if (variable->max == 1) {
890 if (!xbt_dynar_is_empty(variable->content)) {
892 variable->cb_rm(name, 0);
893 else if (variable->type == xbt_cfgelm_string) {
894 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
899 xbt_dynar_set(variable->content, 0, &newval);
902 && xbt_dynar_length(variable->content) == variable->max)
903 THROWF(mismatch_error, 0,
904 "Cannot add value %s to the config element %s since it's already full (size=%d)",
905 name, val, variable->max);
907 xbt_dynar_push(variable->content, &newval);
910 if (variable->cb_set)
911 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
912 variable->isdefault = 0;
915 /** @brief Set or add a boolean value to \a name within \a cfg
917 * @param cfg the config set
918 * @param name the name of the variable
919 * @param val the value of the variable
921 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
923 xbt_cfgelm_t variable;
926 XBT_VERB("Configuration setting: %s=%s", name, val);
927 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
929 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
930 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
934 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
939 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
940 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
943 if (variable->max == 1) {
944 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
945 variable->cb_rm(name, 0);
947 xbt_dynar_set(variable->content, 0, &bval);
950 && xbt_dynar_length(variable->content) ==
951 (unsigned long) variable->max)
952 THROWF(mismatch_error, 0,
953 "Cannot add value %s to the config element %s since it's already full (size=%d)",
954 val, name, variable->max);
956 xbt_dynar_push(variable->content, &bval);
959 if (variable->cb_set)
960 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
961 variable->isdefault = 0;
964 /* ---- [ Removing ] ---- */
966 /** @brief Remove the provided \e val integer value from a variable
968 * @param cfg the config set
969 * @param name the name of the variable
970 * @param val the value to be removed
972 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
975 xbt_cfgelm_t variable;
979 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
981 if (xbt_dynar_length(variable->content) == variable->min)
982 THROWF(mismatch_error, 0,
983 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
984 val, name, variable->min);
986 xbt_dynar_foreach(variable->content, cpt, seen) {
989 variable->cb_rm(name, cpt);
990 xbt_dynar_cursor_rm(variable->content, &cpt);
995 THROWF(not_found_error, 0,
996 "Can't remove the value %d of config element %s: value not found.",
1000 /** @brief Remove the provided \e val double value from a variable
1002 * @param cfg the config set
1003 * @param name the name of the variable
1004 * @param val the value to be removed
1007 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1009 xbt_cfgelm_t variable;
1013 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1015 if (xbt_dynar_length(variable->content) == variable->min)
1016 THROWF(mismatch_error, 0,
1017 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1018 val, name, variable->min);
1020 xbt_dynar_foreach(variable->content, cpt, seen) {
1022 xbt_dynar_cursor_rm(variable->content, &cpt);
1023 if (variable->cb_rm)
1024 variable->cb_rm(name, cpt);
1029 THROWF(not_found_error, 0,
1030 "Can't remove the value %f of config element %s: value not found.",
1034 /** @brief Remove the provided \e val string value from a variable
1036 * @param cfg the config set
1037 * @param name the name of the variable
1038 * @param val the value of the string which will be removed
1040 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1042 xbt_cfgelm_t variable;
1046 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1048 if (xbt_dynar_length(variable->content) == variable->min)
1049 THROWF(mismatch_error, 0,
1050 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1051 name, val, variable->min);
1053 xbt_dynar_foreach(variable->content, cpt, seen) {
1054 if (!strcpy(seen, val)) {
1055 if (variable->cb_rm)
1056 variable->cb_rm(name, cpt);
1057 xbt_dynar_cursor_rm(variable->content, &cpt);
1062 THROWF(not_found_error, 0,
1063 "Can't remove the value %s of config element %s: value not found.",
1067 /** @brief Remove the provided \e val boolean value from a variable
1069 * @param cfg the config set
1070 * @param name the name of the variable
1071 * @param val the value to be removed
1073 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1076 xbt_cfgelm_t variable;
1080 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1082 if (xbt_dynar_length(variable->content) == variable->min)
1083 THROWF(mismatch_error, 0,
1084 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1085 val, name, variable->min);
1087 xbt_dynar_foreach(variable->content, cpt, seen) {
1089 if (variable->cb_rm)
1090 variable->cb_rm(name, cpt);
1091 xbt_dynar_cursor_rm(variable->content, &cpt);
1096 THROWF(not_found_error, 0,
1097 "Can't remove the value %d of config element %s: value not found.",
1101 /** @brief Remove the \e pos th value from the provided variable */
1103 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1106 xbt_cfgelm_t variable;
1108 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1110 if (xbt_dynar_length(variable->content) == variable->min)
1111 THROWF(mismatch_error, 0,
1112 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1113 pos, name, variable->min);
1115 if (variable->cb_rm)
1116 variable->cb_rm(name, pos);
1117 xbt_dynar_remove_at(variable->content, pos, NULL);
1120 /** @brief Remove all the values from a variable
1122 * @param cfg the config set
1123 * @param name the name of the variable
1126 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1128 xbt_cfgelm_t variable = NULL;
1132 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1135 if (e.category != not_found_error)
1139 THROWF(not_found_error, 0,
1140 "Can't empty '%s' since this config element does not exist",
1145 if (variable->cb_rm) {
1148 xbt_dynar_foreach(variable->content, cpt, ignored) {
1149 variable->cb_rm(name, cpt);
1152 xbt_dynar_reset(variable->content);
1156 * Say if the value is the default value
1158 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1160 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1161 return variable->isdefault;
1164 /*----[ Getting ]---------------------------------------------------------*/
1166 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1168 * @param cfg the config set
1169 * @param name the name of the variable
1171 * Returns the first value from the config set under the given name.
1172 * If there is more than one value, it will issue a warning. Consider using
1173 * xbt_cfg_get_dynar() instead.
1175 * \warning the returned value is the actual content of the config set
1177 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1179 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1181 if (xbt_dynar_length(variable->content) > 1) {
1183 ("You asked for the first value of the config element '%s', but there is %lu values",
1184 name, xbt_dynar_length(variable->content));
1187 return xbt_dynar_get_as(variable->content, 0, int);
1190 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1192 * @param cfg the config set
1193 * @param name the name of the variable
1195 * Returns the first value from the config set under the given name.
1196 * If there is more than one value, it will issue a warning. Consider using
1197 * xbt_cfg_get_dynar() instead.
1199 * \warning the returned value is the actual content of the config set
1202 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1204 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1206 if (xbt_dynar_length(variable->content) > 1) {
1208 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1209 name, xbt_dynar_length(variable->content));
1212 return xbt_dynar_get_as(variable->content, 0, double);
1215 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1217 * @param cfg the config set
1218 * @param name the name of the variable
1220 * Returns the first value from the config set under the given name.
1221 * If there is more than one value, it will issue a warning. Consider using
1222 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1224 * \warning the returned value is the actual content of the config set
1227 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1229 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1231 if (xbt_dynar_length(variable->content) > 1) {
1233 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1234 name, xbt_dynar_length(variable->content));
1235 } else if (xbt_dynar_is_empty(variable->content)) {
1239 return xbt_dynar_get_as(variable->content, 0, char *);
1242 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1244 * @param cfg the config set
1245 * @param name the name of the variable
1247 * Returns the first value from the config set under the given name.
1248 * If there is more than one value, it will issue a warning. Consider using
1249 * xbt_cfg_get_dynar() instead.
1251 * \warning the returned value is the actual content of the config set
1253 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1255 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1257 if (xbt_dynar_length(variable->content) > 1) {
1259 ("You asked for the first value of the config element '%s', but there is %lu values",
1260 name, xbt_dynar_length(variable->content));
1263 return xbt_dynar_get_as(variable->content, 0, int);
1266 /** @brief Retrieve the dynar of all the values stored in a variable
1268 * @param cfg where to search in
1269 * @param name what to search for
1271 * Get the data stored in the config set.
1273 * \warning the returned value is the actual content of the config set
1275 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1277 xbt_cfgelm_t variable = NULL;
1281 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1284 if (e.category == not_found_error) {
1286 THROWF(not_found_error, 0,
1287 "No registered variable %s in this config set", name);
1292 return variable->content;
1296 /** @brief Retrieve one of the integer value of a variable */
1297 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1300 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1301 return xbt_dynar_get_as(variable->content, pos, int);
1304 /** @brief Retrieve one of the double value of a variable */
1305 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1308 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1309 return xbt_dynar_get_as(variable->content, pos, double);
1313 /** @brief Retrieve one of the string value of a variable */
1314 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1317 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1318 return xbt_dynar_get_as(variable->content, pos, char *);
1321 /** @brief Retrieve one of the boolean value of a variable */
1322 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1325 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1326 return xbt_dynar_get_as(variable->content, pos, int);
1334 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1336 XBT_TEST_SUITE("config", "Configuration support");
1338 static xbt_cfg_t make_set()
1340 xbt_cfg_t set = NULL;
1342 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1343 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1344 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1345 xbt_cfg_register_str(&set, "user:1_to_10_string");
1348 } /* end_of_make_set */
1350 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1352 xbt_cfg_t set = make_set();
1353 xbt_test_add("Alloc and free a config set");
1354 xbt_cfg_set_parse(set,
1355 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1360 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1362 xbt_cfg_t set = set = make_set();
1365 xbt_test_add("Having too few elements for speed");
1366 xbt_cfg_set_parse(set,
1367 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1372 if (e.category != mismatch_error ||
1373 strncmp(e.msg, "Config elem speed needs",
1374 strlen("Config elem speed needs")))
1375 xbt_test_fail("Got an exception. msg=%s", e.msg);
1383 xbt_test_add("Having too much values of 'speed'");
1385 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1387 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1390 if (e.category != mismatch_error ||
1391 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1392 strlen("Config elem speed needs")))
1393 xbt_test_fail("Got an exception. msg=%s", e.msg);
1402 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1405 xbt_test_add("Get a single value");
1407 /* get_single_value */
1409 xbt_cfg_t myset = make_set();
1411 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1412 ival = xbt_cfg_get_int(myset, "speed");
1414 xbt_test_fail("Speed value = %d, I expected 42", ival);
1415 xbt_cfg_free(&myset);
1418 xbt_test_add("Get multiple values");
1420 /* get_multiple_value */
1422 xbt_cfg_t myset = make_set();
1424 xbt_cfg_set_parse(myset,
1425 "peername:veloce user:foo\nuser:bar\tuser:toto");
1426 xbt_cfg_set_parse(myset, "speed:42");
1427 xbt_cfg_check(myset);
1428 dyn = xbt_cfg_get_dynar(myset, "user");
1430 if (xbt_dynar_length(dyn) != 3)
1431 xbt_test_fail("Dynar length = %lu, I expected 3",
1432 xbt_dynar_length(dyn));
1434 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1435 xbt_test_fail("Dynar[0] = %s, I expected foo",
1436 xbt_dynar_get_as(dyn, 0, char *));
1438 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1439 xbt_test_fail("Dynar[1] = %s, I expected bar",
1440 xbt_dynar_get_as(dyn, 1, char *));
1442 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1443 xbt_test_fail("Dynar[2] = %s, I expected toto",
1444 xbt_dynar_get_as(dyn, 2, char *));
1445 xbt_cfg_free(&myset);
1448 xbt_test_add("Access to a non-existant entry");
1450 /* non-existant_entry */
1451 xbt_cfg_t myset = make_set();
1455 xbt_cfg_set_parse(myset, "color:blue");
1458 if (e.category != not_found_error)
1459 xbt_test_exception(e);
1462 xbt_cfg_free(&myset);
1465 #endif /* SIMGRID_TEST */