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 printf("'%d'%s", xbt_dynar_get_as(variable->content, i, int), sep);
419 case xbt_cfgelm_peer: {
420 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
421 printf("%s:%d%s", hval->name, hval->port, sep);
426 printf("Invalid type!!%s", sep);
431 xbt_dynar_free(&names);
434 /** @brief Check that each variable have the right amount of values */
435 void xbt_cfg_check(xbt_cfg_t cfg)
437 xbt_dict_cursor_t cursor;
438 xbt_cfgelm_t variable;
442 xbt_assert(cfg, "NULL config set.");
443 XBT_DEBUG("Check cfg set %p", cfg);
445 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
446 size = xbt_dynar_length(variable->content);
447 if (variable->min > size) {
448 xbt_dict_cursor_free(&cursor);
449 THROWF(mismatch_error, 0,
450 "Config elem %s needs at least %d %s, but there is only %d values.",
451 name, variable->min, xbt_cfgelm_type_name[variable->type],
455 if (variable->max > 0 && variable->max < size) {
456 xbt_dict_cursor_free(&cursor);
457 THROWF(mismatch_error, 0,
458 "Config elem %s accepts at most %d %s, but there is %d values.",
459 name, variable->max, xbt_cfgelm_type_name[variable->type],
464 xbt_dict_cursor_free(&cursor);
467 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
469 e_xbt_cfgelm_type_t type)
471 xbt_cfgelm_t res = NULL;
473 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
476 THROWF(not_found_error, 0,
477 "No registered variable '%s' in this config set", name);
480 xbt_assert(type == xbt_cfgelm_any || res->type == type,
481 "You tried to access to the config element %s as an %s, but its type is %s.",
483 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
488 /** @brief Get the type of this variable in that configuration set
490 * \arg cfg the config set
491 * \arg name the name of the element
492 * \arg type the result
496 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
499 xbt_cfgelm_t variable = NULL;
501 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
503 THROWF(not_found_error, 0,
504 "Can't get the type of '%s' since this variable does not exist",
507 XBT_DEBUG("type in variable = %d", (int)variable->type);
509 return variable->type;
512 /*----[ Setting ]---------------------------------------------------------*/
513 /** @brief va_args version of xbt_cfg_set
515 * \arg cfg config set to fill
516 * \arg n variable name
517 * \arg pa variable value
519 * Add some values to the config set.
521 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
526 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
531 type = xbt_cfg_get_type(cfg, name);
534 if (e.category == not_found_error) {
536 THROWF(not_found_error, 0,
537 "Can't set the property '%s' since it's not registered",
544 case xbt_cfgelm_peer:
545 str = va_arg(pa, char *);
547 xbt_cfg_set_peer(cfg, name, str, i);
550 case xbt_cfgelm_string:
551 str = va_arg(pa, char *);
552 xbt_cfg_set_string(cfg, name, str);
557 xbt_cfg_set_int(cfg, name, i);
560 case xbt_cfgelm_double:
561 d = va_arg(pa, double);
562 xbt_cfg_set_double(cfg, name, d);
565 case xbt_cfgelm_boolean:
566 str = va_arg(pa, char *);
567 xbt_cfg_set_boolean(cfg, name, str);
571 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
575 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
577 * \arg cfg config set to fill
578 * \arg name variable name
579 * \arg varargs variable value
582 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
587 xbt_cfg_set_vargs(cfg, name, pa);
591 /** @brief Add values parsed from a string into a config set
593 * \arg cfg config set to fill
594 * \arg options a string containing the content to add to the config set. This
595 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
596 * like "[name]:[value]" where [name] is the name of an already registred
597 * variable, and [value] conforms to the data type under which this variable was
600 * @todo This is a crude manual parser, it should be a proper lexer.
603 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
605 char *optionlist_cpy;
606 char *option, *name, *val;
611 if (!options || !strlen(options)) { /* nothing to do */
614 optionlist_cpy = xbt_strdup(options);
616 XBT_DEBUG("List to parse and set:'%s'", options);
617 option = optionlist_cpy;
618 while (1) { /* breaks in the code */
624 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
625 name, len, (long) (option - name));
628 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
629 && *option != '\t' && *option != ',') {
630 XBT_DEBUG("Take %c.", *option);
633 if (option - name == len) {
634 XBT_DEBUG("Boundary=EOL");
635 option = NULL; /* don't do next iteration */
638 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
639 *option, len, (long) (option - name));
641 /* Pass the following blank chars */
643 while (option - name < (len - 1) &&
644 (*option == ' ' || *option == '\n' || *option == '\t')) {
645 /* fprintf(stderr,"Ignore a blank char.\n"); */
648 if (option - name == len - 1)
649 option = NULL; /* don't do next iteration */
651 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
653 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
658 val = strchr(name, ':');
660 free(optionlist_cpy);
661 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
666 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
667 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
670 xbt_cfg_set_as_string(cfg,name,val);
672 free(optionlist_cpy);
676 free(optionlist_cpy);
679 /** @brief Set the value of a variable, using the string representation of that value
681 * @arg cfg config set to modify
682 * @arg key name of the variable to modify
683 * @arg value string representation of the value to set
685 * @return the first char after the parsed value in val
688 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
692 volatile xbt_cfgelm_t variable = NULL;
699 variable = xbt_dict_get((xbt_dict_t) cfg, key);
702 if (e.category == not_found_error) {
704 THROWF(not_found_error, 0,
705 "No registered variable corresponding to '%s'.", key);
710 switch (variable->type) {
711 case xbt_cfgelm_string:
712 xbt_cfg_set_string(cfg, key, value); /* throws */
716 i = strtol(value, &ret, 0);
718 xbt_die("Value of option %s not valid. Should be an integer", key);
721 xbt_cfg_set_int(cfg, key, i); /* throws */
724 case xbt_cfgelm_double:
725 d = strtod(value, &ret);
727 xbt_die("Value of option %s not valid. Should be a double", key);
730 xbt_cfg_set_double(cfg, key, d); /* throws */
733 case xbt_cfgelm_boolean:
734 xbt_cfg_set_boolean(cfg, key, value); /* throws */
737 case xbt_cfgelm_peer:
738 val = xbt_strdup(value);
740 val = strchr(val, ':');
742 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
746 i = strtol(val, &ret, 0);
748 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
751 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
756 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
763 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
765 * This is useful to change the default value of a variable while allowing
766 * users to override it with command line arguments
768 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
770 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
772 if (variable->isdefault){
773 xbt_cfg_set_int(cfg, name, val);
774 variable->isdefault = 1;
778 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
782 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
784 * This is useful to change the default value of a variable while allowing
785 * users to override it with command line arguments
787 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
789 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
791 if (variable->isdefault) {
792 xbt_cfg_set_double(cfg, name, val);
793 variable->isdefault = 1;
797 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
801 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
803 * This is useful to change the default value of a variable while allowing
804 * users to override it with command line arguments
806 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
809 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
811 if (variable->isdefault){
812 xbt_cfg_set_string(cfg, name, val);
813 variable->isdefault = 1;
817 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
822 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
824 * This is useful to change the default value of a variable while allowing
825 * users to override it with command line arguments
827 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
829 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
831 if (variable->isdefault){
832 xbt_cfg_set_boolean(cfg, name, val);
833 variable->isdefault = 1;
837 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
841 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
843 * This is useful to change the default value of a variable while allowing
844 * users to override it with command line arguments
846 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
847 const char *host, int port)
849 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
851 if (variable->isdefault){
852 xbt_cfg_set_peer(cfg, name, host, port);
853 variable->isdefault = 1;
857 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
861 /** @brief Set or add an integer value to \a name within \a cfg
863 * \arg cfg the config set
864 * \arg name the name of the variable
865 * \arg val the value of the variable
867 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
869 xbt_cfgelm_t variable;
871 XBT_VERB("Configuration setting: %s=%d", name, val);
872 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
874 if (variable->max == 1) {
875 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
876 variable->cb_rm(name, 0);
878 xbt_dynar_set(variable->content, 0, &val);
881 && xbt_dynar_length(variable->content) ==
882 (unsigned long) variable->max)
883 THROWF(mismatch_error, 0,
884 "Cannot add value %d to the config element %s since it's already full (size=%d)",
885 val, name, variable->max);
887 xbt_dynar_push(variable->content, &val);
890 if (variable->cb_set)
891 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
892 variable->isdefault = 0;
895 /** @brief Set or add a double value to \a name within \a cfg
897 * \arg cfg the config set
898 * \arg name the name of the variable
899 * \arg val the doule to set
902 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
904 xbt_cfgelm_t variable;
906 XBT_VERB("Configuration setting: %s=%f", name, val);
907 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
909 if (variable->max == 1) {
910 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
911 variable->cb_rm(name, 0);
913 xbt_dynar_set(variable->content, 0, &val);
916 && xbt_dynar_length(variable->content) == variable->max)
917 THROWF(mismatch_error, 0,
918 "Cannot add value %f to the config element %s since it's already full (size=%d)",
919 val, name, variable->max);
921 xbt_dynar_push(variable->content, &val);
924 if (variable->cb_set)
925 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
926 variable->isdefault = 0;
929 /** @brief Set or add a string value to \a name within \a cfg
931 * \arg cfg the config set
932 * \arg name the name of the variable
933 * \arg val the value to be added
937 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
939 xbt_cfgelm_t variable;
940 char *newval = xbt_strdup(val);
942 XBT_VERB("Configuration setting: %s=%s", name, val);
943 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
944 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
945 variable->min, variable->max,
946 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
948 if (variable->max == 1) {
949 if (!xbt_dynar_is_empty(variable->content)) {
951 variable->cb_rm(name, 0);
952 else if (variable->type == xbt_cfgelm_string) {
953 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
958 xbt_dynar_set(variable->content, 0, &newval);
961 && xbt_dynar_length(variable->content) == variable->max)
962 THROWF(mismatch_error, 0,
963 "Cannot add value %s to the config element %s since it's already full (size=%d)",
964 name, val, variable->max);
966 xbt_dynar_push(variable->content, &newval);
969 if (variable->cb_set)
970 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
971 variable->isdefault = 0;
974 /** @brief Set or add a boolean value to \a name within \a cfg
976 * \arg cfg the config set
977 * \arg name the name of the variable
978 * \arg val the value of the variable
980 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
982 xbt_cfgelm_t variable;
985 XBT_VERB("Configuration setting: %s=%s", name, val);
986 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
988 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
989 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
993 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
998 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
999 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
1002 if (variable->max == 1) {
1003 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1004 variable->cb_rm(name, 0);
1006 xbt_dynar_set(variable->content, 0, &bval);
1009 && xbt_dynar_length(variable->content) ==
1010 (unsigned long) variable->max)
1011 THROWF(mismatch_error, 0,
1012 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1013 val, name, variable->max);
1015 xbt_dynar_push(variable->content, &bval);
1018 if (variable->cb_set)
1019 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1020 variable->isdefault = 0;
1023 /** @brief Set or add an peer value to \a name within \a cfg
1025 * \arg cfg the config set
1026 * \arg name the name of the variable
1027 * \arg peer the peer
1028 * \arg port the port number
1030 * \e peer values are composed of a string (peername) and an integer (port)
1034 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1037 xbt_cfgelm_t variable;
1038 xbt_peer_t val = xbt_peer_new(peer, port);
1040 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1042 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1044 if (variable->max == 1) {
1045 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1046 variable->cb_rm(name, 0);
1048 xbt_dynar_set(variable->content, 0, &val);
1051 && xbt_dynar_length(variable->content) == variable->max)
1052 THROWF(mismatch_error, 0,
1053 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1054 peer, port, name, variable->max);
1056 xbt_dynar_push(variable->content, &val);
1059 if (variable->cb_set)
1060 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1061 variable->isdefault = 0;
1064 /* ---- [ Removing ] ---- */
1066 /** @brief Remove the provided \e val integer value from a variable
1068 * \arg cfg the config set
1069 * \arg name the name of the variable
1070 * \arg val the value to be removed
1072 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1075 xbt_cfgelm_t variable;
1079 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1081 if (xbt_dynar_length(variable->content) == variable->min)
1082 THROWF(mismatch_error, 0,
1083 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1084 val, name, variable->min);
1086 xbt_dynar_foreach(variable->content, cpt, seen) {
1088 if (variable->cb_rm)
1089 variable->cb_rm(name, cpt);
1090 xbt_dynar_cursor_rm(variable->content, &cpt);
1095 THROWF(not_found_error, 0,
1096 "Can't remove the value %d of config element %s: value not found.",
1100 /** @brief Remove the provided \e val double value from a variable
1102 * \arg cfg the config set
1103 * \arg name the name of the variable
1104 * \arg val the value to be removed
1107 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1109 xbt_cfgelm_t variable;
1113 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1115 if (xbt_dynar_length(variable->content) == variable->min)
1116 THROWF(mismatch_error, 0,
1117 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1118 val, name, variable->min);
1120 xbt_dynar_foreach(variable->content, cpt, seen) {
1122 xbt_dynar_cursor_rm(variable->content, &cpt);
1123 if (variable->cb_rm)
1124 variable->cb_rm(name, cpt);
1129 THROWF(not_found_error, 0,
1130 "Can't remove the value %f of config element %s: value not found.",
1134 /** @brief Remove the provided \e val string value from a variable
1136 * \arg cfg the config set
1137 * \arg name the name of the variable
1138 * \arg val the value of the string which will be removed
1140 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1142 xbt_cfgelm_t variable;
1146 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1148 if (xbt_dynar_length(variable->content) == variable->min)
1149 THROWF(mismatch_error, 0,
1150 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1151 name, val, variable->min);
1153 xbt_dynar_foreach(variable->content, cpt, seen) {
1154 if (!strcpy(seen, val)) {
1155 if (variable->cb_rm)
1156 variable->cb_rm(name, cpt);
1157 xbt_dynar_cursor_rm(variable->content, &cpt);
1162 THROWF(not_found_error, 0,
1163 "Can't remove the value %s of config element %s: value not found.",
1167 /** @brief Remove the provided \e val boolean value from a variable
1169 * \arg cfg the config set
1170 * \arg name the name of the variable
1171 * \arg val the value to be removed
1173 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1176 xbt_cfgelm_t variable;
1180 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1182 if (xbt_dynar_length(variable->content) == variable->min)
1183 THROWF(mismatch_error, 0,
1184 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1185 val, name, variable->min);
1187 xbt_dynar_foreach(variable->content, cpt, seen) {
1189 if (variable->cb_rm)
1190 variable->cb_rm(name, cpt);
1191 xbt_dynar_cursor_rm(variable->content, &cpt);
1196 THROWF(not_found_error, 0,
1197 "Can't remove the value %d of config element %s: value not found.",
1201 /** @brief Remove the provided \e val peer value from a variable
1203 * \arg cfg the config set
1204 * \arg name the name of the variable
1205 * \arg peer the peername
1206 * \arg port the port number
1210 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1213 xbt_cfgelm_t variable;
1217 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1219 if (xbt_dynar_length(variable->content) == variable->min)
1220 THROWF(mismatch_error, 0,
1221 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1222 peer, port, name, variable->min);
1224 xbt_dynar_foreach(variable->content, cpt, seen) {
1225 if (!strcpy(seen->name, peer) && seen->port == port) {
1226 if (variable->cb_rm)
1227 variable->cb_rm(name, cpt);
1228 xbt_dynar_cursor_rm(variable->content, &cpt);
1233 THROWF(not_found_error, 0,
1234 "Can't remove the value %s:%d of config element %s: value not found.",
1238 /** @brief Remove the \e pos th value from the provided variable */
1240 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1243 xbt_cfgelm_t variable;
1245 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1247 if (xbt_dynar_length(variable->content) == variable->min)
1248 THROWF(mismatch_error, 0,
1249 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1250 pos, name, variable->min);
1252 if (variable->cb_rm)
1253 variable->cb_rm(name, pos);
1254 xbt_dynar_remove_at(variable->content, pos, NULL);
1257 /** @brief Remove all the values from a variable
1259 * \arg cfg the config set
1260 * \arg name the name of the variable
1263 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1265 xbt_cfgelm_t variable = NULL;
1269 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1272 if (e.category != not_found_error)
1276 THROWF(not_found_error, 0,
1277 "Can't empty '%s' since this config element does not exist",
1282 if (variable->cb_rm) {
1285 xbt_dynar_foreach(variable->content, cpt, ignored) {
1286 variable->cb_rm(name, cpt);
1289 xbt_dynar_reset(variable->content);
1293 * Say if the value is the default value
1295 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1297 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1298 return variable->isdefault;
1301 /*----[ Getting ]---------------------------------------------------------*/
1303 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1305 * \arg cfg the config set
1306 * \arg name the name of the variable
1307 * \arg val the wanted value
1309 * Returns the first value from the config set under the given name.
1310 * If there is more than one value, it will issue a warning. Consider using
1311 * xbt_cfg_get_dynar() instead.
1313 * \warning the returned value is the actual content of the config set
1315 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1317 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1319 if (xbt_dynar_length(variable->content) > 1) {
1321 ("You asked for the first value of the config element '%s', but there is %lu values",
1322 name, xbt_dynar_length(variable->content));
1325 return xbt_dynar_get_as(variable->content, 0, int);
1328 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1330 * \arg cfg the config set
1331 * \arg name the name of the variable
1332 * \arg val the wanted value
1334 * Returns the first value from the config set under the given name.
1335 * If there is more than one value, it will issue a warning. Consider using
1336 * xbt_cfg_get_dynar() instead.
1338 * \warning the returned value is the actual content of the config set
1341 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1343 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1345 if (xbt_dynar_length(variable->content) > 1) {
1347 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1348 name, xbt_dynar_length(variable->content));
1351 return xbt_dynar_get_as(variable->content, 0, double);
1354 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1356 * \arg th the config set
1357 * \arg name the name of the variable
1358 * \arg val the wanted value
1360 * Returns the first value from the config set under the given name.
1361 * If there is more than one value, it will issue a warning. Consider using
1362 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1364 * \warning the returned value is the actual content of the config set
1367 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1369 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1371 if (xbt_dynar_length(variable->content) > 1) {
1373 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1374 name, xbt_dynar_length(variable->content));
1375 } else if (xbt_dynar_is_empty(variable->content)) {
1379 return xbt_dynar_get_as(variable->content, 0, char *);
1382 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1384 * \arg cfg the config set
1385 * \arg name the name of the variable
1386 * \arg val the wanted value
1388 * Returns the first value from the config set under the given name.
1389 * If there is more than one value, it will issue a warning. Consider using
1390 * xbt_cfg_get_dynar() instead.
1392 * \warning the returned value is the actual content of the config set
1394 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1396 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1398 if (xbt_dynar_length(variable->content) > 1) {
1400 ("You asked for the first value of the config element '%s', but there is %lu values",
1401 name, xbt_dynar_length(variable->content));
1404 return xbt_dynar_get_as(variable->content, 0, int);
1407 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1409 * \arg cfg the config set
1410 * \arg name the name of the variable
1411 * \arg peer the peer
1412 * \arg port the port number
1414 * Returns the first value from the config set under the given name.
1415 * If there is more than one value, it will issue a warning. Consider using
1416 * xbt_cfg_get_dynar() instead.
1418 * \warning the returned value is the actual content of the config set
1421 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1424 xbt_cfgelm_t variable;
1427 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1429 if (xbt_dynar_length(variable->content) > 1) {
1431 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1432 name, xbt_dynar_length(variable->content));
1435 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1440 /** @brief Retrieve the dynar of all the values stored in a variable
1442 * \arg cfg where to search in
1443 * \arg name what to search for
1446 * Get the data stored in the config set.
1448 * \warning the returned value is the actual content of the config set
1450 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1452 xbt_cfgelm_t variable = NULL;
1456 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1459 if (e.category == not_found_error) {
1461 THROWF(not_found_error, 0,
1462 "No registered variable %s in this config set", name);
1467 return variable->content;
1471 /** @brief Retrieve one of the integer value of a variable */
1472 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1475 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1476 return xbt_dynar_get_as(variable->content, pos, int);
1479 /** @brief Retrieve one of the double value of a variable */
1480 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1483 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1484 return xbt_dynar_get_as(variable->content, pos, double);
1488 /** @brief Retrieve one of the string value of a variable */
1489 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1492 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1493 return xbt_dynar_get_as(variable->content, pos, char *);
1496 /** @brief Retrieve one of the boolean value of a variable */
1497 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1500 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1501 return xbt_dynar_get_as(variable->content, pos, int);
1504 /** @brief Retrieve one of the peer value of a variable */
1506 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1507 char **peer, int *port)
1510 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1511 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1522 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1524 XBT_TEST_SUITE("config", "Configuration support");
1526 static xbt_cfg_t make_set()
1528 xbt_cfg_t set = NULL;
1530 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1531 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1532 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1533 xbt_cfg_register_str(&set, "user:1_to_10_string");
1536 } /* end_of_make_set */
1538 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1540 xbt_cfg_t set = make_set();
1541 xbt_test_add("Alloc and free a config set");
1542 xbt_cfg_set_parse(set,
1543 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1548 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1550 xbt_cfg_t set = set = make_set();
1553 xbt_test_add("Having too few elements for speed");
1554 xbt_cfg_set_parse(set,
1555 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1560 if (e.category != mismatch_error ||
1561 strncmp(e.msg, "Config elem speed needs",
1562 strlen("Config elem speed needs")))
1563 xbt_test_fail("Got an exception. msg=%s", e.msg);
1571 xbt_test_add("Having too much values of 'speed'");
1573 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1575 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1578 if (e.category != mismatch_error ||
1579 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1580 strlen("Config elem speed needs")))
1581 xbt_test_fail("Got an exception. msg=%s", e.msg);
1590 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1593 xbt_test_add("Get a single value");
1595 /* get_single_value */
1597 xbt_cfg_t myset = make_set();
1599 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1600 ival = xbt_cfg_get_int(myset, "speed");
1602 xbt_test_fail("Speed value = %d, I expected 42", ival);
1603 xbt_cfg_free(&myset);
1606 xbt_test_add("Get multiple values");
1608 /* get_multiple_value */
1610 xbt_cfg_t myset = make_set();
1612 xbt_cfg_set_parse(myset,
1613 "peername:veloce user:foo\nuser:bar\tuser:toto");
1614 xbt_cfg_set_parse(myset, "speed:42");
1615 xbt_cfg_check(myset);
1616 dyn = xbt_cfg_get_dynar(myset, "user");
1618 if (xbt_dynar_length(dyn) != 3)
1619 xbt_test_fail("Dynar length = %lu, I expected 3",
1620 xbt_dynar_length(dyn));
1622 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1623 xbt_test_fail("Dynar[0] = %s, I expected foo",
1624 xbt_dynar_get_as(dyn, 0, char *));
1626 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1627 xbt_test_fail("Dynar[1] = %s, I expected bar",
1628 xbt_dynar_get_as(dyn, 1, char *));
1630 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1631 xbt_test_fail("Dynar[2] = %s, I expected toto",
1632 xbt_dynar_get_as(dyn, 2, char *));
1633 xbt_cfg_free(&myset);
1636 xbt_test_add("Access to a non-existant entry");
1638 /* non-existant_entry */
1639 xbt_cfg_t myset = make_set();
1643 xbt_cfg_set_parse(myset, "color:blue");
1646 if (e.category != not_found_error)
1647 xbt_test_exception(e);
1650 xbt_cfg_free(&myset);
1653 #endif /* SIMGRID_TEST */