3 /* config - Dictionnary where the type of each variable is provided. */
5 /* This is useful to build named structs, like option or property sets. */
7 /* Copyright (c) 2001,2002,2003,2004 Martin Quinson. All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "xbt/sysdep.h"
17 #include "xbt/dynar.h"
21 #include "xbt/config.h" /* prototypes of this module */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
25 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27 Both data and DTD are mixed, but fixing it now would prevent me to ever
34 /* Allowed type of the variable */
35 e_xbt_cfgelm_type_t type;
43 (cannot be an union because type peer uses both str and i) */
45 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
47 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
48 { "int", "double", "string", "peer", "any" };
50 /* Internal stuff used in cache to free a variable */
51 static void xbt_cfgelm_free(void *data);
53 /* Retrieve the variable we'll modify */
54 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
55 e_xbt_cfgelm_type_t type);
57 /*----[ Memory management ]-----------------------------------------------*/
59 /** @brief Constructor
61 * Initialise an config set
65 xbt_cfg_t xbt_cfg_new(void)
67 return (xbt_cfg_t) xbt_dict_new();
70 /** \brief Copy an existing configuration set
72 * \arg whereto the config set to be created
73 * \arg tocopy the source data
75 * This only copy the registrations, not the actual content
78 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
80 xbt_dict_cursor_t cursor = NULL;
81 xbt_cfgelm_t variable = NULL;
84 DEBUG1("Copy cfg set %p", tocopy);
86 xbt_assert0(tocopy, "cannot copy NULL config");
88 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
89 xbt_cfg_register(whereto, name, variable->desc, variable->type, NULL, variable->min,
90 variable->max, variable->cb_set, variable->cb_rm);
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);
187 xbt_dynar_free(&(c->content));
191 /*----[ Registering stuff ]-----------------------------------------------*/
193 /** @brief Register an element within a config set
195 * @arg cfg the config set
196 * @arg type the type of the config element
197 * @arg min the minimum
198 * @arg max the maximum
202 xbt_cfg_register(xbt_cfg_t *cfg,
203 const char *name, const char *desc, e_xbt_cfgelm_type_t type, void *default_value,
204 int min, int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
210 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
211 "type of %s not valid (%d should be between %d and %d)",
212 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
213 res = xbt_dict_get_or_null((xbt_dict_t) *cfg, name);
216 WARN1("Config elem %s registered twice.", name);
217 /* Will be removed by the insertion of the new one */
220 res = xbt_new(s_xbt_cfgelm_t, 1);
221 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
222 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, *cfg);
228 res->cb_set = cb_set;
233 res->content = xbt_dynar_new(sizeof(int), NULL);
235 xbt_dynar_push(res->content,default_value);
238 case xbt_cfgelm_double:
239 res->content = xbt_dynar_new(sizeof(double), NULL);
241 xbt_dynar_push(res->content,default_value);
244 case xbt_cfgelm_string:
245 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
247 xbt_dynar_push(res->content,default_value);
250 case xbt_cfgelm_peer:
251 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
253 xbt_dynar_push(res->content,default_value);
257 ERROR1("%d is an invalide type code", type);
260 xbt_dict_set((xbt_dict_t) *cfg, name, res, &xbt_cfgelm_free);
263 /** @brief Unregister an element from a config set.
265 * @arg cfg the config set
266 * @arg name the name of the elem to be freed
268 * Note that it removes both the description and the actual content.
269 * Throws not_found when no such element exists.
272 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
274 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
275 xbt_dict_remove((xbt_dict_t) cfg, name);
279 * @brief Parse a string and register the stuff described.
281 * @arg cfg the config set
282 * @arg entry a string describing the element to register
284 * The string may consist in several variable descriptions separated by a space.
285 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
286 * with type being one of 'string','int', 'peer' or 'double'.
288 * @fixme: this does not allow to set the description
291 void xbt_cfg_register_str(xbt_cfg_t *cfg, const char *entry)
293 char *entrycpy = xbt_strdup(entry);
297 e_xbt_cfgelm_type_t type;
298 DEBUG1("Register string '%s'", entry);
300 tok = strchr(entrycpy, ':');
301 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
302 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
305 min = strtol(tok, &tok, 10);
306 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
308 xbt_assert2(strcmp(tok, "_to_"),
309 "Invalid config element descriptor : %s%s",
310 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
311 tok += strlen("_to_");
313 max = strtol(tok, &tok, 10);
314 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
316 xbt_assert2(*(tok++) == '_',
317 "Invalid config element descriptor: %s%s", entry,
318 "; Should be <name>:<min nb>_to_<max nb>_<type>");
321 type < xbt_cfgelm_type_count
322 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
323 xbt_assert2(type < xbt_cfgelm_type_count,
324 "Invalid type in config element descriptor: %s%s", entry,
325 "; Should be one of 'string', 'int', 'peer' or 'double'.");
327 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
329 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
332 /** @brief Displays the declared options and their description */
333 void xbt_cfg_help(xbt_cfg_t cfg) {
334 xbt_dict_cursor_t cursor;
335 xbt_cfgelm_t variable;
341 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
342 printf(" %s: %s\n",name,variable->desc);
343 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
344 if (variable->min != 1 || variable->max != 1)
345 printf("Arrity: min:%d to max:%d; ",variable->min,variable->max);
346 printf("Current value: ");
347 size = xbt_dynar_length(variable->content);
349 switch (variable->type) {
356 for (i = 0; i < size; i++) {
357 ival = xbt_dynar_get_as(variable->content, i, int);
358 printf("%s%d\n", (i==0?"":" "),ival);
362 case xbt_cfgelm_double:
363 for (i = 0; i < size; i++) {
364 dval = xbt_dynar_get_as(variable->content, i, double);
365 printf("%s%f\n", (i==0?"":" "),dval);
369 case xbt_cfgelm_string:
370 for (i = 0; i < size; i++) {
371 sval = xbt_dynar_get_as(variable->content, i, char *);
372 printf("%s'%s'\n", (i==0?"":" "),sval);
376 case xbt_cfgelm_peer:
377 for (i = 0; i < size; i++) {
378 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
379 printf("%s%s:%d\n", (i==0?"":" "),hval->name, hval->port);
384 printf("Invalid type!!\n");
390 /** @brief Check that each variable have the right amount of values */
391 void xbt_cfg_check(xbt_cfg_t cfg)
393 xbt_dict_cursor_t cursor;
394 xbt_cfgelm_t variable;
398 xbt_assert0(cfg, "NULL config set.");
399 DEBUG1("Check cfg set %p", cfg);
401 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
402 size = xbt_dynar_length(variable->content);
403 if (variable->min > size) {
404 xbt_dict_cursor_free(&cursor);
405 THROW4(mismatch_error, 0,
406 "Config elem %s needs at least %d %s, but there is only %d values.",
407 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
410 if (variable->max > 0 && variable->max < size) {
411 xbt_dict_cursor_free(&cursor);
412 THROW4(mismatch_error, 0,
413 "Config elem %s accepts at most %d %s, but there is %d values.",
414 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
418 xbt_dict_cursor_free(&cursor);
421 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
422 const char *name, e_xbt_cfgelm_type_t type)
424 xbt_cfgelm_t res = NULL;
426 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
429 THROW1(not_found_error, 0,
430 "No registered variable '%s' in this config set", name);
433 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
434 "You tried to access to the config element %s as an %s, but its type is %s.",
436 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
441 /** @brief Get the type of this variable in that configuration set
443 * \arg cfg the config set
444 * \arg name the name of the element
445 * \arg type the result
449 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
452 xbt_cfgelm_t variable = NULL;
454 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
456 THROW1(not_found_error, 0,
457 "Can't get the type of '%s' since this variable does not exist",
460 INFO1("type in variable = %d", variable->type);
462 return variable->type;
465 /*----[ Setting ]---------------------------------------------------------*/
466 /** @brief va_args version of xbt_cfg_set
468 * \arg cfg config set to fill
469 * \arg n variable name
470 * \arg pa variable value
472 * Add some values to the config set.
474 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
479 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
484 type = xbt_cfg_get_type(cfg, name);
486 if (e.category == not_found_error) {
488 THROW1(not_found_error, 0,
489 "Can't set the property '%s' since it's not registered", name);
495 case xbt_cfgelm_peer:
496 str = va_arg(pa, char *);
498 xbt_cfg_set_peer(cfg, name, str, i);
501 case xbt_cfgelm_string:
502 str = va_arg(pa, char *);
503 xbt_cfg_set_string(cfg, name, str);
508 xbt_cfg_set_int(cfg, name, i);
511 case xbt_cfgelm_double:
512 d = va_arg(pa, double);
513 xbt_cfg_set_double(cfg, name, d);
517 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
522 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
524 * \arg cfg config set to fill
525 * \arg name variable name
526 * \arg varargs variable value
529 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
534 xbt_cfg_set_vargs(cfg, name, pa);
538 /** @brief Add values parsed from a string into a config set
540 * \arg cfg config set to fill
541 * \arg options a string containing the content to add to the config set. This
542 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
543 * like "[name]:[value]" where [name] is the name of an already registred
544 * variable, and [value] conforms to the data type under which this variable was
547 * @todo This is a crude manual parser, it should be a proper lexer.
550 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
558 xbt_cfgelm_t variable = NULL;
559 char *optionlist_cpy;
560 char *option, *name, *val;
565 if (!options || !strlen(options)) { /* nothing to do */
568 optionlist_cpy = xbt_strdup(options);
570 DEBUG1("List to parse and set:'%s'", options);
571 option = optionlist_cpy;
572 while (1) { /* breaks in the code */
578 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
579 name, len, (long) (option - name));
582 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
583 && *option != '\t' && *option != ',') {
584 DEBUG1("Take %c.", *option);
587 if (option - name == len) {
588 DEBUG0("Boundary=EOL");
589 option = NULL; /* don't do next iteration */
592 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
593 *option, len, (long) (option - name));
595 /* Pass the following blank chars */
597 while (option - name < (len - 1) &&
598 (*option == ' ' || *option == '\n' || *option == '\t')) {
599 /* fprintf(stderr,"Ignore a blank char.\n"); */
602 if (option - name == len - 1)
603 option = NULL; /* don't do next iteration */
605 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
607 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
612 val = strchr(name, ':');
614 free(optionlist_cpy);
616 "Option '%s' badly formated. Should be of the form 'name:value'",
621 INFO2("Configuration change: Set '%s' to '%s'", name, val);
624 variable = xbt_dict_get((xbt_dict_t) cfg, name);
627 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
628 name = (char *) (optionlist_cpy - name + options);
629 free(optionlist_cpy);
630 if (e.category == not_found_error) {
632 THROW1(not_found_error, 0,
633 "No registrated variable corresponding to '%s'.", name);
639 switch (variable->type) {
640 case xbt_cfgelm_string:
641 xbt_cfg_set_string(cfg, name, val); /* throws */
645 i = strtol(val, &val, 0);
647 free(optionlist_cpy);
649 "Value of option %s not valid. Should be an integer",
653 xbt_cfg_set_int(cfg, name, i); /* throws */
656 case xbt_cfgelm_double:
657 d = strtod(val, &val);
659 free(optionlist_cpy);
661 "Value of option %s not valid. Should be a double",
665 xbt_cfg_set_double(cfg, name, d); /* throws */
668 case xbt_cfgelm_peer:
670 val = strchr(val, ':');
672 free(optionlist_cpy);
674 "Value of option %s not valid. Should be an peer (machine:port)",
679 i = strtol(val, &val, 0);
681 free(optionlist_cpy);
683 "Value of option %s not valid. Should be an peer (machine:port)",
687 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
691 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
696 free(optionlist_cpy);
700 free(optionlist_cpy);
704 /** @brief Set or add an integer value to \a name within \a cfg
706 * \arg cfg the config set
707 * \arg name the name of the variable
708 * \arg val the value of the variable
710 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
712 xbt_cfgelm_t variable;
714 VERB2("Configuration setting: %s=%d", name, val);
715 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
717 if (variable->max == 1) {
718 if (variable->cb_rm && xbt_dynar_length(variable->content))
719 (*variable->cb_rm) (name, 0);
721 xbt_dynar_set(variable->content, 0, &val);
724 && xbt_dynar_length(variable->content) ==
725 (unsigned long) variable->max)
726 THROW3(mismatch_error, 0,
727 "Cannot add value %d to the config element %s since it's already full (size=%d)",
728 val, name, variable->max);
730 xbt_dynar_push(variable->content, &val);
733 if (variable->cb_set)
734 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
737 /** @brief Set or add a double value to \a name within \a cfg
739 * \arg cfg the config set
740 * \arg name the name of the variable
741 * \arg val the doule to set
744 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
746 xbt_cfgelm_t variable;
748 VERB2("Configuration setting: %s=%f", name, val);
749 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
751 if (variable->max == 1) {
752 if (variable->cb_rm && xbt_dynar_length(variable->content))
753 (*variable->cb_rm) (name, 0);
755 xbt_dynar_set(variable->content, 0, &val);
757 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
758 THROW3(mismatch_error, 0,
759 "Cannot add value %f to the config element %s since it's already full (size=%d)",
760 val, name, variable->max);
762 xbt_dynar_push(variable->content, &val);
765 if (variable->cb_set)
766 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
769 /** @brief Set or add a string 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 to be added
777 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
779 xbt_cfgelm_t variable;
780 char *newval = xbt_strdup(val);
782 VERB2("Configuration setting: %s=%s", name, val);
783 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
784 DEBUG5("Variable: %d to %d %s (=%d) @%p",
785 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
786 variable->type, variable);
788 if (variable->max == 1) {
789 if (xbt_dynar_length(variable->content)) {
791 (*variable->cb_rm) (name, 0);
792 else if (variable->type == xbt_cfgelm_string) {
793 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
798 xbt_dynar_set(variable->content, 0, &newval);
800 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
801 THROW3(mismatch_error, 0,
802 "Cannot add value %s to the config element %s since it's already full (size=%d)",
803 name, val, variable->max);
805 xbt_dynar_push(variable->content, &newval);
808 if (variable->cb_set)
809 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
812 /** @brief Set or add an peer value to \a name within \a cfg
814 * \arg cfg the config set
815 * \arg name the name of the variable
817 * \arg port the port number
819 * \e peer values are composed of a string (peername) and an integer (port)
823 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
825 xbt_cfgelm_t variable;
826 xbt_peer_t val = xbt_peer_new(peer, port);
828 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
830 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
832 if (variable->max == 1) {
833 if (variable->cb_rm && xbt_dynar_length(variable->content))
834 (*variable->cb_rm) (name, 0);
836 xbt_dynar_set(variable->content, 0, &val);
838 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
839 THROW4(mismatch_error, 0,
840 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
841 peer, port, name, variable->max);
843 xbt_dynar_push(variable->content, &val);
846 if (variable->cb_set)
847 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
850 /* ---- [ Removing ] ---- */
852 /** @brief Remove the provided \e val integer value from a variable
854 * \arg cfg the config set
855 * \arg name the name of the variable
856 * \arg val the value to be removed
858 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
861 xbt_cfgelm_t variable;
865 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
867 if (xbt_dynar_length(variable->content) == variable->min)
868 THROW3(mismatch_error, 0,
869 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
870 val, name, variable->min);
872 xbt_dynar_foreach(variable->content, cpt, seen) {
875 (*variable->cb_rm) (name, cpt);
876 xbt_dynar_cursor_rm(variable->content, &cpt);
881 THROW2(not_found_error, 0,
882 "Can't remove the value %d of config element %s: value not found.",
886 /** @brief Remove the provided \e val double value from a variable
888 * \arg cfg the config set
889 * \arg name the name of the variable
890 * \arg val the value to be removed
893 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
895 xbt_cfgelm_t variable;
899 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
901 if (xbt_dynar_length(variable->content) == variable->min)
902 THROW3(mismatch_error, 0,
903 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
904 val, name, variable->min);
906 xbt_dynar_foreach(variable->content, cpt, seen) {
908 xbt_dynar_cursor_rm(variable->content, &cpt);
910 (*variable->cb_rm) (name, cpt);
915 THROW2(not_found_error, 0,
916 "Can't remove the value %f of config element %s: value not found.",
920 /** @brief Remove the provided \e val string value from a variable
922 * \arg cfg the config set
923 * \arg name the name of the variable
924 * \arg val the value of the string which will be removed
926 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
928 xbt_cfgelm_t variable;
932 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
934 if (xbt_dynar_length(variable->content) == variable->min)
935 THROW3(mismatch_error, 0,
936 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
937 name, val, variable->min);
939 xbt_dynar_foreach(variable->content, cpt, seen) {
940 if (!strcpy(seen, val)) {
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 %s of config element %s: value not found.",
953 /** @brief Remove the provided \e val peer value from a variable
955 * \arg cfg the config set
956 * \arg name the name of the variable
957 * \arg peer the peername
958 * \arg port the port number
962 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
964 xbt_cfgelm_t variable;
968 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
970 if (xbt_dynar_length(variable->content) == variable->min)
971 THROW4(mismatch_error, 0,
972 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
973 peer, port, name, variable->min);
975 xbt_dynar_foreach(variable->content, cpt, seen) {
976 if (!strcpy(seen->name, peer) && seen->port == port) {
978 (*variable->cb_rm) (name, cpt);
979 xbt_dynar_cursor_rm(variable->content, &cpt);
984 THROW3(not_found_error, 0,
985 "Can't remove the value %s:%d of config element %s: value not found.",
989 /** @brief Remove the \e pos th value from the provided variable */
991 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
994 xbt_cfgelm_t variable;
996 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
998 if (xbt_dynar_length(variable->content) == variable->min)
999 THROW3(mismatch_error, 0,
1000 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1001 pos, name, variable->min);
1003 if (variable->cb_rm)
1004 (*variable->cb_rm) (name, pos);
1005 xbt_dynar_remove_at(variable->content, pos, NULL);
1008 /** @brief Remove all the values from a variable
1010 * \arg cfg the config set
1011 * \arg name the name of the variable
1014 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1016 xbt_cfgelm_t variable = NULL;
1020 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1022 if (e.category != not_found_error)
1026 THROW1(not_found_error, 0,
1027 "Can't empty '%s' since this config element does not exist",
1032 if (variable->cb_rm) {
1035 xbt_dynar_foreach(variable->content, cpt, ignored) {
1036 (*variable->cb_rm) (name, cpt);
1039 xbt_dynar_reset(variable->content);
1043 /*----[ Getting ]---------------------------------------------------------*/
1045 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1047 * \arg cfg the config set
1048 * \arg name the name of the variable
1049 * \arg val the wanted value
1051 * Returns the first value from the config set under the given name.
1052 * If there is more than one value, it will issue a warning. Consider using
1053 * xbt_cfg_get_dynar() instead.
1055 * \warning the returned value is the actual content of the config set
1057 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1059 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1061 if (xbt_dynar_length(variable->content) > 1) {
1063 ("You asked for the first value of the config element '%s', but there is %lu values",
1064 name, xbt_dynar_length(variable->content));
1067 return xbt_dynar_get_as(variable->content, 0, int);
1070 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1072 * \arg cfg the config set
1073 * \arg name the name of the variable
1074 * \arg val the wanted value
1076 * Returns the first value from the config set under the given name.
1077 * If there is more than one value, it will issue a warning. Consider using
1078 * xbt_cfg_get_dynar() instead.
1080 * \warning the returned value is the actual content of the config set
1083 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1085 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1087 if (xbt_dynar_length(variable->content) > 1) {
1089 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1090 name, xbt_dynar_length(variable->content));
1093 return xbt_dynar_get_as(variable->content, 0, double);
1096 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1098 * \arg th the config set
1099 * \arg name the name of the variable
1100 * \arg val the wanted value
1102 * Returns the first value from the config set under the given name.
1103 * If there is more than one value, it will issue a warning. Consider using
1104 * xbt_cfg_get_dynar() instead.
1106 * \warning the returned value is the actual content of the config set
1109 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1111 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1113 if (xbt_dynar_length(variable->content) > 1) {
1115 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1116 name, xbt_dynar_length(variable->content));
1119 return xbt_dynar_get_as(variable->content, 0, char *);
1122 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1124 * \arg cfg the config set
1125 * \arg name the name of the variable
1126 * \arg peer the peer
1127 * \arg port the port number
1129 * Returns the first value from the config set under the given name.
1130 * If there is more than one value, it will issue a warning. Consider using
1131 * xbt_cfg_get_dynar() instead.
1133 * \warning the returned value is the actual content of the config set
1136 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1138 xbt_cfgelm_t variable;
1141 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1143 if (xbt_dynar_length(variable->content) > 1) {
1145 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1146 name, xbt_dynar_length(variable->content));
1149 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1154 /** @brief Retrieve the dynar of all the values stored in a variable
1156 * \arg cfg where to search in
1157 * \arg name what to search for
1160 * Get the data stored in the config set.
1162 * \warning the returned value is the actual content of the config set
1164 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1166 xbt_cfgelm_t variable = NULL;
1170 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1172 if (e.category == not_found_error) {
1174 THROW1(not_found_error, 0,
1175 "No registered variable %s in this config set", name);
1180 return variable->content;
1184 /** @brief Retrieve one of the integer value of a variable */
1185 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1188 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1189 return xbt_dynar_get_as(variable->content, pos, int);
1192 /** @brief Retrieve one of the double value of a variable */
1193 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1196 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1197 return xbt_dynar_get_as(variable->content, pos, double);
1201 /** @brief Retrieve one of the string value of a variable */
1202 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1205 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1206 return xbt_dynar_get_as(variable->content, pos, char *);
1209 /** @brief Retrieve one of the peer value of a variable */
1211 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1212 char **peer, int *port)
1215 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1216 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1227 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1229 XBT_TEST_SUITE("config", "Configuration support");
1231 static xbt_cfg_t make_set()
1233 xbt_cfg_t set = NULL;
1235 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg),xbt_log_priority_critical);
1236 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1237 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1238 xbt_cfg_register_str(&set, "user:1_to_10_string");
1241 } /* end_of_make_set */
1243 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1245 xbt_cfg_t set = make_set();
1246 xbt_test_add0("Alloc and free a config set");
1247 xbt_cfg_set_parse(set,
1248 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1253 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1255 xbt_cfg_t set = set = make_set();
1258 xbt_test_add0("Having too few elements for speed");
1259 xbt_cfg_set_parse(set,
1260 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1265 if (e.category != mismatch_error ||
1266 strncmp(e.msg, "Config elem speed needs",
1267 strlen("Config elem speed needs")))
1268 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1276 xbt_test_add0("Having too much values of 'speed'");
1278 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1280 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1283 if (e.category != mismatch_error ||
1284 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1285 strlen("Config elem speed needs")))
1286 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1295 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1298 xbt_test_add0("Get a single value");
1300 /* get_single_value */
1302 xbt_cfg_t myset = make_set();
1304 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1305 ival = xbt_cfg_get_int(myset, "speed");
1307 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1308 xbt_cfg_free(&myset);
1311 xbt_test_add0("Get multiple values");
1313 /* get_multiple_value */
1315 xbt_cfg_t myset = make_set();
1317 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1318 xbt_cfg_set_parse(myset, "speed:42");
1319 xbt_cfg_check(myset);
1320 dyn = xbt_cfg_get_dynar(myset, "user");
1322 if (xbt_dynar_length(dyn) != 3)
1323 xbt_test_fail1("Dynar length = %lu, I expected 3",
1324 xbt_dynar_length(dyn));
1326 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1327 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1328 xbt_dynar_get_as(dyn, 0, char *));
1330 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1331 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1332 xbt_dynar_get_as(dyn, 1, char *));
1334 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1335 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1336 xbt_dynar_get_as(dyn, 2, char *));
1337 xbt_cfg_free(&myset);
1340 xbt_test_add0("Access to a non-existant entry");
1342 /* non-existant_entry */
1343 xbt_cfg_t myset = make_set();
1347 xbt_cfg_set_parse(myset, "color:blue");
1349 if (e.category != not_found_error)
1350 xbt_test_exception(e);
1353 xbt_cfg_free(&myset);
1356 #endif /* SIMGRID_TEST */