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;
44 (cannot be an union because type peer uses both str and i) */
46 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
48 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
49 { "int", "double", "string", "peer", "any" };
51 /* Internal stuff used in cache to free a variable */
52 static void xbt_cfgelm_free(void *data);
54 /* Retrieve the variable we'll modify */
55 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
56 e_xbt_cfgelm_type_t type);
58 /*----[ Memory management ]-----------------------------------------------*/
60 /** @brief Constructor
62 * Initialise an config set
66 xbt_cfg_t xbt_cfg_new(void)
68 return (xbt_cfg_t) xbt_dict_new();
71 /** \brief Copy an existing configuration set
73 * \arg whereto the config set to be created
74 * \arg tocopy the source data
76 * This only copy the registrations, not the actual content
79 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
81 xbt_dict_cursor_t cursor = NULL;
82 xbt_cfgelm_t variable = NULL;
85 DEBUG1("Copy cfg set %p", tocopy);
87 xbt_assert0(tocopy, "cannot copy NULL config");
89 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
90 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL,
91 variable->min, variable->max, variable->cb_set,
96 /** @brief Destructor */
97 void xbt_cfg_free(xbt_cfg_t * cfg)
99 DEBUG1("Frees cfg set %p", cfg);
100 xbt_dict_free((xbt_dict_t *) cfg);
103 /** @brief Dump a config set for debuging purpose
105 * \arg name The name to give to this config set
106 * \arg indent what to write at the begining of each line (right number of spaces)
107 * \arg cfg the config set
109 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
111 xbt_dict_t dict = (xbt_dict_t) cfg;
112 xbt_dict_cursor_t cursor = NULL;
113 xbt_cfgelm_t variable = NULL;
123 printf("%s>> Dumping of the config set '%s':\n", indent, name);
124 xbt_dict_foreach(dict, cursor, key, variable) {
126 printf("%s %s:", indent, key);
128 size = xbt_dynar_length(variable->content);
130 ("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
131 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
132 size, variable->cb_rm, variable->cb_set);
134 switch (variable->type) {
137 for (i = 0; i < size; i++) {
138 ival = xbt_dynar_get_as(variable->content, i, int);
139 printf("%s %d\n", indent, ival);
143 case xbt_cfgelm_double:
144 for (i = 0; i < size; i++) {
145 dval = xbt_dynar_get_as(variable->content, i, double);
146 printf("%s %f\n", indent, dval);
150 case xbt_cfgelm_string:
151 for (i = 0; i < size; i++) {
152 sval = xbt_dynar_get_as(variable->content, i, char *);
153 printf("%s %s\n", indent, sval);
157 case xbt_cfgelm_peer:
158 for (i = 0; i < size; i++) {
159 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
160 printf("%s %s:%d\n", indent, hval->name, hval->port);
165 printf("%s Invalid type!!\n", indent);
171 printf("%s<< End of the config set '%s'\n", indent, name);
174 xbt_dict_cursor_free(&cursor);
179 * free an config element
182 void xbt_cfgelm_free(void *data)
184 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
186 DEBUG1("Frees cfgelm %p", c);
190 xbt_dynar_free(&(c->content));
194 /*----[ Registering stuff ]-----------------------------------------------*/
196 /** @brief Register an element within a config set
198 * @arg cfg the config set
199 * @arg type the type of the config element
200 * @arg min the minimum
201 * @arg max the maximum
205 xbt_cfg_register(xbt_cfg_t * cfg,
206 const char *name, const char *desc, e_xbt_cfgelm_type_t type,
207 void *default_value, int min, int max, xbt_cfg_cb_t cb_set,
213 *cfg = xbt_cfg_new();
214 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
215 "type of %s not valid (%d should be between %d and %d)",
216 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
217 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
220 WARN1("Config elem %s registered twice.", name);
221 /* Will be removed by the insertion of the new one */
224 res = xbt_new(s_xbt_cfgelm_t, 1);
225 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
226 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, *cfg);
228 res->desc = xbt_strdup(desc);
232 res->cb_set = cb_set;
238 res->content = xbt_dynar_new(sizeof(int), NULL);
240 xbt_dynar_push(res->content, default_value);
243 case xbt_cfgelm_double:
244 res->content = xbt_dynar_new(sizeof(double), NULL);
246 xbt_dynar_push(res->content, default_value);
249 case xbt_cfgelm_string:
250 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
252 xbt_dynar_push(res->content, default_value);
255 case xbt_cfgelm_peer:
256 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
258 xbt_dynar_push(res->content, default_value);
262 ERROR1("%d is an invalide type code", type);
265 xbt_dict_set((xbt_dict_t) * cfg, name, res, &xbt_cfgelm_free);
268 /** @brief Unregister an element from a config set.
270 * @arg cfg the config set
271 * @arg name the name of the elem to be freed
273 * Note that it removes both the description and the actual content.
274 * Throws not_found when no such element exists.
277 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
279 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
280 xbt_dict_remove((xbt_dict_t) cfg, name);
284 * @brief Parse a string and register the stuff described.
286 * @arg cfg the config set
287 * @arg entry a string describing the element to register
289 * The string may consist in several variable descriptions separated by a space.
290 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
291 * with type being one of 'string','int', 'peer' or 'double'.
293 * @fixme: this does not allow to set the description
296 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
298 char *entrycpy = xbt_strdup(entry);
302 e_xbt_cfgelm_type_t type;
303 DEBUG1("Register string '%s'", entry);
305 tok = strchr(entrycpy, ':');
306 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
307 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
310 min = strtol(tok, &tok, 10);
311 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
313 xbt_assert2(strcmp(tok, "_to_"),
314 "Invalid config element descriptor : %s%s",
315 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
316 tok += strlen("_to_");
318 max = strtol(tok, &tok, 10);
319 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
321 xbt_assert2(*(tok++) == '_',
322 "Invalid config element descriptor: %s%s", entry,
323 "; Should be <name>:<min nb>_to_<max nb>_<type>");
326 type < xbt_cfgelm_type_count
327 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
328 xbt_assert2(type < xbt_cfgelm_type_count,
329 "Invalid type in config element descriptor: %s%s", entry,
330 "; Should be one of 'string', 'int', 'peer' or 'double'.");
332 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
334 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
337 /** @brief Displays the declared options and their description */
338 void xbt_cfg_help(xbt_cfg_t cfg)
340 xbt_dict_cursor_t cursor;
341 xbt_cfgelm_t variable;
347 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
348 printf(" %s: %s\n", name, variable->desc);
349 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
350 if (variable->min != 1 || variable->max != 1) {
351 if (variable->min == 0 && variable->max == 0)
352 printf("Arity: no bound; ");
354 printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
356 printf("Current value: ");
357 size = xbt_dynar_length(variable->content);
359 switch (variable->type) {
366 for (i = 0; i < size; i++) {
367 ival = xbt_dynar_get_as(variable->content, i, int);
368 printf("%s%d\n", (i == 0 ? "" : " "), ival);
372 case xbt_cfgelm_double:
373 for (i = 0; i < size; i++) {
374 dval = xbt_dynar_get_as(variable->content, i, double);
375 printf("%s%f\n", (i == 0 ? "" : " "), dval);
379 case xbt_cfgelm_string:
380 for (i = 0; i < size; i++) {
381 sval = xbt_dynar_get_as(variable->content, i, char *);
382 printf("%s'%s'\n", (i == 0 ? "" : " "), sval);
386 case xbt_cfgelm_peer:
387 for (i = 0; i < size; i++) {
388 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
389 printf("%s%s:%d\n", (i == 0 ? "" : " "), hval->name,
395 printf("Invalid type!!\n");
401 /** @brief Check that each variable have the right amount of values */
402 void xbt_cfg_check(xbt_cfg_t cfg)
404 xbt_dict_cursor_t cursor;
405 xbt_cfgelm_t variable;
409 xbt_assert0(cfg, "NULL config set.");
410 DEBUG1("Check cfg set %p", cfg);
412 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
413 size = xbt_dynar_length(variable->content);
414 if (variable->min > size) {
415 xbt_dict_cursor_free(&cursor);
416 THROW4(mismatch_error, 0,
417 "Config elem %s needs at least %d %s, but there is only %d values.",
418 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
421 if (variable->max > 0 && variable->max < size) {
422 xbt_dict_cursor_free(&cursor);
423 THROW4(mismatch_error, 0,
424 "Config elem %s accepts at most %d %s, but there is %d values.",
425 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
429 xbt_dict_cursor_free(&cursor);
432 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
433 const char *name, e_xbt_cfgelm_type_t type)
435 xbt_cfgelm_t res = NULL;
437 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
440 THROW1(not_found_error, 0,
441 "No registered variable '%s' in this config set", name);
444 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
445 "You tried to access to the config element %s as an %s, but its type is %s.",
447 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
452 /** @brief Get the type of this variable in that configuration set
454 * \arg cfg the config set
455 * \arg name the name of the element
456 * \arg type the result
460 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
463 xbt_cfgelm_t variable = NULL;
465 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
467 THROW1(not_found_error, 0,
468 "Can't get the type of '%s' since this variable does not exist",
471 INFO1("type in variable = %d", variable->type);
473 return variable->type;
476 /*----[ Setting ]---------------------------------------------------------*/
477 /** @brief va_args version of xbt_cfg_set
479 * \arg cfg config set to fill
480 * \arg n variable name
481 * \arg pa variable value
483 * Add some values to the config set.
485 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
490 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
495 type = xbt_cfg_get_type(cfg, name);
497 if (e.category == not_found_error) {
499 THROW1(not_found_error, 0,
500 "Can't set the property '%s' since it's not registered", name);
506 case xbt_cfgelm_peer:
507 str = va_arg(pa, char *);
509 xbt_cfg_set_peer(cfg, name, str, i);
512 case xbt_cfgelm_string:
513 str = va_arg(pa, char *);
514 xbt_cfg_set_string(cfg, name, str);
519 xbt_cfg_set_int(cfg, name, i);
522 case xbt_cfgelm_double:
523 d = va_arg(pa, double);
524 xbt_cfg_set_double(cfg, name, d);
528 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
533 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
535 * \arg cfg config set to fill
536 * \arg name variable name
537 * \arg varargs variable value
540 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
545 xbt_cfg_set_vargs(cfg, name, pa);
549 /** @brief Add values parsed from a string into a config set
551 * \arg cfg config set to fill
552 * \arg options a string containing the content to add to the config set. This
553 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
554 * like "[name]:[value]" where [name] is the name of an already registred
555 * variable, and [value] conforms to the data type under which this variable was
558 * @todo This is a crude manual parser, it should be a proper lexer.
561 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
569 xbt_cfgelm_t variable = NULL;
570 char *optionlist_cpy;
571 char *option, *name, *val;
576 if (!options || !strlen(options)) { /* nothing to do */
579 optionlist_cpy = xbt_strdup(options);
581 DEBUG1("List to parse and set:'%s'", options);
582 option = optionlist_cpy;
583 while (1) { /* breaks in the code */
589 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
590 name, len, (long) (option - name));
593 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
594 && *option != '\t' && *option != ',') {
595 DEBUG1("Take %c.", *option);
598 if (option - name == len) {
599 DEBUG0("Boundary=EOL");
600 option = NULL; /* don't do next iteration */
603 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
604 *option, len, (long) (option - name));
606 /* Pass the following blank chars */
608 while (option - name < (len - 1) &&
609 (*option == ' ' || *option == '\n' || *option == '\t')) {
610 /* fprintf(stderr,"Ignore a blank char.\n"); */
613 if (option - name == len - 1)
614 option = NULL; /* don't do next iteration */
616 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
618 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
623 val = strchr(name, ':');
625 free(optionlist_cpy);
627 "Option '%s' badly formated. Should be of the form 'name:value'",
632 INFO2("Configuration change: Set '%s' to '%s'", name, val);
635 variable = xbt_dict_get((xbt_dict_t) cfg, name);
638 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
639 name = (char *) (optionlist_cpy - name + options);
640 free(optionlist_cpy);
641 if (e.category == not_found_error) {
643 THROW1(not_found_error, 0,
644 "No registered variable corresponding to '%s'.", name);
650 switch (variable->type) {
651 case xbt_cfgelm_string:
652 xbt_cfg_set_string(cfg, name, val); /* throws */
656 i = strtol(val, &val, 0);
658 free(optionlist_cpy);
660 "Value of option %s not valid. Should be an integer",
664 xbt_cfg_set_int(cfg, name, i); /* throws */
667 case xbt_cfgelm_double:
668 d = strtod(val, &val);
670 free(optionlist_cpy);
672 "Value of option %s not valid. Should be a double",
676 xbt_cfg_set_double(cfg, name, d); /* throws */
679 case xbt_cfgelm_peer:
681 val = strchr(val, ':');
683 free(optionlist_cpy);
685 "Value of option %s not valid. Should be an peer (machine:port)",
690 i = strtol(val, &val, 0);
692 free(optionlist_cpy);
694 "Value of option %s not valid. Should be an peer (machine:port)",
698 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
702 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
707 free(optionlist_cpy);
711 free(optionlist_cpy);
715 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
717 * This is useful to change the default value of a variable while allowing
718 * users to override it with command line arguments
720 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val) {
721 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
723 if (variable->isdefault)
724 xbt_cfg_set_int(cfg, name,val);
726 DEBUG2("Do not override configuration variable '%s' with value '%d' because it was already set.",name,val);
728 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
730 * This is useful to change the default value of a variable while allowing
731 * users to override it with command line arguments
733 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val) {
734 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
736 if (variable->isdefault)
737 xbt_cfg_set_double(cfg, name, val);
739 DEBUG2("Do not override configuration variable '%s' with value '%lf' because it was already set.",name,val);
741 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
743 * This is useful to change the default value of a variable while allowing
744 * users to override it with command line arguments
746 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char* val) {
747 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
749 if (variable->isdefault)
750 xbt_cfg_set_string(cfg, name,val);
752 DEBUG2("Do not override configuration variable '%s' with value '%s' because it was already set.",name,val);
754 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
756 * This is useful to change the default value of a variable while allowing
757 * users to override it with command line arguments
759 void xbt_cfg_setdefault_peer(xbt_cfg_t cfg, const char *name, const char* host, int port) {
760 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
762 if (variable->isdefault)
763 xbt_cfg_set_peer(cfg, name, host, port);
765 DEBUG3("Do not override configuration variable '%s' with value '%s:%d' because it was already set.",
769 /** @brief Set or add an integer value to \a name within \a cfg
771 * \arg cfg the config set
772 * \arg name the name of the variable
773 * \arg val the value of the variable
775 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
777 xbt_cfgelm_t variable;
779 VERB2("Configuration setting: %s=%d", name, val);
780 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
782 if (variable->max == 1) {
783 if (variable->cb_rm && xbt_dynar_length(variable->content))
784 (*variable->cb_rm) (name, 0);
786 xbt_dynar_set(variable->content, 0, &val);
789 && xbt_dynar_length(variable->content) ==
790 (unsigned long) variable->max)
791 THROW3(mismatch_error, 0,
792 "Cannot add value %d to the config element %s since it's already full (size=%d)",
793 val, name, variable->max);
795 xbt_dynar_push(variable->content, &val);
798 if (variable->cb_set)
799 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
800 variable->isdefault = 0;
803 /** @brief Set or add a double value to \a name within \a cfg
805 * \arg cfg the config set
806 * \arg name the name of the variable
807 * \arg val the doule to set
810 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
812 xbt_cfgelm_t variable;
814 VERB2("Configuration setting: %s=%f", name, val);
815 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
817 if (variable->max == 1) {
818 if (variable->cb_rm && xbt_dynar_length(variable->content))
819 (*variable->cb_rm) (name, 0);
821 xbt_dynar_set(variable->content, 0, &val);
823 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
824 THROW3(mismatch_error, 0,
825 "Cannot add value %f to the config element %s since it's already full (size=%d)",
826 val, name, variable->max);
828 xbt_dynar_push(variable->content, &val);
831 if (variable->cb_set)
832 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
833 variable->isdefault = 0;
836 /** @brief Set or add a string value to \a name within \a cfg
838 * \arg cfg the config set
839 * \arg name the name of the variable
840 * \arg val the value to be added
844 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
846 xbt_cfgelm_t variable;
847 char *newval = xbt_strdup(val);
849 VERB2("Configuration setting: %s=%s", name, val);
850 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
851 DEBUG5("Variable: %d to %d %s (=%d) @%p",
852 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
853 variable->type, variable);
855 if (variable->max == 1) {
856 if (xbt_dynar_length(variable->content)) {
858 (*variable->cb_rm) (name, 0);
859 else if (variable->type == xbt_cfgelm_string) {
860 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
865 xbt_dynar_set(variable->content, 0, &newval);
867 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
868 THROW3(mismatch_error, 0,
869 "Cannot add value %s to the config element %s since it's already full (size=%d)",
870 name, val, variable->max);
872 xbt_dynar_push(variable->content, &newval);
875 if (variable->cb_set)
876 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
877 variable->isdefault = 0;
880 /** @brief Set or add an peer value to \a name within \a cfg
882 * \arg cfg the config set
883 * \arg name the name of the variable
885 * \arg port the port number
887 * \e peer values are composed of a string (peername) and an integer (port)
891 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
893 xbt_cfgelm_t variable;
894 xbt_peer_t val = xbt_peer_new(peer, port);
896 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
898 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
900 if (variable->max == 1) {
901 if (variable->cb_rm && xbt_dynar_length(variable->content))
902 (*variable->cb_rm) (name, 0);
904 xbt_dynar_set(variable->content, 0, &val);
906 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
907 THROW4(mismatch_error, 0,
908 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
909 peer, port, name, variable->max);
911 xbt_dynar_push(variable->content, &val);
914 if (variable->cb_set)
915 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
916 variable->isdefault = 0;
919 /* ---- [ Removing ] ---- */
921 /** @brief Remove the provided \e val integer value from a variable
923 * \arg cfg the config set
924 * \arg name the name of the variable
925 * \arg val the value to be removed
927 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
930 xbt_cfgelm_t variable;
934 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
936 if (xbt_dynar_length(variable->content) == variable->min)
937 THROW3(mismatch_error, 0,
938 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
939 val, name, variable->min);
941 xbt_dynar_foreach(variable->content, cpt, seen) {
944 (*variable->cb_rm) (name, cpt);
945 xbt_dynar_cursor_rm(variable->content, &cpt);
950 THROW2(not_found_error, 0,
951 "Can't remove the value %d of config element %s: value not found.",
955 /** @brief Remove the provided \e val double value from a variable
957 * \arg cfg the config set
958 * \arg name the name of the variable
959 * \arg val the value to be removed
962 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
964 xbt_cfgelm_t variable;
968 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
970 if (xbt_dynar_length(variable->content) == variable->min)
971 THROW3(mismatch_error, 0,
972 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
973 val, name, variable->min);
975 xbt_dynar_foreach(variable->content, cpt, seen) {
977 xbt_dynar_cursor_rm(variable->content, &cpt);
979 (*variable->cb_rm) (name, cpt);
984 THROW2(not_found_error, 0,
985 "Can't remove the value %f of config element %s: value not found.",
989 /** @brief Remove the provided \e val string value from a variable
991 * \arg cfg the config set
992 * \arg name the name of the variable
993 * \arg val the value of the string which will be removed
995 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
997 xbt_cfgelm_t variable;
1001 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1003 if (xbt_dynar_length(variable->content) == variable->min)
1004 THROW3(mismatch_error, 0,
1005 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1006 name, val, variable->min);
1008 xbt_dynar_foreach(variable->content, cpt, seen) {
1009 if (!strcpy(seen, val)) {
1010 if (variable->cb_rm)
1011 (*variable->cb_rm) (name, cpt);
1012 xbt_dynar_cursor_rm(variable->content, &cpt);
1017 THROW2(not_found_error, 0,
1018 "Can't remove the value %s of config element %s: value not found.",
1022 /** @brief Remove the provided \e val peer value from a variable
1024 * \arg cfg the config set
1025 * \arg name the name of the variable
1026 * \arg peer the peername
1027 * \arg port the port number
1031 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
1033 xbt_cfgelm_t variable;
1037 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1039 if (xbt_dynar_length(variable->content) == variable->min)
1040 THROW4(mismatch_error, 0,
1041 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
1042 peer, port, name, variable->min);
1044 xbt_dynar_foreach(variable->content, cpt, seen) {
1045 if (!strcpy(seen->name, peer) && seen->port == port) {
1046 if (variable->cb_rm)
1047 (*variable->cb_rm) (name, cpt);
1048 xbt_dynar_cursor_rm(variable->content, &cpt);
1053 THROW3(not_found_error, 0,
1054 "Can't remove the value %s:%d of config element %s: value not found.",
1058 /** @brief Remove the \e pos th value from the provided variable */
1060 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1063 xbt_cfgelm_t variable;
1065 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1067 if (xbt_dynar_length(variable->content) == variable->min)
1068 THROW3(mismatch_error, 0,
1069 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1070 pos, name, variable->min);
1072 if (variable->cb_rm)
1073 (*variable->cb_rm) (name, pos);
1074 xbt_dynar_remove_at(variable->content, pos, NULL);
1077 /** @brief Remove all the values from a variable
1079 * \arg cfg the config set
1080 * \arg name the name of the variable
1083 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1085 xbt_cfgelm_t variable = NULL;
1089 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1091 if (e.category != not_found_error)
1095 THROW1(not_found_error, 0,
1096 "Can't empty '%s' since this config element does not exist",
1101 if (variable->cb_rm) {
1104 xbt_dynar_foreach(variable->content, cpt, ignored) {
1105 (*variable->cb_rm) (name, cpt);
1108 xbt_dynar_reset(variable->content);
1112 /*----[ Getting ]---------------------------------------------------------*/
1114 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1116 * \arg cfg the config set
1117 * \arg name the name of the variable
1118 * \arg val the wanted value
1120 * Returns the first value from the config set under the given name.
1121 * If there is more than one value, it will issue a warning. Consider using
1122 * xbt_cfg_get_dynar() instead.
1124 * \warning the returned value is the actual content of the config set
1126 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1128 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1130 if (xbt_dynar_length(variable->content) > 1) {
1132 ("You asked for the first value of the config element '%s', but there is %lu values",
1133 name, xbt_dynar_length(variable->content));
1136 return xbt_dynar_get_as(variable->content, 0, int);
1139 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1141 * \arg cfg the config set
1142 * \arg name the name of the variable
1143 * \arg val the wanted value
1145 * Returns the first value from the config set under the given name.
1146 * If there is more than one value, it will issue a warning. Consider using
1147 * xbt_cfg_get_dynar() instead.
1149 * \warning the returned value is the actual content of the config set
1152 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1154 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1156 if (xbt_dynar_length(variable->content) > 1) {
1158 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1159 name, xbt_dynar_length(variable->content));
1162 return xbt_dynar_get_as(variable->content, 0, double);
1165 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1167 * \arg th the config set
1168 * \arg name the name of the variable
1169 * \arg val the wanted value
1171 * Returns the first value from the config set under the given name.
1172 * If there is more than one value, it will issue a warning. Consider using
1173 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1175 * \warning the returned value is the actual content of the config set
1178 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1180 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1182 if (xbt_dynar_length(variable->content) > 1) {
1184 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1185 name, xbt_dynar_length(variable->content));
1186 } else if (xbt_dynar_length(variable->content) == 0) {
1190 return xbt_dynar_get_as(variable->content, 0, char *);
1193 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1195 * \arg cfg the config set
1196 * \arg name the name of the variable
1197 * \arg peer the peer
1198 * \arg port the port number
1200 * Returns the first value from the config set under the given name.
1201 * If there is more than one value, it will issue a warning. Consider using
1202 * xbt_cfg_get_dynar() instead.
1204 * \warning the returned value is the actual content of the config set
1207 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1209 xbt_cfgelm_t variable;
1212 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1214 if (xbt_dynar_length(variable->content) > 1) {
1216 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1217 name, xbt_dynar_length(variable->content));
1220 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1225 /** @brief Retrieve the dynar of all the values stored in a variable
1227 * \arg cfg where to search in
1228 * \arg name what to search for
1231 * Get the data stored in the config set.
1233 * \warning the returned value is the actual content of the config set
1235 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1237 xbt_cfgelm_t variable = NULL;
1241 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1243 if (e.category == not_found_error) {
1245 THROW1(not_found_error, 0,
1246 "No registered variable %s in this config set", name);
1251 return variable->content;
1255 /** @brief Retrieve one of the integer value of a variable */
1256 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1259 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1260 return xbt_dynar_get_as(variable->content, pos, int);
1263 /** @brief Retrieve one of the double value of a variable */
1264 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1267 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1268 return xbt_dynar_get_as(variable->content, pos, double);
1272 /** @brief Retrieve one of the string value of a variable */
1273 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1276 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1277 return xbt_dynar_get_as(variable->content, pos, char *);
1280 /** @brief Retrieve one of the peer value of a variable */
1282 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1283 char **peer, int *port)
1286 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1287 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1298 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1300 XBT_TEST_SUITE("config", "Configuration support");
1302 static xbt_cfg_t make_set()
1304 xbt_cfg_t set = NULL;
1306 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1307 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1308 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1309 xbt_cfg_register_str(&set, "user:1_to_10_string");
1312 } /* end_of_make_set */
1314 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1316 xbt_cfg_t set = make_set();
1317 xbt_test_add0("Alloc and free a config set");
1318 xbt_cfg_set_parse(set,
1319 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1324 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1326 xbt_cfg_t set = set = make_set();
1329 xbt_test_add0("Having too few elements for speed");
1330 xbt_cfg_set_parse(set,
1331 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1336 if (e.category != mismatch_error ||
1337 strncmp(e.msg, "Config elem speed needs",
1338 strlen("Config elem speed needs")))
1339 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1347 xbt_test_add0("Having too much values of 'speed'");
1349 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1351 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1354 if (e.category != mismatch_error ||
1355 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1356 strlen("Config elem speed needs")))
1357 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1366 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1369 xbt_test_add0("Get a single value");
1371 /* get_single_value */
1373 xbt_cfg_t myset = make_set();
1375 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1376 ival = xbt_cfg_get_int(myset, "speed");
1378 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1379 xbt_cfg_free(&myset);
1382 xbt_test_add0("Get multiple values");
1384 /* get_multiple_value */
1386 xbt_cfg_t myset = make_set();
1388 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1389 xbt_cfg_set_parse(myset, "speed:42");
1390 xbt_cfg_check(myset);
1391 dyn = xbt_cfg_get_dynar(myset, "user");
1393 if (xbt_dynar_length(dyn) != 3)
1394 xbt_test_fail1("Dynar length = %lu, I expected 3",
1395 xbt_dynar_length(dyn));
1397 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1398 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1399 xbt_dynar_get_as(dyn, 0, char *));
1401 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1402 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1403 xbt_dynar_get_as(dyn, 1, char *));
1405 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1406 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1407 xbt_dynar_get_as(dyn, 2, char *));
1408 xbt_cfg_free(&myset);
1411 xbt_test_add0("Access to a non-existant entry");
1413 /* non-existant_entry */
1414 xbt_cfg_t myset = make_set();
1418 xbt_cfg_set_parse(myset, "color:blue");
1420 if (e.category != not_found_error)
1421 xbt_test_exception(e);
1424 xbt_cfg_free(&myset);
1427 #endif /* SIMGRID_TEST */