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, 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,
204 int min, int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
209 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
210 "type of %s not valid (%d should be between %d and %d)",
211 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
212 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
215 WARN1("Config elem %s registered twice.", name);
216 /* Will be removed by the insertion of the new one */
219 res = xbt_new(s_xbt_cfgelm_t, 1);
220 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
221 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, cfg);
227 res->cb_set = cb_set;
232 res->content = xbt_dynar_new(sizeof(int), NULL);
235 case xbt_cfgelm_double:
236 res->content = xbt_dynar_new(sizeof(double), NULL);
239 case xbt_cfgelm_string:
240 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
243 case xbt_cfgelm_peer:
244 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
248 ERROR1("%d is an invalide type code", type);
251 xbt_dict_set((xbt_dict_t) cfg, name, res, &xbt_cfgelm_free);
254 /** @brief Unregister an element from a config set.
256 * @arg cfg the config set
257 * @arg name the name of the elem to be freed
259 * Note that it removes both the description and the actual content.
260 * Throws not_found when no such element exists.
263 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
265 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
266 xbt_dict_remove((xbt_dict_t) cfg, name);
270 * @brief Parse a string and register the stuff described.
272 * @arg cfg the config set
273 * @arg entry a string describing the element to register
275 * The string may consist in several variable descriptions separated by a space.
276 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
277 * with type being one of 'string','int', 'peer' or 'double'.
279 * @fixme: this does not allow to set the description
282 void xbt_cfg_register_str(xbt_cfg_t cfg, const char *entry)
284 char *entrycpy = xbt_strdup(entry);
288 e_xbt_cfgelm_type_t type;
289 DEBUG1("Register string '%s'", entry);
291 tok = strchr(entrycpy, ':');
292 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
293 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
296 min = strtol(tok, &tok, 10);
297 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
299 xbt_assert2(strcmp(tok, "_to_"),
300 "Invalid config element descriptor : %s%s",
301 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
302 tok += strlen("_to_");
304 max = strtol(tok, &tok, 10);
305 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
307 xbt_assert2(*(tok++) == '_',
308 "Invalid config element descriptor: %s%s", entry,
309 "; Should be <name>:<min nb>_to_<max nb>_<type>");
312 type < xbt_cfgelm_type_count
313 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
314 xbt_assert2(type < xbt_cfgelm_type_count,
315 "Invalid type in config element descriptor: %s%s", entry,
316 "; Should be one of 'string', 'int', 'peer' or 'double'.");
318 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL, NULL);
320 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
323 /** @brief Check that each variable have the right amount of values */
325 void xbt_cfg_check(xbt_cfg_t cfg)
327 xbt_dict_cursor_t cursor;
328 xbt_cfgelm_t variable;
332 xbt_assert0(cfg, "NULL config set.");
333 DEBUG1("Check cfg set %p", cfg);
335 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
336 size = xbt_dynar_length(variable->content);
337 if (variable->min > size) {
338 xbt_dict_cursor_free(&cursor);
339 THROW4(mismatch_error, 0,
340 "Config elem %s needs at least %d %s, but there is only %d values.",
341 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
344 if (variable->max > 0 && variable->max < size) {
345 xbt_dict_cursor_free(&cursor);
346 THROW4(mismatch_error, 0,
347 "Config elem %s accepts at most %d %s, but there is %d values.",
348 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
352 xbt_dict_cursor_free(&cursor);
355 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
356 const char *name, e_xbt_cfgelm_type_t type)
358 xbt_cfgelm_t res = NULL;
360 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
362 THROW1(not_found_error, 0,
363 "No registered variable '%s' in this config set", name);
365 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
366 "You tried to access to the config element %s as an %s, but its type is %s.",
368 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
373 /** @brief Get the type of this variable in that configuration set
375 * \arg cfg the config set
376 * \arg name the name of the element
377 * \arg type the result
381 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
384 xbt_cfgelm_t variable = NULL;
386 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
388 THROW1(not_found_error, 0,
389 "Can't get the type of '%s' since this variable does not exist",
392 INFO1("type in variable = %d", variable->type);
394 return variable->type;
397 /*----[ Setting ]---------------------------------------------------------*/
398 /** @brief va_args version of xbt_cfg_set
400 * \arg cfg config set to fill
401 * \arg n variable name
402 * \arg pa variable value
404 * Add some values to the config set.
406 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
411 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
416 type = xbt_cfg_get_type(cfg, name);
418 if (e.category == not_found_error) {
420 THROW1(not_found_error, 0,
421 "Can't set the property '%s' since it's not registered", name);
427 case xbt_cfgelm_peer:
428 str = va_arg(pa, char *);
430 xbt_cfg_set_peer(cfg, name, str, i);
433 case xbt_cfgelm_string:
434 str = va_arg(pa, char *);
435 xbt_cfg_set_string(cfg, name, str);
440 xbt_cfg_set_int(cfg, name, i);
443 case xbt_cfgelm_double:
444 d = va_arg(pa, double);
445 xbt_cfg_set_double(cfg, name, d);
449 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
454 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
456 * \arg cfg config set to fill
457 * \arg name variable name
458 * \arg varargs variable value
461 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
466 xbt_cfg_set_vargs(cfg, name, pa);
470 /** @brief Add values parsed from a string into a config set
472 * \arg cfg config set to fill
473 * \arg options a string containing the content to add to the config set. This
474 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
475 * like "[name]:[value]" where [name] is the name of an already registred
476 * variable, and [value] conforms to the data type under which this variable was
479 * @todo This is a crude manual parser, it should be a proper lexer.
482 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
490 xbt_cfgelm_t variable = NULL;
491 char *optionlist_cpy;
492 char *option, *name, *val;
497 if (!options || !strlen(options)) { /* nothing to do */
500 optionlist_cpy = xbt_strdup(options);
502 DEBUG1("List to parse and set:'%s'", options);
503 option = optionlist_cpy;
504 while (1) { /* breaks in the code */
510 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
511 name, len, (long) (option - name));
514 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
515 && *option != '\t' && *option != ',') {
516 DEBUG1("Take %c.", *option);
519 if (option - name == len) {
520 DEBUG0("Boundary=EOL");
521 option = NULL; /* don't do next iteration */
524 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
525 *option, len, (long) (option - name));
527 /* Pass the following blank chars */
529 while (option - name < (len - 1) &&
530 (*option == ' ' || *option == '\n' || *option == '\t')) {
531 /* fprintf(stderr,"Ignore a blank char.\n"); */
534 if (option - name == len - 1)
535 option = NULL; /* don't do next iteration */
537 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
539 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
544 val = strchr(name, ':');
546 free(optionlist_cpy);
548 "Option '%s' badly formated. Should be of the form 'name:value'",
553 INFO2("Configuration change: Set '%s' to '%s'", name, val);
556 variable = xbt_dict_get((xbt_dict_t) cfg, name);
559 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
560 name = (char *) (optionlist_cpy - name + options);
561 free(optionlist_cpy);
562 if (e.category == not_found_error) {
564 THROW1(not_found_error, 0,
565 "No registrated variable corresponding to '%s'.", name);
571 switch (variable->type) {
572 case xbt_cfgelm_string:
573 xbt_cfg_set_string(cfg, name, val); /* throws */
577 i = strtol(val, &val, 0);
579 free(optionlist_cpy);
581 "Value of option %s not valid. Should be an integer",
585 xbt_cfg_set_int(cfg, name, i); /* throws */
588 case xbt_cfgelm_double:
589 d = strtod(val, &val);
591 free(optionlist_cpy);
593 "Value of option %s not valid. Should be a double",
597 xbt_cfg_set_double(cfg, name, d); /* throws */
600 case xbt_cfgelm_peer:
602 val = strchr(val, ':');
604 free(optionlist_cpy);
606 "Value of option %s not valid. Should be an peer (machine:port)",
611 i = strtol(val, &val, 0);
613 free(optionlist_cpy);
615 "Value of option %s not valid. Should be an peer (machine:port)",
619 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
623 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
628 free(optionlist_cpy);
632 free(optionlist_cpy);
636 /** @brief Set or add an integer value to \a name within \a cfg
638 * \arg cfg the config set
639 * \arg name the name of the variable
640 * \arg val the value of the variable
642 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
644 xbt_cfgelm_t variable;
646 VERB2("Configuration setting: %s=%d", name, val);
647 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
649 if (variable->max == 1) {
650 if (variable->cb_rm && xbt_dynar_length(variable->content))
651 (*variable->cb_rm) (name, 0);
653 xbt_dynar_set(variable->content, 0, &val);
656 && xbt_dynar_length(variable->content) ==
657 (unsigned long) variable->max)
658 THROW3(mismatch_error, 0,
659 "Cannot add value %d to the config element %s since it's already full (size=%d)",
660 val, name, variable->max);
662 xbt_dynar_push(variable->content, &val);
665 if (variable->cb_set)
666 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
669 /** @brief Set or add a double value to \a name within \a cfg
671 * \arg cfg the config set
672 * \arg name the name of the variable
673 * \arg val the doule to set
676 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
678 xbt_cfgelm_t variable;
680 VERB2("Configuration setting: %s=%f", name, val);
681 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
683 if (variable->max == 1) {
684 if (variable->cb_rm && xbt_dynar_length(variable->content))
685 (*variable->cb_rm) (name, 0);
687 xbt_dynar_set(variable->content, 0, &val);
689 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
690 THROW3(mismatch_error, 0,
691 "Cannot add value %f to the config element %s since it's already full (size=%d)",
692 val, name, variable->max);
694 xbt_dynar_push(variable->content, &val);
697 if (variable->cb_set)
698 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
701 /** @brief Set or add a string value to \a name within \a cfg
703 * \arg cfg the config set
704 * \arg name the name of the variable
705 * \arg val the value to be added
709 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
711 xbt_cfgelm_t variable;
712 char *newval = xbt_strdup(val);
714 VERB2("Configuration setting: %s=%s", name, val);
715 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
716 DEBUG5("Variable: %d to %d %s (=%d) @%p",
717 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
718 variable->type, variable);
720 if (variable->max == 1) {
721 if (xbt_dynar_length(variable->content)) {
723 (*variable->cb_rm) (name, 0);
724 else if (variable->type == xbt_cfgelm_string) {
725 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
730 xbt_dynar_set(variable->content, 0, &newval);
732 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
733 THROW3(mismatch_error, 0,
734 "Cannot add value %s to the config element %s since it's already full (size=%d)",
735 name, val, variable->max);
737 xbt_dynar_push(variable->content, &newval);
740 if (variable->cb_set)
741 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
744 /** @brief Set or add an peer value to \a name within \a cfg
746 * \arg cfg the config set
747 * \arg name the name of the variable
749 * \arg port the port number
751 * \e peer values are composed of a string (peername) and an integer (port)
755 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
757 xbt_cfgelm_t variable;
758 xbt_peer_t val = xbt_peer_new(peer, port);
760 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
762 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
764 if (variable->max == 1) {
765 if (variable->cb_rm && xbt_dynar_length(variable->content))
766 (*variable->cb_rm) (name, 0);
768 xbt_dynar_set(variable->content, 0, &val);
770 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
771 THROW4(mismatch_error, 0,
772 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
773 peer, port, name, variable->max);
775 xbt_dynar_push(variable->content, &val);
778 if (variable->cb_set)
779 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
782 /* ---- [ Removing ] ---- */
784 /** @brief Remove the provided \e val integer value from a variable
786 * \arg cfg the config set
787 * \arg name the name of the variable
788 * \arg val the value to be removed
790 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
793 xbt_cfgelm_t variable;
797 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
799 if (xbt_dynar_length(variable->content) == variable->min)
800 THROW3(mismatch_error, 0,
801 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
802 val, name, variable->min);
804 xbt_dynar_foreach(variable->content, cpt, seen) {
807 (*variable->cb_rm) (name, cpt);
808 xbt_dynar_cursor_rm(variable->content, &cpt);
813 THROW2(not_found_error, 0,
814 "Can't remove the value %d of config element %s: value not found.",
818 /** @brief Remove the provided \e val double value from a variable
820 * \arg cfg the config set
821 * \arg name the name of the variable
822 * \arg val the value to be removed
825 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
827 xbt_cfgelm_t variable;
831 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
833 if (xbt_dynar_length(variable->content) == variable->min)
834 THROW3(mismatch_error, 0,
835 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
836 val, name, variable->min);
838 xbt_dynar_foreach(variable->content, cpt, seen) {
840 xbt_dynar_cursor_rm(variable->content, &cpt);
842 (*variable->cb_rm) (name, cpt);
847 THROW2(not_found_error, 0,
848 "Can't remove the value %f of config element %s: value not found.",
852 /** @brief Remove the provided \e val string value from a variable
854 * \arg cfg the config set
855 * \arg name the name of the variable
856 * \arg val the value of the string which will be removed
858 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
860 xbt_cfgelm_t variable;
864 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
866 if (xbt_dynar_length(variable->content) == variable->min)
867 THROW3(mismatch_error, 0,
868 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
869 name, val, variable->min);
871 xbt_dynar_foreach(variable->content, cpt, seen) {
872 if (!strcpy(seen, val)) {
874 (*variable->cb_rm) (name, cpt);
875 xbt_dynar_cursor_rm(variable->content, &cpt);
880 THROW2(not_found_error, 0,
881 "Can't remove the value %s of config element %s: value not found.",
885 /** @brief Remove the provided \e val peer value from a variable
887 * \arg cfg the config set
888 * \arg name the name of the variable
889 * \arg peer the peername
890 * \arg port the port number
894 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
896 xbt_cfgelm_t variable;
900 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
902 if (xbt_dynar_length(variable->content) == variable->min)
903 THROW4(mismatch_error, 0,
904 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
905 peer, port, name, variable->min);
907 xbt_dynar_foreach(variable->content, cpt, seen) {
908 if (!strcpy(seen->name, peer) && seen->port == port) {
910 (*variable->cb_rm) (name, cpt);
911 xbt_dynar_cursor_rm(variable->content, &cpt);
916 THROW3(not_found_error, 0,
917 "Can't remove the value %s:%d of config element %s: value not found.",
921 /** @brief Remove the \e pos th value from the provided variable */
923 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
926 xbt_cfgelm_t variable;
928 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
930 if (xbt_dynar_length(variable->content) == variable->min)
931 THROW3(mismatch_error, 0,
932 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
933 pos, name, variable->min);
936 (*variable->cb_rm) (name, pos);
937 xbt_dynar_remove_at(variable->content, pos, NULL);
940 /** @brief Remove all the values from a variable
942 * \arg cfg the config set
943 * \arg name the name of the variable
946 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
948 xbt_cfgelm_t variable = NULL;
952 variable = xbt_dict_get((xbt_dict_t) cfg, name);
954 if (e.category != not_found_error)
958 THROW1(not_found_error, 0,
959 "Can't empty '%s' since this config element does not exist",
964 if (variable->cb_rm) {
967 xbt_dynar_foreach(variable->content, cpt, ignored) {
968 (*variable->cb_rm) (name, cpt);
971 xbt_dynar_reset(variable->content);
975 /*----[ Getting ]---------------------------------------------------------*/
977 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
979 * \arg cfg the config set
980 * \arg name the name of the variable
981 * \arg val the wanted value
983 * Returns the first value from the config set under the given name.
984 * If there is more than one value, it will issue a warning. Consider using
985 * xbt_cfg_get_dynar() instead.
987 * \warning the returned value is the actual content of the config set
989 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
991 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
993 if (xbt_dynar_length(variable->content) > 1) {
995 ("You asked for the first value of the config element '%s', but there is %lu values",
996 name, xbt_dynar_length(variable->content));
999 return xbt_dynar_get_as(variable->content, 0, int);
1002 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1004 * \arg cfg the config set
1005 * \arg name the name of the variable
1006 * \arg val the wanted value
1008 * Returns the first value from the config set under the given name.
1009 * If there is more than one value, it will issue a warning. Consider using
1010 * xbt_cfg_get_dynar() instead.
1012 * \warning the returned value is the actual content of the config set
1015 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1017 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1019 if (xbt_dynar_length(variable->content) > 1) {
1021 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1022 name, xbt_dynar_length(variable->content));
1025 return xbt_dynar_get_as(variable->content, 0, double);
1028 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1030 * \arg th the config set
1031 * \arg name the name of the variable
1032 * \arg val the wanted value
1034 * Returns the first value from the config set under the given name.
1035 * If there is more than one value, it will issue a warning. Consider using
1036 * xbt_cfg_get_dynar() instead.
1038 * \warning the returned value is the actual content of the config set
1041 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1043 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1045 if (xbt_dynar_length(variable->content) > 1) {
1047 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1048 name, xbt_dynar_length(variable->content));
1051 return xbt_dynar_get_as(variable->content, 0, char *);
1054 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1056 * \arg cfg the config set
1057 * \arg name the name of the variable
1058 * \arg peer the peer
1059 * \arg port the port number
1061 * Returns the first value from the config set under the given name.
1062 * If there is more than one value, it will issue a warning. Consider using
1063 * xbt_cfg_get_dynar() instead.
1065 * \warning the returned value is the actual content of the config set
1068 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1070 xbt_cfgelm_t variable;
1073 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1075 if (xbt_dynar_length(variable->content) > 1) {
1077 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1078 name, xbt_dynar_length(variable->content));
1081 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1086 /** @brief Retrieve the dynar of all the values stored in a variable
1088 * \arg cfg where to search in
1089 * \arg name what to search for
1092 * Get the data stored in the config set.
1094 * \warning the returned value is the actual content of the config set
1096 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1098 xbt_cfgelm_t variable = NULL;
1102 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1104 if (e.category == not_found_error) {
1106 THROW1(not_found_error, 0,
1107 "No registered variable %s in this config set", name);
1112 return variable->content;
1116 /** @brief Retrieve one of the integer value of a variable */
1117 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1120 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1121 return xbt_dynar_get_as(variable->content, pos, int);
1124 /** @brief Retrieve one of the double value of a variable */
1125 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1128 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1129 return xbt_dynar_get_as(variable->content, pos, double);
1133 /** @brief Retrieve one of the string value of a variable */
1134 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1137 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1138 return xbt_dynar_get_as(variable->content, pos, char *);
1141 /** @brief Retrieve one of the peer value of a variable */
1143 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1144 char **peer, int *port)
1147 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1148 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1159 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1161 XBT_TEST_SUITE("config", "Configuration support");
1163 static xbt_cfg_t make_set()
1165 xbt_cfg_t set = NULL;
1167 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg),xbt_log_priority_critical);
1168 set = xbt_cfg_new();
1169 xbt_cfg_register_str(set, "speed:1_to_2_int");
1170 xbt_cfg_register_str(set, "peername:1_to_1_string");
1171 xbt_cfg_register_str(set, "user:1_to_10_string");
1174 } /* end_of_make_set */
1176 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1178 xbt_cfg_t set = make_set();
1179 xbt_test_add0("Alloc and free a config set");
1180 xbt_cfg_set_parse(set,
1181 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1186 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1188 xbt_cfg_t set = set = make_set();
1191 xbt_test_add0("Having too few elements for speed");
1192 xbt_cfg_set_parse(set,
1193 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1198 if (e.category != mismatch_error ||
1199 strncmp(e.msg, "Config elem speed needs",
1200 strlen("Config elem speed needs")))
1201 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1209 xbt_test_add0("Having too much values of 'speed'");
1211 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1213 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1216 if (e.category != mismatch_error ||
1217 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1218 strlen("Config elem speed needs")))
1219 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1228 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1231 xbt_test_add0("Get a single value");
1233 /* get_single_value */
1235 xbt_cfg_t myset = make_set();
1237 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1238 ival = xbt_cfg_get_int(myset, "speed");
1240 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1241 xbt_cfg_free(&myset);
1244 xbt_test_add0("Get multiple values");
1246 /* get_multiple_value */
1248 xbt_cfg_t myset = make_set();
1250 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1251 xbt_cfg_set_parse(myset, "speed:42");
1252 xbt_cfg_check(myset);
1253 dyn = xbt_cfg_get_dynar(myset, "user");
1255 if (xbt_dynar_length(dyn) != 3)
1256 xbt_test_fail1("Dynar length = %lu, I expected 3",
1257 xbt_dynar_length(dyn));
1259 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1260 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1261 xbt_dynar_get_as(dyn, 0, char *));
1263 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1264 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1265 xbt_dynar_get_as(dyn, 1, char *));
1267 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1268 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1269 xbt_dynar_get_as(dyn, 2, char *));
1270 xbt_cfg_free(&myset);
1273 xbt_test_add0("Access to a non-existant entry");
1275 /* non-existant_entry */
1276 xbt_cfg_t myset = make_set();
1280 xbt_cfg_set_parse(myset, "color:blue");
1282 if (e.category != not_found_error)
1283 xbt_test_exception(e);
1286 xbt_cfg_free(&myset);
1289 #endif /* SIMGRID_TEST */