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->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)
201 *cfg = xbt_cfg_new();
202 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
203 "type of %s not valid (%d should be between %d and %d)",
204 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
206 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
207 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
209 res = xbt_new(s_xbt_cfgelm_t, 1);
210 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
211 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
213 res->desc = xbt_strdup(desc);
217 res->cb_set = cb_set;
222 res->content = xbt_dynar_new(sizeof(int), NULL);
224 case xbt_cfgelm_double:
225 res->content = xbt_dynar_new(sizeof(double), NULL);
227 case xbt_cfgelm_string:
228 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
230 case xbt_cfgelm_boolean:
231 res->content = xbt_dynar_new(sizeof(int), NULL);
234 XBT_ERROR("%d is an invalid type code", (int)type);
237 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
240 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
241 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,1,1,cb_set);
242 xbt_cfg_setdefault_double(name, default_value);
244 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
245 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,1,1,cb_set);
246 xbt_cfg_setdefault_int(name, default_value);
248 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
249 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,1,1,cb_set);
250 xbt_cfg_setdefault_string(name, default_value);
252 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
253 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,1,1,cb_set);
254 xbt_cfg_setdefault_boolean(name, default_value);
257 void xbt_cfg_register_alias(const char *newname, const char *oldname)
259 if (simgrid_config == NULL)
260 simgrid_config = xbt_cfg_new();
262 xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
263 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
265 res = xbt_dict_get_or_null(simgrid_config, newname);
266 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
268 res = xbt_new0(s_xbt_cfgelm_t, 1);
269 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
271 res->desc = bprintf("Deprecated alias for %s",newname);
272 res->type = xbt_cfgelm_alias;
276 res->content = (xbt_dynar_t)newname;
278 xbt_dict_set(simgrid_config, oldname, res, NULL);
281 /** @brief Unregister an element from a config set.
283 * @param cfg the config set
284 * @param name the name of the element to be freed
286 * Note that it removes both the description and the actual content.
287 * Throws not_found when no such element exists.
289 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
291 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
292 xbt_dict_remove((xbt_dict_t) cfg, name);
296 * @brief Parse a string and register the stuff described.
298 * @param cfg the config set
299 * @param entry a string describing the element to register
301 * The string may consist in several variable descriptions separated by a space.
302 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
303 * with type being one of 'string','int' or 'double'.
305 * Note that this does not allow to set the description, so you should prefer the other interface
307 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
309 char *entrycpy = xbt_strdup(entry);
313 e_xbt_cfgelm_type_t type;
314 XBT_DEBUG("Register string '%s'", entry);
316 tok = strchr(entrycpy, ':');
317 xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
320 min = strtol(tok, &tok, 10);
321 xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
323 xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
324 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
325 tok += strlen("_to_");
327 max = strtol(tok, &tok, 10);
328 xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
330 xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
331 "; Should be <name>:<min nb>_to_<max nb>_<type>");
334 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
335 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
336 "; Should be one of 'string', 'int' or 'double'.");
338 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
340 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
343 /** @brief Displays the declared aliases and their description */
344 void xbt_cfg_aliases(xbt_cfg_t cfg)
346 xbt_dict_cursor_t dict_cursor;
347 unsigned int dynar_cursor;
348 xbt_cfgelm_t variable;
350 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
352 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
353 xbt_dynar_push(names, &name);
354 xbt_dynar_sort_strings(names);
356 xbt_dynar_foreach(names, dynar_cursor, name) {
357 variable = xbt_dict_get((xbt_dict_t )cfg, name);
359 if (variable->type == xbt_cfgelm_alias)
360 printf(" %s: %s\n", name, variable->desc);
364 /** @brief Displays the declared options and their description */
365 void xbt_cfg_help(xbt_cfg_t cfg)
367 xbt_dict_cursor_t dict_cursor;
368 unsigned int dynar_cursor;
369 xbt_cfgelm_t variable;
371 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
373 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
374 xbt_dynar_push(names, &name);
375 xbt_dynar_sort_strings(names);
377 xbt_dynar_foreach(names, dynar_cursor, name) {
380 variable = xbt_dict_get((xbt_dict_t )cfg, name);
381 if (variable->type == xbt_cfgelm_alias)
384 printf(" %s: %s\n", name, variable->desc);
385 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
386 if (variable->min != 1 || variable->max != 1) {
387 printf("Arity: min:%d to max:", variable->min);
388 if (variable->max == 0)
389 printf("(no bound); ");
391 printf("%d; ", variable->max);
393 size = xbt_dynar_length(variable->content);
394 printf("Current value%s: ", (size <= 1 ? "" : "s"));
397 printf(size == 0 ? "n/a\n" : "{ ");
398 for (i = 0; i < size; i++) {
399 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
401 switch (variable->type) {
403 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
405 case xbt_cfgelm_double:
406 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
408 case xbt_cfgelm_string:
409 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
411 case xbt_cfgelm_boolean: {
412 int b = xbt_dynar_get_as(variable->content, i, int);
413 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
414 if (b == 0 || b == 1)
415 printf("'%s'%s", bs, sep);
417 printf("'%s/%d'%s", bs, b, sep);
421 printf("Invalid type!!%s", sep);
425 xbt_dynar_free(&names);
428 /** @brief Check that each variable have the right amount of values */
429 void xbt_cfg_check(void)
431 xbt_dict_cursor_t cursor;
432 xbt_cfgelm_t variable;
435 xbt_dict_foreach((xbt_dict_t) simgrid_config, cursor, name, variable) {
436 if (variable->type == xbt_cfgelm_alias)
439 int size = xbt_dynar_length(variable->content);
440 if (variable->min > size) {
441 xbt_dict_cursor_free(&cursor);
442 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
443 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
446 if (variable->isdefault && size > variable->min) {
447 xbt_dict_cursor_free(&cursor);
448 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
449 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
452 if (variable->max > 0 && variable->max < size) {
453 xbt_dict_cursor_free(&cursor);
454 THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
455 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
458 xbt_dict_cursor_free(&cursor);
461 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
463 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
465 // The user used the old name. Switch to the new one after a short warning
466 while (res && res->type == xbt_cfgelm_alias) {
467 const char* newname = (const char *)res->content;
468 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
469 res = xbt_cfgelm_get(cfg, newname, type);
475 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
478 xbt_assert(type == xbt_cfgelm_any || res->type == type,
479 "You tried to access to the config element %s as an %s, but its type is %s.",
480 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
484 /** @brief Get the type of this variable in that configuration set
486 * @param cfg the config set
487 * @param name the name of the element
489 * @return the type of the given element
491 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
493 xbt_cfgelm_t variable = NULL;
495 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
497 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
499 XBT_DEBUG("type in variable = %d", (int)variable->type);
500 return variable->type;
503 /*----[ Setting ]---------------------------------------------------------*/
504 /** @brief va_args version of xbt_cfg_set
506 * @param cfg config set to fill
507 * @param name variable name
508 * @param pa variable value
510 * Add some values to the config set.
512 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
517 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
522 type = xbt_cfg_get_type(cfg, name);
525 if (e.category == not_found_error) {
527 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
533 case xbt_cfgelm_string:
534 str = va_arg(pa, char *);
535 xbt_cfg_set_string(name, str);
539 xbt_cfg_set_int(name, i);
541 case xbt_cfgelm_double:
542 d = va_arg(pa, double);
543 xbt_cfg_set_double(name, d);
545 case xbt_cfgelm_boolean:
546 str = va_arg(pa, char *);
547 xbt_cfg_set_boolean(name, str);
550 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
554 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
556 * @param cfg config set to fill
557 * @param name variable name
558 * @param ... variable value
560 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
565 xbt_cfg_set_vargs(cfg, name, pa);
569 /** @brief Add values parsed from a string into a config set
571 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
572 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
573 * already registered variable, and [value] conforms to the data type under which this variable was registered.
575 * @todo This is a crude manual parser, it should be a proper lexer.
577 void xbt_cfg_set_parse(const char *options)
579 if (!options || !strlen(options)) { /* nothing to do */
582 char *optionlist_cpy = xbt_strdup(options);
584 XBT_DEBUG("List to parse and set:'%s'", options);
585 char *option = optionlist_cpy;
586 while (1) { /* breaks in the code */
590 int len = strlen(name);
591 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
594 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
595 XBT_DEBUG("Take %c.", *option);
598 if (option - name == len) {
599 XBT_DEBUG("Boundary=EOL");
600 option = NULL; /* don't do next iteration */
602 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
603 /* Pass the following blank chars */
605 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
606 /* fprintf(stderr,"Ignore a blank char.\n"); */
609 if (option - name == len - 1)
610 option = NULL; /* don't do next iteration */
612 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
614 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
619 char *val = strchr(name, ':');
620 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
621 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
624 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
625 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
628 xbt_cfg_set_as_string(name,val);
630 free(optionlist_cpy);
634 free(optionlist_cpy);
637 /** @brief Set the value of a variable, using the string representation of that value
639 * @param key name of the variable to modify
640 * @param value string representation of the value to set
642 * @return the first char after the parsed value in val
645 void *xbt_cfg_set_as_string(const char *key, const char *value) {
649 volatile xbt_cfgelm_t variable = NULL;
654 while (variable == NULL) {
655 variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
656 if (variable->type == xbt_cfgelm_alias) {
657 const char *newname = (const char*)variable->content;
658 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
664 if (e.category == not_found_error) {
666 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
671 switch (variable->type) {
672 case xbt_cfgelm_string:
673 xbt_cfg_set_string(key, value); /* throws */
676 i = strtol(value, &ret, 0);
678 xbt_die("Value of option %s not valid. Should be an integer", key);
680 xbt_cfg_set_int(key, i); /* throws */
682 case xbt_cfgelm_double:
683 d = strtod(value, &ret);
685 xbt_die("Value of option %s not valid. Should be a double", key);
687 xbt_cfg_set_double(key, d); /* throws */
689 case xbt_cfgelm_boolean:
690 xbt_cfg_set_boolean(key, value); /* throws */
691 ret = (char *)value + strlen(value);
694 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
700 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
702 * This is useful to change the default value of a variable while allowing
703 * users to override it with command line arguments
705 void xbt_cfg_setdefault_int(const char *name, int val)
707 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
709 if (variable->isdefault){
710 xbt_cfg_set_int(name, val);
711 variable->isdefault = 1;
713 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
716 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
718 * This is useful to change the default value of a variable while allowing
719 * users to override it with command line arguments
721 void xbt_cfg_setdefault_double(const char *name, double val)
723 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
725 if (variable->isdefault) {
726 xbt_cfg_set_double(name, val);
727 variable->isdefault = 1;
729 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
732 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
734 * This is useful to change the default value of a variable while allowing
735 * users to override it with command line arguments
737 void xbt_cfg_setdefault_string(const char *name, const char *val)
739 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
741 if (variable->isdefault){
742 xbt_cfg_set_string(name, val);
743 variable->isdefault = 1;
745 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
748 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
750 * This is useful to change the default value of a variable while allowing
751 * users to override it with command line arguments
753 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
755 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
757 if (variable->isdefault){
758 xbt_cfg_set_boolean(name, val);
759 variable->isdefault = 1;
762 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
765 /** @brief Set or add an integer value to \a name within \a cfg
767 * @param cfg the config set
768 * @param name the name of the variable
769 * @param val the value of the variable
771 void xbt_cfg_set_int(const char *name, int val)
773 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
775 if (variable->max == 1) {
776 xbt_dynar_set(variable->content, 0, &val);
778 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
779 THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
780 val, name, variable->max);
782 xbt_dynar_push(variable->content, &val);
785 if (variable->cb_set)
786 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
787 variable->isdefault = 0;
790 /** @brief Set or add a double value to \a name within \a cfg
792 * @param cfg the config set
793 * @param name the name of the variable
794 * @param val the doule to set
796 void xbt_cfg_set_double(const char *name, double val)
798 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
800 if (variable->max == 1) {
801 xbt_dynar_set(variable->content, 0, &val);
803 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
804 THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
805 val, name, variable->max);
807 xbt_dynar_push(variable->content, &val);
810 if (variable->cb_set)
811 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
812 variable->isdefault = 0;
815 /** @brief Set or add a string value to \a name within \a cfg
817 * @param cfg the config set
818 * @param name the name of the variable
819 * @param val the value to be added
822 void xbt_cfg_set_string(const char *name, const char *val)
824 char *newval = xbt_strdup(val);
825 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
827 if (variable->max == 1) {
828 if (!xbt_dynar_is_empty(variable->content)) {
829 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
833 xbt_dynar_set(variable->content, 0, &newval);
836 && xbt_dynar_length(variable->content) == variable->max)
837 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
838 name, val, variable->max);
840 xbt_dynar_push(variable->content, &newval);
843 if (variable->cb_set)
844 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
845 variable->isdefault = 0;
848 /** @brief Set or add a boolean value to \a name within \a cfg
850 * @param name the name of the variable
851 * @param val the value of the variable
853 void xbt_cfg_set_boolean(const char *name, const char *val)
856 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
858 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
859 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
863 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
868 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
869 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
872 if (variable->max == 1) {
873 xbt_dynar_set(variable->content, 0, &bval);
875 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
876 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
877 val, name, variable->max);
879 xbt_dynar_push(variable->content, &bval);
882 if (variable->cb_set)
883 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
884 variable->isdefault = 0;
888 /* Say if the value is the default value */
889 int xbt_cfg_is_default_value(const char *name)
891 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
892 return variable->isdefault;
895 /*----[ Getting ]---------------------------------------------------------*/
896 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
898 * @param name the name of the variable
900 * Returns the first value from the config set under the given name.
901 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
903 int xbt_cfg_get_int(const char *name)
905 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
907 if (xbt_dynar_length(variable->content) > 1) {
908 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
909 name, xbt_dynar_length(variable->content));
912 return xbt_dynar_get_as(variable->content, 0, int);
915 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
917 * @param cfg the config set
918 * @param name the name of the variable
920 * Returns the first value from the config set under the given name.
921 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
923 double xbt_cfg_get_double(const char *name)
925 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
927 if (xbt_dynar_length(variable->content) > 1) {
928 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
929 name, xbt_dynar_length(variable->content));
932 return xbt_dynar_get_as(variable->content, 0, double);
935 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
937 * @param cfg the config set
938 * @param name the name of the variable
940 * Returns the first value from the config set under the given name.
941 * If there is more than one value, it will issue a warning. Consider using
942 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
944 * \warning the returned value is the actual content of the config set
946 char *xbt_cfg_get_string(const char *name)
948 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
950 if (xbt_dynar_length(variable->content) > 1) {
951 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
952 name, xbt_dynar_length(variable->content));
953 } else if (xbt_dynar_is_empty(variable->content)) {
957 return xbt_dynar_get_as(variable->content, 0, char *);
960 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
962 * @param cfg the config set
963 * @param name the name of the variable
965 * Returns the first value from the config set under the given name.
966 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
968 int xbt_cfg_get_boolean(const char *name)
970 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
972 if (xbt_dynar_length(variable->content) > 1) {
973 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
974 name, xbt_dynar_length(variable->content));
977 return xbt_dynar_get_as(variable->content, 0, int);
980 /** @brief Retrieve the dynar of all the values stored in a variable
982 * @param cfg where to search in
983 * @param name what to search for
985 * Get the data stored in the config set.
987 * \warning the returned value is the actual content of the config set
989 xbt_dynar_t xbt_cfg_get_dynar(const char *name)
991 xbt_cfgelm_t variable = NULL;
995 variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
997 if (e.category == not_found_error) {
999 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1004 return variable->content;
1007 /** @brief Retrieve one of the integer value of a variable */
1008 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1010 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1011 return xbt_dynar_get_as(variable->content, pos, int);
1014 /** @brief Retrieve one of the double value of a variable */
1015 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1017 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1018 return xbt_dynar_get_as(variable->content, pos, double);
1021 /** @brief Retrieve one of the string value of a variable */
1022 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1024 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1025 return xbt_dynar_get_as(variable->content, pos, char *);
1028 /** @brief Retrieve one of the boolean value of a variable */
1029 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1031 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1032 return xbt_dynar_get_as(variable->content, pos, int);
1039 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1041 XBT_TEST_SUITE("config", "Configuration support");
1043 static xbt_cfg_t make_set()
1045 xbt_cfg_t set = NULL;
1047 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1048 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1049 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1050 xbt_cfg_register_str(&set, "user:1_to_10_string");
1053 } /* end_of_make_set */
1055 extern xbt_cfg_t simgrid_config;
1057 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1059 simgrid_config = make_set();
1060 xbt_test_add("Alloc and free a config set");
1061 xbt_cfg_set_parse("peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1062 xbt_cfg_free(&simgrid_config);
1065 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1069 simgrid_config = make_set();
1070 xbt_test_add("Having too few elements for speed");
1071 xbt_cfg_set_parse("peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1075 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1076 xbt_test_fail("Got an exception. msg=%s", e.msg);
1080 xbt_test_add("Having too much values of 'speed'");
1081 xbt_cfg_set_parse("peername:toto:42 user:alegrand");
1083 xbt_cfg_set_parse("speed:42 speed:24 speed:34");
1085 if (e.category != mismatch_error ||
1086 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1087 xbt_test_fail("Got an exception. msg=%s", e.msg);
1091 xbt_cfg_free(&simgrid_config);
1094 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1096 simgrid_config = make_set();
1097 xbt_test_add("Get a single value");
1099 /* get_single_value */
1102 xbt_cfg_set_parse("peername:toto:42 speed:42");
1103 ival = xbt_cfg_get_int("speed");
1105 xbt_test_fail("Speed value = %d, I expected 42", ival);
1108 xbt_test_add("Get multiple values");
1110 /* get_multiple_value */
1113 xbt_cfg_set_parse("peername:veloce user:foo\nuser:bar\tuser:toto");
1114 xbt_cfg_set_parse("speed:42");
1116 dyn = xbt_cfg_get_dynar("user");
1118 if (xbt_dynar_length(dyn) != 3)
1119 xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1121 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1122 xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1124 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1125 xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1127 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1128 xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1131 xbt_test_add("Access to a non-existant entry");
1136 xbt_cfg_set_parse("color:blue");
1138 if (e.category != not_found_error)
1139 xbt_test_exception(e);
1143 xbt_cfg_free(&simgrid_config);
1145 #endif /* SIMGRID_TEST */