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 */
21 #include "surf/surf_routing.h" /* COORD_HOST_LEVEL and COORD_ASR_LEVEL */
24 int COORD_HOST_LEVEL; //Coordinates level
25 xbt_lib_t as_router_lib;
26 int COORD_ASR_LEVEL; //Coordinates level
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
30 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
32 Both data and DTD are mixed, but fixing it now would prevent me to ever
39 /* Allowed type of the variable */
40 e_xbt_cfgelm_type_t type;
49 (cannot be an union because type peer uses both str and i) */
51 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
53 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
54 { "int", "double", "string", "peer", "any" };
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 an config set
71 xbt_cfg_t xbt_cfg_new(void)
73 return (xbt_cfg_t) xbt_dict_new();
76 /** \brief Copy an existing configuration set
78 * \arg whereto the config set to be created
79 * \arg 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, NULL,
96 variable->min, variable->max, variable->cb_set,
101 /** @brief Destructor */
102 void xbt_cfg_free(xbt_cfg_t * cfg)
104 XBT_DEBUG("Frees cfg set %p", cfg);
105 xbt_dict_free((xbt_dict_t *) cfg);
108 /** @brief Dump a config set for debuging purpose
110 * \arg name The name to give to this config set
111 * \arg indent what to write at the begining of each line (right number of spaces)
112 * \arg cfg the config set
114 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
116 xbt_dict_t dict = (xbt_dict_t) cfg;
117 xbt_dict_cursor_t cursor = NULL;
118 xbt_cfgelm_t variable = NULL;
128 printf("%s>> Dumping of the config set '%s':\n", indent, name);
129 xbt_dict_foreach(dict, cursor, key, variable) {
131 printf("%s %s:", indent, key);
133 size = xbt_dynar_length(variable->content);
135 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
136 variable->min, variable->max,
137 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
140 switch (variable->type) {
143 for (i = 0; i < size; i++) {
144 ival = xbt_dynar_get_as(variable->content, i, int);
145 printf("%s %d\n", indent, ival);
149 case xbt_cfgelm_double:
150 for (i = 0; i < size; i++) {
151 dval = xbt_dynar_get_as(variable->content, i, double);
152 printf("%s %f\n", indent, dval);
156 case xbt_cfgelm_string:
157 for (i = 0; i < size; i++) {
158 sval = xbt_dynar_get_as(variable->content, i, char *);
159 printf("%s %s\n", indent, sval);
163 case xbt_cfgelm_peer:
164 for (i = 0; i < size; i++) {
165 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
166 printf("%s %s:%d\n", indent, hval->name, hval->port);
171 printf("%s Invalid type!!\n", indent);
177 printf("%s<< End of the config set '%s'\n", indent, name);
180 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 xbt_dynar_free(&(c->content));
200 /*----[ Registering stuff ]-----------------------------------------------*/
202 /** @brief Register an element within a config set
204 * @arg cfg the config set
205 * @arg type the type of the config element
206 * @arg min the minimum
207 * @arg max the maximum
211 xbt_cfg_register(xbt_cfg_t * cfg,
212 const char *name, const char *desc,
213 e_xbt_cfgelm_type_t type, void *default_value, int min,
214 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
219 *cfg = xbt_cfg_new();
220 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
221 "type of %s not valid (%d should be between %d and %d)",
222 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
223 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
226 XBT_WARN("Config elem %s registered twice.", name);
227 /* Will be removed by the insertion of the new one */
230 res = xbt_new(s_xbt_cfgelm_t, 1);
231 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
232 name, desc, min, max, xbt_cfgelm_type_name[type], type, res,
235 res->desc = xbt_strdup(desc);
239 res->cb_set = cb_set;
245 res->content = xbt_dynar_new(sizeof(int), NULL);
247 xbt_dynar_push(res->content, default_value);
250 case xbt_cfgelm_double:
251 res->content = xbt_dynar_new(sizeof(double), NULL);
253 xbt_dynar_push(res->content, default_value);
256 case xbt_cfgelm_string:
257 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
259 xbt_dynar_push(res->content, default_value);
262 case xbt_cfgelm_peer:
263 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
265 xbt_dynar_push(res->content, default_value);
269 XBT_ERROR("%d is an invalide type code", type);
272 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
275 /** @brief Unregister an element from a config set.
277 * @arg cfg the config set
278 * @arg name the name of the elem to be freed
280 * Note that it removes both the description and the actual content.
281 * Throws not_found when no such element exists.
284 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
286 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
287 xbt_dict_remove((xbt_dict_t) cfg, name);
291 * @brief Parse a string and register the stuff described.
293 * @arg cfg the config set
294 * @arg entry a string describing the element to register
296 * The string may consist in several variable descriptions separated by a space.
297 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
298 * with type being one of 'string','int', 'peer' or 'double'.
300 * FIXME: this does not allow to set the description
303 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
305 char *entrycpy = xbt_strdup(entry);
309 e_xbt_cfgelm_type_t type;
310 XBT_DEBUG("Register string '%s'", entry);
312 tok = strchr(entrycpy, ':');
313 xbt_assert(tok, "Invalid config element descriptor: %s%s",
314 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
317 min = strtol(tok, &tok, 10);
318 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
321 xbt_assert(strcmp(tok, "_to_"),
322 "Invalid config element descriptor : %s%s",
323 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
324 tok += strlen("_to_");
326 max = strtol(tok, &tok, 10);
327 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
330 xbt_assert(*tok == '_',
331 "Invalid config element descriptor: %s%s", entry,
332 "; Should be <name>:<min nb>_to_<max nb>_<type>");
336 type < xbt_cfgelm_type_count
337 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
338 xbt_assert(type < xbt_cfgelm_type_count,
339 "Invalid type in config element descriptor: %s%s", entry,
340 "; Should be one of 'string', 'int', 'peer' or 'double'.");
342 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
344 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
347 static int strcmp_voidp(const void *pa, const void *pb)
349 return strcmp(*(const char **)pa, *(const char **)pb);
352 /** @brief Displays the declared options and their description */
353 void xbt_cfg_help(xbt_cfg_t cfg)
355 xbt_dict_cursor_t dict_cursor;
356 unsigned int dynar_cursor;
357 xbt_cfgelm_t variable;
359 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
361 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
362 xbt_dynar_push(names, &name);
364 xbt_dynar_sort(names, strcmp_voidp);
366 xbt_dynar_foreach(names, dynar_cursor, name) {
369 variable = xbt_dict_get((xbt_dict_t )cfg, name);
371 printf(" %s: %s\n", name, variable->desc);
372 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
373 if (variable->min != 1 || variable->max != 1) {
374 if (variable->min == 0 && variable->max == 0)
375 printf("Arity: no bound; ");
377 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
379 printf("Current value: ");
380 size = xbt_dynar_length(variable->content);
382 switch (variable->type) {
389 for (i = 0; i < size; i++) {
390 ival = xbt_dynar_get_as(variable->content, i, int);
391 printf("%s%d\n", (i == 0 ? "" : " "), ival);
395 case xbt_cfgelm_double:
396 for (i = 0; i < size; i++) {
397 dval = xbt_dynar_get_as(variable->content, i, double);
398 printf("%s%f\n", (i == 0 ? "" : " "), dval);
402 case xbt_cfgelm_string:
403 for (i = 0; i < size; i++) {
404 sval = xbt_dynar_get_as(variable->content, i, char *);
405 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
409 case xbt_cfgelm_peer:
410 for (i = 0; i < size; i++) {
411 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
412 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
418 printf("Invalid type!!\n");
423 xbt_dynar_free(&names);
426 /** @brief Check that each variable have the right amount of values */
427 void xbt_cfg_check(xbt_cfg_t cfg)
429 xbt_dict_cursor_t cursor;
430 xbt_cfgelm_t variable;
434 xbt_assert(cfg, "NULL config set.");
435 XBT_DEBUG("Check cfg set %p", cfg);
437 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
438 size = xbt_dynar_length(variable->content);
439 if (variable->min > size) {
440 xbt_dict_cursor_free(&cursor);
441 THROWF(mismatch_error, 0,
442 "Config elem %s needs at least %d %s, but there is only %d values.",
443 name, variable->min, xbt_cfgelm_type_name[variable->type],
447 if (variable->max > 0 && variable->max < size) {
448 xbt_dict_cursor_free(&cursor);
449 THROWF(mismatch_error, 0,
450 "Config elem %s accepts at most %d %s, but there is %d values.",
451 name, variable->max, xbt_cfgelm_type_name[variable->type],
456 xbt_dict_cursor_free(&cursor);
459 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
461 e_xbt_cfgelm_type_t type)
463 xbt_cfgelm_t res = NULL;
465 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
468 THROWF(not_found_error, 0,
469 "No registered variable '%s' in this config set", name);
472 xbt_assert(type == xbt_cfgelm_any || res->type == type,
473 "You tried to access to the config element %s as an %s, but its type is %s.",
475 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
480 /** @brief Get the type of this variable in that configuration set
482 * \arg cfg the config set
483 * \arg name the name of the element
484 * \arg type the result
488 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
491 xbt_cfgelm_t variable = NULL;
493 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
495 THROWF(not_found_error, 0,
496 "Can't get the type of '%s' since this variable does not exist",
499 XBT_INFO("type in variable = %d", variable->type);
501 return variable->type;
504 /*----[ Setting ]---------------------------------------------------------*/
505 /** @brief va_args version of xbt_cfg_set
507 * \arg cfg config set to fill
508 * \arg n variable name
509 * \arg pa variable value
511 * Add some values to the config set.
513 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
518 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
523 type = xbt_cfg_get_type(cfg, name);
525 if (e.category == not_found_error) {
527 THROWF(not_found_error, 0,
528 "Can't set the property '%s' since it's not registered",
535 case xbt_cfgelm_peer:
536 str = va_arg(pa, char *);
538 xbt_cfg_set_peer(cfg, name, str, i);
541 case xbt_cfgelm_string:
542 str = va_arg(pa, char *);
543 xbt_cfg_set_string(cfg, name, str);
548 xbt_cfg_set_int(cfg, name, i);
551 case xbt_cfgelm_double:
552 d = va_arg(pa, double);
553 xbt_cfg_set_double(cfg, name, d);
557 xbt_die("Config element variable %s not valid (type=%d)", name, type);
561 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
563 * \arg cfg config set to fill
564 * \arg name variable name
565 * \arg varargs variable value
568 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
573 xbt_cfg_set_vargs(cfg, name, pa);
577 /** @brief Add values parsed from a string into a config set
579 * \arg cfg config set to fill
580 * \arg options a string containing the content to add to the config set. This
581 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
582 * like "[name]:[value]" where [name] is the name of an already registred
583 * variable, and [value] conforms to the data type under which this variable was
586 * @todo This is a crude manual parser, it should be a proper lexer.
589 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
597 xbt_cfgelm_t variable = NULL;
598 char *optionlist_cpy;
599 char *option, *name, *val;
604 if (!options || !strlen(options)) { /* nothing to do */
607 optionlist_cpy = xbt_strdup(options);
609 XBT_DEBUG("List to parse and set:'%s'", options);
610 option = optionlist_cpy;
611 while (1) { /* breaks in the code */
617 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
618 name, len, (long) (option - name));
621 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
622 && *option != '\t' && *option != ',') {
623 XBT_DEBUG("Take %c.", *option);
626 if (option - name == len) {
627 XBT_DEBUG("Boundary=EOL");
628 option = NULL; /* don't do next iteration */
631 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
632 *option, len, (long) (option - name));
634 /* Pass the following blank chars */
636 while (option - name < (len - 1) &&
637 (*option == ' ' || *option == '\n' || *option == '\t')) {
638 /* fprintf(stderr,"Ignore a blank char.\n"); */
641 if (option - name == len - 1)
642 option = NULL; /* don't do next iteration */
644 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
646 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
651 val = strchr(name, ':');
653 free(optionlist_cpy);
654 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
659 if (!strcmp(name,"coordinates")){
660 if(!strcmp(val,"yes") && !COORD_HOST_LEVEL){
661 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
662 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
663 COORD_ASR_LEVEL = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
664 free(optionlist_cpy);
667 if(strcmp(val,"yes"))
668 xbt_die("Setting CMD prop coordinates must be \"yes\"");
671 if (strncmp(name, "contexts/", strlen("contexts/")))
672 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
675 variable = xbt_dict_get((xbt_dict_t) cfg, name);
678 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
679 name = (char *) (optionlist_cpy - name + options);
680 free(optionlist_cpy);
681 if (e.category == not_found_error) {
683 THROWF(not_found_error, 0,
684 "No registered variable corresponding to '%s'.", name);
690 switch (variable->type) {
691 case xbt_cfgelm_string:
692 xbt_cfg_set_string(cfg, name, val); /* throws */
696 i = strtol(val, &val, 0);
698 free(optionlist_cpy);
699 xbt_die("Value of option %s not valid. Should be an integer", name);
702 xbt_cfg_set_int(cfg, name, i); /* throws */
705 case xbt_cfgelm_double:
706 d = strtod(val, &val);
708 free(optionlist_cpy);
709 xbt_die("Value of option %s not valid. Should be a double", name);
712 xbt_cfg_set_double(cfg, name, d); /* throws */
715 case xbt_cfgelm_peer:
717 val = strchr(val, ':');
719 free(optionlist_cpy);
720 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
725 i = strtol(val, &val, 0);
727 free(optionlist_cpy);
728 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
732 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
736 THROWF(unknown_error, 0, "Type of config element %s is not valid.",
741 free(optionlist_cpy);
745 free(optionlist_cpy);
749 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
751 * This is useful to change the default value of a variable while allowing
752 * users to override it with command line arguments
754 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
756 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
758 if (variable->isdefault){
759 xbt_cfg_set_int(cfg, name, val);
760 variable->isdefault = 1;
764 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
768 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
770 * This is useful to change the default value of a variable while allowing
771 * users to override it with command line arguments
773 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
775 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
777 if (variable->isdefault) {
778 xbt_cfg_set_double(cfg, name, val);
779 variable->isdefault = 1;
783 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
787 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
789 * This is useful to change the default value of a variable while allowing
790 * users to override it with command line arguments
792 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
795 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
797 if (variable->isdefault){
798 xbt_cfg_set_string(cfg, name, val);
799 variable->isdefault = 1;
803 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
807 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
809 * This is useful to change the default value of a variable while allowing
810 * users to override it with command line arguments
812 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
813 const char *host, int port)
815 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
817 if (variable->isdefault){
818 xbt_cfg_set_peer(cfg, name, host, port);
819 variable->isdefault = 1;
823 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
827 /** @brief Set or add an integer value to \a name within \a cfg
829 * \arg cfg the config set
830 * \arg name the name of the variable
831 * \arg val the value of the variable
833 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
835 xbt_cfgelm_t variable;
837 XBT_VERB("Configuration setting: %s=%d", name, val);
838 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
840 if (variable->max == 1) {
841 if (variable->cb_rm && xbt_dynar_length(variable->content))
842 (*variable->cb_rm) (name, 0);
844 xbt_dynar_set(variable->content, 0, &val);
847 && xbt_dynar_length(variable->content) ==
848 (unsigned long) variable->max)
849 THROWF(mismatch_error, 0,
850 "Cannot add value %d to the config element %s since it's already full (size=%d)",
851 val, name, variable->max);
853 xbt_dynar_push(variable->content, &val);
856 if (variable->cb_set)
857 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
858 variable->isdefault = 0;
861 /** @brief Set or add a double 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 doule to set
868 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
870 xbt_cfgelm_t variable;
872 XBT_VERB("Configuration setting: %s=%f", name, val);
873 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
875 if (variable->max == 1) {
876 if (variable->cb_rm && xbt_dynar_length(variable->content))
877 (*variable->cb_rm) (name, 0);
879 xbt_dynar_set(variable->content, 0, &val);
882 && xbt_dynar_length(variable->content) == variable->max)
883 THROWF(mismatch_error, 0,
884 "Cannot add value %f 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 string 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 value to be added
903 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
905 xbt_cfgelm_t variable;
906 char *newval = xbt_strdup(val);
908 XBT_VERB("Configuration setting: %s=%s", name, val);
909 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
910 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
911 variable->min, variable->max,
912 xbt_cfgelm_type_name[variable->type], variable->type, variable);
914 if (variable->max == 1) {
915 if (xbt_dynar_length(variable->content)) {
917 (*variable->cb_rm) (name, 0);
918 else if (variable->type == xbt_cfgelm_string) {
919 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
924 xbt_dynar_set(variable->content, 0, &newval);
927 && xbt_dynar_length(variable->content) == variable->max)
928 THROWF(mismatch_error, 0,
929 "Cannot add value %s to the config element %s since it's already full (size=%d)",
930 name, val, variable->max);
932 xbt_dynar_push(variable->content, &newval);
935 if (variable->cb_set)
936 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
937 variable->isdefault = 0;
940 /** @brief Set or add an peer value to \a name within \a cfg
942 * \arg cfg the config set
943 * \arg name the name of the variable
945 * \arg port the port number
947 * \e peer values are composed of a string (peername) and an integer (port)
951 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
954 xbt_cfgelm_t variable;
955 xbt_peer_t val = xbt_peer_new(peer, port);
957 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
959 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
961 if (variable->max == 1) {
962 if (variable->cb_rm && xbt_dynar_length(variable->content))
963 (*variable->cb_rm) (name, 0);
965 xbt_dynar_set(variable->content, 0, &val);
968 && xbt_dynar_length(variable->content) == variable->max)
969 THROWF(mismatch_error, 0,
970 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
971 peer, port, name, variable->max);
973 xbt_dynar_push(variable->content, &val);
976 if (variable->cb_set)
977 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
978 variable->isdefault = 0;
981 /* ---- [ Removing ] ---- */
983 /** @brief Remove the provided \e val integer value from a variable
985 * \arg cfg the config set
986 * \arg name the name of the variable
987 * \arg val the value to be removed
989 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
992 xbt_cfgelm_t variable;
996 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
998 if (xbt_dynar_length(variable->content) == variable->min)
999 THROWF(mismatch_error, 0,
1000 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1001 val, name, variable->min);
1003 xbt_dynar_foreach(variable->content, cpt, seen) {
1005 if (variable->cb_rm)
1006 (*variable->cb_rm) (name, cpt);
1007 xbt_dynar_cursor_rm(variable->content, &cpt);
1012 THROWF(not_found_error, 0,
1013 "Can't remove the value %d of config element %s: value not found.",
1017 /** @brief Remove the provided \e val double value from a variable
1019 * \arg cfg the config set
1020 * \arg name the name of the variable
1021 * \arg val the value to be removed
1024 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1026 xbt_cfgelm_t variable;
1030 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1032 if (xbt_dynar_length(variable->content) == variable->min)
1033 THROWF(mismatch_error, 0,
1034 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1035 val, name, variable->min);
1037 xbt_dynar_foreach(variable->content, cpt, seen) {
1039 xbt_dynar_cursor_rm(variable->content, &cpt);
1040 if (variable->cb_rm)
1041 (*variable->cb_rm) (name, cpt);
1046 THROWF(not_found_error, 0,
1047 "Can't remove the value %f of config element %s: value not found.",
1051 /** @brief Remove the provided \e val string value from a variable
1053 * \arg cfg the config set
1054 * \arg name the name of the variable
1055 * \arg val the value of the string which will be removed
1057 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1059 xbt_cfgelm_t variable;
1063 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1065 if (xbt_dynar_length(variable->content) == variable->min)
1066 THROWF(mismatch_error, 0,
1067 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1068 name, val, variable->min);
1070 xbt_dynar_foreach(variable->content, cpt, seen) {
1071 if (!strcpy(seen, val)) {
1072 if (variable->cb_rm)
1073 (*variable->cb_rm) (name, cpt);
1074 xbt_dynar_cursor_rm(variable->content, &cpt);
1079 THROWF(not_found_error, 0,
1080 "Can't remove the value %s of config element %s: value not found.",
1084 /** @brief Remove the provided \e val peer value from a variable
1086 * \arg cfg the config set
1087 * \arg name the name of the variable
1088 * \arg peer the peername
1089 * \arg port the port number
1093 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1096 xbt_cfgelm_t variable;
1100 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1102 if (xbt_dynar_length(variable->content) == variable->min)
1103 THROWF(mismatch_error, 0,
1104 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1105 peer, port, name, variable->min);
1107 xbt_dynar_foreach(variable->content, cpt, seen) {
1108 if (!strcpy(seen->name, peer) && seen->port == port) {
1109 if (variable->cb_rm)
1110 (*variable->cb_rm) (name, cpt);
1111 xbt_dynar_cursor_rm(variable->content, &cpt);
1116 THROWF(not_found_error, 0,
1117 "Can't remove the value %s:%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;
1128 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1130 if (xbt_dynar_length(variable->content) == variable->min)
1131 THROWF(mismatch_error, 0,
1132 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1133 pos, name, variable->min);
1135 if (variable->cb_rm)
1136 (*variable->cb_rm) (name, pos);
1137 xbt_dynar_remove_at(variable->content, pos, NULL);
1140 /** @brief Remove all the values from a variable
1142 * \arg cfg the config set
1143 * \arg name the name of the variable
1146 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1148 xbt_cfgelm_t variable = NULL;
1152 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1154 if (e.category != not_found_error)
1158 THROWF(not_found_error, 0,
1159 "Can't empty '%s' since this config element does not exist",
1164 if (variable->cb_rm) {
1167 xbt_dynar_foreach(variable->content, cpt, ignored) {
1168 (*variable->cb_rm) (name, cpt);
1171 xbt_dynar_reset(variable->content);
1175 * Say if the value is the default value
1177 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1179 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1180 return variable->isdefault;
1183 /*----[ Getting ]---------------------------------------------------------*/
1185 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1187 * \arg cfg the config set
1188 * \arg name the name of the variable
1189 * \arg val the wanted value
1191 * Returns the first value from the config set under the given name.
1192 * If there is more than one value, it will issue a warning. Consider using
1193 * xbt_cfg_get_dynar() instead.
1195 * \warning the returned value is the actual content of the config set
1197 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1199 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1201 if (xbt_dynar_length(variable->content) > 1) {
1203 ("You asked for the first value of the config element '%s', but there is %lu values",
1204 name, xbt_dynar_length(variable->content));
1207 return xbt_dynar_get_as(variable->content, 0, int);
1210 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1212 * \arg cfg the config set
1213 * \arg name the name of the variable
1214 * \arg val the wanted value
1216 * Returns the first value from the config set under the given name.
1217 * If there is more than one value, it will issue a warning. Consider using
1218 * xbt_cfg_get_dynar() instead.
1220 * \warning the returned value is the actual content of the config set
1223 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1225 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1227 if (xbt_dynar_length(variable->content) > 1) {
1229 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1230 name, xbt_dynar_length(variable->content));
1233 return xbt_dynar_get_as(variable->content, 0, double);
1236 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1238 * \arg th the config set
1239 * \arg name the name of the variable
1240 * \arg val the wanted value
1242 * Returns the first value from the config set under the given name.
1243 * If there is more than one value, it will issue a warning. Consider using
1244 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1246 * \warning the returned value is the actual content of the config set
1249 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1251 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1253 if (xbt_dynar_length(variable->content) > 1) {
1255 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1256 name, xbt_dynar_length(variable->content));
1257 } else if (xbt_dynar_length(variable->content) == 0) {
1261 return xbt_dynar_get_as(variable->content, 0, char *);
1264 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1266 * \arg cfg the config set
1267 * \arg name the name of the variable
1268 * \arg peer the peer
1269 * \arg port the port number
1271 * Returns the first value from the config set under the given name.
1272 * If there is more than one value, it will issue a warning. Consider using
1273 * xbt_cfg_get_dynar() instead.
1275 * \warning the returned value is the actual content of the config set
1278 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1281 xbt_cfgelm_t variable;
1284 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1286 if (xbt_dynar_length(variable->content) > 1) {
1288 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1289 name, xbt_dynar_length(variable->content));
1292 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1297 /** @brief Retrieve the dynar of all the values stored in a variable
1299 * \arg cfg where to search in
1300 * \arg name what to search for
1303 * Get the data stored in the config set.
1305 * \warning the returned value is the actual content of the config set
1307 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1309 xbt_cfgelm_t variable = NULL;
1313 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1315 if (e.category == not_found_error) {
1317 THROWF(not_found_error, 0,
1318 "No registered variable %s in this config set", name);
1323 return variable->content;
1327 /** @brief Retrieve one of the integer value of a variable */
1328 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1331 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1332 return xbt_dynar_get_as(variable->content, pos, int);
1335 /** @brief Retrieve one of the double value of a variable */
1336 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1339 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1340 return xbt_dynar_get_as(variable->content, pos, double);
1344 /** @brief Retrieve one of the string value of a variable */
1345 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1348 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1349 return xbt_dynar_get_as(variable->content, pos, char *);
1352 /** @brief Retrieve one of the peer value of a variable */
1354 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1355 char **peer, int *port)
1358 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1359 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1370 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1371 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1373 XBT_TEST_SUITE("config", "Configuration support");
1375 static xbt_cfg_t make_set()
1377 xbt_cfg_t set = NULL;
1379 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1380 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1381 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1382 xbt_cfg_register_str(&set, "user:1_to_10_string");
1385 } /* end_of_make_set */
1387 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1389 xbt_cfg_t set = make_set();
1390 xbt_test_add("Alloc and free a config set");
1391 xbt_cfg_set_parse(set,
1392 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1397 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1399 xbt_cfg_t set = set = make_set();
1402 xbt_test_add("Having too few elements for speed");
1403 xbt_cfg_set_parse(set,
1404 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1409 if (e.category != mismatch_error ||
1410 strncmp(e.msg, "Config elem speed needs",
1411 strlen("Config elem speed needs")))
1412 xbt_test_fail("Got an exception. msg=%s", e.msg);
1420 xbt_test_add("Having too much values of 'speed'");
1422 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1424 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1427 if (e.category != mismatch_error ||
1428 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1429 strlen("Config elem speed needs")))
1430 xbt_test_fail("Got an exception. msg=%s", e.msg);
1439 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1442 xbt_test_add("Get a single value");
1444 /* get_single_value */
1446 xbt_cfg_t myset = make_set();
1448 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1449 ival = xbt_cfg_get_int(myset, "speed");
1451 xbt_test_fail("Speed value = %d, I expected 42", ival);
1452 xbt_cfg_free(&myset);
1455 xbt_test_add("Get multiple values");
1457 /* get_multiple_value */
1459 xbt_cfg_t myset = make_set();
1461 xbt_cfg_set_parse(myset,
1462 "peername:veloce user:foo\nuser:bar\tuser:toto");
1463 xbt_cfg_set_parse(myset, "speed:42");
1464 xbt_cfg_check(myset);
1465 dyn = xbt_cfg_get_dynar(myset, "user");
1467 if (xbt_dynar_length(dyn) != 3)
1468 xbt_test_fail("Dynar length = %lu, I expected 3",
1469 xbt_dynar_length(dyn));
1471 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1472 xbt_test_fail("Dynar[0] = %s, I expected foo",
1473 xbt_dynar_get_as(dyn, 0, char *));
1475 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1476 xbt_test_fail("Dynar[1] = %s, I expected bar",
1477 xbt_dynar_get_as(dyn, 1, char *));
1479 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1480 xbt_test_fail("Dynar[2] = %s, I expected toto",
1481 xbt_dynar_get_as(dyn, 2, char *));
1482 xbt_cfg_free(&myset);
1485 xbt_test_add("Access to a non-existant entry");
1487 /* non-existant_entry */
1488 xbt_cfg_t myset = make_set();
1492 xbt_cfg_set_parse(myset, "color:blue");
1494 if (e.category != not_found_error)
1495 xbt_test_exception(e);
1498 xbt_cfg_free(&myset);
1501 #endif /* SIMGRID_TEST */