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) 2001,2002,2003,2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/sysdep.h"
15 #include "xbt/dynar.h"
19 #include "xbt/config.h" /* prototypes of this module */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
23 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
25 Both data and DTD are mixed, but fixing it now would prevent me to ever
32 /* Allowed type of the variable */
33 e_xbt_cfgelm_type_t type;
42 (cannot be an union because type peer uses both str and i) */
44 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
46 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
47 { "int", "double", "string", "peer", "any" };
49 /* Internal stuff used in cache to free a variable */
50 static void xbt_cfgelm_free(void *data);
52 /* Retrieve the variable we'll modify */
53 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
54 e_xbt_cfgelm_type_t type);
56 /*----[ Memory management ]-----------------------------------------------*/
58 /** @brief Constructor
60 * Initialise an config set
64 xbt_cfg_t xbt_cfg_new(void)
66 return (xbt_cfg_t) xbt_dict_new();
69 /** \brief Copy an existing configuration set
71 * \arg whereto the config set to be created
72 * \arg tocopy the source data
74 * This only copy the registrations, not the actual content
77 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
79 xbt_dict_cursor_t cursor = NULL;
80 xbt_cfgelm_t variable = NULL;
83 DEBUG1("Copy cfg set %p", tocopy);
85 xbt_assert0(tocopy, "cannot copy NULL config");
87 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
88 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
89 variable->min, variable->max, variable->cb_set,
94 /** @brief Destructor */
95 void xbt_cfg_free(xbt_cfg_t * cfg)
97 DEBUG1("Frees cfg set %p", cfg);
98 xbt_dict_free((xbt_dict_t *) cfg);
101 /** @brief Dump a config set for debuging purpose
103 * \arg name The name to give to this config set
104 * \arg indent what to write at the begining of each line (right number of spaces)
105 * \arg cfg the config set
107 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
109 xbt_dict_t dict = (xbt_dict_t) cfg;
110 xbt_dict_cursor_t cursor = NULL;
111 xbt_cfgelm_t variable = NULL;
121 printf("%s>> Dumping of the config set '%s':\n", indent, name);
122 xbt_dict_foreach(dict, cursor, key, variable) {
124 printf("%s %s:", indent, key);
126 size = xbt_dynar_length(variable->content);
128 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
129 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
130 size, variable->cb_rm, variable->cb_set);
132 switch (variable->type) {
135 for (i = 0; i < size; i++) {
136 ival = xbt_dynar_get_as(variable->content, i, int);
137 printf("%s %d\n", indent, ival);
141 case xbt_cfgelm_double:
142 for (i = 0; i < size; i++) {
143 dval = xbt_dynar_get_as(variable->content, i, double);
144 printf("%s %f\n", indent, dval);
148 case xbt_cfgelm_string:
149 for (i = 0; i < size; i++) {
150 sval = xbt_dynar_get_as(variable->content, i, char *);
151 printf("%s %s\n", indent, sval);
155 case xbt_cfgelm_peer:
156 for (i = 0; i < size; i++) {
157 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
158 printf("%s %s:%d\n", indent, hval->name, hval->port);
163 printf("%s Invalid type!!\n", indent);
169 printf("%s<< End of the config set '%s'\n", indent, name);
172 xbt_dict_cursor_free(&cursor);
177 * free an config element
180 void xbt_cfgelm_free(void *data)
182 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
184 DEBUG1("Frees cfgelm %p", c);
188 xbt_dynar_free(&(c->content));
192 /*----[ Registering stuff ]-----------------------------------------------*/
194 /** @brief Register an element within a config set
196 * @arg cfg the config set
197 * @arg type the type of the config element
198 * @arg min the minimum
199 * @arg max the maximum
203 xbt_cfg_register(xbt_cfg_t * cfg,
204 const char *name, const char *desc, e_xbt_cfgelm_type_t type,
205 void *default_value, int min, int max, xbt_cfg_cb_t cb_set,
211 *cfg = xbt_cfg_new();
212 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
213 "type of %s not valid (%d should be between %d and %d)",
214 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
215 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
218 WARN1("Config elem %s registered twice.", name);
219 /* Will be removed by the insertion of the new one */
222 res = xbt_new(s_xbt_cfgelm_t, 1);
223 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
224 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, *cfg);
226 res->desc = xbt_strdup(desc);
230 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 if (variable->min == 0 && variable->max == 0)
350 printf("Arity: no bound; ");
352 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
354 printf("Current value: ");
355 size = xbt_dynar_length(variable->content);
357 switch (variable->type) {
364 for (i = 0; i < size; i++) {
365 ival = xbt_dynar_get_as(variable->content, i, int);
366 printf("%s%d\n", (i == 0 ? "" : " "), ival);
370 case xbt_cfgelm_double:
371 for (i = 0; i < size; i++) {
372 dval = xbt_dynar_get_as(variable->content, i, double);
373 printf("%s%f\n", (i == 0 ? "" : " "), dval);
377 case xbt_cfgelm_string:
378 for (i = 0; i < size; i++) {
379 sval = xbt_dynar_get_as(variable->content, i, char *);
380 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
384 case xbt_cfgelm_peer:
385 for (i = 0; i < size; i++) {
386 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
387 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
393 printf("Invalid type!!\n");
399 /** @brief Check that each variable have the right amount of values */
400 void xbt_cfg_check(xbt_cfg_t cfg)
402 xbt_dict_cursor_t cursor;
403 xbt_cfgelm_t variable;
407 xbt_assert0(cfg, "NULL config set.");
408 DEBUG1("Check cfg set %p", cfg);
410 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
411 size = xbt_dynar_length(variable->content);
412 if (variable->min > size) {
413 xbt_dict_cursor_free(&cursor);
414 THROW4(mismatch_error, 0,
415 "Config elem %s needs at least %d %s, but there is only %d values.",
416 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
419 if (variable->max > 0 && variable->max < size) {
420 xbt_dict_cursor_free(&cursor);
421 THROW4(mismatch_error, 0,
422 "Config elem %s accepts at most %d %s, but there is %d values.",
423 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
427 xbt_dict_cursor_free(&cursor);
430 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
431 const char *name, e_xbt_cfgelm_type_t type)
433 xbt_cfgelm_t res = NULL;
435 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
438 THROW1(not_found_error, 0,
439 "No registered variable '%s' in this config set", name);
442 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
443 "You tried to access to the config element %s as an %s, but its type is %s.",
445 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
450 /** @brief Get the type of this variable in that configuration set
452 * \arg cfg the config set
453 * \arg name the name of the element
454 * \arg type the result
458 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
461 xbt_cfgelm_t variable = NULL;
463 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
465 THROW1(not_found_error, 0,
466 "Can't get the type of '%s' since this variable does not exist",
469 INFO1("type in variable = %d", variable->type);
471 return variable->type;
474 /*----[ Setting ]---------------------------------------------------------*/
475 /** @brief va_args version of xbt_cfg_set
477 * \arg cfg config set to fill
478 * \arg n variable name
479 * \arg pa variable value
481 * Add some values to the config set.
483 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
488 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
493 type = xbt_cfg_get_type(cfg, name);
495 if (e.category == not_found_error) {
497 THROW1(not_found_error, 0,
498 "Can't set the property '%s' since it's not registered", name);
504 case xbt_cfgelm_peer:
505 str = va_arg(pa, char *);
507 xbt_cfg_set_peer(cfg, name, str, i);
510 case xbt_cfgelm_string:
511 str = va_arg(pa, char *);
512 xbt_cfg_set_string(cfg, name, str);
517 xbt_cfg_set_int(cfg, name, i);
520 case xbt_cfgelm_double:
521 d = va_arg(pa, double);
522 xbt_cfg_set_double(cfg, name, d);
526 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
531 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
533 * \arg cfg config set to fill
534 * \arg name variable name
535 * \arg varargs variable value
538 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
543 xbt_cfg_set_vargs(cfg, name, pa);
547 /** @brief Add values parsed from a string into a config set
549 * \arg cfg config set to fill
550 * \arg options a string containing the content to add to the config set. This
551 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
552 * like "[name]:[value]" where [name] is the name of an already registred
553 * variable, and [value] conforms to the data type under which this variable was
556 * @todo This is a crude manual parser, it should be a proper lexer.
559 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
567 xbt_cfgelm_t variable = NULL;
568 char *optionlist_cpy;
569 char *option, *name, *val;
574 if (!options || !strlen(options)) { /* nothing to do */
577 optionlist_cpy = xbt_strdup(options);
579 DEBUG1("List to parse and set:'%s'", options);
580 option = optionlist_cpy;
581 while (1) { /* breaks in the code */
587 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
588 name, len, (long) (option - name));
591 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
592 && *option != '\t' && *option != ',') {
593 DEBUG1("Take %c.", *option);
596 if (option - name == len) {
597 DEBUG0("Boundary=EOL");
598 option = NULL; /* don't do next iteration */
601 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
602 *option, len, (long) (option - name));
604 /* Pass the following blank chars */
606 while (option - name < (len - 1) &&
607 (*option == ' ' || *option == '\n' || *option == '\t')) {
608 /* fprintf(stderr,"Ignore a blank char.\n"); */
611 if (option - name == len - 1)
612 option = NULL; /* don't do next iteration */
614 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
616 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
621 val = strchr(name, ':');
623 free(optionlist_cpy);
625 "Option '%s' badly formated. Should be of the form 'name:value'",
630 INFO2("Configuration change: Set '%s' to '%s'", name, val);
633 variable = xbt_dict_get((xbt_dict_t) cfg, name);
636 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
637 name = (char *) (optionlist_cpy - name + options);
638 free(optionlist_cpy);
639 if (e.category == not_found_error) {
641 THROW1(not_found_error, 0,
642 "No registered variable corresponding to '%s'.", name);
648 switch (variable->type) {
649 case xbt_cfgelm_string:
650 xbt_cfg_set_string(cfg, name, val); /* throws */
654 i = strtol(val, &val, 0);
656 free(optionlist_cpy);
658 "Value of option %s not valid. Should be an integer",
662 xbt_cfg_set_int(cfg, name, i); /* throws */
665 case xbt_cfgelm_double:
666 d = strtod(val, &val);
668 free(optionlist_cpy);
670 "Value of option %s not valid. Should be a double",
674 xbt_cfg_set_double(cfg, name, d); /* throws */
677 case xbt_cfgelm_peer:
679 val = strchr(val, ':');
681 free(optionlist_cpy);
683 "Value of option %s not valid. Should be an peer (machine:port)",
688 i = strtol(val, &val, 0);
690 free(optionlist_cpy);
692 "Value of option %s not valid. Should be an peer (machine:port)",
696 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
700 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
705 free(optionlist_cpy);
709 free(optionlist_cpy);
713 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
715 * This is useful to change the default value of a variable while allowing
716 * users to override it with command line arguments
718 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val) {
719 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
721 if (variable->isdefault)
722 xbt_cfg_set_int(cfg, name,val);
724 DEBUG2("Do not override configuration variable '%s' with value '%d' because it was already set.",name,val);
726 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
728 * This is useful to change the default value of a variable while allowing
729 * users to override it with command line arguments
731 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val) {
732 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
734 if (variable->isdefault)
735 xbt_cfg_set_double(cfg, name, val);
737 DEBUG2("Do not override configuration variable '%s' with value '%lf' because it was already set.",name,val);
739 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
741 * This is useful to change the default value of a variable while allowing
742 * users to override it with command line arguments
744 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char* val) {
745 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
747 if (variable->isdefault)
748 xbt_cfg_set_string(cfg, name,val);
750 DEBUG2("Do not override configuration variable '%s' with value '%s' because it was already set.",name,val);
752 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
754 * This is useful to change the default value of a variable while allowing
755 * users to override it with command line arguments
757 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name, const char* host, int port) {
758 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
760 if (variable->isdefault)
761 xbt_cfg_set_peer(cfg, name, host, port);
763 DEBUG3("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
767 /** @brief Set or add an integer value to \a name within \a cfg
769 * \arg cfg the config set
770 * \arg name the name of the variable
771 * \arg val the value of the variable
773 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
775 xbt_cfgelm_t variable;
777 VERB2("Configuration setting: %s=%d", name, val);
778 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
780 if (variable->max == 1) {
781 if (variable->cb_rm && xbt_dynar_length(variable->content))
782 (*variable->cb_rm) (name, 0);
784 xbt_dynar_set(variable->content, 0, &val);
787 && xbt_dynar_length(variable->content) ==
788 (unsigned long) variable->max)
789 THROW3(mismatch_error, 0,
790 "Cannot add value %d to the config element %s since it's already full (size=%d)",
791 val, name, variable->max);
793 xbt_dynar_push(variable->content, &val);
796 if (variable->cb_set)
797 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
798 variable->isdefault = 0;
801 /** @brief Set or add a double value to \a name within \a cfg
803 * \arg cfg the config set
804 * \arg name the name of the variable
805 * \arg val the doule to set
808 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
810 xbt_cfgelm_t variable;
812 VERB2("Configuration setting: %s=%f", name, val);
813 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
815 if (variable->max == 1) {
816 if (variable->cb_rm && xbt_dynar_length(variable->content))
817 (*variable->cb_rm) (name, 0);
819 xbt_dynar_set(variable->content, 0, &val);
821 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
822 THROW3(mismatch_error, 0,
823 "Cannot add value %f to the config element %s since it's already full (size=%d)",
824 val, name, variable->max);
826 xbt_dynar_push(variable->content, &val);
829 if (variable->cb_set)
830 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
831 variable->isdefault = 0;
834 /** @brief Set or add a string value to \a name within \a cfg
836 * \arg cfg the config set
837 * \arg name the name of the variable
838 * \arg val the value to be added
842 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
844 xbt_cfgelm_t variable;
845 char *newval = xbt_strdup(val);
847 VERB2("Configuration setting: %s=%s", name, val);
848 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
849 DEBUG5("Variable: %d to %d %s (=%d) @%p",
850 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
851 variable->type, variable);
853 if (variable->max == 1) {
854 if (xbt_dynar_length(variable->content)) {
856 (*variable->cb_rm) (name, 0);
857 else if (variable->type == xbt_cfgelm_string) {
858 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
863 xbt_dynar_set(variable->content, 0, &newval);
865 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
866 THROW3(mismatch_error, 0,
867 "Cannot add value %s to the config element %s since it's already full (size=%d)",
868 name, val, variable->max);
870 xbt_dynar_push(variable->content, &newval);
873 if (variable->cb_set)
874 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
875 variable->isdefault = 0;
878 /** @brief Set or add an peer value to \a name within \a cfg
880 * \arg cfg the config set
881 * \arg name the name of the variable
883 * \arg port the port number
885 * \e peer values are composed of a string (peername) and an integer (port)
889 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
891 xbt_cfgelm_t variable;
892 xbt_peer_t val = xbt_peer_new(peer, port);
894 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
896 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
898 if (variable->max == 1) {
899 if (variable->cb_rm && xbt_dynar_length(variable->content))
900 (*variable->cb_rm) (name, 0);
902 xbt_dynar_set(variable->content, 0, &val);
904 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
905 THROW4(mismatch_error, 0,
906 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
907 peer, port, name, variable->max);
909 xbt_dynar_push(variable->content, &val);
912 if (variable->cb_set)
913 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
914 variable->isdefault = 0;
917 /* ---- [ Removing ] ---- */
919 /** @brief Remove the provided \e val integer value from a variable
921 * \arg cfg the config set
922 * \arg name the name of the variable
923 * \arg val the value to be removed
925 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
928 xbt_cfgelm_t variable;
932 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
934 if (xbt_dynar_length(variable->content) == variable->min)
935 THROW3(mismatch_error, 0,
936 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
937 val, name, variable->min);
939 xbt_dynar_foreach(variable->content, cpt, seen) {
942 (*variable->cb_rm) (name, cpt);
943 xbt_dynar_cursor_rm(variable->content, &cpt);
948 THROW2(not_found_error, 0,
949 "Can't remove the value %d of config element %s: value not found.",
953 /** @brief Remove the provided \e val double value from a variable
955 * \arg cfg the config set
956 * \arg name the name of the variable
957 * \arg val the value to be removed
960 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
962 xbt_cfgelm_t variable;
966 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
968 if (xbt_dynar_length(variable->content) == variable->min)
969 THROW3(mismatch_error, 0,
970 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
971 val, name, variable->min);
973 xbt_dynar_foreach(variable->content, cpt, seen) {
975 xbt_dynar_cursor_rm(variable->content, &cpt);
977 (*variable->cb_rm) (name, cpt);
982 THROW2(not_found_error, 0,
983 "Can't remove the value %f of config element %s: value not found.",
987 /** @brief Remove the provided \e val string value from a variable
989 * \arg cfg the config set
990 * \arg name the name of the variable
991 * \arg val the value of the string which will be removed
993 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
995 xbt_cfgelm_t variable;
999 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1001 if (xbt_dynar_length(variable->content) == variable->min)
1002 THROW3(mismatch_error, 0,
1003 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1004 name, val, variable->min);
1006 xbt_dynar_foreach(variable->content, cpt, seen) {
1007 if (!strcpy(seen, val)) {
1008 if (variable->cb_rm)
1009 (*variable->cb_rm) (name, cpt);
1010 xbt_dynar_cursor_rm(variable->content, &cpt);
1015 THROW2(not_found_error, 0,
1016 "Can't remove the value %s of config element %s: value not found.",
1020 /** @brief Remove the provided \e val peer value from a variable
1022 * \arg cfg the config set
1023 * \arg name the name of the variable
1024 * \arg peer the peername
1025 * \arg port the port number
1029 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
1031 xbt_cfgelm_t variable;
1035 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1037 if (xbt_dynar_length(variable->content) == variable->min)
1038 THROW4(mismatch_error, 0,
1039 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1040 peer, port, name, variable->min);
1042 xbt_dynar_foreach(variable->content, cpt, seen) {
1043 if (!strcpy(seen->name, peer) && seen->port == port) {
1044 if (variable->cb_rm)
1045 (*variable->cb_rm) (name, cpt);
1046 xbt_dynar_cursor_rm(variable->content, &cpt);
1051 THROW3(not_found_error, 0,
1052 "Can't remove the value %s:%d of config element %s: value not found.",
1056 /** @brief Remove the \e pos th value from the provided variable */
1058 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1061 xbt_cfgelm_t variable;
1063 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1065 if (xbt_dynar_length(variable->content) == variable->min)
1066 THROW3(mismatch_error, 0,
1067 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1068 pos, name, variable->min);
1070 if (variable->cb_rm)
1071 (*variable->cb_rm) (name, pos);
1072 xbt_dynar_remove_at(variable->content, pos, NULL);
1075 /** @brief Remove all the values from a variable
1077 * \arg cfg the config set
1078 * \arg name the name of the variable
1081 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1083 xbt_cfgelm_t variable = NULL;
1087 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1089 if (e.category != not_found_error)
1093 THROW1(not_found_error, 0,
1094 "Can't empty '%s' since this config element does not exist",
1099 if (variable->cb_rm) {
1102 xbt_dynar_foreach(variable->content, cpt, ignored) {
1103 (*variable->cb_rm) (name, cpt);
1106 xbt_dynar_reset(variable->content);
1110 /*----[ Getting ]---------------------------------------------------------*/
1112 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1114 * \arg cfg the config set
1115 * \arg name the name of the variable
1116 * \arg val the wanted value
1118 * Returns the first value from the config set under the given name.
1119 * If there is more than one value, it will issue a warning. Consider using
1120 * xbt_cfg_get_dynar() instead.
1122 * \warning the returned value is the actual content of the config set
1124 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1126 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1128 if (xbt_dynar_length(variable->content) > 1) {
1130 ("You asked for the first value of the config element '%s', but there is %lu values",
1131 name, xbt_dynar_length(variable->content));
1134 return xbt_dynar_get_as(variable->content, 0, int);
1137 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1139 * \arg cfg the config set
1140 * \arg name the name of the variable
1141 * \arg val the wanted value
1143 * Returns the first value from the config set under the given name.
1144 * If there is more than one value, it will issue a warning. Consider using
1145 * xbt_cfg_get_dynar() instead.
1147 * \warning the returned value is the actual content of the config set
1150 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1152 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1154 if (xbt_dynar_length(variable->content) > 1) {
1156 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1157 name, xbt_dynar_length(variable->content));
1160 return xbt_dynar_get_as(variable->content, 0, double);
1163 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1165 * \arg th the config set
1166 * \arg name the name of the variable
1167 * \arg val the wanted value
1169 * Returns the first value from the config set under the given name.
1170 * If there is more than one value, it will issue a warning. Consider using
1171 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1173 * \warning the returned value is the actual content of the config set
1176 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1178 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1180 if (xbt_dynar_length(variable->content) > 1) {
1182 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1183 name, xbt_dynar_length(variable->content));
1184 } else if (xbt_dynar_length(variable->content) == 0) {
1188 return xbt_dynar_get_as(variable->content, 0, char *);
1191 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1193 * \arg cfg the config set
1194 * \arg name the name of the variable
1195 * \arg peer the peer
1196 * \arg port the port number
1198 * Returns the first value from the config set under the given name.
1199 * If there is more than one value, it will issue a warning. Consider using
1200 * xbt_cfg_get_dynar() instead.
1202 * \warning the returned value is the actual content of the config set
1205 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1207 xbt_cfgelm_t variable;
1210 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1212 if (xbt_dynar_length(variable->content) > 1) {
1214 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1215 name, xbt_dynar_length(variable->content));
1218 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1223 /** @brief Retrieve the dynar of all the values stored in a variable
1225 * \arg cfg where to search in
1226 * \arg name what to search for
1229 * Get the data stored in the config set.
1231 * \warning the returned value is the actual content of the config set
1233 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1235 xbt_cfgelm_t variable = NULL;
1239 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1241 if (e.category == not_found_error) {
1243 THROW1(not_found_error, 0,
1244 "No registered variable %s in this config set", name);
1249 return variable->content;
1253 /** @brief Retrieve one of the integer value of a variable */
1254 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1257 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1258 return xbt_dynar_get_as(variable->content, pos, int);
1261 /** @brief Retrieve one of the double value of a variable */
1262 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1265 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1266 return xbt_dynar_get_as(variable->content, pos, double);
1270 /** @brief Retrieve one of the string value of a variable */
1271 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1274 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1275 return xbt_dynar_get_as(variable->content, pos, char *);
1278 /** @brief Retrieve one of the peer value of a variable */
1280 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1281 char **peer, int *port)
1284 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1285 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1296 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1298 XBT_TEST_SUITE("config", "Configuration support");
1300 static xbt_cfg_t make_set()
1302 xbt_cfg_t set = NULL;
1304 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1305 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1306 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1307 xbt_cfg_register_str(&set, "user:1_to_10_string");
1310 } /* end_of_make_set */
1312 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1314 xbt_cfg_t set = make_set();
1315 xbt_test_add0("Alloc and free a config set");
1316 xbt_cfg_set_parse(set,
1317 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1322 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1324 xbt_cfg_t set = set = make_set();
1327 xbt_test_add0("Having too few elements for speed");
1328 xbt_cfg_set_parse(set,
1329 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1334 if (e.category != mismatch_error ||
1335 strncmp(e.msg, "Config elem speed needs",
1336 strlen("Config elem speed needs")))
1337 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1345 xbt_test_add0("Having too much values of 'speed'");
1347 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1349 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1352 if (e.category != mismatch_error ||
1353 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1354 strlen("Config elem speed needs")))
1355 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1364 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1367 xbt_test_add0("Get a single value");
1369 /* get_single_value */
1371 xbt_cfg_t myset = make_set();
1373 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1374 ival = xbt_cfg_get_int(myset, "speed");
1376 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1377 xbt_cfg_free(&myset);
1380 xbt_test_add0("Get multiple values");
1382 /* get_multiple_value */
1384 xbt_cfg_t myset = make_set();
1386 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1387 xbt_cfg_set_parse(myset, "speed:42");
1388 xbt_cfg_check(myset);
1389 dyn = xbt_cfg_get_dynar(myset, "user");
1391 if (xbt_dynar_length(dyn) != 3)
1392 xbt_test_fail1("Dynar length = %lu, I expected 3",
1393 xbt_dynar_length(dyn));
1395 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1396 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1397 xbt_dynar_get_as(dyn, 0, char *));
1399 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1400 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1401 xbt_dynar_get_as(dyn, 1, char *));
1403 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1404 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1405 xbt_dynar_get_as(dyn, 2, char *));
1406 xbt_cfg_free(&myset);
1409 xbt_test_add0("Access to a non-existant entry");
1411 /* non-existant_entry */
1412 xbt_cfg_t myset = make_set();
1416 xbt_cfg_set_parse(myset, "color:blue");
1418 if (e.category != not_found_error)
1419 xbt_test_exception(e);
1422 xbt_cfg_free(&myset);
1425 #endif /* SIMGRID_TEST */