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 defend my thesis. */
32 /* Allowed type of the variable */
33 e_xbt_cfgelm_type_t type;
40 /* actual content (could be an union or something) */
42 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
44 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
46 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
54 /* Internal stuff used in cache to free a variable */
55 static void xbt_cfgelm_free(void *data);
57 /* Retrieve the variable we'll modify */
58 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
60 /*----[ Memory management ]-----------------------------------------------*/
61 /** @brief Constructor
63 * Initialise a config set
65 xbt_cfg_t xbt_cfg_new(void)
67 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
70 /** \brief Copy an existing configuration set
72 * @param whereto the config set to be created
73 * @param tocopy the source data
75 * This only copy the registrations, not the actual content
77 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
79 xbt_dict_cursor_t cursor = NULL;
80 xbt_cfgelm_t variable = NULL;
83 XBT_DEBUG("Copy cfg set %p", tocopy);
85 xbt_assert(tocopy, "cannot copy NULL config");
87 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
88 xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->max, variable->cb_set);
92 /** @brief Destructor */
93 void xbt_cfg_free(xbt_cfg_t * cfg)
95 XBT_DEBUG("Frees cfg set %p", cfg);
96 xbt_dict_free((xbt_dict_t *) cfg);
99 /** @brief Dump a config set for debuging purpose
101 * @param name The name to give to this config set
102 * @param indent what to write at the beginning of each line (right number of spaces)
103 * @param cfg the config set
105 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
107 xbt_dict_t dict = (xbt_dict_t) cfg;
108 xbt_dict_cursor_t cursor = NULL;
109 xbt_cfgelm_t variable = NULL;
118 printf("%s>> Dumping of the config set '%s':\n", indent, name);
120 xbt_dict_foreach(dict, cursor, key, variable) {
121 printf("%s %s:", indent, key);
123 size = xbt_dynar_length(variable->content);
124 printf ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
125 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
127 switch (variable->type) {
129 for (i = 0; i < size; i++) {
130 ival = xbt_dynar_get_as(variable->content, i, int);
131 printf("%s %d\n", indent, ival);
134 case xbt_cfgelm_double:
135 for (i = 0; i < size; i++) {
136 dval = xbt_dynar_get_as(variable->content, i, double);
137 printf("%s %f\n", indent, dval);
140 case xbt_cfgelm_string:
141 for (i = 0; i < size; i++) {
142 sval = xbt_dynar_get_as(variable->content, i, char *);
143 printf("%s %s\n", indent, sval);
146 case xbt_cfgelm_boolean:
147 for (i = 0; i < size; i++) {
148 ival = xbt_dynar_get_as(variable->content, i, int);
149 printf("%s %d\n", indent, ival);
152 case xbt_cfgelm_alias:
156 printf("%s Invalid type!!\n", indent);
162 printf("%s<< End of the config set '%s'\n", indent, name);
165 xbt_dict_cursor_free(&cursor);
169 * free an config element
171 void xbt_cfgelm_free(void *data)
173 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
175 XBT_DEBUG("Frees cfgelm %p", c);
179 if (c->type != xbt_cfgelm_alias)
180 xbt_dynar_free(&(c->content));
184 /*----[ Registering stuff ]-----------------------------------------------*/
185 /** @brief Register an element within a config set
187 * @param cfg the config set
188 * @param name the name of the config element
189 * @param desc a description for this item (used by xbt_cfg_help())
190 * @param type the type of the config element
191 * @param min the minimum number of values for this config element
192 * @param max the maximum number of values for this config element
193 * @param cb_set callback function called when a value is set
195 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min,
196 int max, xbt_cfg_cb_t cb_set)
201 *cfg = xbt_cfg_new();
202 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
203 "type of %s not valid (%d should be between %d and %d)",
204 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
205 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
206 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
208 res = xbt_new(s_xbt_cfgelm_t, 1);
209 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
210 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
212 res->desc = xbt_strdup(desc);
216 res->cb_set = cb_set;
221 res->content = xbt_dynar_new(sizeof(int), NULL);
223 case xbt_cfgelm_double:
224 res->content = xbt_dynar_new(sizeof(double), NULL);
226 case xbt_cfgelm_string:
227 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
229 case xbt_cfgelm_boolean:
230 res->content = xbt_dynar_new(sizeof(int), NULL);
233 XBT_ERROR("%d is an invalid type code", (int)type);
236 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
239 void xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname)
242 *cfg = xbt_cfg_new();
244 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, oldname);
245 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
247 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, newname);
248 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
250 res = xbt_new0(s_xbt_cfgelm_t, 1);
251 XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
253 res->desc = bprintf("Deprecated alias for %s",newname);
254 res->type = xbt_cfgelm_alias;
258 res->content = (xbt_dynar_t)newname;
260 xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
263 /** @brief Unregister an element from a config set.
265 * @param cfg the config set
266 * @param name the name of the element to be freed
268 * Note that it removes both the description and the actual content.
269 * Throws not_found when no such element exists.
271 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
273 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
274 xbt_dict_remove((xbt_dict_t) cfg, name);
278 * @brief Parse a string and register the stuff described.
280 * @param cfg the config set
281 * @param entry a string describing the element to register
283 * The string may consist in several variable descriptions separated by a space.
284 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
285 * with type being one of 'string','int' or 'double'.
287 * Note that this does not allow to set the description, so you should prefer the other interface
289 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
291 char *entrycpy = xbt_strdup(entry);
295 e_xbt_cfgelm_type_t type;
296 XBT_DEBUG("Register string '%s'", entry);
298 tok = strchr(entrycpy, ':');
299 xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
302 min = strtol(tok, &tok, 10);
303 xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
305 xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
306 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
307 tok += strlen("_to_");
309 max = strtol(tok, &tok, 10);
310 xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
312 xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
313 "; Should be <name>:<min nb>_to_<max nb>_<type>");
316 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
317 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
318 "; Should be one of 'string', 'int' or 'double'.");
320 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
322 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
325 /** @brief Displays the declared aliases and their description */
326 void xbt_cfg_aliases(xbt_cfg_t cfg)
328 xbt_dict_cursor_t dict_cursor;
329 unsigned int dynar_cursor;
330 xbt_cfgelm_t variable;
332 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
334 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
335 xbt_dynar_push(names, &name);
336 xbt_dynar_sort_strings(names);
338 xbt_dynar_foreach(names, dynar_cursor, name) {
339 variable = xbt_dict_get((xbt_dict_t )cfg, name);
341 if (variable->type == xbt_cfgelm_alias)
342 printf(" %s: %s\n", name, variable->desc);
346 /** @brief Displays the declared options and their description */
347 void xbt_cfg_help(xbt_cfg_t cfg)
349 xbt_dict_cursor_t dict_cursor;
350 unsigned int dynar_cursor;
351 xbt_cfgelm_t variable;
353 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
355 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
356 xbt_dynar_push(names, &name);
357 xbt_dynar_sort_strings(names);
359 xbt_dynar_foreach(names, dynar_cursor, name) {
362 variable = xbt_dict_get((xbt_dict_t )cfg, name);
363 if (variable->type == xbt_cfgelm_alias)
366 printf(" %s: %s\n", name, variable->desc);
367 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
368 if (variable->min != 1 || variable->max != 1) {
369 printf("Arity: min:%d to max:", variable->min);
370 if (variable->max == 0)
371 printf("(no bound); ");
373 printf("%d; ", variable->max);
375 size = xbt_dynar_length(variable->content);
376 printf("Current value%s: ", (size <= 1 ? "" : "s"));
379 printf(size == 0 ? "n/a\n" : "{ ");
380 for (i = 0; i < size; i++) {
381 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
383 switch (variable->type) {
385 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
387 case xbt_cfgelm_double:
388 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
390 case xbt_cfgelm_string:
391 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
393 case xbt_cfgelm_boolean: {
394 int b = xbt_dynar_get_as(variable->content, i, int);
395 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
396 if (b == 0 || b == 1)
397 printf("'%s'%s", bs, sep);
399 printf("'%s/%d'%s", bs, b, sep);
403 printf("Invalid type!!%s", sep);
407 xbt_dynar_free(&names);
410 /** @brief Check that each variable have the right amount of values */
411 void xbt_cfg_check(xbt_cfg_t cfg)
413 xbt_dict_cursor_t cursor;
414 xbt_cfgelm_t variable;
418 xbt_assert(cfg, "NULL config set.");
419 XBT_DEBUG("Check cfg set %p", cfg);
421 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
422 if (variable->type == xbt_cfgelm_alias)
425 size = xbt_dynar_length(variable->content);
426 if (variable->min > size) {
427 xbt_dict_cursor_free(&cursor);
428 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
429 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
432 if (variable->isdefault && size > variable->min) {
433 xbt_dict_cursor_free(&cursor);
434 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
435 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
438 if (variable->max > 0 && variable->max < size) {
439 xbt_dict_cursor_free(&cursor);
440 THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
441 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
444 xbt_dict_cursor_free(&cursor);
447 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
449 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
451 // The user used the old name. Switch to the new one after a short warning
452 while (res && res->type == xbt_cfgelm_alias) {
453 const char* newname = (const char *)res->content;
454 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
455 res = xbt_cfgelm_get(cfg, newname, type);
461 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
464 xbt_assert(type == xbt_cfgelm_any || res->type == type,
465 "You tried to access to the config element %s as an %s, but its type is %s.",
466 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
470 /** @brief Get the type of this variable in that configuration set
472 * @param cfg the config set
473 * @param name the name of the element
475 * @return the type of the given element
477 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
479 xbt_cfgelm_t variable = NULL;
481 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
483 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
485 XBT_DEBUG("type in variable = %d", (int)variable->type);
486 return variable->type;
489 /*----[ Setting ]---------------------------------------------------------*/
490 /** @brief va_args version of xbt_cfg_set
492 * @param cfg config set to fill
493 * @param name variable name
494 * @param pa variable value
496 * Add some values to the config set.
498 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
503 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
508 type = xbt_cfg_get_type(cfg, name);
511 if (e.category == not_found_error) {
513 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
519 case xbt_cfgelm_string:
520 str = va_arg(pa, char *);
521 xbt_cfg_set_string(cfg, name, str);
525 xbt_cfg_set_int(cfg, name, i);
527 case xbt_cfgelm_double:
528 d = va_arg(pa, double);
529 xbt_cfg_set_double(cfg, name, d);
531 case xbt_cfgelm_boolean:
532 str = va_arg(pa, char *);
533 xbt_cfg_set_boolean(cfg, name, str);
536 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
540 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
542 * @param cfg config set to fill
543 * @param name variable name
544 * @param ... variable value
546 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
551 xbt_cfg_set_vargs(cfg, name, pa);
555 /** @brief Add values parsed from a string into a config set
557 * @param cfg config set to fill
558 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
559 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
560 * already registered variable, and [value] conforms to the data type under which this variable was registered.
562 * @todo This is a crude manual parser, it should be a proper lexer.
564 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
565 char *optionlist_cpy;
566 char *option, *name, *val;
570 if (!options || !strlen(options)) { /* nothing to do */
573 optionlist_cpy = xbt_strdup(options);
575 XBT_DEBUG("List to parse and set:'%s'", options);
576 option = optionlist_cpy;
577 while (1) { /* breaks in the code */
582 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
585 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
586 XBT_DEBUG("Take %c.", *option);
589 if (option - name == len) {
590 XBT_DEBUG("Boundary=EOL");
591 option = NULL; /* don't do next iteration */
593 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
594 /* Pass the following blank chars */
596 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
597 /* fprintf(stderr,"Ignore a blank char.\n"); */
600 if (option - name == len - 1)
601 option = NULL; /* don't do next iteration */
603 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
605 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
610 val = strchr(name, ':');
612 /* don't free(optionlist_cpy) here, 'name' points inside it */
613 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'", name);
617 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
618 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
621 xbt_cfg_set_as_string(cfg,name,val);
623 free(optionlist_cpy);
627 free(optionlist_cpy);
630 /** @brief Set the value of a variable, using the string representation of that value
632 * @param cfg config set to modify
633 * @param key name of the variable to modify
634 * @param value string representation of the value to set
636 * @return the first char after the parsed value in val
639 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
643 volatile xbt_cfgelm_t variable = NULL;
648 while (variable == NULL) {
649 variable = xbt_dict_get((xbt_dict_t) cfg, key);
650 if (variable->type == xbt_cfgelm_alias) {
651 const char *newname = (const char*)variable->content;
652 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
658 if (e.category == not_found_error) {
660 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
665 switch (variable->type) {
666 case xbt_cfgelm_string:
667 xbt_cfg_set_string(cfg, key, value); /* throws */
670 i = strtol(value, &ret, 0);
672 xbt_die("Value of option %s not valid. Should be an integer", key);
674 xbt_cfg_set_int(cfg, key, i); /* throws */
676 case xbt_cfgelm_double:
677 d = strtod(value, &ret);
679 xbt_die("Value of option %s not valid. Should be a double", key);
681 xbt_cfg_set_double(cfg, key, d); /* throws */
683 case xbt_cfgelm_boolean:
684 xbt_cfg_set_boolean(cfg, key, value); /* throws */
685 ret = (char *)value + strlen(value);
688 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
694 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
696 * This is useful to change the default value of a variable while allowing
697 * users to override it with command line arguments
699 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
701 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
703 if (variable->isdefault){
704 xbt_cfg_set_int(cfg, name, val);
705 variable->isdefault = 1;
707 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
710 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
712 * This is useful to change the default value of a variable while allowing
713 * users to override it with command line arguments
715 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
717 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
719 if (variable->isdefault) {
720 xbt_cfg_set_double(cfg, name, val);
721 variable->isdefault = 1;
723 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
726 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
728 * This is useful to change the default value of a variable while allowing
729 * users to override it with command line arguments
731 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char *val)
733 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
735 if (variable->isdefault){
736 xbt_cfg_set_string(cfg, name, val);
737 variable->isdefault = 1;
739 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
742 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
744 * This is useful to change the default value of a variable while allowing
745 * users to override it with command line arguments
747 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
749 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
751 if (variable->isdefault){
752 xbt_cfg_set_boolean(cfg, name, val);
753 variable->isdefault = 1;
756 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
759 /** @brief Set or add an integer value to \a name within \a cfg
761 * @param cfg the config set
762 * @param name the name of the variable
763 * @param val the value of the variable
765 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
767 XBT_VERB("Configuration setting: %s=%d", name, val);
768 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
770 if (variable->max == 1) {
771 xbt_dynar_set(variable->content, 0, &val);
773 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
774 THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
775 val, name, variable->max);
777 xbt_dynar_push(variable->content, &val);
780 if (variable->cb_set)
781 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
782 variable->isdefault = 0;
785 /** @brief Set or add a double value to \a name within \a cfg
787 * @param cfg the config set
788 * @param name the name of the variable
789 * @param val the doule to set
791 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
793 XBT_VERB("Configuration setting: %s=%f", name, val);
794 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
796 if (variable->max == 1) {
797 xbt_dynar_set(variable->content, 0, &val);
799 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
800 THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
801 val, name, variable->max);
803 xbt_dynar_push(variable->content, &val);
806 if (variable->cb_set)
807 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
808 variable->isdefault = 0;
811 /** @brief Set or add a string value to \a name within \a cfg
813 * @param cfg the config set
814 * @param name the name of the variable
815 * @param val the value to be added
818 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
820 char *newval = xbt_strdup(val);
822 XBT_VERB("Configuration setting: %s=%s", name, val);
823 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
825 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
826 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
828 if (variable->max == 1) {
829 if (!xbt_dynar_is_empty(variable->content)) {
830 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
834 xbt_dynar_set(variable->content, 0, &newval);
837 && xbt_dynar_length(variable->content) == variable->max)
838 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
839 name, val, variable->max);
841 xbt_dynar_push(variable->content, &newval);
844 if (variable->cb_set)
845 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
846 variable->isdefault = 0;
849 /** @brief Set or add a boolean value to \a name within \a cfg
851 * @param cfg the config set
852 * @param name the name of the variable
853 * @param val the value of the variable
855 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
859 XBT_VERB("Configuration setting: %s=%s", name, val);
860 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
862 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
863 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
867 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
872 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
873 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
876 if (variable->max == 1) {
877 xbt_dynar_set(variable->content, 0, &bval);
879 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
880 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
881 val, name, variable->max);
883 xbt_dynar_push(variable->content, &bval);
886 if (variable->cb_set)
887 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
888 variable->isdefault = 0;
891 /* ---- [ Removing ] ---- */
892 /** @brief Remove the provided \e val integer value from a variable
894 * @param cfg the config set
895 * @param name the name of the variable
896 * @param val the value to be removed
898 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
903 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
905 if (xbt_dynar_length(variable->content) == variable->min)
906 THROWF(mismatch_error, 0,
907 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
908 val, name, variable->min);
910 xbt_dynar_foreach(variable->content, cpt, seen) {
912 xbt_dynar_cursor_rm(variable->content, &cpt);
916 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
919 /** @brief Remove the provided \e val double value from a variable
921 * @param cfg the config set
922 * @param name the name of the variable
923 * @param val the value to be removed
925 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
930 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
932 if (xbt_dynar_length(variable->content) == variable->min)
933 THROWF(mismatch_error, 0,
934 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
935 val, name, variable->min);
937 xbt_dynar_foreach(variable->content, cpt, seen) {
939 xbt_dynar_cursor_rm(variable->content, &cpt);
944 THROWF(not_found_error, 0,"Can't remove the value %f of config element %s: value not found.", val, name);
947 /** @brief Remove the provided \e val string value from a variable
949 * @param cfg the config set
950 * @param name the name of the variable
951 * @param val the value of the string which will be removed
953 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
957 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
959 if (xbt_dynar_length(variable->content) == variable->min)
960 THROWF(mismatch_error, 0,
961 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
962 name, val, variable->min);
964 xbt_dynar_foreach(variable->content, cpt, seen) {
965 if (!strcpy(seen, val)) {
966 xbt_dynar_cursor_rm(variable->content, &cpt);
971 THROWF(not_found_error, 0, "Can't remove the value %s of config element %s: value not found.", val, name);
974 /** @brief Remove the provided \e val boolean value from a variable
976 * @param cfg the config set
977 * @param name the name of the variable
978 * @param val the value to be removed
980 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
984 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
986 if (xbt_dynar_length(variable->content) == variable->min)
987 THROWF(mismatch_error, 0,
988 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
989 val, name, variable->min);
991 xbt_dynar_foreach(variable->content, cpt, seen) {
993 xbt_dynar_cursor_rm(variable->content, &cpt);
998 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
1001 /** @brief Remove the \e pos th value from the provided variable */
1002 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1004 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1006 if (xbt_dynar_length(variable->content) == variable->min)
1007 THROWF(mismatch_error, 0,
1008 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1009 pos, name, variable->min);
1011 xbt_dynar_remove_at(variable->content, pos, NULL);
1014 /** @brief Remove all the values from a variable
1016 * @param cfg the config set
1017 * @param name the name of the variable
1019 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1021 xbt_cfgelm_t variable = NULL;
1025 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1027 if (e.category != not_found_error)
1031 THROWF(not_found_error, 0, "Can't empty '%s' since this option does not exist", name);
1035 xbt_dynar_reset(variable->content);
1038 /* Say if the value is the default value */
1039 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1041 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1042 return variable->isdefault;
1045 /*----[ Getting ]---------------------------------------------------------*/
1046 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1048 * @param cfg the config set
1049 * @param name the name of the variable
1051 * Returns the first value from the config set under the given name.
1052 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1054 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1056 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1058 if (xbt_dynar_length(variable->content) > 1) {
1059 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1060 name, xbt_dynar_length(variable->content));
1063 return xbt_dynar_get_as(variable->content, 0, int);
1066 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1068 * @param cfg the config set
1069 * @param name the name of the variable
1071 * Returns the first value from the config set under the given name.
1072 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1074 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1076 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1078 if (xbt_dynar_length(variable->content) > 1) {
1079 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
1080 name, xbt_dynar_length(variable->content));
1083 return xbt_dynar_get_as(variable->content, 0, double);
1086 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1088 * @param cfg the config set
1089 * @param name the name of the variable
1091 * Returns the first value from the config set under the given name.
1092 * If there is more than one value, it will issue a warning. Consider using
1093 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1095 * \warning the returned value is the actual content of the config set
1097 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1099 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1101 if (xbt_dynar_length(variable->content) > 1) {
1102 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
1103 name, xbt_dynar_length(variable->content));
1104 } else if (xbt_dynar_is_empty(variable->content)) {
1108 return xbt_dynar_get_as(variable->content, 0, char *);
1111 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1113 * @param cfg the config set
1114 * @param name the name of the variable
1116 * Returns the first value from the config set under the given name.
1117 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1119 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1121 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1123 if (xbt_dynar_length(variable->content) > 1) {
1124 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1125 name, xbt_dynar_length(variable->content));
1128 return xbt_dynar_get_as(variable->content, 0, int);
1131 /** @brief Retrieve the dynar of all the values stored in a variable
1133 * @param cfg where to search in
1134 * @param name what to search for
1136 * Get the data stored in the config set.
1138 * \warning the returned value is the actual content of the config set
1140 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1142 xbt_cfgelm_t variable = NULL;
1146 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1148 if (e.category == not_found_error) {
1150 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1155 return variable->content;
1158 /** @brief Retrieve one of the integer value of a variable */
1159 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1161 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1162 return xbt_dynar_get_as(variable->content, pos, int);
1165 /** @brief Retrieve one of the double value of a variable */
1166 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1168 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1169 return xbt_dynar_get_as(variable->content, pos, double);
1172 /** @brief Retrieve one of the string value of a variable */
1173 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1175 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1176 return xbt_dynar_get_as(variable->content, pos, char *);
1179 /** @brief Retrieve one of the boolean value of a variable */
1180 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1182 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1183 return xbt_dynar_get_as(variable->content, pos, int);
1190 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1192 XBT_TEST_SUITE("config", "Configuration support");
1194 static xbt_cfg_t make_set()
1196 xbt_cfg_t set = NULL;
1198 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1199 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1200 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1201 xbt_cfg_register_str(&set, "user:1_to_10_string");
1204 } /* end_of_make_set */
1206 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1208 xbt_cfg_t set = make_set();
1209 xbt_test_add("Alloc and free a config set");
1210 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1215 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1217 xbt_cfg_t set = set = make_set();
1220 xbt_test_add("Having too few elements for speed");
1221 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1225 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1226 xbt_test_fail("Got an exception. msg=%s", e.msg);
1232 xbt_test_add("Having too much values of 'speed'");
1234 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1236 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1238 if (e.category != mismatch_error ||
1239 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1240 xbt_test_fail("Got an exception. msg=%s", e.msg);
1248 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1250 xbt_test_add("Get a single value");
1252 /* get_single_value */
1254 xbt_cfg_t myset = make_set();
1256 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1257 ival = xbt_cfg_get_int(myset, "speed");
1259 xbt_test_fail("Speed value = %d, I expected 42", ival);
1260 xbt_cfg_free(&myset);
1263 xbt_test_add("Get multiple values");
1265 /* get_multiple_value */
1267 xbt_cfg_t myset = make_set();
1269 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1270 xbt_cfg_set_parse(myset, "speed:42");
1271 xbt_cfg_check(myset);
1272 dyn = xbt_cfg_get_dynar(myset, "user");
1274 if (xbt_dynar_length(dyn) != 3)
1275 xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1277 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1278 xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1280 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1281 xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1283 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1284 xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1285 xbt_cfg_free(&myset);
1288 xbt_test_add("Access to a non-existant entry");
1290 /* non-existant_entry */
1291 xbt_cfg_t myset = make_set();
1295 xbt_cfg_set_parse(myset, "color:blue");
1297 if (e.category != not_found_error)
1298 xbt_test_exception(e);
1301 xbt_cfg_free(&myset);
1304 #endif /* SIMGRID_TEST */