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_cfg_t simgrid_config = NULL;
25 static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set);
27 /* xbt_cfgelm_t: the typedef corresponding to a config variable. */
33 /* Allowed type of the variable */
34 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", "outofbound" };
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->cb_set);
91 /** @brief Destructor */
92 void xbt_cfg_free(xbt_cfg_t * cfg)
94 XBT_DEBUG("Frees cfg set %p", cfg);
95 xbt_dict_free((xbt_dict_t *) cfg);
98 /** @brief Dump a config set for debuging purpose
100 * @param name The name to give to this config set
101 * @param indent what to write at the beginning of each line (right number of spaces)
102 * @param cfg the config set
104 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
106 xbt_dict_t dict = (xbt_dict_t) cfg;
107 xbt_dict_cursor_t cursor = NULL;
108 xbt_cfgelm_t variable = NULL;
117 printf("%s>> Dumping of the config set '%s':\n", indent, name);
119 xbt_dict_foreach(dict, cursor, key, variable) {
120 printf("%s %s:", indent, key);
122 size = xbt_dynar_length(variable->content);
123 printf ("%s. Actual size=%d. postset=%p, List of values:\n",
124 xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
126 switch (variable->type) {
128 for (i = 0; i < size; i++) {
129 ival = xbt_dynar_get_as(variable->content, i, int);
130 printf("%s %d\n", indent, ival);
133 case xbt_cfgelm_double:
134 for (i = 0; i < size; i++) {
135 dval = xbt_dynar_get_as(variable->content, i, double);
136 printf("%s %f\n", indent, dval);
139 case xbt_cfgelm_string:
140 for (i = 0; i < size; i++) {
141 sval = xbt_dynar_get_as(variable->content, i, char *);
142 printf("%s %s\n", indent, sval);
145 case xbt_cfgelm_boolean:
146 for (i = 0; i < size; i++) {
147 ival = xbt_dynar_get_as(variable->content, i, int);
148 printf("%s %d\n", indent, ival);
151 case xbt_cfgelm_alias:
155 printf("%s Invalid type!!\n", indent);
161 printf("%s<< End of the config set '%s'\n", indent, name);
164 xbt_dict_cursor_free(&cursor);
168 * free an config element
170 void xbt_cfgelm_free(void *data)
172 xbt_cfgelm_t c = (xbt_cfgelm_t) data;
174 XBT_DEBUG("Frees cfgelm %p", c);
178 if (c->type != xbt_cfgelm_alias)
179 xbt_dynar_free(&(c->content));
183 /*----[ Registering stuff ]-----------------------------------------------*/
184 /** @brief Register an element within a config set
186 * @param cfg the config set
187 * @param name the name of the config element
188 * @param desc a description for this item (used by xbt_cfg_help())
189 * @param type the type of the config element
190 * @param cb_set callback function called when a value is set
192 static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
195 *cfg = xbt_cfg_new();
196 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
197 "type of %s not valid (%d should be between %d and %d)",
198 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
200 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
201 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
203 res = xbt_new(s_xbt_cfgelm_t, 1);
204 XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
205 name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
207 res->desc = xbt_strdup(desc);
209 res->cb_set = cb_set;
214 res->content = xbt_dynar_new(sizeof(int), NULL);
216 case xbt_cfgelm_double:
217 res->content = xbt_dynar_new(sizeof(double), NULL);
219 case xbt_cfgelm_string:
220 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
222 case xbt_cfgelm_boolean:
223 res->content = xbt_dynar_new(sizeof(int), NULL);
226 XBT_ERROR("%d is an invalid type code", (int)type);
229 xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
232 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
233 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set);
234 xbt_cfg_setdefault_double(name, default_value);
236 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
237 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set);
238 xbt_cfg_setdefault_int(name, default_value);
240 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
241 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
242 xbt_cfg_setdefault_string(name, default_value);
244 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
245 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
246 xbt_cfg_setdefault_boolean(name, default_value);
249 void xbt_cfg_register_alias(const char *newname, const char *oldname)
251 if (simgrid_config == NULL)
252 simgrid_config = xbt_cfg_new();
254 xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
255 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
257 res = xbt_dict_get_or_null(simgrid_config, newname);
258 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
260 res = xbt_new0(s_xbt_cfgelm_t, 1);
261 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
263 res->desc = bprintf("Deprecated alias for %s",newname);
264 res->type = xbt_cfgelm_alias;
266 res->content = (xbt_dynar_t)newname;
268 xbt_dict_set(simgrid_config, oldname, res, NULL);
272 * @brief Parse a string and register the stuff described.
274 * @param cfg the config set
275 * @param entry a string describing the element to register
277 * The string may consist in several variable descriptions separated by a space.
278 * Each of them must use the following syntax: \<name\>:\<type\>
279 * with type being one of 'string','int','bool' or 'double'.
281 * Note that this does not allow to set the description, so you should prefer the other interface
283 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
285 char *entrycpy = xbt_strdup(entry);
288 e_xbt_cfgelm_type_t type;
289 XBT_DEBUG("Register string '%s'", entry);
291 tok = strchr(entrycpy, ':');
292 xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
295 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
296 xbt_assert(type < xbt_cfgelm_type_count,
297 "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
299 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
301 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
304 /** @brief Displays the declared aliases and their description */
305 void xbt_cfg_aliases(void)
307 xbt_dict_cursor_t dict_cursor;
308 unsigned int dynar_cursor;
309 xbt_cfgelm_t variable;
311 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
313 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
314 xbt_dynar_push(names, &name);
315 xbt_dynar_sort_strings(names);
317 xbt_dynar_foreach(names, dynar_cursor, name) {
318 variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
320 if (variable->type == xbt_cfgelm_alias)
321 printf(" %s: %s\n", name, variable->desc);
325 /** @brief Displays the declared options and their description */
326 void xbt_cfg_help(void)
328 xbt_dict_cursor_t dict_cursor;
329 unsigned int dynar_cursor;
330 xbt_cfgelm_t variable;
332 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
334 xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
335 xbt_dynar_push(names, &name);
336 xbt_dynar_sort_strings(names);
338 xbt_dynar_foreach(names, dynar_cursor, name) {
340 variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
341 if (variable->type == xbt_cfgelm_alias)
344 printf(" %s: %s\n", name, variable->desc);
345 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
346 size = xbt_dynar_length(variable->content);
347 printf("Current value: ");
350 printf(size == 0 ? "n/a\n" : "{ ");
351 for (int i = 0; i < size; i++) {
352 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
354 switch (variable->type) {
356 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
358 case xbt_cfgelm_double:
359 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
361 case xbt_cfgelm_string:
362 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
364 case xbt_cfgelm_boolean: {
365 int b = xbt_dynar_get_as(variable->content, i, int);
366 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
367 if (b == 0 || b == 1)
368 printf("'%s'%s", bs, sep);
370 printf("'%s/%d'%s", bs, b, sep);
374 printf("Invalid type!!%s", sep);
379 xbt_dynar_free(&names);
382 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
384 xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
386 // The user used the old name. Switch to the new one after a short warning
387 while (res && res->type == xbt_cfgelm_alias) {
388 const char* newname = (const char *)res->content;
389 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
390 res = xbt_cfgelm_get(cfg, newname, type);
396 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
399 xbt_assert(type == xbt_cfgelm_any || res->type == type,
400 "You tried to access to the config element %s as an %s, but its type is %s.",
401 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
405 /** @brief Get the type of this variable in that configuration set
407 * @param cfg the config set
408 * @param name the name of the element
410 * @return the type of the given element
412 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
414 xbt_cfgelm_t variable = NULL;
416 variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
418 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
420 XBT_DEBUG("type in variable = %d", (int)variable->type);
421 return variable->type;
424 /*----[ Setting ]---------------------------------------------------------*/
425 /** @brief va_args version of xbt_cfg_set
427 * @param cfg config set to fill
428 * @param name variable name
429 * @param pa variable value
431 * Add some values to the config set.
433 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
438 e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
443 type = xbt_cfg_get_type(cfg, name);
446 if (e.category == not_found_error) {
448 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
454 case xbt_cfgelm_string:
455 str = va_arg(pa, char *);
456 xbt_cfg_set_string(name, str);
460 xbt_cfg_set_int(name, i);
462 case xbt_cfgelm_double:
463 d = va_arg(pa, double);
464 xbt_cfg_set_double(name, d);
466 case xbt_cfgelm_boolean:
467 str = va_arg(pa, char *);
468 xbt_cfg_set_boolean(name, str);
471 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
475 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
477 * @param cfg config set to fill
478 * @param name variable name
479 * @param ... variable value
481 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
486 xbt_cfg_set_vargs(cfg, name, pa);
490 /** @brief Add values parsed from a string into a config set
492 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
493 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
494 * already registered variable, and [value] conforms to the data type under which this variable was registered.
496 * @todo This is a crude manual parser, it should be a proper lexer.
498 void xbt_cfg_set_parse(const char *options)
500 if (!options || !strlen(options)) { /* nothing to do */
503 char *optionlist_cpy = xbt_strdup(options);
505 XBT_DEBUG("List to parse and set:'%s'", options);
506 char *option = optionlist_cpy;
507 while (1) { /* breaks in the code */
511 int len = strlen(name);
512 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
515 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
516 XBT_DEBUG("Take %c.", *option);
519 if (option - name == len) {
520 XBT_DEBUG("Boundary=EOL");
521 option = NULL; /* don't do next iteration */
523 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
524 /* Pass the following blank chars */
526 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
527 /* fprintf(stderr,"Ignore a blank char.\n"); */
530 if (option - name == len - 1)
531 option = NULL; /* don't do next iteration */
533 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
535 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
540 char *val = strchr(name, ':');
541 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
542 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
545 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
546 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
549 xbt_cfg_set_as_string(name,val);
551 free(optionlist_cpy);
555 free(optionlist_cpy);
558 /** @brief Set the value of a variable, using the string representation of that value
560 * @param key name of the variable to modify
561 * @param value string representation of the value to set
563 * @return the first char after the parsed value in val
566 void *xbt_cfg_set_as_string(const char *key, const char *value) {
570 volatile xbt_cfgelm_t variable = NULL;
575 while (variable == NULL) {
576 variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
577 if (variable->type == xbt_cfgelm_alias) {
578 const char *newname = (const char*)variable->content;
579 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
585 if (e.category == not_found_error) {
587 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
592 switch (variable->type) {
593 case xbt_cfgelm_string:
594 xbt_cfg_set_string(key, value); /* throws */
597 i = strtol(value, &ret, 0);
599 xbt_die("Value of option %s not valid. Should be an integer", key);
601 xbt_cfg_set_int(key, i); /* throws */
603 case xbt_cfgelm_double:
604 d = strtod(value, &ret);
606 xbt_die("Value of option %s not valid. Should be a double", key);
608 xbt_cfg_set_double(key, d); /* throws */
610 case xbt_cfgelm_boolean:
611 xbt_cfg_set_boolean(key, value); /* throws */
612 ret = (char *)value + strlen(value);
615 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
621 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
623 * This is useful to change the default value of a variable while allowing
624 * users to override it with command line arguments
626 void xbt_cfg_setdefault_int(const char *name, int val)
628 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
630 if (variable->isdefault){
631 xbt_cfg_set_int(name, val);
632 variable->isdefault = 1;
634 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
637 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
639 * This is useful to change the default value of a variable while allowing
640 * users to override it with command line arguments
642 void xbt_cfg_setdefault_double(const char *name, double val)
644 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
646 if (variable->isdefault) {
647 xbt_cfg_set_double(name, val);
648 variable->isdefault = 1;
650 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
653 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
655 * This is useful to change the default value of a variable while allowing
656 * users to override it with command line arguments
658 void xbt_cfg_setdefault_string(const char *name, const char *val)
660 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
662 if (variable->isdefault){
663 xbt_cfg_set_string(name, val);
664 variable->isdefault = 1;
666 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
669 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
671 * This is useful to change the default value of a variable while allowing
672 * users to override it with command line arguments
674 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
676 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
678 if (variable->isdefault){
679 xbt_cfg_set_boolean(name, val);
680 variable->isdefault = 1;
683 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
686 /** @brief Set or add an integer value to \a name within \a cfg
688 * @param cfg the config set
689 * @param name the name of the variable
690 * @param val the value of the variable
692 void xbt_cfg_set_int(const char *name, int val)
694 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
696 xbt_dynar_set(variable->content, 0, &val);
698 if (variable->cb_set)
699 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
700 variable->isdefault = 0;
703 /** @brief Set or add a double value to \a name within \a cfg
705 * @param cfg the config set
706 * @param name the name of the variable
707 * @param val the doule to set
709 void xbt_cfg_set_double(const char *name, double val)
711 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
713 xbt_dynar_set(variable->content, 0, &val);
715 if (variable->cb_set)
716 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
717 variable->isdefault = 0;
720 /** @brief Set or add a string value to \a name within \a cfg
722 * @param cfg the config set
723 * @param name the name of the variable
724 * @param val the value to be added
727 void xbt_cfg_set_string(const char *name, const char *val)
729 char *newval = xbt_strdup(val);
730 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
732 if (!xbt_dynar_is_empty(variable->content)) {
733 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
737 xbt_dynar_set(variable->content, 0, &newval);
739 if (variable->cb_set)
740 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
741 variable->isdefault = 0;
744 /** @brief Set or add a boolean value to \a name within \a cfg
746 * @param name the name of the variable
747 * @param val the value of the variable
749 void xbt_cfg_set_boolean(const char *name, const char *val)
752 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
754 for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
755 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
759 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
764 if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
765 xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
768 xbt_dynar_set(variable->content, 0, &bval);
770 if (variable->cb_set)
771 variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
772 variable->isdefault = 0;
776 /* Say if the value is the default value */
777 int xbt_cfg_is_default_value(const char *name)
779 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
780 return variable->isdefault;
783 /*----[ Getting ]---------------------------------------------------------*/
784 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
786 * @param name the name of the variable
788 * Returns the first value from the config set under the given name.
789 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
791 int xbt_cfg_get_int(const char *name)
793 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
795 if (xbt_dynar_length(variable->content) > 1) {
796 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
797 name, xbt_dynar_length(variable->content));
800 return xbt_dynar_get_as(variable->content, 0, int);
803 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
805 * @param cfg the config set
806 * @param name the name of the variable
808 * Returns the first value from the config set under the given name.
809 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
811 double xbt_cfg_get_double(const char *name)
813 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
815 if (xbt_dynar_length(variable->content) > 1) {
816 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
817 name, xbt_dynar_length(variable->content));
820 return xbt_dynar_get_as(variable->content, 0, double);
823 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
825 * @param cfg the config set
826 * @param name the name of the variable
828 * Returns the first value from the config set under the given name.
829 * If there is more than one value, it will issue a warning. Consider using
830 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
832 * \warning the returned value is the actual content of the config set
834 char *xbt_cfg_get_string(const char *name)
836 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
838 if (xbt_dynar_length(variable->content) > 1) {
839 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
840 name, xbt_dynar_length(variable->content));
841 } else if (xbt_dynar_is_empty(variable->content)) {
845 return xbt_dynar_get_as(variable->content, 0, char *);
848 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
850 * @param cfg the config set
851 * @param name the name of the variable
853 * Returns the first value from the config set under the given name.
854 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
856 int xbt_cfg_get_boolean(const char *name)
858 xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
860 if (xbt_dynar_length(variable->content) > 1) {
861 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
862 name, xbt_dynar_length(variable->content));
865 return xbt_dynar_get_as(variable->content, 0, int);
868 /** @brief Retrieve the dynar of all the values stored in a variable
870 * @param cfg where to search in
871 * @param name what to search for
873 * Get the data stored in the config set.
875 * \warning the returned value is the actual content of the config set
877 xbt_dynar_t xbt_cfg_get_dynar(const char *name)
879 xbt_cfgelm_t variable = NULL;
883 variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
885 if (e.category == not_found_error) {
887 THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
892 return variable->content;
895 /** @brief Retrieve one of the integer value of a variable */
896 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
898 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
899 return xbt_dynar_get_as(variable->content, pos, int);
902 /** @brief Retrieve one of the double value of a variable */
903 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
905 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
906 return xbt_dynar_get_as(variable->content, pos, double);
909 /** @brief Retrieve one of the string value of a variable */
910 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
912 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
913 return xbt_dynar_get_as(variable->content, pos, char *);
916 /** @brief Retrieve one of the boolean value of a variable */
917 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
919 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
920 return xbt_dynar_get_as(variable->content, pos, int);
927 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
929 XBT_TEST_SUITE("config", "Configuration support");
931 static xbt_cfg_t make_set()
933 xbt_cfg_t set = NULL;
935 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
936 xbt_cfg_register_str(&set, "speed:int");
937 xbt_cfg_register_str(&set, "peername:string");
938 xbt_cfg_register_str(&set, "user:string");
941 } /* end_of_make_set */
943 extern xbt_cfg_t simgrid_config;
945 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
947 simgrid_config = make_set();
948 xbt_test_add("Alloc and free a config set");
949 xbt_cfg_set_parse("peername:veloce user:bidule");
950 xbt_cfg_free(&simgrid_config);
953 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
955 simgrid_config = make_set();
956 xbt_test_add("Get a single value");
958 /* get_single_value */
961 xbt_cfg_set_parse("peername:toto:42 speed:42");
962 ival = xbt_cfg_get_int("speed");
964 xbt_test_fail("Speed value = %d, I expected 42", ival);
967 xbt_test_add("Access to a non-existant entry");
972 xbt_cfg_set_parse("color:blue");
974 if (e.category != not_found_error)
975 xbt_test_exception(e);
979 xbt_cfg_free(&simgrid_config);
981 #endif /* SIMGRID_TEST */