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 option '%s' twice.", oldname);
280 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, newname);
281 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
283 res = xbt_new0(s_xbt_cfgelm_t, 1);
284 XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
286 res->desc = bprintf("Deprecated alias for %s",newname);
287 res->type = xbt_cfgelm_alias;
291 res->content = (xbt_dynar_t)newname;
293 xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
295 /** @brief Unregister an element from a config set.
297 * @param cfg the config set
298 * @param name the name of the element to be freed
300 * Note that it removes both the description and the actual content.
301 * Throws not_found when no such element exists.
304 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
306 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
307 xbt_dict_remove((xbt_dict_t) cfg, name);
311 * @brief Parse a string and register the stuff described.
313 * @param cfg the config set
314 * @param entry a string describing the element to register
316 * The string may consist in several variable descriptions separated by a space.
317 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
318 * with type being one of 'string','int' or 'double'.
320 * Note that this does not allow to set the description, so you should prefer the other interface
323 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
325 char *entrycpy = xbt_strdup(entry);
329 e_xbt_cfgelm_type_t type;
330 XBT_DEBUG("Register string '%s'", entry);
332 tok = strchr(entrycpy, ':');
333 xbt_assert(tok, "Invalid config element descriptor: %s%s",
334 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
337 min = strtol(tok, &tok, 10);
338 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
341 xbt_assert(strcmp(tok, "_to_"),
342 "Invalid config element descriptor : %s%s",
343 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
344 tok += strlen("_to_");
346 max = strtol(tok, &tok, 10);
347 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
350 xbt_assert(*tok == '_',
351 "Invalid config element descriptor: %s%s", entry,
352 "; Should be <name>:<min nb>_to_<max nb>_<type>");
355 for (type = (e_xbt_cfgelm_type_t)0;
356 type < xbt_cfgelm_type_count
357 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
358 xbt_assert(type < xbt_cfgelm_type_count,
359 "Invalid type in config element descriptor: %s%s", entry,
360 "; Should be one of 'string', 'int' or 'double'.");
362 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
364 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
367 /** @brief Displays the declared aliases and their description */
368 void xbt_cfg_aliases(xbt_cfg_t cfg)
370 xbt_dict_cursor_t dict_cursor;
371 unsigned int dynar_cursor;
372 xbt_cfgelm_t variable;
374 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
376 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
377 xbt_dynar_push(names, &name);
378 xbt_dynar_sort_strings(names);
380 xbt_dynar_foreach(names, dynar_cursor, name) {
381 variable = xbt_dict_get((xbt_dict_t )cfg, name);
383 if (variable->type == xbt_cfgelm_alias)
384 printf(" %s: %s\n", name, variable->desc);
387 /** @brief Displays the declared options and their description */
388 void xbt_cfg_help(xbt_cfg_t cfg)
390 xbt_dict_cursor_t dict_cursor;
391 unsigned int dynar_cursor;
392 xbt_cfgelm_t variable;
394 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
396 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
397 xbt_dynar_push(names, &name);
398 xbt_dynar_sort_strings(names);
400 xbt_dynar_foreach(names, dynar_cursor, name) {
403 variable = xbt_dict_get((xbt_dict_t )cfg, name);
404 if (variable->type == xbt_cfgelm_alias)
407 printf(" %s: %s\n", name, variable->desc);
408 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
409 if (variable->min != 1 || variable->max != 1) {
410 printf("Arity: min:%d to max:", variable->min);
411 if (variable->max == 0)
412 printf("(no bound); ");
414 printf("%d; ", variable->max);
416 size = xbt_dynar_length(variable->content);
417 printf("Current value%s: ", (size <= 1 ? "" : "s"));
420 printf(size == 0 ? "n/a\n" : "{ ");
421 for (i = 0; i < size; i++) {
422 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
424 switch (variable->type) {
426 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
429 case xbt_cfgelm_double:
430 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
433 case xbt_cfgelm_string:
434 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
437 case xbt_cfgelm_boolean: {
438 int b = xbt_dynar_get_as(variable->content, i, int);
439 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
440 : xbt_cfgelm_boolean_values[0].false_val;
441 if (b == 0 || b == 1)
442 printf("'%s'%s", bs, sep);
444 printf("'%s/%d'%s", bs, b, sep);
449 printf("Invalid type!!%s", sep);
454 xbt_dynar_free(&names);
457 /** @brief Check that each variable have the right amount of values */
458 void xbt_cfg_check(xbt_cfg_t cfg)
460 xbt_dict_cursor_t cursor;
461 xbt_cfgelm_t variable;
465 xbt_assert(cfg, "NULL config set.");
466 XBT_DEBUG("Check cfg set %p", cfg);
468 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
469 if (variable->type == xbt_cfgelm_alias)
472 size = xbt_dynar_length(variable->content);
473 if (variable->min > size) {
474 xbt_dict_cursor_free(&cursor);
475 THROWF(mismatch_error, 0,
476 "Config elem %s needs at least %d %s, but there is only %d values.",
477 name, variable->min, xbt_cfgelm_type_name[variable->type],
481 if (variable->isdefault && size > variable->min) {
482 xbt_dict_cursor_free(&cursor);
483 THROWF(mismatch_error, 0,
484 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
485 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
488 if (variable->max > 0 && variable->max < size) {
489 xbt_dict_cursor_free(&cursor);
490 THROWF(mismatch_error, 0,
491 "Config elem %s accepts at most %d %s, but there is %d values.",
492 name, variable->max, xbt_cfgelm_type_name[variable->type],
497 xbt_dict_cursor_free(&cursor);
500 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
502 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
504 // The user used the old name. Switch to the new one after a short warning
505 while (res && res->type == xbt_cfgelm_alias) {
506 const char* newname = (const char *)res->content;
507 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
508 res = xbt_cfgelm_get(cfg, newname, type);
514 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
517 xbt_assert(type == xbt_cfgelm_any || res->type == type,
518 "You tried to access to the config element %s as an %s, but its type is %s.",
520 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
525 /** @brief Get the type of this variable in that configuration set
527 * @param cfg the config set
528 * @param name the name of the element
530 * @return the type of the given element
533 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
536 xbt_cfgelm_t variable = NULL;
538 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
540 THROWF(not_found_error, 0,
541 "Can't get the type of '%s' since this variable does not exist",
544 XBT_DEBUG("type in variable = %d", (int)variable->type);
546 return variable->type;
549 /*----[ Setting ]---------------------------------------------------------*/
550 /** @brief va_args version of xbt_cfg_set
552 * @param cfg config set to fill
553 * @param name variable name
554 * @param pa variable value
556 * Add some values to the config set.
558 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
563 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
568 type = xbt_cfg_get_type(cfg, name);
571 if (e.category == not_found_error) {
573 THROWF(not_found_error, 0,
574 "Can't set the property '%s' since it's not registered",
581 case xbt_cfgelm_string:
582 str = va_arg(pa, char *);
583 xbt_cfg_set_string(cfg, name, str);
588 xbt_cfg_set_int(cfg, name, i);
591 case xbt_cfgelm_double:
592 d = va_arg(pa, double);
593 xbt_cfg_set_double(cfg, name, d);
596 case xbt_cfgelm_boolean:
597 str = va_arg(pa, char *);
598 xbt_cfg_set_boolean(cfg, name, str);
602 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
606 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
608 * @param cfg config set to fill
609 * @param name variable name
610 * @param ... variable value
613 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
618 xbt_cfg_set_vargs(cfg, name, pa);
622 /** @brief Add values parsed from a string into a config set
624 * @param cfg config set to fill
625 * @param options a string containing the content to add to the config set. This
626 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
627 * like "[name]:[value]" where [name] is the name of an already registred
628 * variable, and [value] conforms to the data type under which this variable was
631 * @todo This is a crude manual parser, it should be a proper lexer.
634 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
636 char *optionlist_cpy;
637 char *option, *name, *val;
642 if (!options || !strlen(options)) { /* nothing to do */
645 optionlist_cpy = xbt_strdup(options);
647 XBT_DEBUG("List to parse and set:'%s'", options);
648 option = optionlist_cpy;
649 while (1) { /* breaks in the code */
655 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
656 name, len, (long) (option - name));
659 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
660 && *option != '\t' && *option != ',') {
661 XBT_DEBUG("Take %c.", *option);
664 if (option - name == len) {
665 XBT_DEBUG("Boundary=EOL");
666 option = NULL; /* don't do next iteration */
669 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
670 *option, len, (long) (option - name));
672 /* Pass the following blank chars */
674 while (option - name < (len - 1) &&
675 (*option == ' ' || *option == '\n' || *option == '\t')) {
676 /* fprintf(stderr,"Ignore a blank char.\n"); */
679 if (option - name == len - 1)
680 option = NULL; /* don't do next iteration */
682 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
684 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
689 val = strchr(name, ':');
691 /* don't free(optionlist_cpy) here, 'name' points inside it */
692 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
697 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
698 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
701 xbt_cfg_set_as_string(cfg,name,val);
703 free(optionlist_cpy);
707 free(optionlist_cpy);
710 /** @brief Set the value of a variable, using the string representation of that value
712 * @param cfg config set to modify
713 * @param key name of the variable to modify
714 * @param value string representation of the value to set
716 * @return the first char after the parsed value in val
719 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
723 volatile xbt_cfgelm_t variable = NULL;
728 while (variable == NULL) {
729 variable = xbt_dict_get((xbt_dict_t) cfg, key);
730 while (variable->type == xbt_cfgelm_alias) {
731 const char *newname = (const char*)variable->content;
732 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
739 if (e.category == not_found_error) {
741 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
746 switch (variable->type) {
747 case xbt_cfgelm_string:
748 xbt_cfg_set_string(cfg, key, value); /* throws */
752 i = strtol(value, &ret, 0);
754 xbt_die("Value of option %s not valid. Should be an integer", key);
757 xbt_cfg_set_int(cfg, key, i); /* throws */
760 case xbt_cfgelm_double:
761 d = strtod(value, &ret);
763 xbt_die("Value of option %s not valid. Should be a double", key);
766 xbt_cfg_set_double(cfg, key, d); /* throws */
769 case xbt_cfgelm_boolean:
770 xbt_cfg_set_boolean(cfg, key, value); /* throws */
771 ret = (char *)value + strlen(value);
775 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
782 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
784 * This is useful to change the default value of a variable while allowing
785 * users to override it with command line arguments
787 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
789 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
791 if (variable->isdefault){
792 xbt_cfg_set_int(cfg, name, val);
793 variable->isdefault = 1;
797 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
801 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
803 * This is useful to change the default value of a variable while allowing
804 * users to override it with command line arguments
806 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
808 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
810 if (variable->isdefault) {
811 xbt_cfg_set_double(cfg, name, val);
812 variable->isdefault = 1;
816 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
820 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
822 * This is useful to change the default value of a variable while allowing
823 * users to override it with command line arguments
825 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
828 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
830 if (variable->isdefault){
831 xbt_cfg_set_string(cfg, name, val);
832 variable->isdefault = 1;
836 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
841 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
843 * This is useful to change the default value of a variable while allowing
844 * users to override it with command line arguments
846 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
848 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
850 if (variable->isdefault){
851 xbt_cfg_set_boolean(cfg, name, val);
852 variable->isdefault = 1;
856 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
860 /** @brief Set or add an integer value to \a name within \a cfg
862 * @param cfg the config set
863 * @param name the name of the variable
864 * @param val the value of the variable
866 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
869 XBT_VERB("Configuration setting: %s=%d", name, val);
870 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
872 if (variable->max == 1) {
873 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
874 variable->cb_rm(name, 0);
876 xbt_dynar_set(variable->content, 0, &val);
879 && xbt_dynar_length(variable->content) ==
880 (unsigned long) variable->max)
881 THROWF(mismatch_error, 0,
882 "Cannot add value %d to the config element %s since it's already full (size=%d)",
883 val, name, variable->max);
885 xbt_dynar_push(variable->content, &val);
888 if (variable->cb_set)
889 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
890 variable->isdefault = 0;
893 /** @brief Set or add a double value to \a name within \a cfg
895 * @param cfg the config set
896 * @param name the name of the variable
897 * @param val the doule to set
900 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
903 XBT_VERB("Configuration setting: %s=%f", name, val);
904 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
906 if (variable->max == 1) {
907 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
908 variable->cb_rm(name, 0);
910 xbt_dynar_set(variable->content, 0, &val);
913 && xbt_dynar_length(variable->content) == variable->max)
914 THROWF(mismatch_error, 0,
915 "Cannot add value %f to the config element %s since it's already full (size=%d)",
916 val, name, variable->max);
918 xbt_dynar_push(variable->content, &val);
921 if (variable->cb_set)
922 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
923 variable->isdefault = 0;
926 /** @brief Set or add a string value to \a name within \a cfg
928 * @param cfg the config set
929 * @param name the name of the variable
930 * @param val the value to be added
934 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
936 char *newval = xbt_strdup(val);
938 XBT_VERB("Configuration setting: %s=%s", name, val);
939 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
941 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
942 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
944 if (variable->max == 1) {
945 if (!xbt_dynar_is_empty(variable->content)) {
947 variable->cb_rm(name, 0);
948 else if (variable->type == xbt_cfgelm_string) {
949 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
954 xbt_dynar_set(variable->content, 0, &newval);
957 && xbt_dynar_length(variable->content) == variable->max)
958 THROWF(mismatch_error, 0,
959 "Cannot add value %s to the config element %s since it's already full (size=%d)",
960 name, val, variable->max);
962 xbt_dynar_push(variable->content, &newval);
965 if (variable->cb_set)
966 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
967 variable->isdefault = 0;
970 /** @brief Set or add a boolean value to \a name within \a cfg
972 * @param cfg the config set
973 * @param name the name of the variable
974 * @param val the value of the variable
976 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
980 XBT_VERB("Configuration setting: %s=%s", name, val);
981 xbt_cfgelm_t 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)
1032 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1034 if (xbt_dynar_length(variable->content) == variable->min)
1035 THROWF(mismatch_error, 0,
1036 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1037 val, name, variable->min);
1039 xbt_dynar_foreach(variable->content, cpt, seen) {
1041 if (variable->cb_rm)
1042 variable->cb_rm(name, cpt);
1043 xbt_dynar_cursor_rm(variable->content, &cpt);
1048 THROWF(not_found_error, 0,
1049 "Can't remove the value %d of config element %s: value not found.",
1053 /** @brief Remove the provided \e val double value from a variable
1055 * @param cfg the config set
1056 * @param name the name of the variable
1057 * @param val the value to be removed
1060 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1065 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1067 if (xbt_dynar_length(variable->content) == variable->min)
1068 THROWF(mismatch_error, 0,
1069 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1070 val, name, variable->min);
1072 xbt_dynar_foreach(variable->content, cpt, seen) {
1074 xbt_dynar_cursor_rm(variable->content, &cpt);
1075 if (variable->cb_rm)
1076 variable->cb_rm(name, cpt);
1081 THROWF(not_found_error, 0,
1082 "Can't remove the value %f of config element %s: value not found.",
1086 /** @brief Remove the provided \e val string value from a variable
1088 * @param cfg the config set
1089 * @param name the name of the variable
1090 * @param val the value of the string which will be removed
1092 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1096 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1098 if (xbt_dynar_length(variable->content) == variable->min)
1099 THROWF(mismatch_error, 0,
1100 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1101 name, val, variable->min);
1103 xbt_dynar_foreach(variable->content, cpt, seen) {
1104 if (!strcpy(seen, val)) {
1105 if (variable->cb_rm)
1106 variable->cb_rm(name, cpt);
1107 xbt_dynar_cursor_rm(variable->content, &cpt);
1112 THROWF(not_found_error, 0,
1113 "Can't remove the value %s of config element %s: value not found.",
1117 /** @brief Remove the provided \e val boolean value from a variable
1119 * @param cfg the config set
1120 * @param name the name of the variable
1121 * @param val the value to be removed
1123 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1128 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1130 if (xbt_dynar_length(variable->content) == variable->min)
1131 THROWF(mismatch_error, 0,
1132 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1133 val, name, variable->min);
1135 xbt_dynar_foreach(variable->content, cpt, seen) {
1137 if (variable->cb_rm)
1138 variable->cb_rm(name, cpt);
1139 xbt_dynar_cursor_rm(variable->content, &cpt);
1144 THROWF(not_found_error, 0,
1145 "Can't remove the value %d of config element %s: value not found.",
1149 /** @brief Remove the \e pos th value from the provided variable */
1151 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1154 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1156 if (xbt_dynar_length(variable->content) == variable->min)
1157 THROWF(mismatch_error, 0,
1158 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1159 pos, name, variable->min);
1161 if (variable->cb_rm)
1162 variable->cb_rm(name, pos);
1163 xbt_dynar_remove_at(variable->content, pos, NULL);
1166 /** @brief Remove all the values from a variable
1168 * @param cfg the config set
1169 * @param name the name of the variable
1172 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1174 xbt_cfgelm_t variable = NULL;
1178 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1181 if (e.category != not_found_error)
1185 THROWF(not_found_error, 0,
1186 "Can't empty '%s' since this config element does not exist",
1191 if (variable->cb_rm) {
1194 xbt_dynar_foreach(variable->content, cpt, ignored) {
1195 variable->cb_rm(name, cpt);
1198 xbt_dynar_reset(variable->content);
1202 * Say if the value is the default value
1204 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1206 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1207 return variable->isdefault;
1210 /*----[ Getting ]---------------------------------------------------------*/
1212 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1214 * @param cfg the config set
1215 * @param name the name of the variable
1217 * Returns the first value from the config set under the given name.
1218 * If there is more than one value, it will issue a warning. Consider using
1219 * xbt_cfg_get_dynar() instead.
1221 * \warning the returned value is the actual content of the config set
1223 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1225 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1227 if (xbt_dynar_length(variable->content) > 1) {
1229 ("You asked for the first value of the config element '%s', but there is %lu values",
1230 name, xbt_dynar_length(variable->content));
1233 return xbt_dynar_get_as(variable->content, 0, int);
1236 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1238 * @param cfg the config set
1239 * @param name the name of the variable
1241 * Returns the first value from the config set under the given name.
1242 * If there is more than one value, it will issue a warning. Consider using
1243 * xbt_cfg_get_dynar() instead.
1245 * \warning the returned value is the actual content of the config set
1248 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1250 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1252 if (xbt_dynar_length(variable->content) > 1) {
1254 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1255 name, xbt_dynar_length(variable->content));
1258 return xbt_dynar_get_as(variable->content, 0, double);
1261 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1263 * @param cfg the config set
1264 * @param name the name of the variable
1266 * Returns the first value from the config set under the given name.
1267 * If there is more than one value, it will issue a warning. Consider using
1268 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1270 * \warning the returned value is the actual content of the config set
1273 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1275 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1277 if (xbt_dynar_length(variable->content) > 1) {
1279 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1280 name, xbt_dynar_length(variable->content));
1281 } else if (xbt_dynar_is_empty(variable->content)) {
1285 return xbt_dynar_get_as(variable->content, 0, char *);
1288 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1290 * @param cfg the config set
1291 * @param name the name of the variable
1293 * Returns the first value from the config set under the given name.
1294 * If there is more than one value, it will issue a warning. Consider using
1295 * xbt_cfg_get_dynar() instead.
1297 * \warning the returned value is the actual content of the config set
1299 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1301 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1303 if (xbt_dynar_length(variable->content) > 1) {
1305 ("You asked for the first value of the config element '%s', but there is %lu values",
1306 name, xbt_dynar_length(variable->content));
1309 return xbt_dynar_get_as(variable->content, 0, int);
1312 /** @brief Retrieve the dynar of all the values stored in a variable
1314 * @param cfg where to search in
1315 * @param name what to search for
1317 * Get the data stored in the config set.
1319 * \warning the returned value is the actual content of the config set
1321 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1323 xbt_cfgelm_t variable = NULL;
1327 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1330 if (e.category == not_found_error) {
1332 THROWF(not_found_error, 0,
1333 "No registered variable %s in this config set", name);
1338 return variable->content;
1342 /** @brief Retrieve one of the integer value of a variable */
1343 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1346 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1347 return xbt_dynar_get_as(variable->content, pos, int);
1350 /** @brief Retrieve one of the double value of a variable */
1351 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1354 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1355 return xbt_dynar_get_as(variable->content, pos, double);
1359 /** @brief Retrieve one of the string value of a variable */
1360 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1363 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1364 return xbt_dynar_get_as(variable->content, pos, char *);
1367 /** @brief Retrieve one of the boolean value of a variable */
1368 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1371 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1372 return xbt_dynar_get_as(variable->content, pos, int);
1380 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1382 XBT_TEST_SUITE("config", "Configuration support");
1384 static xbt_cfg_t make_set()
1386 xbt_cfg_t set = NULL;
1388 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1389 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1390 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1391 xbt_cfg_register_str(&set, "user:1_to_10_string");
1394 } /* end_of_make_set */
1396 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1398 xbt_cfg_t set = make_set();
1399 xbt_test_add("Alloc and free a config set");
1400 xbt_cfg_set_parse(set,
1401 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1406 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1408 xbt_cfg_t set = set = make_set();
1411 xbt_test_add("Having too few elements for speed");
1412 xbt_cfg_set_parse(set,
1413 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1418 if (e.category != mismatch_error ||
1419 strncmp(e.msg, "Config elem speed needs",
1420 strlen("Config elem speed needs")))
1421 xbt_test_fail("Got an exception. msg=%s", e.msg);
1429 xbt_test_add("Having too much values of 'speed'");
1431 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1433 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1436 if (e.category != mismatch_error ||
1437 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1438 strlen("Config elem speed needs")))
1439 xbt_test_fail("Got an exception. msg=%s", e.msg);
1448 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1451 xbt_test_add("Get a single value");
1453 /* get_single_value */
1455 xbt_cfg_t myset = make_set();
1457 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1458 ival = xbt_cfg_get_int(myset, "speed");
1460 xbt_test_fail("Speed value = %d, I expected 42", ival);
1461 xbt_cfg_free(&myset);
1464 xbt_test_add("Get multiple values");
1466 /* get_multiple_value */
1468 xbt_cfg_t myset = make_set();
1470 xbt_cfg_set_parse(myset,
1471 "peername:veloce user:foo\nuser:bar\tuser:toto");
1472 xbt_cfg_set_parse(myset, "speed:42");
1473 xbt_cfg_check(myset);
1474 dyn = xbt_cfg_get_dynar(myset, "user");
1476 if (xbt_dynar_length(dyn) != 3)
1477 xbt_test_fail("Dynar length = %lu, I expected 3",
1478 xbt_dynar_length(dyn));
1480 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1481 xbt_test_fail("Dynar[0] = %s, I expected foo",
1482 xbt_dynar_get_as(dyn, 0, char *));
1484 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1485 xbt_test_fail("Dynar[1] = %s, I expected bar",
1486 xbt_dynar_get_as(dyn, 1, char *));
1488 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1489 xbt_test_fail("Dynar[2] = %s, I expected toto",
1490 xbt_dynar_get_as(dyn, 2, char *));
1491 xbt_cfg_free(&myset);
1494 xbt_test_add("Access to a non-existant entry");
1496 /* non-existant_entry */
1497 xbt_cfg_t myset = make_set();
1501 xbt_cfg_set_parse(myset, "color:blue");
1504 if (e.category != not_found_error)
1505 xbt_test_exception(e);
1508 xbt_cfg_free(&myset);
1511 #endif /* SIMGRID_TEST */