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 DEBUG1("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 DEBUG1("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 DEBUG1("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 WARN1("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 DEBUG8("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 ERROR1("%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 DEBUG2("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 DEBUG1("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 /** @brief Displays the declared options and their description */
341 void xbt_cfg_help(xbt_cfg_t cfg)
343 xbt_dict_cursor_t cursor;
344 xbt_cfgelm_t variable;
350 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
351 printf(" %s: %s\n", name, variable->desc);
352 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
353 if (variable->min != 1 || variable->max != 1) {
354 if (variable->min == 0 && variable->max == 0)
355 printf("Arity: no bound; ");
357 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
359 printf("Current value: ");
360 size = xbt_dynar_length(variable->content);
362 switch (variable->type) {
369 for (i = 0; i < size; i++) {
370 ival = xbt_dynar_get_as(variable->content, i, int);
371 printf("%s%d\n", (i == 0 ? "" : " "), ival);
375 case xbt_cfgelm_double:
376 for (i = 0; i < size; i++) {
377 dval = xbt_dynar_get_as(variable->content, i, double);
378 printf("%s%f\n", (i == 0 ? "" : " "), dval);
382 case xbt_cfgelm_string:
383 for (i = 0; i < size; i++) {
384 sval = xbt_dynar_get_as(variable->content, i, char *);
385 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
389 case xbt_cfgelm_peer:
390 for (i = 0; i < size; i++) {
391 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
392 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
398 printf("Invalid type!!\n");
404 /** @brief Check that each variable have the right amount of values */
405 void xbt_cfg_check(xbt_cfg_t cfg)
407 xbt_dict_cursor_t cursor;
408 xbt_cfgelm_t variable;
412 xbt_assert0(cfg, "NULL config set.");
413 DEBUG1("Check cfg set %p", cfg);
415 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
416 size = xbt_dynar_length(variable->content);
417 if (variable->min > size) {
418 xbt_dict_cursor_free(&cursor);
419 THROW4(mismatch_error, 0,
420 "Config elem %s needs at least %d %s, but there is only %d values.",
421 name, variable->min, xbt_cfgelm_type_name[variable->type],
425 if (variable->max > 0 && variable->max < size) {
426 xbt_dict_cursor_free(&cursor);
427 THROW4(mismatch_error, 0,
428 "Config elem %s accepts at most %d %s, but there is %d values.",
429 name, variable->max, xbt_cfgelm_type_name[variable->type],
434 xbt_dict_cursor_free(&cursor);
437 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
439 e_xbt_cfgelm_type_t type)
441 xbt_cfgelm_t res = NULL;
443 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
446 THROW1(not_found_error, 0,
447 "No registered variable '%s' in this config set", name);
450 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
451 "You tried to access to the config element %s as an %s, but its type is %s.",
453 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
458 /** @brief Get the type of this variable in that configuration set
460 * \arg cfg the config set
461 * \arg name the name of the element
462 * \arg type the result
466 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
469 xbt_cfgelm_t variable = NULL;
471 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
473 THROW1(not_found_error, 0,
474 "Can't get the type of '%s' since this variable does not exist",
477 INFO1("type in variable = %d", variable->type);
479 return variable->type;
482 /*----[ Setting ]---------------------------------------------------------*/
483 /** @brief va_args version of xbt_cfg_set
485 * \arg cfg config set to fill
486 * \arg n variable name
487 * \arg pa variable value
489 * Add some values to the config set.
491 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
496 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
501 type = xbt_cfg_get_type(cfg, name);
503 if (e.category == not_found_error) {
505 THROW1(not_found_error, 0,
506 "Can't set the property '%s' since it's not registered",
513 case xbt_cfgelm_peer:
514 str = va_arg(pa, char *);
516 xbt_cfg_set_peer(cfg, name, str, i);
519 case xbt_cfgelm_string:
520 str = va_arg(pa, char *);
521 xbt_cfg_set_string(cfg, name, str);
526 xbt_cfg_set_int(cfg, name, i);
529 case xbt_cfgelm_double:
530 d = va_arg(pa, double);
531 xbt_cfg_set_double(cfg, name, d);
535 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
540 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
542 * \arg cfg config set to fill
543 * \arg name variable name
544 * \arg varargs variable value
547 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
552 xbt_cfg_set_vargs(cfg, name, pa);
556 /** @brief Add values parsed from a string into a config set
558 * \arg cfg config set to fill
559 * \arg options a string containing the content to add to the config set. This
560 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
561 * like "[name]:[value]" where [name] is the name of an already registred
562 * variable, and [value] conforms to the data type under which this variable was
565 * @todo This is a crude manual parser, it should be a proper lexer.
568 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
576 xbt_cfgelm_t variable = NULL;
577 char *optionlist_cpy;
578 char *option, *name, *val;
583 if (!options || !strlen(options)) { /* nothing to do */
586 optionlist_cpy = xbt_strdup(options);
588 DEBUG1("List to parse and set:'%s'", options);
589 option = optionlist_cpy;
590 while (1) { /* breaks in the code */
596 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
597 name, len, (long) (option - name));
600 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
601 && *option != '\t' && *option != ',') {
602 DEBUG1("Take %c.", *option);
605 if (option - name == len) {
606 DEBUG0("Boundary=EOL");
607 option = NULL; /* don't do next iteration */
610 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
611 *option, len, (long) (option - name));
613 /* Pass the following blank chars */
615 while (option - name < (len - 1) &&
616 (*option == ' ' || *option == '\n' || *option == '\t')) {
617 /* fprintf(stderr,"Ignore a blank char.\n"); */
620 if (option - name == len - 1)
621 option = NULL; /* don't do next iteration */
623 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
625 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
630 val = strchr(name, ':');
632 free(optionlist_cpy);
634 "Option '%s' badly formated. Should be of the form 'name:value'",
639 if(strcmp(name,"simix/context"))
640 INFO2("Configuration change: Set '%s' to '%s'", name, val);
643 variable = xbt_dict_get((xbt_dict_t) cfg, name);
646 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
647 name = (char *) (optionlist_cpy - name + options);
648 free(optionlist_cpy);
649 if (e.category == not_found_error) {
651 THROW1(not_found_error, 0,
652 "No registered variable corresponding to '%s'.", name);
658 switch (variable->type) {
659 case xbt_cfgelm_string:
660 xbt_cfg_set_string(cfg, name, val); /* throws */
664 i = strtol(val, &val, 0);
666 free(optionlist_cpy);
668 "Value of option %s not valid. Should be an integer",
672 xbt_cfg_set_int(cfg, name, i); /* throws */
675 case xbt_cfgelm_double:
676 d = strtod(val, &val);
678 free(optionlist_cpy);
680 "Value of option %s not valid. Should be a double",
684 xbt_cfg_set_double(cfg, name, d); /* throws */
687 case xbt_cfgelm_peer:
689 val = strchr(val, ':');
691 free(optionlist_cpy);
693 "Value of option %s not valid. Should be an peer (machine:port)",
698 i = strtol(val, &val, 0);
700 free(optionlist_cpy);
702 "Value of option %s not valid. Should be an peer (machine:port)",
706 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
710 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
715 free(optionlist_cpy);
719 free(optionlist_cpy);
723 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
725 * This is useful to change the default value of a variable while allowing
726 * users to override it with command line arguments
728 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
730 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
732 if (variable->isdefault)
733 xbt_cfg_set_int(cfg, name, val);
736 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
740 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
742 * This is useful to change the default value of a variable while allowing
743 * users to override it with command line arguments
745 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
747 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
749 if (variable->isdefault)
750 xbt_cfg_set_double(cfg, name, val);
753 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
757 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
759 * This is useful to change the default value of a variable while allowing
760 * users to override it with command line arguments
762 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
765 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
767 if (variable->isdefault)
768 xbt_cfg_set_string(cfg, name, val);
771 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
775 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
777 * This is useful to change the default value of a variable while allowing
778 * users to override it with command line arguments
780 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
781 const char *host, int port)
783 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
785 if (variable->isdefault)
786 xbt_cfg_set_peer(cfg, name, host, port);
789 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
793 /** @brief Set or add an integer value to \a name within \a cfg
795 * \arg cfg the config set
796 * \arg name the name of the variable
797 * \arg val the value of the variable
799 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
801 xbt_cfgelm_t variable;
803 VERB2("Configuration setting: %s=%d", name, val);
804 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
806 if (variable->max == 1) {
807 if (variable->cb_rm && xbt_dynar_length(variable->content))
808 (*variable->cb_rm) (name, 0);
810 xbt_dynar_set(variable->content, 0, &val);
813 && xbt_dynar_length(variable->content) ==
814 (unsigned long) variable->max)
815 THROW3(mismatch_error, 0,
816 "Cannot add value %d to the config element %s since it's already full (size=%d)",
817 val, name, variable->max);
819 xbt_dynar_push(variable->content, &val);
822 if (variable->cb_set)
823 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
824 variable->isdefault = 0;
827 /** @brief Set or add a double value to \a name within \a cfg
829 * \arg cfg the config set
830 * \arg name the name of the variable
831 * \arg val the doule to set
834 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
836 xbt_cfgelm_t variable;
838 VERB2("Configuration setting: %s=%f", name, val);
839 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
841 if (variable->max == 1) {
842 if (variable->cb_rm && xbt_dynar_length(variable->content))
843 (*variable->cb_rm) (name, 0);
845 xbt_dynar_set(variable->content, 0, &val);
848 && xbt_dynar_length(variable->content) == variable->max)
849 THROW3(mismatch_error, 0,
850 "Cannot add value %f to the config element %s since it's already full (size=%d)",
851 val, name, variable->max);
853 xbt_dynar_push(variable->content, &val);
856 if (variable->cb_set)
857 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
858 variable->isdefault = 0;
861 /** @brief Set or add a string value to \a name within \a cfg
863 * \arg cfg the config set
864 * \arg name the name of the variable
865 * \arg val the value to be added
869 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
871 xbt_cfgelm_t variable;
872 char *newval = xbt_strdup(val);
874 VERB2("Configuration setting: %s=%s", name, val);
875 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
876 DEBUG5("Variable: %d to %d %s (=%d) @%p",
877 variable->min, variable->max,
878 xbt_cfgelm_type_name[variable->type], variable->type, variable);
880 if (variable->max == 1) {
881 if (xbt_dynar_length(variable->content)) {
883 (*variable->cb_rm) (name, 0);
884 else if (variable->type == xbt_cfgelm_string) {
885 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
890 xbt_dynar_set(variable->content, 0, &newval);
893 && xbt_dynar_length(variable->content) == variable->max)
894 THROW3(mismatch_error, 0,
895 "Cannot add value %s to the config element %s since it's already full (size=%d)",
896 name, val, variable->max);
898 xbt_dynar_push(variable->content, &newval);
901 if (variable->cb_set)
902 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
903 variable->isdefault = 0;
906 /** @brief Set or add an peer value to \a name within \a cfg
908 * \arg cfg the config set
909 * \arg name the name of the variable
911 * \arg port the port number
913 * \e peer values are composed of a string (peername) and an integer (port)
917 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
920 xbt_cfgelm_t variable;
921 xbt_peer_t val = xbt_peer_new(peer, port);
923 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
925 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
927 if (variable->max == 1) {
928 if (variable->cb_rm && xbt_dynar_length(variable->content))
929 (*variable->cb_rm) (name, 0);
931 xbt_dynar_set(variable->content, 0, &val);
934 && xbt_dynar_length(variable->content) == variable->max)
935 THROW4(mismatch_error, 0,
936 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
937 peer, port, name, variable->max);
939 xbt_dynar_push(variable->content, &val);
942 if (variable->cb_set)
943 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
944 variable->isdefault = 0;
947 /* ---- [ Removing ] ---- */
949 /** @brief Remove the provided \e val integer value from a variable
951 * \arg cfg the config set
952 * \arg name the name of the variable
953 * \arg val the value to be removed
955 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
958 xbt_cfgelm_t variable;
962 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
964 if (xbt_dynar_length(variable->content) == variable->min)
965 THROW3(mismatch_error, 0,
966 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
967 val, name, variable->min);
969 xbt_dynar_foreach(variable->content, cpt, seen) {
972 (*variable->cb_rm) (name, cpt);
973 xbt_dynar_cursor_rm(variable->content, &cpt);
978 THROW2(not_found_error, 0,
979 "Can't remove the value %d of config element %s: value not found.",
983 /** @brief Remove the provided \e val double value from a variable
985 * \arg cfg the config set
986 * \arg name the name of the variable
987 * \arg val the value to be removed
990 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
992 xbt_cfgelm_t variable;
996 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
998 if (xbt_dynar_length(variable->content) == variable->min)
999 THROW3(mismatch_error, 0,
1000 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1001 val, name, variable->min);
1003 xbt_dynar_foreach(variable->content, cpt, seen) {
1005 xbt_dynar_cursor_rm(variable->content, &cpt);
1006 if (variable->cb_rm)
1007 (*variable->cb_rm) (name, cpt);
1012 THROW2(not_found_error, 0,
1013 "Can't remove the value %f of config element %s: value not found.",
1017 /** @brief Remove the provided \e val string value from a variable
1019 * \arg cfg the config set
1020 * \arg name the name of the variable
1021 * \arg val the value of the string which will be removed
1023 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1025 xbt_cfgelm_t variable;
1029 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1031 if (xbt_dynar_length(variable->content) == variable->min)
1032 THROW3(mismatch_error, 0,
1033 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1034 name, val, variable->min);
1036 xbt_dynar_foreach(variable->content, cpt, seen) {
1037 if (!strcpy(seen, val)) {
1038 if (variable->cb_rm)
1039 (*variable->cb_rm) (name, cpt);
1040 xbt_dynar_cursor_rm(variable->content, &cpt);
1045 THROW2(not_found_error, 0,
1046 "Can't remove the value %s of config element %s: value not found.",
1050 /** @brief Remove the provided \e val peer value from a variable
1052 * \arg cfg the config set
1053 * \arg name the name of the variable
1054 * \arg peer the peername
1055 * \arg port the port number
1059 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1062 xbt_cfgelm_t variable;
1066 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1068 if (xbt_dynar_length(variable->content) == variable->min)
1069 THROW4(mismatch_error, 0,
1070 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1071 peer, port, name, variable->min);
1073 xbt_dynar_foreach(variable->content, cpt, seen) {
1074 if (!strcpy(seen->name, peer) && seen->port == port) {
1075 if (variable->cb_rm)
1076 (*variable->cb_rm) (name, cpt);
1077 xbt_dynar_cursor_rm(variable->content, &cpt);
1082 THROW3(not_found_error, 0,
1083 "Can't remove the value %s:%d of config element %s: value not found.",
1087 /** @brief Remove the \e pos th value from the provided variable */
1089 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1092 xbt_cfgelm_t variable;
1094 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1096 if (xbt_dynar_length(variable->content) == variable->min)
1097 THROW3(mismatch_error, 0,
1098 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1099 pos, name, variable->min);
1101 if (variable->cb_rm)
1102 (*variable->cb_rm) (name, pos);
1103 xbt_dynar_remove_at(variable->content, pos, NULL);
1106 /** @brief Remove all the values from a variable
1108 * \arg cfg the config set
1109 * \arg name the name of the variable
1112 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1114 xbt_cfgelm_t variable = NULL;
1118 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1120 if (e.category != not_found_error)
1124 THROW1(not_found_error, 0,
1125 "Can't empty '%s' since this config element does not exist",
1130 if (variable->cb_rm) {
1133 xbt_dynar_foreach(variable->content, cpt, ignored) {
1134 (*variable->cb_rm) (name, cpt);
1137 xbt_dynar_reset(variable->content);
1141 /*----[ Getting ]---------------------------------------------------------*/
1143 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1145 * \arg cfg the config set
1146 * \arg name the name of the variable
1147 * \arg val the wanted value
1149 * Returns the first value from the config set under the given name.
1150 * If there is more than one value, it will issue a warning. Consider using
1151 * xbt_cfg_get_dynar() instead.
1153 * \warning the returned value is the actual content of the config set
1155 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1157 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1159 if (xbt_dynar_length(variable->content) > 1) {
1161 ("You asked for the first value of the config element '%s', but there is %lu values",
1162 name, xbt_dynar_length(variable->content));
1165 return xbt_dynar_get_as(variable->content, 0, int);
1168 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1170 * \arg cfg the config set
1171 * \arg name the name of the variable
1172 * \arg val the wanted value
1174 * Returns the first value from the config set under the given name.
1175 * If there is more than one value, it will issue a warning. Consider using
1176 * xbt_cfg_get_dynar() instead.
1178 * \warning the returned value is the actual content of the config set
1181 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1183 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
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\n",
1188 name, xbt_dynar_length(variable->content));
1191 return xbt_dynar_get_as(variable->content, 0, double);
1194 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1196 * \arg th 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. Returns NULL if there is no value.
1204 * \warning the returned value is the actual content of the config set
1207 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1209 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
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));
1215 } else if (xbt_dynar_length(variable->content) == 0) {
1219 return xbt_dynar_get_as(variable->content, 0, char *);
1222 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1224 * \arg cfg the config set
1225 * \arg name the name of the variable
1226 * \arg peer the peer
1227 * \arg port the port number
1229 * Returns the first value from the config set under the given name.
1230 * If there is more than one value, it will issue a warning. Consider using
1231 * xbt_cfg_get_dynar() instead.
1233 * \warning the returned value is the actual content of the config set
1236 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1239 xbt_cfgelm_t variable;
1242 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1244 if (xbt_dynar_length(variable->content) > 1) {
1246 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1247 name, xbt_dynar_length(variable->content));
1250 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1255 /** @brief Retrieve the dynar of all the values stored in a variable
1257 * \arg cfg where to search in
1258 * \arg name what to search for
1261 * Get the data stored in the config set.
1263 * \warning the returned value is the actual content of the config set
1265 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1267 xbt_cfgelm_t variable = NULL;
1271 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1273 if (e.category == not_found_error) {
1275 THROW1(not_found_error, 0,
1276 "No registered variable %s in this config set", name);
1281 return variable->content;
1285 /** @brief Retrieve one of the integer value of a variable */
1286 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1289 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1290 return xbt_dynar_get_as(variable->content, pos, int);
1293 /** @brief Retrieve one of the double value of a variable */
1294 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1297 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1298 return xbt_dynar_get_as(variable->content, pos, double);
1302 /** @brief Retrieve one of the string value of a variable */
1303 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1306 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1307 return xbt_dynar_get_as(variable->content, pos, char *);
1310 /** @brief Retrieve one of the peer value of a variable */
1312 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1313 char **peer, int *port)
1316 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1317 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1328 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1329 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1331 XBT_TEST_SUITE("config", "Configuration support");
1333 static xbt_cfg_t make_set()
1335 xbt_cfg_t set = NULL;
1337 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1338 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1339 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1340 xbt_cfg_register_str(&set, "user:1_to_10_string");
1343 } /* end_of_make_set */
1345 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1347 xbt_cfg_t set = make_set();
1348 xbt_test_add0("Alloc and free a config set");
1349 xbt_cfg_set_parse(set,
1350 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1355 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1357 xbt_cfg_t set = set = make_set();
1360 xbt_test_add0("Having too few elements for speed");
1361 xbt_cfg_set_parse(set,
1362 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1367 if (e.category != mismatch_error ||
1368 strncmp(e.msg, "Config elem speed needs",
1369 strlen("Config elem speed needs")))
1370 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1378 xbt_test_add0("Having too much values of 'speed'");
1380 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1382 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1385 if (e.category != mismatch_error ||
1386 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1387 strlen("Config elem speed needs")))
1388 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1397 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1400 xbt_test_add0("Get a single value");
1402 /* get_single_value */
1404 xbt_cfg_t myset = make_set();
1406 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1407 ival = xbt_cfg_get_int(myset, "speed");
1409 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1410 xbt_cfg_free(&myset);
1413 xbt_test_add0("Get multiple values");
1415 /* get_multiple_value */
1417 xbt_cfg_t myset = make_set();
1419 xbt_cfg_set_parse(myset,
1420 "peername:veloce user:foo\nuser:bar\tuser:toto");
1421 xbt_cfg_set_parse(myset, "speed:42");
1422 xbt_cfg_check(myset);
1423 dyn = xbt_cfg_get_dynar(myset, "user");
1425 if (xbt_dynar_length(dyn) != 3)
1426 xbt_test_fail1("Dynar length = %lu, I expected 3",
1427 xbt_dynar_length(dyn));
1429 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1430 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1431 xbt_dynar_get_as(dyn, 0, char *));
1433 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1434 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1435 xbt_dynar_get_as(dyn, 1, char *));
1437 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1438 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1439 xbt_dynar_get_as(dyn, 2, char *));
1440 xbt_cfg_free(&myset);
1443 xbt_test_add0("Access to a non-existant entry");
1445 /* non-existant_entry */
1446 xbt_cfg_t myset = make_set();
1450 xbt_cfg_set_parse(myset, "color:blue");
1452 if (e.category != not_found_error)
1453 xbt_test_exception(e);
1456 xbt_cfg_free(&myset);
1459 #endif /* SIMGRID_TEST */