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-2013. 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. */
13 #include "xbt/sysdep.h"
16 #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 uses both str and i) */
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", "peer", "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;
130 printf("%s>> Dumping of the config set '%s':\n", indent, name);
131 xbt_dict_foreach(dict, cursor, key, variable) {
133 printf("%s %s:", indent, key);
135 size = xbt_dynar_length(variable->content);
137 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
138 variable->min, variable->max,
139 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
142 switch (variable->type) {
145 for (i = 0; i < size; i++) {
146 ival = xbt_dynar_get_as(variable->content, i, int);
147 printf("%s %d\n", indent, ival);
151 case xbt_cfgelm_double:
152 for (i = 0; i < size; i++) {
153 dval = xbt_dynar_get_as(variable->content, i, double);
154 printf("%s %f\n", indent, dval);
158 case xbt_cfgelm_string:
159 for (i = 0; i < size; i++) {
160 sval = xbt_dynar_get_as(variable->content, i, char *);
161 printf("%s %s\n", indent, sval);
165 case xbt_cfgelm_boolean:
166 for (i = 0; i < size; i++) {
167 ival = xbt_dynar_get_as(variable->content, i, int);
168 printf("%s %d\n", indent, ival);
172 case xbt_cfgelm_peer:
173 for (i = 0; i < size; i++) {
174 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
175 printf("%s %s:%d\n", indent, hval->name, hval->port);
180 printf("%s Invalid type!!\n", indent);
186 printf("%s<< End of the config set '%s'\n", indent, name);
189 xbt_dict_cursor_free(&cursor);
194 * free an config element
197 void xbt_cfgelm_free(void *data)
199 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
201 XBT_DEBUG("Frees cfgelm %p", c);
205 xbt_dynar_free(&(c->content));
209 /*----[ Registering stuff ]-----------------------------------------------*/
211 /** @brief Register an element within a config set
213 * @param cfg the config set
214 * @param name the name of the config element
215 * @param desc a description for this item (used by xbt_cfg_help())
216 * @param type the type of the config element
217 * @param min the minimum number of values for this config element
218 * @param max the maximum number of values for this config element
219 * @param cb_set callback function called when a value is set
220 * @param cb_rm callback function called when a value is removed
224 xbt_cfg_register(xbt_cfg_t * cfg,
225 const char *name, const char *desc,
226 e_xbt_cfgelm_type_t type, int min,
227 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
232 *cfg = xbt_cfg_new();
233 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
234 "type of %s not valid (%d should be between %d and %d)",
235 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer);
236 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
239 XBT_WARN("Config elem %s registered twice.", name);
240 /* Will be removed by the insertion of the new one */
243 res = xbt_new(s_xbt_cfgelm_t, 1);
244 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
245 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
248 res->desc = xbt_strdup(desc);
252 res->cb_set = cb_set;
258 res->content = xbt_dynar_new(sizeof(int), NULL);
261 case xbt_cfgelm_double:
262 res->content = xbt_dynar_new(sizeof(double), NULL);
265 case xbt_cfgelm_string:
266 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
269 case xbt_cfgelm_boolean:
270 res->content = xbt_dynar_new(sizeof(int), NULL);
273 case xbt_cfgelm_peer:
274 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
278 XBT_ERROR("%d is an invalide type code", (int)type);
281 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
284 /** @brief Unregister an element from a config set.
286 * @param cfg the config set
287 * @param name the name of the elem to be freed
289 * Note that it removes both the description and the actual content.
290 * Throws not_found when no such element exists.
293 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
295 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
296 xbt_dict_remove((xbt_dict_t) cfg, name);
300 * @brief Parse a string and register the stuff described.
302 * @param cfg the config set
303 * @param entry a string describing the element to register
305 * The string may consist in several variable descriptions separated by a space.
306 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
307 * with type being one of 'string','int', 'peer' or 'double'.
309 * FIXME: this does not allow to set the description
312 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
314 char *entrycpy = xbt_strdup(entry);
318 e_xbt_cfgelm_type_t type;
319 XBT_DEBUG("Register string '%s'", entry);
321 tok = strchr(entrycpy, ':');
322 xbt_assert(tok, "Invalid config element descriptor: %s%s",
323 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
326 min = strtol(tok, &tok, 10);
327 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
330 xbt_assert(strcmp(tok, "_to_"),
331 "Invalid config element descriptor : %s%s",
332 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
333 tok += strlen("_to_");
335 max = strtol(tok, &tok, 10);
336 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
339 xbt_assert(*tok == '_',
340 "Invalid config element descriptor: %s%s", entry,
341 "; Should be <name>:<min nb>_to_<max nb>_<type>");
344 for (type = (e_xbt_cfgelm_type_t)0;
345 type < xbt_cfgelm_type_count
346 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
347 xbt_assert(type < xbt_cfgelm_type_count,
348 "Invalid type in config element descriptor: %s%s", entry,
349 "; Should be one of 'string', 'int', 'peer' or 'double'.");
351 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
353 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
356 static int strcmp_voidp(const void *pa, const void *pb)
358 return strcmp(*(const char **)pa, *(const char **)pb);
361 /** @brief Displays the declared options and their description */
362 void xbt_cfg_help(xbt_cfg_t cfg)
364 xbt_dict_cursor_t dict_cursor;
365 unsigned int dynar_cursor;
366 xbt_cfgelm_t variable;
368 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
370 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
371 xbt_dynar_push(names, &name);
373 xbt_dynar_sort(names, strcmp_voidp);
375 xbt_dynar_foreach(names, dynar_cursor, name) {
378 variable = xbt_dict_get((xbt_dict_t )cfg, name);
380 printf(" %s: %s\n", name, variable->desc);
381 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
382 if (variable->min != 1 || variable->max != 1) {
383 if (variable->min == 0 && variable->max == 0)
384 printf("Arity: no bound; ");
386 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
388 size = xbt_dynar_length(variable->content);
389 printf("Current value%s: ", (size <= 1 ? "" : "s"));
392 printf(size == 0 ? "n/a\n" : "{ ");
393 for (i = 0; i < size; i++) {
394 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
396 switch (variable->type) {
398 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
401 case xbt_cfgelm_double:
402 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
405 case xbt_cfgelm_string:
406 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
409 case xbt_cfgelm_boolean: {
410 int b = xbt_dynar_get_as(variable->content, i, int);
411 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
412 : xbt_cfgelm_boolean_values[0].false_val;
413 if (b == 0 || b == 1)
414 printf("'%s'%s", bs, sep);
416 printf("'%s/%d'%s", bs, b, sep);
420 case xbt_cfgelm_peer: {
421 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
422 printf("%s:%d%s", hval->name, hval->port, sep);
427 printf("Invalid type!!%s", sep);
432 xbt_dynar_free(&names);
435 /** @brief Check that each variable have the right amount of values */
436 void xbt_cfg_check(xbt_cfg_t cfg)
438 xbt_dict_cursor_t cursor;
439 xbt_cfgelm_t variable;
443 xbt_assert(cfg, "NULL config set.");
444 XBT_DEBUG("Check cfg set %p", cfg);
446 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
447 size = xbt_dynar_length(variable->content);
448 if (variable->min > size) {
449 xbt_dict_cursor_free(&cursor);
450 THROWF(mismatch_error, 0,
451 "Config elem %s needs at least %d %s, but there is only %d values.",
452 name, variable->min, xbt_cfgelm_type_name[variable->type],
456 if (variable->max > 0 && variable->max < size) {
457 xbt_dict_cursor_free(&cursor);
458 THROWF(mismatch_error, 0,
459 "Config elem %s accepts at most %d %s, but there is %d values.",
460 name, variable->max, xbt_cfgelm_type_name[variable->type],
465 xbt_dict_cursor_free(&cursor);
468 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
470 e_xbt_cfgelm_type_t type)
472 xbt_cfgelm_t res = NULL;
474 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
477 THROWF(not_found_error, 0,
478 "No registered variable '%s' in this config set", name);
481 xbt_assert(type == xbt_cfgelm_any || res->type == type,
482 "You tried to access to the config element %s as an %s, but its type is %s.",
484 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
497 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
500 xbt_cfgelm_t variable = NULL;
502 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
504 THROWF(not_found_error, 0,
505 "Can't get the type of '%s' since this variable does not exist",
508 XBT_DEBUG("type in variable = %d", (int)variable->type);
510 return variable->type;
513 /*----[ Setting ]---------------------------------------------------------*/
514 /** @brief va_args version of xbt_cfg_set
516 * @param cfg config set to fill
517 * @param name variable name
518 * @param pa variable value
520 * Add some values to the config set.
522 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
527 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
532 type = xbt_cfg_get_type(cfg, name);
535 if (e.category == not_found_error) {
537 THROWF(not_found_error, 0,
538 "Can't set the property '%s' since it's not registered",
545 case xbt_cfgelm_peer:
546 str = va_arg(pa, char *);
548 xbt_cfg_set_peer(cfg, name, str, i);
551 case xbt_cfgelm_string:
552 str = va_arg(pa, char *);
553 xbt_cfg_set_string(cfg, name, str);
558 xbt_cfg_set_int(cfg, name, i);
561 case xbt_cfgelm_double:
562 d = va_arg(pa, double);
563 xbt_cfg_set_double(cfg, name, d);
566 case xbt_cfgelm_boolean:
567 str = va_arg(pa, char *);
568 xbt_cfg_set_boolean(cfg, name, str);
572 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
576 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
578 * @param cfg config set to fill
579 * @param name variable name
580 * @param ... variable value
583 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
588 xbt_cfg_set_vargs(cfg, name, pa);
592 /** @brief Add values parsed from a string into a config set
594 * @param cfg config set to fill
595 * @param options a string containing the content to add to the config set. This
596 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
597 * like "[name]:[value]" where [name] is the name of an already registred
598 * variable, and [value] conforms to the data type under which this variable was
601 * @todo This is a crude manual parser, it should be a proper lexer.
604 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
606 char *optionlist_cpy;
607 char *option, *name, *val;
612 if (!options || !strlen(options)) { /* nothing to do */
615 optionlist_cpy = xbt_strdup(options);
617 XBT_DEBUG("List to parse and set:'%s'", options);
618 option = optionlist_cpy;
619 while (1) { /* breaks in the code */
625 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
626 name, len, (long) (option - name));
629 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
630 && *option != '\t' && *option != ',') {
631 XBT_DEBUG("Take %c.", *option);
634 if (option - name == len) {
635 XBT_DEBUG("Boundary=EOL");
636 option = NULL; /* don't do next iteration */
639 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
640 *option, len, (long) (option - name));
642 /* Pass the following blank chars */
644 while (option - name < (len - 1) &&
645 (*option == ' ' || *option == '\n' || *option == '\t')) {
646 /* fprintf(stderr,"Ignore a blank char.\n"); */
649 if (option - name == len - 1)
650 option = NULL; /* don't do next iteration */
652 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
654 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
659 val = strchr(name, ':');
661 /* don't free(optionlist_cpy) here, 'name' points inside it */
662 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
667 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
668 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
671 xbt_cfg_set_as_string(cfg,name,val);
673 free(optionlist_cpy);
677 free(optionlist_cpy);
680 /** @brief Set the value of a variable, using the string representation of that value
682 * @param cfg config set to modify
683 * @param key name of the variable to modify
684 * @param value string representation of the value to set
686 * @return the first char after the parsed value in val
689 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
693 volatile xbt_cfgelm_t variable = NULL;
700 variable = xbt_dict_get((xbt_dict_t) cfg, key);
703 if (e.category == not_found_error) {
705 THROWF(not_found_error, 0,
706 "No registered variable corresponding to '%s'.", key);
711 switch (variable->type) {
712 case xbt_cfgelm_string:
713 xbt_cfg_set_string(cfg, key, value); /* throws */
717 i = strtol(value, &ret, 0);
719 xbt_die("Value of option %s not valid. Should be an integer", key);
722 xbt_cfg_set_int(cfg, key, i); /* throws */
725 case xbt_cfgelm_double:
726 d = strtod(value, &ret);
728 xbt_die("Value of option %s not valid. Should be a double", key);
731 xbt_cfg_set_double(cfg, key, d); /* throws */
734 case xbt_cfgelm_boolean:
735 xbt_cfg_set_boolean(cfg, key, value); /* throws */
738 case xbt_cfgelm_peer:
739 val = xbt_strdup(value);
741 val = strchr(val, ':');
743 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
747 i = strtol(val, &ret, 0);
749 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
752 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
757 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
764 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
766 * This is useful to change the default value of a variable while allowing
767 * users to override it with command line arguments
769 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
771 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
773 if (variable->isdefault){
774 xbt_cfg_set_int(cfg, name, val);
775 variable->isdefault = 1;
779 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
783 /** @brief Set an integer 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_double(xbt_cfg_t cfg, const char *name, double val)
790 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
792 if (variable->isdefault) {
793 xbt_cfg_set_double(cfg, name, val);
794 variable->isdefault = 1;
798 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
802 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
804 * This is useful to change the default value of a variable while allowing
805 * users to override it with command line arguments
807 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
810 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
812 if (variable->isdefault){
813 xbt_cfg_set_string(cfg, name, val);
814 variable->isdefault = 1;
818 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
823 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
825 * This is useful to change the default value of a variable while allowing
826 * users to override it with command line arguments
828 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
830 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
832 if (variable->isdefault){
833 xbt_cfg_set_boolean(cfg, name, val);
834 variable->isdefault = 1;
838 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
842 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
844 * This is useful to change the default value of a variable while allowing
845 * users to override it with command line arguments
847 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
848 const char *host, int port)
850 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
852 if (variable->isdefault){
853 xbt_cfg_set_peer(cfg, name, host, port);
854 variable->isdefault = 1;
858 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
862 /** @brief Set or add an integer value to \a name within \a cfg
864 * @param cfg the config set
865 * @param name the name of the variable
866 * @param val the value of the variable
868 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
870 xbt_cfgelm_t variable;
872 XBT_VERB("Configuration setting: %s=%d", name, val);
873 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
875 if (variable->max == 1) {
876 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
877 variable->cb_rm(name, 0);
879 xbt_dynar_set(variable->content, 0, &val);
882 && xbt_dynar_length(variable->content) ==
883 (unsigned long) variable->max)
884 THROWF(mismatch_error, 0,
885 "Cannot add value %d to the config element %s since it's already full (size=%d)",
886 val, name, variable->max);
888 xbt_dynar_push(variable->content, &val);
891 if (variable->cb_set)
892 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
893 variable->isdefault = 0;
896 /** @brief Set or add a double value to \a name within \a cfg
898 * @param cfg the config set
899 * @param name the name of the variable
900 * @param val the doule to set
903 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
905 xbt_cfgelm_t variable;
907 XBT_VERB("Configuration setting: %s=%f", name, val);
908 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
910 if (variable->max == 1) {
911 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
912 variable->cb_rm(name, 0);
914 xbt_dynar_set(variable->content, 0, &val);
917 && xbt_dynar_length(variable->content) == variable->max)
918 THROWF(mismatch_error, 0,
919 "Cannot add value %f to the config element %s since it's already full (size=%d)",
920 val, name, variable->max);
922 xbt_dynar_push(variable->content, &val);
925 if (variable->cb_set)
926 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
927 variable->isdefault = 0;
930 /** @brief Set or add a string value to \a name within \a cfg
932 * @param cfg the config set
933 * @param name the name of the variable
934 * @param val the value to be added
938 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
940 xbt_cfgelm_t variable;
941 char *newval = xbt_strdup(val);
943 XBT_VERB("Configuration setting: %s=%s", name, val);
944 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
945 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
946 variable->min, variable->max,
947 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
949 if (variable->max == 1) {
950 if (!xbt_dynar_is_empty(variable->content)) {
952 variable->cb_rm(name, 0);
953 else if (variable->type == xbt_cfgelm_string) {
954 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
959 xbt_dynar_set(variable->content, 0, &newval);
962 && xbt_dynar_length(variable->content) == variable->max)
963 THROWF(mismatch_error, 0,
964 "Cannot add value %s to the config element %s since it's already full (size=%d)",
965 name, val, variable->max);
967 xbt_dynar_push(variable->content, &newval);
970 if (variable->cb_set)
971 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
972 variable->isdefault = 0;
975 /** @brief Set or add a boolean value to \a name within \a cfg
977 * @param cfg the config set
978 * @param name the name of the variable
979 * @param val the value of the variable
981 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
983 xbt_cfgelm_t variable;
986 XBT_VERB("Configuration setting: %s=%s", name, val);
987 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
989 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
990 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
994 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
999 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
1000 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
1003 if (variable->max == 1) {
1004 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1005 variable->cb_rm(name, 0);
1007 xbt_dynar_set(variable->content, 0, &bval);
1010 && xbt_dynar_length(variable->content) ==
1011 (unsigned long) variable->max)
1012 THROWF(mismatch_error, 0,
1013 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1014 val, name, variable->max);
1016 xbt_dynar_push(variable->content, &bval);
1019 if (variable->cb_set)
1020 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1021 variable->isdefault = 0;
1024 /** @brief Set or add an peer value to \a name within \a cfg
1026 * @param cfg the config set
1027 * @param name the name of the variable
1028 * @param peer the peer
1029 * @param port the port number
1031 * \e peer values are composed of a string (peername) and an integer (port)
1035 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1038 xbt_cfgelm_t variable;
1039 xbt_peer_t val = xbt_peer_new(peer, port);
1041 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1043 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1045 if (variable->max == 1) {
1046 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1047 variable->cb_rm(name, 0);
1049 xbt_dynar_set(variable->content, 0, &val);
1052 && xbt_dynar_length(variable->content) == variable->max)
1053 THROWF(mismatch_error, 0,
1054 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1055 peer, port, name, variable->max);
1057 xbt_dynar_push(variable->content, &val);
1060 if (variable->cb_set)
1061 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1062 variable->isdefault = 0;
1065 /* ---- [ Removing ] ---- */
1067 /** @brief Remove the provided \e val integer 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_int(xbt_cfg_t cfg, const char *name, int val)
1076 xbt_cfgelm_t variable;
1080 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
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 provided \e val double value from a variable
1103 * @param cfg the config set
1104 * @param name the name of the variable
1105 * @param val the value to be removed
1108 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1110 xbt_cfgelm_t variable;
1114 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1116 if (xbt_dynar_length(variable->content) == variable->min)
1117 THROWF(mismatch_error, 0,
1118 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1119 val, name, variable->min);
1121 xbt_dynar_foreach(variable->content, cpt, seen) {
1123 xbt_dynar_cursor_rm(variable->content, &cpt);
1124 if (variable->cb_rm)
1125 variable->cb_rm(name, cpt);
1130 THROWF(not_found_error, 0,
1131 "Can't remove the value %f of config element %s: value not found.",
1135 /** @brief Remove the provided \e val string value from a variable
1137 * @param cfg the config set
1138 * @param name the name of the variable
1139 * @param val the value of the string which will be removed
1141 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1143 xbt_cfgelm_t variable;
1147 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1149 if (xbt_dynar_length(variable->content) == variable->min)
1150 THROWF(mismatch_error, 0,
1151 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1152 name, val, variable->min);
1154 xbt_dynar_foreach(variable->content, cpt, seen) {
1155 if (!strcpy(seen, val)) {
1156 if (variable->cb_rm)
1157 variable->cb_rm(name, cpt);
1158 xbt_dynar_cursor_rm(variable->content, &cpt);
1163 THROWF(not_found_error, 0,
1164 "Can't remove the value %s of config element %s: value not found.",
1168 /** @brief Remove the provided \e val boolean value from a variable
1170 * @param cfg the config set
1171 * @param name the name of the variable
1172 * @param val the value to be removed
1174 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1177 xbt_cfgelm_t variable;
1181 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1183 if (xbt_dynar_length(variable->content) == variable->min)
1184 THROWF(mismatch_error, 0,
1185 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1186 val, name, variable->min);
1188 xbt_dynar_foreach(variable->content, cpt, seen) {
1190 if (variable->cb_rm)
1191 variable->cb_rm(name, cpt);
1192 xbt_dynar_cursor_rm(variable->content, &cpt);
1197 THROWF(not_found_error, 0,
1198 "Can't remove the value %d of config element %s: value not found.",
1202 /** @brief Remove the provided \e val peer value from a variable
1204 * @param cfg the config set
1205 * @param name the name of the variable
1206 * @param peer the peername
1207 * @param port the port number
1211 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1214 xbt_cfgelm_t variable;
1218 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1220 if (xbt_dynar_length(variable->content) == variable->min)
1221 THROWF(mismatch_error, 0,
1222 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1223 peer, port, name, variable->min);
1225 xbt_dynar_foreach(variable->content, cpt, seen) {
1226 if (!strcpy(seen->name, peer) && seen->port == port) {
1227 if (variable->cb_rm)
1228 variable->cb_rm(name, cpt);
1229 xbt_dynar_cursor_rm(variable->content, &cpt);
1234 THROWF(not_found_error, 0,
1235 "Can't remove the value %s:%d of config element %s: value not found.",
1239 /** @brief Remove the \e pos th value from the provided variable */
1241 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1244 xbt_cfgelm_t variable;
1246 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1248 if (xbt_dynar_length(variable->content) == variable->min)
1249 THROWF(mismatch_error, 0,
1250 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1251 pos, name, variable->min);
1253 if (variable->cb_rm)
1254 variable->cb_rm(name, pos);
1255 xbt_dynar_remove_at(variable->content, pos, NULL);
1258 /** @brief Remove all the values from a variable
1260 * @param cfg the config set
1261 * @param name the name of the variable
1264 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1266 xbt_cfgelm_t variable = NULL;
1270 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1273 if (e.category != not_found_error)
1277 THROWF(not_found_error, 0,
1278 "Can't empty '%s' since this config element does not exist",
1283 if (variable->cb_rm) {
1286 xbt_dynar_foreach(variable->content, cpt, ignored) {
1287 variable->cb_rm(name, cpt);
1290 xbt_dynar_reset(variable->content);
1294 * Say if the value is the default value
1296 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1298 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1299 return variable->isdefault;
1302 /*----[ Getting ]---------------------------------------------------------*/
1304 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1306 * @param cfg the config set
1307 * @param name the name of the variable
1309 * Returns the first value from the config set under the given name.
1310 * If there is more than one value, it will issue a warning. Consider using
1311 * xbt_cfg_get_dynar() instead.
1313 * \warning the returned value is the actual content of the config set
1315 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1317 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1319 if (xbt_dynar_length(variable->content) > 1) {
1321 ("You asked for the first value of the config element '%s', but there is %lu values",
1322 name, xbt_dynar_length(variable->content));
1325 return xbt_dynar_get_as(variable->content, 0, int);
1328 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1330 * @param cfg the config set
1331 * @param name the name of the variable
1333 * Returns the first value from the config set under the given name.
1334 * If there is more than one value, it will issue a warning. Consider using
1335 * xbt_cfg_get_dynar() instead.
1337 * \warning the returned value is the actual content of the config set
1340 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1342 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1344 if (xbt_dynar_length(variable->content) > 1) {
1346 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1347 name, xbt_dynar_length(variable->content));
1350 return xbt_dynar_get_as(variable->content, 0, double);
1353 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1355 * @param cfg the config set
1356 * @param name the name of the variable
1358 * Returns the first value from the config set under the given name.
1359 * If there is more than one value, it will issue a warning. Consider using
1360 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1362 * \warning the returned value is the actual content of the config set
1365 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1367 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1369 if (xbt_dynar_length(variable->content) > 1) {
1371 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1372 name, xbt_dynar_length(variable->content));
1373 } else if (xbt_dynar_is_empty(variable->content)) {
1377 return xbt_dynar_get_as(variable->content, 0, char *);
1380 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1382 * @param cfg the config set
1383 * @param name the name of the variable
1385 * Returns the first value from the config set under the given name.
1386 * If there is more than one value, it will issue a warning. Consider using
1387 * xbt_cfg_get_dynar() instead.
1389 * \warning the returned value is the actual content of the config set
1391 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1393 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1395 if (xbt_dynar_length(variable->content) > 1) {
1397 ("You asked for the first value of the config element '%s', but there is %lu values",
1398 name, xbt_dynar_length(variable->content));
1401 return xbt_dynar_get_as(variable->content, 0, int);
1404 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1406 * @param cfg the config set
1407 * @param name the name of the variable
1408 * @param peer the peer
1409 * @param port the port number
1411 * Returns the first value from the config set under the given name.
1412 * If there is more than one value, it will issue a warning. Consider using
1413 * xbt_cfg_get_dynar() instead.
1415 * \warning the returned value is the actual content of the config set
1418 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1421 xbt_cfgelm_t variable;
1424 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1426 if (xbt_dynar_length(variable->content) > 1) {
1428 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1429 name, xbt_dynar_length(variable->content));
1432 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1437 /** @brief Retrieve the dynar of all the values stored in a variable
1439 * @param cfg where to search in
1440 * @param name what to search for
1442 * Get the data stored in the config set.
1444 * \warning the returned value is the actual content of the config set
1446 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1448 xbt_cfgelm_t variable = NULL;
1452 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1455 if (e.category == not_found_error) {
1457 THROWF(not_found_error, 0,
1458 "No registered variable %s in this config set", name);
1463 return variable->content;
1467 /** @brief Retrieve one of the integer value of a variable */
1468 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1471 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1472 return xbt_dynar_get_as(variable->content, pos, int);
1475 /** @brief Retrieve one of the double value of a variable */
1476 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1479 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1480 return xbt_dynar_get_as(variable->content, pos, double);
1484 /** @brief Retrieve one of the string value of a variable */
1485 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1488 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1489 return xbt_dynar_get_as(variable->content, pos, char *);
1492 /** @brief Retrieve one of the boolean value of a variable */
1493 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1496 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1497 return xbt_dynar_get_as(variable->content, pos, int);
1500 /** @brief Retrieve one of the peer value of a variable */
1502 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1503 char **peer, int *port)
1506 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1507 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1518 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1520 XBT_TEST_SUITE("config", "Configuration support");
1522 static xbt_cfg_t make_set()
1524 xbt_cfg_t set = NULL;
1526 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1527 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1528 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1529 xbt_cfg_register_str(&set, "user:1_to_10_string");
1532 } /* end_of_make_set */
1534 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1536 xbt_cfg_t set = make_set();
1537 xbt_test_add("Alloc and free a config set");
1538 xbt_cfg_set_parse(set,
1539 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1544 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1546 xbt_cfg_t set = set = make_set();
1549 xbt_test_add("Having too few elements for speed");
1550 xbt_cfg_set_parse(set,
1551 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1556 if (e.category != mismatch_error ||
1557 strncmp(e.msg, "Config elem speed needs",
1558 strlen("Config elem speed needs")))
1559 xbt_test_fail("Got an exception. msg=%s", e.msg);
1567 xbt_test_add("Having too much values of 'speed'");
1569 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1571 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1574 if (e.category != mismatch_error ||
1575 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1576 strlen("Config elem speed needs")))
1577 xbt_test_fail("Got an exception. msg=%s", e.msg);
1586 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1589 xbt_test_add("Get a single value");
1591 /* get_single_value */
1593 xbt_cfg_t myset = make_set();
1595 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1596 ival = xbt_cfg_get_int(myset, "speed");
1598 xbt_test_fail("Speed value = %d, I expected 42", ival);
1599 xbt_cfg_free(&myset);
1602 xbt_test_add("Get multiple values");
1604 /* get_multiple_value */
1606 xbt_cfg_t myset = make_set();
1608 xbt_cfg_set_parse(myset,
1609 "peername:veloce user:foo\nuser:bar\tuser:toto");
1610 xbt_cfg_set_parse(myset, "speed:42");
1611 xbt_cfg_check(myset);
1612 dyn = xbt_cfg_get_dynar(myset, "user");
1614 if (xbt_dynar_length(dyn) != 3)
1615 xbt_test_fail("Dynar length = %lu, I expected 3",
1616 xbt_dynar_length(dyn));
1618 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1619 xbt_test_fail("Dynar[0] = %s, I expected foo",
1620 xbt_dynar_get_as(dyn, 0, char *));
1622 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1623 xbt_test_fail("Dynar[1] = %s, I expected bar",
1624 xbt_dynar_get_as(dyn, 1, char *));
1626 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1627 xbt_test_fail("Dynar[2] = %s, I expected toto",
1628 xbt_dynar_get_as(dyn, 2, char *));
1629 xbt_cfg_free(&myset);
1632 xbt_test_add("Access to a non-existant entry");
1634 /* non-existant_entry */
1635 xbt_cfg_t myset = make_set();
1639 xbt_cfg_set_parse(myset, "color:blue");
1642 if (e.category != not_found_error)
1643 xbt_test_exception(e);
1646 xbt_cfg_free(&myset);
1649 #endif /* SIMGRID_TEST */