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-2014. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "xbt/sysdep.h"
16 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
33 /* Allowed type of the variable */
34 e_xbt_cfgelm_type_t type;
43 (cannot be an union because type peer uses both str and i) */
45 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
47 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
48 { "int", "double", "string", "boolean", "peer", "any" };
50 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
58 /* Internal stuff used in cache to free a variable */
59 static void xbt_cfgelm_free(void *data);
61 /* Retrieve the variable we'll modify */
62 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
63 e_xbt_cfgelm_type_t type);
65 /*----[ Memory management ]-----------------------------------------------*/
67 /** @brief Constructor
69 * Initialise a config set
73 xbt_cfg_t xbt_cfg_new(void)
75 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
78 /** \brief Copy an existing configuration set
80 * @param whereto the config set to be created
81 * @param tocopy the source data
83 * This only copy the registrations, not the actual content
86 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
88 xbt_dict_cursor_t cursor = NULL;
89 xbt_cfgelm_t variable = NULL;
92 XBT_DEBUG("Copy cfg set %p", tocopy);
94 xbt_assert(tocopy, "cannot copy NULL config");
96 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
97 xbt_cfg_register(whereto, name, variable->desc, variable->type,
98 variable->min, variable->max, variable->cb_set,
103 /** @brief Destructor */
104 void xbt_cfg_free(xbt_cfg_t * cfg)
106 XBT_DEBUG("Frees cfg set %p", cfg);
107 xbt_dict_free((xbt_dict_t *) cfg);
110 /** @brief Dump a config set for debuging purpose
112 * @param name The name to give to this config set
113 * @param indent what to write at the begining of each line (right number of spaces)
114 * @param cfg the config set
116 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
118 xbt_dict_t dict = (xbt_dict_t) cfg;
119 xbt_dict_cursor_t cursor = NULL;
120 xbt_cfgelm_t variable = NULL;
130 printf("%s>> Dumping of the config set '%s':\n", indent, name);
131 xbt_dict_foreach(dict, cursor, key, variable) {
133 printf("%s %s:", indent, key);
135 size = xbt_dynar_length(variable->content);
137 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
138 variable->min, variable->max,
139 xbt_cfgelm_type_name[variable->type], size, variable->cb_rm,
142 switch (variable->type) {
145 for (i = 0; i < size; i++) {
146 ival = xbt_dynar_get_as(variable->content, i, int);
147 printf("%s %d\n", indent, ival);
151 case xbt_cfgelm_double:
152 for (i = 0; i < size; i++) {
153 dval = xbt_dynar_get_as(variable->content, i, double);
154 printf("%s %f\n", indent, dval);
158 case xbt_cfgelm_string:
159 for (i = 0; i < size; i++) {
160 sval = xbt_dynar_get_as(variable->content, i, char *);
161 printf("%s %s\n", indent, sval);
165 case xbt_cfgelm_boolean:
166 for (i = 0; i < size; i++) {
167 ival = xbt_dynar_get_as(variable->content, i, int);
168 printf("%s %d\n", indent, ival);
172 case xbt_cfgelm_peer:
173 for (i = 0; i < size; i++) {
174 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
175 printf("%s %s:%d\n", indent, hval->name, hval->port);
180 printf("%s Invalid type!!\n", indent);
186 printf("%s<< End of the config set '%s'\n", indent, name);
189 xbt_dict_cursor_free(&cursor);
194 * free an config element
197 void xbt_cfgelm_free(void *data)
199 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
201 XBT_DEBUG("Frees cfgelm %p", c);
205 xbt_dynar_free(&(c->content));
209 /*----[ Registering stuff ]-----------------------------------------------*/
211 /** @brief Register an element within a config set
213 * @param cfg the config set
214 * @param name the name of the config element
215 * @param desc a description for this item (used by xbt_cfg_help())
216 * @param type the type of the config element
217 * @param min the minimum number of values for this config element
218 * @param max the maximum number of values for this config element
219 * @param cb_set callback function called when a value is set
220 * @param cb_rm callback function called when a value is removed
224 xbt_cfg_register(xbt_cfg_t * cfg,
225 const char *name, const char *desc,
226 e_xbt_cfgelm_type_t type, int min,
227 int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
232 *cfg = xbt_cfg_new();
233 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
234 "type of %s not valid (%d should be between %d and %d)",
235 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_peer);
236 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
239 XBT_WARN("Config elem %s registered twice.", name);
240 /* Will be removed by the insertion of the new one */
243 res = xbt_new(s_xbt_cfgelm_t, 1);
244 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
245 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
248 res->desc = xbt_strdup(desc);
252 res->cb_set = cb_set;
258 res->content = xbt_dynar_new(sizeof(int), NULL);
261 case xbt_cfgelm_double:
262 res->content = xbt_dynar_new(sizeof(double), NULL);
265 case xbt_cfgelm_string:
266 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
269 case xbt_cfgelm_boolean:
270 res->content = xbt_dynar_new(sizeof(int), NULL);
273 case xbt_cfgelm_peer:
274 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
278 XBT_ERROR("%d is an invalide type code", (int)type);
281 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
284 /** @brief Unregister an element from a config set.
286 * @param cfg the config set
287 * @param name the name of the elem to be freed
289 * Note that it removes both the description and the actual content.
290 * Throws not_found when no such element exists.
293 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
295 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
296 xbt_dict_remove((xbt_dict_t) cfg, name);
300 * @brief Parse a string and register the stuff described.
302 * @param cfg the config set
303 * @param entry a string describing the element to register
305 * The string may consist in several variable descriptions separated by a space.
306 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
307 * with type being one of 'string','int', 'peer' or 'double'.
309 * FIXME: this does not allow to set the description
312 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
314 char *entrycpy = xbt_strdup(entry);
318 e_xbt_cfgelm_type_t type;
319 XBT_DEBUG("Register string '%s'", entry);
321 tok = strchr(entrycpy, ':');
322 xbt_assert(tok, "Invalid config element descriptor: %s%s",
323 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
326 min = strtol(tok, &tok, 10);
327 xbt_assert(tok, "Invalid minimum in config element descriptor %s",
330 xbt_assert(strcmp(tok, "_to_"),
331 "Invalid config element descriptor : %s%s",
332 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
333 tok += strlen("_to_");
335 max = strtol(tok, &tok, 10);
336 xbt_assert(tok, "Invalid maximum in config element descriptor %s",
339 xbt_assert(*tok == '_',
340 "Invalid config element descriptor: %s%s", entry,
341 "; Should be <name>:<min nb>_to_<max nb>_<type>");
344 for (type = (e_xbt_cfgelm_type_t)0;
345 type < xbt_cfgelm_type_count
346 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
347 xbt_assert(type < xbt_cfgelm_type_count,
348 "Invalid type in config element descriptor: %s%s", entry,
349 "; Should be one of 'string', 'int', 'peer' or 'double'.");
351 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
353 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
356 static int strcmp_voidp(const void *pa, const void *pb)
358 return strcmp(*(const char **)pa, *(const char **)pb);
361 /** @brief Displays the declared options and their description */
362 void xbt_cfg_help(xbt_cfg_t cfg)
364 xbt_dict_cursor_t dict_cursor;
365 unsigned int dynar_cursor;
366 xbt_cfgelm_t variable;
368 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
370 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
371 xbt_dynar_push(names, &name);
373 xbt_dynar_sort(names, strcmp_voidp);
375 xbt_dynar_foreach(names, dynar_cursor, name) {
378 variable = xbt_dict_get((xbt_dict_t )cfg, name);
380 printf(" %s: %s\n", name, variable->desc);
381 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
382 if (variable->min != 1 || variable->max != 1) {
383 printf("Arity: min:%d to max:", variable->min);
384 if (variable->max == 0)
385 printf("(no bound); ");
387 printf("%d; ", variable->max);
389 size = xbt_dynar_length(variable->content);
390 printf("Current value%s: ", (size <= 1 ? "" : "s"));
393 printf(size == 0 ? "n/a\n" : "{ ");
394 for (i = 0; i < size; i++) {
395 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
397 switch (variable->type) {
399 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
402 case xbt_cfgelm_double:
403 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
406 case xbt_cfgelm_string:
407 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
410 case xbt_cfgelm_boolean: {
411 int b = xbt_dynar_get_as(variable->content, i, int);
412 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
413 : xbt_cfgelm_boolean_values[0].false_val;
414 if (b == 0 || b == 1)
415 printf("'%s'%s", bs, sep);
417 printf("'%s/%d'%s", bs, b, sep);
421 case xbt_cfgelm_peer: {
422 xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
423 printf("%s:%d%s", hval->name, hval->port, sep);
428 printf("Invalid type!!%s", sep);
433 xbt_dynar_free(&names);
436 /** @brief Check that each variable have the right amount of values */
437 void xbt_cfg_check(xbt_cfg_t cfg)
439 xbt_dict_cursor_t cursor;
440 xbt_cfgelm_t variable;
444 xbt_assert(cfg, "NULL config set.");
445 XBT_DEBUG("Check cfg set %p", cfg);
447 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
448 size = xbt_dynar_length(variable->content);
449 if (variable->min > size) {
450 xbt_dict_cursor_free(&cursor);
451 THROWF(mismatch_error, 0,
452 "Config elem %s needs at least %d %s, but there is only %d values.",
453 name, variable->min, xbt_cfgelm_type_name[variable->type],
457 if (variable->isdefault && size > variable->min) {
458 xbt_dict_cursor_free(&cursor);
459 THROWF(mismatch_error, 0,
460 "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
461 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
464 if (variable->max > 0 && variable->max < size) {
465 xbt_dict_cursor_free(&cursor);
466 THROWF(mismatch_error, 0,
467 "Config elem %s accepts at most %d %s, but there is %d values.",
468 name, variable->max, xbt_cfgelm_type_name[variable->type],
473 xbt_dict_cursor_free(&cursor);
476 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
478 e_xbt_cfgelm_type_t type)
480 xbt_cfgelm_t res = NULL;
482 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
485 THROWF(not_found_error, 0,
486 "No registered variable '%s' in this config set", name);
489 xbt_assert(type == xbt_cfgelm_any || res->type == type,
490 "You tried to access to the config element %s as an %s, but its type is %s.",
492 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
497 /** @brief Get the type of this variable in that configuration set
499 * @param cfg the config set
500 * @param name the name of the element
502 * @return the type of the given element
505 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
508 xbt_cfgelm_t variable = NULL;
510 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
512 THROWF(not_found_error, 0,
513 "Can't get the type of '%s' since this variable does not exist",
516 XBT_DEBUG("type in variable = %d", (int)variable->type);
518 return variable->type;
521 /*----[ Setting ]---------------------------------------------------------*/
522 /** @brief va_args version of xbt_cfg_set
524 * @param cfg config set to fill
525 * @param name variable name
526 * @param pa variable value
528 * Add some values to the config set.
530 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
535 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
540 type = xbt_cfg_get_type(cfg, name);
543 if (e.category == not_found_error) {
545 THROWF(not_found_error, 0,
546 "Can't set the property '%s' since it's not registered",
553 case xbt_cfgelm_peer:
554 str = va_arg(pa, char *);
556 xbt_cfg_set_peer(cfg, name, str, i);
559 case xbt_cfgelm_string:
560 str = va_arg(pa, char *);
561 xbt_cfg_set_string(cfg, name, str);
566 xbt_cfg_set_int(cfg, name, i);
569 case xbt_cfgelm_double:
570 d = va_arg(pa, double);
571 xbt_cfg_set_double(cfg, name, d);
574 case xbt_cfgelm_boolean:
575 str = va_arg(pa, char *);
576 xbt_cfg_set_boolean(cfg, name, str);
580 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
584 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
586 * @param cfg config set to fill
587 * @param name variable name
588 * @param ... variable value
591 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
596 xbt_cfg_set_vargs(cfg, name, pa);
600 /** @brief Add values parsed from a string into a config set
602 * @param cfg config set to fill
603 * @param options a string containing the content to add to the config set. This
604 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
605 * like "[name]:[value]" where [name] is the name of an already registred
606 * variable, and [value] conforms to the data type under which this variable was
609 * @todo This is a crude manual parser, it should be a proper lexer.
612 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
614 char *optionlist_cpy;
615 char *option, *name, *val;
620 if (!options || !strlen(options)) { /* nothing to do */
623 optionlist_cpy = xbt_strdup(options);
625 XBT_DEBUG("List to parse and set:'%s'", options);
626 option = optionlist_cpy;
627 while (1) { /* breaks in the code */
633 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
634 name, len, (long) (option - name));
637 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
638 && *option != '\t' && *option != ',') {
639 XBT_DEBUG("Take %c.", *option);
642 if (option - name == len) {
643 XBT_DEBUG("Boundary=EOL");
644 option = NULL; /* don't do next iteration */
647 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
648 *option, len, (long) (option - name));
650 /* Pass the following blank chars */
652 while (option - name < (len - 1) &&
653 (*option == ' ' || *option == '\n' || *option == '\t')) {
654 /* fprintf(stderr,"Ignore a blank char.\n"); */
657 if (option - name == len - 1)
658 option = NULL; /* don't do next iteration */
660 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
662 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
667 val = strchr(name, ':');
669 /* don't free(optionlist_cpy) here, 'name' points inside it */
670 xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
675 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
676 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
679 xbt_cfg_set_as_string(cfg,name,val);
681 free(optionlist_cpy);
685 free(optionlist_cpy);
688 /** @brief Set the value of a variable, using the string representation of that value
690 * @param cfg config set to modify
691 * @param key name of the variable to modify
692 * @param value string representation of the value to set
694 * @return the first char after the parsed value in val
697 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
701 volatile xbt_cfgelm_t variable = NULL;
708 variable = xbt_dict_get((xbt_dict_t) cfg, key);
711 if (e.category == not_found_error) {
713 THROWF(not_found_error, 0,
714 "No registered variable corresponding to '%s'.", key);
719 switch (variable->type) {
720 case xbt_cfgelm_string:
721 xbt_cfg_set_string(cfg, key, value); /* throws */
725 i = strtol(value, &ret, 0);
727 xbt_die("Value of option %s not valid. Should be an integer", key);
730 xbt_cfg_set_int(cfg, key, i); /* throws */
733 case xbt_cfgelm_double:
734 d = strtod(value, &ret);
736 xbt_die("Value of option %s not valid. Should be a double", key);
739 xbt_cfg_set_double(cfg, key, d); /* throws */
742 case xbt_cfgelm_boolean:
743 xbt_cfg_set_boolean(cfg, key, value); /* throws */
746 case xbt_cfgelm_peer:
747 val = xbt_strdup(value);
749 val = strchr(val, ':');
751 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
755 i = strtol(val, &ret, 0);
757 xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
760 xbt_cfg_set_peer(cfg, key, str, i); /* throws */
765 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
772 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
774 * This is useful to change the default value of a variable while allowing
775 * users to override it with command line arguments
777 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
779 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
781 if (variable->isdefault){
782 xbt_cfg_set_int(cfg, name, val);
783 variable->isdefault = 1;
787 ("Do not override configuration variable '%s' with value '%d' because it was already set.",
791 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
793 * This is useful to change the default value of a variable while allowing
794 * users to override it with command line arguments
796 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
798 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
800 if (variable->isdefault) {
801 xbt_cfg_set_double(cfg, name, val);
802 variable->isdefault = 1;
806 ("Do not override configuration variable '%s' with value '%f' because it was already set.",
810 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
812 * This is useful to change the default value of a variable while allowing
813 * users to override it with command line arguments
815 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
818 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
820 if (variable->isdefault){
821 xbt_cfg_set_string(cfg, name, val);
822 variable->isdefault = 1;
826 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
831 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
833 * This is useful to change the default value of a variable while allowing
834 * users to override it with command line arguments
836 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
838 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
840 if (variable->isdefault){
841 xbt_cfg_set_boolean(cfg, name, val);
842 variable->isdefault = 1;
846 ("Do not override configuration variable '%s' with value '%s' because it was already set.",
850 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
852 * This is useful to change the default value of a variable while allowing
853 * users to override it with command line arguments
855 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name,
856 const char *host, int port)
858 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
860 if (variable->isdefault){
861 xbt_cfg_set_peer(cfg, name, host, port);
862 variable->isdefault = 1;
866 ("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
870 /** @brief Set or add an integer value to \a name within \a cfg
872 * @param cfg the config set
873 * @param name the name of the variable
874 * @param val the value of the variable
876 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
878 xbt_cfgelm_t variable;
880 XBT_VERB("Configuration setting: %s=%d", name, val);
881 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
883 if (variable->max == 1) {
884 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
885 variable->cb_rm(name, 0);
887 xbt_dynar_set(variable->content, 0, &val);
890 && xbt_dynar_length(variable->content) ==
891 (unsigned long) variable->max)
892 THROWF(mismatch_error, 0,
893 "Cannot add value %d to the config element %s since it's already full (size=%d)",
894 val, name, variable->max);
896 xbt_dynar_push(variable->content, &val);
899 if (variable->cb_set)
900 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
901 variable->isdefault = 0;
904 /** @brief Set or add a double value to \a name within \a cfg
906 * @param cfg the config set
907 * @param name the name of the variable
908 * @param val the doule to set
911 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
913 xbt_cfgelm_t variable;
915 XBT_VERB("Configuration setting: %s=%f", name, val);
916 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
918 if (variable->max == 1) {
919 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
920 variable->cb_rm(name, 0);
922 xbt_dynar_set(variable->content, 0, &val);
925 && xbt_dynar_length(variable->content) == variable->max)
926 THROWF(mismatch_error, 0,
927 "Cannot add value %f to the config element %s since it's already full (size=%d)",
928 val, name, variable->max);
930 xbt_dynar_push(variable->content, &val);
933 if (variable->cb_set)
934 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
935 variable->isdefault = 0;
938 /** @brief Set or add a string value to \a name within \a cfg
940 * @param cfg the config set
941 * @param name the name of the variable
942 * @param val the value to be added
946 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
948 xbt_cfgelm_t variable;
949 char *newval = xbt_strdup(val);
951 XBT_VERB("Configuration setting: %s=%s", name, val);
952 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
953 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
954 variable->min, variable->max,
955 xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
957 if (variable->max == 1) {
958 if (!xbt_dynar_is_empty(variable->content)) {
960 variable->cb_rm(name, 0);
961 else if (variable->type == xbt_cfgelm_string) {
962 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
967 xbt_dynar_set(variable->content, 0, &newval);
970 && xbt_dynar_length(variable->content) == variable->max)
971 THROWF(mismatch_error, 0,
972 "Cannot add value %s to the config element %s since it's already full (size=%d)",
973 name, val, variable->max);
975 xbt_dynar_push(variable->content, &newval);
978 if (variable->cb_set)
979 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
980 variable->isdefault = 0;
983 /** @brief Set or add a boolean value to \a name within \a cfg
985 * @param cfg the config set
986 * @param name the name of the variable
987 * @param val the value of the variable
989 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
991 xbt_cfgelm_t variable;
994 XBT_VERB("Configuration setting: %s=%s", name, val);
995 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
997 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
998 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
1002 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
1007 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
1008 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
1011 if (variable->max == 1) {
1012 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1013 variable->cb_rm(name, 0);
1015 xbt_dynar_set(variable->content, 0, &bval);
1018 && xbt_dynar_length(variable->content) ==
1019 (unsigned long) variable->max)
1020 THROWF(mismatch_error, 0,
1021 "Cannot add value %s to the config element %s since it's already full (size=%d)",
1022 val, name, variable->max);
1024 xbt_dynar_push(variable->content, &bval);
1027 if (variable->cb_set)
1028 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1029 variable->isdefault = 0;
1032 /** @brief Set or add an peer value to \a name within \a cfg
1034 * @param cfg the config set
1035 * @param name the name of the variable
1036 * @param peer the peer
1037 * @param port the port number
1039 * \e peer values are composed of a string (peername) and an integer (port)
1043 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1046 xbt_cfgelm_t variable;
1047 xbt_peer_t val = xbt_peer_new(peer, port);
1049 XBT_VERB("Configuration setting: %s=%s:%d", name, peer, port);
1051 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1053 if (variable->max == 1) {
1054 if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
1055 variable->cb_rm(name, 0);
1057 xbt_dynar_set(variable->content, 0, &val);
1060 && xbt_dynar_length(variable->content) == variable->max)
1061 THROWF(mismatch_error, 0,
1062 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
1063 peer, port, name, variable->max);
1065 xbt_dynar_push(variable->content, &val);
1068 if (variable->cb_set)
1069 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1070 variable->isdefault = 0;
1073 /* ---- [ Removing ] ---- */
1075 /** @brief Remove the provided \e val integer value from a variable
1077 * @param cfg the config set
1078 * @param name the name of the variable
1079 * @param val the value to be removed
1081 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1084 xbt_cfgelm_t variable;
1088 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1090 if (xbt_dynar_length(variable->content) == variable->min)
1091 THROWF(mismatch_error, 0,
1092 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1093 val, name, variable->min);
1095 xbt_dynar_foreach(variable->content, cpt, seen) {
1097 if (variable->cb_rm)
1098 variable->cb_rm(name, cpt);
1099 xbt_dynar_cursor_rm(variable->content, &cpt);
1104 THROWF(not_found_error, 0,
1105 "Can't remove the value %d of config element %s: value not found.",
1109 /** @brief Remove the provided \e val double value from a variable
1111 * @param cfg the config set
1112 * @param name the name of the variable
1113 * @param val the value to be removed
1116 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1118 xbt_cfgelm_t variable;
1122 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1124 if (xbt_dynar_length(variable->content) == variable->min)
1125 THROWF(mismatch_error, 0,
1126 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1127 val, name, variable->min);
1129 xbt_dynar_foreach(variable->content, cpt, seen) {
1131 xbt_dynar_cursor_rm(variable->content, &cpt);
1132 if (variable->cb_rm)
1133 variable->cb_rm(name, cpt);
1138 THROWF(not_found_error, 0,
1139 "Can't remove the value %f of config element %s: value not found.",
1143 /** @brief Remove the provided \e val string value from a variable
1145 * @param cfg the config set
1146 * @param name the name of the variable
1147 * @param val the value of the string which will be removed
1149 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1151 xbt_cfgelm_t variable;
1155 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1157 if (xbt_dynar_length(variable->content) == variable->min)
1158 THROWF(mismatch_error, 0,
1159 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1160 name, val, variable->min);
1162 xbt_dynar_foreach(variable->content, cpt, seen) {
1163 if (!strcpy(seen, val)) {
1164 if (variable->cb_rm)
1165 variable->cb_rm(name, cpt);
1166 xbt_dynar_cursor_rm(variable->content, &cpt);
1171 THROWF(not_found_error, 0,
1172 "Can't remove the value %s of config element %s: value not found.",
1176 /** @brief Remove the provided \e val boolean value from a variable
1178 * @param cfg the config set
1179 * @param name the name of the variable
1180 * @param val the value to be removed
1182 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1185 xbt_cfgelm_t variable;
1189 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1191 if (xbt_dynar_length(variable->content) == variable->min)
1192 THROWF(mismatch_error, 0,
1193 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1194 val, name, variable->min);
1196 xbt_dynar_foreach(variable->content, cpt, seen) {
1198 if (variable->cb_rm)
1199 variable->cb_rm(name, cpt);
1200 xbt_dynar_cursor_rm(variable->content, &cpt);
1205 THROWF(not_found_error, 0,
1206 "Can't remove the value %d of config element %s: value not found.",
1210 /** @brief Remove the provided \e val peer value from a variable
1212 * @param cfg the config set
1213 * @param name the name of the variable
1214 * @param peer the peername
1215 * @param port the port number
1219 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer,
1222 xbt_cfgelm_t variable;
1226 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1228 if (xbt_dynar_length(variable->content) == variable->min)
1229 THROWF(mismatch_error, 0,
1230 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1231 peer, port, name, variable->min);
1233 xbt_dynar_foreach(variable->content, cpt, seen) {
1234 if (!strcpy(seen->name, peer) && seen->port == port) {
1235 if (variable->cb_rm)
1236 variable->cb_rm(name, cpt);
1237 xbt_dynar_cursor_rm(variable->content, &cpt);
1242 THROWF(not_found_error, 0,
1243 "Can't remove the value %s:%d of config element %s: value not found.",
1247 /** @brief Remove the \e pos th value from the provided variable */
1249 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1252 xbt_cfgelm_t variable;
1254 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1256 if (xbt_dynar_length(variable->content) == variable->min)
1257 THROWF(mismatch_error, 0,
1258 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1259 pos, name, variable->min);
1261 if (variable->cb_rm)
1262 variable->cb_rm(name, pos);
1263 xbt_dynar_remove_at(variable->content, pos, NULL);
1266 /** @brief Remove all the values from a variable
1268 * @param cfg the config set
1269 * @param name the name of the variable
1272 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1274 xbt_cfgelm_t variable = NULL;
1278 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1281 if (e.category != not_found_error)
1285 THROWF(not_found_error, 0,
1286 "Can't empty '%s' since this config element does not exist",
1291 if (variable->cb_rm) {
1294 xbt_dynar_foreach(variable->content, cpt, ignored) {
1295 variable->cb_rm(name, cpt);
1298 xbt_dynar_reset(variable->content);
1302 * Say if the value is the default value
1304 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1306 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1307 return variable->isdefault;
1310 /*----[ Getting ]---------------------------------------------------------*/
1312 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1314 * @param cfg the config set
1315 * @param name the name of the variable
1317 * Returns the first value from the config set under the given name.
1318 * If there is more than one value, it will issue a warning. Consider using
1319 * xbt_cfg_get_dynar() instead.
1321 * \warning the returned value is the actual content of the config set
1323 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1325 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1327 if (xbt_dynar_length(variable->content) > 1) {
1329 ("You asked for the first value of the config element '%s', but there is %lu values",
1330 name, xbt_dynar_length(variable->content));
1333 return xbt_dynar_get_as(variable->content, 0, int);
1336 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1338 * @param cfg the config set
1339 * @param name the name of the variable
1341 * Returns the first value from the config set under the given name.
1342 * If there is more than one value, it will issue a warning. Consider using
1343 * xbt_cfg_get_dynar() instead.
1345 * \warning the returned value is the actual content of the config set
1348 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1350 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1352 if (xbt_dynar_length(variable->content) > 1) {
1354 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1355 name, xbt_dynar_length(variable->content));
1358 return xbt_dynar_get_as(variable->content, 0, double);
1361 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1363 * @param cfg the config set
1364 * @param name the name of the variable
1366 * Returns the first value from the config set under the given name.
1367 * If there is more than one value, it will issue a warning. Consider using
1368 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1370 * \warning the returned value is the actual content of the config set
1373 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1375 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1377 if (xbt_dynar_length(variable->content) > 1) {
1379 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1380 name, xbt_dynar_length(variable->content));
1381 } else if (xbt_dynar_is_empty(variable->content)) {
1385 return xbt_dynar_get_as(variable->content, 0, char *);
1388 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1390 * @param cfg the config set
1391 * @param name the name of the variable
1393 * Returns the first value from the config set under the given name.
1394 * If there is more than one value, it will issue a warning. Consider using
1395 * xbt_cfg_get_dynar() instead.
1397 * \warning the returned value is the actual content of the config set
1399 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1401 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1403 if (xbt_dynar_length(variable->content) > 1) {
1405 ("You asked for the first value of the config element '%s', but there is %lu values",
1406 name, xbt_dynar_length(variable->content));
1409 return xbt_dynar_get_as(variable->content, 0, int);
1412 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1414 * @param cfg the config set
1415 * @param name the name of the variable
1416 * @param peer the peer
1417 * @param port the port number
1419 * Returns the first value from the config set under the given name.
1420 * If there is more than one value, it will issue a warning. Consider using
1421 * xbt_cfg_get_dynar() instead.
1423 * \warning the returned value is the actual content of the config set
1426 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer,
1429 xbt_cfgelm_t variable;
1432 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1434 if (xbt_dynar_length(variable->content) > 1) {
1436 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1437 name, xbt_dynar_length(variable->content));
1440 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1445 /** @brief Retrieve the dynar of all the values stored in a variable
1447 * @param cfg where to search in
1448 * @param name what to search for
1450 * Get the data stored in the config set.
1452 * \warning the returned value is the actual content of the config set
1454 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1456 xbt_cfgelm_t variable = NULL;
1460 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1463 if (e.category == not_found_error) {
1465 THROWF(not_found_error, 0,
1466 "No registered variable %s in this config set", name);
1471 return variable->content;
1475 /** @brief Retrieve one of the integer value of a variable */
1476 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1479 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1480 return xbt_dynar_get_as(variable->content, pos, int);
1483 /** @brief Retrieve one of the double value of a variable */
1484 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1487 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1488 return xbt_dynar_get_as(variable->content, pos, double);
1492 /** @brief Retrieve one of the string value of a variable */
1493 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1496 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1497 return xbt_dynar_get_as(variable->content, pos, char *);
1500 /** @brief Retrieve one of the boolean value of a variable */
1501 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1504 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1505 return xbt_dynar_get_as(variable->content, pos, int);
1508 /** @brief Retrieve one of the peer value of a variable */
1510 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1511 char **peer, int *port)
1514 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1515 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1526 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1528 XBT_TEST_SUITE("config", "Configuration support");
1530 static xbt_cfg_t make_set()
1532 xbt_cfg_t set = NULL;
1534 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1535 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1536 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1537 xbt_cfg_register_str(&set, "user:1_to_10_string");
1540 } /* end_of_make_set */
1542 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1544 xbt_cfg_t set = make_set();
1545 xbt_test_add("Alloc and free a config set");
1546 xbt_cfg_set_parse(set,
1547 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1552 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1554 xbt_cfg_t set = set = make_set();
1557 xbt_test_add("Having too few elements for speed");
1558 xbt_cfg_set_parse(set,
1559 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1564 if (e.category != mismatch_error ||
1565 strncmp(e.msg, "Config elem speed needs",
1566 strlen("Config elem speed needs")))
1567 xbt_test_fail("Got an exception. msg=%s", e.msg);
1575 xbt_test_add("Having too much values of 'speed'");
1577 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1579 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1582 if (e.category != mismatch_error ||
1583 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1584 strlen("Config elem speed needs")))
1585 xbt_test_fail("Got an exception. msg=%s", e.msg);
1594 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1597 xbt_test_add("Get a single value");
1599 /* get_single_value */
1601 xbt_cfg_t myset = make_set();
1603 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1604 ival = xbt_cfg_get_int(myset, "speed");
1606 xbt_test_fail("Speed value = %d, I expected 42", ival);
1607 xbt_cfg_free(&myset);
1610 xbt_test_add("Get multiple values");
1612 /* get_multiple_value */
1614 xbt_cfg_t myset = make_set();
1616 xbt_cfg_set_parse(myset,
1617 "peername:veloce user:foo\nuser:bar\tuser:toto");
1618 xbt_cfg_set_parse(myset, "speed:42");
1619 xbt_cfg_check(myset);
1620 dyn = xbt_cfg_get_dynar(myset, "user");
1622 if (xbt_dynar_length(dyn) != 3)
1623 xbt_test_fail("Dynar length = %lu, I expected 3",
1624 xbt_dynar_length(dyn));
1626 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1627 xbt_test_fail("Dynar[0] = %s, I expected foo",
1628 xbt_dynar_get_as(dyn, 0, char *));
1630 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1631 xbt_test_fail("Dynar[1] = %s, I expected bar",
1632 xbt_dynar_get_as(dyn, 1, char *));
1634 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1635 xbt_test_fail("Dynar[2] = %s, I expected toto",
1636 xbt_dynar_get_as(dyn, 2, char *));
1637 xbt_cfg_free(&myset);
1640 xbt_test_add("Access to a non-existant entry");
1642 /* non-existant_entry */
1643 xbt_cfg_t myset = make_set();
1647 xbt_cfg_set_parse(myset, "color:blue");
1650 if (e.category != not_found_error)
1651 xbt_test_exception(e);
1654 xbt_cfg_free(&myset);
1657 #endif /* SIMGRID_TEST */