1 /* config - Dictionnary where the type of each variable is provided. */
3 /* This is useful to build named structs, like option or property sets. */
5 /* Copyright (c) 2004-2014. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/sysdep.h"
15 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
33 /* Allowed type of the variable */
34 e_xbt_cfgelm_type_t type;
43 (cannot be an union because type peer used to use both str and i, but it could be converted now) */
45 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
47 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
48 { "int", "double", "string", "boolean", "any" };
50 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
58 /* Internal stuff used in cache to free a variable */
59 static void xbt_cfgelm_free(void *data);
61 /* Retrieve the variable we'll modify */
62 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
63 e_xbt_cfgelm_type_t type);
65 /*----[ Memory management ]-----------------------------------------------*/
67 /** @brief Constructor
69 * Initialise a config set
73 xbt_cfg_t xbt_cfg_new(void)
75 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
78 /** \brief Copy an existing configuration set
80 * @param whereto the config set to be created
81 * @param tocopy the source data
83 * This only copy the registrations, not the actual content
86 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
88 xbt_dict_cursor_t cursor = NULL;
89 xbt_cfgelm_t variable = NULL;
92 XBT_DEBUG("Copy cfg set %p", tocopy);
94 xbt_assert(tocopy, "cannot copy NULL config");
96 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
97 xbt_cfg_register(whereto, name, variable->desc, variable->type,
98 variable->min, variable->max, variable->cb_set,
103 /** @brief Destructor */
104 void xbt_cfg_free(xbt_cfg_t * cfg)
106 XBT_DEBUG("Frees cfg set %p", cfg);
107 xbt_dict_free((xbt_dict_t *) cfg);
110 /** @brief Dump a config set for debuging purpose
112 * @param name The name to give to this config set
113 * @param indent what to write at the begining of each line (right number of spaces)
114 * @param cfg the config set
116 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
118 xbt_dict_t dict = (xbt_dict_t) cfg;
119 xbt_dict_cursor_t cursor = NULL;
120 xbt_cfgelm_t variable = NULL;
129 printf("%s>> Dumping of the config set '%s':\n", indent, name);
131 xbt_dict_foreach(dict, cursor, key, variable) {
132 printf("%s %s:", indent, key);
134 size = xbt_dynar_length(variable->content);
136 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
137 variable->min, variable->max,
138 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
141 switch (variable->type) {
144 for (i = 0; i < size; i++) {
145 ival = xbt_dynar_get_as(variable->content, i, int);
146 printf("%s %d\n", indent, ival);
150 case xbt_cfgelm_double:
151 for (i = 0; i < size; i++) {
152 dval = xbt_dynar_get_as(variable->content, i, double);
153 printf("%s %f\n", indent, dval);
157 case xbt_cfgelm_string:
158 for (i = 0; i < size; i++) {
159 sval = xbt_dynar_get_as(variable->content, i, char *);
160 printf("%s %s\n", indent, sval);
164 case xbt_cfgelm_boolean:
165 for (i = 0; i < size; i++) {
166 ival = xbt_dynar_get_as(variable->content, i, int);
167 printf("%s %d\n", indent, ival);
171 case xbt_cfgelm_alias:
176 printf("%s Invalid type!!\n", indent);
183 printf("%s<< End of the config set '%s'\n", indent, name);
186 xbt_dict_cursor_free(&cursor);
190 * free an config element
193 void xbt_cfgelm_free(void *data)
195 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
197 XBT_DEBUG("Frees cfgelm %p", c);
201 if (c->type != xbt_cfgelm_alias)
202 xbt_dynar_free(&(c->content));
206 /*----[ Registering stuff ]-----------------------------------------------*/
208 /** @brief Register an element within a config set
210 * @param cfg the config set
211 * @param name the name of the config element
212 * @param desc a description for this item (used by xbt_cfg_help())
213 * @param type the type of the config element
214 * @param min the minimum number of values for this config element
215 * @param max the maximum number of values for this config element
216 * @param cb_set callback function called when a value is set
217 * @param cb_rm callback function called when a value is removed
220 void 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_boolean,
230 "type of %s not valid (%d should be between %d and %d)",
231 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
232 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
233 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
235 res = xbt_new(s_xbt_cfgelm_t, 1);
236 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
237 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
239 res->desc = xbt_strdup(desc);
243 res->cb_set = cb_set;
249 res->content = xbt_dynar_new(sizeof(int), NULL);
252 case xbt_cfgelm_double:
253 res->content = xbt_dynar_new(sizeof(double), NULL);
256 case xbt_cfgelm_string:
257 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
260 case xbt_cfgelm_boolean:
261 res->content = xbt_dynar_new(sizeof(int), NULL);
265 XBT_ERROR("%d is an invalid type code", (int)type);
269 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
272 void xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname)
275 *cfg = xbt_cfg_new();
277 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, oldname);
278 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", oldname);
280 res = xbt_new0(s_xbt_cfgelm_t, 1);
281 XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
283 res->desc = bprintf("Deprecated alias for %s",newname);
284 res->type = xbt_cfgelm_alias;
288 res->content = (xbt_dynar_t)newname;
290 xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
292 /** @brief Unregister an element from a config set.
294 * @param cfg the config set
295 * @param name the name of the element to be freed
297 * Note that it removes both the description and the actual content.
298 * Throws not_found when no such element exists.
301 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
303 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
304 xbt_dict_remove((xbt_dict_t) cfg, name);
308 * @brief Parse a string and register the stuff described.
310 * @param cfg the config set
311 * @param entry a string describing the element to register
313 * The string may consist in several variable descriptions separated by a space.
314 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
315 * with type being one of 'string','int' or 'double'.
317 * Note that this does not allow to set the description, so you should prefer the other interface
320 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
322 char *entrycpy = xbt_strdup(entry);
326 e_xbt_cfgelm_type_t type;
327 XBT_DEBUG("Register string '%s'", entry);
329 tok = strchr(entrycpy, ':');
330 xbt_assert(tok, "Invalid config element descriptor: %s%s",
331 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
334 min = strtol(tok, &tok, 10);
335 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
338 xbt_assert(strcmp(tok, "_to_"),
339 "Invalid config element descriptor : %s%s",
340 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
341 tok += strlen("_to_");
343 max = strtol(tok, &tok, 10);
344 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
347 xbt_assert(*tok == '_',
348 "Invalid config element descriptor: %s%s", entry,
349 "; Should be <name>:<min nb>_to_<max nb>_<type>");
352 for (type = (e_xbt_cfgelm_type_t)0;
353 type < xbt_cfgelm_type_count
354 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
355 xbt_assert(type < xbt_cfgelm_type_count,
356 "Invalid type in config element descriptor: %s%s", entry,
357 "; Should be one of 'string', 'int' or 'double'.");
359 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
361 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
364 /** @brief Displays the declared aliases and their description */
365 void xbt_cfg_aliases(xbt_cfg_t cfg)
367 xbt_dict_cursor_t dict_cursor;
368 unsigned int dynar_cursor;
369 xbt_cfgelm_t variable;
371 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
373 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
374 xbt_dynar_push(names, &name);
375 xbt_dynar_sort_strings(names);
377 xbt_dynar_foreach(names, dynar_cursor, name) {
378 variable = xbt_dict_get((xbt_dict_t )cfg, name);
380 if (variable->type == xbt_cfgelm_alias)
381 printf(" %s: %s\n", name, variable->desc);
384 /** @brief Displays the declared options and their description */
385 void xbt_cfg_help(xbt_cfg_t cfg)
387 xbt_dict_cursor_t dict_cursor;
388 unsigned int dynar_cursor;
389 xbt_cfgelm_t variable;
391 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
393 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
394 xbt_dynar_push(names, &name);
395 xbt_dynar_sort_strings(names);
397 xbt_dynar_foreach(names, dynar_cursor, name) {
400 variable = xbt_dict_get((xbt_dict_t )cfg, name);
401 if (variable->type == xbt_cfgelm_alias)
404 printf(" %s: %s\n", name, variable->desc);
405 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
406 if (variable->min != 1 || variable->max != 1) {
407 printf("Arity: min:%d to max:", variable->min);
408 if (variable->max == 0)
409 printf("(no bound); ");
411 printf("%d; ", variable->max);
413 size = xbt_dynar_length(variable->content);
414 printf("Current value%s: ", (size <= 1 ? "" : "s"));
417 printf(size == 0 ? "n/a\n" : "{ ");
418 for (i = 0; i < size; i++) {
419 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
421 switch (variable->type) {
423 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
426 case xbt_cfgelm_double:
427 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
430 case xbt_cfgelm_string:
431 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
434 case xbt_cfgelm_boolean: {
435 int b = xbt_dynar_get_as(variable->content, i, int);
436 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
437 : xbt_cfgelm_boolean_values[0].false_val;
438 if (b == 0 || b == 1)
439 printf("'%s'%s", bs, sep);
441 printf("'%s/%d'%s", bs, b, sep);
446 printf("Invalid type!!%s", sep);
451 xbt_dynar_free(&names);
454 /** @brief Check that each variable have the right amount of values */
455 void xbt_cfg_check(xbt_cfg_t cfg)
457 xbt_dict_cursor_t cursor;
458 xbt_cfgelm_t variable;
462 xbt_assert(cfg, "NULL config set.");
463 XBT_DEBUG("Check cfg set %p", cfg);
465 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
466 if (variable->type == xbt_cfgelm_alias)
469 size = xbt_dynar_length(variable->content);
470 if (variable->min > size) {
471 xbt_dict_cursor_free(&cursor);
472 THROWF(mismatch_error, 0,
473 "Config elem %s needs at least %d %s, but there is only %d values.",
474 name, variable->min, xbt_cfgelm_type_name[variable->type],
478 if (variable->isdefault && size > variable->min) {
479 xbt_dict_cursor_free(&cursor);
480 THROWF(mismatch_error, 0,
481 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
482 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
485 if (variable->max > 0 && variable->max < size) {
486 xbt_dict_cursor_free(&cursor);
487 THROWF(mismatch_error, 0,
488 "Config elem %s accepts at most %d %s, but there is %d values.",
489 name, variable->max, xbt_cfgelm_type_name[variable->type],
494 xbt_dict_cursor_free(&cursor);
497 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
499 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
501 // The user used the old name. Switch to the new one after a short warning
502 if (res && res->type == xbt_cfgelm_alias) {
503 const char* newname = (const char *)res->content;
504 XBT_VERB("Option %s has been renamed to %s. Consider switching.", name, newname);
505 res = xbt_cfgelm_get(cfg, newname, type);
510 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
513 xbt_assert(type == xbt_cfgelm_any || res->type == type,
514 "You tried to access to the config element %s as an %s, but its type is %s.",
516 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
521 /** @brief Get the type of this variable in that configuration set
523 * @param cfg the config set
524 * @param name the name of the element
526 * @return the type of the given element
529 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
532 xbt_cfgelm_t variable = NULL;
534 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
536 THROWF(not_found_error, 0,
537 "Can't get the type of '%s' since this variable does not exist",
540 XBT_DEBUG("type in variable = %d", (int)variable->type);
542 return variable->type;
545 /*----[ Setting ]---------------------------------------------------------*/
546 /** @brief va_args version of xbt_cfg_set
548 * @param cfg config set to fill
549 * @param name variable name
550 * @param pa variable value
552 * Add some values to the config set.
554 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
559 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
564 type = xbt_cfg_get_type(cfg, name);
567 if (e.category == not_found_error) {
569 THROWF(not_found_error, 0,
570 "Can't set the property '%s' since it's not registered",
577 case xbt_cfgelm_string:
578 str = va_arg(pa, char *);
579 xbt_cfg_set_string(cfg, name, str);
584 xbt_cfg_set_int(cfg, name, i);
587 case xbt_cfgelm_double:
588 d = va_arg(pa, double);
589 xbt_cfg_set_double(cfg, name, d);
592 case xbt_cfgelm_boolean:
593 str = va_arg(pa, char *);
594 xbt_cfg_set_boolean(cfg, name, str);
598 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
602 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
604 * @param cfg config set to fill
605 * @param name variable name
606 * @param ... variable value
609 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
614 xbt_cfg_set_vargs(cfg, name, pa);
618 /** @brief Add values parsed from a string into a config set
620 * @param cfg config set to fill
621 * @param options a string containing the content to add to the config set. This
622 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
623 * like "[name]:[value]" where [name] is the name of an already registred
624 * variable, and [value] conforms to the data type under which this variable was
627 * @todo This is a crude manual parser, it should be a proper lexer.
630 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
632 char *optionlist_cpy;
633 char *option, *name, *val;
638 if (!options || !strlen(options)) { /* nothing to do */
641 optionlist_cpy = xbt_strdup(options);
643 XBT_DEBUG("List to parse and set:'%s'", options);
644 option = optionlist_cpy;
645 while (1) { /* breaks in the code */
651 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
652 name, len, (long) (option - name));
655 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
656 && *option != '\t' && *option != ',') {
657 XBT_DEBUG("Take %c.", *option);
660 if (option - name == len) {
661 XBT_DEBUG("Boundary=EOL");
662 option = NULL; /* don't do next iteration */
665 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
666 *option, len, (long) (option - name));
668 /* Pass the following blank chars */
670 while (option - name < (len - 1) &&
671 (*option == ' ' || *option == '\n' || *option == '\t')) {
672 /* fprintf(stderr,"Ignore a blank char.\n"); */
675 if (option - name == len - 1)
676 option = NULL; /* don't do next iteration */
678 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
680 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
685 val = strchr(name, ':');
687 /* don't free(optionlist_cpy) here, 'name' points inside it */
688 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
694 xbt_cfg_set_as_string(cfg,name,val);
696 free(optionlist_cpy);
700 free(optionlist_cpy);
703 /** @brief Set the value of a variable, using the string representation of that value
705 * @param cfg config set to modify
706 * @param key name of the variable to modify
707 * @param value string representation of the value to set
709 * @return the first char after the parsed value in val
712 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
716 volatile xbt_cfgelm_t variable = NULL;
720 if (strncmp(key, "contexts/", strlen("contexts/")) && strncmp(key, "path", strlen("path")))
721 XBT_INFO("Configuration change: Set '%s' to '%s'", key, value);
724 while (variable == NULL) {
725 variable = xbt_dict_get((xbt_dict_t) cfg, key);
726 if (variable->type == xbt_cfgelm_alias) {
727 const char *newname = (const char*)variable->content;
728 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
735 if (e.category == not_found_error) {
737 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
742 switch (variable->type) {
743 case xbt_cfgelm_string:
744 xbt_cfg_set_string(cfg, key, value); /* throws */
748 i = strtol(value, &ret, 0);
750 xbt_die("Value of option %s not valid. Should be an integer", key);
753 xbt_cfg_set_int(cfg, key, i); /* throws */
756 case xbt_cfgelm_double:
757 d = strtod(value, &ret);
759 xbt_die("Value of option %s not valid. Should be a double", key);
762 xbt_cfg_set_double(cfg, key, d); /* throws */
765 case xbt_cfgelm_boolean:
766 xbt_cfg_set_boolean(cfg, key, value); /* throws */
767 ret = (char *)value + strlen(value);
771 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
778 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
780 * This is useful to change the default value of a variable while allowing
781 * users to override it with command line arguments
783 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
785 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
787 if (variable->isdefault){
788 xbt_cfg_set_int(cfg, name, val);
789 variable->isdefault = 1;
793 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
797 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
799 * This is useful to change the default value of a variable while allowing
800 * users to override it with command line arguments
802 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
804 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
806 if (variable->isdefault) {
807 xbt_cfg_set_double(cfg, name, val);
808 variable->isdefault = 1;
812 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
816 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
818 * This is useful to change the default value of a variable while allowing
819 * users to override it with command line arguments
821 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
824 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
826 if (variable->isdefault){
827 xbt_cfg_set_string(cfg, name, val);
828 variable->isdefault = 1;
832 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
837 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
839 * This is useful to change the default value of a variable while allowing
840 * users to override it with command line arguments
842 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
844 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
846 if (variable->isdefault){
847 xbt_cfg_set_boolean(cfg, name, val);
848 variable->isdefault = 1;
852 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
856 /** @brief Set or add an integer value to \a name within \a cfg
858 * @param cfg the config set
859 * @param name the name of the variable
860 * @param val the value of the variable
862 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
864 xbt_cfgelm_t variable;
866 XBT_VERB("Configuration setting: %s=%d", name, val);
867 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
869 if (variable->max == 1) {
870 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
871 variable->cb_rm(name, 0);
873 xbt_dynar_set(variable->content, 0, &val);
876 && xbt_dynar_length(variable->content) ==
877 (unsigned long) variable->max)
878 THROWF(mismatch_error, 0,
879 "Cannot add value %d to the config element %s since it's already full (size=%d)",
880 val, name, variable->max);
882 xbt_dynar_push(variable->content, &val);
885 if (variable->cb_set)
886 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
887 variable->isdefault = 0;
890 /** @brief Set or add a double value to \a name within \a cfg
892 * @param cfg the config set
893 * @param name the name of the variable
894 * @param val the doule to set
897 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
899 xbt_cfgelm_t variable;
901 XBT_VERB("Configuration setting: %s=%f", name, val);
902 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
904 if (variable->max == 1) {
905 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
906 variable->cb_rm(name, 0);
908 xbt_dynar_set(variable->content, 0, &val);
911 && xbt_dynar_length(variable->content) == variable->max)
912 THROWF(mismatch_error, 0,
913 "Cannot add value %f to the config element %s since it's already full (size=%d)",
914 val, name, variable->max);
916 xbt_dynar_push(variable->content, &val);
919 if (variable->cb_set)
920 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
921 variable->isdefault = 0;
924 /** @brief Set or add a string value to \a name within \a cfg
926 * @param cfg the config set
927 * @param name the name of the variable
928 * @param val the value to be added
932 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
934 xbt_cfgelm_t variable;
935 char *newval = xbt_strdup(val);
937 XBT_VERB("Configuration setting: %s=%s", name, val);
938 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
939 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
940 variable->min, variable->max,
941 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
943 if (variable->max == 1) {
944 if (!xbt_dynar_is_empty(variable->content)) {
946 variable->cb_rm(name, 0);
947 else if (variable->type == xbt_cfgelm_string) {
948 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
953 xbt_dynar_set(variable->content, 0, &newval);
956 && xbt_dynar_length(variable->content) == variable->max)
957 THROWF(mismatch_error, 0,
958 "Cannot add value %s to the config element %s since it's already full (size=%d)",
959 name, val, variable->max);
961 xbt_dynar_push(variable->content, &newval);
964 if (variable->cb_set)
965 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
966 variable->isdefault = 0;
969 /** @brief Set or add a boolean value to \a name within \a cfg
971 * @param cfg the config set
972 * @param name the name of the variable
973 * @param val the value of the variable
975 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
977 xbt_cfgelm_t variable;
980 XBT_VERB("Configuration setting: %s=%s", name, val);
981 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
983 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
984 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
988 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
993 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
994 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
997 if (variable->max == 1) {
998 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
999 variable->cb_rm(name, 0);
1001 xbt_dynar_set(variable->content, 0, &bval);
1004 && xbt_dynar_length(variable->content) ==
1005 (unsigned long) variable->max)
1006 THROWF(mismatch_error, 0,
1007 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1008 val, name, variable->max);
1010 xbt_dynar_push(variable->content, &bval);
1013 if (variable->cb_set)
1014 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1015 variable->isdefault = 0;
1018 /* ---- [ Removing ] ---- */
1020 /** @brief Remove the provided \e val integer value from a variable
1022 * @param cfg the config set
1023 * @param name the name of the variable
1024 * @param val the value to be removed
1026 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1029 xbt_cfgelm_t variable;
1033 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1035 if (xbt_dynar_length(variable->content) == variable->min)
1036 THROWF(mismatch_error, 0,
1037 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1038 val, name, variable->min);
1040 xbt_dynar_foreach(variable->content, cpt, seen) {
1042 if (variable->cb_rm)
1043 variable->cb_rm(name, cpt);
1044 xbt_dynar_cursor_rm(variable->content, &cpt);
1049 THROWF(not_found_error, 0,
1050 "Can't remove the value %d of config element %s: value not found.",
1054 /** @brief Remove the provided \e val double value from a variable
1056 * @param cfg the config set
1057 * @param name the name of the variable
1058 * @param val the value to be removed
1061 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1063 xbt_cfgelm_t variable;
1067 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1069 if (xbt_dynar_length(variable->content) == variable->min)
1070 THROWF(mismatch_error, 0,
1071 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1072 val, name, variable->min);
1074 xbt_dynar_foreach(variable->content, cpt, seen) {
1076 xbt_dynar_cursor_rm(variable->content, &cpt);
1077 if (variable->cb_rm)
1078 variable->cb_rm(name, cpt);
1083 THROWF(not_found_error, 0,
1084 "Can't remove the value %f of config element %s: value not found.",
1088 /** @brief Remove the provided \e val string value from a variable
1090 * @param cfg the config set
1091 * @param name the name of the variable
1092 * @param val the value of the string which will be removed
1094 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1096 xbt_cfgelm_t variable;
1100 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1102 if (xbt_dynar_length(variable->content) == variable->min)
1103 THROWF(mismatch_error, 0,
1104 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1105 name, val, variable->min);
1107 xbt_dynar_foreach(variable->content, cpt, seen) {
1108 if (!strcpy(seen, val)) {
1109 if (variable->cb_rm)
1110 variable->cb_rm(name, cpt);
1111 xbt_dynar_cursor_rm(variable->content, &cpt);
1116 THROWF(not_found_error, 0,
1117 "Can't remove the value %s of config element %s: value not found.",
1121 /** @brief Remove the provided \e val boolean value from a variable
1123 * @param cfg the config set
1124 * @param name the name of the variable
1125 * @param val the value to be removed
1127 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1130 xbt_cfgelm_t variable;
1134 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1136 if (xbt_dynar_length(variable->content) == variable->min)
1137 THROWF(mismatch_error, 0,
1138 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1139 val, name, variable->min);
1141 xbt_dynar_foreach(variable->content, cpt, seen) {
1143 if (variable->cb_rm)
1144 variable->cb_rm(name, cpt);
1145 xbt_dynar_cursor_rm(variable->content, &cpt);
1150 THROWF(not_found_error, 0,
1151 "Can't remove the value %d of config element %s: value not found.",
1155 /** @brief Remove the \e pos th value from the provided variable */
1157 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1160 xbt_cfgelm_t variable;
1162 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1164 if (xbt_dynar_length(variable->content) == variable->min)
1165 THROWF(mismatch_error, 0,
1166 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1167 pos, name, variable->min);
1169 if (variable->cb_rm)
1170 variable->cb_rm(name, pos);
1171 xbt_dynar_remove_at(variable->content, pos, NULL);
1174 /** @brief Remove all the values from a variable
1176 * @param cfg the config set
1177 * @param name the name of the variable
1180 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1182 xbt_cfgelm_t variable = NULL;
1186 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1189 if (e.category != not_found_error)
1193 THROWF(not_found_error, 0,
1194 "Can't empty '%s' since this config element does not exist",
1199 if (variable->cb_rm) {
1202 xbt_dynar_foreach(variable->content, cpt, ignored) {
1203 variable->cb_rm(name, cpt);
1206 xbt_dynar_reset(variable->content);
1210 * Say if the value is the default value
1212 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1214 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1215 return variable->isdefault;
1218 /*----[ Getting ]---------------------------------------------------------*/
1220 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1222 * @param cfg the config set
1223 * @param name the name of the variable
1225 * Returns the first value from the config set under the given name.
1226 * If there is more than one value, it will issue a warning. Consider using
1227 * xbt_cfg_get_dynar() instead.
1229 * \warning the returned value is the actual content of the config set
1231 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1233 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1235 if (xbt_dynar_length(variable->content) > 1) {
1237 ("You asked for the first value of the config element '%s', but there is %lu values",
1238 name, xbt_dynar_length(variable->content));
1241 return xbt_dynar_get_as(variable->content, 0, int);
1244 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1246 * @param cfg the config set
1247 * @param name the name of the variable
1249 * Returns the first value from the config set under the given name.
1250 * If there is more than one value, it will issue a warning. Consider using
1251 * xbt_cfg_get_dynar() instead.
1253 * \warning the returned value is the actual content of the config set
1256 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1258 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1260 if (xbt_dynar_length(variable->content) > 1) {
1262 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1263 name, xbt_dynar_length(variable->content));
1266 return xbt_dynar_get_as(variable->content, 0, double);
1269 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1271 * @param cfg the config set
1272 * @param name the name of the variable
1274 * Returns the first value from the config set under the given name.
1275 * If there is more than one value, it will issue a warning. Consider using
1276 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1278 * \warning the returned value is the actual content of the config set
1281 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1283 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1285 if (xbt_dynar_length(variable->content) > 1) {
1287 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1288 name, xbt_dynar_length(variable->content));
1289 } else if (xbt_dynar_is_empty(variable->content)) {
1293 return xbt_dynar_get_as(variable->content, 0, char *);
1296 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1298 * @param cfg the config set
1299 * @param name the name of the variable
1301 * Returns the first value from the config set under the given name.
1302 * If there is more than one value, it will issue a warning. Consider using
1303 * xbt_cfg_get_dynar() instead.
1305 * \warning the returned value is the actual content of the config set
1307 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1309 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1311 if (xbt_dynar_length(variable->content) > 1) {
1313 ("You asked for the first value of the config element '%s', but there is %lu values",
1314 name, xbt_dynar_length(variable->content));
1317 return xbt_dynar_get_as(variable->content, 0, int);
1320 /** @brief Retrieve the dynar of all the values stored in a variable
1322 * @param cfg where to search in
1323 * @param name what to search for
1325 * Get the data stored in the config set.
1327 * \warning the returned value is the actual content of the config set
1329 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1331 xbt_cfgelm_t variable = NULL;
1335 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1338 if (e.category == not_found_error) {
1340 THROWF(not_found_error, 0,
1341 "No registered variable %s in this config set", name);
1346 return variable->content;
1350 /** @brief Retrieve one of the integer value of a variable */
1351 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1354 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1355 return xbt_dynar_get_as(variable->content, pos, int);
1358 /** @brief Retrieve one of the double value of a variable */
1359 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1362 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1363 return xbt_dynar_get_as(variable->content, pos, double);
1367 /** @brief Retrieve one of the string value of a variable */
1368 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1371 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1372 return xbt_dynar_get_as(variable->content, pos, char *);
1375 /** @brief Retrieve one of the boolean value of a variable */
1376 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1379 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1380 return xbt_dynar_get_as(variable->content, pos, int);
1388 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1390 XBT_TEST_SUITE("config", "Configuration support");
1392 static xbt_cfg_t make_set()
1394 xbt_cfg_t set = NULL;
1396 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1397 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1398 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1399 xbt_cfg_register_str(&set, "user:1_to_10_string");
1402 } /* end_of_make_set */
1404 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1406 xbt_cfg_t set = make_set();
1407 xbt_test_add("Alloc and free a config set");
1408 xbt_cfg_set_parse(set,
1409 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1414 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1416 xbt_cfg_t set = set = make_set();
1419 xbt_test_add("Having too few elements for speed");
1420 xbt_cfg_set_parse(set,
1421 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1426 if (e.category != mismatch_error ||
1427 strncmp(e.msg, "Config elem speed needs",
1428 strlen("Config elem speed needs")))
1429 xbt_test_fail("Got an exception. msg=%s", e.msg);
1437 xbt_test_add("Having too much values of 'speed'");
1439 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1441 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1444 if (e.category != mismatch_error ||
1445 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1446 strlen("Config elem speed needs")))
1447 xbt_test_fail("Got an exception. msg=%s", e.msg);
1456 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1459 xbt_test_add("Get a single value");
1461 /* get_single_value */
1463 xbt_cfg_t myset = make_set();
1465 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1466 ival = xbt_cfg_get_int(myset, "speed");
1468 xbt_test_fail("Speed value = %d, I expected 42", ival);
1469 xbt_cfg_free(&myset);
1472 xbt_test_add("Get multiple values");
1474 /* get_multiple_value */
1476 xbt_cfg_t myset = make_set();
1478 xbt_cfg_set_parse(myset,
1479 "peername:veloce user:foo\nuser:bar\tuser:toto");
1480 xbt_cfg_set_parse(myset, "speed:42");
1481 xbt_cfg_check(myset);
1482 dyn = xbt_cfg_get_dynar(myset, "user");
1484 if (xbt_dynar_length(dyn) != 3)
1485 xbt_test_fail("Dynar length = %lu, I expected 3",
1486 xbt_dynar_length(dyn));
1488 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1489 xbt_test_fail("Dynar[0] = %s, I expected foo",
1490 xbt_dynar_get_as(dyn, 0, char *));
1492 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1493 xbt_test_fail("Dynar[1] = %s, I expected bar",
1494 xbt_dynar_get_as(dyn, 1, char *));
1496 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1497 xbt_test_fail("Dynar[2] = %s, I expected toto",
1498 xbt_dynar_get_as(dyn, 2, char *));
1499 xbt_cfg_free(&myset);
1502 xbt_test_add("Access to a non-existant entry");
1504 /* non-existant_entry */
1505 xbt_cfg_t myset = make_set();
1509 xbt_cfg_set_parse(myset, "color:blue");
1512 if (e.category != not_found_error)
1513 xbt_test_exception(e);
1516 xbt_cfg_free(&myset);
1519 #endif /* SIMGRID_TEST */