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_assert(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_assert(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_assert(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_assert(tok, "Invalid minimum in config element descriptor %s",
315 xbt_assert(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_assert(tok, "Invalid maximum in config element descriptor %s",
324 xbt_assert(*tok == '_',
325 "Invalid config element descriptor: %s%s", entry,
326 "; Should be <name>:<min nb>_to_<max nb>_<type>");
330 type < xbt_cfgelm_type_count
331 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
332 xbt_assert(type < xbt_cfgelm_type_count,
333 "Invalid type in config element descriptor: %s%s", entry,
334 "; Should be one of 'string', 'int', 'peer' or 'double'.");
336 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
338 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
341 static int strcmp_voidp(const void *pa, const void *pb)
343 return strcmp(*(const char **)pa, *(const char **)pb);
346 /** @brief Displays the declared options and their description */
347 void xbt_cfg_help(xbt_cfg_t cfg)
349 xbt_dict_cursor_t dict_cursor;
350 unsigned int dynar_cursor;
351 xbt_cfgelm_t variable;
353 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
355 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
356 xbt_dynar_push(names, &name);
358 xbt_dynar_sort(names, strcmp_voidp);
360 xbt_dynar_foreach(names, dynar_cursor, name) {
363 variable = xbt_dict_get((xbt_dict_t )cfg, name);
365 printf(" %s: %s\n", name, variable->desc);
366 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
367 if (variable->min != 1 || variable->max != 1) {
368 if (variable->min == 0 && variable->max == 0)
369 printf("Arity: no bound; ");
371 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
373 printf("Current value: ");
374 size = xbt_dynar_length(variable->content);
376 switch (variable->type) {
383 for (i = 0; i < size; i++) {
384 ival = xbt_dynar_get_as(variable->content, i, int);
385 printf("%s%d\n", (i == 0 ? "" : " "), ival);
389 case xbt_cfgelm_double:
390 for (i = 0; i < size; i++) {
391 dval = xbt_dynar_get_as(variable->content, i, double);
392 printf("%s%f\n", (i == 0 ? "" : " "), dval);
396 case xbt_cfgelm_string:
397 for (i = 0; i < size; i++) {
398 sval = xbt_dynar_get_as(variable->content, i, char *);
399 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
403 case xbt_cfgelm_peer:
404 for (i = 0; i < size; i++) {
405 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
406 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
412 printf("Invalid type!!\n");
417 xbt_dynar_free(&names);
420 /** @brief Check that each variable have the right amount of values */
421 void xbt_cfg_check(xbt_cfg_t cfg)
423 xbt_dict_cursor_t cursor;
424 xbt_cfgelm_t variable;
428 xbt_assert(cfg, "NULL config set.");
429 XBT_DEBUG("Check cfg set %p", cfg);
431 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
432 size = xbt_dynar_length(variable->content);
433 if (variable->min > size) {
434 xbt_dict_cursor_free(&cursor);
435 THROWF(mismatch_error, 0,
436 "Config elem %s needs at least %d %s, but there is only %d values.",
437 name, variable->min, xbt_cfgelm_type_name[variable->type],
441 if (variable->max > 0 && variable->max < size) {
442 xbt_dict_cursor_free(&cursor);
443 THROWF(mismatch_error, 0,
444 "Config elem %s accepts at most %d %s, but there is %d values.",
445 name, variable->max, xbt_cfgelm_type_name[variable->type],
450 xbt_dict_cursor_free(&cursor);
453 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
455 e_xbt_cfgelm_type_t type)
457 xbt_cfgelm_t res = NULL;
459 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
462 THROWF(not_found_error, 0,
463 "No registered variable '%s' in this config set", name);
466 xbt_assert(type == xbt_cfgelm_any || res->type == type,
467 "You tried to access to the config element %s as an %s, but its type is %s.",
469 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
474 /** @brief Get the type of this variable in that configuration set
476 * \arg cfg the config set
477 * \arg name the name of the element
478 * \arg type the result
482 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
485 xbt_cfgelm_t variable = NULL;
487 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
489 THROWF(not_found_error, 0,
490 "Can't get the type of '%s' since this variable does not exist",
493 XBT_INFO("type in variable = %d", variable->type);
495 return variable->type;
498 /*----[ Setting ]---------------------------------------------------------*/
499 /** @brief va_args version of xbt_cfg_set
501 * \arg cfg config set to fill
502 * \arg n variable name
503 * \arg pa variable value
505 * Add some values to the config set.
507 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
512 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
517 type = xbt_cfg_get_type(cfg, name);
520 if (e.category == not_found_error) {
522 THROWF(not_found_error, 0,
523 "Can't set the property '%s' since it's not registered",
530 case xbt_cfgelm_peer:
531 str = va_arg(pa, char *);
533 xbt_cfg_set_peer(cfg, name, str, i);
536 case xbt_cfgelm_string:
537 str = va_arg(pa, char *);
538 xbt_cfg_set_string(cfg, name, str);
543 xbt_cfg_set_int(cfg, name, i);
546 case xbt_cfgelm_double:
547 d = va_arg(pa, double);
548 xbt_cfg_set_double(cfg, name, d);
552 xbt_die("Config element variable %s not valid (type=%d)", name, type);
556 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
558 * \arg cfg config set to fill
559 * \arg name variable name
560 * \arg varargs variable value
563 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
568 xbt_cfg_set_vargs(cfg, name, pa);
572 /** @brief Add values parsed from a string into a config set
574 * \arg cfg config set to fill
575 * \arg options a string containing the content to add to the config set. This
576 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
577 * like "[name]:[value]" where [name] is the name of an already registred
578 * variable, and [value] conforms to the data type under which this variable was
581 * @todo This is a crude manual parser, it should be a proper lexer.
584 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
592 volatile xbt_cfgelm_t variable = NULL;
593 char *optionlist_cpy;
594 char *option, *name, *val;
599 if (!options || !strlen(options)) { /* nothing to do */
602 optionlist_cpy = xbt_strdup(options);
604 XBT_DEBUG("List to parse and set:'%s'", options);
605 option = optionlist_cpy;
606 while (1) { /* breaks in the code */
612 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
613 name, len, (long) (option - name));
616 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
617 && *option != '\t' && *option != ',') {
618 XBT_DEBUG("Take %c.", *option);
621 if (option - name == len) {
622 XBT_DEBUG("Boundary=EOL");
623 option = NULL; /* don't do next iteration */
626 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
627 *option, len, (long) (option - name));
629 /* Pass the following blank chars */
631 while (option - name < (len - 1) &&
632 (*option == ' ' || *option == '\n' || *option == '\t')) {
633 /* fprintf(stderr,"Ignore a blank char.\n"); */
636 if (option - name == len - 1)
637 option = NULL; /* don't do next iteration */
639 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
641 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
646 val = strchr(name, ':');
648 free(optionlist_cpy);
649 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
654 if (strncmp(name, "contexts/", strlen("contexts/")))
655 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
658 variable = xbt_dict_get((xbt_dict_t) cfg, name);
661 if (e.category == not_found_error) {
663 name = xbt_strdup(name);
664 free(optionlist_cpy);
665 THROWF(not_found_error, 0,
666 "No registered variable corresponding to '%s'.", name);
668 free(optionlist_cpy);
673 switch (variable->type) {
674 case xbt_cfgelm_string:
675 xbt_cfg_set_string(cfg, name, val); /* throws */
679 i = strtol(val, &val, 0);
681 free(optionlist_cpy);
682 xbt_die("Value of option %s not valid. Should be an integer", name);
685 xbt_cfg_set_int(cfg, name, i); /* throws */
688 case xbt_cfgelm_double:
689 d = strtod(val, &val);
691 free(optionlist_cpy);
692 xbt_die("Value of option %s not valid. Should be a double", name);
695 xbt_cfg_set_double(cfg, name, d); /* throws */
698 case xbt_cfgelm_peer:
700 val = strchr(val, ':');
702 free(optionlist_cpy);
703 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
708 i = strtol(val, &val, 0);
710 free(optionlist_cpy);
711 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
715 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
719 THROWF(unknown_error, 0, "Type of config element %s is not valid.",
724 free(optionlist_cpy);
728 free(optionlist_cpy);
732 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
734 * This is useful to change the default value of a variable while allowing
735 * users to override it with command line arguments
737 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
739 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
741 if (variable->isdefault){
742 xbt_cfg_set_int(cfg, name, val);
743 variable->isdefault = 1;
747 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
751 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
753 * This is useful to change the default value of a variable while allowing
754 * users to override it with command line arguments
756 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
758 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
760 if (variable->isdefault) {
761 xbt_cfg_set_double(cfg, name, val);
762 variable->isdefault = 1;
766 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
770 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
772 * This is useful to change the default value of a variable while allowing
773 * users to override it with command line arguments
775 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
778 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
780 if (variable->isdefault){
781 xbt_cfg_set_string(cfg, name, val);
782 variable->isdefault = 1;
786 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
790 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
792 * This is useful to change the default value of a variable while allowing
793 * users to override it with command line arguments
795 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
796 const char *host, int port)
798 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
800 if (variable->isdefault){
801 xbt_cfg_set_peer(cfg, name, host, port);
802 variable->isdefault = 1;
806 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
810 /** @brief Set or add an integer value to \a name within \a cfg
812 * \arg cfg the config set
813 * \arg name the name of the variable
814 * \arg val the value of the variable
816 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
818 xbt_cfgelm_t variable;
820 XBT_VERB("Configuration setting: %s=%d", name, val);
821 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
823 if (variable->max == 1) {
824 if (variable->cb_rm && xbt_dynar_length(variable->content))
825 (*variable->cb_rm) (name, 0);
827 xbt_dynar_set(variable->content, 0, &val);
830 && xbt_dynar_length(variable->content) ==
831 (unsigned long) variable->max)
832 THROWF(mismatch_error, 0,
833 "Cannot add value %d to the config element %s since it's already full (size=%d)",
834 val, name, variable->max);
836 xbt_dynar_push(variable->content, &val);
839 if (variable->cb_set)
840 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
841 variable->isdefault = 0;
844 /** @brief Set or add a double value to \a name within \a cfg
846 * \arg cfg the config set
847 * \arg name the name of the variable
848 * \arg val the doule to set
851 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
853 xbt_cfgelm_t variable;
855 XBT_VERB("Configuration setting: %s=%f", name, val);
856 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
858 if (variable->max == 1) {
859 if (variable->cb_rm && xbt_dynar_length(variable->content))
860 (*variable->cb_rm) (name, 0);
862 xbt_dynar_set(variable->content, 0, &val);
865 && xbt_dynar_length(variable->content) == variable->max)
866 THROWF(mismatch_error, 0,
867 "Cannot add value %f to the config element %s since it's already full (size=%d)",
868 val, name, variable->max);
870 xbt_dynar_push(variable->content, &val);
873 if (variable->cb_set)
874 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
875 variable->isdefault = 0;
878 /** @brief Set or add a string value to \a name within \a cfg
880 * \arg cfg the config set
881 * \arg name the name of the variable
882 * \arg val the value to be added
886 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
888 xbt_cfgelm_t variable;
889 char *newval = xbt_strdup(val);
891 XBT_VERB("Configuration setting: %s=%s", name, val);
892 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
893 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
894 variable->min, variable->max,
895 xbt_cfgelm_type_name[variable->type], variable->type, variable);
897 if (variable->max == 1) {
898 if (xbt_dynar_length(variable->content)) {
900 (*variable->cb_rm) (name, 0);
901 else if (variable->type == xbt_cfgelm_string) {
902 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
907 xbt_dynar_set(variable->content, 0, &newval);
910 && xbt_dynar_length(variable->content) == variable->max)
911 THROWF(mismatch_error, 0,
912 "Cannot add value %s to the config element %s since it's already full (size=%d)",
913 name, val, variable->max);
915 xbt_dynar_push(variable->content, &newval);
918 if (variable->cb_set)
919 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
920 variable->isdefault = 0;
923 /** @brief Set or add an peer value to \a name within \a cfg
925 * \arg cfg the config set
926 * \arg name the name of the variable
928 * \arg port the port number
930 * \e peer values are composed of a string (peername) and an integer (port)
934 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
937 xbt_cfgelm_t variable;
938 xbt_peer_t val = xbt_peer_new(peer, port);
940 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
942 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
944 if (variable->max == 1) {
945 if (variable->cb_rm && xbt_dynar_length(variable->content))
946 (*variable->cb_rm) (name, 0);
948 xbt_dynar_set(variable->content, 0, &val);
951 && xbt_dynar_length(variable->content) == variable->max)
952 THROWF(mismatch_error, 0,
953 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
954 peer, port, name, variable->max);
956 xbt_dynar_push(variable->content, &val);
959 if (variable->cb_set)
960 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
961 variable->isdefault = 0;
964 /* ---- [ Removing ] ---- */
966 /** @brief Remove the provided \e val integer value from a variable
968 * \arg cfg the config set
969 * \arg name the name of the variable
970 * \arg val the value to be removed
972 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
975 xbt_cfgelm_t variable;
979 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
981 if (xbt_dynar_length(variable->content) == variable->min)
982 THROWF(mismatch_error, 0,
983 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
984 val, name, variable->min);
986 xbt_dynar_foreach(variable->content, cpt, seen) {
989 (*variable->cb_rm) (name, cpt);
990 xbt_dynar_cursor_rm(variable->content, &cpt);
995 THROWF(not_found_error, 0,
996 "Can't remove the value %d of config element %s: value not found.",
1000 /** @brief Remove the provided \e val double value from a variable
1002 * \arg cfg the config set
1003 * \arg name the name of the variable
1004 * \arg val the value to be removed
1007 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1009 xbt_cfgelm_t variable;
1013 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1015 if (xbt_dynar_length(variable->content) == variable->min)
1016 THROWF(mismatch_error, 0,
1017 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1018 val, name, variable->min);
1020 xbt_dynar_foreach(variable->content, cpt, seen) {
1022 xbt_dynar_cursor_rm(variable->content, &cpt);
1023 if (variable->cb_rm)
1024 (*variable->cb_rm) (name, cpt);
1029 THROWF(not_found_error, 0,
1030 "Can't remove the value %f of config element %s: value not found.",
1034 /** @brief Remove the provided \e val string value from a variable
1036 * \arg cfg the config set
1037 * \arg name the name of the variable
1038 * \arg val the value of the string which will be removed
1040 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1042 xbt_cfgelm_t variable;
1046 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1048 if (xbt_dynar_length(variable->content) == variable->min)
1049 THROWF(mismatch_error, 0,
1050 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1051 name, val, variable->min);
1053 xbt_dynar_foreach(variable->content, cpt, seen) {
1054 if (!strcpy(seen, val)) {
1055 if (variable->cb_rm)
1056 (*variable->cb_rm) (name, cpt);
1057 xbt_dynar_cursor_rm(variable->content, &cpt);
1062 THROWF(not_found_error, 0,
1063 "Can't remove the value %s of config element %s: value not found.",
1067 /** @brief Remove the provided \e val peer value from a variable
1069 * \arg cfg the config set
1070 * \arg name the name of the variable
1071 * \arg peer the peername
1072 * \arg port the port number
1076 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1079 xbt_cfgelm_t variable;
1083 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1085 if (xbt_dynar_length(variable->content) == variable->min)
1086 THROWF(mismatch_error, 0,
1087 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1088 peer, port, name, variable->min);
1090 xbt_dynar_foreach(variable->content, cpt, seen) {
1091 if (!strcpy(seen->name, peer) && seen->port == port) {
1092 if (variable->cb_rm)
1093 (*variable->cb_rm) (name, cpt);
1094 xbt_dynar_cursor_rm(variable->content, &cpt);
1099 THROWF(not_found_error, 0,
1100 "Can't remove the value %s:%d of config element %s: value not found.",
1104 /** @brief Remove the \e pos th value from the provided variable */
1106 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1109 xbt_cfgelm_t variable;
1111 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1113 if (xbt_dynar_length(variable->content) == variable->min)
1114 THROWF(mismatch_error, 0,
1115 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1116 pos, name, variable->min);
1118 if (variable->cb_rm)
1119 (*variable->cb_rm) (name, pos);
1120 xbt_dynar_remove_at(variable->content, pos, NULL);
1123 /** @brief Remove all the values from a variable
1125 * \arg cfg the config set
1126 * \arg name the name of the variable
1129 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1131 xbt_cfgelm_t variable = NULL;
1135 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1138 if (e.category != not_found_error)
1142 THROWF(not_found_error, 0,
1143 "Can't empty '%s' since this config element does not exist",
1148 if (variable->cb_rm) {
1151 xbt_dynar_foreach(variable->content, cpt, ignored) {
1152 (*variable->cb_rm) (name, cpt);
1155 xbt_dynar_reset(variable->content);
1159 * Say if the value is the default value
1161 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1163 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1164 return variable->isdefault;
1167 /*----[ Getting ]---------------------------------------------------------*/
1169 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1171 * \arg cfg the config set
1172 * \arg name the name of the variable
1173 * \arg val the wanted value
1175 * Returns the first value from the config set under the given name.
1176 * If there is more than one value, it will issue a warning. Consider using
1177 * xbt_cfg_get_dynar() instead.
1179 * \warning the returned value is the actual content of the config set
1181 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1183 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1185 if (xbt_dynar_length(variable->content) > 1) {
1187 ("You asked for the first value of the config element '%s', but there is %lu values",
1188 name, xbt_dynar_length(variable->content));
1191 return xbt_dynar_get_as(variable->content, 0, int);
1194 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1196 * \arg cfg the config set
1197 * \arg name the name of the variable
1198 * \arg val the wanted value
1200 * Returns the first value from the config set under the given name.
1201 * If there is more than one value, it will issue a warning. Consider using
1202 * xbt_cfg_get_dynar() instead.
1204 * \warning the returned value is the actual content of the config set
1207 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1209 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1211 if (xbt_dynar_length(variable->content) > 1) {
1213 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1214 name, xbt_dynar_length(variable->content));
1217 return xbt_dynar_get_as(variable->content, 0, double);
1220 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1222 * \arg th the config set
1223 * \arg name the name of the variable
1224 * \arg val the wanted value
1226 * Returns the first value from the config set under the given name.
1227 * If there is more than one value, it will issue a warning. Consider using
1228 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1230 * \warning the returned value is the actual content of the config set
1233 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1235 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1237 if (xbt_dynar_length(variable->content) > 1) {
1239 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1240 name, xbt_dynar_length(variable->content));
1241 } else if (xbt_dynar_length(variable->content) == 0) {
1245 return xbt_dynar_get_as(variable->content, 0, char *);
1248 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1250 * \arg cfg the config set
1251 * \arg name the name of the variable
1252 * \arg peer the peer
1253 * \arg port the port number
1255 * Returns the first value from the config set under the given name.
1256 * If there is more than one value, it will issue a warning. Consider using
1257 * xbt_cfg_get_dynar() instead.
1259 * \warning the returned value is the actual content of the config set
1262 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1265 xbt_cfgelm_t variable;
1268 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1270 if (xbt_dynar_length(variable->content) > 1) {
1272 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1273 name, xbt_dynar_length(variable->content));
1276 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1281 /** @brief Retrieve the dynar of all the values stored in a variable
1283 * \arg cfg where to search in
1284 * \arg name what to search for
1287 * Get the data stored in the config set.
1289 * \warning the returned value is the actual content of the config set
1291 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1293 xbt_cfgelm_t variable = NULL;
1297 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1300 if (e.category == not_found_error) {
1302 THROWF(not_found_error, 0,
1303 "No registered variable %s in this config set", name);
1308 return variable->content;
1312 /** @brief Retrieve one of the integer value of a variable */
1313 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1316 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1317 return xbt_dynar_get_as(variable->content, pos, int);
1320 /** @brief Retrieve one of the double value of a variable */
1321 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1324 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1325 return xbt_dynar_get_as(variable->content, pos, double);
1329 /** @brief Retrieve one of the string value of a variable */
1330 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1333 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1334 return xbt_dynar_get_as(variable->content, pos, char *);
1337 /** @brief Retrieve one of the peer value of a variable */
1339 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1340 char **peer, int *port)
1343 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1344 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1355 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1356 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1358 XBT_TEST_SUITE("config", "Configuration support");
1360 static xbt_cfg_t make_set()
1362 xbt_cfg_t set = NULL;
1364 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1365 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1366 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1367 xbt_cfg_register_str(&set, "user:1_to_10_string");
1370 } /* end_of_make_set */
1372 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1374 xbt_cfg_t set = make_set();
1375 xbt_test_add("Alloc and free a config set");
1376 xbt_cfg_set_parse(set,
1377 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1382 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1384 xbt_cfg_t set = set = make_set();
1387 xbt_test_add("Having too few elements for speed");
1388 xbt_cfg_set_parse(set,
1389 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1394 if (e.category != mismatch_error ||
1395 strncmp(e.msg, "Config elem speed needs",
1396 strlen("Config elem speed needs")))
1397 xbt_test_fail("Got an exception. msg=%s", e.msg);
1405 xbt_test_add("Having too much values of 'speed'");
1407 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1409 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1412 if (e.category != mismatch_error ||
1413 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1414 strlen("Config elem speed needs")))
1415 xbt_test_fail("Got an exception. msg=%s", e.msg);
1424 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1427 xbt_test_add("Get a single value");
1429 /* get_single_value */
1431 xbt_cfg_t myset = make_set();
1433 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1434 ival = xbt_cfg_get_int(myset, "speed");
1436 xbt_test_fail("Speed value = %d, I expected 42", ival);
1437 xbt_cfg_free(&myset);
1440 xbt_test_add("Get multiple values");
1442 /* get_multiple_value */
1444 xbt_cfg_t myset = make_set();
1446 xbt_cfg_set_parse(myset,
1447 "peername:veloce user:foo\nuser:bar\tuser:toto");
1448 xbt_cfg_set_parse(myset, "speed:42");
1449 xbt_cfg_check(myset);
1450 dyn = xbt_cfg_get_dynar(myset, "user");
1452 if (xbt_dynar_length(dyn) != 3)
1453 xbt_test_fail("Dynar length = %lu, I expected 3",
1454 xbt_dynar_length(dyn));
1456 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1457 xbt_test_fail("Dynar[0] = %s, I expected foo",
1458 xbt_dynar_get_as(dyn, 0, char *));
1460 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1461 xbt_test_fail("Dynar[1] = %s, I expected bar",
1462 xbt_dynar_get_as(dyn, 1, char *));
1464 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1465 xbt_test_fail("Dynar[2] = %s, I expected toto",
1466 xbt_dynar_get_as(dyn, 2, char *));
1467 xbt_cfg_free(&myset);
1470 xbt_test_add("Access to a non-existant entry");
1472 /* non-existant_entry */
1473 xbt_cfg_t myset = make_set();
1477 xbt_cfg_set_parse(myset, "color:blue");
1480 if (e.category != not_found_error)
1481 xbt_test_exception(e);
1484 xbt_cfg_free(&myset);
1487 #endif /* SIMGRID_TEST */