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 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
25 { "int", "double", "string", "peer", "any" };
27 /* Internal stuff used in cache to free a variable */
28 static void xbt_cfgelm_free(void *data);
30 /*----[ Memory management ]-----------------------------------------------*/
32 /** @brief Constructor
34 * Initialise an config set
38 xbt_cfg_t xbt_cfg_new(void)
40 return (xbt_cfg_t) xbt_dict_new();
43 /** \brief Copy an existing configuration set
45 * \arg whereto the config set to be created
46 * \arg tocopy the source data
48 * This only copy the registrations, not the actual content
51 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
53 xbt_dict_cursor_t cursor = NULL;
54 xbt_cfgelm_t variable = NULL;
57 DEBUG1("Copy cfg set %p", tocopy);
59 xbt_assert0(tocopy, "cannot copy NULL config");
61 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
62 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
63 variable->min, variable->max, variable->cb_set,
68 /** @brief Destructor */
69 void xbt_cfg_free(xbt_cfg_t * cfg)
71 DEBUG1("Frees cfg set %p", cfg);
72 xbt_dict_free((xbt_dict_t *) cfg);
75 /** @brief Dump a config set for debuging purpose
77 * \arg name The name to give to this config set
78 * \arg indent what to write at the begining of each line (right number of spaces)
79 * \arg cfg the config set
81 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
83 xbt_dict_t dict = (xbt_dict_t) cfg;
84 xbt_dict_cursor_t cursor = NULL;
85 xbt_cfgelm_t variable = NULL;
95 printf("%s>> Dumping of the config set '%s':\n", indent, name);
96 xbt_dict_foreach(dict, cursor, key, variable) {
98 printf("%s %s:", indent, key);
100 size = xbt_dynar_length(variable->content);
102 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
103 variable->min, variable->max,
104 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
107 switch (variable->type) {
110 for (i = 0; i < size; i++) {
111 ival = xbt_dynar_get_as(variable->content, i, int);
112 printf("%s %d\n", indent, ival);
116 case xbt_cfgelm_double:
117 for (i = 0; i < size; i++) {
118 dval = xbt_dynar_get_as(variable->content, i, double);
119 printf("%s %f\n", indent, dval);
123 case xbt_cfgelm_string:
124 for (i = 0; i < size; i++) {
125 sval = xbt_dynar_get_as(variable->content, i, char *);
126 printf("%s %s\n", indent, sval);
130 case xbt_cfgelm_peer:
131 for (i = 0; i < size; i++) {
132 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
133 printf("%s %s:%d\n", indent, hval->name, hval->port);
138 printf("%s Invalid type!!\n", indent);
144 printf("%s<< End of the config set '%s'\n", indent, name);
147 xbt_dict_cursor_free(&cursor);
152 * free an config element
155 void xbt_cfgelm_free(void *data)
157 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
159 DEBUG1("Frees cfgelm %p", c);
163 xbt_dynar_free(&(c->content));
167 /*----[ Registering stuff ]-----------------------------------------------*/
169 /** @brief Register an element within a config set
171 * @arg cfg the config set
172 * @arg type the type of the config element
173 * @arg min the minimum
174 * @arg max the maximum
178 xbt_cfg_register(xbt_cfg_t * cfg,
179 const char *name, const char *desc,
180 e_xbt_cfgelm_type_t type, void *default_value, int min,
181 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
186 *cfg = xbt_cfg_new();
187 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
188 "type of %s not valid (%d should be between %d and %d)",
189 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
190 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
193 WARN1("Config elem %s registered twice.", name);
194 /* Will be removed by the insertion of the new one */
197 res = xbt_new(s_xbt_cfgelm_t, 1);
198 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
199 name, desc, min, max, xbt_cfgelm_type_name[type], type, res,
202 res->desc = xbt_strdup(desc);
206 res->cb_set = cb_set;
212 res->content = xbt_dynar_new(sizeof(int), NULL);
214 xbt_dynar_push(res->content, default_value);
217 case xbt_cfgelm_double:
218 res->content = xbt_dynar_new(sizeof(double), NULL);
220 xbt_dynar_push(res->content, default_value);
223 case xbt_cfgelm_string:
224 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
226 xbt_dynar_push(res->content, default_value);
229 case xbt_cfgelm_peer:
230 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
232 xbt_dynar_push(res->content, default_value);
236 ERROR1("%d is an invalide type code", type);
239 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
242 /** @brief Unregister an element from a config set.
244 * @arg cfg the config set
245 * @arg name the name of the elem to be freed
247 * Note that it removes both the description and the actual content.
248 * Throws not_found when no such element exists.
251 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
253 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
254 xbt_dict_remove((xbt_dict_t) cfg, name);
258 * @brief Parse a string and register the stuff described.
260 * @arg cfg the config set
261 * @arg entry a string describing the element to register
263 * The string may consist in several variable descriptions separated by a space.
264 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
265 * with type being one of 'string','int', 'peer' or 'double'.
267 * @fixme: this does not allow to set the description
270 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
272 char *entrycpy = xbt_strdup(entry);
276 e_xbt_cfgelm_type_t type;
277 DEBUG1("Register string '%s'", entry);
279 tok = strchr(entrycpy, ':');
280 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
281 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
284 min = strtol(tok, &tok, 10);
285 xbt_assert1(tok, "Invalid minimum in config element descriptor %s",
288 xbt_assert2(strcmp(tok, "_to_"),
289 "Invalid config element descriptor : %s%s",
290 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
291 tok += strlen("_to_");
293 max = strtol(tok, &tok, 10);
294 xbt_assert1(tok, "Invalid maximum in config element descriptor %s",
297 xbt_assert2(*(tok++) == '_',
298 "Invalid config element descriptor: %s%s", entry,
299 "; Should be <name>:<min nb>_to_<max nb>_<type>");
302 type < xbt_cfgelm_type_count
303 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
304 xbt_assert2(type < xbt_cfgelm_type_count,
305 "Invalid type in config element descriptor: %s%s", entry,
306 "; Should be one of 'string', 'int', 'peer' or 'double'.");
308 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
310 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
313 /** @brief Displays the declared options and their description */
314 void xbt_cfg_help(xbt_cfg_t cfg)
316 xbt_dict_cursor_t cursor;
317 xbt_cfgelm_t variable;
323 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
324 printf(" %s: %s\n", name, variable->desc);
325 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
326 if (variable->min != 1 || variable->max != 1) {
327 if (variable->min == 0 && variable->max == 0)
328 printf("Arity: no bound; ");
330 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
332 printf("Current value: ");
333 size = xbt_dynar_length(variable->content);
335 switch (variable->type) {
342 for (i = 0; i < size; i++) {
343 ival = xbt_dynar_get_as(variable->content, i, int);
344 printf("%s%d\n", (i == 0 ? "" : " "), ival);
348 case xbt_cfgelm_double:
349 for (i = 0; i < size; i++) {
350 dval = xbt_dynar_get_as(variable->content, i, double);
351 printf("%s%f\n", (i == 0 ? "" : " "), dval);
355 case xbt_cfgelm_string:
356 for (i = 0; i < size; i++) {
357 sval = xbt_dynar_get_as(variable->content, i, char *);
358 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
362 case xbt_cfgelm_peer:
363 for (i = 0; i < size; i++) {
364 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
365 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
371 printf("Invalid type!!\n");
377 /** @brief Check that each variable have the right amount of values */
378 void xbt_cfg_check(xbt_cfg_t cfg)
380 xbt_dict_cursor_t cursor;
381 xbt_cfgelm_t variable;
385 xbt_assert0(cfg, "NULL config set.");
386 DEBUG1("Check cfg set %p", cfg);
388 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
389 size = xbt_dynar_length(variable->content);
390 if (variable->min > size) {
391 xbt_dict_cursor_free(&cursor);
392 THROW4(mismatch_error, 0,
393 "Config elem %s needs at least %d %s, but there is only %d values.",
394 name, variable->min, xbt_cfgelm_type_name[variable->type],
398 if (variable->max > 0 && variable->max < size) {
399 xbt_dict_cursor_free(&cursor);
400 THROW4(mismatch_error, 0,
401 "Config elem %s accepts at most %d %s, but there is %d values.",
402 name, variable->max, xbt_cfgelm_type_name[variable->type],
407 xbt_dict_cursor_free(&cursor);
410 xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
412 e_xbt_cfgelm_type_t type)
414 xbt_cfgelm_t res = NULL;
416 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
419 THROW1(not_found_error, 0,
420 "No registered variable '%s' in this config set", name);
423 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
424 "You tried to access to the config element %s as an %s, but its type is %s.",
426 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
431 /** @brief Get the type of this variable in that configuration set
433 * \arg cfg the config set
434 * \arg name the name of the element
435 * \arg type the result
439 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
442 xbt_cfgelm_t variable = NULL;
444 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
446 THROW1(not_found_error, 0,
447 "Can't get the type of '%s' since this variable does not exist",
450 INFO1("type in variable = %d", variable->type);
452 return variable->type;
455 /*----[ Setting ]---------------------------------------------------------*/
456 /** @brief va_args version of xbt_cfg_set
458 * \arg cfg config set to fill
459 * \arg n variable name
460 * \arg pa variable value
462 * Add some values to the config set.
464 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
469 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
474 type = xbt_cfg_get_type(cfg, name);
476 if (e.category == not_found_error) {
478 THROW1(not_found_error, 0,
479 "Can't set the property '%s' since it's not registered",
486 case xbt_cfgelm_peer:
487 str = va_arg(pa, char *);
489 xbt_cfg_set_peer(cfg, name, str, i);
492 case xbt_cfgelm_string:
493 str = va_arg(pa, char *);
494 xbt_cfg_set_string(cfg, name, str);
499 xbt_cfg_set_int(cfg, name, i);
502 case xbt_cfgelm_double:
503 d = va_arg(pa, double);
504 xbt_cfg_set_double(cfg, name, d);
508 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
513 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
515 * \arg cfg config set to fill
516 * \arg name variable name
517 * \arg varargs variable value
520 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
525 xbt_cfg_set_vargs(cfg, name, pa);
529 /** @brief Add values parsed from a string into a config set
531 * \arg cfg config set to fill
532 * \arg options a string containing the content to add to the config set. This
533 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
534 * like "[name]:[value]" where [name] is the name of an already registred
535 * variable, and [value] conforms to the data type under which this variable was
538 * @todo This is a crude manual parser, it should be a proper lexer.
541 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
549 xbt_cfgelm_t variable = NULL;
550 char *optionlist_cpy;
551 char *option, *name, *val;
556 if (!options || !strlen(options)) { /* nothing to do */
559 optionlist_cpy = xbt_strdup(options);
561 DEBUG1("List to parse and set:'%s'", options);
562 option = optionlist_cpy;
563 while (1) { /* breaks in the code */
569 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
570 name, len, (long) (option - name));
573 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
574 && *option != '\t' && *option != ',') {
575 DEBUG1("Take %c.", *option);
578 if (option - name == len) {
579 DEBUG0("Boundary=EOL");
580 option = NULL; /* don't do next iteration */
583 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
584 *option, len, (long) (option - name));
586 /* Pass the following blank chars */
588 while (option - name < (len - 1) &&
589 (*option == ' ' || *option == '\n' || *option == '\t')) {
590 /* fprintf(stderr,"Ignore a blank char.\n"); */
593 if (option - name == len - 1)
594 option = NULL; /* don't do next iteration */
596 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
598 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
603 val = strchr(name, ':');
605 free(optionlist_cpy);
607 "Option '%s' badly formated. Should be of the form 'name:value'",
612 if(strcmp(name,"simix/context"))
613 INFO2("Configuration change: Set '%s' to '%s'", name, val);
616 variable = xbt_dict_get((xbt_dict_t) cfg, name);
619 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
620 name = (char *) (optionlist_cpy - name + options);
621 free(optionlist_cpy);
622 if (e.category == not_found_error) {
624 THROW1(not_found_error, 0,
625 "No registered variable corresponding to '%s'.", name);
631 switch (variable->type) {
632 case xbt_cfgelm_string:
633 xbt_cfg_set_string(cfg, name, val); /* throws */
637 i = strtol(val, &val, 0);
639 free(optionlist_cpy);
641 "Value of option %s not valid. Should be an integer",
645 xbt_cfg_set_int(cfg, name, i); /* throws */
648 case xbt_cfgelm_double:
649 d = strtod(val, &val);
651 free(optionlist_cpy);
653 "Value of option %s not valid. Should be a double",
657 xbt_cfg_set_double(cfg, name, d); /* throws */
660 case xbt_cfgelm_peer:
662 val = strchr(val, ':');
664 free(optionlist_cpy);
666 "Value of option %s not valid. Should be an peer (machine:port)",
671 i = strtol(val, &val, 0);
673 free(optionlist_cpy);
675 "Value of option %s not valid. Should be an peer (machine:port)",
679 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
683 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
688 free(optionlist_cpy);
692 free(optionlist_cpy);
696 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
698 * This is useful to change the default value of a variable while allowing
699 * users to override it with command line arguments
701 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
703 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
705 if (variable->isdefault){
706 xbt_cfg_set_int(cfg, name, val);
707 variable->isdefault = 1;
711 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
715 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
717 * This is useful to change the default value of a variable while allowing
718 * users to override it with command line arguments
720 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
722 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
724 if (variable->isdefault) {
725 xbt_cfg_set_double(cfg, name, val);
726 variable->isdefault = 1;
730 ("Do not override configuration variable '%s' with value '%lf' because it was already set.",
734 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
736 * This is useful to change the default value of a variable while allowing
737 * users to override it with command line arguments
739 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
742 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
744 if (variable->isdefault){
745 xbt_cfg_set_string(cfg, name, val);
746 variable->isdefault = 1;
750 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
754 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
756 * This is useful to change the default value of a variable while allowing
757 * users to override it with command line arguments
759 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
760 const char *host, int port)
762 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
764 if (variable->isdefault){
765 xbt_cfg_set_peer(cfg, name, host, port);
766 variable->isdefault = 1;
770 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
774 /** @brief Set or add an integer value to \a name within \a cfg
776 * \arg cfg the config set
777 * \arg name the name of the variable
778 * \arg val the value of the variable
780 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
782 xbt_cfgelm_t variable;
784 VERB2("Configuration setting: %s=%d", name, val);
785 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
787 if (variable->max == 1) {
788 if (variable->cb_rm && xbt_dynar_length(variable->content))
789 (*variable->cb_rm) (name, 0);
791 xbt_dynar_set(variable->content, 0, &val);
794 && xbt_dynar_length(variable->content) ==
795 (unsigned long) variable->max)
796 THROW3(mismatch_error, 0,
797 "Cannot add value %d to the config element %s since it's already full (size=%d)",
798 val, name, variable->max);
800 xbt_dynar_push(variable->content, &val);
803 if (variable->cb_set)
804 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
805 variable->isdefault = 0;
808 /** @brief Set or add a double value to \a name within \a cfg
810 * \arg cfg the config set
811 * \arg name the name of the variable
812 * \arg val the doule to set
815 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
817 xbt_cfgelm_t variable;
819 VERB2("Configuration setting: %s=%f", name, val);
820 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
822 if (variable->max == 1) {
823 if (variable->cb_rm && xbt_dynar_length(variable->content))
824 (*variable->cb_rm) (name, 0);
826 xbt_dynar_set(variable->content, 0, &val);
829 && xbt_dynar_length(variable->content) == variable->max)
830 THROW3(mismatch_error, 0,
831 "Cannot add value %f to the config element %s since it's already full (size=%d)",
832 val, name, variable->max);
834 xbt_dynar_push(variable->content, &val);
837 if (variable->cb_set)
838 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
839 variable->isdefault = 0;
842 /** @brief Set or add a string value to \a name within \a cfg
844 * \arg cfg the config set
845 * \arg name the name of the variable
846 * \arg val the value to be added
850 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
852 xbt_cfgelm_t variable;
853 char *newval = xbt_strdup(val);
855 VERB2("Configuration setting: %s=%s", name, val);
856 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
857 DEBUG5("Variable: %d to %d %s (=%d) @%p",
858 variable->min, variable->max,
859 xbt_cfgelm_type_name[variable->type], variable->type, variable);
861 if (variable->max == 1) {
862 if (xbt_dynar_length(variable->content)) {
864 (*variable->cb_rm) (name, 0);
865 else if (variable->type == xbt_cfgelm_string) {
866 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
871 xbt_dynar_set(variable->content, 0, &newval);
874 && xbt_dynar_length(variable->content) == variable->max)
875 THROW3(mismatch_error, 0,
876 "Cannot add value %s to the config element %s since it's already full (size=%d)",
877 name, val, variable->max);
879 xbt_dynar_push(variable->content, &newval);
882 if (variable->cb_set)
883 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
884 variable->isdefault = 0;
887 /** @brief Set or add an peer value to \a name within \a cfg
889 * \arg cfg the config set
890 * \arg name the name of the variable
892 * \arg port the port number
894 * \e peer values are composed of a string (peername) and an integer (port)
898 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
901 xbt_cfgelm_t variable;
902 xbt_peer_t val = xbt_peer_new(peer, port);
904 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
906 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
908 if (variable->max == 1) {
909 if (variable->cb_rm && xbt_dynar_length(variable->content))
910 (*variable->cb_rm) (name, 0);
912 xbt_dynar_set(variable->content, 0, &val);
915 && xbt_dynar_length(variable->content) == variable->max)
916 THROW4(mismatch_error, 0,
917 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
918 peer, port, name, variable->max);
920 xbt_dynar_push(variable->content, &val);
923 if (variable->cb_set)
924 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
925 variable->isdefault = 0;
928 /* ---- [ Removing ] ---- */
930 /** @brief Remove the provided \e val integer value from a variable
932 * \arg cfg the config set
933 * \arg name the name of the variable
934 * \arg val the value to be removed
936 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
939 xbt_cfgelm_t variable;
943 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
945 if (xbt_dynar_length(variable->content) == variable->min)
946 THROW3(mismatch_error, 0,
947 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
948 val, name, variable->min);
950 xbt_dynar_foreach(variable->content, cpt, seen) {
953 (*variable->cb_rm) (name, cpt);
954 xbt_dynar_cursor_rm(variable->content, &cpt);
959 THROW2(not_found_error, 0,
960 "Can't remove the value %d of config element %s: value not found.",
964 /** @brief Remove the provided \e val double value from a variable
966 * \arg cfg the config set
967 * \arg name the name of the variable
968 * \arg val the value to be removed
971 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
973 xbt_cfgelm_t variable;
977 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
979 if (xbt_dynar_length(variable->content) == variable->min)
980 THROW3(mismatch_error, 0,
981 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
982 val, name, variable->min);
984 xbt_dynar_foreach(variable->content, cpt, seen) {
986 xbt_dynar_cursor_rm(variable->content, &cpt);
988 (*variable->cb_rm) (name, cpt);
993 THROW2(not_found_error, 0,
994 "Can't remove the value %f of config element %s: value not found.",
998 /** @brief Remove the provided \e val string value from a variable
1000 * \arg cfg the config set
1001 * \arg name the name of the variable
1002 * \arg val the value of the string which will be removed
1004 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1006 xbt_cfgelm_t variable;
1010 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1012 if (xbt_dynar_length(variable->content) == variable->min)
1013 THROW3(mismatch_error, 0,
1014 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1015 name, val, variable->min);
1017 xbt_dynar_foreach(variable->content, cpt, seen) {
1018 if (!strcpy(seen, val)) {
1019 if (variable->cb_rm)
1020 (*variable->cb_rm) (name, cpt);
1021 xbt_dynar_cursor_rm(variable->content, &cpt);
1026 THROW2(not_found_error, 0,
1027 "Can't remove the value %s of config element %s: value not found.",
1031 /** @brief Remove the provided \e val peer value from a variable
1033 * \arg cfg the config set
1034 * \arg name the name of the variable
1035 * \arg peer the peername
1036 * \arg port the port number
1040 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1043 xbt_cfgelm_t variable;
1047 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1049 if (xbt_dynar_length(variable->content) == variable->min)
1050 THROW4(mismatch_error, 0,
1051 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1052 peer, port, name, variable->min);
1054 xbt_dynar_foreach(variable->content, cpt, seen) {
1055 if (!strcpy(seen->name, peer) && seen->port == port) {
1056 if (variable->cb_rm)
1057 (*variable->cb_rm) (name, cpt);
1058 xbt_dynar_cursor_rm(variable->content, &cpt);
1063 THROW3(not_found_error, 0,
1064 "Can't remove the value %s:%d of config element %s: value not found.",
1068 /** @brief Remove the \e pos th value from the provided variable */
1070 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1073 xbt_cfgelm_t variable;
1075 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1077 if (xbt_dynar_length(variable->content) == variable->min)
1078 THROW3(mismatch_error, 0,
1079 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1080 pos, name, variable->min);
1082 if (variable->cb_rm)
1083 (*variable->cb_rm) (name, pos);
1084 xbt_dynar_remove_at(variable->content, pos, NULL);
1087 /** @brief Remove all the values from a variable
1089 * \arg cfg the config set
1090 * \arg name the name of the variable
1093 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1095 xbt_cfgelm_t variable = NULL;
1099 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1101 if (e.category != not_found_error)
1105 THROW1(not_found_error, 0,
1106 "Can't empty '%s' since this config element does not exist",
1111 if (variable->cb_rm) {
1114 xbt_dynar_foreach(variable->content, cpt, ignored) {
1115 (*variable->cb_rm) (name, cpt);
1118 xbt_dynar_reset(variable->content);
1122 /*----[ Getting ]---------------------------------------------------------*/
1124 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1126 * \arg cfg the config set
1127 * \arg name the name of the variable
1128 * \arg val the wanted value
1130 * Returns the first value from the config set under the given name.
1131 * If there is more than one value, it will issue a warning. Consider using
1132 * xbt_cfg_get_dynar() instead.
1134 * \warning the returned value is the actual content of the config set
1136 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1138 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1140 if (xbt_dynar_length(variable->content) > 1) {
1142 ("You asked for the first value of the config element '%s', but there is %lu values",
1143 name, xbt_dynar_length(variable->content));
1146 return xbt_dynar_get_as(variable->content, 0, int);
1149 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1151 * \arg cfg the config set
1152 * \arg name the name of the variable
1153 * \arg val the wanted value
1155 * Returns the first value from the config set under the given name.
1156 * If there is more than one value, it will issue a warning. Consider using
1157 * xbt_cfg_get_dynar() instead.
1159 * \warning the returned value is the actual content of the config set
1162 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1164 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1166 if (xbt_dynar_length(variable->content) > 1) {
1168 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1169 name, xbt_dynar_length(variable->content));
1172 return xbt_dynar_get_as(variable->content, 0, double);
1175 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1177 * \arg th the config set
1178 * \arg name the name of the variable
1179 * \arg val the wanted value
1181 * Returns the first value from the config set under the given name.
1182 * If there is more than one value, it will issue a warning. Consider using
1183 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1185 * \warning the returned value is the actual content of the config set
1188 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1190 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1192 if (xbt_dynar_length(variable->content) > 1) {
1194 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1195 name, xbt_dynar_length(variable->content));
1196 } else if (xbt_dynar_length(variable->content) == 0) {
1200 return xbt_dynar_get_as(variable->content, 0, char *);
1203 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1205 * \arg cfg the config set
1206 * \arg name the name of the variable
1207 * \arg peer the peer
1208 * \arg port the port number
1210 * Returns the first value from the config set under the given name.
1211 * If there is more than one value, it will issue a warning. Consider using
1212 * xbt_cfg_get_dynar() instead.
1214 * \warning the returned value is the actual content of the config set
1217 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1220 xbt_cfgelm_t variable;
1223 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1225 if (xbt_dynar_length(variable->content) > 1) {
1227 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1228 name, xbt_dynar_length(variable->content));
1231 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1236 /** @brief Retrieve the dynar of all the values stored in a variable
1238 * \arg cfg where to search in
1239 * \arg name what to search for
1242 * Get the data stored in the config set.
1244 * \warning the returned value is the actual content of the config set
1246 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1248 xbt_cfgelm_t variable = NULL;
1252 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1254 if (e.category == not_found_error) {
1256 THROW1(not_found_error, 0,
1257 "No registered variable %s in this config set", name);
1262 return variable->content;
1266 /** @brief Retrieve one of the integer value of a variable */
1267 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1270 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1271 return xbt_dynar_get_as(variable->content, pos, int);
1274 /** @brief Retrieve one of the double value of a variable */
1275 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1278 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1279 return xbt_dynar_get_as(variable->content, pos, double);
1283 /** @brief Retrieve one of the string value of a variable */
1284 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1287 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1288 return xbt_dynar_get_as(variable->content, pos, char *);
1291 /** @brief Retrieve one of the peer value of a variable */
1293 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1294 char **peer, int *port)
1297 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1298 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1309 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1310 XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
1312 XBT_TEST_SUITE("config", "Configuration support");
1314 static xbt_cfg_t make_set()
1316 xbt_cfg_t set = NULL;
1318 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1319 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1320 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1321 xbt_cfg_register_str(&set, "user:1_to_10_string");
1324 } /* end_of_make_set */
1326 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1328 xbt_cfg_t set = make_set();
1329 xbt_test_add0("Alloc and free a config set");
1330 xbt_cfg_set_parse(set,
1331 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1336 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1338 xbt_cfg_t set = set = make_set();
1341 xbt_test_add0("Having too few elements for speed");
1342 xbt_cfg_set_parse(set,
1343 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1348 if (e.category != mismatch_error ||
1349 strncmp(e.msg, "Config elem speed needs",
1350 strlen("Config elem speed needs")))
1351 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1359 xbt_test_add0("Having too much values of 'speed'");
1361 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1363 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1366 if (e.category != mismatch_error ||
1367 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1368 strlen("Config elem speed needs")))
1369 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1378 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1381 xbt_test_add0("Get a single value");
1383 /* get_single_value */
1385 xbt_cfg_t myset = make_set();
1387 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1388 ival = xbt_cfg_get_int(myset, "speed");
1390 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1391 xbt_cfg_free(&myset);
1394 xbt_test_add0("Get multiple values");
1396 /* get_multiple_value */
1398 xbt_cfg_t myset = make_set();
1400 xbt_cfg_set_parse(myset,
1401 "peername:veloce user:foo\nuser:bar\tuser:toto");
1402 xbt_cfg_set_parse(myset, "speed:42");
1403 xbt_cfg_check(myset);
1404 dyn = xbt_cfg_get_dynar(myset, "user");
1406 if (xbt_dynar_length(dyn) != 3)
1407 xbt_test_fail1("Dynar length = %lu, I expected 3",
1408 xbt_dynar_length(dyn));
1410 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1411 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1412 xbt_dynar_get_as(dyn, 0, char *));
1414 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1415 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1416 xbt_dynar_get_as(dyn, 1, char *));
1418 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1419 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1420 xbt_dynar_get_as(dyn, 2, char *));
1421 xbt_cfg_free(&myset);
1424 xbt_test_add0("Access to a non-existant entry");
1426 /* non-existant_entry */
1427 xbt_cfg_t myset = make_set();
1431 xbt_cfg_set_parse(myset, "color:blue");
1433 if (e.category != not_found_error)
1434 xbt_test_exception(e);
1437 xbt_cfg_free(&myset);
1440 #endif /* SIMGRID_TEST */