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 simgrid_config = 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->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_%s. Actual size=%d. postset=%p, List of values:\n",
127 variable->min, 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 (0 for optional elements)
194 * @param cb_set callback function called when a value is set
196 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min, xbt_cfg_cb_t cb_set)
199 *cfg = xbt_cfg_new();
200 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
201 "type of %s not valid (%d should be between %d and %d)",
202 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
204 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
205 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
207 res = xbt_new(s_xbt_cfgelm_t, 1);
208 XBT_DEBUG("Register cfg elm %s (%s) (%d %s (=%d) @%p in set %p)",
209 name, desc, min, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
211 res->desc = xbt_strdup(desc);
214 res->cb_set = cb_set;
219 res->content = xbt_dynar_new(sizeof(int), NULL);
221 case xbt_cfgelm_double:
222 res->content = xbt_dynar_new(sizeof(double), NULL);
224 case xbt_cfgelm_string:
225 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
227 case xbt_cfgelm_boolean:
228 res->content = xbt_dynar_new(sizeof(int), NULL);
231 XBT_ERROR("%d is an invalid type code", (int)type);
234 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
237 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
238 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,1,cb_set);
239 xbt_cfg_setdefault_double(name, default_value);
241 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
242 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,1,cb_set);
243 xbt_cfg_setdefault_int(name, default_value);
245 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
246 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,1,cb_set);
247 xbt_cfg_setdefault_string(name, default_value);
249 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
250 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,1,cb_set);
251 xbt_cfg_setdefault_boolean(name, default_value);
254 void xbt_cfg_register_alias(const char *newname, const char *oldname)
256 if (simgrid_config == NULL)
257 simgrid_config = xbt_cfg_new();
259 xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
260 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
262 res = xbt_dict_get_or_null(simgrid_config, newname);
263 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
265 res = xbt_new0(s_xbt_cfgelm_t, 1);
266 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
268 res->desc = bprintf("Deprecated alias for %s",newname);
269 res->type = xbt_cfgelm_alias;
272 res->content = (xbt_dynar_t)newname;
274 xbt_dict_set(simgrid_config, oldname, res, NULL);
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(*tok == '_', "Invalid config element descriptor: %s%s", entry,
306 "; Should be <name>:<min nb>_to_<max nb>_<type>");
309 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
310 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
311 "; Should be one of 'string', 'int' or 'double'.");
313 xbt_cfg_register(cfg, entrycpy, NULL, type, min, NULL);
315 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
318 /** @brief Displays the declared aliases and their description */
319 void xbt_cfg_aliases(xbt_cfg_t cfg)
321 xbt_dict_cursor_t dict_cursor;
322 unsigned int dynar_cursor;
323 xbt_cfgelm_t variable;
325 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
327 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
328 xbt_dynar_push(names, &name);
329 xbt_dynar_sort_strings(names);
331 xbt_dynar_foreach(names, dynar_cursor, name) {
332 variable = xbt_dict_get((xbt_dict_t )cfg, name);
334 if (variable->type == xbt_cfgelm_alias)
335 printf(" %s: %s\n", name, variable->desc);
339 /** @brief Displays the declared options and their description */
340 void xbt_cfg_help(xbt_cfg_t cfg)
342 xbt_dict_cursor_t dict_cursor;
343 unsigned int dynar_cursor;
344 xbt_cfgelm_t variable;
346 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
348 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
349 xbt_dynar_push(names, &name);
350 xbt_dynar_sort_strings(names);
352 xbt_dynar_foreach(names, dynar_cursor, name) {
355 variable = xbt_dict_get((xbt_dict_t )cfg, name);
356 if (variable->type == xbt_cfgelm_alias)
359 printf(" %s: %s\n", name, variable->desc);
360 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
361 if (variable->min != 1)
362 printf("Arity: min:%d", variable->min);
363 size = xbt_dynar_length(variable->content);
364 printf("Current value%s: ", (size <= 1 ? "" : "s"));
367 printf(size == 0 ? "n/a\n" : "{ ");
368 for (i = 0; i < size; i++) {
369 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
371 switch (variable->type) {
373 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
375 case xbt_cfgelm_double:
376 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
378 case xbt_cfgelm_string:
379 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
381 case xbt_cfgelm_boolean: {
382 int b = xbt_dynar_get_as(variable->content, i, int);
383 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
384 if (b == 0 || b == 1)
385 printf("'%s'%s", bs, sep);
387 printf("'%s/%d'%s", bs, b, sep);
391 printf("Invalid type!!%s", sep);
396 xbt_dynar_free(&names);
399 /** @brief Check that each variable have the right amount of values */
400 void xbt_cfg_check(void)
402 xbt_dict_cursor_t cursor;
403 xbt_cfgelm_t variable;
406 xbt_dict_foreach((xbt_dict_t) simgrid_config, cursor, name, variable) {
407 if (variable->type == xbt_cfgelm_alias)
410 int size = xbt_dynar_length(variable->content);
411 if (variable->min > size) {
412 xbt_dict_cursor_free(&cursor);
413 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
414 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
417 if (variable->isdefault && size > variable->min) {
418 xbt_dict_cursor_free(&cursor);
419 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
420 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
423 xbt_dict_cursor_free(&cursor);
426 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
428 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
430 // The user used the old name. Switch to the new one after a short warning
431 while (res && res->type == xbt_cfgelm_alias) {
432 const char* newname = (const char *)res->content;
433 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
434 res = xbt_cfgelm_get(cfg, newname, type);
440 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
443 xbt_assert(type == xbt_cfgelm_any || res->type == type,
444 "You tried to access to the config element %s as an %s, but its type is %s.",
445 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
449 /** @brief Get the type of this variable in that configuration set
451 * @param cfg the config set
452 * @param name the name of the element
454 * @return the type of the given element
456 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
458 xbt_cfgelm_t variable = NULL;
460 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
462 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
464 XBT_DEBUG("type in variable = %d", (int)variable->type);
465 return variable->type;
468 /*----[ Setting ]---------------------------------------------------------*/
469 /** @brief va_args version of xbt_cfg_set
471 * @param cfg config set to fill
472 * @param name variable name
473 * @param pa variable value
475 * Add some values to the config set.
477 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
482 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
487 type = xbt_cfg_get_type(cfg, name);
490 if (e.category == not_found_error) {
492 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
498 case xbt_cfgelm_string:
499 str = va_arg(pa, char *);
500 xbt_cfg_set_string(name, str);
504 xbt_cfg_set_int(name, i);
506 case xbt_cfgelm_double:
507 d = va_arg(pa, double);
508 xbt_cfg_set_double(name, d);
510 case xbt_cfgelm_boolean:
511 str = va_arg(pa, char *);
512 xbt_cfg_set_boolean(name, str);
515 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
519 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
521 * @param cfg config set to fill
522 * @param name variable name
523 * @param ... variable value
525 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
530 xbt_cfg_set_vargs(cfg, name, pa);
534 /** @brief Add values parsed from a string into a config set
536 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
537 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
538 * already registered variable, and [value] conforms to the data type under which this variable was registered.
540 * @todo This is a crude manual parser, it should be a proper lexer.
542 void xbt_cfg_set_parse(const char *options)
544 if (!options || !strlen(options)) { /* nothing to do */
547 char *optionlist_cpy = xbt_strdup(options);
549 XBT_DEBUG("List to parse and set:'%s'", options);
550 char *option = optionlist_cpy;
551 while (1) { /* breaks in the code */
555 int len = strlen(name);
556 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
559 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
560 XBT_DEBUG("Take %c.", *option);
563 if (option - name == len) {
564 XBT_DEBUG("Boundary=EOL");
565 option = NULL; /* don't do next iteration */
567 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
568 /* Pass the following blank chars */
570 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
571 /* fprintf(stderr,"Ignore a blank char.\n"); */
574 if (option - name == len - 1)
575 option = NULL; /* don't do next iteration */
577 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
579 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
584 char *val = strchr(name, ':');
585 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
586 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
589 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
590 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
593 xbt_cfg_set_as_string(name,val);
595 free(optionlist_cpy);
599 free(optionlist_cpy);
602 /** @brief Set the value of a variable, using the string representation of that value
604 * @param key name of the variable to modify
605 * @param value string representation of the value to set
607 * @return the first char after the parsed value in val
610 void *xbt_cfg_set_as_string(const char *key, const char *value) {
614 volatile xbt_cfgelm_t variable = NULL;
619 while (variable == NULL) {
620 variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
621 if (variable->type == xbt_cfgelm_alias) {
622 const char *newname = (const char*)variable->content;
623 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
629 if (e.category == not_found_error) {
631 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
636 switch (variable->type) {
637 case xbt_cfgelm_string:
638 xbt_cfg_set_string(key, value); /* throws */
641 i = strtol(value, &ret, 0);
643 xbt_die("Value of option %s not valid. Should be an integer", key);
645 xbt_cfg_set_int(key, i); /* throws */
647 case xbt_cfgelm_double:
648 d = strtod(value, &ret);
650 xbt_die("Value of option %s not valid. Should be a double", key);
652 xbt_cfg_set_double(key, d); /* throws */
654 case xbt_cfgelm_boolean:
655 xbt_cfg_set_boolean(key, value); /* throws */
656 ret = (char *)value + strlen(value);
659 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
665 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
667 * This is useful to change the default value of a variable while allowing
668 * users to override it with command line arguments
670 void xbt_cfg_setdefault_int(const char *name, int val)
672 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
674 if (variable->isdefault){
675 xbt_cfg_set_int(name, val);
676 variable->isdefault = 1;
678 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
681 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
683 * This is useful to change the default value of a variable while allowing
684 * users to override it with command line arguments
686 void xbt_cfg_setdefault_double(const char *name, double val)
688 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
690 if (variable->isdefault) {
691 xbt_cfg_set_double(name, val);
692 variable->isdefault = 1;
694 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
697 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
699 * This is useful to change the default value of a variable while allowing
700 * users to override it with command line arguments
702 void xbt_cfg_setdefault_string(const char *name, const char *val)
704 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
706 if (variable->isdefault){
707 xbt_cfg_set_string(name, val);
708 variable->isdefault = 1;
710 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
713 /** @brief Set an boolean 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_boolean(const char *name, const char *val)
720 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
722 if (variable->isdefault){
723 xbt_cfg_set_boolean(name, val);
724 variable->isdefault = 1;
727 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
730 /** @brief Set or add an integer value to \a name within \a cfg
732 * @param cfg the config set
733 * @param name the name of the variable
734 * @param val the value of the variable
736 void xbt_cfg_set_int(const char *name, int val)
738 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
740 xbt_dynar_set(variable->content, 0, &val);
742 if (variable->cb_set)
743 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
744 variable->isdefault = 0;
747 /** @brief Set or add a double value to \a name within \a cfg
749 * @param cfg the config set
750 * @param name the name of the variable
751 * @param val the doule to set
753 void xbt_cfg_set_double(const char *name, double val)
755 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
757 xbt_dynar_set(variable->content, 0, &val);
759 if (variable->cb_set)
760 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
761 variable->isdefault = 0;
764 /** @brief Set or add a string value to \a name within \a cfg
766 * @param cfg the config set
767 * @param name the name of the variable
768 * @param val the value to be added
771 void xbt_cfg_set_string(const char *name, const char *val)
773 char *newval = xbt_strdup(val);
774 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
776 if (!xbt_dynar_is_empty(variable->content)) {
777 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
781 xbt_dynar_set(variable->content, 0, &newval);
783 if (variable->cb_set)
784 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
785 variable->isdefault = 0;
788 /** @brief Set or add a boolean value to \a name within \a cfg
790 * @param name the name of the variable
791 * @param val the value of the variable
793 void xbt_cfg_set_boolean(const char *name, const char *val)
796 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
798 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
799 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
803 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
808 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
809 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
812 xbt_dynar_set(variable->content, 0, &bval);
814 if (variable->cb_set)
815 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
816 variable->isdefault = 0;
820 /* Say if the value is the default value */
821 int xbt_cfg_is_default_value(const char *name)
823 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
824 return variable->isdefault;
827 /*----[ Getting ]---------------------------------------------------------*/
828 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
830 * @param name the name of the variable
832 * Returns the first value from the config set under the given name.
833 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
835 int xbt_cfg_get_int(const char *name)
837 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
839 if (xbt_dynar_length(variable->content) > 1) {
840 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
841 name, xbt_dynar_length(variable->content));
844 return xbt_dynar_get_as(variable->content, 0, int);
847 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
849 * @param cfg the config set
850 * @param name the name of the variable
852 * Returns the first value from the config set under the given name.
853 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
855 double xbt_cfg_get_double(const char *name)
857 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
859 if (xbt_dynar_length(variable->content) > 1) {
860 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
861 name, xbt_dynar_length(variable->content));
864 return xbt_dynar_get_as(variable->content, 0, double);
867 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
869 * @param cfg the config set
870 * @param name the name of the variable
872 * Returns the first value from the config set under the given name.
873 * If there is more than one value, it will issue a warning. Consider using
874 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
876 * \warning the returned value is the actual content of the config set
878 char *xbt_cfg_get_string(const char *name)
880 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
882 if (xbt_dynar_length(variable->content) > 1) {
883 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
884 name, xbt_dynar_length(variable->content));
885 } else if (xbt_dynar_is_empty(variable->content)) {
889 return xbt_dynar_get_as(variable->content, 0, char *);
892 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
894 * @param cfg the config set
895 * @param name the name of the variable
897 * Returns the first value from the config set under the given name.
898 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
900 int xbt_cfg_get_boolean(const char *name)
902 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
904 if (xbt_dynar_length(variable->content) > 1) {
905 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
906 name, xbt_dynar_length(variable->content));
909 return xbt_dynar_get_as(variable->content, 0, int);
912 /** @brief Retrieve the dynar of all the values stored in a variable
914 * @param cfg where to search in
915 * @param name what to search for
917 * Get the data stored in the config set.
919 * \warning the returned value is the actual content of the config set
921 xbt_dynar_t xbt_cfg_get_dynar(const char *name)
923 xbt_cfgelm_t variable = NULL;
927 variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
929 if (e.category == not_found_error) {
931 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
936 return variable->content;
939 /** @brief Retrieve one of the integer value of a variable */
940 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
942 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
943 return xbt_dynar_get_as(variable->content, pos, int);
946 /** @brief Retrieve one of the double value of a variable */
947 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
949 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
950 return xbt_dynar_get_as(variable->content, pos, double);
953 /** @brief Retrieve one of the string value of a variable */
954 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
956 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
957 return xbt_dynar_get_as(variable->content, pos, char *);
960 /** @brief Retrieve one of the boolean value of a variable */
961 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
963 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
964 return xbt_dynar_get_as(variable->content, pos, int);
971 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
973 XBT_TEST_SUITE("config", "Configuration support");
975 static xbt_cfg_t make_set()
977 xbt_cfg_t set = NULL;
979 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
980 xbt_cfg_register_str(&set, "speed:1_int");
981 xbt_cfg_register_str(&set, "peername:1_string");
982 xbt_cfg_register_str(&set, "user:1_string");
985 } /* end_of_make_set */
987 extern xbt_cfg_t simgrid_config;
989 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
991 simgrid_config = make_set();
992 xbt_test_add("Alloc and free a config set");
993 xbt_cfg_set_parse("peername:veloce user:bidule");
994 xbt_cfg_free(&simgrid_config);
997 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1001 simgrid_config = make_set();
1002 xbt_test_add("Having too few elements for speed");
1003 xbt_cfg_set_parse("peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1007 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1008 xbt_test_fail("Got an exception. msg=%s", e.msg);
1012 xbt_test_add("Having too much values of 'speed'");
1013 xbt_cfg_set_parse("peername:toto:42 user:machin");
1015 xbt_cfg_set_parse("speed:42 speed:24");
1017 if (e.category != mismatch_error ||
1018 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1019 xbt_test_fail("Got an exception. msg=%s", e.msg);
1023 xbt_cfg_free(&simgrid_config);
1026 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1028 simgrid_config = make_set();
1029 xbt_test_add("Get a single value");
1031 /* get_single_value */
1034 xbt_cfg_set_parse("peername:toto:42 speed:42");
1035 ival = xbt_cfg_get_int("speed");
1037 xbt_test_fail("Speed value = %d, I expected 42", ival);
1040 xbt_test_add("Access to a non-existant entry");
1045 xbt_cfg_set_parse("color:blue");
1047 if (e.category != not_found_error)
1048 xbt_test_exception(e);
1052 xbt_cfg_free(&simgrid_config);
1054 #endif /* SIMGRID_TEST */