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.h" /* COORD_HOST_LEVEL and COORD_ASR_LEVEL */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
25 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27 Both data and DTD are mixed, but fixing it now would prevent me to ever
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
44 (cannot be an union because type peer uses both str and i) */
46 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
48 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
49 { "int", "double", "string", "peer", "any" };
51 /* Internal stuff used in cache to free a variable */
52 static void xbt_cfgelm_free(void *data);
54 /* Retrieve the variable we'll modify */
55 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
56 e_xbt_cfgelm_type_t type);
58 /*----[ Memory management ]-----------------------------------------------*/
60 /** @brief Constructor
62 * Initialise an config set
66 xbt_cfg_t xbt_cfg_new(void)
68 return (xbt_cfg_t) xbt_dict_new();
71 /** \brief Copy an existing configuration set
73 * \arg whereto the config set to be created
74 * \arg tocopy the source data
76 * This only copy the registrations, not the actual content
79 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
81 xbt_dict_cursor_t cursor = NULL;
82 xbt_cfgelm_t variable = NULL;
85 XBT_DEBUG("Copy cfg set %p", tocopy);
87 xbt_assert(tocopy, "cannot copy NULL config");
89 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
90 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
91 variable->min, variable->max, variable->cb_set,
96 /** @brief Destructor */
97 void xbt_cfg_free(xbt_cfg_t * cfg)
99 XBT_DEBUG("Frees cfg set %p", cfg);
100 xbt_dict_free((xbt_dict_t *) cfg);
103 /** @brief Dump a config set for debuging purpose
105 * \arg name The name to give to this config set
106 * \arg indent what to write at the begining of each line (right number of spaces)
107 * \arg cfg the config set
109 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
111 xbt_dict_t dict = (xbt_dict_t) cfg;
112 xbt_dict_cursor_t cursor = NULL;
113 xbt_cfgelm_t variable = NULL;
123 printf("%s>> Dumping of the config set '%s':\n", indent, name);
124 xbt_dict_foreach(dict, cursor, key, variable) {
126 printf("%s %s:", indent, key);
128 size = xbt_dynar_length(variable->content);
130 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
131 variable->min, variable->max,
132 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
135 switch (variable->type) {
138 for (i = 0; i < size; i++) {
139 ival = xbt_dynar_get_as(variable->content, i, int);
140 printf("%s %d\n", indent, ival);
144 case xbt_cfgelm_double:
145 for (i = 0; i < size; i++) {
146 dval = xbt_dynar_get_as(variable->content, i, double);
147 printf("%s %f\n", indent, dval);
151 case xbt_cfgelm_string:
152 for (i = 0; i < size; i++) {
153 sval = xbt_dynar_get_as(variable->content, i, char *);
154 printf("%s %s\n", indent, sval);
158 case xbt_cfgelm_peer:
159 for (i = 0; i < size; i++) {
160 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
161 printf("%s %s:%d\n", indent, hval->name, hval->port);
166 printf("%s Invalid type!!\n", indent);
172 printf("%s<< End of the config set '%s'\n", indent, name);
175 xbt_dict_cursor_free(&cursor);
180 * free an config element
183 void xbt_cfgelm_free(void *data)
185 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
187 XBT_DEBUG("Frees cfgelm %p", c);
191 xbt_dynar_free(&(c->content));
195 /*----[ Registering stuff ]-----------------------------------------------*/
197 /** @brief Register an element within a config set
199 * @arg cfg the config set
200 * @arg type the type of the config element
201 * @arg min the minimum
202 * @arg max the maximum
206 xbt_cfg_register(xbt_cfg_t * cfg,
207 const char *name, const char *desc,
208 e_xbt_cfgelm_type_t type, void *default_value, int min,
209 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
214 *cfg = xbt_cfg_new();
215 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
216 "type of %s not valid (%d should be between %d and %d)",
217 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
218 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
221 XBT_WARN("Config elem %s registered twice.", name);
222 /* Will be removed by the insertion of the new one */
225 res = xbt_new(s_xbt_cfgelm_t, 1);
226 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
227 name, desc, min, max, xbt_cfgelm_type_name[type], type, res,
230 res->desc = xbt_strdup(desc);
234 res->cb_set = cb_set;
240 res->content = xbt_dynar_new(sizeof(int), NULL);
242 xbt_dynar_push(res->content, default_value);
245 case xbt_cfgelm_double:
246 res->content = xbt_dynar_new(sizeof(double), NULL);
248 xbt_dynar_push(res->content, default_value);
251 case xbt_cfgelm_string:
252 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
254 xbt_dynar_push(res->content, default_value);
257 case xbt_cfgelm_peer:
258 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
260 xbt_dynar_push(res->content, default_value);
264 XBT_ERROR("%d is an invalide type code", type);
267 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
270 /** @brief Unregister an element from a config set.
272 * @arg cfg the config set
273 * @arg name the name of the elem to be freed
275 * Note that it removes both the description and the actual content.
276 * Throws not_found when no such element exists.
279 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
281 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
282 xbt_dict_remove((xbt_dict_t) cfg, name);
286 * @brief Parse a string and register the stuff described.
288 * @arg cfg the config set
289 * @arg entry a string describing the element to register
291 * The string may consist in several variable descriptions separated by a space.
292 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
293 * with type being one of 'string','int', 'peer' or 'double'.
295 * @fixme: this does not allow to set the description
298 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
300 char *entrycpy = xbt_strdup(entry);
304 e_xbt_cfgelm_type_t type;
305 XBT_DEBUG("Register string '%s'", entry);
307 tok = strchr(entrycpy, ':');
308 xbt_assert(tok, "Invalid config element descriptor: %s%s",
309 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
312 min = strtol(tok, &tok, 10);
313 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
316 xbt_assert(strcmp(tok, "_to_"),
317 "Invalid config element descriptor : %s%s",
318 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
319 tok += strlen("_to_");
321 max = strtol(tok, &tok, 10);
322 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
325 xbt_assert(*tok == '_',
326 "Invalid config element descriptor: %s%s", entry,
327 "; Should be <name>:<min nb>_to_<max nb>_<type>");
331 type < xbt_cfgelm_type_count
332 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
333 xbt_assert(type < xbt_cfgelm_type_count,
334 "Invalid type in config element descriptor: %s%s", entry,
335 "; Should be one of 'string', 'int', 'peer' or 'double'.");
337 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
339 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
342 static int strcmp_voidp(const void *pa, const void *pb)
344 return strcmp(*(const char **)pa, *(const char **)pb);
347 /** @brief Displays the declared options and their description */
348 void xbt_cfg_help(xbt_cfg_t cfg)
350 xbt_dict_cursor_t dict_cursor;
351 unsigned int dynar_cursor;
352 xbt_cfgelm_t variable;
354 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
356 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
357 xbt_dynar_push(names, &name);
359 xbt_dynar_sort(names, strcmp_voidp);
361 xbt_dynar_foreach(names, dynar_cursor, name) {
364 variable = xbt_dict_get((xbt_dict_t )cfg, name);
366 printf(" %s: %s\n", name, variable->desc);
367 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
368 if (variable->min != 1 || variable->max != 1) {
369 if (variable->min == 0 && variable->max == 0)
370 printf("Arity: no bound; ");
372 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
374 printf("Current value: ");
375 size = xbt_dynar_length(variable->content);
377 switch (variable->type) {
384 for (i = 0; i < size; i++) {
385 ival = xbt_dynar_get_as(variable->content, i, int);
386 printf("%s%d\n", (i == 0 ? "" : " "), ival);
390 case xbt_cfgelm_double:
391 for (i = 0; i < size; i++) {
392 dval = xbt_dynar_get_as(variable->content, i, double);
393 printf("%s%f\n", (i == 0 ? "" : " "), dval);
397 case xbt_cfgelm_string:
398 for (i = 0; i < size; i++) {
399 sval = xbt_dynar_get_as(variable->content, i, char *);
400 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
404 case xbt_cfgelm_peer:
405 for (i = 0; i < size; i++) {
406 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
407 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
413 printf("Invalid type!!\n");
418 xbt_dynar_free(&names);
421 /** @brief Check that each variable have the right amount of values */
422 void xbt_cfg_check(xbt_cfg_t cfg)
424 xbt_dict_cursor_t cursor;
425 xbt_cfgelm_t variable;
429 xbt_assert(cfg, "NULL config set.");
430 XBT_DEBUG("Check cfg set %p", cfg);
432 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
433 size = xbt_dynar_length(variable->content);
434 if (variable->min > size) {
435 xbt_dict_cursor_free(&cursor);
436 THROWF(mismatch_error, 0,
437 "Config elem %s needs at least %d %s, but there is only %d values.",
438 name, variable->min, xbt_cfgelm_type_name[variable->type],
442 if (variable->max > 0 && variable->max < size) {
443 xbt_dict_cursor_free(&cursor);
444 THROWF(mismatch_error, 0,
445 "Config elem %s accepts at most %d %s, but there is %d values.",
446 name, variable->max, xbt_cfgelm_type_name[variable->type],
451 xbt_dict_cursor_free(&cursor);
454 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
456 e_xbt_cfgelm_type_t type)
458 xbt_cfgelm_t res = NULL;
460 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
463 THROWF(not_found_error, 0,
464 "No registered variable '%s' in this config set", name);
467 xbt_assert(type == xbt_cfgelm_any || res->type == type,
468 "You tried to access to the config element %s as an %s, but its type is %s.",
470 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
475 /** @brief Get the type of this variable in that configuration set
477 * \arg cfg the config set
478 * \arg name the name of the element
479 * \arg type the result
483 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
486 xbt_cfgelm_t variable = NULL;
488 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
490 THROWF(not_found_error, 0,
491 "Can't get the type of '%s' since this variable does not exist",
494 XBT_INFO("type in variable = %d", variable->type);
496 return variable->type;
499 /*----[ Setting ]---------------------------------------------------------*/
500 /** @brief va_args version of xbt_cfg_set
502 * \arg cfg config set to fill
503 * \arg n variable name
504 * \arg pa variable value
506 * Add some values to the config set.
508 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
513 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
518 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 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 (!strcmp(name,"coordinates")){
655 if(!strcmp(val,"yes") && !COORD_HOST_LEVEL){
656 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
657 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
658 COORD_ASR_LEVEL = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
659 free(optionlist_cpy);
662 if(strcmp(val,"yes"))
663 xbt_die("Setting CMD prop coordinates must be \"yes\"");
666 if (strcmp(name,"contexts/factory"))
667 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
670 variable = xbt_dict_get((xbt_dict_t) cfg, name);
673 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
674 name = (char *) (optionlist_cpy - name + options);
675 free(optionlist_cpy);
676 if (e.category == not_found_error) {
678 THROWF(not_found_error, 0,
679 "No registered variable corresponding to '%s'.", name);
685 switch (variable->type) {
686 case xbt_cfgelm_string:
687 xbt_cfg_set_string(cfg, name, val); /* throws */
691 i = strtol(val, &val, 0);
693 free(optionlist_cpy);
694 xbt_die("Value of option %s not valid. Should be an integer", name);
697 xbt_cfg_set_int(cfg, name, i); /* throws */
700 case xbt_cfgelm_double:
701 d = strtod(val, &val);
703 free(optionlist_cpy);
704 xbt_die("Value of option %s not valid. Should be a double", name);
707 xbt_cfg_set_double(cfg, name, d); /* throws */
710 case xbt_cfgelm_peer:
712 val = strchr(val, ':');
714 free(optionlist_cpy);
715 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
720 i = strtol(val, &val, 0);
722 free(optionlist_cpy);
723 xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
727 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
731 THROWF(unknown_error, 0, "Type of config element %s is not valid.",
736 free(optionlist_cpy);
740 free(optionlist_cpy);
744 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
746 * This is useful to change the default value of a variable while allowing
747 * users to override it with command line arguments
749 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
751 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
753 if (variable->isdefault){
754 xbt_cfg_set_int(cfg, name, val);
755 variable->isdefault = 1;
759 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
763 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
765 * This is useful to change the default value of a variable while allowing
766 * users to override it with command line arguments
768 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
770 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
772 if (variable->isdefault) {
773 xbt_cfg_set_double(cfg, name, val);
774 variable->isdefault = 1;
778 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
782 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
784 * This is useful to change the default value of a variable while allowing
785 * users to override it with command line arguments
787 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
790 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
792 if (variable->isdefault){
793 xbt_cfg_set_string(cfg, name, val);
794 variable->isdefault = 1;
798 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
802 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
804 * This is useful to change the default value of a variable while allowing
805 * users to override it with command line arguments
807 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
808 const char *host, int port)
810 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
812 if (variable->isdefault){
813 xbt_cfg_set_peer(cfg, name, host, port);
814 variable->isdefault = 1;
818 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
822 /** @brief Set or add an integer value to \a name within \a cfg
824 * \arg cfg the config set
825 * \arg name the name of the variable
826 * \arg val the value of the variable
828 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
830 xbt_cfgelm_t variable;
832 XBT_VERB("Configuration setting: %s=%d", name, val);
833 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
835 if (variable->max == 1) {
836 if (variable->cb_rm && xbt_dynar_length(variable->content))
837 (*variable->cb_rm) (name, 0);
839 xbt_dynar_set(variable->content, 0, &val);
842 && xbt_dynar_length(variable->content) ==
843 (unsigned long) variable->max)
844 THROWF(mismatch_error, 0,
845 "Cannot add value %d to the config element %s since it's already full (size=%d)",
846 val, name, variable->max);
848 xbt_dynar_push(variable->content, &val);
851 if (variable->cb_set)
852 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
853 variable->isdefault = 0;
856 /** @brief Set or add a double value to \a name within \a cfg
858 * \arg cfg the config set
859 * \arg name the name of the variable
860 * \arg val the doule to set
863 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
865 xbt_cfgelm_t variable;
867 XBT_VERB("Configuration setting: %s=%f", name, val);
868 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
870 if (variable->max == 1) {
871 if (variable->cb_rm && xbt_dynar_length(variable->content))
872 (*variable->cb_rm) (name, 0);
874 xbt_dynar_set(variable->content, 0, &val);
877 && xbt_dynar_length(variable->content) == variable->max)
878 THROWF(mismatch_error, 0,
879 "Cannot add value %f to the config element %s since it's already full (size=%d)",
880 val, name, variable->max);
882 xbt_dynar_push(variable->content, &val);
885 if (variable->cb_set)
886 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
887 variable->isdefault = 0;
890 /** @brief Set or add a string value to \a name within \a cfg
892 * \arg cfg the config set
893 * \arg name the name of the variable
894 * \arg val the value to be added
898 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
900 xbt_cfgelm_t variable;
901 char *newval = xbt_strdup(val);
903 XBT_VERB("Configuration setting: %s=%s", name, val);
904 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
905 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
906 variable->min, variable->max,
907 xbt_cfgelm_type_name[variable->type], variable->type, variable);
909 if (variable->max == 1) {
910 if (xbt_dynar_length(variable->content)) {
912 (*variable->cb_rm) (name, 0);
913 else if (variable->type == xbt_cfgelm_string) {
914 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
919 xbt_dynar_set(variable->content, 0, &newval);
922 && xbt_dynar_length(variable->content) == variable->max)
923 THROWF(mismatch_error, 0,
924 "Cannot add value %s to the config element %s since it's already full (size=%d)",
925 name, val, variable->max);
927 xbt_dynar_push(variable->content, &newval);
930 if (variable->cb_set)
931 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
932 variable->isdefault = 0;
935 /** @brief Set or add an peer value to \a name within \a cfg
937 * \arg cfg the config set
938 * \arg name the name of the variable
940 * \arg port the port number
942 * \e peer values are composed of a string (peername) and an integer (port)
946 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
949 xbt_cfgelm_t variable;
950 xbt_peer_t val = xbt_peer_new(peer, port);
952 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
954 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
956 if (variable->max == 1) {
957 if (variable->cb_rm && xbt_dynar_length(variable->content))
958 (*variable->cb_rm) (name, 0);
960 xbt_dynar_set(variable->content, 0, &val);
963 && xbt_dynar_length(variable->content) == variable->max)
964 THROWF(mismatch_error, 0,
965 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
966 peer, port, name, variable->max);
968 xbt_dynar_push(variable->content, &val);
971 if (variable->cb_set)
972 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
973 variable->isdefault = 0;
976 /* ---- [ Removing ] ---- */
978 /** @brief Remove the provided \e val integer value from a variable
980 * \arg cfg the config set
981 * \arg name the name of the variable
982 * \arg val the value to be removed
984 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
987 xbt_cfgelm_t variable;
991 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
993 if (xbt_dynar_length(variable->content) == variable->min)
994 THROWF(mismatch_error, 0,
995 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
996 val, name, variable->min);
998 xbt_dynar_foreach(variable->content, cpt, seen) {
1000 if (variable->cb_rm)
1001 (*variable->cb_rm) (name, cpt);
1002 xbt_dynar_cursor_rm(variable->content, &cpt);
1007 THROWF(not_found_error, 0,
1008 "Can't remove the value %d of config element %s: value not found.",
1012 /** @brief Remove the provided \e val double value from a variable
1014 * \arg cfg the config set
1015 * \arg name the name of the variable
1016 * \arg val the value to be removed
1019 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1021 xbt_cfgelm_t variable;
1025 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1027 if (xbt_dynar_length(variable->content) == variable->min)
1028 THROWF(mismatch_error, 0,
1029 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1030 val, name, variable->min);
1032 xbt_dynar_foreach(variable->content, cpt, seen) {
1034 xbt_dynar_cursor_rm(variable->content, &cpt);
1035 if (variable->cb_rm)
1036 (*variable->cb_rm) (name, cpt);
1041 THROWF(not_found_error, 0,
1042 "Can't remove the value %f of config element %s: value not found.",
1046 /** @brief Remove the provided \e val string value from a variable
1048 * \arg cfg the config set
1049 * \arg name the name of the variable
1050 * \arg val the value of the string which will be removed
1052 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1054 xbt_cfgelm_t variable;
1058 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1060 if (xbt_dynar_length(variable->content) == variable->min)
1061 THROWF(mismatch_error, 0,
1062 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1063 name, val, variable->min);
1065 xbt_dynar_foreach(variable->content, cpt, seen) {
1066 if (!strcpy(seen, val)) {
1067 if (variable->cb_rm)
1068 (*variable->cb_rm) (name, cpt);
1069 xbt_dynar_cursor_rm(variable->content, &cpt);
1074 THROWF(not_found_error, 0,
1075 "Can't remove the value %s of config element %s: value not found.",
1079 /** @brief Remove the provided \e val peer value from a variable
1081 * \arg cfg the config set
1082 * \arg name the name of the variable
1083 * \arg peer the peername
1084 * \arg port the port number
1088 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1091 xbt_cfgelm_t variable;
1095 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1097 if (xbt_dynar_length(variable->content) == variable->min)
1098 THROWF(mismatch_error, 0,
1099 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1100 peer, port, name, variable->min);
1102 xbt_dynar_foreach(variable->content, cpt, seen) {
1103 if (!strcpy(seen->name, peer) && seen->port == port) {
1104 if (variable->cb_rm)
1105 (*variable->cb_rm) (name, cpt);
1106 xbt_dynar_cursor_rm(variable->content, &cpt);
1111 THROWF(not_found_error, 0,
1112 "Can't remove the value %s:%d of config element %s: value not found.",
1116 /** @brief Remove the \e pos th value from the provided variable */
1118 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1121 xbt_cfgelm_t variable;
1123 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1125 if (xbt_dynar_length(variable->content) == variable->min)
1126 THROWF(mismatch_error, 0,
1127 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1128 pos, name, variable->min);
1130 if (variable->cb_rm)
1131 (*variable->cb_rm) (name, pos);
1132 xbt_dynar_remove_at(variable->content, pos, NULL);
1135 /** @brief Remove all the values from a variable
1137 * \arg cfg the config set
1138 * \arg name the name of the variable
1141 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1143 xbt_cfgelm_t variable = NULL;
1147 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1149 if (e.category != not_found_error)
1153 THROWF(not_found_error, 0,
1154 "Can't empty '%s' since this config element does not exist",
1159 if (variable->cb_rm) {
1162 xbt_dynar_foreach(variable->content, cpt, ignored) {
1163 (*variable->cb_rm) (name, cpt);
1166 xbt_dynar_reset(variable->content);
1170 * Say if the value is the default value
1172 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1174 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1175 return variable->isdefault;
1178 /*----[ Getting ]---------------------------------------------------------*/
1180 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1182 * \arg cfg the config set
1183 * \arg name the name of the variable
1184 * \arg val the wanted value
1186 * Returns the first value from the config set under the given name.
1187 * If there is more than one value, it will issue a warning. Consider using
1188 * xbt_cfg_get_dynar() instead.
1190 * \warning the returned value is the actual content of the config set
1192 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1194 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1196 if (xbt_dynar_length(variable->content) > 1) {
1198 ("You asked for the first value of the config element '%s', but there is %lu values",
1199 name, xbt_dynar_length(variable->content));
1202 return xbt_dynar_get_as(variable->content, 0, int);
1205 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1207 * \arg cfg the config set
1208 * \arg name the name of the variable
1209 * \arg val the wanted value
1211 * Returns the first value from the config set under the given name.
1212 * If there is more than one value, it will issue a warning. Consider using
1213 * xbt_cfg_get_dynar() instead.
1215 * \warning the returned value is the actual content of the config set
1218 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1220 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1222 if (xbt_dynar_length(variable->content) > 1) {
1224 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1225 name, xbt_dynar_length(variable->content));
1228 return xbt_dynar_get_as(variable->content, 0, double);
1231 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1233 * \arg th the config set
1234 * \arg name the name of the variable
1235 * \arg val the wanted value
1237 * Returns the first value from the config set under the given name.
1238 * If there is more than one value, it will issue a warning. Consider using
1239 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1241 * \warning the returned value is the actual content of the config set
1244 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1246 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1248 if (xbt_dynar_length(variable->content) > 1) {
1250 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1251 name, xbt_dynar_length(variable->content));
1252 } else if (xbt_dynar_length(variable->content) == 0) {
1256 return xbt_dynar_get_as(variable->content, 0, char *);
1259 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1261 * \arg cfg the config set
1262 * \arg name the name of the variable
1263 * \arg peer the peer
1264 * \arg port the port number
1266 * Returns the first value from the config set under the given name.
1267 * If there is more than one value, it will issue a warning. Consider using
1268 * xbt_cfg_get_dynar() instead.
1270 * \warning the returned value is the actual content of the config set
1273 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1276 xbt_cfgelm_t variable;
1279 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1281 if (xbt_dynar_length(variable->content) > 1) {
1283 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1284 name, xbt_dynar_length(variable->content));
1287 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1292 /** @brief Retrieve the dynar of all the values stored in a variable
1294 * \arg cfg where to search in
1295 * \arg name what to search for
1298 * Get the data stored in the config set.
1300 * \warning the returned value is the actual content of the config set
1302 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1304 xbt_cfgelm_t variable = NULL;
1308 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1310 if (e.category == not_found_error) {
1312 THROWF(not_found_error, 0,
1313 "No registered variable %s in this config set", name);
1318 return variable->content;
1322 /** @brief Retrieve one of the integer value of a variable */
1323 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1326 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1327 return xbt_dynar_get_as(variable->content, pos, int);
1330 /** @brief Retrieve one of the double value of a variable */
1331 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1334 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1335 return xbt_dynar_get_as(variable->content, pos, double);
1339 /** @brief Retrieve one of the string value of a variable */
1340 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1343 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1344 return xbt_dynar_get_as(variable->content, pos, char *);
1347 /** @brief Retrieve one of the peer value of a variable */
1349 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1350 char **peer, int *port)
1353 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1354 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1365 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1366 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1368 XBT_TEST_SUITE("config", "Configuration support");
1370 static xbt_cfg_t make_set()
1372 xbt_cfg_t set = NULL;
1374 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1375 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1376 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1377 xbt_cfg_register_str(&set, "user:1_to_10_string");
1380 } /* end_of_make_set */
1382 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1384 xbt_cfg_t set = make_set();
1385 xbt_test_add("Alloc and free a config set");
1386 xbt_cfg_set_parse(set,
1387 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1392 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1394 xbt_cfg_t set = set = make_set();
1397 xbt_test_add("Having too few elements for speed");
1398 xbt_cfg_set_parse(set,
1399 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1404 if (e.category != mismatch_error ||
1405 strncmp(e.msg, "Config elem speed needs",
1406 strlen("Config elem speed needs")))
1407 xbt_test_fail("Got an exception. msg=%s", e.msg);
1415 xbt_test_add("Having too much values of 'speed'");
1417 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1419 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1422 if (e.category != mismatch_error ||
1423 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1424 strlen("Config elem speed needs")))
1425 xbt_test_fail("Got an exception. msg=%s", e.msg);
1434 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1437 xbt_test_add("Get a single value");
1439 /* get_single_value */
1441 xbt_cfg_t myset = make_set();
1443 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1444 ival = xbt_cfg_get_int(myset, "speed");
1446 xbt_test_fail("Speed value = %d, I expected 42", ival);
1447 xbt_cfg_free(&myset);
1450 xbt_test_add("Get multiple values");
1452 /* get_multiple_value */
1454 xbt_cfg_t myset = make_set();
1456 xbt_cfg_set_parse(myset,
1457 "peername:veloce user:foo\nuser:bar\tuser:toto");
1458 xbt_cfg_set_parse(myset, "speed:42");
1459 xbt_cfg_check(myset);
1460 dyn = xbt_cfg_get_dynar(myset, "user");
1462 if (xbt_dynar_length(dyn) != 3)
1463 xbt_test_fail("Dynar length = %lu, I expected 3",
1464 xbt_dynar_length(dyn));
1466 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1467 xbt_test_fail("Dynar[0] = %s, I expected foo",
1468 xbt_dynar_get_as(dyn, 0, char *));
1470 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1471 xbt_test_fail("Dynar[1] = %s, I expected bar",
1472 xbt_dynar_get_as(dyn, 1, char *));
1474 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1475 xbt_test_fail("Dynar[2] = %s, I expected toto",
1476 xbt_dynar_get_as(dyn, 2, char *));
1477 xbt_cfg_free(&myset);
1480 xbt_test_add("Access to a non-existant entry");
1482 /* non-existant_entry */
1483 xbt_cfg_t myset = make_set();
1487 xbt_cfg_set_parse(myset, "color:blue");
1489 if (e.category != not_found_error)
1490 xbt_test_exception(e);
1493 xbt_cfg_free(&myset);
1496 #endif /* SIMGRID_TEST */