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_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL;
26 /* xbt_cfgelm_t: the typedef corresponding to a config variable. */
32 /* Allowed type of the variable */
33 e_xbt_cfgelm_type_t type;
39 /* Advanced callbacks */
40 xbt_cfg_cb_ext_t cb_set_ext;
42 xbt_cfg_cb_free_t cb_set_free;
44 /* actual content (could be an union or something) */
46 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
48 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
50 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
58 /* Internal stuff used in cache to free a variable */
59 static void xbt_cfgelm_free(void *data);
61 /* Retrieve the variable we'll modify */
62 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
64 /*----[ Memory management ]-----------------------------------------------*/
65 /** @brief Constructor
67 * Initialise a config set
69 xbt_cfg_t xbt_cfg_new(void)
71 return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
74 /** @brief Destructor */
75 void xbt_cfg_free(xbt_cfg_t * cfg)
77 XBT_DEBUG("Frees cfg set %p", cfg);
78 xbt_dict_free((xbt_dict_t *) cfg);
81 /** @brief Dump a config set for debuging purpose
83 * @param name The name to give to this config set
84 * @param indent what to write at the beginning of each line (right number of spaces)
85 * @param cfg the config set
87 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
89 xbt_dict_t dict = (xbt_dict_t) cfg;
90 xbt_dict_cursor_t cursor = NULL;
91 xbt_cfgelm_t variable = NULL;
100 printf("%s>> Dumping of the config set '%s':\n", indent, name);
102 xbt_dict_foreach(dict, cursor, key, variable) {
103 printf("%s %s:", indent, key);
105 size = xbt_dynar_length(variable->content);
106 printf ("%s. Actual size=%d. postset=%p\n",
107 xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
109 switch (variable->type) {
111 for (i = 0; i < size; i++) {
112 ival = xbt_dynar_get_as(variable->content, i, int);
113 printf("%s %d\n", indent, ival);
116 case xbt_cfgelm_double:
117 for (i = 0; i < size; i++) {
118 dval = xbt_dynar_get_as(variable->content, i, double);
119 printf("%s %f\n", indent, dval);
122 case xbt_cfgelm_string:
123 for (i = 0; i < size; i++) {
124 sval = xbt_dynar_get_as(variable->content, i, char *);
125 printf("%s %s\n", indent, sval);
128 case xbt_cfgelm_boolean:
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_alias:
138 printf("%s Invalid type!!\n", indent);
144 printf("%s<< End of the config set '%s'\n", indent, name);
147 xbt_dict_cursor_free(&cursor);
151 * free an config element
153 void xbt_cfgelm_free(void *data)
155 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
157 XBT_DEBUG("Frees cfgelm %p", c);
161 c->cb_set_free(c->cb_set_data);
163 if (c->type != xbt_cfgelm_alias)
164 xbt_dynar_free(&(c->content));
168 /*----[ Registering stuff ]-----------------------------------------------*/
169 /** @brief Register an element within a config set
171 * @param cfg the config set
172 * @param name the name of the config element
173 * @param desc a description for this item (used by xbt_cfg_help())
174 * @param type the type of the config element
175 * @param cb_set callback function called when a value is set
177 static void xbt_cfg_register(
178 xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type,
180 xbt_cfg_cb_ext_t cb_set_ext, void* cb_set_data, xbt_cfg_cb_free_t cb_set_free)
183 *cfg = xbt_cfg_new();
184 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
185 "type of %s not valid (%d should be between %d and %d)",
186 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
188 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
189 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
191 res = xbt_new(s_xbt_cfgelm_t, 1);
192 XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
193 name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
195 res->desc = xbt_strdup(desc);
197 res->cb_set = cb_set;
198 res->cb_set_ext = cb_set_ext;
199 res->cb_set_data = cb_set_data;
200 res->cb_set_free = cb_set_free;
205 res->content = xbt_dynar_new(sizeof(int), NULL);
207 case xbt_cfgelm_double:
208 res->content = xbt_dynar_new(sizeof(double), NULL);
210 case xbt_cfgelm_string:
211 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
213 case xbt_cfgelm_boolean:
214 res->content = xbt_dynar_new(sizeof(int), NULL);
217 XBT_ERROR("%d is an invalid type code", (int)type);
220 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
225 void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc){
226 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set, NULL, NULL, NULL);
227 xbt_cfg_setdefault_double(name, default_value);
229 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) {
230 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set, NULL, NULL, NULL);
231 xbt_cfg_setdefault_int(name, default_value);
233 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc){
234 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set, NULL, NULL, NULL);
235 xbt_cfg_setdefault_string(name, default_value);
237 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc){
238 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set, NULL, NULL, NULL);
239 xbt_cfg_setdefault_boolean(name, default_value);
242 /** Register a config with an extended callback
244 * @param name Name of the flag
245 * @param desc Description of the flag
246 * @param type Type of the flag
247 * @param cb Extended callback
248 * @param data Data associated with the callback
249 * @param data_free Function used to free the callback data (or NULL)
251 void xbt_cfg_register_ext(const char *name, const char *desc, e_xbt_cfgelm_type_t type,
252 xbt_cfg_cb_ext_t cb, void* data, xbt_cfg_cb_free_t data_free)
254 xbt_cfg_register(&simgrid_config, name, desc, type, NULL, cb, data, data_free);
257 void xbt_cfg_register_alias(const char *newname, const char *oldname)
259 if (simgrid_config == NULL)
260 simgrid_config = xbt_cfg_new();
262 xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
263 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
265 res = xbt_dict_get_or_null(simgrid_config, newname);
266 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
268 res = xbt_new0(s_xbt_cfgelm_t, 1);
269 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
271 res->desc = bprintf("Deprecated alias for %s",newname);
272 res->type = xbt_cfgelm_alias;
274 res->content = (xbt_dynar_t)newname;
276 xbt_dict_set(simgrid_config, oldname, res, NULL);
280 * @brief Parse a string and register the stuff described.
282 * @param cfg the config set
283 * @param 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\>:\<type\>
287 * with type being one of 'string','int','bool' or 'double'.
289 * Note that this does not allow to set the description, so you should prefer the other interface
291 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
293 char *entrycpy = xbt_strdup(entry);
296 e_xbt_cfgelm_type_t type;
297 XBT_DEBUG("Register string '%s'", entry);
299 tok = strchr(entrycpy, ':');
300 xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
303 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
304 xbt_assert(type < xbt_cfgelm_type_count,
305 "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
307 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, NULL, NULL, NULL);
309 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
312 /** @brief Displays the declared aliases and their description */
313 void xbt_cfg_aliases(void)
315 xbt_dict_cursor_t dict_cursor;
316 unsigned int dynar_cursor;
317 xbt_cfgelm_t variable;
319 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
321 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
322 xbt_dynar_push(names, &name);
323 xbt_dynar_sort_strings(names);
325 xbt_dynar_foreach(names, dynar_cursor, name) {
326 variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
328 if (variable->type == xbt_cfgelm_alias)
329 printf(" %s: %s\n", name, variable->desc);
333 /** @brief Displays the declared options and their description */
334 void xbt_cfg_help(void)
336 xbt_dict_cursor_t dict_cursor;
337 unsigned int dynar_cursor;
338 xbt_cfgelm_t variable;
340 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
342 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
343 xbt_dynar_push(names, &name);
344 xbt_dynar_sort_strings(names);
346 xbt_dynar_foreach(names, dynar_cursor, name) {
348 variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
349 if (variable->type == xbt_cfgelm_alias)
352 printf(" %s: %s\n", name, variable->desc);
353 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
354 size = xbt_dynar_length(variable->content);
355 printf("Current value: ");
358 printf(size == 0 ? "n/a\n" : "{ ");
359 for (int i = 0; i < size; i++) {
360 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
362 switch (variable->type) {
364 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
366 case xbt_cfgelm_double:
367 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
369 case xbt_cfgelm_string:
370 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
372 case xbt_cfgelm_boolean: {
373 int b = xbt_dynar_get_as(variable->content, i, int);
374 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
375 if (b == 0 || b == 1)
376 printf("'%s'%s", bs, sep);
378 printf("'%s/%d'%s", bs, b, sep);
382 printf("Invalid type!!%s", sep);
387 xbt_dynar_free(&names);
390 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
392 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
394 // The user used the old name. Switch to the new one after a short warning
395 while (res && res->type == xbt_cfgelm_alias) {
396 const char* newname = (const char *)res->content;
397 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
398 res = xbt_cfgelm_get(cfg, newname, type);
404 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
407 xbt_assert(type == xbt_cfgelm_any || res->type == type,
408 "You tried to access to the config element %s as an %s, but its type is %s.",
409 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
413 /** @brief Get the type of this variable in that configuration set
415 * @param cfg the config set
416 * @param name the name of the element
418 * @return the type of the given element
420 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
422 xbt_cfgelm_t variable = NULL;
424 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
426 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
428 XBT_DEBUG("type in variable = %d", (int)variable->type);
429 return variable->type;
432 /*----[ Setting ]---------------------------------------------------------*/
433 /** @brief va_args version of xbt_cfg_set
435 * @param cfg config set to fill
436 * @param name variable name
437 * @param pa variable value
439 * Add some values to the config set.
441 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
446 e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
451 type = xbt_cfg_get_type(cfg, name);
454 if (e.category == not_found_error) {
456 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
462 case xbt_cfgelm_string:
463 str = va_arg(pa, char *);
464 xbt_cfg_set_string(name, str);
468 xbt_cfg_set_int(name, i);
470 case xbt_cfgelm_double:
471 d = va_arg(pa, double);
472 xbt_cfg_set_double(name, d);
474 case xbt_cfgelm_boolean:
475 str = va_arg(pa, char *);
476 xbt_cfg_set_boolean(name, str);
479 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
483 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
485 * @param cfg config set to fill
486 * @param name variable name
487 * @param ... variable value
489 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
494 xbt_cfg_set_vargs(cfg, name, pa);
498 /** @brief Add values parsed from a string into a config set
500 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
501 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
502 * already registered variable, and [value] conforms to the data type under which this variable was registered.
504 * @todo This is a crude manual parser, it should be a proper lexer.
506 void xbt_cfg_set_parse(const char *options)
508 if (!options || !strlen(options)) { /* nothing to do */
511 char *optionlist_cpy = xbt_strdup(options);
513 XBT_DEBUG("List to parse and set:'%s'", options);
514 char *option = optionlist_cpy;
515 while (1) { /* breaks in the code */
519 int len = strlen(name);
520 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
523 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
524 XBT_DEBUG("Take %c.", *option);
527 if (option - name == len) {
528 XBT_DEBUG("Boundary=EOL");
529 option = NULL; /* don't do next iteration */
531 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
532 /* Pass the following blank chars */
534 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
535 /* fprintf(stderr,"Ignore a blank char.\n"); */
538 if (option - name == len - 1)
539 option = NULL; /* don't do next iteration */
541 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
543 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
548 char *val = strchr(name, ':');
549 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
550 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
553 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
554 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
557 xbt_cfg_set_as_string(name,val);
559 free(optionlist_cpy);
563 free(optionlist_cpy);
566 /** @brief Set the value of a variable, using the string representation of that value
568 * @param key name of the variable to modify
569 * @param value string representation of the value to set
571 * @return the first char after the parsed value in val
574 void *xbt_cfg_set_as_string(const char *key, const char *value) {
578 volatile xbt_cfgelm_t variable = NULL;
583 while (variable == NULL) {
584 variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
585 if (variable->type == xbt_cfgelm_alias) {
586 const char *newname = (const char*)variable->content;
587 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
593 if (e.category == not_found_error) {
595 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
600 switch (variable->type) {
601 case xbt_cfgelm_string:
602 xbt_cfg_set_string(key, value); /* throws */
605 i = strtol(value, &ret, 0);
607 xbt_die("Value of option %s not valid. Should be an integer", key);
609 xbt_cfg_set_int(key, i); /* throws */
611 case xbt_cfgelm_double:
612 d = strtod(value, &ret);
614 xbt_die("Value of option %s not valid. Should be a double", key);
616 xbt_cfg_set_double(key, d); /* throws */
618 case xbt_cfgelm_boolean:
619 xbt_cfg_set_boolean(key, value); /* throws */
620 ret = (char *)value + strlen(value);
623 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
629 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
631 * This is useful to change the default value of a variable while allowing
632 * users to override it with command line arguments
634 void xbt_cfg_setdefault_int(const char *name, int val)
636 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
638 if (variable->isdefault){
639 xbt_cfg_set_int(name, val);
640 variable->isdefault = 1;
642 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
645 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
647 * This is useful to change the default value of a variable while allowing
648 * users to override it with command line arguments
650 void xbt_cfg_setdefault_double(const char *name, double val)
652 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
654 if (variable->isdefault) {
655 xbt_cfg_set_double(name, val);
656 variable->isdefault = 1;
658 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
661 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
663 * This is useful to change the default value of a variable while allowing
664 * users to override it with command line arguments
666 void xbt_cfg_setdefault_string(const char *name, const char *val)
668 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
670 if (variable->isdefault){
671 xbt_cfg_set_string(name, val);
672 variable->isdefault = 1;
674 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
677 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
679 * This is useful to change the default value of a variable while allowing
680 * users to override it with command line arguments
682 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
684 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
686 if (variable->isdefault){
687 xbt_cfg_set_boolean(name, val);
688 variable->isdefault = 1;
691 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
694 /** @brief Set an integer value to \a name within \a cfg
696 * @param name the name of the variable
697 * @param val the value of the variable
699 void xbt_cfg_set_int(const char *name, int val)
701 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
703 xbt_dynar_set(variable->content, 0, &val);
705 if (variable->cb_set)
706 variable->cb_set(name);
707 if (variable->cb_set_ext)
708 variable->cb_set_ext(name, variable->cb_set_data);
709 variable->isdefault = 0;
712 /** @brief Set or add a double value to \a name within \a cfg
714 * @param name the name of the variable
715 * @param val the double to set
717 void xbt_cfg_set_double(const char *name, double val)
719 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
721 xbt_dynar_set(variable->content, 0, &val);
723 if (variable->cb_set)
724 variable->cb_set(name);
725 if (variable->cb_set_ext)
726 variable->cb_set_ext(name, variable->cb_set_data);
727 variable->isdefault = 0;
730 /** @brief Set or add a string value to \a name within \a cfg
732 * @param cfg the config set
733 * @param name the name of the variable
734 * @param val the value to be added
737 void xbt_cfg_set_string(const char *name, const char *val)
739 char *newval = xbt_strdup(val);
740 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
742 if (!xbt_dynar_is_empty(variable->content)) {
743 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
747 xbt_dynar_set(variable->content, 0, &newval);
749 if (variable->cb_set)
750 variable->cb_set(name);
751 if (variable->cb_set_ext)
752 variable->cb_set_ext(name, variable->cb_set_data);
753 variable->isdefault = 0;
756 /** @brief Set or add a boolean value to \a name within \a cfg
758 * @param name the name of the variable
759 * @param val the value of the variable
761 void xbt_cfg_set_boolean(const char *name, const char *val)
764 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
766 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
767 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
771 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
776 xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
777 xbt_dynar_set(variable->content, 0, &bval);
779 if (variable->cb_set)
780 variable->cb_set(name);
781 if (variable->cb_set_ext)
782 variable->cb_set_ext(name, variable->cb_set_data);
783 variable->isdefault = 0;
787 /* Say if the value is the default value */
788 int xbt_cfg_is_default_value(const char *name)
790 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
791 return variable->isdefault;
794 /*----[ Getting ]---------------------------------------------------------*/
795 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
797 * @param name the name of the variable
799 * Returns the first value from the config set under the given name.
800 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
802 int xbt_cfg_get_int(const char *name)
804 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
806 if (xbt_dynar_length(variable->content) > 1) {
807 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
808 name, xbt_dynar_length(variable->content));
811 return xbt_dynar_get_as(variable->content, 0, int);
814 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
816 * @param cfg the config set
817 * @param name the name of the variable
819 * Returns the first value from the config set under the given name.
820 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
822 double xbt_cfg_get_double(const char *name)
824 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
826 if (xbt_dynar_length(variable->content) > 1) {
827 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
828 name, xbt_dynar_length(variable->content));
831 return xbt_dynar_get_as(variable->content, 0, double);
834 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
836 * @param cfg the config set
837 * @param name the name of the variable
839 * Returns the first value from the config set under the given name.
840 * If there is more than one value, it will issue a warning. Consider using
841 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
843 * \warning the returned value is the actual content of the config set
845 char *xbt_cfg_get_string(const char *name)
847 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
849 if (xbt_dynar_length(variable->content) > 1) {
850 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
851 name, xbt_dynar_length(variable->content));
852 } else if (xbt_dynar_is_empty(variable->content)) {
856 return xbt_dynar_get_as(variable->content, 0, char *);
859 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
861 * @param cfg the config set
862 * @param name the name of the variable
864 * Returns the first value from the config set under the given name.
865 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
867 int xbt_cfg_get_boolean(const char *name)
869 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
871 if (xbt_dynar_length(variable->content) > 1) {
872 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
873 name, xbt_dynar_length(variable->content));
876 return xbt_dynar_get_as(variable->content, 0, int);
883 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
885 XBT_TEST_SUITE("config", "Configuration support");
887 static xbt_cfg_t make_set()
889 xbt_cfg_t set = NULL;
891 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
892 xbt_cfg_register_str(&set, "speed:int");
893 xbt_cfg_register_str(&set, "peername:string");
894 xbt_cfg_register_str(&set, "user:string");
897 } /* end_of_make_set */
899 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
901 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
903 simgrid_config = make_set();
904 xbt_test_add("Alloc and free a config set");
905 xbt_cfg_set_parse("peername:veloce user:bidule");
906 xbt_cfg_free(&simgrid_config);
909 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
911 simgrid_config = make_set();
912 xbt_test_add("Get a single value");
914 /* get_single_value */
917 xbt_cfg_set_parse("peername:toto:42 speed:42");
918 ival = xbt_cfg_get_int("speed");
920 xbt_test_fail("Speed value = %d, I expected 42", ival);
923 xbt_test_add("Access to a non-existant entry");
928 xbt_cfg_set_parse("color:blue");
930 if (e.category != not_found_error)
931 xbt_test_exception(e);
935 xbt_cfg_free(&simgrid_config);
937 #endif /* SIMGRID_TEST */