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, xbt_cfgelm_type_name[variable->type],
131 size, variable->cb_rm, variable->cb_set);
133 switch (variable->type) {
136 for (i = 0; i < size; i++) {
137 ival = xbt_dynar_get_as(variable->content, i, int);
138 printf("%s %d\n", indent, ival);
142 case xbt_cfgelm_double:
143 for (i = 0; i < size; i++) {
144 dval = xbt_dynar_get_as(variable->content, i, double);
145 printf("%s %f\n", indent, dval);
149 case xbt_cfgelm_string:
150 for (i = 0; i < size; i++) {
151 sval = xbt_dynar_get_as(variable->content, i, char *);
152 printf("%s %s\n", indent, sval);
156 case xbt_cfgelm_peer:
157 for (i = 0; i < size; i++) {
158 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
159 printf("%s %s:%d\n", indent, hval->name, hval->port);
164 printf("%s Invalid type!!\n", indent);
170 printf("%s<< End of the config set '%s'\n", indent, name);
173 xbt_dict_cursor_free(&cursor);
178 * free an config element
181 void xbt_cfgelm_free(void *data)
183 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
185 DEBUG1("Frees cfgelm %p", c);
189 xbt_dynar_free(&(c->content));
193 /*----[ Registering stuff ]-----------------------------------------------*/
195 /** @brief Register an element within a config set
197 * @arg cfg the config set
198 * @arg type the type of the config element
199 * @arg min the minimum
200 * @arg max the maximum
204 xbt_cfg_register(xbt_cfg_t * cfg,
205 const char *name, const char *desc, e_xbt_cfgelm_type_t type,
206 void *default_value, int min, int max, xbt_cfg_cb_t cb_set,
212 *cfg = xbt_cfg_new();
213 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
214 "type of %s not valid (%d should be between %d and %d)",
215 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
216 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
219 WARN1("Config elem %s registered twice.", name);
220 /* Will be removed by the insertion of the new one */
223 res = xbt_new(s_xbt_cfgelm_t, 1);
224 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
225 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, *cfg);
227 res->desc = xbt_strdup(desc);
231 res->cb_set = cb_set;
237 res->content = xbt_dynar_new(sizeof(int), NULL);
239 xbt_dynar_push(res->content, default_value);
242 case xbt_cfgelm_double:
243 res->content = xbt_dynar_new(sizeof(double), NULL);
245 xbt_dynar_push(res->content, default_value);
248 case xbt_cfgelm_string:
249 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
251 xbt_dynar_push(res->content, default_value);
254 case xbt_cfgelm_peer:
255 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
257 xbt_dynar_push(res->content, default_value);
261 ERROR1("%d is an invalide type code", type);
264 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
267 /** @brief Unregister an element from a config set.
269 * @arg cfg the config set
270 * @arg name the name of the elem to be freed
272 * Note that it removes both the description and the actual content.
273 * Throws not_found when no such element exists.
276 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
278 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
279 xbt_dict_remove((xbt_dict_t) cfg, name);
283 * @brief Parse a string and register the stuff described.
285 * @arg cfg the config set
286 * @arg entry a string describing the element to register
288 * The string may consist in several variable descriptions separated by a space.
289 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
290 * with type being one of 'string','int', 'peer' or 'double'.
292 * @fixme: this does not allow to set the description
295 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
297 char *entrycpy = xbt_strdup(entry);
301 e_xbt_cfgelm_type_t type;
302 DEBUG1("Register string '%s'", entry);
304 tok = strchr(entrycpy, ':');
305 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
306 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
309 min = strtol(tok, &tok, 10);
310 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
312 xbt_assert2(strcmp(tok, "_to_"),
313 "Invalid config element descriptor : %s%s",
314 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
315 tok += strlen("_to_");
317 max = strtol(tok, &tok, 10);
318 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
320 xbt_assert2(*(tok++) == '_',
321 "Invalid config element descriptor: %s%s", entry,
322 "; Should be <name>:<min nb>_to_<max nb>_<type>");
325 type < xbt_cfgelm_type_count
326 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
327 xbt_assert2(type < xbt_cfgelm_type_count,
328 "Invalid type in config element descriptor: %s%s", entry,
329 "; Should be one of 'string', 'int', 'peer' or 'double'.");
331 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
333 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
336 /** @brief Displays the declared options and their description */
337 void xbt_cfg_help(xbt_cfg_t cfg)
339 xbt_dict_cursor_t cursor;
340 xbt_cfgelm_t variable;
346 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
347 printf(" %s: %s\n", name, variable->desc);
348 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
349 if (variable->min != 1 || variable->max != 1) {
350 if (variable->min == 0 && variable->max == 0)
351 printf("Arity: no bound; ");
353 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
355 printf("Current value: ");
356 size = xbt_dynar_length(variable->content);
358 switch (variable->type) {
365 for (i = 0; i < size; i++) {
366 ival = xbt_dynar_get_as(variable->content, i, int);
367 printf("%s%d\n", (i == 0 ? "" : " "), ival);
371 case xbt_cfgelm_double:
372 for (i = 0; i < size; i++) {
373 dval = xbt_dynar_get_as(variable->content, i, double);
374 printf("%s%f\n", (i == 0 ? "" : " "), dval);
378 case xbt_cfgelm_string:
379 for (i = 0; i < size; i++) {
380 sval = xbt_dynar_get_as(variable->content, i, char *);
381 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
385 case xbt_cfgelm_peer:
386 for (i = 0; i < size; i++) {
387 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
388 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
394 printf("Invalid type!!\n");
400 /** @brief Check that each variable have the right amount of values */
401 void xbt_cfg_check(xbt_cfg_t cfg)
403 xbt_dict_cursor_t cursor;
404 xbt_cfgelm_t variable;
408 xbt_assert0(cfg, "NULL config set.");
409 DEBUG1("Check cfg set %p", cfg);
411 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
412 size = xbt_dynar_length(variable->content);
413 if (variable->min > size) {
414 xbt_dict_cursor_free(&cursor);
415 THROW4(mismatch_error, 0,
416 "Config elem %s needs at least %d %s, but there is only %d values.",
417 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
420 if (variable->max > 0 && variable->max < size) {
421 xbt_dict_cursor_free(&cursor);
422 THROW4(mismatch_error, 0,
423 "Config elem %s accepts at most %d %s, but there is %d values.",
424 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
428 xbt_dict_cursor_free(&cursor);
431 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
432 const char *name, e_xbt_cfgelm_type_t type)
434 xbt_cfgelm_t res = NULL;
436 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
439 THROW1(not_found_error, 0,
440 "No registered variable '%s' in this config set", name);
443 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
444 "You tried to access to the config element %s as an %s, but its type is %s.",
446 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
451 /** @brief Get the type of this variable in that configuration set
453 * \arg cfg the config set
454 * \arg name the name of the element
455 * \arg type the result
459 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
462 xbt_cfgelm_t variable = NULL;
464 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
466 THROW1(not_found_error, 0,
467 "Can't get the type of '%s' since this variable does not exist",
470 INFO1("type in variable = %d", variable->type);
472 return variable->type;
475 /*----[ Setting ]---------------------------------------------------------*/
476 /** @brief va_args version of xbt_cfg_set
478 * \arg cfg config set to fill
479 * \arg n variable name
480 * \arg pa variable value
482 * Add some values to the config set.
484 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
489 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
494 type = xbt_cfg_get_type(cfg, name);
496 if (e.category == not_found_error) {
498 THROW1(not_found_error, 0,
499 "Can't set the property '%s' since it's not registered", name);
505 case xbt_cfgelm_peer:
506 str = va_arg(pa, char *);
508 xbt_cfg_set_peer(cfg, name, str, i);
511 case xbt_cfgelm_string:
512 str = va_arg(pa, char *);
513 xbt_cfg_set_string(cfg, name, str);
518 xbt_cfg_set_int(cfg, name, i);
521 case xbt_cfgelm_double:
522 d = va_arg(pa, double);
523 xbt_cfg_set_double(cfg, name, d);
527 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
532 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
534 * \arg cfg config set to fill
535 * \arg name variable name
536 * \arg varargs variable value
539 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
544 xbt_cfg_set_vargs(cfg, name, pa);
548 /** @brief Add values parsed from a string into a config set
550 * \arg cfg config set to fill
551 * \arg options a string containing the content to add to the config set. This
552 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
553 * like "[name]:[value]" where [name] is the name of an already registred
554 * variable, and [value] conforms to the data type under which this variable was
557 * @todo This is a crude manual parser, it should be a proper lexer.
560 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
568 xbt_cfgelm_t variable = NULL;
569 char *optionlist_cpy;
570 char *option, *name, *val;
575 if (!options || !strlen(options)) { /* nothing to do */
578 optionlist_cpy = xbt_strdup(options);
580 DEBUG1("List to parse and set:'%s'", options);
581 option = optionlist_cpy;
582 while (1) { /* breaks in the code */
588 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
589 name, len, (long) (option - name));
592 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
593 && *option != '\t' && *option != ',') {
594 DEBUG1("Take %c.", *option);
597 if (option - name == len) {
598 DEBUG0("Boundary=EOL");
599 option = NULL; /* don't do next iteration */
602 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
603 *option, len, (long) (option - name));
605 /* Pass the following blank chars */
607 while (option - name < (len - 1) &&
608 (*option == ' ' || *option == '\n' || *option == '\t')) {
609 /* fprintf(stderr,"Ignore a blank char.\n"); */
612 if (option - name == len - 1)
613 option = NULL; /* don't do next iteration */
615 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
617 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
622 val = strchr(name, ':');
624 free(optionlist_cpy);
626 "Option '%s' badly formated. Should be of the form 'name:value'",
631 INFO2("Configuration change: Set '%s' to '%s'", name, val);
634 variable = xbt_dict_get((xbt_dict_t) cfg, name);
637 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
638 name = (char *) (optionlist_cpy - name + options);
639 free(optionlist_cpy);
640 if (e.category == not_found_error) {
642 THROW1(not_found_error, 0,
643 "No registered variable corresponding to '%s'.", name);
649 switch (variable->type) {
650 case xbt_cfgelm_string:
651 xbt_cfg_set_string(cfg, name, val); /* throws */
655 i = strtol(val, &val, 0);
657 free(optionlist_cpy);
659 "Value of option %s not valid. Should be an integer",
663 xbt_cfg_set_int(cfg, name, i); /* throws */
666 case xbt_cfgelm_double:
667 d = strtod(val, &val);
669 free(optionlist_cpy);
671 "Value of option %s not valid. Should be a double",
675 xbt_cfg_set_double(cfg, name, d); /* throws */
678 case xbt_cfgelm_peer:
680 val = strchr(val, ':');
682 free(optionlist_cpy);
684 "Value of option %s not valid. Should be an peer (machine:port)",
689 i = strtol(val, &val, 0);
691 free(optionlist_cpy);
693 "Value of option %s not valid. Should be an peer (machine:port)",
697 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
701 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
706 free(optionlist_cpy);
710 free(optionlist_cpy);
714 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
716 * This is useful to change the default value of a variable while allowing
717 * users to override it with command line arguments
719 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val) {
720 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
722 if (variable->isdefault)
723 xbt_cfg_set_int(cfg, name,val);
725 DEBUG2("Do not override configuration variable '%s' with value '%d' because it was already set.",name,val);
727 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
729 * This is useful to change the default value of a variable while allowing
730 * users to override it with command line arguments
732 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val) {
733 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
735 if (variable->isdefault)
736 xbt_cfg_set_double(cfg, name, val);
738 DEBUG2("Do not override configuration variable '%s' with value '%lf' because it was already set.",name,val);
740 /** @brief Set a string 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_string(xbt_cfg_t cfg, const char *name, const char* val) {
746 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
748 if (variable->isdefault)
749 xbt_cfg_set_string(cfg, name,val);
751 DEBUG2("Do not override configuration variable '%s' with value '%s' because it was already set.",name,val);
753 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
755 * This is useful to change the default value of a variable while allowing
756 * users to override it with command line arguments
758 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name, const char* host, int port) {
759 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
761 if (variable->isdefault)
762 xbt_cfg_set_peer(cfg, name, host, port);
764 DEBUG3("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
768 /** @brief Set or add an integer value to \a name within \a cfg
770 * \arg cfg the config set
771 * \arg name the name of the variable
772 * \arg val the value of the variable
774 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
776 xbt_cfgelm_t variable;
778 VERB2("Configuration setting: %s=%d", name, val);
779 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
781 if (variable->max == 1) {
782 if (variable->cb_rm && xbt_dynar_length(variable->content))
783 (*variable->cb_rm) (name, 0);
785 xbt_dynar_set(variable->content, 0, &val);
788 && xbt_dynar_length(variable->content) ==
789 (unsigned long) variable->max)
790 THROW3(mismatch_error, 0,
791 "Cannot add value %d to the config element %s since it's already full (size=%d)",
792 val, name, variable->max);
794 xbt_dynar_push(variable->content, &val);
797 if (variable->cb_set)
798 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
799 variable->isdefault = 0;
802 /** @brief Set or add a double value to \a name within \a cfg
804 * \arg cfg the config set
805 * \arg name the name of the variable
806 * \arg val the doule to set
809 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
811 xbt_cfgelm_t variable;
813 VERB2("Configuration setting: %s=%f", name, val);
814 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
816 if (variable->max == 1) {
817 if (variable->cb_rm && xbt_dynar_length(variable->content))
818 (*variable->cb_rm) (name, 0);
820 xbt_dynar_set(variable->content, 0, &val);
822 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
823 THROW3(mismatch_error, 0,
824 "Cannot add value %f to the config element %s since it's already full (size=%d)",
825 val, name, variable->max);
827 xbt_dynar_push(variable->content, &val);
830 if (variable->cb_set)
831 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
832 variable->isdefault = 0;
835 /** @brief Set or add a string value to \a name within \a cfg
837 * \arg cfg the config set
838 * \arg name the name of the variable
839 * \arg val the value to be added
843 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
845 xbt_cfgelm_t variable;
846 char *newval = xbt_strdup(val);
848 VERB2("Configuration setting: %s=%s", name, val);
849 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
850 DEBUG5("Variable: %d to %d %s (=%d) @%p",
851 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
852 variable->type, variable);
854 if (variable->max == 1) {
855 if (xbt_dynar_length(variable->content)) {
857 (*variable->cb_rm) (name, 0);
858 else if (variable->type == xbt_cfgelm_string) {
859 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
864 xbt_dynar_set(variable->content, 0, &newval);
866 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
867 THROW3(mismatch_error, 0,
868 "Cannot add value %s to the config element %s since it's already full (size=%d)",
869 name, val, variable->max);
871 xbt_dynar_push(variable->content, &newval);
874 if (variable->cb_set)
875 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
876 variable->isdefault = 0;
879 /** @brief Set or add an peer value to \a name within \a cfg
881 * \arg cfg the config set
882 * \arg name the name of the variable
884 * \arg port the port number
886 * \e peer values are composed of a string (peername) and an integer (port)
890 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
892 xbt_cfgelm_t variable;
893 xbt_peer_t val = xbt_peer_new(peer, port);
895 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
897 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
899 if (variable->max == 1) {
900 if (variable->cb_rm && xbt_dynar_length(variable->content))
901 (*variable->cb_rm) (name, 0);
903 xbt_dynar_set(variable->content, 0, &val);
905 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
906 THROW4(mismatch_error, 0,
907 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
908 peer, port, name, variable->max);
910 xbt_dynar_push(variable->content, &val);
913 if (variable->cb_set)
914 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
915 variable->isdefault = 0;
918 /* ---- [ Removing ] ---- */
920 /** @brief Remove the provided \e val integer value from a variable
922 * \arg cfg the config set
923 * \arg name the name of the variable
924 * \arg val the value to be removed
926 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
929 xbt_cfgelm_t variable;
933 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
935 if (xbt_dynar_length(variable->content) == variable->min)
936 THROW3(mismatch_error, 0,
937 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
938 val, name, variable->min);
940 xbt_dynar_foreach(variable->content, cpt, seen) {
943 (*variable->cb_rm) (name, cpt);
944 xbt_dynar_cursor_rm(variable->content, &cpt);
949 THROW2(not_found_error, 0,
950 "Can't remove the value %d of config element %s: value not found.",
954 /** @brief Remove the provided \e val double value from a variable
956 * \arg cfg the config set
957 * \arg name the name of the variable
958 * \arg val the value to be removed
961 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
963 xbt_cfgelm_t variable;
967 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
969 if (xbt_dynar_length(variable->content) == variable->min)
970 THROW3(mismatch_error, 0,
971 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
972 val, name, variable->min);
974 xbt_dynar_foreach(variable->content, cpt, seen) {
976 xbt_dynar_cursor_rm(variable->content, &cpt);
978 (*variable->cb_rm) (name, cpt);
983 THROW2(not_found_error, 0,
984 "Can't remove the value %f of config element %s: value not found.",
988 /** @brief Remove the provided \e val string value from a variable
990 * \arg cfg the config set
991 * \arg name the name of the variable
992 * \arg val the value of the string which will be removed
994 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
996 xbt_cfgelm_t variable;
1000 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1002 if (xbt_dynar_length(variable->content) == variable->min)
1003 THROW3(mismatch_error, 0,
1004 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1005 name, val, variable->min);
1007 xbt_dynar_foreach(variable->content, cpt, seen) {
1008 if (!strcpy(seen, val)) {
1009 if (variable->cb_rm)
1010 (*variable->cb_rm) (name, cpt);
1011 xbt_dynar_cursor_rm(variable->content, &cpt);
1016 THROW2(not_found_error, 0,
1017 "Can't remove the value %s of config element %s: value not found.",
1021 /** @brief Remove the provided \e val peer value from a variable
1023 * \arg cfg the config set
1024 * \arg name the name of the variable
1025 * \arg peer the peername
1026 * \arg port the port number
1030 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
1032 xbt_cfgelm_t variable;
1036 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1038 if (xbt_dynar_length(variable->content) == variable->min)
1039 THROW4(mismatch_error, 0,
1040 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1041 peer, port, name, variable->min);
1043 xbt_dynar_foreach(variable->content, cpt, seen) {
1044 if (!strcpy(seen->name, peer) && seen->port == port) {
1045 if (variable->cb_rm)
1046 (*variable->cb_rm) (name, cpt);
1047 xbt_dynar_cursor_rm(variable->content, &cpt);
1052 THROW3(not_found_error, 0,
1053 "Can't remove the value %s:%d of config element %s: value not found.",
1057 /** @brief Remove the \e pos th value from the provided variable */
1059 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1062 xbt_cfgelm_t variable;
1064 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1066 if (xbt_dynar_length(variable->content) == variable->min)
1067 THROW3(mismatch_error, 0,
1068 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1069 pos, name, variable->min);
1071 if (variable->cb_rm)
1072 (*variable->cb_rm) (name, pos);
1073 xbt_dynar_remove_at(variable->content, pos, NULL);
1076 /** @brief Remove all the values from a variable
1078 * \arg cfg the config set
1079 * \arg name the name of the variable
1082 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1084 xbt_cfgelm_t variable = NULL;
1088 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1090 if (e.category != not_found_error)
1094 THROW1(not_found_error, 0,
1095 "Can't empty '%s' since this config element does not exist",
1100 if (variable->cb_rm) {
1103 xbt_dynar_foreach(variable->content, cpt, ignored) {
1104 (*variable->cb_rm) (name, cpt);
1107 xbt_dynar_reset(variable->content);
1111 /*----[ Getting ]---------------------------------------------------------*/
1113 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1115 * \arg cfg the config set
1116 * \arg name the name of the variable
1117 * \arg val the wanted value
1119 * Returns the first value from the config set under the given name.
1120 * If there is more than one value, it will issue a warning. Consider using
1121 * xbt_cfg_get_dynar() instead.
1123 * \warning the returned value is the actual content of the config set
1125 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1127 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1129 if (xbt_dynar_length(variable->content) > 1) {
1131 ("You asked for the first value of the config element '%s', but there is %lu values",
1132 name, xbt_dynar_length(variable->content));
1135 return xbt_dynar_get_as(variable->content, 0, int);
1138 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1140 * \arg cfg the config set
1141 * \arg name the name of the variable
1142 * \arg val the wanted value
1144 * Returns the first value from the config set under the given name.
1145 * If there is more than one value, it will issue a warning. Consider using
1146 * xbt_cfg_get_dynar() instead.
1148 * \warning the returned value is the actual content of the config set
1151 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1153 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1155 if (xbt_dynar_length(variable->content) > 1) {
1157 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1158 name, xbt_dynar_length(variable->content));
1161 return xbt_dynar_get_as(variable->content, 0, double);
1164 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1166 * \arg th the config set
1167 * \arg name the name of the variable
1168 * \arg val the wanted value
1170 * Returns the first value from the config set under the given name.
1171 * If there is more than one value, it will issue a warning. Consider using
1172 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1174 * \warning the returned value is the actual content of the config set
1177 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1179 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1181 if (xbt_dynar_length(variable->content) > 1) {
1183 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1184 name, xbt_dynar_length(variable->content));
1185 } else if (xbt_dynar_length(variable->content) == 0) {
1189 return xbt_dynar_get_as(variable->content, 0, char *);
1192 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1194 * \arg cfg the config set
1195 * \arg name the name of the variable
1196 * \arg peer the peer
1197 * \arg port the port number
1199 * Returns the first value from the config set under the given name.
1200 * If there is more than one value, it will issue a warning. Consider using
1201 * xbt_cfg_get_dynar() instead.
1203 * \warning the returned value is the actual content of the config set
1206 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1208 xbt_cfgelm_t variable;
1211 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1213 if (xbt_dynar_length(variable->content) > 1) {
1215 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1216 name, xbt_dynar_length(variable->content));
1219 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1224 /** @brief Retrieve the dynar of all the values stored in a variable
1226 * \arg cfg where to search in
1227 * \arg name what to search for
1230 * Get the data stored in the config set.
1232 * \warning the returned value is the actual content of the config set
1234 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1236 xbt_cfgelm_t variable = NULL;
1240 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1242 if (e.category == not_found_error) {
1244 THROW1(not_found_error, 0,
1245 "No registered variable %s in this config set", name);
1250 return variable->content;
1254 /** @brief Retrieve one of the integer value of a variable */
1255 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1258 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1259 return xbt_dynar_get_as(variable->content, pos, int);
1262 /** @brief Retrieve one of the double value of a variable */
1263 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1266 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1267 return xbt_dynar_get_as(variable->content, pos, double);
1271 /** @brief Retrieve one of the string value of a variable */
1272 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1275 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1276 return xbt_dynar_get_as(variable->content, pos, char *);
1279 /** @brief Retrieve one of the peer value of a variable */
1281 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1282 char **peer, int *port)
1285 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1286 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1297 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1299 XBT_TEST_SUITE("config", "Configuration support");
1301 static xbt_cfg_t make_set()
1303 xbt_cfg_t set = NULL;
1305 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1306 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1307 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1308 xbt_cfg_register_str(&set, "user:1_to_10_string");
1311 } /* end_of_make_set */
1313 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1315 xbt_cfg_t set = make_set();
1316 xbt_test_add0("Alloc and free a config set");
1317 xbt_cfg_set_parse(set,
1318 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1323 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1325 xbt_cfg_t set = set = make_set();
1328 xbt_test_add0("Having too few elements for speed");
1329 xbt_cfg_set_parse(set,
1330 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1335 if (e.category != mismatch_error ||
1336 strncmp(e.msg, "Config elem speed needs",
1337 strlen("Config elem speed needs")))
1338 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1346 xbt_test_add0("Having too much values of 'speed'");
1348 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1350 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1353 if (e.category != mismatch_error ||
1354 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1355 strlen("Config elem speed needs")))
1356 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1365 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1368 xbt_test_add0("Get a single value");
1370 /* get_single_value */
1372 xbt_cfg_t myset = make_set();
1374 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1375 ival = xbt_cfg_get_int(myset, "speed");
1377 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1378 xbt_cfg_free(&myset);
1381 xbt_test_add0("Get multiple values");
1383 /* get_multiple_value */
1385 xbt_cfg_t myset = make_set();
1387 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1388 xbt_cfg_set_parse(myset, "speed:42");
1389 xbt_cfg_check(myset);
1390 dyn = xbt_cfg_get_dynar(myset, "user");
1392 if (xbt_dynar_length(dyn) != 3)
1393 xbt_test_fail1("Dynar length = %lu, I expected 3",
1394 xbt_dynar_length(dyn));
1396 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1397 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1398 xbt_dynar_get_as(dyn, 0, char *));
1400 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1401 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1402 xbt_dynar_get_as(dyn, 1, char *));
1404 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1405 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1406 xbt_dynar_get_as(dyn, 2, char *));
1407 xbt_cfg_free(&myset);
1410 xbt_test_add0("Access to a non-existant entry");
1412 /* non-existant_entry */
1413 xbt_cfg_t myset = make_set();
1417 xbt_cfg_set_parse(myset, "color:blue");
1419 if (e.category != not_found_error)
1420 xbt_test_exception(e);
1423 xbt_cfg_free(&myset);
1426 #endif /* SIMGRID_TEST */