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, 2005, 2006, 2007, 2008, 2009, 2010. 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. */
13 #include "xbt/sysdep.h"
16 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
33 /* Allowed type of the variable */
34 e_xbt_cfgelm_type_t type;
43 (cannot be an union because type peer uses both str and i) */
45 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
47 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
48 { "int", "double", "string", "boolean", "peer", "any" };
50 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
58 /* Internal stuff used in cache to free a variable */
59 static void xbt_cfgelm_free(void *data);
61 /* Retrieve the variable we'll modify */
62 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
63 e_xbt_cfgelm_type_t type);
65 /*----[ Memory management ]-----------------------------------------------*/
67 /** @brief Constructor
69 * Initialise a config set
73 xbt_cfg_t xbt_cfg_new(void)
75 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
78 /** \brief Copy an existing configuration set
80 * \arg whereto the config set to be created
81 * \arg tocopy the source data
83 * This only copy the registrations, not the actual content
86 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
88 xbt_dict_cursor_t cursor = NULL;
89 xbt_cfgelm_t variable = NULL;
92 XBT_DEBUG("Copy cfg set %p", tocopy);
94 xbt_assert(tocopy, "cannot copy NULL config");
96 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
97 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
98 variable->min, variable->max, variable->cb_set,
103 /** @brief Destructor */
104 void xbt_cfg_free(xbt_cfg_t * cfg)
106 XBT_DEBUG("Frees cfg set %p", cfg);
107 xbt_dict_free((xbt_dict_t *) cfg);
110 /** @brief Dump a config set for debuging purpose
112 * \arg name The name to give to this config set
113 * \arg indent what to write at the begining of each line (right number of spaces)
114 * \arg cfg the config set
116 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
118 xbt_dict_t dict = (xbt_dict_t) cfg;
119 xbt_dict_cursor_t cursor = NULL;
120 xbt_cfgelm_t variable = NULL;
130 printf("%s>> Dumping of the config set '%s':\n", indent, name);
131 xbt_dict_foreach(dict, cursor, key, variable) {
133 printf("%s %s:", indent, key);
135 size = xbt_dynar_length(variable->content);
137 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
138 variable->min, variable->max,
139 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
142 switch (variable->type) {
145 for (i = 0; i < size; i++) {
146 ival = xbt_dynar_get_as(variable->content, i, int);
147 printf("%s %d\n", indent, ival);
151 case xbt_cfgelm_double:
152 for (i = 0; i < size; i++) {
153 dval = xbt_dynar_get_as(variable->content, i, double);
154 printf("%s %f\n", indent, dval);
158 case xbt_cfgelm_string:
159 for (i = 0; i < size; i++) {
160 sval = xbt_dynar_get_as(variable->content, i, char *);
161 printf("%s %s\n", indent, sval);
165 case xbt_cfgelm_boolean:
166 for (i = 0; i < size; i++) {
167 ival = xbt_dynar_get_as(variable->content, i, int);
168 printf("%s %d\n", indent, ival);
172 case xbt_cfgelm_peer:
173 for (i = 0; i < size; i++) {
174 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
175 printf("%s %s:%d\n", indent, hval->name, hval->port);
180 printf("%s Invalid type!!\n", indent);
186 printf("%s<< End of the config set '%s'\n", indent, name);
189 xbt_dict_cursor_free(&cursor);
194 * free an config element
197 void xbt_cfgelm_free(void *data)
199 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
201 XBT_DEBUG("Frees cfgelm %p", c);
205 xbt_dynar_free(&(c->content));
209 /*----[ Registering stuff ]-----------------------------------------------*/
211 /** @brief Register an element within a config set
213 * @arg cfg the config set
214 * @arg type the type of the config element
215 * @arg min the minimum
216 * @arg max the maximum
220 xbt_cfg_register(xbt_cfg_t * cfg,
221 const char *name, const char *desc,
222 e_xbt_cfgelm_type_t type, void *default_value, int min,
223 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
228 *cfg = xbt_cfg_new();
229 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
230 "type of %s not valid (%d should be between %d and %d)",
231 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer);
232 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
235 XBT_WARN("Config elem %s registered twice.", name);
236 /* Will be removed by the insertion of the new one */
239 res = xbt_new(s_xbt_cfgelm_t, 1);
240 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
241 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
244 res->desc = xbt_strdup(desc);
248 res->cb_set = cb_set;
254 res->content = xbt_dynar_new(sizeof(int), NULL);
256 xbt_dynar_push(res->content, default_value);
259 case xbt_cfgelm_double:
260 res->content = xbt_dynar_new(sizeof(double), NULL);
262 xbt_dynar_push(res->content, default_value);
265 case xbt_cfgelm_string:
266 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
268 xbt_dynar_push(res->content, default_value);
271 case xbt_cfgelm_boolean:
272 res->content = xbt_dynar_new(sizeof(int), NULL);
274 xbt_dynar_push(res->content, default_value);
277 case xbt_cfgelm_peer:
278 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
280 xbt_dynar_push(res->content, default_value);
284 XBT_ERROR("%d is an invalide type code", (int)type);
287 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
290 /** @brief Unregister an element from a config set.
292 * @arg cfg the config set
293 * @arg name the name of the elem to be freed
295 * Note that it removes both the description and the actual content.
296 * Throws not_found when no such element exists.
299 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
301 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
302 xbt_dict_remove((xbt_dict_t) cfg, name);
306 * @brief Parse a string and register the stuff described.
308 * @arg cfg the config set
309 * @arg entry a string describing the element to register
311 * The string may consist in several variable descriptions separated by a space.
312 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
313 * with type being one of 'string','int', 'peer' or 'double'.
315 * FIXME: this does not allow to set the description
318 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
320 char *entrycpy = xbt_strdup(entry);
324 e_xbt_cfgelm_type_t type;
325 XBT_DEBUG("Register string '%s'", entry);
327 tok = strchr(entrycpy, ':');
328 xbt_assert(tok, "Invalid config element descriptor: %s%s",
329 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
332 min = strtol(tok, &tok, 10);
333 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
336 xbt_assert(strcmp(tok, "_to_"),
337 "Invalid config element descriptor : %s%s",
338 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
339 tok += strlen("_to_");
341 max = strtol(tok, &tok, 10);
342 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
345 xbt_assert(*tok == '_',
346 "Invalid config element descriptor: %s%s", entry,
347 "; Should be <name>:<min nb>_to_<max nb>_<type>");
350 for (type = (e_xbt_cfgelm_type_t)0;
351 type < xbt_cfgelm_type_count
352 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
353 xbt_assert(type < xbt_cfgelm_type_count,
354 "Invalid type in config element descriptor: %s%s", entry,
355 "; Should be one of 'string', 'int', 'peer' or 'double'.");
357 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
359 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
362 static int strcmp_voidp(const void *pa, const void *pb)
364 return strcmp(*(const char **)pa, *(const char **)pb);
367 /** @brief Displays the declared options and their description */
368 void xbt_cfg_help(xbt_cfg_t cfg)
370 xbt_dict_cursor_t dict_cursor;
371 unsigned int dynar_cursor;
372 xbt_cfgelm_t variable;
374 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
376 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
377 xbt_dynar_push(names, &name);
379 xbt_dynar_sort(names, strcmp_voidp);
381 xbt_dynar_foreach(names, dynar_cursor, name) {
384 variable = xbt_dict_get((xbt_dict_t )cfg, name);
386 printf(" %s: %s\n", name, variable->desc);
387 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
388 if (variable->min != 1 || variable->max != 1) {
389 if (variable->min == 0 && variable->max == 0)
390 printf("Arity: no bound; ");
392 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
394 size = xbt_dynar_length(variable->content);
395 printf("Current value%s: ", (size <= 1 ? "" : "s"));
398 printf(size == 0 ? "n/a\n" : "{ ");
399 for (i = 0; i < size; i++) {
400 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
402 switch (variable->type) {
404 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
407 case xbt_cfgelm_double:
408 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
411 case xbt_cfgelm_string:
412 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
415 case xbt_cfgelm_boolean: {
416 int b = xbt_dynar_get_as(variable->content, i, int);
417 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
418 : xbt_cfgelm_boolean_values[0].false_val;
419 if (b == 0 || b == 1)
420 printf("'%s'%s", bs, sep);
422 printf("'%s/%d'%s", bs, b, sep);
426 case xbt_cfgelm_peer: {
427 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
428 printf("%s:%d%s", hval->name, hval->port, sep);
433 printf("Invalid type!!%s", sep);
438 xbt_dynar_free(&names);
441 /** @brief Check that each variable have the right amount of values */
442 void xbt_cfg_check(xbt_cfg_t cfg)
444 xbt_dict_cursor_t cursor;
445 xbt_cfgelm_t variable;
449 xbt_assert(cfg, "NULL config set.");
450 XBT_DEBUG("Check cfg set %p", cfg);
452 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
453 size = xbt_dynar_length(variable->content);
454 if (variable->min > size) {
455 xbt_dict_cursor_free(&cursor);
456 THROWF(mismatch_error, 0,
457 "Config elem %s needs at least %d %s, but there is only %d values.",
458 name, variable->min, xbt_cfgelm_type_name[variable->type],
462 if (variable->max > 0 && variable->max < size) {
463 xbt_dict_cursor_free(&cursor);
464 THROWF(mismatch_error, 0,
465 "Config elem %s accepts at most %d %s, but there is %d values.",
466 name, variable->max, xbt_cfgelm_type_name[variable->type],
471 xbt_dict_cursor_free(&cursor);
474 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
476 e_xbt_cfgelm_type_t type)
478 xbt_cfgelm_t res = NULL;
480 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
483 THROWF(not_found_error, 0,
484 "No registered variable '%s' in this config set", name);
487 xbt_assert(type == xbt_cfgelm_any || res->type == type,
488 "You tried to access to the config element %s as an %s, but its type is %s.",
490 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
495 /** @brief Get the type of this variable in that configuration set
497 * \arg cfg the config set
498 * \arg name the name of the element
499 * \arg type the result
503 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
506 xbt_cfgelm_t variable = NULL;
508 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
510 THROWF(not_found_error, 0,
511 "Can't get the type of '%s' since this variable does not exist",
514 XBT_DEBUG("type in variable = %d", (int)variable->type);
516 return variable->type;
519 /*----[ Setting ]---------------------------------------------------------*/
520 /** @brief va_args version of xbt_cfg_set
522 * \arg cfg config set to fill
523 * \arg n variable name
524 * \arg pa variable value
526 * Add some values to the config set.
528 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
533 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
538 type = xbt_cfg_get_type(cfg, name);
541 if (e.category == not_found_error) {
543 THROWF(not_found_error, 0,
544 "Can't set the property '%s' since it's not registered",
551 case xbt_cfgelm_peer:
552 str = va_arg(pa, char *);
554 xbt_cfg_set_peer(cfg, name, str, i);
557 case xbt_cfgelm_string:
558 str = va_arg(pa, char *);
559 xbt_cfg_set_string(cfg, name, str);
564 xbt_cfg_set_int(cfg, name, i);
567 case xbt_cfgelm_double:
568 d = va_arg(pa, double);
569 xbt_cfg_set_double(cfg, name, d);
572 case xbt_cfgelm_boolean:
573 str = va_arg(pa, char *);
574 xbt_cfg_set_boolean(cfg, name, str);
578 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
582 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
584 * \arg cfg config set to fill
585 * \arg name variable name
586 * \arg varargs variable value
589 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
594 xbt_cfg_set_vargs(cfg, name, pa);
598 /** @brief Add values parsed from a string into a config set
600 * \arg cfg config set to fill
601 * \arg options a string containing the content to add to the config set. This
602 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
603 * like "[name]:[value]" where [name] is the name of an already registred
604 * variable, and [value] conforms to the data type under which this variable was
607 * @todo This is a crude manual parser, it should be a proper lexer.
610 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
612 char *optionlist_cpy;
613 char *option, *name, *val;
618 if (!options || !strlen(options)) { /* nothing to do */
621 optionlist_cpy = xbt_strdup(options);
623 XBT_DEBUG("List to parse and set:'%s'", options);
624 option = optionlist_cpy;
625 while (1) { /* breaks in the code */
631 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
632 name, len, (long) (option - name));
635 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
636 && *option != '\t' && *option != ',') {
637 XBT_DEBUG("Take %c.", *option);
640 if (option - name == len) {
641 XBT_DEBUG("Boundary=EOL");
642 option = NULL; /* don't do next iteration */
645 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
646 *option, len, (long) (option - name));
648 /* Pass the following blank chars */
650 while (option - name < (len - 1) &&
651 (*option == ' ' || *option == '\n' || *option == '\t')) {
652 /* fprintf(stderr,"Ignore a blank char.\n"); */
655 if (option - name == len - 1)
656 option = NULL; /* don't do next iteration */
658 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
660 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
665 val = strchr(name, ':');
667 /* don't free(optionlist_cpy) here, 'name' points inside it */
668 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
673 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
674 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
677 xbt_cfg_set_as_string(cfg,name,val);
679 free(optionlist_cpy);
683 free(optionlist_cpy);
686 /** @brief Set the value of a variable, using the string representation of that value
688 * @arg cfg config set to modify
689 * @arg key name of the variable to modify
690 * @arg value string representation of the value to set
692 * @return the first char after the parsed value in val
695 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
699 volatile xbt_cfgelm_t variable = NULL;
706 variable = xbt_dict_get((xbt_dict_t) cfg, key);
709 if (e.category == not_found_error) {
711 THROWF(not_found_error, 0,
712 "No registered variable corresponding to '%s'.", key);
717 switch (variable->type) {
718 case xbt_cfgelm_string:
719 xbt_cfg_set_string(cfg, key, value); /* throws */
723 i = strtol(value, &ret, 0);
725 xbt_die("Value of option %s not valid. Should be an integer", key);
728 xbt_cfg_set_int(cfg, key, i); /* throws */
731 case xbt_cfgelm_double:
732 d = strtod(value, &ret);
734 xbt_die("Value of option %s not valid. Should be a double", key);
737 xbt_cfg_set_double(cfg, key, d); /* throws */
740 case xbt_cfgelm_boolean:
741 xbt_cfg_set_boolean(cfg, key, value); /* throws */
744 case xbt_cfgelm_peer:
745 val = xbt_strdup(value);
747 val = strchr(val, ':');
749 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
753 i = strtol(val, &ret, 0);
755 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
758 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
763 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
770 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
772 * This is useful to change the default value of a variable while allowing
773 * users to override it with command line arguments
775 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
777 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
779 if (variable->isdefault){
780 xbt_cfg_set_int(cfg, name, val);
781 variable->isdefault = 1;
785 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
789 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
791 * This is useful to change the default value of a variable while allowing
792 * users to override it with command line arguments
794 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
796 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
798 if (variable->isdefault) {
799 xbt_cfg_set_double(cfg, name, val);
800 variable->isdefault = 1;
804 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
808 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
810 * This is useful to change the default value of a variable while allowing
811 * users to override it with command line arguments
813 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
816 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
818 if (variable->isdefault){
819 xbt_cfg_set_string(cfg, name, val);
820 variable->isdefault = 1;
824 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
829 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
831 * This is useful to change the default value of a variable while allowing
832 * users to override it with command line arguments
834 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
836 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
838 if (variable->isdefault){
839 xbt_cfg_set_boolean(cfg, name, val);
840 variable->isdefault = 1;
844 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
848 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
850 * This is useful to change the default value of a variable while allowing
851 * users to override it with command line arguments
853 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
854 const char *host, int port)
856 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
858 if (variable->isdefault){
859 xbt_cfg_set_peer(cfg, name, host, port);
860 variable->isdefault = 1;
864 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
868 /** @brief Set or add an integer value to \a name within \a cfg
870 * \arg cfg the config set
871 * \arg name the name of the variable
872 * \arg val the value of the variable
874 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
876 xbt_cfgelm_t variable;
878 XBT_VERB("Configuration setting: %s=%d", name, val);
879 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
881 if (variable->max == 1) {
882 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
883 variable->cb_rm(name, 0);
885 xbt_dynar_set(variable->content, 0, &val);
888 && xbt_dynar_length(variable->content) ==
889 (unsigned long) variable->max)
890 THROWF(mismatch_error, 0,
891 "Cannot add value %d to the config element %s since it's already full (size=%d)",
892 val, name, variable->max);
894 xbt_dynar_push(variable->content, &val);
897 if (variable->cb_set)
898 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
899 variable->isdefault = 0;
902 /** @brief Set or add a double value to \a name within \a cfg
904 * \arg cfg the config set
905 * \arg name the name of the variable
906 * \arg val the doule to set
909 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
911 xbt_cfgelm_t variable;
913 XBT_VERB("Configuration setting: %s=%f", name, val);
914 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
916 if (variable->max == 1) {
917 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
918 variable->cb_rm(name, 0);
920 xbt_dynar_set(variable->content, 0, &val);
923 && xbt_dynar_length(variable->content) == variable->max)
924 THROWF(mismatch_error, 0,
925 "Cannot add value %f to the config element %s since it's already full (size=%d)",
926 val, name, variable->max);
928 xbt_dynar_push(variable->content, &val);
931 if (variable->cb_set)
932 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
933 variable->isdefault = 0;
936 /** @brief Set or add a string value to \a name within \a cfg
938 * \arg cfg the config set
939 * \arg name the name of the variable
940 * \arg val the value to be added
944 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
946 xbt_cfgelm_t variable;
947 char *newval = xbt_strdup(val);
949 XBT_VERB("Configuration setting: %s=%s", name, val);
950 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
951 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
952 variable->min, variable->max,
953 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
955 if (variable->max == 1) {
956 if (!xbt_dynar_is_empty(variable->content)) {
958 variable->cb_rm(name, 0);
959 else if (variable->type == xbt_cfgelm_string) {
960 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
965 xbt_dynar_set(variable->content, 0, &newval);
968 && xbt_dynar_length(variable->content) == variable->max)
969 THROWF(mismatch_error, 0,
970 "Cannot add value %s to the config element %s since it's already full (size=%d)",
971 name, val, variable->max);
973 xbt_dynar_push(variable->content, &newval);
976 if (variable->cb_set)
977 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
978 variable->isdefault = 0;
981 /** @brief Set or add a boolean value to \a name within \a cfg
983 * \arg cfg the config set
984 * \arg name the name of the variable
985 * \arg val the value of the variable
987 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
989 xbt_cfgelm_t variable;
992 XBT_VERB("Configuration setting: %s=%s", name, val);
993 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
995 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
996 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
1000 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
1005 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
1006 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
1009 if (variable->max == 1) {
1010 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1011 variable->cb_rm(name, 0);
1013 xbt_dynar_set(variable->content, 0, &bval);
1016 && xbt_dynar_length(variable->content) ==
1017 (unsigned long) variable->max)
1018 THROWF(mismatch_error, 0,
1019 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1020 val, name, variable->max);
1022 xbt_dynar_push(variable->content, &bval);
1025 if (variable->cb_set)
1026 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1027 variable->isdefault = 0;
1030 /** @brief Set or add an peer value to \a name within \a cfg
1032 * \arg cfg the config set
1033 * \arg name the name of the variable
1034 * \arg peer the peer
1035 * \arg port the port number
1037 * \e peer values are composed of a string (peername) and an integer (port)
1041 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1044 xbt_cfgelm_t variable;
1045 xbt_peer_t val = xbt_peer_new(peer, port);
1047 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1049 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1051 if (variable->max == 1) {
1052 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1053 variable->cb_rm(name, 0);
1055 xbt_dynar_set(variable->content, 0, &val);
1058 && xbt_dynar_length(variable->content) == variable->max)
1059 THROWF(mismatch_error, 0,
1060 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1061 peer, port, name, variable->max);
1063 xbt_dynar_push(variable->content, &val);
1066 if (variable->cb_set)
1067 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1068 variable->isdefault = 0;
1071 /* ---- [ Removing ] ---- */
1073 /** @brief Remove the provided \e val integer value from a variable
1075 * \arg cfg the config set
1076 * \arg name the name of the variable
1077 * \arg val the value to be removed
1079 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1082 xbt_cfgelm_t variable;
1086 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1088 if (xbt_dynar_length(variable->content) == variable->min)
1089 THROWF(mismatch_error, 0,
1090 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1091 val, name, variable->min);
1093 xbt_dynar_foreach(variable->content, cpt, seen) {
1095 if (variable->cb_rm)
1096 variable->cb_rm(name, cpt);
1097 xbt_dynar_cursor_rm(variable->content, &cpt);
1102 THROWF(not_found_error, 0,
1103 "Can't remove the value %d of config element %s: value not found.",
1107 /** @brief Remove the provided \e val double value from a variable
1109 * \arg cfg the config set
1110 * \arg name the name of the variable
1111 * \arg val the value to be removed
1114 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1116 xbt_cfgelm_t variable;
1120 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1122 if (xbt_dynar_length(variable->content) == variable->min)
1123 THROWF(mismatch_error, 0,
1124 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1125 val, name, variable->min);
1127 xbt_dynar_foreach(variable->content, cpt, seen) {
1129 xbt_dynar_cursor_rm(variable->content, &cpt);
1130 if (variable->cb_rm)
1131 variable->cb_rm(name, cpt);
1136 THROWF(not_found_error, 0,
1137 "Can't remove the value %f of config element %s: value not found.",
1141 /** @brief Remove the provided \e val string value from a variable
1143 * \arg cfg the config set
1144 * \arg name the name of the variable
1145 * \arg val the value of the string which will be removed
1147 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1149 xbt_cfgelm_t variable;
1153 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1155 if (xbt_dynar_length(variable->content) == variable->min)
1156 THROWF(mismatch_error, 0,
1157 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1158 name, val, variable->min);
1160 xbt_dynar_foreach(variable->content, cpt, seen) {
1161 if (!strcpy(seen, val)) {
1162 if (variable->cb_rm)
1163 variable->cb_rm(name, cpt);
1164 xbt_dynar_cursor_rm(variable->content, &cpt);
1169 THROWF(not_found_error, 0,
1170 "Can't remove the value %s of config element %s: value not found.",
1174 /** @brief Remove the provided \e val boolean value from a variable
1176 * \arg cfg the config set
1177 * \arg name the name of the variable
1178 * \arg val the value to be removed
1180 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1183 xbt_cfgelm_t variable;
1187 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1189 if (xbt_dynar_length(variable->content) == variable->min)
1190 THROWF(mismatch_error, 0,
1191 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1192 val, name, variable->min);
1194 xbt_dynar_foreach(variable->content, cpt, seen) {
1196 if (variable->cb_rm)
1197 variable->cb_rm(name, cpt);
1198 xbt_dynar_cursor_rm(variable->content, &cpt);
1203 THROWF(not_found_error, 0,
1204 "Can't remove the value %d of config element %s: value not found.",
1208 /** @brief Remove the provided \e val peer value from a variable
1210 * \arg cfg the config set
1211 * \arg name the name of the variable
1212 * \arg peer the peername
1213 * \arg port the port number
1217 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1220 xbt_cfgelm_t variable;
1224 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1226 if (xbt_dynar_length(variable->content) == variable->min)
1227 THROWF(mismatch_error, 0,
1228 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1229 peer, port, name, variable->min);
1231 xbt_dynar_foreach(variable->content, cpt, seen) {
1232 if (!strcpy(seen->name, peer) && seen->port == port) {
1233 if (variable->cb_rm)
1234 variable->cb_rm(name, cpt);
1235 xbt_dynar_cursor_rm(variable->content, &cpt);
1240 THROWF(not_found_error, 0,
1241 "Can't remove the value %s:%d of config element %s: value not found.",
1245 /** @brief Remove the \e pos th value from the provided variable */
1247 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1250 xbt_cfgelm_t variable;
1252 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1254 if (xbt_dynar_length(variable->content) == variable->min)
1255 THROWF(mismatch_error, 0,
1256 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1257 pos, name, variable->min);
1259 if (variable->cb_rm)
1260 variable->cb_rm(name, pos);
1261 xbt_dynar_remove_at(variable->content, pos, NULL);
1264 /** @brief Remove all the values from a variable
1266 * \arg cfg the config set
1267 * \arg name the name of the variable
1270 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1272 xbt_cfgelm_t variable = NULL;
1276 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1279 if (e.category != not_found_error)
1283 THROWF(not_found_error, 0,
1284 "Can't empty '%s' since this config element does not exist",
1289 if (variable->cb_rm) {
1292 xbt_dynar_foreach(variable->content, cpt, ignored) {
1293 variable->cb_rm(name, cpt);
1296 xbt_dynar_reset(variable->content);
1300 * Say if the value is the default value
1302 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1304 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1305 return variable->isdefault;
1308 /*----[ Getting ]---------------------------------------------------------*/
1310 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1312 * \arg cfg the config set
1313 * \arg name the name of the variable
1314 * \arg val the wanted value
1316 * Returns the first value from the config set under the given name.
1317 * If there is more than one value, it will issue a warning. Consider using
1318 * xbt_cfg_get_dynar() instead.
1320 * \warning the returned value is the actual content of the config set
1322 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1324 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1326 if (xbt_dynar_length(variable->content) > 1) {
1328 ("You asked for the first value of the config element '%s', but there is %lu values",
1329 name, xbt_dynar_length(variable->content));
1332 return xbt_dynar_get_as(variable->content, 0, int);
1335 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1337 * \arg cfg the config set
1338 * \arg name the name of the variable
1339 * \arg val the wanted value
1341 * Returns the first value from the config set under the given name.
1342 * If there is more than one value, it will issue a warning. Consider using
1343 * xbt_cfg_get_dynar() instead.
1345 * \warning the returned value is the actual content of the config set
1348 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1350 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1352 if (xbt_dynar_length(variable->content) > 1) {
1354 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1355 name, xbt_dynar_length(variable->content));
1358 return xbt_dynar_get_as(variable->content, 0, double);
1361 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1363 * \arg th the config set
1364 * \arg name the name of the variable
1365 * \arg val the wanted value
1367 * Returns the first value from the config set under the given name.
1368 * If there is more than one value, it will issue a warning. Consider using
1369 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1371 * \warning the returned value is the actual content of the config set
1374 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1376 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1378 if (xbt_dynar_length(variable->content) > 1) {
1380 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1381 name, xbt_dynar_length(variable->content));
1382 } else if (xbt_dynar_is_empty(variable->content)) {
1386 return xbt_dynar_get_as(variable->content, 0, char *);
1389 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1391 * \arg cfg the config set
1392 * \arg name the name of the variable
1393 * \arg val the wanted value
1395 * Returns the first value from the config set under the given name.
1396 * If there is more than one value, it will issue a warning. Consider using
1397 * xbt_cfg_get_dynar() instead.
1399 * \warning the returned value is the actual content of the config set
1401 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1403 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1405 if (xbt_dynar_length(variable->content) > 1) {
1407 ("You asked for the first value of the config element '%s', but there is %lu values",
1408 name, xbt_dynar_length(variable->content));
1411 return xbt_dynar_get_as(variable->content, 0, int);
1414 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1416 * \arg cfg the config set
1417 * \arg name the name of the variable
1418 * \arg peer the peer
1419 * \arg port the port number
1421 * Returns the first value from the config set under the given name.
1422 * If there is more than one value, it will issue a warning. Consider using
1423 * xbt_cfg_get_dynar() instead.
1425 * \warning the returned value is the actual content of the config set
1428 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1431 xbt_cfgelm_t variable;
1434 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1436 if (xbt_dynar_length(variable->content) > 1) {
1438 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1439 name, xbt_dynar_length(variable->content));
1442 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1447 /** @brief Retrieve the dynar of all the values stored in a variable
1449 * \arg cfg where to search in
1450 * \arg name what to search for
1453 * Get the data stored in the config set.
1455 * \warning the returned value is the actual content of the config set
1457 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1459 xbt_cfgelm_t variable = NULL;
1463 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1466 if (e.category == not_found_error) {
1468 THROWF(not_found_error, 0,
1469 "No registered variable %s in this config set", name);
1474 return variable->content;
1478 /** @brief Retrieve one of the integer value of a variable */
1479 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1482 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1483 return xbt_dynar_get_as(variable->content, pos, int);
1486 /** @brief Retrieve one of the double value of a variable */
1487 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1490 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1491 return xbt_dynar_get_as(variable->content, pos, double);
1495 /** @brief Retrieve one of the string value of a variable */
1496 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1499 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1500 return xbt_dynar_get_as(variable->content, pos, char *);
1503 /** @brief Retrieve one of the boolean value of a variable */
1504 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1507 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1508 return xbt_dynar_get_as(variable->content, pos, int);
1511 /** @brief Retrieve one of the peer value of a variable */
1513 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1514 char **peer, int *port)
1517 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1518 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1529 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1531 XBT_TEST_SUITE("config", "Configuration support");
1533 static xbt_cfg_t make_set()
1535 xbt_cfg_t set = NULL;
1537 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1538 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1539 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1540 xbt_cfg_register_str(&set, "user:1_to_10_string");
1543 } /* end_of_make_set */
1545 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1547 xbt_cfg_t set = make_set();
1548 xbt_test_add("Alloc and free a config set");
1549 xbt_cfg_set_parse(set,
1550 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1555 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1557 xbt_cfg_t set = set = make_set();
1560 xbt_test_add("Having too few elements for speed");
1561 xbt_cfg_set_parse(set,
1562 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1567 if (e.category != mismatch_error ||
1568 strncmp(e.msg, "Config elem speed needs",
1569 strlen("Config elem speed needs")))
1570 xbt_test_fail("Got an exception. msg=%s", e.msg);
1578 xbt_test_add("Having too much values of 'speed'");
1580 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1582 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1585 if (e.category != mismatch_error ||
1586 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1587 strlen("Config elem speed needs")))
1588 xbt_test_fail("Got an exception. msg=%s", e.msg);
1597 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1600 xbt_test_add("Get a single value");
1602 /* get_single_value */
1604 xbt_cfg_t myset = make_set();
1606 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1607 ival = xbt_cfg_get_int(myset, "speed");
1609 xbt_test_fail("Speed value = %d, I expected 42", ival);
1610 xbt_cfg_free(&myset);
1613 xbt_test_add("Get multiple values");
1615 /* get_multiple_value */
1617 xbt_cfg_t myset = make_set();
1619 xbt_cfg_set_parse(myset,
1620 "peername:veloce user:foo\nuser:bar\tuser:toto");
1621 xbt_cfg_set_parse(myset, "speed:42");
1622 xbt_cfg_check(myset);
1623 dyn = xbt_cfg_get_dynar(myset, "user");
1625 if (xbt_dynar_length(dyn) != 3)
1626 xbt_test_fail("Dynar length = %lu, I expected 3",
1627 xbt_dynar_length(dyn));
1629 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1630 xbt_test_fail("Dynar[0] = %s, I expected foo",
1631 xbt_dynar_get_as(dyn, 0, char *));
1633 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1634 xbt_test_fail("Dynar[1] = %s, I expected bar",
1635 xbt_dynar_get_as(dyn, 1, char *));
1637 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1638 xbt_test_fail("Dynar[2] = %s, I expected toto",
1639 xbt_dynar_get_as(dyn, 2, char *));
1640 xbt_cfg_free(&myset);
1643 xbt_test_add("Access to a non-existant entry");
1645 /* non-existant_entry */
1646 xbt_cfg_t myset = make_set();
1650 xbt_cfg_set_parse(myset, "color:blue");
1653 if (e.category != not_found_error)
1654 xbt_test_exception(e);
1657 xbt_cfg_free(&myset);
1660 #endif /* SIMGRID_TEST */