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;
41 /* actual content (could be an union or something) */
43 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
45 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
46 { "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,
61 e_xbt_cfgelm_type_t type);
63 /*----[ Memory management ]-----------------------------------------------*/
65 /** @brief Constructor
67 * Initialise a config set
71 xbt_cfg_t xbt_cfg_new(void)
73 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
76 /** \brief Copy an existing configuration set
78 * @param whereto the config set to be created
79 * @param tocopy the source data
81 * This only copy the registrations, not the actual content
84 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
86 xbt_dict_cursor_t cursor = NULL;
87 xbt_cfgelm_t variable = NULL;
90 XBT_DEBUG("Copy cfg set %p", tocopy);
92 xbt_assert(tocopy, "cannot copy NULL config");
94 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
95 xbt_cfg_register(whereto, name, variable->desc, variable->type,
96 variable->min, variable->max, variable->cb_set);
100 /** @brief Destructor */
101 void xbt_cfg_free(xbt_cfg_t * cfg)
103 XBT_DEBUG("Frees cfg set %p", cfg);
104 xbt_dict_free((xbt_dict_t *) cfg);
107 /** @brief Dump a config set for debuging purpose
109 * @param name The name to give to this config set
110 * @param indent what to write at the beginning of each line (right number of spaces)
111 * @param cfg the config set
113 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
115 xbt_dict_t dict = (xbt_dict_t) cfg;
116 xbt_dict_cursor_t cursor = NULL;
117 xbt_cfgelm_t variable = NULL;
126 printf("%s>> Dumping of the config set '%s':\n", indent, name);
128 xbt_dict_foreach(dict, cursor, key, variable) {
129 printf("%s %s:", indent, key);
131 size = xbt_dynar_length(variable->content);
133 ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
134 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
136 switch (variable->type) {
139 for (i = 0; i < size; i++) {
140 ival = xbt_dynar_get_as(variable->content, i, int);
141 printf("%s %d\n", indent, ival);
145 case xbt_cfgelm_double:
146 for (i = 0; i < size; i++) {
147 dval = xbt_dynar_get_as(variable->content, i, double);
148 printf("%s %f\n", indent, dval);
152 case xbt_cfgelm_string:
153 for (i = 0; i < size; i++) {
154 sval = xbt_dynar_get_as(variable->content, i, char *);
155 printf("%s %s\n", indent, sval);
159 case xbt_cfgelm_boolean:
160 for (i = 0; i < size; i++) {
161 ival = xbt_dynar_get_as(variable->content, i, int);
162 printf("%s %d\n", indent, ival);
166 case xbt_cfgelm_alias:
171 printf("%s Invalid type!!\n", indent);
178 printf("%s<< End of the config set '%s'\n", indent, name);
181 xbt_dict_cursor_free(&cursor);
185 * free an config element
188 void xbt_cfgelm_free(void *data)
190 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
192 XBT_DEBUG("Frees cfgelm %p", c);
196 if (c->type != xbt_cfgelm_alias)
197 xbt_dynar_free(&(c->content));
201 /*----[ Registering stuff ]-----------------------------------------------*/
203 /** @brief Register an element within a config set
205 * @param cfg the config set
206 * @param name the name of the config element
207 * @param desc a description for this item (used by xbt_cfg_help())
208 * @param type the type of the config element
209 * @param min the minimum number of values for this config element
210 * @param max the maximum number of values for this config element
211 * @param cb_set callback function called when a value is set
214 void xbt_cfg_register(xbt_cfg_t * cfg,
215 const char *name, const char *desc,
216 e_xbt_cfgelm_type_t type, int min,
217 int max, xbt_cfg_cb_t cb_set)
222 *cfg = xbt_cfg_new();
223 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
224 "type of %s not valid (%d should be between %d and %d)",
225 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
226 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
227 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
229 res = xbt_new(s_xbt_cfgelm_t, 1);
230 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
231 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
233 res->desc = xbt_strdup(desc);
237 res->cb_set = cb_set;
242 res->content = xbt_dynar_new(sizeof(int), NULL);
245 case xbt_cfgelm_double:
246 res->content = xbt_dynar_new(sizeof(double), NULL);
249 case xbt_cfgelm_string:
250 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
253 case xbt_cfgelm_boolean:
254 res->content = xbt_dynar_new(sizeof(int), NULL);
258 XBT_ERROR("%d is an invalid type code", (int)type);
262 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
265 void xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname)
268 *cfg = xbt_cfg_new();
270 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, oldname);
271 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
273 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, newname);
274 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
276 res = xbt_new0(s_xbt_cfgelm_t, 1);
277 XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
279 res->desc = bprintf("Deprecated alias for %s",newname);
280 res->type = xbt_cfgelm_alias;
284 res->content = (xbt_dynar_t)newname;
286 xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
288 /** @brief Unregister an element from a config set.
290 * @param cfg the config set
291 * @param name the name of the element to be freed
293 * Note that it removes both the description and the actual content.
294 * Throws not_found when no such element exists.
297 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
299 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
300 xbt_dict_remove((xbt_dict_t) cfg, name);
304 * @brief Parse a string and register the stuff described.
306 * @param cfg the config set
307 * @param entry a string describing the element to register
309 * The string may consist in several variable descriptions separated by a space.
310 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
311 * with type being one of 'string','int' or 'double'.
313 * Note that this does not allow to set the description, so you should prefer the other interface
316 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
318 char *entrycpy = xbt_strdup(entry);
322 e_xbt_cfgelm_type_t type;
323 XBT_DEBUG("Register string '%s'", entry);
325 tok = strchr(entrycpy, ':');
326 xbt_assert(tok, "Invalid config element descriptor: %s%s",
327 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
330 min = strtol(tok, &tok, 10);
331 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
334 xbt_assert(strcmp(tok, "_to_"),
335 "Invalid config element descriptor : %s%s",
336 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
337 tok += strlen("_to_");
339 max = strtol(tok, &tok, 10);
340 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
343 xbt_assert(*tok == '_',
344 "Invalid config element descriptor: %s%s", entry,
345 "; Should be <name>:<min nb>_to_<max nb>_<type>");
348 for (type = (e_xbt_cfgelm_type_t)0;
349 type < xbt_cfgelm_type_count
350 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
351 xbt_assert(type < xbt_cfgelm_type_count,
352 "Invalid type in config element descriptor: %s%s", entry,
353 "; Should be one of 'string', 'int' or 'double'.");
355 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
357 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
360 /** @brief Displays the declared aliases and their description */
361 void xbt_cfg_aliases(xbt_cfg_t cfg)
363 xbt_dict_cursor_t dict_cursor;
364 unsigned int dynar_cursor;
365 xbt_cfgelm_t variable;
367 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
369 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
370 xbt_dynar_push(names, &name);
371 xbt_dynar_sort_strings(names);
373 xbt_dynar_foreach(names, dynar_cursor, name) {
374 variable = xbt_dict_get((xbt_dict_t )cfg, name);
376 if (variable->type == xbt_cfgelm_alias)
377 printf(" %s: %s\n", name, variable->desc);
380 /** @brief Displays the declared options and their description */
381 void xbt_cfg_help(xbt_cfg_t cfg)
383 xbt_dict_cursor_t dict_cursor;
384 unsigned int dynar_cursor;
385 xbt_cfgelm_t variable;
387 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
389 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
390 xbt_dynar_push(names, &name);
391 xbt_dynar_sort_strings(names);
393 xbt_dynar_foreach(names, dynar_cursor, name) {
396 variable = xbt_dict_get((xbt_dict_t )cfg, name);
397 if (variable->type == xbt_cfgelm_alias)
400 printf(" %s: %s\n", name, variable->desc);
401 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
402 if (variable->min != 1 || variable->max != 1) {
403 printf("Arity: min:%d to max:", variable->min);
404 if (variable->max == 0)
405 printf("(no bound); ");
407 printf("%d; ", variable->max);
409 size = xbt_dynar_length(variable->content);
410 printf("Current value%s: ", (size <= 1 ? "" : "s"));
413 printf(size == 0 ? "n/a\n" : "{ ");
414 for (i = 0; i < size; i++) {
415 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
417 switch (variable->type) {
419 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
422 case xbt_cfgelm_double:
423 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
426 case xbt_cfgelm_string:
427 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
430 case xbt_cfgelm_boolean: {
431 int b = xbt_dynar_get_as(variable->content, i, int);
432 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
433 : xbt_cfgelm_boolean_values[0].false_val;
434 if (b == 0 || b == 1)
435 printf("'%s'%s", bs, sep);
437 printf("'%s/%d'%s", bs, b, sep);
442 printf("Invalid type!!%s", sep);
447 xbt_dynar_free(&names);
450 /** @brief Check that each variable have the right amount of values */
451 void xbt_cfg_check(xbt_cfg_t cfg)
453 xbt_dict_cursor_t cursor;
454 xbt_cfgelm_t variable;
458 xbt_assert(cfg, "NULL config set.");
459 XBT_DEBUG("Check cfg set %p", cfg);
461 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
462 if (variable->type == xbt_cfgelm_alias)
465 size = xbt_dynar_length(variable->content);
466 if (variable->min > size) {
467 xbt_dict_cursor_free(&cursor);
468 THROWF(mismatch_error, 0,
469 "Config elem %s needs at least %d %s, but there is only %d values.",
470 name, variable->min, xbt_cfgelm_type_name[variable->type],
474 if (variable->isdefault && size > variable->min) {
475 xbt_dict_cursor_free(&cursor);
476 THROWF(mismatch_error, 0,
477 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
478 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
481 if (variable->max > 0 && variable->max < size) {
482 xbt_dict_cursor_free(&cursor);
483 THROWF(mismatch_error, 0,
484 "Config elem %s accepts at most %d %s, but there is %d values.",
485 name, variable->max, xbt_cfgelm_type_name[variable->type],
490 xbt_dict_cursor_free(&cursor);
493 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
495 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
497 // The user used the old name. Switch to the new one after a short warning
498 while (res && res->type == xbt_cfgelm_alias) {
499 const char* newname = (const char *)res->content;
500 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
501 res = xbt_cfgelm_get(cfg, newname, type);
507 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
510 xbt_assert(type == xbt_cfgelm_any || res->type == type,
511 "You tried to access to the config element %s as an %s, but its type is %s.",
513 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
518 /** @brief Get the type of this variable in that configuration set
520 * @param cfg the config set
521 * @param name the name of the element
523 * @return the type of the given element
526 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
529 xbt_cfgelm_t variable = NULL;
531 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
533 THROWF(not_found_error, 0,
534 "Can't get the type of '%s' since this variable does not exist",
537 XBT_DEBUG("type in variable = %d", (int)variable->type);
539 return variable->type;
542 /*----[ Setting ]---------------------------------------------------------*/
543 /** @brief va_args version of xbt_cfg_set
545 * @param cfg config set to fill
546 * @param name variable name
547 * @param pa variable value
549 * Add some values to the config set.
551 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
556 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
561 type = xbt_cfg_get_type(cfg, name);
564 if (e.category == not_found_error) {
566 THROWF(not_found_error, 0,
567 "Can't set the property '%s' since it's not registered",
574 case xbt_cfgelm_string:
575 str = va_arg(pa, char *);
576 xbt_cfg_set_string(cfg, name, str);
581 xbt_cfg_set_int(cfg, name, i);
584 case xbt_cfgelm_double:
585 d = va_arg(pa, double);
586 xbt_cfg_set_double(cfg, name, d);
589 case xbt_cfgelm_boolean:
590 str = va_arg(pa, char *);
591 xbt_cfg_set_boolean(cfg, name, str);
595 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
599 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
601 * @param cfg config set to fill
602 * @param name variable name
603 * @param ... variable value
606 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
611 xbt_cfg_set_vargs(cfg, name, pa);
615 /** @brief Add values parsed from a string into a config set
617 * @param cfg config set to fill
618 * @param options a string containing the content to add to the config set. This
619 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
620 * like "[name]:[value]" where [name] is the name of an already registred
621 * variable, and [value] conforms to the data type under which this variable was
624 * @todo This is a crude manual parser, it should be a proper lexer.
627 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
629 char *optionlist_cpy;
630 char *option, *name, *val;
635 if (!options || !strlen(options)) { /* nothing to do */
638 optionlist_cpy = xbt_strdup(options);
640 XBT_DEBUG("List to parse and set:'%s'", options);
641 option = optionlist_cpy;
642 while (1) { /* breaks in the code */
648 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
649 name, len, (long) (option - name));
652 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
653 && *option != '\t' && *option != ',') {
654 XBT_DEBUG("Take %c.", *option);
657 if (option - name == len) {
658 XBT_DEBUG("Boundary=EOL");
659 option = NULL; /* don't do next iteration */
662 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
663 *option, len, (long) (option - name));
665 /* Pass the following blank chars */
667 while (option - name < (len - 1) &&
668 (*option == ' ' || *option == '\n' || *option == '\t')) {
669 /* fprintf(stderr,"Ignore a blank char.\n"); */
672 if (option - name == len - 1)
673 option = NULL; /* don't do next iteration */
675 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
677 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
682 val = strchr(name, ':');
684 /* don't free(optionlist_cpy) here, 'name' points inside it */
685 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
690 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
691 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
694 xbt_cfg_set_as_string(cfg,name,val);
696 free(optionlist_cpy);
700 free(optionlist_cpy);
703 /** @brief Set the value of a variable, using the string representation of that value
705 * @param cfg config set to modify
706 * @param key name of the variable to modify
707 * @param value string representation of the value to set
709 * @return the first char after the parsed value in val
712 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
716 volatile xbt_cfgelm_t variable = NULL;
721 while (variable == NULL) {
722 variable = xbt_dict_get((xbt_dict_t) cfg, key);
723 if (variable->type == xbt_cfgelm_alias) {
724 const char *newname = (const char*)variable->content;
725 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
732 if (e.category == not_found_error) {
734 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
739 switch (variable->type) {
740 case xbt_cfgelm_string:
741 xbt_cfg_set_string(cfg, key, value); /* throws */
745 i = strtol(value, &ret, 0);
747 xbt_die("Value of option %s not valid. Should be an integer", key);
750 xbt_cfg_set_int(cfg, key, i); /* throws */
753 case xbt_cfgelm_double:
754 d = strtod(value, &ret);
756 xbt_die("Value of option %s not valid. Should be a double", key);
759 xbt_cfg_set_double(cfg, key, d); /* throws */
762 case xbt_cfgelm_boolean:
763 xbt_cfg_set_boolean(cfg, key, value); /* throws */
764 ret = (char *)value + strlen(value);
768 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
775 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
777 * This is useful to change the default value of a variable while allowing
778 * users to override it with command line arguments
780 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
782 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
784 if (variable->isdefault){
785 xbt_cfg_set_int(cfg, name, val);
786 variable->isdefault = 1;
790 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
794 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
796 * This is useful to change the default value of a variable while allowing
797 * users to override it with command line arguments
799 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
801 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
803 if (variable->isdefault) {
804 xbt_cfg_set_double(cfg, name, val);
805 variable->isdefault = 1;
809 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
813 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
815 * This is useful to change the default value of a variable while allowing
816 * users to override it with command line arguments
818 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
821 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
823 if (variable->isdefault){
824 xbt_cfg_set_string(cfg, name, val);
825 variable->isdefault = 1;
829 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
834 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
836 * This is useful to change the default value of a variable while allowing
837 * users to override it with command line arguments
839 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
841 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
843 if (variable->isdefault){
844 xbt_cfg_set_boolean(cfg, name, val);
845 variable->isdefault = 1;
849 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
853 /** @brief Set or add an integer value to \a name within \a cfg
855 * @param cfg the config set
856 * @param name the name of the variable
857 * @param val the value of the variable
859 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
862 XBT_VERB("Configuration setting: %s=%d", name, val);
863 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
865 if (variable->max == 1) {
866 xbt_dynar_set(variable->content, 0, &val);
869 && xbt_dynar_length(variable->content) ==
870 (unsigned long) variable->max)
871 THROWF(mismatch_error, 0,
872 "Cannot add value %d to the config element %s since it's already full (size=%d)",
873 val, name, variable->max);
875 xbt_dynar_push(variable->content, &val);
878 if (variable->cb_set)
879 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
880 variable->isdefault = 0;
883 /** @brief Set or add a double value to \a name within \a cfg
885 * @param cfg the config set
886 * @param name the name of the variable
887 * @param val the doule to set
890 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
893 XBT_VERB("Configuration setting: %s=%f", name, val);
894 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
896 if (variable->max == 1) {
897 xbt_dynar_set(variable->content, 0, &val);
900 && xbt_dynar_length(variable->content) == variable->max)
901 THROWF(mismatch_error, 0,
902 "Cannot add value %f to the config element %s since it's already full (size=%d)",
903 val, name, variable->max);
905 xbt_dynar_push(variable->content, &val);
908 if (variable->cb_set)
909 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
910 variable->isdefault = 0;
913 /** @brief Set or add a string value to \a name within \a cfg
915 * @param cfg the config set
916 * @param name the name of the variable
917 * @param val the value to be added
921 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
923 char *newval = xbt_strdup(val);
925 XBT_VERB("Configuration setting: %s=%s", name, val);
926 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
928 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
929 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
931 if (variable->max == 1) {
932 if (!xbt_dynar_is_empty(variable->content)) {
933 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
937 xbt_dynar_set(variable->content, 0, &newval);
940 && xbt_dynar_length(variable->content) == variable->max)
941 THROWF(mismatch_error, 0,
942 "Cannot add value %s to the config element %s since it's already full (size=%d)",
943 name, val, variable->max);
945 xbt_dynar_push(variable->content, &newval);
948 if (variable->cb_set)
949 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
950 variable->isdefault = 0;
953 /** @brief Set or add a boolean value to \a name within \a cfg
955 * @param cfg the config set
956 * @param name the name of the variable
957 * @param val the value of the variable
959 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
963 XBT_VERB("Configuration setting: %s=%s", name, val);
964 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
966 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
967 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
971 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
976 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
977 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
980 if (variable->max == 1) {
981 xbt_dynar_set(variable->content, 0, &bval);
984 && xbt_dynar_length(variable->content) ==
985 (unsigned long) variable->max)
986 THROWF(mismatch_error, 0,
987 "Cannot add value %s to the config element %s since it's already full (size=%d)",
988 val, name, variable->max);
990 xbt_dynar_push(variable->content, &bval);
993 if (variable->cb_set)
994 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
995 variable->isdefault = 0;
998 /* ---- [ Removing ] ---- */
1000 /** @brief Remove the provided \e val integer value from a variable
1002 * @param cfg the config set
1003 * @param name the name of the variable
1004 * @param val the value to be removed
1006 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1012 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1014 if (xbt_dynar_length(variable->content) == variable->min)
1015 THROWF(mismatch_error, 0,
1016 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1017 val, name, variable->min);
1019 xbt_dynar_foreach(variable->content, cpt, seen) {
1021 xbt_dynar_cursor_rm(variable->content, &cpt);
1026 THROWF(not_found_error, 0,
1027 "Can't remove the value %d of config element %s: value not found.",
1031 /** @brief Remove the provided \e val double value from a variable
1033 * @param cfg the config set
1034 * @param name the name of the variable
1035 * @param val the value to be removed
1038 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1043 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1045 if (xbt_dynar_length(variable->content) == variable->min)
1046 THROWF(mismatch_error, 0,
1047 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1048 val, name, variable->min);
1050 xbt_dynar_foreach(variable->content, cpt, seen) {
1052 xbt_dynar_cursor_rm(variable->content, &cpt);
1057 THROWF(not_found_error, 0,
1058 "Can't remove the value %f of config element %s: value not found.",
1062 /** @brief Remove the provided \e val string value from a variable
1064 * @param cfg the config set
1065 * @param name the name of the variable
1066 * @param val the value of the string which will be removed
1068 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1072 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1074 if (xbt_dynar_length(variable->content) == variable->min)
1075 THROWF(mismatch_error, 0,
1076 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1077 name, val, variable->min);
1079 xbt_dynar_foreach(variable->content, cpt, seen) {
1080 if (!strcpy(seen, val)) {
1081 xbt_dynar_cursor_rm(variable->content, &cpt);
1086 THROWF(not_found_error, 0,
1087 "Can't remove the value %s of config element %s: value not found.",
1091 /** @brief Remove the provided \e val boolean value from a variable
1093 * @param cfg the config set
1094 * @param name the name of the variable
1095 * @param val the value to be removed
1097 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1102 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1104 if (xbt_dynar_length(variable->content) == variable->min)
1105 THROWF(mismatch_error, 0,
1106 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1107 val, name, variable->min);
1109 xbt_dynar_foreach(variable->content, cpt, seen) {
1111 xbt_dynar_cursor_rm(variable->content, &cpt);
1116 THROWF(not_found_error, 0,
1117 "Can't remove the value %d of config element %s: value not found.",
1121 /** @brief Remove the \e pos th value from the provided variable */
1123 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1126 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1128 if (xbt_dynar_length(variable->content) == variable->min)
1129 THROWF(mismatch_error, 0,
1130 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1131 pos, name, variable->min);
1133 xbt_dynar_remove_at(variable->content, pos, NULL);
1136 /** @brief Remove all the values from a variable
1138 * @param cfg the config set
1139 * @param name the name of the variable
1142 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1144 xbt_cfgelm_t variable = NULL;
1148 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1151 if (e.category != not_found_error)
1155 THROWF(not_found_error, 0, "Can't empty '%s' since this option does not exist", name);
1159 xbt_dynar_reset(variable->content);
1162 * Say if the value is the default value
1164 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1166 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1167 return variable->isdefault;
1170 /*----[ Getting ]---------------------------------------------------------*/
1172 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1174 * @param cfg the config set
1175 * @param name the name of the variable
1177 * Returns the first value from the config set under the given name.
1178 * If there is more than one value, it will issue a warning. Consider using
1179 * xbt_cfg_get_dynar() instead.
1181 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1183 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1185 if (xbt_dynar_length(variable->content) > 1) {
1187 ("You asked for the first value of the config element '%s', but there is %lu values",
1188 name, xbt_dynar_length(variable->content));
1191 return xbt_dynar_get_as(variable->content, 0, int);
1194 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1196 * @param cfg the config set
1197 * @param name the name of the variable
1199 * Returns the first value from the config set under the given name.
1200 * If there is more than one value, it will issue a warning. Consider using
1201 * xbt_cfg_get_dynar() instead.
1204 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1206 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1208 if (xbt_dynar_length(variable->content) > 1) {
1210 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1211 name, xbt_dynar_length(variable->content));
1214 return xbt_dynar_get_as(variable->content, 0, double);
1217 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1219 * @param cfg the config set
1220 * @param name the name of the variable
1222 * Returns the first value from the config set under the given name.
1223 * If there is more than one value, it will issue a warning. Consider using
1224 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1226 * \warning the returned value is the actual content of the config set
1229 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1231 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1233 if (xbt_dynar_length(variable->content) > 1) {
1235 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1236 name, xbt_dynar_length(variable->content));
1237 } else if (xbt_dynar_is_empty(variable->content)) {
1241 return xbt_dynar_get_as(variable->content, 0, char *);
1244 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1246 * @param cfg the config set
1247 * @param name the name of the variable
1249 * Returns the first value from the config set under the given name.
1250 * If there is more than one value, it will issue a warning. Consider using
1251 * xbt_cfg_get_dynar() instead.
1253 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1255 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1257 if (xbt_dynar_length(variable->content) > 1) {
1259 ("You asked for the first value of the config element '%s', but there is %lu values",
1260 name, xbt_dynar_length(variable->content));
1263 return xbt_dynar_get_as(variable->content, 0, int);
1266 /** @brief Retrieve the dynar of all the values stored in a variable
1268 * @param cfg where to search in
1269 * @param name what to search for
1271 * Get the data stored in the config set.
1273 * \warning the returned value is the actual content of the config set
1275 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1277 xbt_cfgelm_t variable = NULL;
1281 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1284 if (e.category == not_found_error) {
1286 THROWF(not_found_error, 0,
1287 "No registered variable %s in this config set", name);
1292 return variable->content;
1296 /** @brief Retrieve one of the integer value of a variable */
1297 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1300 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1301 return xbt_dynar_get_as(variable->content, pos, int);
1304 /** @brief Retrieve one of the double value of a variable */
1305 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1308 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1309 return xbt_dynar_get_as(variable->content, pos, double);
1313 /** @brief Retrieve one of the string value of a variable */
1314 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1317 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1318 return xbt_dynar_get_as(variable->content, pos, char *);
1321 /** @brief Retrieve one of the boolean value of a variable */
1322 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1325 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1326 return xbt_dynar_get_as(variable->content, pos, int);
1334 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1336 XBT_TEST_SUITE("config", "Configuration support");
1338 static xbt_cfg_t make_set()
1340 xbt_cfg_t set = NULL;
1342 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1343 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1344 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1345 xbt_cfg_register_str(&set, "user:1_to_10_string");
1348 } /* end_of_make_set */
1350 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1352 xbt_cfg_t set = make_set();
1353 xbt_test_add("Alloc and free a config set");
1354 xbt_cfg_set_parse(set,
1355 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1360 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1362 xbt_cfg_t set = set = make_set();
1365 xbt_test_add("Having too few elements for speed");
1366 xbt_cfg_set_parse(set,
1367 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1372 if (e.category != mismatch_error ||
1373 strncmp(e.msg, "Config elem speed needs",
1374 strlen("Config elem speed needs")))
1375 xbt_test_fail("Got an exception. msg=%s", e.msg);
1383 xbt_test_add("Having too much values of 'speed'");
1385 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1387 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1390 if (e.category != mismatch_error ||
1391 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1392 strlen("Config elem speed needs")))
1393 xbt_test_fail("Got an exception. msg=%s", e.msg);
1402 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1405 xbt_test_add("Get a single value");
1407 /* get_single_value */
1409 xbt_cfg_t myset = make_set();
1411 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1412 ival = xbt_cfg_get_int(myset, "speed");
1414 xbt_test_fail("Speed value = %d, I expected 42", ival);
1415 xbt_cfg_free(&myset);
1418 xbt_test_add("Get multiple values");
1420 /* get_multiple_value */
1422 xbt_cfg_t myset = make_set();
1424 xbt_cfg_set_parse(myset,
1425 "peername:veloce user:foo\nuser:bar\tuser:toto");
1426 xbt_cfg_set_parse(myset, "speed:42");
1427 xbt_cfg_check(myset);
1428 dyn = xbt_cfg_get_dynar(myset, "user");
1430 if (xbt_dynar_length(dyn) != 3)
1431 xbt_test_fail("Dynar length = %lu, I expected 3",
1432 xbt_dynar_length(dyn));
1434 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1435 xbt_test_fail("Dynar[0] = %s, I expected foo",
1436 xbt_dynar_get_as(dyn, 0, char *));
1438 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1439 xbt_test_fail("Dynar[1] = %s, I expected bar",
1440 xbt_dynar_get_as(dyn, 1, char *));
1442 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1443 xbt_test_fail("Dynar[2] = %s, I expected toto",
1444 xbt_dynar_get_as(dyn, 2, char *));
1445 xbt_cfg_free(&myset);
1448 xbt_test_add("Access to a non-existant entry");
1450 /* non-existant_entry */
1451 xbt_cfg_t myset = make_set();
1455 xbt_cfg_set_parse(myset, "color:blue");
1458 if (e.category != not_found_error)
1459 xbt_test_exception(e);
1462 xbt_cfg_free(&myset);
1465 #endif /* SIMGRID_TEST */