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 * \arg whereto the config set to be created
81 * \arg 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 * \arg name The name to give to this config set
113 * \arg indent what to write at the begining of each line (right number of spaces)
114 * \arg 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 * @arg cfg the config set
214 * @arg type the type of the config element
215 * @arg min the minimum
216 * @arg max the maximum
220 xbt_cfg_register(xbt_cfg_t * cfg,
221 const char *name, const char *desc,
222 e_xbt_cfgelm_type_t type, int min,
223 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
228 *cfg = xbt_cfg_new();
229 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
230 "type of %s not valid (%d should be between %d and %d)",
231 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer);
232 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
235 XBT_WARN("Config elem %s registered twice.", name);
236 /* Will be removed by the insertion of the new one */
239 res = xbt_new(s_xbt_cfgelm_t, 1);
240 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
241 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
244 res->desc = xbt_strdup(desc);
248 res->cb_set = cb_set;
254 res->content = xbt_dynar_new(sizeof(int), NULL);
257 case xbt_cfgelm_double:
258 res->content = xbt_dynar_new(sizeof(double), NULL);
261 case xbt_cfgelm_string:
262 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
265 case xbt_cfgelm_boolean:
266 res->content = xbt_dynar_new(sizeof(int), NULL);
269 case xbt_cfgelm_peer:
270 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
274 XBT_ERROR("%d is an invalide type code", (int)type);
277 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
280 /** @brief Unregister an element from a config set.
282 * @arg cfg the config set
283 * @arg name the name of the elem to be freed
285 * Note that it removes both the description and the actual content.
286 * Throws not_found when no such element exists.
289 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
291 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
292 xbt_dict_remove((xbt_dict_t) cfg, name);
296 * @brief Parse a string and register the stuff described.
298 * @arg cfg the config set
299 * @arg entry a string describing the element to register
301 * The string may consist in several variable descriptions separated by a space.
302 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
303 * with type being one of 'string','int', 'peer' or 'double'.
305 * FIXME: this does not allow to set the description
308 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
310 char *entrycpy = xbt_strdup(entry);
314 e_xbt_cfgelm_type_t type;
315 XBT_DEBUG("Register string '%s'", entry);
317 tok = strchr(entrycpy, ':');
318 xbt_assert(tok, "Invalid config element descriptor: %s%s",
319 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
322 min = strtol(tok, &tok, 10);
323 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
326 xbt_assert(strcmp(tok, "_to_"),
327 "Invalid config element descriptor : %s%s",
328 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
329 tok += strlen("_to_");
331 max = strtol(tok, &tok, 10);
332 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
335 xbt_assert(*tok == '_',
336 "Invalid config element descriptor: %s%s", entry,
337 "; Should be <name>:<min nb>_to_<max nb>_<type>");
340 for (type = (e_xbt_cfgelm_type_t)0;
341 type < xbt_cfgelm_type_count
342 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
343 xbt_assert(type < xbt_cfgelm_type_count,
344 "Invalid type in config element descriptor: %s%s", entry,
345 "; Should be one of 'string', 'int', 'peer' or 'double'.");
347 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
349 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
352 static int strcmp_voidp(const void *pa, const void *pb)
354 return strcmp(*(const char **)pa, *(const char **)pb);
357 /** @brief Displays the declared options and their description */
358 void xbt_cfg_help(xbt_cfg_t cfg)
360 xbt_dict_cursor_t dict_cursor;
361 unsigned int dynar_cursor;
362 xbt_cfgelm_t variable;
364 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
366 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
367 xbt_dynar_push(names, &name);
369 xbt_dynar_sort(names, strcmp_voidp);
371 xbt_dynar_foreach(names, dynar_cursor, name) {
374 variable = xbt_dict_get((xbt_dict_t )cfg, name);
376 printf(" %s: %s\n", name, variable->desc);
377 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
378 if (variable->min != 1 || variable->max != 1) {
379 if (variable->min == 0 && variable->max == 0)
380 printf("Arity: no bound; ");
382 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
384 size = xbt_dynar_length(variable->content);
385 printf("Current value%s: ", (size <= 1 ? "" : "s"));
388 printf(size == 0 ? "n/a\n" : "{ ");
389 for (i = 0; i < size; i++) {
390 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
392 switch (variable->type) {
394 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
397 case xbt_cfgelm_double:
398 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
401 case xbt_cfgelm_string:
402 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
405 case xbt_cfgelm_boolean: {
406 int b = xbt_dynar_get_as(variable->content, i, int);
407 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
408 : xbt_cfgelm_boolean_values[0].false_val;
409 if (b == 0 || b == 1)
410 printf("'%s'%s", bs, sep);
412 printf("'%s/%d'%s", bs, b, sep);
416 case xbt_cfgelm_peer: {
417 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
418 printf("%s:%d%s", hval->name, hval->port, sep);
423 printf("Invalid type!!%s", sep);
428 xbt_dynar_free(&names);
431 /** @brief Check that each variable have the right amount of values */
432 void xbt_cfg_check(xbt_cfg_t cfg)
434 xbt_dict_cursor_t cursor;
435 xbt_cfgelm_t variable;
439 xbt_assert(cfg, "NULL config set.");
440 XBT_DEBUG("Check cfg set %p", cfg);
442 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
443 size = xbt_dynar_length(variable->content);
444 if (variable->min > size) {
445 xbt_dict_cursor_free(&cursor);
446 THROWF(mismatch_error, 0,
447 "Config elem %s needs at least %d %s, but there is only %d values.",
448 name, variable->min, xbt_cfgelm_type_name[variable->type],
452 if (variable->max > 0 && variable->max < size) {
453 xbt_dict_cursor_free(&cursor);
454 THROWF(mismatch_error, 0,
455 "Config elem %s accepts at most %d %s, but there is %d values.",
456 name, variable->max, xbt_cfgelm_type_name[variable->type],
461 xbt_dict_cursor_free(&cursor);
464 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
466 e_xbt_cfgelm_type_t type)
468 xbt_cfgelm_t res = NULL;
470 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
473 THROWF(not_found_error, 0,
474 "No registered variable '%s' in this config set", name);
477 xbt_assert(type == xbt_cfgelm_any || res->type == type,
478 "You tried to access to the config element %s as an %s, but its type is %s.",
480 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
485 /** @brief Get the type of this variable in that configuration set
487 * \arg cfg the config set
488 * \arg name the name of the element
489 * \arg type the result
493 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
496 xbt_cfgelm_t variable = NULL;
498 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
500 THROWF(not_found_error, 0,
501 "Can't get the type of '%s' since this variable does not exist",
504 XBT_DEBUG("type in variable = %d", (int)variable->type);
506 return variable->type;
509 /*----[ Setting ]---------------------------------------------------------*/
510 /** @brief va_args version of xbt_cfg_set
512 * \arg cfg config set to fill
513 * \arg n variable name
514 * \arg pa variable value
516 * Add some values to the config set.
518 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
523 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
528 type = xbt_cfg_get_type(cfg, name);
531 if (e.category == not_found_error) {
533 THROWF(not_found_error, 0,
534 "Can't set the property '%s' since it's not registered",
541 case xbt_cfgelm_peer:
542 str = va_arg(pa, char *);
544 xbt_cfg_set_peer(cfg, name, str, i);
547 case xbt_cfgelm_string:
548 str = va_arg(pa, char *);
549 xbt_cfg_set_string(cfg, name, str);
554 xbt_cfg_set_int(cfg, name, i);
557 case xbt_cfgelm_double:
558 d = va_arg(pa, double);
559 xbt_cfg_set_double(cfg, name, d);
562 case xbt_cfgelm_boolean:
563 str = va_arg(pa, char *);
564 xbt_cfg_set_boolean(cfg, name, str);
568 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
572 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
574 * \arg cfg config set to fill
575 * \arg name variable name
576 * \arg varargs variable value
579 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
584 xbt_cfg_set_vargs(cfg, name, pa);
588 /** @brief Add values parsed from a string into a config set
590 * \arg cfg config set to fill
591 * \arg options a string containing the content to add to the config set. This
592 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
593 * like "[name]:[value]" where [name] is the name of an already registred
594 * variable, and [value] conforms to the data type under which this variable was
597 * @todo This is a crude manual parser, it should be a proper lexer.
600 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
602 char *optionlist_cpy;
603 char *option, *name, *val;
608 if (!options || !strlen(options)) { /* nothing to do */
611 optionlist_cpy = xbt_strdup(options);
613 XBT_DEBUG("List to parse and set:'%s'", options);
614 option = optionlist_cpy;
615 while (1) { /* breaks in the code */
621 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
622 name, len, (long) (option - name));
625 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
626 && *option != '\t' && *option != ',') {
627 XBT_DEBUG("Take %c.", *option);
630 if (option - name == len) {
631 XBT_DEBUG("Boundary=EOL");
632 option = NULL; /* don't do next iteration */
635 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
636 *option, len, (long) (option - name));
638 /* Pass the following blank chars */
640 while (option - name < (len - 1) &&
641 (*option == ' ' || *option == '\n' || *option == '\t')) {
642 /* fprintf(stderr,"Ignore a blank char.\n"); */
645 if (option - name == len - 1)
646 option = NULL; /* don't do next iteration */
648 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
650 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
655 val = strchr(name, ':');
657 /* don't free(optionlist_cpy) here, 'name' points inside it */
658 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
663 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
664 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
667 xbt_cfg_set_as_string(cfg,name,val);
669 free(optionlist_cpy);
673 free(optionlist_cpy);
676 /** @brief Set the value of a variable, using the string representation of that value
678 * @arg cfg config set to modify
679 * @arg key name of the variable to modify
680 * @arg value string representation of the value to set
682 * @return the first char after the parsed value in val
685 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
689 volatile xbt_cfgelm_t variable = NULL;
696 variable = xbt_dict_get((xbt_dict_t) cfg, key);
699 if (e.category == not_found_error) {
701 THROWF(not_found_error, 0,
702 "No registered variable corresponding to '%s'.", key);
707 switch (variable->type) {
708 case xbt_cfgelm_string:
709 xbt_cfg_set_string(cfg, key, value); /* throws */
713 i = strtol(value, &ret, 0);
715 xbt_die("Value of option %s not valid. Should be an integer", key);
718 xbt_cfg_set_int(cfg, key, i); /* throws */
721 case xbt_cfgelm_double:
722 d = strtod(value, &ret);
724 xbt_die("Value of option %s not valid. Should be a double", key);
727 xbt_cfg_set_double(cfg, key, d); /* throws */
730 case xbt_cfgelm_boolean:
731 xbt_cfg_set_boolean(cfg, key, value); /* throws */
734 case xbt_cfgelm_peer:
735 val = xbt_strdup(value);
737 val = strchr(val, ':');
739 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
743 i = strtol(val, &ret, 0);
745 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
748 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
753 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
760 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
762 * This is useful to change the default value of a variable while allowing
763 * users to override it with command line arguments
765 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
767 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
769 if (variable->isdefault){
770 xbt_cfg_set_int(cfg, name, val);
771 variable->isdefault = 1;
775 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
779 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
781 * This is useful to change the default value of a variable while allowing
782 * users to override it with command line arguments
784 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
786 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
788 if (variable->isdefault) {
789 xbt_cfg_set_double(cfg, name, val);
790 variable->isdefault = 1;
794 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
798 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
800 * This is useful to change the default value of a variable while allowing
801 * users to override it with command line arguments
803 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
806 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
808 if (variable->isdefault){
809 xbt_cfg_set_string(cfg, name, val);
810 variable->isdefault = 1;
814 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
819 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
821 * This is useful to change the default value of a variable while allowing
822 * users to override it with command line arguments
824 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
826 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
828 if (variable->isdefault){
829 xbt_cfg_set_boolean(cfg, name, val);
830 variable->isdefault = 1;
834 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
838 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
840 * This is useful to change the default value of a variable while allowing
841 * users to override it with command line arguments
843 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
844 const char *host, int port)
846 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
848 if (variable->isdefault){
849 xbt_cfg_set_peer(cfg, name, host, port);
850 variable->isdefault = 1;
854 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
858 /** @brief Set or add an integer value to \a name within \a cfg
860 * \arg cfg the config set
861 * \arg name the name of the variable
862 * \arg val the value of the variable
864 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
866 xbt_cfgelm_t variable;
868 XBT_VERB("Configuration setting: %s=%d", name, val);
869 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
871 if (variable->max == 1) {
872 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
873 variable->cb_rm(name, 0);
875 xbt_dynar_set(variable->content, 0, &val);
878 && xbt_dynar_length(variable->content) ==
879 (unsigned long) variable->max)
880 THROWF(mismatch_error, 0,
881 "Cannot add value %d to the config element %s since it's already full (size=%d)",
882 val, name, variable->max);
884 xbt_dynar_push(variable->content, &val);
887 if (variable->cb_set)
888 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
889 variable->isdefault = 0;
892 /** @brief Set or add a double value to \a name within \a cfg
894 * \arg cfg the config set
895 * \arg name the name of the variable
896 * \arg val the doule to set
899 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
901 xbt_cfgelm_t variable;
903 XBT_VERB("Configuration setting: %s=%f", name, val);
904 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
906 if (variable->max == 1) {
907 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
908 variable->cb_rm(name, 0);
910 xbt_dynar_set(variable->content, 0, &val);
913 && xbt_dynar_length(variable->content) == variable->max)
914 THROWF(mismatch_error, 0,
915 "Cannot add value %f to the config element %s since it's already full (size=%d)",
916 val, name, variable->max);
918 xbt_dynar_push(variable->content, &val);
921 if (variable->cb_set)
922 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
923 variable->isdefault = 0;
926 /** @brief Set or add a string value to \a name within \a cfg
928 * \arg cfg the config set
929 * \arg name the name of the variable
930 * \arg val the value to be added
934 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
936 xbt_cfgelm_t variable;
937 char *newval = xbt_strdup(val);
939 XBT_VERB("Configuration setting: %s=%s", name, val);
940 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
941 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
942 variable->min, variable->max,
943 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
945 if (variable->max == 1) {
946 if (!xbt_dynar_is_empty(variable->content)) {
948 variable->cb_rm(name, 0);
949 else if (variable->type == xbt_cfgelm_string) {
950 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
955 xbt_dynar_set(variable->content, 0, &newval);
958 && xbt_dynar_length(variable->content) == variable->max)
959 THROWF(mismatch_error, 0,
960 "Cannot add value %s to the config element %s since it's already full (size=%d)",
961 name, val, variable->max);
963 xbt_dynar_push(variable->content, &newval);
966 if (variable->cb_set)
967 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
968 variable->isdefault = 0;
971 /** @brief Set or add a boolean value to \a name within \a cfg
973 * \arg cfg the config set
974 * \arg name the name of the variable
975 * \arg val the value of the variable
977 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
979 xbt_cfgelm_t variable;
982 XBT_VERB("Configuration setting: %s=%s", name, val);
983 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
985 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
986 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
990 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
995 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
996 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
999 if (variable->max == 1) {
1000 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1001 variable->cb_rm(name, 0);
1003 xbt_dynar_set(variable->content, 0, &bval);
1006 && xbt_dynar_length(variable->content) ==
1007 (unsigned long) variable->max)
1008 THROWF(mismatch_error, 0,
1009 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1010 val, name, variable->max);
1012 xbt_dynar_push(variable->content, &bval);
1015 if (variable->cb_set)
1016 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1017 variable->isdefault = 0;
1020 /** @brief Set or add an peer value to \a name within \a cfg
1022 * \arg cfg the config set
1023 * \arg name the name of the variable
1024 * \arg peer the peer
1025 * \arg port the port number
1027 * \e peer values are composed of a string (peername) and an integer (port)
1031 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1034 xbt_cfgelm_t variable;
1035 xbt_peer_t val = xbt_peer_new(peer, port);
1037 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1039 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1041 if (variable->max == 1) {
1042 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1043 variable->cb_rm(name, 0);
1045 xbt_dynar_set(variable->content, 0, &val);
1048 && xbt_dynar_length(variable->content) == variable->max)
1049 THROWF(mismatch_error, 0,
1050 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1051 peer, port, name, variable->max);
1053 xbt_dynar_push(variable->content, &val);
1056 if (variable->cb_set)
1057 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1058 variable->isdefault = 0;
1061 /* ---- [ Removing ] ---- */
1063 /** @brief Remove the provided \e val integer value from a variable
1065 * \arg cfg the config set
1066 * \arg name the name of the variable
1067 * \arg val the value to be removed
1069 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1072 xbt_cfgelm_t variable;
1076 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1078 if (xbt_dynar_length(variable->content) == variable->min)
1079 THROWF(mismatch_error, 0,
1080 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1081 val, name, variable->min);
1083 xbt_dynar_foreach(variable->content, cpt, seen) {
1085 if (variable->cb_rm)
1086 variable->cb_rm(name, cpt);
1087 xbt_dynar_cursor_rm(variable->content, &cpt);
1092 THROWF(not_found_error, 0,
1093 "Can't remove the value %d of config element %s: value not found.",
1097 /** @brief Remove the provided \e val double value from a variable
1099 * \arg cfg the config set
1100 * \arg name the name of the variable
1101 * \arg val the value to be removed
1104 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1106 xbt_cfgelm_t variable;
1110 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1112 if (xbt_dynar_length(variable->content) == variable->min)
1113 THROWF(mismatch_error, 0,
1114 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1115 val, name, variable->min);
1117 xbt_dynar_foreach(variable->content, cpt, seen) {
1119 xbt_dynar_cursor_rm(variable->content, &cpt);
1120 if (variable->cb_rm)
1121 variable->cb_rm(name, cpt);
1126 THROWF(not_found_error, 0,
1127 "Can't remove the value %f of config element %s: value not found.",
1131 /** @brief Remove the provided \e val string value from a variable
1133 * \arg cfg the config set
1134 * \arg name the name of the variable
1135 * \arg val the value of the string which will be removed
1137 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1139 xbt_cfgelm_t variable;
1143 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1145 if (xbt_dynar_length(variable->content) == variable->min)
1146 THROWF(mismatch_error, 0,
1147 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1148 name, val, variable->min);
1150 xbt_dynar_foreach(variable->content, cpt, seen) {
1151 if (!strcpy(seen, val)) {
1152 if (variable->cb_rm)
1153 variable->cb_rm(name, cpt);
1154 xbt_dynar_cursor_rm(variable->content, &cpt);
1159 THROWF(not_found_error, 0,
1160 "Can't remove the value %s of config element %s: value not found.",
1164 /** @brief Remove the provided \e val boolean value from a variable
1166 * \arg cfg the config set
1167 * \arg name the name of the variable
1168 * \arg val the value to be removed
1170 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1173 xbt_cfgelm_t variable;
1177 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1179 if (xbt_dynar_length(variable->content) == variable->min)
1180 THROWF(mismatch_error, 0,
1181 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1182 val, name, variable->min);
1184 xbt_dynar_foreach(variable->content, cpt, seen) {
1186 if (variable->cb_rm)
1187 variable->cb_rm(name, cpt);
1188 xbt_dynar_cursor_rm(variable->content, &cpt);
1193 THROWF(not_found_error, 0,
1194 "Can't remove the value %d of config element %s: value not found.",
1198 /** @brief Remove the provided \e val peer value from a variable
1200 * \arg cfg the config set
1201 * \arg name the name of the variable
1202 * \arg peer the peername
1203 * \arg port the port number
1207 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1210 xbt_cfgelm_t variable;
1214 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1216 if (xbt_dynar_length(variable->content) == variable->min)
1217 THROWF(mismatch_error, 0,
1218 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1219 peer, port, name, variable->min);
1221 xbt_dynar_foreach(variable->content, cpt, seen) {
1222 if (!strcpy(seen->name, peer) && seen->port == port) {
1223 if (variable->cb_rm)
1224 variable->cb_rm(name, cpt);
1225 xbt_dynar_cursor_rm(variable->content, &cpt);
1230 THROWF(not_found_error, 0,
1231 "Can't remove the value %s:%d of config element %s: value not found.",
1235 /** @brief Remove the \e pos th value from the provided variable */
1237 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1240 xbt_cfgelm_t variable;
1242 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1244 if (xbt_dynar_length(variable->content) == variable->min)
1245 THROWF(mismatch_error, 0,
1246 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1247 pos, name, variable->min);
1249 if (variable->cb_rm)
1250 variable->cb_rm(name, pos);
1251 xbt_dynar_remove_at(variable->content, pos, NULL);
1254 /** @brief Remove all the values from a variable
1256 * \arg cfg the config set
1257 * \arg name the name of the variable
1260 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1262 xbt_cfgelm_t variable = NULL;
1266 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1269 if (e.category != not_found_error)
1273 THROWF(not_found_error, 0,
1274 "Can't empty '%s' since this config element does not exist",
1279 if (variable->cb_rm) {
1282 xbt_dynar_foreach(variable->content, cpt, ignored) {
1283 variable->cb_rm(name, cpt);
1286 xbt_dynar_reset(variable->content);
1290 * Say if the value is the default value
1292 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1294 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1295 return variable->isdefault;
1298 /*----[ Getting ]---------------------------------------------------------*/
1300 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1302 * \arg cfg the config set
1303 * \arg name the name of the variable
1304 * \arg val the wanted value
1306 * Returns the first value from the config set under the given name.
1307 * If there is more than one value, it will issue a warning. Consider using
1308 * xbt_cfg_get_dynar() instead.
1310 * \warning the returned value is the actual content of the config set
1312 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1314 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1316 if (xbt_dynar_length(variable->content) > 1) {
1318 ("You asked for the first value of the config element '%s', but there is %lu values",
1319 name, xbt_dynar_length(variable->content));
1322 return xbt_dynar_get_as(variable->content, 0, int);
1325 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1327 * \arg cfg the config set
1328 * \arg name the name of the variable
1329 * \arg val the wanted value
1331 * Returns the first value from the config set under the given name.
1332 * If there is more than one value, it will issue a warning. Consider using
1333 * xbt_cfg_get_dynar() instead.
1335 * \warning the returned value is the actual content of the config set
1338 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1340 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1342 if (xbt_dynar_length(variable->content) > 1) {
1344 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1345 name, xbt_dynar_length(variable->content));
1348 return xbt_dynar_get_as(variable->content, 0, double);
1351 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1353 * \arg th the config set
1354 * \arg name the name of the variable
1355 * \arg val the wanted value
1357 * Returns the first value from the config set under the given name.
1358 * If there is more than one value, it will issue a warning. Consider using
1359 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1361 * \warning the returned value is the actual content of the config set
1364 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1366 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1368 if (xbt_dynar_length(variable->content) > 1) {
1370 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1371 name, xbt_dynar_length(variable->content));
1372 } else if (xbt_dynar_is_empty(variable->content)) {
1376 return xbt_dynar_get_as(variable->content, 0, char *);
1379 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1381 * \arg cfg the config set
1382 * \arg name the name of the variable
1383 * \arg val the wanted value
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 * \arg cfg the config set
1407 * \arg name the name of the variable
1408 * \arg peer the peer
1409 * \arg 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 * \arg cfg where to search in
1440 * \arg name what to search for
1443 * Get the data stored in the config set.
1445 * \warning the returned value is the actual content of the config set
1447 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1449 xbt_cfgelm_t variable = NULL;
1453 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1456 if (e.category == not_found_error) {
1458 THROWF(not_found_error, 0,
1459 "No registered variable %s in this config set", name);
1464 return variable->content;
1468 /** @brief Retrieve one of the integer value of a variable */
1469 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1472 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1473 return xbt_dynar_get_as(variable->content, pos, int);
1476 /** @brief Retrieve one of the double value of a variable */
1477 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1480 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1481 return xbt_dynar_get_as(variable->content, pos, double);
1485 /** @brief Retrieve one of the string value of a variable */
1486 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1489 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1490 return xbt_dynar_get_as(variable->content, pos, char *);
1493 /** @brief Retrieve one of the boolean value of a variable */
1494 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1497 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1498 return xbt_dynar_get_as(variable->content, pos, int);
1501 /** @brief Retrieve one of the peer value of a variable */
1503 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1504 char **peer, int *port)
1507 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1508 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1519 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1521 XBT_TEST_SUITE("config", "Configuration support");
1523 static xbt_cfg_t make_set()
1525 xbt_cfg_t set = NULL;
1527 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1528 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1529 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1530 xbt_cfg_register_str(&set, "user:1_to_10_string");
1533 } /* end_of_make_set */
1535 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1537 xbt_cfg_t set = make_set();
1538 xbt_test_add("Alloc and free a config set");
1539 xbt_cfg_set_parse(set,
1540 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1545 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1547 xbt_cfg_t set = set = make_set();
1550 xbt_test_add("Having too few elements for speed");
1551 xbt_cfg_set_parse(set,
1552 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1557 if (e.category != mismatch_error ||
1558 strncmp(e.msg, "Config elem speed needs",
1559 strlen("Config elem speed needs")))
1560 xbt_test_fail("Got an exception. msg=%s", e.msg);
1568 xbt_test_add("Having too much values of 'speed'");
1570 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1572 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1575 if (e.category != mismatch_error ||
1576 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1577 strlen("Config elem speed needs")))
1578 xbt_test_fail("Got an exception. msg=%s", e.msg);
1587 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1590 xbt_test_add("Get a single value");
1592 /* get_single_value */
1594 xbt_cfg_t myset = make_set();
1596 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1597 ival = xbt_cfg_get_int(myset, "speed");
1599 xbt_test_fail("Speed value = %d, I expected 42", ival);
1600 xbt_cfg_free(&myset);
1603 xbt_test_add("Get multiple values");
1605 /* get_multiple_value */
1607 xbt_cfg_t myset = make_set();
1609 xbt_cfg_set_parse(myset,
1610 "peername:veloce user:foo\nuser:bar\tuser:toto");
1611 xbt_cfg_set_parse(myset, "speed:42");
1612 xbt_cfg_check(myset);
1613 dyn = xbt_cfg_get_dynar(myset, "user");
1615 if (xbt_dynar_length(dyn) != 3)
1616 xbt_test_fail("Dynar length = %lu, I expected 3",
1617 xbt_dynar_length(dyn));
1619 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1620 xbt_test_fail("Dynar[0] = %s, I expected foo",
1621 xbt_dynar_get_as(dyn, 0, char *));
1623 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1624 xbt_test_fail("Dynar[1] = %s, I expected bar",
1625 xbt_dynar_get_as(dyn, 1, char *));
1627 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1628 xbt_test_fail("Dynar[2] = %s, I expected toto",
1629 xbt_dynar_get_as(dyn, 2, char *));
1630 xbt_cfg_free(&myset);
1633 xbt_test_add("Access to a non-existant entry");
1635 /* non-existant_entry */
1636 xbt_cfg_t myset = make_set();
1640 xbt_cfg_set_parse(myset, "color:blue");
1643 if (e.category != not_found_error)
1644 xbt_test_exception(e);
1647 xbt_cfg_free(&myset);
1650 #endif /* SIMGRID_TEST */