1 /* config - Dictionnary where the type of each variable is provided. */
3 /* This is useful to build named structs, like option or property sets. */
5 /* Copyright (c) 2004-2014. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/sysdep.h"
15 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever defend my thesis. */
32 /* Allowed type of the variable */
33 e_xbt_cfgelm_type_t type;
40 /* actual content (could be an union or something) */
42 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
44 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
46 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
54 /* Internal stuff used in cache to free a variable */
55 static void xbt_cfgelm_free(void *data);
57 /* Retrieve the variable we'll modify */
58 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
60 /*----[ Memory management ]-----------------------------------------------*/
61 /** @brief Constructor
63 * Initialise a config set
65 xbt_cfg_t xbt_cfg_new(void)
67 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
70 /** \brief Copy an existing configuration set
72 * @param whereto the config set to be created
73 * @param tocopy the source data
75 * This only copy the registrations, not the actual content
77 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
79 xbt_dict_cursor_t cursor = NULL;
80 xbt_cfgelm_t variable = NULL;
83 XBT_DEBUG("Copy cfg set %p", tocopy);
85 xbt_assert(tocopy, "cannot copy NULL config");
87 xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
88 xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->max, variable->cb_set);
92 /** @brief Destructor */
93 void xbt_cfg_free(xbt_cfg_t * cfg)
95 XBT_DEBUG("Frees cfg set %p", cfg);
96 xbt_dict_free((xbt_dict_t *) cfg);
99 /** @brief Dump a config set for debuging purpose
101 * @param name The name to give to this config set
102 * @param indent what to write at the beginning of each line (right number of spaces)
103 * @param cfg the config set
105 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
107 xbt_dict_t dict = (xbt_dict_t) cfg;
108 xbt_dict_cursor_t cursor = NULL;
109 xbt_cfgelm_t variable = NULL;
118 printf("%s>> Dumping of the config set '%s':\n", indent, name);
120 xbt_dict_foreach(dict, cursor, key, variable) {
121 printf("%s %s:", indent, key);
123 size = xbt_dynar_length(variable->content);
124 printf ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
125 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
127 switch (variable->type) {
129 for (i = 0; i < size; i++) {
130 ival = xbt_dynar_get_as(variable->content, i, int);
131 printf("%s %d\n", indent, ival);
134 case xbt_cfgelm_double:
135 for (i = 0; i < size; i++) {
136 dval = xbt_dynar_get_as(variable->content, i, double);
137 printf("%s %f\n", indent, dval);
140 case xbt_cfgelm_string:
141 for (i = 0; i < size; i++) {
142 sval = xbt_dynar_get_as(variable->content, i, char *);
143 printf("%s %s\n", indent, sval);
146 case xbt_cfgelm_boolean:
147 for (i = 0; i < size; i++) {
148 ival = xbt_dynar_get_as(variable->content, i, int);
149 printf("%s %d\n", indent, ival);
152 case xbt_cfgelm_alias:
156 printf("%s Invalid type!!\n", indent);
162 printf("%s<< End of the config set '%s'\n", indent, name);
165 xbt_dict_cursor_free(&cursor);
169 * free an config element
171 void xbt_cfgelm_free(void *data)
173 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
175 XBT_DEBUG("Frees cfgelm %p", c);
179 if (c->type != xbt_cfgelm_alias)
180 xbt_dynar_free(&(c->content));
184 /*----[ Registering stuff ]-----------------------------------------------*/
185 /** @brief Register an element within a config set
187 * @param cfg the config set
188 * @param name the name of the config element
189 * @param desc a description for this item (used by xbt_cfg_help())
190 * @param type the type of the config element
191 * @param min the minimum number of values for this config element
192 * @param max the maximum number of values for this config element
193 * @param cb_set callback function called when a value is set
195 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min,
196 int max, xbt_cfg_cb_t cb_set)
201 *cfg = xbt_cfg_new();
202 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
203 "type of %s not valid (%d should be between %d and %d)",
204 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
205 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
206 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
208 res = xbt_new(s_xbt_cfgelm_t, 1);
209 XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
210 name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
212 res->desc = xbt_strdup(desc);
216 res->cb_set = cb_set;
221 res->content = xbt_dynar_new(sizeof(int), NULL);
223 case xbt_cfgelm_double:
224 res->content = xbt_dynar_new(sizeof(double), NULL);
226 case xbt_cfgelm_string:
227 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
229 case xbt_cfgelm_boolean:
230 res->content = xbt_dynar_new(sizeof(int), NULL);
233 XBT_ERROR("%d is an invalid type code", (int)type);
236 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
239 void xbt_cfg_register_double(xbt_cfg_t * cfg, const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
240 xbt_cfg_register(cfg,name,desc,xbt_cfgelm_double,1,1,cb_set);
241 xbt_cfg_setdefault_double(*cfg, name, default_value);
243 void xbt_cfg_register_int(xbt_cfg_t * cfg, const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
244 xbt_cfg_register(cfg,name,desc,xbt_cfgelm_int,1,1,cb_set);
245 xbt_cfg_setdefault_int(*cfg, name, default_value);
247 void xbt_cfg_register_string(xbt_cfg_t * cfg, const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
248 xbt_cfg_register(cfg,name,desc,xbt_cfgelm_string,1,1,cb_set);
249 xbt_cfg_setdefault_string(*cfg, name, default_value);
251 void xbt_cfg_register_boolean(xbt_cfg_t * cfg, const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
252 xbt_cfg_register(cfg,name,desc,xbt_cfgelm_boolean,1,1,cb_set);
253 xbt_cfg_setdefault_boolean(*cfg, name, default_value);
256 void xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname)
259 *cfg = xbt_cfg_new();
261 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, oldname);
262 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
264 res = xbt_dict_get_or_null((xbt_dict_t) * cfg, newname);
265 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
267 res = xbt_new0(s_xbt_cfgelm_t, 1);
268 XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
270 res->desc = bprintf("Deprecated alias for %s",newname);
271 res->type = xbt_cfgelm_alias;
275 res->content = (xbt_dynar_t)newname;
277 xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
280 /** @brief Unregister an element from a config set.
282 * @param cfg the config set
283 * @param name the name of the element to be freed
285 * Note that it removes both the description and the actual content.
286 * Throws not_found when no such element exists.
288 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
290 XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
291 xbt_dict_remove((xbt_dict_t) cfg, name);
295 * @brief Parse a string and register the stuff described.
297 * @param cfg the config set
298 * @param entry a string describing the element to register
300 * The string may consist in several variable descriptions separated by a space.
301 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
302 * with type being one of 'string','int' or 'double'.
304 * Note that this does not allow to set the description, so you should prefer the other interface
306 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
308 char *entrycpy = xbt_strdup(entry);
312 e_xbt_cfgelm_type_t type;
313 XBT_DEBUG("Register string '%s'", entry);
315 tok = strchr(entrycpy, ':');
316 xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
319 min = strtol(tok, &tok, 10);
320 xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
322 xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
323 entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
324 tok += strlen("_to_");
326 max = strtol(tok, &tok, 10);
327 xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
329 xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
330 "; Should be <name>:<min nb>_to_<max nb>_<type>");
333 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
334 xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
335 "; Should be one of 'string', 'int' or 'double'.");
337 xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
339 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
342 /** @brief Displays the declared aliases and their description */
343 void xbt_cfg_aliases(xbt_cfg_t cfg)
345 xbt_dict_cursor_t dict_cursor;
346 unsigned int dynar_cursor;
347 xbt_cfgelm_t variable;
349 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
351 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
352 xbt_dynar_push(names, &name);
353 xbt_dynar_sort_strings(names);
355 xbt_dynar_foreach(names, dynar_cursor, name) {
356 variable = xbt_dict_get((xbt_dict_t )cfg, name);
358 if (variable->type == xbt_cfgelm_alias)
359 printf(" %s: %s\n", name, variable->desc);
363 /** @brief Displays the declared options and their description */
364 void xbt_cfg_help(xbt_cfg_t cfg)
366 xbt_dict_cursor_t dict_cursor;
367 unsigned int dynar_cursor;
368 xbt_cfgelm_t variable;
370 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
372 xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
373 xbt_dynar_push(names, &name);
374 xbt_dynar_sort_strings(names);
376 xbt_dynar_foreach(names, dynar_cursor, name) {
379 variable = xbt_dict_get((xbt_dict_t )cfg, name);
380 if (variable->type == xbt_cfgelm_alias)
383 printf(" %s: %s\n", name, variable->desc);
384 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
385 if (variable->min != 1 || variable->max != 1) {
386 printf("Arity: min:%d to max:", variable->min);
387 if (variable->max == 0)
388 printf("(no bound); ");
390 printf("%d; ", variable->max);
392 size = xbt_dynar_length(variable->content);
393 printf("Current value%s: ", (size <= 1 ? "" : "s"));
396 printf(size == 0 ? "n/a\n" : "{ ");
397 for (i = 0; i < size; i++) {
398 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
400 switch (variable->type) {
402 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
404 case xbt_cfgelm_double:
405 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
407 case xbt_cfgelm_string:
408 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
410 case xbt_cfgelm_boolean: {
411 int b = xbt_dynar_get_as(variable->content, i, int);
412 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
413 if (b == 0 || b == 1)
414 printf("'%s'%s", bs, sep);
416 printf("'%s/%d'%s", bs, b, sep);
420 printf("Invalid type!!%s", sep);
424 xbt_dynar_free(&names);
427 /** @brief Check that each variable have the right amount of values */
428 void xbt_cfg_check(xbt_cfg_t cfg)
430 xbt_dict_cursor_t cursor;
431 xbt_cfgelm_t variable;
435 xbt_assert(cfg, "NULL config set.");
436 XBT_DEBUG("Check cfg set %p", cfg);
438 xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
439 if (variable->type == xbt_cfgelm_alias)
442 size = xbt_dynar_length(variable->content);
443 if (variable->min > size) {
444 xbt_dict_cursor_free(&cursor);
445 THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
446 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
449 if (variable->isdefault && size > variable->min) {
450 xbt_dict_cursor_free(&cursor);
451 THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
452 name, variable->min, xbt_cfgelm_type_name[variable->type], size);
455 if (variable->max > 0 && variable->max < size) {
456 xbt_dict_cursor_free(&cursor);
457 THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
458 name, variable->max, xbt_cfgelm_type_name[variable->type], size);
461 xbt_dict_cursor_free(&cursor);
464 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
466 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
468 // The user used the old name. Switch to the new one after a short warning
469 while (res && res->type == xbt_cfgelm_alias) {
470 const char* newname = (const char *)res->content;
471 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
472 res = xbt_cfgelm_get(cfg, newname, type);
478 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
481 xbt_assert(type == xbt_cfgelm_any || res->type == type,
482 "You tried to access to the config element %s as an %s, but its type is %s.",
483 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
487 /** @brief Get the type of this variable in that configuration set
489 * @param cfg the config set
490 * @param name the name of the element
492 * @return the type of the given element
494 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
496 xbt_cfgelm_t variable = NULL;
498 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
500 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
502 XBT_DEBUG("type in variable = %d", (int)variable->type);
503 return variable->type;
506 /*----[ Setting ]---------------------------------------------------------*/
507 /** @brief va_args version of xbt_cfg_set
509 * @param cfg config set to fill
510 * @param name variable name
511 * @param pa variable value
513 * Add some values to the config set.
515 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
520 e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
525 type = xbt_cfg_get_type(cfg, name);
528 if (e.category == not_found_error) {
530 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
536 case xbt_cfgelm_string:
537 str = va_arg(pa, char *);
538 xbt_cfg_set_string(cfg, name, str);
542 xbt_cfg_set_int(cfg, name, i);
544 case xbt_cfgelm_double:
545 d = va_arg(pa, double);
546 xbt_cfg_set_double(cfg, name, d);
548 case xbt_cfgelm_boolean:
549 str = va_arg(pa, char *);
550 xbt_cfg_set_boolean(cfg, name, str);
553 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
557 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
559 * @param cfg config set to fill
560 * @param name variable name
561 * @param ... variable value
563 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
568 xbt_cfg_set_vargs(cfg, name, pa);
572 /** @brief Add values parsed from a string into a config set
574 * @param cfg config set to fill
575 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
576 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
577 * already registered variable, and [value] conforms to the data type under which this variable was registered.
579 * @todo This is a crude manual parser, it should be a proper lexer.
581 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
582 char *optionlist_cpy;
583 char *option, *name, *val;
587 if (!options || !strlen(options)) { /* nothing to do */
590 optionlist_cpy = xbt_strdup(options);
592 XBT_DEBUG("List to parse and set:'%s'", options);
593 option = optionlist_cpy;
594 while (1) { /* breaks in the code */
599 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
602 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
603 XBT_DEBUG("Take %c.", *option);
606 if (option - name == len) {
607 XBT_DEBUG("Boundary=EOL");
608 option = NULL; /* don't do next iteration */
610 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
611 /* Pass the following blank chars */
613 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
614 /* fprintf(stderr,"Ignore a blank char.\n"); */
617 if (option - name == len - 1)
618 option = NULL; /* don't do next iteration */
620 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
622 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
627 val = strchr(name, ':');
629 /* don't free(optionlist_cpy) here, 'name' points inside it */
630 xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'", name);
634 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
635 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
638 xbt_cfg_set_as_string(cfg,name,val);
640 free(optionlist_cpy);
644 free(optionlist_cpy);
647 /** @brief Set the value of a variable, using the string representation of that value
649 * @param cfg config set to modify
650 * @param key name of the variable to modify
651 * @param value string representation of the value to set
653 * @return the first char after the parsed value in val
656 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
660 volatile xbt_cfgelm_t variable = NULL;
665 while (variable == NULL) {
666 variable = xbt_dict_get((xbt_dict_t) cfg, key);
667 if (variable->type == xbt_cfgelm_alias) {
668 const char *newname = (const char*)variable->content;
669 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
675 if (e.category == not_found_error) {
677 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
682 switch (variable->type) {
683 case xbt_cfgelm_string:
684 xbt_cfg_set_string(cfg, key, value); /* throws */
687 i = strtol(value, &ret, 0);
689 xbt_die("Value of option %s not valid. Should be an integer", key);
691 xbt_cfg_set_int(cfg, key, i); /* throws */
693 case xbt_cfgelm_double:
694 d = strtod(value, &ret);
696 xbt_die("Value of option %s not valid. Should be a double", key);
698 xbt_cfg_set_double(cfg, key, d); /* throws */
700 case xbt_cfgelm_boolean:
701 xbt_cfg_set_boolean(cfg, key, value); /* throws */
702 ret = (char *)value + strlen(value);
705 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
711 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
713 * This is useful to change the default value of a variable while allowing
714 * users to override it with command line arguments
716 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
718 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
720 if (variable->isdefault){
721 xbt_cfg_set_int(cfg, name, val);
722 variable->isdefault = 1;
724 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
727 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
729 * This is useful to change the default value of a variable while allowing
730 * users to override it with command line arguments
732 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
734 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
736 if (variable->isdefault) {
737 xbt_cfg_set_double(cfg, name, val);
738 variable->isdefault = 1;
740 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
743 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
745 * This is useful to change the default value of a variable while allowing
746 * users to override it with command line arguments
748 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char *val)
750 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
752 if (variable->isdefault){
753 xbt_cfg_set_string(cfg, name, val);
754 variable->isdefault = 1;
756 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
759 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
761 * This is useful to change the default value of a variable while allowing
762 * users to override it with command line arguments
764 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
766 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
768 if (variable->isdefault){
769 xbt_cfg_set_boolean(cfg, name, val);
770 variable->isdefault = 1;
773 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
776 /** @brief Set or add an integer value to \a name within \a cfg
778 * @param cfg the config set
779 * @param name the name of the variable
780 * @param val the value of the variable
782 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
784 XBT_VERB("Configuration setting: %s=%d", name, val);
785 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
787 if (variable->max == 1) {
788 xbt_dynar_set(variable->content, 0, &val);
790 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
791 THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
792 val, name, variable->max);
794 xbt_dynar_push(variable->content, &val);
797 if (variable->cb_set)
798 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
799 variable->isdefault = 0;
802 /** @brief Set or add a double value to \a name within \a cfg
804 * @param cfg the config set
805 * @param name the name of the variable
806 * @param val the doule to set
808 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val)
810 XBT_VERB("Configuration setting: %s=%f", name, val);
811 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
813 if (variable->max == 1) {
814 xbt_dynar_set(variable->content, 0, &val);
816 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
817 THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
818 val, name, variable->max);
820 xbt_dynar_push(variable->content, &val);
823 if (variable->cb_set)
824 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
825 variable->isdefault = 0;
828 /** @brief Set or add a string value to \a name within \a cfg
830 * @param cfg the config set
831 * @param name the name of the variable
832 * @param val the value to be added
835 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
837 char *newval = xbt_strdup(val);
839 XBT_VERB("Configuration setting: %s=%s", name, val);
840 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
842 XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
843 variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
845 if (variable->max == 1) {
846 if (!xbt_dynar_is_empty(variable->content)) {
847 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
851 xbt_dynar_set(variable->content, 0, &newval);
854 && xbt_dynar_length(variable->content) == variable->max)
855 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
856 name, val, variable->max);
858 xbt_dynar_push(variable->content, &newval);
861 if (variable->cb_set)
862 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
863 variable->isdefault = 0;
866 /** @brief Set or add a boolean value to \a name within \a cfg
868 * @param cfg the config set
869 * @param name the name of the variable
870 * @param val the value of the variable
872 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
876 XBT_VERB("Configuration setting: %s=%s", name, val);
877 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
879 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
880 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
884 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
889 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
890 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
893 if (variable->max == 1) {
894 xbt_dynar_set(variable->content, 0, &bval);
896 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
897 THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
898 val, name, variable->max);
900 xbt_dynar_push(variable->content, &bval);
903 if (variable->cb_set)
904 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
905 variable->isdefault = 0;
908 /* ---- [ Removing ] ---- */
909 /** @brief Remove the provided \e val integer value from a variable
911 * @param cfg the config set
912 * @param name the name of the variable
913 * @param val the value to be removed
915 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
920 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
922 if (xbt_dynar_length(variable->content) == variable->min)
923 THROWF(mismatch_error, 0,
924 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
925 val, name, variable->min);
927 xbt_dynar_foreach(variable->content, cpt, seen) {
929 xbt_dynar_cursor_rm(variable->content, &cpt);
933 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
936 /** @brief Remove the provided \e val double value from a variable
938 * @param cfg the config set
939 * @param name the name of the variable
940 * @param val the value to be removed
942 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
947 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
949 if (xbt_dynar_length(variable->content) == variable->min)
950 THROWF(mismatch_error, 0,
951 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
952 val, name, variable->min);
954 xbt_dynar_foreach(variable->content, cpt, seen) {
956 xbt_dynar_cursor_rm(variable->content, &cpt);
961 THROWF(not_found_error, 0,"Can't remove the value %f of config element %s: value not found.", val, name);
964 /** @brief Remove the provided \e val string value from a variable
966 * @param cfg the config set
967 * @param name the name of the variable
968 * @param val the value of the string which will be removed
970 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
974 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
976 if (xbt_dynar_length(variable->content) == variable->min)
977 THROWF(mismatch_error, 0,
978 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
979 name, val, variable->min);
981 xbt_dynar_foreach(variable->content, cpt, seen) {
982 if (!strcpy(seen, val)) {
983 xbt_dynar_cursor_rm(variable->content, &cpt);
988 THROWF(not_found_error, 0, "Can't remove the value %s of config element %s: value not found.", val, name);
991 /** @brief Remove the provided \e val boolean value from a variable
993 * @param cfg the config set
994 * @param name the name of the variable
995 * @param val the value to be removed
997 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1001 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1003 if (xbt_dynar_length(variable->content) == variable->min)
1004 THROWF(mismatch_error, 0,
1005 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1006 val, name, variable->min);
1008 xbt_dynar_foreach(variable->content, cpt, seen) {
1010 xbt_dynar_cursor_rm(variable->content, &cpt);
1015 THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
1018 /** @brief Remove the \e pos th value from the provided variable */
1019 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1021 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1023 if (xbt_dynar_length(variable->content) == variable->min)
1024 THROWF(mismatch_error, 0,
1025 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1026 pos, name, variable->min);
1028 xbt_dynar_remove_at(variable->content, pos, NULL);
1031 /** @brief Remove all the values from a variable
1033 * @param cfg the config set
1034 * @param name the name of the variable
1036 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1038 xbt_cfgelm_t variable = NULL;
1042 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1044 if (e.category != not_found_error)
1048 THROWF(not_found_error, 0, "Can't empty '%s' since this option does not exist", name);
1052 xbt_dynar_reset(variable->content);
1055 /* Say if the value is the default value */
1056 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1058 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1059 return variable->isdefault;
1062 /*----[ Getting ]---------------------------------------------------------*/
1063 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1065 * @param cfg the config set
1066 * @param name the name of the variable
1068 * Returns the first value from the config set under the given name.
1069 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1071 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1073 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1075 if (xbt_dynar_length(variable->content) > 1) {
1076 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1077 name, xbt_dynar_length(variable->content));
1080 return xbt_dynar_get_as(variable->content, 0, int);
1083 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1085 * @param cfg the config set
1086 * @param name the name of the variable
1088 * Returns the first value from the config set under the given name.
1089 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1091 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1093 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1095 if (xbt_dynar_length(variable->content) > 1) {
1096 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
1097 name, xbt_dynar_length(variable->content));
1100 return xbt_dynar_get_as(variable->content, 0, double);
1103 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1105 * @param cfg the config set
1106 * @param name the name of the variable
1108 * Returns the first value from the config set under the given name.
1109 * If there is more than one value, it will issue a warning. Consider using
1110 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1112 * \warning the returned value is the actual content of the config set
1114 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1116 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1118 if (xbt_dynar_length(variable->content) > 1) {
1119 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
1120 name, xbt_dynar_length(variable->content));
1121 } else if (xbt_dynar_is_empty(variable->content)) {
1125 return xbt_dynar_get_as(variable->content, 0, char *);
1128 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1130 * @param cfg the config set
1131 * @param name the name of the variable
1133 * Returns the first value from the config set under the given name.
1134 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1136 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1138 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1140 if (xbt_dynar_length(variable->content) > 1) {
1141 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1142 name, xbt_dynar_length(variable->content));
1145 return xbt_dynar_get_as(variable->content, 0, int);
1148 /** @brief Retrieve the dynar of all the values stored in a variable
1150 * @param cfg where to search in
1151 * @param name what to search for
1153 * Get the data stored in the config set.
1155 * \warning the returned value is the actual content of the config set
1157 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1159 xbt_cfgelm_t variable = NULL;
1163 variable = xbt_dict_get((xbt_dict_t) cfg, name);
1165 if (e.category == not_found_error) {
1167 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1172 return variable->content;
1175 /** @brief Retrieve one of the integer value of a variable */
1176 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1178 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1179 return xbt_dynar_get_as(variable->content, pos, int);
1182 /** @brief Retrieve one of the double value of a variable */
1183 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1185 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1186 return xbt_dynar_get_as(variable->content, pos, double);
1189 /** @brief Retrieve one of the string value of a variable */
1190 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1192 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1193 return xbt_dynar_get_as(variable->content, pos, char *);
1196 /** @brief Retrieve one of the boolean value of a variable */
1197 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1199 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1200 return xbt_dynar_get_as(variable->content, pos, int);
1207 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1209 XBT_TEST_SUITE("config", "Configuration support");
1211 static xbt_cfg_t make_set()
1213 xbt_cfg_t set = NULL;
1215 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1216 xbt_cfg_register_str(&set, "speed:1_to_2_int");
1217 xbt_cfg_register_str(&set, "peername:1_to_1_string");
1218 xbt_cfg_register_str(&set, "user:1_to_10_string");
1221 } /* end_of_make_set */
1223 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1225 xbt_cfg_t set = make_set();
1226 xbt_test_add("Alloc and free a config set");
1227 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1232 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1234 xbt_cfg_t set = set = make_set();
1237 xbt_test_add("Having too few elements for speed");
1238 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1242 if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1243 xbt_test_fail("Got an exception. msg=%s", e.msg);
1249 xbt_test_add("Having too much values of 'speed'");
1251 xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1253 xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1255 if (e.category != mismatch_error ||
1256 strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1257 xbt_test_fail("Got an exception. msg=%s", e.msg);
1265 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1267 xbt_test_add("Get a single value");
1269 /* get_single_value */
1271 xbt_cfg_t myset = make_set();
1273 xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1274 ival = xbt_cfg_get_int(myset, "speed");
1276 xbt_test_fail("Speed value = %d, I expected 42", ival);
1277 xbt_cfg_free(&myset);
1280 xbt_test_add("Get multiple values");
1282 /* get_multiple_value */
1284 xbt_cfg_t myset = make_set();
1286 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1287 xbt_cfg_set_parse(myset, "speed:42");
1288 xbt_cfg_check(myset);
1289 dyn = xbt_cfg_get_dynar(myset, "user");
1291 if (xbt_dynar_length(dyn) != 3)
1292 xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1294 if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1295 xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1297 if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1298 xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1300 if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1301 xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1302 xbt_cfg_free(&myset);
1305 xbt_test_add("Access to a non-existant entry");
1307 /* non-existant_entry */
1308 xbt_cfg_t myset = make_set();
1312 xbt_cfg_set_parse(myset, "color:blue");
1314 if (e.category != not_found_error)
1315 xbt_test_exception(e);
1318 xbt_cfg_free(&myset);
1321 #endif /* SIMGRID_TEST */