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"
21 #include "xbt/config.h" /* prototypes of this module */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
25 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27 Both data and DTD are mixed, but fixing it now would prevent me to ever
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
44 (cannot be an union because type peer uses both str and i) */
46 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
48 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
49 { "int", "double", "string", "boolean", "peer", "any" };
51 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
59 /* Internal stuff used in cache to free a variable */
60 static void xbt_cfgelm_free(void *data);
62 /* Retrieve the variable we'll modify */
63 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
64 e_xbt_cfgelm_type_t type);
66 /*----[ Memory management ]-----------------------------------------------*/
68 /** @brief Constructor
70 * Initialise a config set
74 xbt_cfg_t xbt_cfg_new(void)
76 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
79 /** \brief Copy an existing configuration set
81 * @param whereto the config set to be created
82 * @param tocopy the source data
84 * This only copy the registrations, not the actual content
87 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
89 xbt_dict_cursor_t cursor = NULL;
90 xbt_cfgelm_t variable = NULL;
93 XBT_DEBUG("Copy cfg set %p", tocopy);
95 xbt_assert(tocopy, "cannot copy NULL config");
97 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
98 xbt_cfg_register(whereto, name, variable->desc, variable->type,
99 variable->min, variable->max, variable->cb_set,
104 /** @brief Destructor */
105 void xbt_cfg_free(xbt_cfg_t * cfg)
107 XBT_DEBUG("Frees cfg set %p", cfg);
108 xbt_dict_free((xbt_dict_t *) cfg);
111 /** @brief Dump a config set for debuging purpose
113 * @param name The name to give to this config set
114 * @param indent what to write at the begining of each line (right number of spaces)
115 * @param cfg the config set
117 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
119 xbt_dict_t dict = (xbt_dict_t) cfg;
120 xbt_dict_cursor_t cursor = NULL;
121 xbt_cfgelm_t variable = NULL;
131 printf("%s>> Dumping of the config set '%s':\n", indent, name);
132 xbt_dict_foreach(dict, cursor, key, variable) {
134 printf("%s %s:", indent, key);
136 size = xbt_dynar_length(variable->content);
138 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
139 variable->min, variable->max,
140 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
143 switch (variable->type) {
146 for (i = 0; i < size; i++) {
147 ival = xbt_dynar_get_as(variable->content, i, int);
148 printf("%s %d\n", indent, ival);
152 case xbt_cfgelm_double:
153 for (i = 0; i < size; i++) {
154 dval = xbt_dynar_get_as(variable->content, i, double);
155 printf("%s %f\n", indent, dval);
159 case xbt_cfgelm_string:
160 for (i = 0; i < size; i++) {
161 sval = xbt_dynar_get_as(variable->content, i, char *);
162 printf("%s %s\n", indent, sval);
166 case xbt_cfgelm_boolean:
167 for (i = 0; i < size; i++) {
168 ival = xbt_dynar_get_as(variable->content, i, int);
169 printf("%s %d\n", indent, ival);
173 case xbt_cfgelm_peer:
174 for (i = 0; i < size; i++) {
175 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
176 printf("%s %s:%d\n", indent, hval->name, hval->port);
181 printf("%s Invalid type!!\n", indent);
187 printf("%s<< End of the config set '%s'\n", indent, name);
190 xbt_dict_cursor_free(&cursor);
195 * free an config element
198 void xbt_cfgelm_free(void *data)
200 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
202 XBT_DEBUG("Frees cfgelm %p", c);
206 xbt_dynar_free(&(c->content));
210 /*----[ Registering stuff ]-----------------------------------------------*/
212 /** @brief Register an element within a config set
214 * @param cfg the config set
215 * @param name the name of the config element
216 * @param desc a description for this item (used by xbt_cfg_help())
217 * @param type the type of the config element
218 * @param min the minimum number of values for this config element
219 * @param max the maximum number of values for this config element
220 * @param cb_set callback function called when a value is set
221 * @param cb_rm callback function called when a value is removed
225 xbt_cfg_register(xbt_cfg_t * cfg,
226 const char *name, const char *desc,
227 e_xbt_cfgelm_type_t type, int min,
228 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
233 *cfg = xbt_cfg_new();
234 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
235 "type of %s not valid (%d should be between %d and %d)",
236 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer);
237 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
240 XBT_WARN("Config elem %s registered twice.", name);
241 /* Will be removed by the insertion of the new one */
244 res = xbt_new(s_xbt_cfgelm_t, 1);
245 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
246 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
249 res->desc = xbt_strdup(desc);
253 res->cb_set = cb_set;
259 res->content = xbt_dynar_new(sizeof(int), NULL);
262 case xbt_cfgelm_double:
263 res->content = xbt_dynar_new(sizeof(double), NULL);
266 case xbt_cfgelm_string:
267 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
270 case xbt_cfgelm_boolean:
271 res->content = xbt_dynar_new(sizeof(int), NULL);
274 case xbt_cfgelm_peer:
275 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
279 XBT_ERROR("%d is an invalid type code", (int)type);
282 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
285 /** @brief Unregister an element from a config set.
287 * @param cfg the config set
288 * @param name the name of the element to be freed
290 * Note that it removes both the description and the actual content.
291 * Throws not_found when no such element exists.
294 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
296 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
297 xbt_dict_remove((xbt_dict_t) cfg, name);
301 * @brief Parse a string and register the stuff described.
303 * @param cfg the config set
304 * @param entry a string describing the element to register
306 * The string may consist in several variable descriptions separated by a space.
307 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
308 * with type being one of 'string','int', 'peer' or 'double'.
310 * FIXME: this does not allow to set the description
313 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
315 char *entrycpy = xbt_strdup(entry);
319 e_xbt_cfgelm_type_t type;
320 XBT_DEBUG("Register string '%s'", entry);
322 tok = strchr(entrycpy, ':');
323 xbt_assert(tok, "Invalid config element descriptor: %s%s",
324 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
327 min = strtol(tok, &tok, 10);
328 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
331 xbt_assert(strcmp(tok, "_to_"),
332 "Invalid config element descriptor : %s%s",
333 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
334 tok += strlen("_to_");
336 max = strtol(tok, &tok, 10);
337 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
340 xbt_assert(*tok == '_',
341 "Invalid config element descriptor: %s%s", entry,
342 "; Should be <name>:<min nb>_to_<max nb>_<type>");
345 for (type = (e_xbt_cfgelm_type_t)0;
346 type < xbt_cfgelm_type_count
347 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
348 xbt_assert(type < xbt_cfgelm_type_count,
349 "Invalid type in config element descriptor: %s%s", entry,
350 "; Should be one of 'string', 'int', 'peer' or 'double'.");
352 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
354 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
357 static int strcmp_voidp(const void *pa, const void *pb)
359 return strcmp(*(const char **)pa, *(const char **)pb);
362 /** @brief Displays the declared options and their description */
363 void xbt_cfg_help(xbt_cfg_t cfg)
365 xbt_dict_cursor_t dict_cursor;
366 unsigned int dynar_cursor;
367 xbt_cfgelm_t variable;
369 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
371 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
372 xbt_dynar_push(names, &name);
374 xbt_dynar_sort(names, strcmp_voidp);
376 xbt_dynar_foreach(names, dynar_cursor, name) {
379 variable = xbt_dict_get((xbt_dict_t )cfg, name);
381 printf(" %s: %s\n", name, variable->desc);
382 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
383 if (variable->min != 1 || variable->max != 1) {
384 printf("Arity: min:%d to max:", variable->min);
385 if (variable->max == 0)
386 printf("(no bound); ");
388 printf("%d; ", variable->max);
390 size = xbt_dynar_length(variable->content);
391 printf("Current value%s: ", (size <= 1 ? "" : "s"));
394 printf(size == 0 ? "n/a\n" : "{ ");
395 for (i = 0; i < size; i++) {
396 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
398 switch (variable->type) {
400 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
403 case xbt_cfgelm_double:
404 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
407 case xbt_cfgelm_string:
408 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
411 case xbt_cfgelm_boolean: {
412 int b = xbt_dynar_get_as(variable->content, i, int);
413 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
414 : 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 case xbt_cfgelm_peer: {
423 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
424 printf("%s:%d%s", hval->name, hval->port, sep);
429 printf("Invalid type!!%s", sep);
434 xbt_dynar_free(&names);
437 /** @brief Check that each variable have the right amount of values */
438 void xbt_cfg_check(xbt_cfg_t cfg)
440 xbt_dict_cursor_t cursor;
441 xbt_cfgelm_t variable;
445 xbt_assert(cfg, "NULL config set.");
446 XBT_DEBUG("Check cfg set %p", cfg);
448 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
449 size = xbt_dynar_length(variable->content);
450 if (variable->min > size) {
451 xbt_dict_cursor_free(&cursor);
452 THROWF(mismatch_error, 0,
453 "Config elem %s needs at least %d %s, but there is only %d values.",
454 name, variable->min, xbt_cfgelm_type_name[variable->type],
458 if (variable->isdefault && size > variable->min) {
459 xbt_dict_cursor_free(&cursor);
460 THROWF(mismatch_error, 0,
461 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
462 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
465 if (variable->max > 0 && variable->max < size) {
466 xbt_dict_cursor_free(&cursor);
467 THROWF(mismatch_error, 0,
468 "Config elem %s accepts at most %d %s, but there is %d values.",
469 name, variable->max, xbt_cfgelm_type_name[variable->type],
474 xbt_dict_cursor_free(&cursor);
477 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
479 e_xbt_cfgelm_type_t type)
481 xbt_cfgelm_t res = NULL;
483 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
486 THROWF(not_found_error, 0,
487 "No registered variable '%s' in this config set", name);
490 xbt_assert(type == xbt_cfgelm_any || res->type == type,
491 "You tried to access to the config element %s as an %s, but its type is %s.",
493 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
498 /** @brief Get the type of this variable in that configuration set
500 * @param cfg the config set
501 * @param name the name of the element
503 * @return the type of the given element
506 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
509 xbt_cfgelm_t variable = NULL;
511 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
513 THROWF(not_found_error, 0,
514 "Can't get the type of '%s' since this variable does not exist",
517 XBT_DEBUG("type in variable = %d", (int)variable->type);
519 return variable->type;
522 /*----[ Setting ]---------------------------------------------------------*/
523 /** @brief va_args version of xbt_cfg_set
525 * @param cfg config set to fill
526 * @param name variable name
527 * @param pa variable value
529 * Add some values to the config set.
531 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
536 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
541 type = xbt_cfg_get_type(cfg, name);
544 if (e.category == not_found_error) {
546 THROWF(not_found_error, 0,
547 "Can't set the property '%s' since it's not registered",
554 case xbt_cfgelm_peer:
555 str = va_arg(pa, char *);
557 xbt_cfg_set_peer(cfg, name, str, i);
560 case xbt_cfgelm_string:
561 str = va_arg(pa, char *);
562 xbt_cfg_set_string(cfg, name, str);
567 xbt_cfg_set_int(cfg, name, i);
570 case xbt_cfgelm_double:
571 d = va_arg(pa, double);
572 xbt_cfg_set_double(cfg, name, d);
575 case xbt_cfgelm_boolean:
576 str = va_arg(pa, char *);
577 xbt_cfg_set_boolean(cfg, name, str);
581 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
585 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
587 * @param cfg config set to fill
588 * @param name variable name
589 * @param ... variable value
592 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
597 xbt_cfg_set_vargs(cfg, name, pa);
601 /** @brief Add values parsed from a string into a config set
603 * @param cfg config set to fill
604 * @param options a string containing the content to add to the config set. This
605 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
606 * like "[name]:[value]" where [name] is the name of an already registred
607 * variable, and [value] conforms to the data type under which this variable was
610 * @todo This is a crude manual parser, it should be a proper lexer.
613 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
615 char *optionlist_cpy;
616 char *option, *name, *val;
621 if (!options || !strlen(options)) { /* nothing to do */
624 optionlist_cpy = xbt_strdup(options);
626 XBT_DEBUG("List to parse and set:'%s'", options);
627 option = optionlist_cpy;
628 while (1) { /* breaks in the code */
634 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
635 name, len, (long) (option - name));
638 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
639 && *option != '\t' && *option != ',') {
640 XBT_DEBUG("Take %c.", *option);
643 if (option - name == len) {
644 XBT_DEBUG("Boundary=EOL");
645 option = NULL; /* don't do next iteration */
648 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
649 *option, len, (long) (option - name));
651 /* Pass the following blank chars */
653 while (option - name < (len - 1) &&
654 (*option == ' ' || *option == '\n' || *option == '\t')) {
655 /* fprintf(stderr,"Ignore a blank char.\n"); */
658 if (option - name == len - 1)
659 option = NULL; /* don't do next iteration */
661 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
663 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
668 val = strchr(name, ':');
670 /* don't free(optionlist_cpy) here, 'name' points inside it */
671 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
676 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
677 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
680 xbt_cfg_set_as_string(cfg,name,val);
682 free(optionlist_cpy);
686 free(optionlist_cpy);
689 /** @brief Set the value of a variable, using the string representation of that value
691 * @param cfg config set to modify
692 * @param key name of the variable to modify
693 * @param value string representation of the value to set
695 * @return the first char after the parsed value in val
698 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
702 volatile xbt_cfgelm_t variable = NULL;
709 variable = xbt_dict_get((xbt_dict_t) cfg, key);
712 if (e.category == not_found_error) {
714 THROWF(not_found_error, 0,
715 "No registered variable corresponding to '%s'.", key);
720 switch (variable->type) {
721 case xbt_cfgelm_string:
722 xbt_cfg_set_string(cfg, key, value); /* throws */
726 i = strtol(value, &ret, 0);
728 xbt_die("Value of option %s not valid. Should be an integer", key);
731 xbt_cfg_set_int(cfg, key, i); /* throws */
734 case xbt_cfgelm_double:
735 d = strtod(value, &ret);
737 xbt_die("Value of option %s not valid. Should be a double", key);
740 xbt_cfg_set_double(cfg, key, d); /* throws */
743 case xbt_cfgelm_boolean:
744 xbt_cfg_set_boolean(cfg, key, value); /* throws */
745 ret = (char *)value + strlen(value);
748 case xbt_cfgelm_peer:
749 val = xbt_strdup(value);
751 val = strchr(val, ':');
753 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
757 i = strtol(val, &ret, 0);
759 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
762 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
767 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
774 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
776 * This is useful to change the default value of a variable while allowing
777 * users to override it with command line arguments
779 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
781 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
783 if (variable->isdefault){
784 xbt_cfg_set_int(cfg, name, val);
785 variable->isdefault = 1;
789 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
793 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
795 * This is useful to change the default value of a variable while allowing
796 * users to override it with command line arguments
798 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
800 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
802 if (variable->isdefault) {
803 xbt_cfg_set_double(cfg, name, val);
804 variable->isdefault = 1;
808 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
812 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
814 * This is useful to change the default value of a variable while allowing
815 * users to override it with command line arguments
817 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
820 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
822 if (variable->isdefault){
823 xbt_cfg_set_string(cfg, name, val);
824 variable->isdefault = 1;
828 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
833 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
835 * This is useful to change the default value of a variable while allowing
836 * users to override it with command line arguments
838 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
840 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
842 if (variable->isdefault){
843 xbt_cfg_set_boolean(cfg, name, val);
844 variable->isdefault = 1;
848 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
852 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
854 * This is useful to change the default value of a variable while allowing
855 * users to override it with command line arguments
857 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
858 const char *host, int port)
860 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
862 if (variable->isdefault){
863 xbt_cfg_set_peer(cfg, name, host, port);
864 variable->isdefault = 1;
868 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
872 /** @brief Set or add an integer value to \a name within \a cfg
874 * @param cfg the config set
875 * @param name the name of the variable
876 * @param val the value of the variable
878 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
880 xbt_cfgelm_t variable;
882 XBT_VERB("Configuration setting: %s=%d", name, val);
883 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
885 if (variable->max == 1) {
886 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
887 variable->cb_rm(name, 0);
889 xbt_dynar_set(variable->content, 0, &val);
892 && xbt_dynar_length(variable->content) ==
893 (unsigned long) variable->max)
894 THROWF(mismatch_error, 0,
895 "Cannot add value %d to the config element %s since it's already full (size=%d)",
896 val, name, variable->max);
898 xbt_dynar_push(variable->content, &val);
901 if (variable->cb_set)
902 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
903 variable->isdefault = 0;
906 /** @brief Set or add a double value to \a name within \a cfg
908 * @param cfg the config set
909 * @param name the name of the variable
910 * @param val the doule to set
913 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
915 xbt_cfgelm_t variable;
917 XBT_VERB("Configuration setting: %s=%f", name, val);
918 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
920 if (variable->max == 1) {
921 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
922 variable->cb_rm(name, 0);
924 xbt_dynar_set(variable->content, 0, &val);
927 && xbt_dynar_length(variable->content) == variable->max)
928 THROWF(mismatch_error, 0,
929 "Cannot add value %f to the config element %s since it's already full (size=%d)",
930 val, name, variable->max);
932 xbt_dynar_push(variable->content, &val);
935 if (variable->cb_set)
936 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
937 variable->isdefault = 0;
940 /** @brief Set or add a string value to \a name within \a cfg
942 * @param cfg the config set
943 * @param name the name of the variable
944 * @param val the value to be added
948 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
950 xbt_cfgelm_t variable;
951 char *newval = xbt_strdup(val);
953 XBT_VERB("Configuration setting: %s=%s", name, val);
954 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
955 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
956 variable->min, variable->max,
957 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
959 if (variable->max == 1) {
960 if (!xbt_dynar_is_empty(variable->content)) {
962 variable->cb_rm(name, 0);
963 else if (variable->type == xbt_cfgelm_string) {
964 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
969 xbt_dynar_set(variable->content, 0, &newval);
972 && xbt_dynar_length(variable->content) == variable->max)
973 THROWF(mismatch_error, 0,
974 "Cannot add value %s to the config element %s since it's already full (size=%d)",
975 name, val, variable->max);
977 xbt_dynar_push(variable->content, &newval);
980 if (variable->cb_set)
981 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
982 variable->isdefault = 0;
985 /** @brief Set or add a boolean value to \a name within \a cfg
987 * @param cfg the config set
988 * @param name the name of the variable
989 * @param val the value of the variable
991 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
993 xbt_cfgelm_t variable;
996 XBT_VERB("Configuration setting: %s=%s", name, val);
997 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
999 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
1000 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
1004 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
1009 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
1010 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
1013 if (variable->max == 1) {
1014 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1015 variable->cb_rm(name, 0);
1017 xbt_dynar_set(variable->content, 0, &bval);
1020 && xbt_dynar_length(variable->content) ==
1021 (unsigned long) variable->max)
1022 THROWF(mismatch_error, 0,
1023 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1024 val, name, variable->max);
1026 xbt_dynar_push(variable->content, &bval);
1029 if (variable->cb_set)
1030 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1031 variable->isdefault = 0;
1034 /** @brief Set or add an peer value to \a name within \a cfg
1036 * @param cfg the config set
1037 * @param name the name of the variable
1038 * @param peer the peer
1039 * @param port the port number
1041 * \e peer values are composed of a string (peername) and an integer (port)
1045 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1048 xbt_cfgelm_t variable;
1049 xbt_peer_t val = xbt_peer_new(peer, port);
1051 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1053 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1055 if (variable->max == 1) {
1056 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1057 variable->cb_rm(name, 0);
1059 xbt_dynar_set(variable->content, 0, &val);
1062 && xbt_dynar_length(variable->content) == variable->max)
1063 THROWF(mismatch_error, 0,
1064 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1065 peer, port, name, variable->max);
1067 xbt_dynar_push(variable->content, &val);
1070 if (variable->cb_set)
1071 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1072 variable->isdefault = 0;
1075 /* ---- [ Removing ] ---- */
1077 /** @brief Remove the provided \e val integer value from a variable
1079 * @param cfg the config set
1080 * @param name the name of the variable
1081 * @param val the value to be removed
1083 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1086 xbt_cfgelm_t variable;
1090 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1092 if (xbt_dynar_length(variable->content) == variable->min)
1093 THROWF(mismatch_error, 0,
1094 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1095 val, name, variable->min);
1097 xbt_dynar_foreach(variable->content, cpt, seen) {
1099 if (variable->cb_rm)
1100 variable->cb_rm(name, cpt);
1101 xbt_dynar_cursor_rm(variable->content, &cpt);
1106 THROWF(not_found_error, 0,
1107 "Can't remove the value %d of config element %s: value not found.",
1111 /** @brief Remove the provided \e val double value from a variable
1113 * @param cfg the config set
1114 * @param name the name of the variable
1115 * @param val the value to be removed
1118 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1120 xbt_cfgelm_t variable;
1124 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1126 if (xbt_dynar_length(variable->content) == variable->min)
1127 THROWF(mismatch_error, 0,
1128 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1129 val, name, variable->min);
1131 xbt_dynar_foreach(variable->content, cpt, seen) {
1133 xbt_dynar_cursor_rm(variable->content, &cpt);
1134 if (variable->cb_rm)
1135 variable->cb_rm(name, cpt);
1140 THROWF(not_found_error, 0,
1141 "Can't remove the value %f of config element %s: value not found.",
1145 /** @brief Remove the provided \e val string value from a variable
1147 * @param cfg the config set
1148 * @param name the name of the variable
1149 * @param val the value of the string which will be removed
1151 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1153 xbt_cfgelm_t variable;
1157 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1159 if (xbt_dynar_length(variable->content) == variable->min)
1160 THROWF(mismatch_error, 0,
1161 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1162 name, val, variable->min);
1164 xbt_dynar_foreach(variable->content, cpt, seen) {
1165 if (!strcpy(seen, val)) {
1166 if (variable->cb_rm)
1167 variable->cb_rm(name, cpt);
1168 xbt_dynar_cursor_rm(variable->content, &cpt);
1173 THROWF(not_found_error, 0,
1174 "Can't remove the value %s of config element %s: value not found.",
1178 /** @brief Remove the provided \e val boolean value from a variable
1180 * @param cfg the config set
1181 * @param name the name of the variable
1182 * @param val the value to be removed
1184 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1187 xbt_cfgelm_t variable;
1191 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1193 if (xbt_dynar_length(variable->content) == variable->min)
1194 THROWF(mismatch_error, 0,
1195 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1196 val, name, variable->min);
1198 xbt_dynar_foreach(variable->content, cpt, seen) {
1200 if (variable->cb_rm)
1201 variable->cb_rm(name, cpt);
1202 xbt_dynar_cursor_rm(variable->content, &cpt);
1207 THROWF(not_found_error, 0,
1208 "Can't remove the value %d of config element %s: value not found.",
1212 /** @brief Remove the provided \e val peer value from a variable
1214 * @param cfg the config set
1215 * @param name the name of the variable
1216 * @param peer the peername
1217 * @param port the port number
1221 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1224 xbt_cfgelm_t variable;
1228 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1230 if (xbt_dynar_length(variable->content) == variable->min)
1231 THROWF(mismatch_error, 0,
1232 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1233 peer, port, name, variable->min);
1235 xbt_dynar_foreach(variable->content, cpt, seen) {
1236 if (!strcpy(seen->name, peer) && seen->port == port) {
1237 if (variable->cb_rm)
1238 variable->cb_rm(name, cpt);
1239 xbt_dynar_cursor_rm(variable->content, &cpt);
1244 THROWF(not_found_error, 0,
1245 "Can't remove the value %s:%d of config element %s: value not found.",
1249 /** @brief Remove the \e pos th value from the provided variable */
1251 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1254 xbt_cfgelm_t variable;
1256 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1258 if (xbt_dynar_length(variable->content) == variable->min)
1259 THROWF(mismatch_error, 0,
1260 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1261 pos, name, variable->min);
1263 if (variable->cb_rm)
1264 variable->cb_rm(name, pos);
1265 xbt_dynar_remove_at(variable->content, pos, NULL);
1268 /** @brief Remove all the values from a variable
1270 * @param cfg the config set
1271 * @param name the name of the variable
1274 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1276 xbt_cfgelm_t variable = NULL;
1280 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1283 if (e.category != not_found_error)
1287 THROWF(not_found_error, 0,
1288 "Can't empty '%s' since this config element does not exist",
1293 if (variable->cb_rm) {
1296 xbt_dynar_foreach(variable->content, cpt, ignored) {
1297 variable->cb_rm(name, cpt);
1300 xbt_dynar_reset(variable->content);
1304 * Say if the value is the default value
1306 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1308 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1309 return variable->isdefault;
1312 /*----[ Getting ]---------------------------------------------------------*/
1314 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1316 * @param cfg the config set
1317 * @param name the name of the variable
1319 * Returns the first value from the config set under the given name.
1320 * If there is more than one value, it will issue a warning. Consider using
1321 * xbt_cfg_get_dynar() instead.
1323 * \warning the returned value is the actual content of the config set
1325 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1327 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1329 if (xbt_dynar_length(variable->content) > 1) {
1331 ("You asked for the first value of the config element '%s', but there is %lu values",
1332 name, xbt_dynar_length(variable->content));
1335 return xbt_dynar_get_as(variable->content, 0, int);
1338 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1340 * @param cfg the config set
1341 * @param name the name of the variable
1343 * Returns the first value from the config set under the given name.
1344 * If there is more than one value, it will issue a warning. Consider using
1345 * xbt_cfg_get_dynar() instead.
1347 * \warning the returned value is the actual content of the config set
1350 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1352 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1354 if (xbt_dynar_length(variable->content) > 1) {
1356 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1357 name, xbt_dynar_length(variable->content));
1360 return xbt_dynar_get_as(variable->content, 0, double);
1363 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1365 * @param cfg the config set
1366 * @param name the name of the variable
1368 * Returns the first value from the config set under the given name.
1369 * If there is more than one value, it will issue a warning. Consider using
1370 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1372 * \warning the returned value is the actual content of the config set
1375 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1377 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1379 if (xbt_dynar_length(variable->content) > 1) {
1381 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1382 name, xbt_dynar_length(variable->content));
1383 } else if (xbt_dynar_is_empty(variable->content)) {
1387 return xbt_dynar_get_as(variable->content, 0, char *);
1390 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1392 * @param cfg the config set
1393 * @param name the name of the variable
1395 * Returns the first value from the config set under the given name.
1396 * If there is more than one value, it will issue a warning. Consider using
1397 * xbt_cfg_get_dynar() instead.
1399 * \warning the returned value is the actual content of the config set
1401 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1403 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1405 if (xbt_dynar_length(variable->content) > 1) {
1407 ("You asked for the first value of the config element '%s', but there is %lu values",
1408 name, xbt_dynar_length(variable->content));
1411 return xbt_dynar_get_as(variable->content, 0, int);
1414 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1416 * @param cfg the config set
1417 * @param name the name of the variable
1418 * @param peer the peer
1419 * @param port the port number
1421 * Returns the first value from the config set under the given name.
1422 * If there is more than one value, it will issue a warning. Consider using
1423 * xbt_cfg_get_dynar() instead.
1425 * \warning the returned value is the actual content of the config set
1428 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1431 xbt_cfgelm_t variable;
1434 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1436 if (xbt_dynar_length(variable->content) > 1) {
1438 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1439 name, xbt_dynar_length(variable->content));
1442 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1447 /** @brief Retrieve the dynar of all the values stored in a variable
1449 * @param cfg where to search in
1450 * @param name what to search for
1452 * Get the data stored in the config set.
1454 * \warning the returned value is the actual content of the config set
1456 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1458 xbt_cfgelm_t variable = NULL;
1462 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1465 if (e.category == not_found_error) {
1467 THROWF(not_found_error, 0,
1468 "No registered variable %s in this config set", name);
1473 return variable->content;
1477 /** @brief Retrieve one of the integer value of a variable */
1478 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1481 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1482 return xbt_dynar_get_as(variable->content, pos, int);
1485 /** @brief Retrieve one of the double value of a variable */
1486 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1489 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1490 return xbt_dynar_get_as(variable->content, pos, double);
1494 /** @brief Retrieve one of the string value of a variable */
1495 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1498 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1499 return xbt_dynar_get_as(variable->content, pos, char *);
1502 /** @brief Retrieve one of the boolean value of a variable */
1503 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1506 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1507 return xbt_dynar_get_as(variable->content, pos, int);
1510 /** @brief Retrieve one of the peer value of a variable */
1512 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1513 char **peer, int *port)
1516 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1517 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1528 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1530 XBT_TEST_SUITE("config", "Configuration support");
1532 static xbt_cfg_t make_set()
1534 xbt_cfg_t set = NULL;
1536 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1537 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1538 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1539 xbt_cfg_register_str(&set, "user:1_to_10_string");
1542 } /* end_of_make_set */
1544 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1546 xbt_cfg_t set = make_set();
1547 xbt_test_add("Alloc and free a config set");
1548 xbt_cfg_set_parse(set,
1549 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1554 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1556 xbt_cfg_t set = set = make_set();
1559 xbt_test_add("Having too few elements for speed");
1560 xbt_cfg_set_parse(set,
1561 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1566 if (e.category != mismatch_error ||
1567 strncmp(e.msg, "Config elem speed needs",
1568 strlen("Config elem speed needs")))
1569 xbt_test_fail("Got an exception. msg=%s", e.msg);
1577 xbt_test_add("Having too much values of 'speed'");
1579 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1581 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1584 if (e.category != mismatch_error ||
1585 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1586 strlen("Config elem speed needs")))
1587 xbt_test_fail("Got an exception. msg=%s", e.msg);
1596 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1599 xbt_test_add("Get a single value");
1601 /* get_single_value */
1603 xbt_cfg_t myset = make_set();
1605 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1606 ival = xbt_cfg_get_int(myset, "speed");
1608 xbt_test_fail("Speed value = %d, I expected 42", ival);
1609 xbt_cfg_free(&myset);
1612 xbt_test_add("Get multiple values");
1614 /* get_multiple_value */
1616 xbt_cfg_t myset = make_set();
1618 xbt_cfg_set_parse(myset,
1619 "peername:veloce user:foo\nuser:bar\tuser:toto");
1620 xbt_cfg_set_parse(myset, "speed:42");
1621 xbt_cfg_check(myset);
1622 dyn = xbt_cfg_get_dynar(myset, "user");
1624 if (xbt_dynar_length(dyn) != 3)
1625 xbt_test_fail("Dynar length = %lu, I expected 3",
1626 xbt_dynar_length(dyn));
1628 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1629 xbt_test_fail("Dynar[0] = %s, I expected foo",
1630 xbt_dynar_get_as(dyn, 0, char *));
1632 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1633 xbt_test_fail("Dynar[1] = %s, I expected bar",
1634 xbt_dynar_get_as(dyn, 1, char *));
1636 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1637 xbt_test_fail("Dynar[2] = %s, I expected toto",
1638 xbt_dynar_get_as(dyn, 2, char *));
1639 xbt_cfg_free(&myset);
1642 xbt_test_add("Access to a non-existant entry");
1644 /* non-existant_entry */
1645 xbt_cfg_t myset = make_set();
1649 xbt_cfg_set_parse(myset, "color:blue");
1652 if (e.category != not_found_error)
1653 xbt_test_exception(e);
1656 xbt_cfg_free(&myset);
1659 #endif /* SIMGRID_TEST */