3 /* config - Dictionnary where the type of each variable is provided. */
5 /* This is useful to build named structs, like option or property sets. */
7 /* Copyright (c) 2001,2002,2003,2004 Martin Quinson. All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "xbt/sysdep.h"
17 #include "xbt/dynar.h"
21 #include "xbt/config.h" /* prototypes of this module */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
25 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27 Both data and DTD are mixed, but fixing it now would prevent me to ever
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
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;
236 res->content = xbt_dynar_new(sizeof(int), NULL);
238 xbt_dynar_push(res->content, default_value);
241 case xbt_cfgelm_double:
242 res->content = xbt_dynar_new(sizeof(double), NULL);
244 xbt_dynar_push(res->content, default_value);
247 case xbt_cfgelm_string:
248 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
250 xbt_dynar_push(res->content, default_value);
253 case xbt_cfgelm_peer:
254 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
256 xbt_dynar_push(res->content, default_value);
260 ERROR1("%d is an invalide type code", type);
263 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
266 /** @brief Unregister an element from a config set.
268 * @arg cfg the config set
269 * @arg name the name of the elem to be freed
271 * Note that it removes both the description and the actual content.
272 * Throws not_found when no such element exists.
275 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
277 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
278 xbt_dict_remove((xbt_dict_t) cfg, name);
282 * @brief Parse a string and register the stuff described.
284 * @arg cfg the config set
285 * @arg entry a string describing the element to register
287 * The string may consist in several variable descriptions separated by a space.
288 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
289 * with type being one of 'string','int', 'peer' or 'double'.
291 * @fixme: this does not allow to set the description
294 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
296 char *entrycpy = xbt_strdup(entry);
300 e_xbt_cfgelm_type_t type;
301 DEBUG1("Register string '%s'", entry);
303 tok = strchr(entrycpy, ':');
304 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
305 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
308 min = strtol(tok, &tok, 10);
309 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
311 xbt_assert2(strcmp(tok, "_to_"),
312 "Invalid config element descriptor : %s%s",
313 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
314 tok += strlen("_to_");
316 max = strtol(tok, &tok, 10);
317 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
319 xbt_assert2(*(tok++) == '_',
320 "Invalid config element descriptor: %s%s", entry,
321 "; Should be <name>:<min nb>_to_<max nb>_<type>");
324 type < xbt_cfgelm_type_count
325 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
326 xbt_assert2(type < xbt_cfgelm_type_count,
327 "Invalid type in config element descriptor: %s%s", entry,
328 "; Should be one of 'string', 'int', 'peer' or 'double'.");
330 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
332 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
335 /** @brief Displays the declared options and their description */
336 void xbt_cfg_help(xbt_cfg_t cfg)
338 xbt_dict_cursor_t cursor;
339 xbt_cfgelm_t variable;
345 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
346 printf(" %s: %s\n", name, variable->desc);
347 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
348 if (variable->min != 1 || variable->max != 1)
349 printf("Arrity: min:%d to max:%d; ", variable->min, variable->max);
350 printf("Current value: ");
351 size = xbt_dynar_length(variable->content);
353 switch (variable->type) {
360 for (i = 0; i < size; i++) {
361 ival = xbt_dynar_get_as(variable->content, i, int);
362 printf("%s%d\n", (i == 0 ? "" : " "), ival);
366 case xbt_cfgelm_double:
367 for (i = 0; i < size; i++) {
368 dval = xbt_dynar_get_as(variable->content, i, double);
369 printf("%s%f\n", (i == 0 ? "" : " "), dval);
373 case xbt_cfgelm_string:
374 for (i = 0; i < size; i++) {
375 sval = xbt_dynar_get_as(variable->content, i, char *);
376 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
380 case xbt_cfgelm_peer:
381 for (i = 0; i < size; i++) {
382 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
383 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
389 printf("Invalid type!!\n");
395 /** @brief Check that each variable have the right amount of values */
396 void xbt_cfg_check(xbt_cfg_t cfg)
398 xbt_dict_cursor_t cursor;
399 xbt_cfgelm_t variable;
403 xbt_assert0(cfg, "NULL config set.");
404 DEBUG1("Check cfg set %p", cfg);
406 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
407 size = xbt_dynar_length(variable->content);
408 if (variable->min > size) {
409 xbt_dict_cursor_free(&cursor);
410 THROW4(mismatch_error, 0,
411 "Config elem %s needs at least %d %s, but there is only %d values.",
412 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
415 if (variable->max > 0 && variable->max < size) {
416 xbt_dict_cursor_free(&cursor);
417 THROW4(mismatch_error, 0,
418 "Config elem %s accepts at most %d %s, but there is %d values.",
419 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
423 xbt_dict_cursor_free(&cursor);
426 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
427 const char *name, e_xbt_cfgelm_type_t type)
429 xbt_cfgelm_t res = NULL;
431 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
434 THROW1(not_found_error, 0,
435 "No registered variable '%s' in this config set", name);
438 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
439 "You tried to access to the config element %s as an %s, but its type is %s.",
441 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
446 /** @brief Get the type of this variable in that configuration set
448 * \arg cfg the config set
449 * \arg name the name of the element
450 * \arg type the result
454 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
457 xbt_cfgelm_t variable = NULL;
459 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
461 THROW1(not_found_error, 0,
462 "Can't get the type of '%s' since this variable does not exist",
465 INFO1("type in variable = %d", variable->type);
467 return variable->type;
470 /*----[ Setting ]---------------------------------------------------------*/
471 /** @brief va_args version of xbt_cfg_set
473 * \arg cfg config set to fill
474 * \arg n variable name
475 * \arg pa variable value
477 * Add some values to the config set.
479 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
484 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
489 type = xbt_cfg_get_type(cfg, name);
491 if (e.category == not_found_error) {
493 THROW1(not_found_error, 0,
494 "Can't set the property '%s' since it's not registered", name);
500 case xbt_cfgelm_peer:
501 str = va_arg(pa, char *);
503 xbt_cfg_set_peer(cfg, name, str, i);
506 case xbt_cfgelm_string:
507 str = va_arg(pa, char *);
508 xbt_cfg_set_string(cfg, name, str);
513 xbt_cfg_set_int(cfg, name, i);
516 case xbt_cfgelm_double:
517 d = va_arg(pa, double);
518 xbt_cfg_set_double(cfg, name, d);
522 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
527 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
529 * \arg cfg config set to fill
530 * \arg name variable name
531 * \arg varargs variable value
534 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
539 xbt_cfg_set_vargs(cfg, name, pa);
543 /** @brief Add values parsed from a string into a config set
545 * \arg cfg config set to fill
546 * \arg options a string containing the content to add to the config set. This
547 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
548 * like "[name]:[value]" where [name] is the name of an already registred
549 * variable, and [value] conforms to the data type under which this variable was
552 * @todo This is a crude manual parser, it should be a proper lexer.
555 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
563 xbt_cfgelm_t variable = NULL;
564 char *optionlist_cpy;
565 char *option, *name, *val;
570 if (!options || !strlen(options)) { /* nothing to do */
573 optionlist_cpy = xbt_strdup(options);
575 DEBUG1("List to parse and set:'%s'", options);
576 option = optionlist_cpy;
577 while (1) { /* breaks in the code */
583 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
584 name, len, (long) (option - name));
587 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
588 && *option != '\t' && *option != ',') {
589 DEBUG1("Take %c.", *option);
592 if (option - name == len) {
593 DEBUG0("Boundary=EOL");
594 option = NULL; /* don't do next iteration */
597 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
598 *option, len, (long) (option - name));
600 /* Pass the following blank chars */
602 while (option - name < (len - 1) &&
603 (*option == ' ' || *option == '\n' || *option == '\t')) {
604 /* fprintf(stderr,"Ignore a blank char.\n"); */
607 if (option - name == len - 1)
608 option = NULL; /* don't do next iteration */
610 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
612 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
617 val = strchr(name, ':');
619 free(optionlist_cpy);
621 "Option '%s' badly formated. Should be of the form 'name:value'",
626 INFO2("Configuration change: Set '%s' to '%s'", name, val);
629 variable = xbt_dict_get((xbt_dict_t) cfg, name);
632 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
633 name = (char *) (optionlist_cpy - name + options);
634 free(optionlist_cpy);
635 if (e.category == not_found_error) {
637 THROW1(not_found_error, 0,
638 "No registrated variable corresponding to '%s'.", name);
644 switch (variable->type) {
645 case xbt_cfgelm_string:
646 xbt_cfg_set_string(cfg, name, val); /* throws */
650 i = strtol(val, &val, 0);
652 free(optionlist_cpy);
654 "Value of option %s not valid. Should be an integer",
658 xbt_cfg_set_int(cfg, name, i); /* throws */
661 case xbt_cfgelm_double:
662 d = strtod(val, &val);
664 free(optionlist_cpy);
666 "Value of option %s not valid. Should be a double",
670 xbt_cfg_set_double(cfg, name, d); /* throws */
673 case xbt_cfgelm_peer:
675 val = strchr(val, ':');
677 free(optionlist_cpy);
679 "Value of option %s not valid. Should be an peer (machine:port)",
684 i = strtol(val, &val, 0);
686 free(optionlist_cpy);
688 "Value of option %s not valid. Should be an peer (machine:port)",
692 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
696 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
701 free(optionlist_cpy);
705 free(optionlist_cpy);
709 /** @brief Set or add an integer value to \a name within \a cfg
711 * \arg cfg the config set
712 * \arg name the name of the variable
713 * \arg val the value of the variable
715 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
717 xbt_cfgelm_t variable;
719 VERB2("Configuration setting: %s=%d", name, val);
720 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
722 if (variable->max == 1) {
723 if (variable->cb_rm && xbt_dynar_length(variable->content))
724 (*variable->cb_rm) (name, 0);
726 xbt_dynar_set(variable->content, 0, &val);
729 && xbt_dynar_length(variable->content) ==
730 (unsigned long) variable->max)
731 THROW3(mismatch_error, 0,
732 "Cannot add value %d to the config element %s since it's already full (size=%d)",
733 val, name, variable->max);
735 xbt_dynar_push(variable->content, &val);
738 if (variable->cb_set)
739 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
742 /** @brief Set or add a double value to \a name within \a cfg
744 * \arg cfg the config set
745 * \arg name the name of the variable
746 * \arg val the doule to set
749 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
751 xbt_cfgelm_t variable;
753 VERB2("Configuration setting: %s=%f", name, val);
754 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
756 if (variable->max == 1) {
757 if (variable->cb_rm && xbt_dynar_length(variable->content))
758 (*variable->cb_rm) (name, 0);
760 xbt_dynar_set(variable->content, 0, &val);
762 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
763 THROW3(mismatch_error, 0,
764 "Cannot add value %f to the config element %s since it's already full (size=%d)",
765 val, name, variable->max);
767 xbt_dynar_push(variable->content, &val);
770 if (variable->cb_set)
771 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
774 /** @brief Set or add a string 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 to be added
782 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
784 xbt_cfgelm_t variable;
785 char *newval = xbt_strdup(val);
787 VERB2("Configuration setting: %s=%s", name, val);
788 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
789 DEBUG5("Variable: %d to %d %s (=%d) @%p",
790 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
791 variable->type, variable);
793 if (variable->max == 1) {
794 if (xbt_dynar_length(variable->content)) {
796 (*variable->cb_rm) (name, 0);
797 else if (variable->type == xbt_cfgelm_string) {
798 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
803 xbt_dynar_set(variable->content, 0, &newval);
805 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
806 THROW3(mismatch_error, 0,
807 "Cannot add value %s to the config element %s since it's already full (size=%d)",
808 name, val, variable->max);
810 xbt_dynar_push(variable->content, &newval);
813 if (variable->cb_set)
814 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
817 /** @brief Set or add an peer value to \a name within \a cfg
819 * \arg cfg the config set
820 * \arg name the name of the variable
822 * \arg port the port number
824 * \e peer values are composed of a string (peername) and an integer (port)
828 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
830 xbt_cfgelm_t variable;
831 xbt_peer_t val = xbt_peer_new(peer, port);
833 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
835 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
837 if (variable->max == 1) {
838 if (variable->cb_rm && xbt_dynar_length(variable->content))
839 (*variable->cb_rm) (name, 0);
841 xbt_dynar_set(variable->content, 0, &val);
843 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
844 THROW4(mismatch_error, 0,
845 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
846 peer, port, name, variable->max);
848 xbt_dynar_push(variable->content, &val);
851 if (variable->cb_set)
852 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
855 /* ---- [ Removing ] ---- */
857 /** @brief Remove the provided \e val integer value from a variable
859 * \arg cfg the config set
860 * \arg name the name of the variable
861 * \arg val the value to be removed
863 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
866 xbt_cfgelm_t variable;
870 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
872 if (xbt_dynar_length(variable->content) == variable->min)
873 THROW3(mismatch_error, 0,
874 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
875 val, name, variable->min);
877 xbt_dynar_foreach(variable->content, cpt, seen) {
880 (*variable->cb_rm) (name, cpt);
881 xbt_dynar_cursor_rm(variable->content, &cpt);
886 THROW2(not_found_error, 0,
887 "Can't remove the value %d of config element %s: value not found.",
891 /** @brief Remove the provided \e val double value from a variable
893 * \arg cfg the config set
894 * \arg name the name of the variable
895 * \arg val the value to be removed
898 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
900 xbt_cfgelm_t variable;
904 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
906 if (xbt_dynar_length(variable->content) == variable->min)
907 THROW3(mismatch_error, 0,
908 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
909 val, name, variable->min);
911 xbt_dynar_foreach(variable->content, cpt, seen) {
913 xbt_dynar_cursor_rm(variable->content, &cpt);
915 (*variable->cb_rm) (name, cpt);
920 THROW2(not_found_error, 0,
921 "Can't remove the value %f of config element %s: value not found.",
925 /** @brief Remove the provided \e val string value from a variable
927 * \arg cfg the config set
928 * \arg name the name of the variable
929 * \arg val the value of the string which will be removed
931 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
933 xbt_cfgelm_t variable;
937 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
939 if (xbt_dynar_length(variable->content) == variable->min)
940 THROW3(mismatch_error, 0,
941 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
942 name, val, variable->min);
944 xbt_dynar_foreach(variable->content, cpt, seen) {
945 if (!strcpy(seen, val)) {
947 (*variable->cb_rm) (name, cpt);
948 xbt_dynar_cursor_rm(variable->content, &cpt);
953 THROW2(not_found_error, 0,
954 "Can't remove the value %s of config element %s: value not found.",
958 /** @brief Remove the provided \e val peer value from a variable
960 * \arg cfg the config set
961 * \arg name the name of the variable
962 * \arg peer the peername
963 * \arg port the port number
967 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
969 xbt_cfgelm_t variable;
973 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
975 if (xbt_dynar_length(variable->content) == variable->min)
976 THROW4(mismatch_error, 0,
977 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
978 peer, port, name, variable->min);
980 xbt_dynar_foreach(variable->content, cpt, seen) {
981 if (!strcpy(seen->name, peer) && seen->port == port) {
983 (*variable->cb_rm) (name, cpt);
984 xbt_dynar_cursor_rm(variable->content, &cpt);
989 THROW3(not_found_error, 0,
990 "Can't remove the value %s:%d of config element %s: value not found.",
994 /** @brief Remove the \e pos th value from the provided variable */
996 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
999 xbt_cfgelm_t variable;
1001 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1003 if (xbt_dynar_length(variable->content) == variable->min)
1004 THROW3(mismatch_error, 0,
1005 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1006 pos, name, variable->min);
1008 if (variable->cb_rm)
1009 (*variable->cb_rm) (name, pos);
1010 xbt_dynar_remove_at(variable->content, pos, NULL);
1013 /** @brief Remove all the values from a variable
1015 * \arg cfg the config set
1016 * \arg name the name of the variable
1019 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1021 xbt_cfgelm_t variable = NULL;
1025 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1027 if (e.category != not_found_error)
1031 THROW1(not_found_error, 0,
1032 "Can't empty '%s' since this config element does not exist",
1037 if (variable->cb_rm) {
1040 xbt_dynar_foreach(variable->content, cpt, ignored) {
1041 (*variable->cb_rm) (name, cpt);
1044 xbt_dynar_reset(variable->content);
1048 /*----[ Getting ]---------------------------------------------------------*/
1050 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1052 * \arg cfg the config set
1053 * \arg name the name of the variable
1054 * \arg val the wanted value
1056 * Returns the first value from the config set under the given name.
1057 * If there is more than one value, it will issue a warning. Consider using
1058 * xbt_cfg_get_dynar() instead.
1060 * \warning the returned value is the actual content of the config set
1062 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1064 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1066 if (xbt_dynar_length(variable->content) > 1) {
1068 ("You asked for the first value of the config element '%s', but there is %lu values",
1069 name, xbt_dynar_length(variable->content));
1072 return xbt_dynar_get_as(variable->content, 0, int);
1075 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1077 * \arg cfg the config set
1078 * \arg name the name of the variable
1079 * \arg val the wanted value
1081 * Returns the first value from the config set under the given name.
1082 * If there is more than one value, it will issue a warning. Consider using
1083 * xbt_cfg_get_dynar() instead.
1085 * \warning the returned value is the actual content of the config set
1088 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1090 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1092 if (xbt_dynar_length(variable->content) > 1) {
1094 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1095 name, xbt_dynar_length(variable->content));
1098 return xbt_dynar_get_as(variable->content, 0, double);
1101 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1103 * \arg th the config set
1104 * \arg name the name of the variable
1105 * \arg val the wanted value
1107 * Returns the first value from the config set under the given name.
1108 * If there is more than one value, it will issue a warning. Consider using
1109 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1111 * \warning the returned value is the actual content of the config set
1114 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1116 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1118 if (xbt_dynar_length(variable->content) > 1) {
1120 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1121 name, xbt_dynar_length(variable->content));
1122 } else if (xbt_dynar_length(variable->content) == 0) {
1126 return xbt_dynar_get_as(variable->content, 0, char *);
1129 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1131 * \arg cfg the config set
1132 * \arg name the name of the variable
1133 * \arg peer the peer
1134 * \arg port the port number
1136 * Returns the first value from the config set under the given name.
1137 * If there is more than one value, it will issue a warning. Consider using
1138 * xbt_cfg_get_dynar() instead.
1140 * \warning the returned value is the actual content of the config set
1143 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1145 xbt_cfgelm_t variable;
1148 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1150 if (xbt_dynar_length(variable->content) > 1) {
1152 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1153 name, xbt_dynar_length(variable->content));
1156 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1161 /** @brief Retrieve the dynar of all the values stored in a variable
1163 * \arg cfg where to search in
1164 * \arg name what to search for
1167 * Get the data stored in the config set.
1169 * \warning the returned value is the actual content of the config set
1171 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1173 xbt_cfgelm_t variable = NULL;
1177 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1179 if (e.category == not_found_error) {
1181 THROW1(not_found_error, 0,
1182 "No registered variable %s in this config set", name);
1187 return variable->content;
1191 /** @brief Retrieve one of the integer value of a variable */
1192 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1195 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1196 return xbt_dynar_get_as(variable->content, pos, int);
1199 /** @brief Retrieve one of the double value of a variable */
1200 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1203 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1204 return xbt_dynar_get_as(variable->content, pos, double);
1208 /** @brief Retrieve one of the string value of a variable */
1209 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1212 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1213 return xbt_dynar_get_as(variable->content, pos, char *);
1216 /** @brief Retrieve one of the peer value of a variable */
1218 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1219 char **peer, int *port)
1222 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1223 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1234 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1236 XBT_TEST_SUITE("config", "Configuration support");
1238 static xbt_cfg_t make_set()
1240 xbt_cfg_t set = NULL;
1242 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1243 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1244 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1245 xbt_cfg_register_str(&set, "user:1_to_10_string");
1248 } /* end_of_make_set */
1250 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1252 xbt_cfg_t set = make_set();
1253 xbt_test_add0("Alloc and free a config set");
1254 xbt_cfg_set_parse(set,
1255 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1260 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1262 xbt_cfg_t set = set = make_set();
1265 xbt_test_add0("Having too few elements for speed");
1266 xbt_cfg_set_parse(set,
1267 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1272 if (e.category != mismatch_error ||
1273 strncmp(e.msg, "Config elem speed needs",
1274 strlen("Config elem speed needs")))
1275 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1283 xbt_test_add0("Having too much values of 'speed'");
1285 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1287 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1290 if (e.category != mismatch_error ||
1291 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1292 strlen("Config elem speed needs")))
1293 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1302 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1305 xbt_test_add0("Get a single value");
1307 /* get_single_value */
1309 xbt_cfg_t myset = make_set();
1311 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1312 ival = xbt_cfg_get_int(myset, "speed");
1314 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1315 xbt_cfg_free(&myset);
1318 xbt_test_add0("Get multiple values");
1320 /* get_multiple_value */
1322 xbt_cfg_t myset = make_set();
1324 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1325 xbt_cfg_set_parse(myset, "speed:42");
1326 xbt_cfg_check(myset);
1327 dyn = xbt_cfg_get_dynar(myset, "user");
1329 if (xbt_dynar_length(dyn) != 3)
1330 xbt_test_fail1("Dynar length = %lu, I expected 3",
1331 xbt_dynar_length(dyn));
1333 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1334 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1335 xbt_dynar_get_as(dyn, 0, char *));
1337 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1338 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1339 xbt_dynar_get_as(dyn, 1, char *));
1341 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1342 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1343 xbt_dynar_get_as(dyn, 2, char *));
1344 xbt_cfg_free(&myset);
1347 xbt_test_add0("Access to a non-existant entry");
1349 /* non-existant_entry */
1350 xbt_cfg_t myset = make_set();
1354 xbt_cfg_set_parse(myset, "color:blue");
1356 if (e.category != not_found_error)
1357 xbt_test_exception(e);
1360 xbt_cfg_free(&myset);
1363 #endif /* SIMGRID_TEST */