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_cfg_t _sg_cfg_set = NULL;
26 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
28 Both data and DTD are mixed, but fixing it now would prevent me to ever defend my thesis. */
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
42 /* actual content (could be an union or something) */
44 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
46 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
48 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
56 /* Internal stuff used in cache to free a variable */
57 static void xbt_cfgelm_free(void *data);
59 /* Retrieve the variable we'll modify */
60 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
62 /*----[ Memory management ]-----------------------------------------------*/
63 /** @brief Constructor
65 * Initialise a config set
67 xbt_cfg_t xbt_cfg_new(void)
69 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
72 /** \brief Copy an existing configuration set
74 * @param whereto the config set to be created
75 * @param tocopy the source data
77 * This only copy the registrations, not the actual content
79 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
81 xbt_dict_cursor_t cursor = NULL;
82 xbt_cfgelm_t variable = NULL;
85 XBT_DEBUG("Copy cfg set %p", tocopy);
87 xbt_assert(tocopy, "cannot copy NULL config");
89 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
90 xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->max, variable->cb_set);
94 /** @brief Destructor */
95 void xbt_cfg_free(xbt_cfg_t * cfg)
97 XBT_DEBUG("Frees cfg set %p", cfg);
98 xbt_dict_free((xbt_dict_t *) cfg);
101 /** @brief Dump a config set for debuging purpose
103 * @param name The name to give to this config set
104 * @param indent what to write at the beginning of each line (right number of spaces)
105 * @param cfg the config set
107 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
109 xbt_dict_t dict = (xbt_dict_t) cfg;
110 xbt_dict_cursor_t cursor = NULL;
111 xbt_cfgelm_t variable = NULL;
120 printf("%s>> Dumping of the config set '%s':\n", indent, name);
122 xbt_dict_foreach(dict, cursor, key, variable) {
123 printf("%s %s:", indent, key);
125 size = xbt_dynar_length(variable->content);
126 printf ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
127 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
129 switch (variable->type) {
131 for (i = 0; i < size; i++) {
132 ival = xbt_dynar_get_as(variable->content, i, int);
133 printf("%s %d\n", indent, ival);
136 case xbt_cfgelm_double:
137 for (i = 0; i < size; i++) {
138 dval = xbt_dynar_get_as(variable->content, i, double);
139 printf("%s %f\n", indent, dval);
142 case xbt_cfgelm_string:
143 for (i = 0; i < size; i++) {
144 sval = xbt_dynar_get_as(variable->content, i, char *);
145 printf("%s %s\n", indent, sval);
148 case xbt_cfgelm_boolean:
149 for (i = 0; i < size; i++) {
150 ival = xbt_dynar_get_as(variable->content, i, int);
151 printf("%s %d\n", indent, ival);
154 case xbt_cfgelm_alias:
158 printf("%s Invalid type!!\n", indent);
164 printf("%s<< End of the config set '%s'\n", indent, name);
167 xbt_dict_cursor_free(&cursor);
171 * free an config element
173 void xbt_cfgelm_free(void *data)
175 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
177 XBT_DEBUG("Frees cfgelm %p", c);
181 if (c->type != xbt_cfgelm_alias)
182 xbt_dynar_free(&(c->content));
186 /*----[ Registering stuff ]-----------------------------------------------*/
187 /** @brief Register an element within a config set
189 * @param cfg the config set
190 * @param name the name of the config element
191 * @param desc a description for this item (used by xbt_cfg_help())
192 * @param type the type of the config element
193 * @param min the minimum number of values for this config element
194 * @param max the maximum number of values for this config element
195 * @param cb_set callback function called when a value is set
197 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min,
198 int max, xbt_cfg_cb_t cb_set)
203 *cfg = xbt_cfg_new();
204 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
205 "type of %s not valid (%d should be between %d and %d)",
206 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
207 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
208 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
210 res = xbt_new(s_xbt_cfgelm_t, 1);
211 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
212 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
214 res->desc = xbt_strdup(desc);
218 res->cb_set = cb_set;
223 res->content = xbt_dynar_new(sizeof(int), NULL);
225 case xbt_cfgelm_double:
226 res->content = xbt_dynar_new(sizeof(double), NULL);
228 case xbt_cfgelm_string:
229 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
231 case xbt_cfgelm_boolean:
232 res->content = xbt_dynar_new(sizeof(int), NULL);
235 XBT_ERROR("%d is an invalid type code", (int)type);
238 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
241 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
242 xbt_cfg_register(_sg_cfg_set,name,desc,xbt_cfgelm_double,1,1,cb_set);
243 xbt_cfg_setdefault_double(_sg_cfg_set, name, default_value);
245 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
246 xbt_cfg_register(_sg_cfg_set,name,desc,xbt_cfgelm_int,1,1,cb_set);
247 xbt_cfg_setdefault_int(_sg_cfg_set, name, default_value);
249 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
250 xbt_cfg_register(_sg_cfg_set,name,desc,xbt_cfgelm_string,1,1,cb_set);
251 xbt_cfg_setdefault_string(_sg_cfg_set, name, default_value);
253 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
254 xbt_cfg_register(_sg_cfg_set,name,desc,xbt_cfgelm_boolean,1,1,cb_set);
255 xbt_cfg_setdefault_boolean(_sg_cfg_set, name, default_value);
258 void xbt_cfg_register_alias(const char *newname, const char *oldname)
260 if (_sg_cfg_set == NULL)
261 _sg_cfg_set = xbt_cfg_new();
263 xbt_cfgelm_t res = xbt_dict_get_or_null(_sg_cfg_set, oldname);
264 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
266 res = xbt_dict_get_or_null(_sg_cfg_set, newname);
267 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
269 res = xbt_new0(s_xbt_cfgelm_t, 1);
270 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
272 res->desc = bprintf("Deprecated alias for %s",newname);
273 res->type = xbt_cfgelm_alias;
277 res->content = (xbt_dynar_t)newname;
279 xbt_dict_set(_sg_cfg_set, oldname, res, NULL);
282 /** @brief Unregister an element from a config set.
284 * @param cfg the config set
285 * @param name the name of the element to be freed
287 * Note that it removes both the description and the actual content.
288 * Throws not_found when no such element exists.
290 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
292 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
293 xbt_dict_remove((xbt_dict_t) cfg, name);
297 * @brief Parse a string and register the stuff described.
299 * @param cfg the config set
300 * @param entry a string describing the element to register
302 * The string may consist in several variable descriptions separated by a space.
303 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
304 * with type being one of 'string','int' or 'double'.
306 * Note that this does not allow to set the description, so you should prefer the other interface
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", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
321 min = strtol(tok, &tok, 10);
322 xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
324 xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
325 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
326 tok += strlen("_to_");
328 max = strtol(tok, &tok, 10);
329 xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
331 xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
332 "; Should be <name>:<min nb>_to_<max nb>_<type>");
335 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
336 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
337 "; Should be one of 'string', 'int' or 'double'.");
339 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
341 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
344 /** @brief Displays the declared aliases and their description */
345 void xbt_cfg_aliases(xbt_cfg_t cfg)
347 xbt_dict_cursor_t dict_cursor;
348 unsigned int dynar_cursor;
349 xbt_cfgelm_t variable;
351 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
353 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
354 xbt_dynar_push(names, &name);
355 xbt_dynar_sort_strings(names);
357 xbt_dynar_foreach(names, dynar_cursor, name) {
358 variable = xbt_dict_get((xbt_dict_t )cfg, name);
360 if (variable->type == xbt_cfgelm_alias)
361 printf(" %s: %s\n", name, variable->desc);
365 /** @brief Displays the declared options and their description */
366 void xbt_cfg_help(xbt_cfg_t cfg)
368 xbt_dict_cursor_t dict_cursor;
369 unsigned int dynar_cursor;
370 xbt_cfgelm_t variable;
372 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
374 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
375 xbt_dynar_push(names, &name);
376 xbt_dynar_sort_strings(names);
378 xbt_dynar_foreach(names, dynar_cursor, name) {
381 variable = xbt_dict_get((xbt_dict_t )cfg, name);
382 if (variable->type == xbt_cfgelm_alias)
385 printf(" %s: %s\n", name, variable->desc);
386 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
387 if (variable->min != 1 || variable->max != 1) {
388 printf("Arity: min:%d to max:", variable->min);
389 if (variable->max == 0)
390 printf("(no bound); ");
392 printf("%d; ", variable->max);
394 size = xbt_dynar_length(variable->content);
395 printf("Current value%s: ", (size <= 1 ? "" : "s"));
398 printf(size == 0 ? "n/a\n" : "{ ");
399 for (i = 0; i < size; i++) {
400 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
402 switch (variable->type) {
404 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
406 case xbt_cfgelm_double:
407 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
409 case xbt_cfgelm_string:
410 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
412 case xbt_cfgelm_boolean: {
413 int b = xbt_dynar_get_as(variable->content, i, int);
414 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
415 if (b == 0 || b == 1)
416 printf("'%s'%s", bs, sep);
418 printf("'%s/%d'%s", bs, b, sep);
422 printf("Invalid type!!%s", sep);
426 xbt_dynar_free(&names);
429 /** @brief Check that each variable have the right amount of values */
430 void xbt_cfg_check(xbt_cfg_t cfg)
432 xbt_dict_cursor_t cursor;
433 xbt_cfgelm_t variable;
437 xbt_assert(cfg, "NULL config set.");
438 XBT_DEBUG("Check cfg set %p", cfg);
440 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
441 if (variable->type == xbt_cfgelm_alias)
444 size = xbt_dynar_length(variable->content);
445 if (variable->min > size) {
446 xbt_dict_cursor_free(&cursor);
447 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
448 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
451 if (variable->isdefault && size > variable->min) {
452 xbt_dict_cursor_free(&cursor);
453 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
454 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
457 if (variable->max > 0 && variable->max < size) {
458 xbt_dict_cursor_free(&cursor);
459 THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
460 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
463 xbt_dict_cursor_free(&cursor);
466 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
468 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
470 // The user used the old name. Switch to the new one after a short warning
471 while (res && res->type == xbt_cfgelm_alias) {
472 const char* newname = (const char *)res->content;
473 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
474 res = xbt_cfgelm_get(cfg, newname, type);
480 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
483 xbt_assert(type == xbt_cfgelm_any || res->type == type,
484 "You tried to access to the config element %s as an %s, but its type is %s.",
485 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
489 /** @brief Get the type of this variable in that configuration set
491 * @param cfg the config set
492 * @param name the name of the element
494 * @return the type of the given element
496 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
498 xbt_cfgelm_t variable = NULL;
500 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
502 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
504 XBT_DEBUG("type in variable = %d", (int)variable->type);
505 return variable->type;
508 /*----[ Setting ]---------------------------------------------------------*/
509 /** @brief va_args version of xbt_cfg_set
511 * @param cfg config set to fill
512 * @param name variable name
513 * @param pa variable value
515 * Add some values to the config set.
517 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
522 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
527 type = xbt_cfg_get_type(cfg, name);
530 if (e.category == not_found_error) {
532 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
538 case xbt_cfgelm_string:
539 str = va_arg(pa, char *);
540 xbt_cfg_set_string(cfg, name, str);
544 xbt_cfg_set_int(cfg, name, i);
546 case xbt_cfgelm_double:
547 d = va_arg(pa, double);
548 xbt_cfg_set_double(cfg, name, d);
550 case xbt_cfgelm_boolean:
551 str = va_arg(pa, char *);
552 xbt_cfg_set_boolean(cfg, name, str);
555 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
559 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
561 * @param cfg config set to fill
562 * @param name variable name
563 * @param ... variable value
565 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
570 xbt_cfg_set_vargs(cfg, name, pa);
574 /** @brief Add values parsed from a string into a config set
576 * @param cfg config set to fill
577 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
578 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
579 * already registered variable, and [value] conforms to the data type under which this variable was registered.
581 * @todo This is a crude manual parser, it should be a proper lexer.
583 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
584 char *optionlist_cpy;
585 char *option, *name, *val;
589 if (!options || !strlen(options)) { /* nothing to do */
592 optionlist_cpy = xbt_strdup(options);
594 XBT_DEBUG("List to parse and set:'%s'", options);
595 option = optionlist_cpy;
596 while (1) { /* breaks in the code */
601 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
604 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
605 XBT_DEBUG("Take %c.", *option);
608 if (option - name == len) {
609 XBT_DEBUG("Boundary=EOL");
610 option = NULL; /* don't do next iteration */
612 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
613 /* Pass the following blank chars */
615 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
616 /* fprintf(stderr,"Ignore a blank char.\n"); */
619 if (option - name == len - 1)
620 option = NULL; /* don't do next iteration */
622 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
624 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
629 val = strchr(name, ':');
631 /* don't free(optionlist_cpy) here, 'name' points inside it */
632 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'", name);
636 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
637 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
640 xbt_cfg_set_as_string(cfg,name,val);
642 free(optionlist_cpy);
646 free(optionlist_cpy);
649 /** @brief Set the value of a variable, using the string representation of that value
651 * @param cfg config set to modify
652 * @param key name of the variable to modify
653 * @param value string representation of the value to set
655 * @return the first char after the parsed value in val
658 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
662 volatile xbt_cfgelm_t variable = NULL;
667 while (variable == NULL) {
668 variable = xbt_dict_get((xbt_dict_t) cfg, key);
669 if (variable->type == xbt_cfgelm_alias) {
670 const char *newname = (const char*)variable->content;
671 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
677 if (e.category == not_found_error) {
679 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
684 switch (variable->type) {
685 case xbt_cfgelm_string:
686 xbt_cfg_set_string(cfg, key, value); /* throws */
689 i = strtol(value, &ret, 0);
691 xbt_die("Value of option %s not valid. Should be an integer", key);
693 xbt_cfg_set_int(cfg, key, i); /* throws */
695 case xbt_cfgelm_double:
696 d = strtod(value, &ret);
698 xbt_die("Value of option %s not valid. Should be a double", key);
700 xbt_cfg_set_double(cfg, key, d); /* throws */
702 case xbt_cfgelm_boolean:
703 xbt_cfg_set_boolean(cfg, key, value); /* throws */
704 ret = (char *)value + strlen(value);
707 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
713 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
715 * This is useful to change the default value of a variable while allowing
716 * users to override it with command line arguments
718 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
720 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
722 if (variable->isdefault){
723 xbt_cfg_set_int(cfg, name, val);
724 variable->isdefault = 1;
726 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
729 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
731 * This is useful to change the default value of a variable while allowing
732 * users to override it with command line arguments
734 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
736 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
738 if (variable->isdefault) {
739 xbt_cfg_set_double(cfg, name, val);
740 variable->isdefault = 1;
742 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
745 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
747 * This is useful to change the default value of a variable while allowing
748 * users to override it with command line arguments
750 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char *val)
752 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
754 if (variable->isdefault){
755 xbt_cfg_set_string(cfg, name, val);
756 variable->isdefault = 1;
758 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
761 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
763 * This is useful to change the default value of a variable while allowing
764 * users to override it with command line arguments
766 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
768 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
770 if (variable->isdefault){
771 xbt_cfg_set_boolean(cfg, name, val);
772 variable->isdefault = 1;
775 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
778 /** @brief Set or add an integer value to \a name within \a cfg
780 * @param cfg the config set
781 * @param name the name of the variable
782 * @param val the value of the variable
784 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
786 XBT_VERB("Configuration setting: %s=%d", name, val);
787 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
789 if (variable->max == 1) {
790 xbt_dynar_set(variable->content, 0, &val);
792 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
793 THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
794 val, name, variable->max);
796 xbt_dynar_push(variable->content, &val);
799 if (variable->cb_set)
800 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
801 variable->isdefault = 0;
804 /** @brief Set or add a double value to \a name within \a cfg
806 * @param cfg the config set
807 * @param name the name of the variable
808 * @param val the doule to set
810 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
812 XBT_VERB("Configuration setting: %s=%f", name, val);
813 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
815 if (variable->max == 1) {
816 xbt_dynar_set(variable->content, 0, &val);
818 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
819 THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
820 val, name, variable->max);
822 xbt_dynar_push(variable->content, &val);
825 if (variable->cb_set)
826 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
827 variable->isdefault = 0;
830 /** @brief Set or add a string value to \a name within \a cfg
832 * @param cfg the config set
833 * @param name the name of the variable
834 * @param val the value to be added
837 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
839 char *newval = xbt_strdup(val);
841 XBT_VERB("Configuration setting: %s=%s", name, val);
842 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
844 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
845 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
847 if (variable->max == 1) {
848 if (!xbt_dynar_is_empty(variable->content)) {
849 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
853 xbt_dynar_set(variable->content, 0, &newval);
856 && xbt_dynar_length(variable->content) == variable->max)
857 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
858 name, val, variable->max);
860 xbt_dynar_push(variable->content, &newval);
863 if (variable->cb_set)
864 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
865 variable->isdefault = 0;
868 /** @brief Set or add a boolean value to \a name within \a cfg
870 * @param cfg the config set
871 * @param name the name of the variable
872 * @param val the value of the variable
874 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
878 XBT_VERB("Configuration setting: %s=%s", name, val);
879 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
881 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
882 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
886 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
891 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
892 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
895 if (variable->max == 1) {
896 xbt_dynar_set(variable->content, 0, &bval);
898 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
899 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
900 val, name, variable->max);
902 xbt_dynar_push(variable->content, &bval);
905 if (variable->cb_set)
906 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
907 variable->isdefault = 0;
910 /* ---- [ Removing ] ---- */
911 /** @brief Remove the provided \e val integer value from a variable
913 * @param cfg the config set
914 * @param name the name of the variable
915 * @param val the value to be removed
917 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
922 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
924 if (xbt_dynar_length(variable->content) == variable->min)
925 THROWF(mismatch_error, 0,
926 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
927 val, name, variable->min);
929 xbt_dynar_foreach(variable->content, cpt, seen) {
931 xbt_dynar_cursor_rm(variable->content, &cpt);
935 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
938 /** @brief Remove the provided \e val double value from a variable
940 * @param cfg the config set
941 * @param name the name of the variable
942 * @param val the value to be removed
944 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
949 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
951 if (xbt_dynar_length(variable->content) == variable->min)
952 THROWF(mismatch_error, 0,
953 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
954 val, name, variable->min);
956 xbt_dynar_foreach(variable->content, cpt, seen) {
958 xbt_dynar_cursor_rm(variable->content, &cpt);
963 THROWF(not_found_error, 0,"Can't remove the value %f of config element %s: value not found.", val, name);
966 /** @brief Remove the provided \e val string value from a variable
968 * @param cfg the config set
969 * @param name the name of the variable
970 * @param val the value of the string which will be removed
972 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
976 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
978 if (xbt_dynar_length(variable->content) == variable->min)
979 THROWF(mismatch_error, 0,
980 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
981 name, val, variable->min);
983 xbt_dynar_foreach(variable->content, cpt, seen) {
984 if (!strcpy(seen, val)) {
985 xbt_dynar_cursor_rm(variable->content, &cpt);
990 THROWF(not_found_error, 0, "Can't remove the value %s of config element %s: value not found.", val, name);
993 /** @brief Remove the provided \e val boolean value from a variable
995 * @param cfg the config set
996 * @param name the name of the variable
997 * @param val the value to be removed
999 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1003 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1005 if (xbt_dynar_length(variable->content) == variable->min)
1006 THROWF(mismatch_error, 0,
1007 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1008 val, name, variable->min);
1010 xbt_dynar_foreach(variable->content, cpt, seen) {
1012 xbt_dynar_cursor_rm(variable->content, &cpt);
1017 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
1020 /** @brief Remove the \e pos th value from the provided variable */
1021 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1023 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1025 if (xbt_dynar_length(variable->content) == variable->min)
1026 THROWF(mismatch_error, 0,
1027 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1028 pos, name, variable->min);
1030 xbt_dynar_remove_at(variable->content, pos, NULL);
1033 /** @brief Remove all the values from a variable
1035 * @param cfg the config set
1036 * @param name the name of the variable
1038 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1040 xbt_cfgelm_t variable = NULL;
1044 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1046 if (e.category != not_found_error)
1050 THROWF(not_found_error, 0, "Can't empty '%s' since this option does not exist", name);
1054 xbt_dynar_reset(variable->content);
1057 /* Say if the value is the default value */
1058 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1060 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1061 return variable->isdefault;
1064 /*----[ Getting ]---------------------------------------------------------*/
1065 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1067 * @param cfg the config set
1068 * @param name the name of the variable
1070 * Returns the first value from the config set under the given name.
1071 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1073 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1075 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1077 if (xbt_dynar_length(variable->content) > 1) {
1078 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1079 name, xbt_dynar_length(variable->content));
1082 return xbt_dynar_get_as(variable->content, 0, int);
1085 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1087 * @param cfg the config set
1088 * @param name the name of the variable
1090 * Returns the first value from the config set under the given name.
1091 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1093 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1095 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1097 if (xbt_dynar_length(variable->content) > 1) {
1098 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
1099 name, xbt_dynar_length(variable->content));
1102 return xbt_dynar_get_as(variable->content, 0, double);
1105 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1107 * @param cfg the config set
1108 * @param name the name of the variable
1110 * Returns the first value from the config set under the given name.
1111 * If there is more than one value, it will issue a warning. Consider using
1112 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1114 * \warning the returned value is the actual content of the config set
1116 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1118 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1120 if (xbt_dynar_length(variable->content) > 1) {
1121 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
1122 name, xbt_dynar_length(variable->content));
1123 } else if (xbt_dynar_is_empty(variable->content)) {
1127 return xbt_dynar_get_as(variable->content, 0, char *);
1130 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1132 * @param cfg the config set
1133 * @param name the name of the variable
1135 * Returns the first value from the config set under the given name.
1136 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1138 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1140 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1142 if (xbt_dynar_length(variable->content) > 1) {
1143 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1144 name, xbt_dynar_length(variable->content));
1147 return xbt_dynar_get_as(variable->content, 0, int);
1150 /** @brief Retrieve the dynar of all the values stored in a variable
1152 * @param cfg where to search in
1153 * @param name what to search for
1155 * Get the data stored in the config set.
1157 * \warning the returned value is the actual content of the config set
1159 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1161 xbt_cfgelm_t variable = NULL;
1165 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1167 if (e.category == not_found_error) {
1169 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1174 return variable->content;
1177 /** @brief Retrieve one of the integer value of a variable */
1178 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1180 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1181 return xbt_dynar_get_as(variable->content, pos, int);
1184 /** @brief Retrieve one of the double value of a variable */
1185 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1187 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1188 return xbt_dynar_get_as(variable->content, pos, double);
1191 /** @brief Retrieve one of the string value of a variable */
1192 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1194 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1195 return xbt_dynar_get_as(variable->content, pos, char *);
1198 /** @brief Retrieve one of the boolean value of a variable */
1199 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1201 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1202 return xbt_dynar_get_as(variable->content, pos, int);
1209 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1211 XBT_TEST_SUITE("config", "Configuration support");
1213 static xbt_cfg_t make_set()
1215 xbt_cfg_t set = NULL;
1217 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1218 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1219 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1220 xbt_cfg_register_str(&set, "user:1_to_10_string");
1223 } /* end_of_make_set */
1225 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1227 xbt_cfg_t set = make_set();
1228 xbt_test_add("Alloc and free a config set");
1229 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1234 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1236 xbt_cfg_t set = set = make_set();
1239 xbt_test_add("Having too few elements for speed");
1240 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1244 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1245 xbt_test_fail("Got an exception. msg=%s", e.msg);
1251 xbt_test_add("Having too much values of 'speed'");
1253 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1255 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1257 if (e.category != mismatch_error ||
1258 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1259 xbt_test_fail("Got an exception. msg=%s", e.msg);
1267 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1269 xbt_test_add("Get a single value");
1271 /* get_single_value */
1273 xbt_cfg_t myset = make_set();
1275 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1276 ival = xbt_cfg_get_int(myset, "speed");
1278 xbt_test_fail("Speed value = %d, I expected 42", ival);
1279 xbt_cfg_free(&myset);
1282 xbt_test_add("Get multiple values");
1284 /* get_multiple_value */
1286 xbt_cfg_t myset = make_set();
1288 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1289 xbt_cfg_set_parse(myset, "speed:42");
1290 xbt_cfg_check(myset);
1291 dyn = xbt_cfg_get_dynar(myset, "user");
1293 if (xbt_dynar_length(dyn) != 3)
1294 xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1296 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1297 xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1299 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1300 xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1302 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1303 xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1304 xbt_cfg_free(&myset);
1307 xbt_test_add("Access to a non-existant entry");
1309 /* non-existant_entry */
1310 xbt_cfg_t myset = make_set();
1314 xbt_cfg_set_parse(myset, "color:blue");
1316 if (e.category != not_found_error)
1317 xbt_test_exception(e);
1320 xbt_cfg_free(&myset);
1323 #endif /* SIMGRID_TEST */