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);
188 xbt_dynar_free(&(c->content));
192 /*----[ Registering stuff ]-----------------------------------------------*/
194 /** @brief Register an element within a config set
196 * @arg cfg the config set
197 * @arg type the type of the config element
198 * @arg min the minimum
199 * @arg max the maximum
203 xbt_cfg_register(xbt_cfg_t *cfg,
204 const char *name, const char *desc, e_xbt_cfgelm_type_t type, void *default_value,
205 int min, int max, xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm)
211 xbt_assert4(type >= xbt_cfgelm_int && type <= xbt_cfgelm_peer,
212 "type of %s not valid (%d should be between %d and %d)",
213 name, type, xbt_cfgelm_int, xbt_cfgelm_peer);
214 res = xbt_dict_get_or_null((xbt_dict_t) *cfg, name);
217 WARN1("Config elem %s registered twice.", name);
218 /* Will be removed by the insertion of the new one */
221 res = xbt_new(s_xbt_cfgelm_t, 1);
222 DEBUG8("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
223 name, desc, min, max, xbt_cfgelm_type_name[type], type, res, *cfg);
225 res->desc = xbt_strdup(desc);
229 res->cb_set = cb_set;
234 res->content = xbt_dynar_new(sizeof(int), NULL);
236 xbt_dynar_push(res->content,default_value);
239 case xbt_cfgelm_double:
240 res->content = xbt_dynar_new(sizeof(double), NULL);
242 xbt_dynar_push(res->content,default_value);
245 case xbt_cfgelm_string:
246 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
248 xbt_dynar_push(res->content,default_value);
251 case xbt_cfgelm_peer:
252 res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
254 xbt_dynar_push(res->content,default_value);
258 ERROR1("%d is an invalide type code", type);
261 xbt_dict_set((xbt_dict_t) *cfg, name, res, &xbt_cfgelm_free);
264 /** @brief Unregister an element from a config set.
266 * @arg cfg the config set
267 * @arg name the name of the elem to be freed
269 * Note that it removes both the description and the actual content.
270 * Throws not_found when no such element exists.
273 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
275 DEBUG2("Unregister elm '%s' from set %p", name, cfg);
276 xbt_dict_remove((xbt_dict_t) cfg, name);
280 * @brief Parse a string and register the stuff described.
282 * @arg cfg the config set
283 * @arg entry a string describing the element to register
285 * The string may consist in several variable descriptions separated by a space.
286 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
287 * with type being one of 'string','int', 'peer' or 'double'.
289 * @fixme: this does not allow to set the description
292 void xbt_cfg_register_str(xbt_cfg_t *cfg, const char *entry)
294 char *entrycpy = xbt_strdup(entry);
298 e_xbt_cfgelm_type_t type;
299 DEBUG1("Register string '%s'", entry);
301 tok = strchr(entrycpy, ':');
302 xbt_assert2(tok, "Invalid config element descriptor: %s%s",
303 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
306 min = strtol(tok, &tok, 10);
307 xbt_assert1(tok, "Invalid minimum in config element descriptor %s", entry);
309 xbt_assert2(strcmp(tok, "_to_"),
310 "Invalid config element descriptor : %s%s",
311 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
312 tok += strlen("_to_");
314 max = strtol(tok, &tok, 10);
315 xbt_assert1(tok, "Invalid maximum in config element descriptor %s", entry);
317 xbt_assert2(*(tok++) == '_',
318 "Invalid config element descriptor: %s%s", entry,
319 "; Should be <name>:<min nb>_to_<max nb>_<type>");
322 type < xbt_cfgelm_type_count
323 && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
324 xbt_assert2(type < xbt_cfgelm_type_count,
325 "Invalid type in config element descriptor: %s%s", entry,
326 "; Should be one of 'string', 'int', 'peer' or 'double'.");
328 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, min, max, NULL, NULL);
330 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
333 /** @brief Displays the declared options and their description */
334 void xbt_cfg_help(xbt_cfg_t cfg) {
335 xbt_dict_cursor_t cursor;
336 xbt_cfgelm_t variable;
342 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
343 printf(" %s: %s\n",name,variable->desc);
344 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
345 if (variable->min != 1 || variable->max != 1)
346 printf("Arrity: min:%d to max:%d; ",variable->min,variable->max);
347 printf("Current value: ");
348 size = xbt_dynar_length(variable->content);
350 switch (variable->type) {
357 for (i = 0; i < size; i++) {
358 ival = xbt_dynar_get_as(variable->content, i, int);
359 printf("%s%d\n", (i==0?"":" "),ival);
363 case xbt_cfgelm_double:
364 for (i = 0; i < size; i++) {
365 dval = xbt_dynar_get_as(variable->content, i, double);
366 printf("%s%f\n", (i==0?"":" "),dval);
370 case xbt_cfgelm_string:
371 for (i = 0; i < size; i++) {
372 sval = xbt_dynar_get_as(variable->content, i, char *);
373 printf("%s'%s'\n", (i==0?"":" "),sval);
377 case xbt_cfgelm_peer:
378 for (i = 0; i < size; i++) {
379 hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
380 printf("%s%s:%d\n", (i==0?"":" "),hval->name, hval->port);
385 printf("Invalid type!!\n");
391 /** @brief Check that each variable have the right amount of values */
392 void xbt_cfg_check(xbt_cfg_t cfg)
394 xbt_dict_cursor_t cursor;
395 xbt_cfgelm_t variable;
399 xbt_assert0(cfg, "NULL config set.");
400 DEBUG1("Check cfg set %p", cfg);
402 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
403 size = xbt_dynar_length(variable->content);
404 if (variable->min > size) {
405 xbt_dict_cursor_free(&cursor);
406 THROW4(mismatch_error, 0,
407 "Config elem %s needs at least %d %s, but there is only %d values.",
408 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
411 if (variable->max > 0 && variable->max < size) {
412 xbt_dict_cursor_free(&cursor);
413 THROW4(mismatch_error, 0,
414 "Config elem %s accepts at most %d %s, but there is %d values.",
415 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
419 xbt_dict_cursor_free(&cursor);
422 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
423 const char *name, e_xbt_cfgelm_type_t type)
425 xbt_cfgelm_t res = NULL;
427 res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
430 THROW1(not_found_error, 0,
431 "No registered variable '%s' in this config set", name);
434 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
435 "You tried to access to the config element %s as an %s, but its type is %s.",
437 xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
442 /** @brief Get the type of this variable in that configuration set
444 * \arg cfg the config set
445 * \arg name the name of the element
446 * \arg type the result
450 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
453 xbt_cfgelm_t variable = NULL;
455 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
457 THROW1(not_found_error, 0,
458 "Can't get the type of '%s' since this variable does not exist",
461 INFO1("type in variable = %d", variable->type);
463 return variable->type;
466 /*----[ Setting ]---------------------------------------------------------*/
467 /** @brief va_args version of xbt_cfg_set
469 * \arg cfg config set to fill
470 * \arg n variable name
471 * \arg pa variable value
473 * Add some values to the config set.
475 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
480 e_xbt_cfgelm_type_t type = 0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
485 type = xbt_cfg_get_type(cfg, name);
487 if (e.category == not_found_error) {
489 THROW1(not_found_error, 0,
490 "Can't set the property '%s' since it's not registered", name);
496 case xbt_cfgelm_peer:
497 str = va_arg(pa, char *);
499 xbt_cfg_set_peer(cfg, name, str, i);
502 case xbt_cfgelm_string:
503 str = va_arg(pa, char *);
504 xbt_cfg_set_string(cfg, name, str);
509 xbt_cfg_set_int(cfg, name, i);
512 case xbt_cfgelm_double:
513 d = va_arg(pa, double);
514 xbt_cfg_set_double(cfg, name, d);
518 xbt_assert2(0, "Config element variable %s not valid (type=%d)", name,
523 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
525 * \arg cfg config set to fill
526 * \arg name variable name
527 * \arg varargs variable value
530 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
535 xbt_cfg_set_vargs(cfg, name, pa);
539 /** @brief Add values parsed from a string into a config set
541 * \arg cfg config set to fill
542 * \arg options a string containing the content to add to the config set. This
543 * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
544 * like "[name]:[value]" where [name] is the name of an already registred
545 * variable, and [value] conforms to the data type under which this variable was
548 * @todo This is a crude manual parser, it should be a proper lexer.
551 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
559 xbt_cfgelm_t variable = NULL;
560 char *optionlist_cpy;
561 char *option, *name, *val;
566 if (!options || !strlen(options)) { /* nothing to do */
569 optionlist_cpy = xbt_strdup(options);
571 DEBUG1("List to parse and set:'%s'", options);
572 option = optionlist_cpy;
573 while (1) { /* breaks in the code */
579 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
580 name, len, (long) (option - name));
583 while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
584 && *option != '\t' && *option != ',') {
585 DEBUG1("Take %c.", *option);
588 if (option - name == len) {
589 DEBUG0("Boundary=EOL");
590 option = NULL; /* don't do next iteration */
593 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
594 *option, len, (long) (option - name));
596 /* Pass the following blank chars */
598 while (option - name < (len - 1) &&
599 (*option == ' ' || *option == '\n' || *option == '\t')) {
600 /* fprintf(stderr,"Ignore a blank char.\n"); */
603 if (option - name == len - 1)
604 option = NULL; /* don't do next iteration */
606 DEBUG2("parse now:'%s'; parse later:'%s'", name, option);
608 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
613 val = strchr(name, ':');
615 free(optionlist_cpy);
617 "Option '%s' badly formated. Should be of the form 'name:value'",
622 INFO2("Configuration change: Set '%s' to '%s'", name, val);
625 variable = xbt_dict_get((xbt_dict_t) cfg, name);
628 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
629 name = (char *) (optionlist_cpy - name + options);
630 free(optionlist_cpy);
631 if (e.category == not_found_error) {
633 THROW1(not_found_error, 0,
634 "No registrated variable corresponding to '%s'.", name);
640 switch (variable->type) {
641 case xbt_cfgelm_string:
642 xbt_cfg_set_string(cfg, name, val); /* throws */
646 i = strtol(val, &val, 0);
648 free(optionlist_cpy);
650 "Value of option %s not valid. Should be an integer",
654 xbt_cfg_set_int(cfg, name, i); /* throws */
657 case xbt_cfgelm_double:
658 d = strtod(val, &val);
660 free(optionlist_cpy);
662 "Value of option %s not valid. Should be a double",
666 xbt_cfg_set_double(cfg, name, d); /* throws */
669 case xbt_cfgelm_peer:
671 val = strchr(val, ':');
673 free(optionlist_cpy);
675 "Value of option %s not valid. Should be an peer (machine:port)",
680 i = strtol(val, &val, 0);
682 free(optionlist_cpy);
684 "Value of option %s not valid. Should be an peer (machine:port)",
688 xbt_cfg_set_peer(cfg, name, str, i); /* throws */
692 THROW1(unknown_error, 0, "Type of config element %s is not valid.",
697 free(optionlist_cpy);
701 free(optionlist_cpy);
705 /** @brief Set or add an integer value to \a name within \a cfg
707 * \arg cfg the config set
708 * \arg name the name of the variable
709 * \arg val the value of the variable
711 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
713 xbt_cfgelm_t variable;
715 VERB2("Configuration setting: %s=%d", name, val);
716 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
718 if (variable->max == 1) {
719 if (variable->cb_rm && xbt_dynar_length(variable->content))
720 (*variable->cb_rm) (name, 0);
722 xbt_dynar_set(variable->content, 0, &val);
725 && xbt_dynar_length(variable->content) ==
726 (unsigned long) variable->max)
727 THROW3(mismatch_error, 0,
728 "Cannot add value %d to the config element %s since it's already full (size=%d)",
729 val, name, variable->max);
731 xbt_dynar_push(variable->content, &val);
734 if (variable->cb_set)
735 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
738 /** @brief Set or add a double value to \a name within \a cfg
740 * \arg cfg the config set
741 * \arg name the name of the variable
742 * \arg val the doule to set
745 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
747 xbt_cfgelm_t variable;
749 VERB2("Configuration setting: %s=%f", name, val);
750 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
752 if (variable->max == 1) {
753 if (variable->cb_rm && xbt_dynar_length(variable->content))
754 (*variable->cb_rm) (name, 0);
756 xbt_dynar_set(variable->content, 0, &val);
758 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
759 THROW3(mismatch_error, 0,
760 "Cannot add value %f to the config element %s since it's already full (size=%d)",
761 val, name, variable->max);
763 xbt_dynar_push(variable->content, &val);
766 if (variable->cb_set)
767 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
770 /** @brief Set or add a string value to \a name within \a cfg
772 * \arg cfg the config set
773 * \arg name the name of the variable
774 * \arg val the value to be added
778 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
780 xbt_cfgelm_t variable;
781 char *newval = xbt_strdup(val);
783 VERB2("Configuration setting: %s=%s", name, val);
784 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
785 DEBUG5("Variable: %d to %d %s (=%d) @%p",
786 variable->min, variable->max, xbt_cfgelm_type_name[variable->type],
787 variable->type, variable);
789 if (variable->max == 1) {
790 if (xbt_dynar_length(variable->content)) {
792 (*variable->cb_rm) (name, 0);
793 else if (variable->type == xbt_cfgelm_string) {
794 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
799 xbt_dynar_set(variable->content, 0, &newval);
801 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
802 THROW3(mismatch_error, 0,
803 "Cannot add value %s to the config element %s since it's already full (size=%d)",
804 name, val, variable->max);
806 xbt_dynar_push(variable->content, &newval);
809 if (variable->cb_set)
810 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
813 /** @brief Set or add an peer value to \a name within \a cfg
815 * \arg cfg the config set
816 * \arg name the name of the variable
818 * \arg port the port number
820 * \e peer values are composed of a string (peername) and an integer (port)
824 xbt_cfg_set_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
826 xbt_cfgelm_t variable;
827 xbt_peer_t val = xbt_peer_new(peer, port);
829 VERB3("Configuration setting: %s=%s:%d", name, peer, port);
831 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
833 if (variable->max == 1) {
834 if (variable->cb_rm && xbt_dynar_length(variable->content))
835 (*variable->cb_rm) (name, 0);
837 xbt_dynar_set(variable->content, 0, &val);
839 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
840 THROW4(mismatch_error, 0,
841 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
842 peer, port, name, variable->max);
844 xbt_dynar_push(variable->content, &val);
847 if (variable->cb_set)
848 (*variable->cb_set) (name, xbt_dynar_length(variable->content) - 1);
851 /* ---- [ Removing ] ---- */
853 /** @brief Remove the provided \e val integer value from a variable
855 * \arg cfg the config set
856 * \arg name the name of the variable
857 * \arg val the value to be removed
859 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
862 xbt_cfgelm_t variable;
866 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
868 if (xbt_dynar_length(variable->content) == variable->min)
869 THROW3(mismatch_error, 0,
870 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
871 val, name, variable->min);
873 xbt_dynar_foreach(variable->content, cpt, seen) {
876 (*variable->cb_rm) (name, cpt);
877 xbt_dynar_cursor_rm(variable->content, &cpt);
882 THROW2(not_found_error, 0,
883 "Can't remove the value %d of config element %s: value not found.",
887 /** @brief Remove the provided \e val double value from a variable
889 * \arg cfg the config set
890 * \arg name the name of the variable
891 * \arg val the value to be removed
894 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
896 xbt_cfgelm_t variable;
900 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
902 if (xbt_dynar_length(variable->content) == variable->min)
903 THROW3(mismatch_error, 0,
904 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
905 val, name, variable->min);
907 xbt_dynar_foreach(variable->content, cpt, seen) {
909 xbt_dynar_cursor_rm(variable->content, &cpt);
911 (*variable->cb_rm) (name, cpt);
916 THROW2(not_found_error, 0,
917 "Can't remove the value %f of config element %s: value not found.",
921 /** @brief Remove the provided \e val string value from a variable
923 * \arg cfg the config set
924 * \arg name the name of the variable
925 * \arg val the value of the string which will be removed
927 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
929 xbt_cfgelm_t variable;
933 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
935 if (xbt_dynar_length(variable->content) == variable->min)
936 THROW3(mismatch_error, 0,
937 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
938 name, val, variable->min);
940 xbt_dynar_foreach(variable->content, cpt, seen) {
941 if (!strcpy(seen, val)) {
943 (*variable->cb_rm) (name, cpt);
944 xbt_dynar_cursor_rm(variable->content, &cpt);
949 THROW2(not_found_error, 0,
950 "Can't remove the value %s of config element %s: value not found.",
954 /** @brief Remove the provided \e val peer value from a variable
956 * \arg cfg the config set
957 * \arg name the name of the variable
958 * \arg peer the peername
959 * \arg port the port number
963 xbt_cfg_rm_peer(xbt_cfg_t cfg, const char *name, const char *peer, int port)
965 xbt_cfgelm_t variable;
969 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
971 if (xbt_dynar_length(variable->content) == variable->min)
972 THROW4(mismatch_error, 0,
973 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
974 peer, port, name, variable->min);
976 xbt_dynar_foreach(variable->content, cpt, seen) {
977 if (!strcpy(seen->name, peer) && seen->port == port) {
979 (*variable->cb_rm) (name, cpt);
980 xbt_dynar_cursor_rm(variable->content, &cpt);
985 THROW3(not_found_error, 0,
986 "Can't remove the value %s:%d of config element %s: value not found.",
990 /** @brief Remove the \e pos th value from the provided variable */
992 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
995 xbt_cfgelm_t variable;
997 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
999 if (xbt_dynar_length(variable->content) == variable->min)
1000 THROW3(mismatch_error, 0,
1001 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1002 pos, name, variable->min);
1004 if (variable->cb_rm)
1005 (*variable->cb_rm) (name, pos);
1006 xbt_dynar_remove_at(variable->content, pos, NULL);
1009 /** @brief Remove all the values from a variable
1011 * \arg cfg the config set
1012 * \arg name the name of the variable
1015 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1017 xbt_cfgelm_t variable = NULL;
1021 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1023 if (e.category != not_found_error)
1027 THROW1(not_found_error, 0,
1028 "Can't empty '%s' since this config element does not exist",
1033 if (variable->cb_rm) {
1036 xbt_dynar_foreach(variable->content, cpt, ignored) {
1037 (*variable->cb_rm) (name, cpt);
1040 xbt_dynar_reset(variable->content);
1044 /*----[ Getting ]---------------------------------------------------------*/
1046 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1048 * \arg cfg the config set
1049 * \arg name the name of the variable
1050 * \arg val the wanted value
1052 * Returns the first value from the config set under the given name.
1053 * If there is more than one value, it will issue a warning. Consider using
1054 * xbt_cfg_get_dynar() instead.
1056 * \warning the returned value is the actual content of the config set
1058 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1060 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1062 if (xbt_dynar_length(variable->content) > 1) {
1064 ("You asked for the first value of the config element '%s', but there is %lu values",
1065 name, xbt_dynar_length(variable->content));
1068 return xbt_dynar_get_as(variable->content, 0, int);
1071 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1073 * \arg cfg the config set
1074 * \arg name the name of the variable
1075 * \arg val the wanted value
1077 * Returns the first value from the config set under the given name.
1078 * If there is more than one value, it will issue a warning. Consider using
1079 * xbt_cfg_get_dynar() instead.
1081 * \warning the returned value is the actual content of the config set
1084 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1086 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1088 if (xbt_dynar_length(variable->content) > 1) {
1090 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1091 name, xbt_dynar_length(variable->content));
1094 return xbt_dynar_get_as(variable->content, 0, double);
1097 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1099 * \arg th the config set
1100 * \arg name the name of the variable
1101 * \arg val the wanted value
1103 * Returns the first value from the config set under the given name.
1104 * If there is more than one value, it will issue a warning. Consider using
1105 * xbt_cfg_get_dynar() instead.
1107 * \warning the returned value is the actual content of the config set
1110 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1112 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1114 if (xbt_dynar_length(variable->content) > 1) {
1116 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1117 name, xbt_dynar_length(variable->content));
1120 return xbt_dynar_get_as(variable->content, 0, char *);
1123 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1125 * \arg cfg the config set
1126 * \arg name the name of the variable
1127 * \arg peer the peer
1128 * \arg port the port number
1130 * Returns the first value from the config set under the given name.
1131 * If there is more than one value, it will issue a warning. Consider using
1132 * xbt_cfg_get_dynar() instead.
1134 * \warning the returned value is the actual content of the config set
1137 void xbt_cfg_get_peer(xbt_cfg_t cfg, const char *name, char **peer, int *port)
1139 xbt_cfgelm_t variable;
1142 variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_peer);
1144 if (xbt_dynar_length(variable->content) > 1) {
1146 ("You asked for the first value of the config element '%s', but there is %lu values\n",
1147 name, xbt_dynar_length(variable->content));
1150 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1155 /** @brief Retrieve the dynar of all the values stored in a variable
1157 * \arg cfg where to search in
1158 * \arg name what to search for
1161 * Get the data stored in the config set.
1163 * \warning the returned value is the actual content of the config set
1165 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1167 xbt_cfgelm_t variable = NULL;
1171 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1173 if (e.category == not_found_error) {
1175 THROW1(not_found_error, 0,
1176 "No registered variable %s in this config set", name);
1181 return variable->content;
1185 /** @brief Retrieve one of the integer value of a variable */
1186 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1189 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1190 return xbt_dynar_get_as(variable->content, pos, int);
1193 /** @brief Retrieve one of the double value of a variable */
1194 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1197 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1198 return xbt_dynar_get_as(variable->content, pos, double);
1202 /** @brief Retrieve one of the string value of a variable */
1203 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1206 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1207 return xbt_dynar_get_as(variable->content, pos, char *);
1210 /** @brief Retrieve one of the peer value of a variable */
1212 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1213 char **peer, int *port)
1216 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1217 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1228 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
1230 XBT_TEST_SUITE("config", "Configuration support");
1232 static xbt_cfg_t make_set()
1234 xbt_cfg_t set = NULL;
1236 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg),xbt_log_priority_critical);
1237 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1238 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1239 xbt_cfg_register_str(&set, "user:1_to_10_string");
1242 } /* end_of_make_set */
1244 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1246 xbt_cfg_t set = make_set();
1247 xbt_test_add0("Alloc and free a config set");
1248 xbt_cfg_set_parse(set,
1249 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1254 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1256 xbt_cfg_t set = set = make_set();
1259 xbt_test_add0("Having too few elements for speed");
1260 xbt_cfg_set_parse(set,
1261 "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1266 if (e.category != mismatch_error ||
1267 strncmp(e.msg, "Config elem speed needs",
1268 strlen("Config elem speed needs")))
1269 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1277 xbt_test_add0("Having too much values of 'speed'");
1279 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1281 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1284 if (e.category != mismatch_error ||
1285 strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1286 strlen("Config elem speed needs")))
1287 xbt_test_fail1("Got an exception. msg=%s", e.msg);
1296 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1299 xbt_test_add0("Get a single value");
1301 /* get_single_value */
1303 xbt_cfg_t myset = make_set();
1305 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1306 ival = xbt_cfg_get_int(myset, "speed");
1308 xbt_test_fail1("Speed value = %d, I expected 42", ival);
1309 xbt_cfg_free(&myset);
1312 xbt_test_add0("Get multiple values");
1314 /* get_multiple_value */
1316 xbt_cfg_t myset = make_set();
1318 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1319 xbt_cfg_set_parse(myset, "speed:42");
1320 xbt_cfg_check(myset);
1321 dyn = xbt_cfg_get_dynar(myset, "user");
1323 if (xbt_dynar_length(dyn) != 3)
1324 xbt_test_fail1("Dynar length = %lu, I expected 3",
1325 xbt_dynar_length(dyn));
1327 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1328 xbt_test_fail1("Dynar[0] = %s, I expected foo",
1329 xbt_dynar_get_as(dyn, 0, char *));
1331 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1332 xbt_test_fail1("Dynar[1] = %s, I expected bar",
1333 xbt_dynar_get_as(dyn, 1, char *));
1335 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1336 xbt_test_fail1("Dynar[2] = %s, I expected toto",
1337 xbt_dynar_get_as(dyn, 2, char *));
1338 xbt_cfg_free(&myset);
1341 xbt_test_add0("Access to a non-existant entry");
1343 /* non-existant_entry */
1344 xbt_cfg_t myset = make_set();
1348 xbt_cfg_set_parse(myset, "color:blue");
1350 if (e.category != not_found_error)
1351 xbt_test_exception(e);
1354 xbt_cfg_free(&myset);
1357 #endif /* SIMGRID_TEST */