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", "peer", "any" };
50 /* Internal stuff used in cache to free a variable */
51 static void xbt_cfgelm_free(void *data);
53 /* Retrieve the variable we'll modify */
54 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
55 e_xbt_cfgelm_type_t type);
57 /*----[ Memory management ]-----------------------------------------------*/
59 /** @brief Constructor
61 * Initialise an config set
65 xbt_cfg_t xbt_cfg_new(void)
67 return (xbt_cfg_t) xbt_dict_new();
70 /** \brief Copy an existing configuration set
72 * \arg whereto the config set to be created
73 * \arg tocopy the source data
75 * This only copy the registrations, not the actual content
78 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
80 xbt_dict_cursor_t cursor = NULL;
81 xbt_cfgelm_t variable = NULL;
84 XBT_DEBUG("Copy cfg set %p", tocopy);
86 xbt_assert0(tocopy, "cannot copy NULL config");
88 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
89 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
90 variable->min, variable->max, variable->cb_set,
95 /** @brief Destructor */
96 void xbt_cfg_free(xbt_cfg_t * cfg)
98 XBT_DEBUG("Frees cfg set %p", cfg);
99 xbt_dict_free((xbt_dict_t *) cfg);
102 /** @brief Dump a config set for debuging purpose
104 * \arg name The name to give to this config set
105 * \arg indent what to write at the begining of each line (right number of spaces)
106 * \arg cfg the config set
108 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
110 xbt_dict_t dict = (xbt_dict_t) cfg;
111 xbt_dict_cursor_t cursor = NULL;
112 xbt_cfgelm_t variable = NULL;
122 printf("%s>> Dumping of the config set '%s':\n", indent, name);
123 xbt_dict_foreach(dict, cursor, key, variable) {
125 printf("%s %s:", indent, key);
127 size = xbt_dynar_length(variable->content);
129 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
130 variable->min, variable->max,
131 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
134 switch (variable->type) {
137 for (i = 0; i < size; i++) {
138 ival = xbt_dynar_get_as(variable->content, i, int);
139 printf("%s %d\n", indent, ival);
143 case xbt_cfgelm_double:
144 for (i = 0; i < size; i++) {
145 dval = xbt_dynar_get_as(variable->content, i, double);
146 printf("%s %f\n", indent, dval);
150 case xbt_cfgelm_string:
151 for (i = 0; i < size; i++) {
152 sval = xbt_dynar_get_as(variable->content, i, char *);
153 printf("%s %s\n", indent, sval);
157 case xbt_cfgelm_peer:
158 for (i = 0; i < size; i++) {
159 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
160 printf("%s %s:%d\n", indent, hval->name, hval->port);
165 printf("%s Invalid type!!\n", indent);
171 printf("%s<< End of the config set '%s'\n", indent, name);
174 xbt_dict_cursor_free(&cursor);
179 * free an config element
182 void xbt_cfgelm_free(void *data)
184 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
186 XBT_DEBUG("Frees cfgelm %p", c);
190 xbt_dynar_free(&(c->content));
194 /*----[ Registering stuff ]-----------------------------------------------*/
196 /** @brief Register an element within a config set
198 * @arg cfg the config set
199 * @arg type the type of the config element
200 * @arg min the minimum
201 * @arg max the maximum
205 xbt_cfg_register(xbt_cfg_t * cfg,
206 const char *name, const char *desc,
207 e_xbt_cfgelm_type_t type, void *default_value, int min,
208 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
213 *cfg = xbt_cfg_new();
214 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
215 "type of %s not valid (%d should be between %d and %d)",
216 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
217 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
220 XBT_WARN("Config elem %s registered twice.", name);
221 /* Will be removed by the insertion of the new one */
224 res = xbt_new(s_xbt_cfgelm_t, 1);
225 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
226 name, desc, min, max, xbt_cfgelm_type_name[type], type, res,
229 res->desc = xbt_strdup(desc);
233 res->cb_set = cb_set;
239 res->content = xbt_dynar_new(sizeof(int), NULL);
241 xbt_dynar_push(res->content, default_value);
244 case xbt_cfgelm_double:
245 res->content = xbt_dynar_new(sizeof(double), NULL);
247 xbt_dynar_push(res->content, default_value);
250 case xbt_cfgelm_string:
251 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
253 xbt_dynar_push(res->content, default_value);
256 case xbt_cfgelm_peer:
257 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
259 xbt_dynar_push(res->content, default_value);
263 XBT_ERROR("%d is an invalide type code", type);
266 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
269 /** @brief Unregister an element from a config set.
271 * @arg cfg the config set
272 * @arg name the name of the elem to be freed
274 * Note that it removes both the description and the actual content.
275 * Throws not_found when no such element exists.
278 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
280 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
281 xbt_dict_remove((xbt_dict_t) cfg, name);
285 * @brief Parse a string and register the stuff described.
287 * @arg cfg the config set
288 * @arg entry a string describing the element to register
290 * The string may consist in several variable descriptions separated by a space.
291 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
292 * with type being one of 'string','int', 'peer' or 'double'.
294 * @fixme: this does not allow to set the description
297 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
299 char *entrycpy = xbt_strdup(entry);
303 e_xbt_cfgelm_type_t type;
304 XBT_DEBUG("Register string '%s'", entry);
306 tok = strchr(entrycpy, ':');
307 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
308 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
311 min = strtol(tok, &tok, 10);
312 xbt_assert1(tok, "Invalid minimum in config element descriptor %s",
315 xbt_assert2(strcmp(tok, "_to_"),
316 "Invalid config element descriptor : %s%s",
317 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
318 tok += strlen("_to_");
320 max = strtol(tok, &tok, 10);
321 xbt_assert1(tok, "Invalid maximum in config element descriptor %s",
324 xbt_assert2(*(tok++) == '_',
325 "Invalid config element descriptor: %s%s", entry,
326 "; Should be <name>:<min nb>_to_<max nb>_<type>");
329 type < xbt_cfgelm_type_count
330 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
331 xbt_assert2(type < xbt_cfgelm_type_count,
332 "Invalid type in config element descriptor: %s%s", entry,
333 "; Should be one of 'string', 'int', 'peer' or 'double'.");
335 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
337 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
340 static int strcmp_voidp(const void *pa, const void *pb)
342 return strcmp(*(const char **)pa, *(const char **)pb);
345 /** @brief Displays the declared options and their description */
346 void xbt_cfg_help(xbt_cfg_t cfg)
348 xbt_dict_cursor_t dict_cursor;
349 unsigned int dynar_cursor;
350 xbt_cfgelm_t variable;
352 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
354 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
355 xbt_dynar_push(names, &name);
357 xbt_dynar_sort(names, strcmp_voidp);
359 xbt_dynar_foreach(names, dynar_cursor, name) {
362 variable = xbt_dict_get((xbt_dict_t )cfg, name);
364 printf(" %s: %s\n", name, variable->desc);
365 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
366 if (variable->min != 1 || variable->max != 1) {
367 if (variable->min == 0 && variable->max == 0)
368 printf("Arity: no bound; ");
370 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
372 printf("Current value: ");
373 size = xbt_dynar_length(variable->content);
375 switch (variable->type) {
382 for (i = 0; i < size; i++) {
383 ival = xbt_dynar_get_as(variable->content, i, int);
384 printf("%s%d\n", (i == 0 ? "" : " "), ival);
388 case xbt_cfgelm_double:
389 for (i = 0; i < size; i++) {
390 dval = xbt_dynar_get_as(variable->content, i, double);
391 printf("%s%f\n", (i == 0 ? "" : " "), dval);
395 case xbt_cfgelm_string:
396 for (i = 0; i < size; i++) {
397 sval = xbt_dynar_get_as(variable->content, i, char *);
398 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
402 case xbt_cfgelm_peer:
403 for (i = 0; i < size; i++) {
404 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
405 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
411 printf("Invalid type!!\n");
416 xbt_dynar_free(&names);
419 /** @brief Check that each variable have the right amount of values */
420 void xbt_cfg_check(xbt_cfg_t cfg)
422 xbt_dict_cursor_t cursor;
423 xbt_cfgelm_t variable;
427 xbt_assert0(cfg, "NULL config set.");
428 XBT_DEBUG("Check cfg set %p", cfg);
430 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
431 size = xbt_dynar_length(variable->content);
432 if (variable->min > size) {
433 xbt_dict_cursor_free(&cursor);
434 THROW4(mismatch_error, 0,
435 "Config elem %s needs at least %d %s, but there is only %d values.",
436 name, variable->min, xbt_cfgelm_type_name[variable->type],
440 if (variable->max > 0 && variable->max < size) {
441 xbt_dict_cursor_free(&cursor);
442 THROW4(mismatch_error, 0,
443 "Config elem %s accepts at most %d %s, but there is %d values.",
444 name, variable->max, xbt_cfgelm_type_name[variable->type],
449 xbt_dict_cursor_free(&cursor);
452 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
454 e_xbt_cfgelm_type_t type)
456 xbt_cfgelm_t res = NULL;
458 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
461 THROW1(not_found_error, 0,
462 "No registered variable '%s' in this config set", name);
465 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
466 "You tried to access to the config element %s as an %s, but its type is %s.",
468 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
473 /** @brief Get the type of this variable in that configuration set
475 * \arg cfg the config set
476 * \arg name the name of the element
477 * \arg type the result
481 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
484 xbt_cfgelm_t variable = NULL;
486 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
488 THROW1(not_found_error, 0,
489 "Can't get the type of '%s' since this variable does not exist",
492 XBT_INFO("type in variable = %d", variable->type);
494 return variable->type;
497 /*----[ Setting ]---------------------------------------------------------*/
498 /** @brief va_args version of xbt_cfg_set
500 * \arg cfg config set to fill
501 * \arg n variable name
502 * \arg pa variable value
504 * Add some values to the config set.
506 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
511 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
516 type = xbt_cfg_get_type(cfg, name);
518 if (e.category == not_found_error) {
520 THROW1(not_found_error, 0,
521 "Can't set the property '%s' since it's not registered",
528 case xbt_cfgelm_peer:
529 str = va_arg(pa, char *);
531 xbt_cfg_set_peer(cfg, name, str, i);
534 case xbt_cfgelm_string:
535 str = va_arg(pa, char *);
536 xbt_cfg_set_string(cfg, name, str);
541 xbt_cfg_set_int(cfg, name, i);
544 case xbt_cfgelm_double:
545 d = va_arg(pa, double);
546 xbt_cfg_set_double(cfg, name, d);
550 xbt_die("Config element variable %s not valid (type=%d)", name, type);
554 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
556 * \arg cfg config set to fill
557 * \arg name variable name
558 * \arg varargs variable value
561 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
566 xbt_cfg_set_vargs(cfg, name, pa);
570 /** @brief Add values parsed from a string into a config set
572 * \arg cfg config set to fill
573 * \arg options a string containing the content to add to the config set. This
574 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
575 * like "[name]:[value]" where [name] is the name of an already registred
576 * variable, and [value] conforms to the data type under which this variable was
579 * @todo This is a crude manual parser, it should be a proper lexer.
582 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
590 xbt_cfgelm_t variable = NULL;
591 char *optionlist_cpy;
592 char *option, *name, *val;
597 if (!options || !strlen(options)) { /* nothing to do */
600 optionlist_cpy = xbt_strdup(options);
602 XBT_DEBUG("List to parse and set:'%s'", options);
603 option = optionlist_cpy;
604 while (1) { /* breaks in the code */
610 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
611 name, len, (long) (option - name));
614 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
615 && *option != '\t' && *option != ',') {
616 XBT_DEBUG("Take %c.", *option);
619 if (option - name == len) {
620 XBT_DEBUG("Boundary=EOL");
621 option = NULL; /* don't do next iteration */
624 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
625 *option, len, (long) (option - name));
627 /* Pass the following blank chars */
629 while (option - name < (len - 1) &&
630 (*option == ' ' || *option == '\n' || *option == '\t')) {
631 /* fprintf(stderr,"Ignore a blank char.\n"); */
634 if (option - name == len - 1)
635 option = NULL; /* don't do next iteration */
637 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
639 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
644 val = strchr(name, ':');
646 free(optionlist_cpy);
647 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
652 if (strcmp(name,"contexts/factory"))
653 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
656 variable = xbt_dict_get((xbt_dict_t) cfg, name);
659 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
660 name = (char *) (optionlist_cpy - name + options);
661 free(optionlist_cpy);
662 if (e.category == not_found_error) {
664 THROW1(not_found_error, 0,
665 "No registered variable corresponding to '%s'.", name);
671 switch (variable->type) {
672 case xbt_cfgelm_string:
673 xbt_cfg_set_string(cfg, name, val); /* throws */
677 i = strtol(val, &val, 0);
679 free(optionlist_cpy);
680 xbt_die("Value of option %s not valid. Should be an integer", name);
683 xbt_cfg_set_int(cfg, name, i); /* throws */
686 case xbt_cfgelm_double:
687 d = strtod(val, &val);
689 free(optionlist_cpy);
690 xbt_die("Value of option %s not valid. Should be a double", name);
693 xbt_cfg_set_double(cfg, name, d); /* throws */
696 case xbt_cfgelm_peer:
698 val = strchr(val, ':');
700 free(optionlist_cpy);
701 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
706 i = strtol(val, &val, 0);
708 free(optionlist_cpy);
709 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
713 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
717 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
722 free(optionlist_cpy);
726 free(optionlist_cpy);
730 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
732 * This is useful to change the default value of a variable while allowing
733 * users to override it with command line arguments
735 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
737 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
739 if (variable->isdefault){
740 xbt_cfg_set_int(cfg, name, val);
741 variable->isdefault = 1;
745 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
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_double(xbt_cfg_t cfg, const char *name, double val)
756 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
758 if (variable->isdefault) {
759 xbt_cfg_set_double(cfg, name, val);
760 variable->isdefault = 1;
764 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
768 /** @brief Set a string 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_string(xbt_cfg_t cfg, const char *name,
776 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
778 if (variable->isdefault){
779 xbt_cfg_set_string(cfg, name, val);
780 variable->isdefault = 1;
784 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
788 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
790 * This is useful to change the default value of a variable while allowing
791 * users to override it with command line arguments
793 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
794 const char *host, int port)
796 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
798 if (variable->isdefault){
799 xbt_cfg_set_peer(cfg, name, host, port);
800 variable->isdefault = 1;
804 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
808 /** @brief Set or add an integer value to \a name within \a cfg
810 * \arg cfg the config set
811 * \arg name the name of the variable
812 * \arg val the value of the variable
814 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
816 xbt_cfgelm_t variable;
818 XBT_VERB("Configuration setting: %s=%d", name, val);
819 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
821 if (variable->max == 1) {
822 if (variable->cb_rm && xbt_dynar_length(variable->content))
823 (*variable->cb_rm) (name, 0);
825 xbt_dynar_set(variable->content, 0, &val);
828 && xbt_dynar_length(variable->content) ==
829 (unsigned long) variable->max)
830 THROW3(mismatch_error, 0,
831 "Cannot add value %d to the config element %s since it's already full (size=%d)",
832 val, name, variable->max);
834 xbt_dynar_push(variable->content, &val);
837 if (variable->cb_set)
838 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
839 variable->isdefault = 0;
842 /** @brief Set or add a double value to \a name within \a cfg
844 * \arg cfg the config set
845 * \arg name the name of the variable
846 * \arg val the doule to set
849 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
851 xbt_cfgelm_t variable;
853 XBT_VERB("Configuration setting: %s=%f", name, val);
854 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
856 if (variable->max == 1) {
857 if (variable->cb_rm && xbt_dynar_length(variable->content))
858 (*variable->cb_rm) (name, 0);
860 xbt_dynar_set(variable->content, 0, &val);
863 && xbt_dynar_length(variable->content) == variable->max)
864 THROW3(mismatch_error, 0,
865 "Cannot add value %f to the config element %s since it's already full (size=%d)",
866 val, name, variable->max);
868 xbt_dynar_push(variable->content, &val);
871 if (variable->cb_set)
872 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
873 variable->isdefault = 0;
876 /** @brief Set or add a string value to \a name within \a cfg
878 * \arg cfg the config set
879 * \arg name the name of the variable
880 * \arg val the value to be added
884 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
886 xbt_cfgelm_t variable;
887 char *newval = xbt_strdup(val);
889 XBT_VERB("Configuration setting: %s=%s", name, val);
890 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
891 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
892 variable->min, variable->max,
893 xbt_cfgelm_type_name[variable->type], variable->type, variable);
895 if (variable->max == 1) {
896 if (xbt_dynar_length(variable->content)) {
898 (*variable->cb_rm) (name, 0);
899 else if (variable->type == xbt_cfgelm_string) {
900 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
905 xbt_dynar_set(variable->content, 0, &newval);
908 && xbt_dynar_length(variable->content) == variable->max)
909 THROW3(mismatch_error, 0,
910 "Cannot add value %s to the config element %s since it's already full (size=%d)",
911 name, val, variable->max);
913 xbt_dynar_push(variable->content, &newval);
916 if (variable->cb_set)
917 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
918 variable->isdefault = 0;
921 /** @brief Set or add an peer value to \a name within \a cfg
923 * \arg cfg the config set
924 * \arg name the name of the variable
926 * \arg port the port number
928 * \e peer values are composed of a string (peername) and an integer (port)
932 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
935 xbt_cfgelm_t variable;
936 xbt_peer_t val = xbt_peer_new(peer, port);
938 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
940 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
942 if (variable->max == 1) {
943 if (variable->cb_rm && xbt_dynar_length(variable->content))
944 (*variable->cb_rm) (name, 0);
946 xbt_dynar_set(variable->content, 0, &val);
949 && xbt_dynar_length(variable->content) == variable->max)
950 THROW4(mismatch_error, 0,
951 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
952 peer, port, name, variable->max);
954 xbt_dynar_push(variable->content, &val);
957 if (variable->cb_set)
958 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
959 variable->isdefault = 0;
962 /* ---- [ Removing ] ---- */
964 /** @brief Remove the provided \e val integer value from a variable
966 * \arg cfg the config set
967 * \arg name the name of the variable
968 * \arg val the value to be removed
970 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
973 xbt_cfgelm_t variable;
977 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
979 if (xbt_dynar_length(variable->content) == variable->min)
980 THROW3(mismatch_error, 0,
981 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
982 val, name, variable->min);
984 xbt_dynar_foreach(variable->content, cpt, seen) {
987 (*variable->cb_rm) (name, cpt);
988 xbt_dynar_cursor_rm(variable->content, &cpt);
993 THROW2(not_found_error, 0,
994 "Can't remove the value %d of config element %s: value not found.",
998 /** @brief Remove the provided \e val double value from a variable
1000 * \arg cfg the config set
1001 * \arg name the name of the variable
1002 * \arg val the value to be removed
1005 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1007 xbt_cfgelm_t variable;
1011 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1013 if (xbt_dynar_length(variable->content) == variable->min)
1014 THROW3(mismatch_error, 0,
1015 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1016 val, name, variable->min);
1018 xbt_dynar_foreach(variable->content, cpt, seen) {
1020 xbt_dynar_cursor_rm(variable->content, &cpt);
1021 if (variable->cb_rm)
1022 (*variable->cb_rm) (name, cpt);
1027 THROW2(not_found_error, 0,
1028 "Can't remove the value %f of config element %s: value not found.",
1032 /** @brief Remove the provided \e val string value from a variable
1034 * \arg cfg the config set
1035 * \arg name the name of the variable
1036 * \arg val the value of the string which will be removed
1038 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1040 xbt_cfgelm_t variable;
1044 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1046 if (xbt_dynar_length(variable->content) == variable->min)
1047 THROW3(mismatch_error, 0,
1048 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1049 name, val, variable->min);
1051 xbt_dynar_foreach(variable->content, cpt, seen) {
1052 if (!strcpy(seen, val)) {
1053 if (variable->cb_rm)
1054 (*variable->cb_rm) (name, cpt);
1055 xbt_dynar_cursor_rm(variable->content, &cpt);
1060 THROW2(not_found_error, 0,
1061 "Can't remove the value %s of config element %s: value not found.",
1065 /** @brief Remove the provided \e val peer value from a variable
1067 * \arg cfg the config set
1068 * \arg name the name of the variable
1069 * \arg peer the peername
1070 * \arg port the port number
1074 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1077 xbt_cfgelm_t variable;
1081 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1083 if (xbt_dynar_length(variable->content) == variable->min)
1084 THROW4(mismatch_error, 0,
1085 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1086 peer, port, name, variable->min);
1088 xbt_dynar_foreach(variable->content, cpt, seen) {
1089 if (!strcpy(seen->name, peer) && seen->port == port) {
1090 if (variable->cb_rm)
1091 (*variable->cb_rm) (name, cpt);
1092 xbt_dynar_cursor_rm(variable->content, &cpt);
1097 THROW3(not_found_error, 0,
1098 "Can't remove the value %s:%d of config element %s: value not found.",
1102 /** @brief Remove the \e pos th value from the provided variable */
1104 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1107 xbt_cfgelm_t variable;
1109 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1111 if (xbt_dynar_length(variable->content) == variable->min)
1112 THROW3(mismatch_error, 0,
1113 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1114 pos, name, variable->min);
1116 if (variable->cb_rm)
1117 (*variable->cb_rm) (name, pos);
1118 xbt_dynar_remove_at(variable->content, pos, NULL);
1121 /** @brief Remove all the values from a variable
1123 * \arg cfg the config set
1124 * \arg name the name of the variable
1127 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1129 xbt_cfgelm_t variable = NULL;
1133 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1135 if (e.category != not_found_error)
1139 THROW1(not_found_error, 0,
1140 "Can't empty '%s' since this config element does not exist",
1145 if (variable->cb_rm) {
1148 xbt_dynar_foreach(variable->content, cpt, ignored) {
1149 (*variable->cb_rm) (name, cpt);
1152 xbt_dynar_reset(variable->content);
1156 * Say if the value is the default value
1158 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1160 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1161 return variable->isdefault;
1164 /*----[ Getting ]---------------------------------------------------------*/
1166 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1168 * \arg cfg the config set
1169 * \arg name the name of the variable
1170 * \arg val the wanted value
1172 * Returns the first value from the config set under the given name.
1173 * If there is more than one value, it will issue a warning. Consider using
1174 * xbt_cfg_get_dynar() instead.
1176 * \warning the returned value is the actual content of the config set
1178 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1180 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1182 if (xbt_dynar_length(variable->content) > 1) {
1184 ("You asked for the first value of the config element '%s', but there is %lu values",
1185 name, xbt_dynar_length(variable->content));
1188 return xbt_dynar_get_as(variable->content, 0, int);
1191 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1193 * \arg cfg the config set
1194 * \arg name the name of the variable
1195 * \arg val the wanted value
1197 * Returns the first value from the config set under the given name.
1198 * If there is more than one value, it will issue a warning. Consider using
1199 * xbt_cfg_get_dynar() instead.
1201 * \warning the returned value is the actual content of the config set
1204 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1206 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1208 if (xbt_dynar_length(variable->content) > 1) {
1210 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1211 name, xbt_dynar_length(variable->content));
1214 return xbt_dynar_get_as(variable->content, 0, double);
1217 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1219 * \arg th the config set
1220 * \arg name the name of the variable
1221 * \arg val the wanted value
1223 * Returns the first value from the config set under the given name.
1224 * If there is more than one value, it will issue a warning. Consider using
1225 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1227 * \warning the returned value is the actual content of the config set
1230 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1232 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1234 if (xbt_dynar_length(variable->content) > 1) {
1236 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1237 name, xbt_dynar_length(variable->content));
1238 } else if (xbt_dynar_length(variable->content) == 0) {
1242 return xbt_dynar_get_as(variable->content, 0, char *);
1245 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1247 * \arg cfg the config set
1248 * \arg name the name of the variable
1249 * \arg peer the peer
1250 * \arg port the port number
1252 * Returns the first value from the config set under the given name.
1253 * If there is more than one value, it will issue a warning. Consider using
1254 * xbt_cfg_get_dynar() instead.
1256 * \warning the returned value is the actual content of the config set
1259 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1262 xbt_cfgelm_t variable;
1265 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1267 if (xbt_dynar_length(variable->content) > 1) {
1269 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1270 name, xbt_dynar_length(variable->content));
1273 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1278 /** @brief Retrieve the dynar of all the values stored in a variable
1280 * \arg cfg where to search in
1281 * \arg name what to search for
1284 * Get the data stored in the config set.
1286 * \warning the returned value is the actual content of the config set
1288 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1290 xbt_cfgelm_t variable = NULL;
1294 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1296 if (e.category == not_found_error) {
1298 THROW1(not_found_error, 0,
1299 "No registered variable %s in this config set", name);
1304 return variable->content;
1308 /** @brief Retrieve one of the integer value of a variable */
1309 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1312 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1313 return xbt_dynar_get_as(variable->content, pos, int);
1316 /** @brief Retrieve one of the double value of a variable */
1317 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1320 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1321 return xbt_dynar_get_as(variable->content, pos, double);
1325 /** @brief Retrieve one of the string value of a variable */
1326 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1329 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1330 return xbt_dynar_get_as(variable->content, pos, char *);
1333 /** @brief Retrieve one of the peer value of a variable */
1335 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1336 char **peer, int *port)
1339 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1340 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1351 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1352 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1354 XBT_TEST_SUITE("config", "Configuration support");
1356 static xbt_cfg_t make_set()
1358 xbt_cfg_t set = NULL;
1360 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1361 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1362 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1363 xbt_cfg_register_str(&set, "user:1_to_10_string");
1366 } /* end_of_make_set */
1368 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1370 xbt_cfg_t set = make_set();
1371 xbt_test_add0("Alloc and free a config set");
1372 xbt_cfg_set_parse(set,
1373 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1378 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1380 xbt_cfg_t set = set = make_set();
1383 xbt_test_add0("Having too few elements for speed");
1384 xbt_cfg_set_parse(set,
1385 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1390 if (e.category != mismatch_error ||
1391 strncmp(e.msg, "Config elem speed needs",
1392 strlen("Config elem speed needs")))
1393 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1401 xbt_test_add0("Having too much values of 'speed'");
1403 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1405 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1408 if (e.category != mismatch_error ||
1409 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1410 strlen("Config elem speed needs")))
1411 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1420 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1423 xbt_test_add0("Get a single value");
1425 /* get_single_value */
1427 xbt_cfg_t myset = make_set();
1429 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1430 ival = xbt_cfg_get_int(myset, "speed");
1432 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1433 xbt_cfg_free(&myset);
1436 xbt_test_add0("Get multiple values");
1438 /* get_multiple_value */
1440 xbt_cfg_t myset = make_set();
1442 xbt_cfg_set_parse(myset,
1443 "peername:veloce user:foo\nuser:bar\tuser:toto");
1444 xbt_cfg_set_parse(myset, "speed:42");
1445 xbt_cfg_check(myset);
1446 dyn = xbt_cfg_get_dynar(myset, "user");
1448 if (xbt_dynar_length(dyn) != 3)
1449 xbt_test_fail1("Dynar length = %lu, I expected 3",
1450 xbt_dynar_length(dyn));
1452 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1453 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1454 xbt_dynar_get_as(dyn, 0, char *));
1456 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1457 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1458 xbt_dynar_get_as(dyn, 1, char *));
1460 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1461 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1462 xbt_dynar_get_as(dyn, 2, char *));
1463 xbt_cfg_free(&myset);
1466 xbt_test_add0("Access to a non-existant entry");
1468 /* non-existant_entry */
1469 xbt_cfg_t myset = make_set();
1473 xbt_cfg_set_parse(myset, "color:blue");
1475 if (e.category != not_found_error)
1476 xbt_test_exception(e);
1479 xbt_cfg_free(&myset);
1482 #endif /* SIMGRID_TEST */