Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
config: kill unused functions
[simgrid.git] / src / xbt / config.c
1 /* config - Dictionnary where the type of each variable is provided.            */
2
3 /* This is useful to build named structs, like option or property sets.     */
4
5 /* Copyright (c) 2004-2014. The SimGrid Team.
6  * All rights reserved.                                                     */
7
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. */
10
11 #include "xbt/misc.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/ex.h"
15 #include "xbt/dynar.h"
16 #include "xbt/dict.h"
17
18 #include <stdio.h>
19
20 #include "xbt/config.h"         /* prototypes of this module */
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
23
24 XBT_EXPORT_NO_IMPORT(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);
26
27 /* xbt_cfgelm_t: the typedef corresponding to a config variable. */
28
29 typedef struct {
30   /* Description */
31   char *desc;
32
33   /* Allowed type of the variable */
34   e_xbt_cfgelm_type_t type;
35   unsigned isdefault:1;
36
37   /* Callbacks */
38   xbt_cfg_cb_t cb_set;
39
40   /* actual content (could be an union or something) */
41   xbt_dynar_t content;
42 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
43
44 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
45
46 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
47   { "yes",    "no"},
48   {  "on",   "off"},
49   {"true", "false"},
50   {   "1",     "0"},
51   {  NULL,    NULL}
52 };
53
54 /* Internal stuff used in cache to free a variable */
55 static void xbt_cfgelm_free(void *data);
56
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);
59
60 /*----[ Memory management ]-----------------------------------------------*/
61 /** @brief Constructor
62  *
63  * Initialise a config set
64  */
65 xbt_cfg_t xbt_cfg_new(void)
66 {
67   return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
68 }
69
70 /** \brief Copy an existing configuration set
71  *
72  * @param whereto the config set to be created
73  * @param tocopy the source data
74  *
75  * This only copy the registrations, not the actual content
76  */
77 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
78 {
79   xbt_dict_cursor_t cursor = NULL;
80   xbt_cfgelm_t variable = NULL;
81   char *name = NULL;
82
83   XBT_DEBUG("Copy cfg set %p", tocopy);
84   *whereto = NULL;
85   xbt_assert(tocopy, "cannot copy NULL config");
86
87   xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable)
88     xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->cb_set);
89 }
90
91 /** @brief Destructor */
92 void xbt_cfg_free(xbt_cfg_t * cfg)
93 {
94   XBT_DEBUG("Frees cfg set %p", cfg);
95   xbt_dict_free((xbt_dict_t *) cfg);
96 }
97
98 /** @brief Dump a config set for debuging purpose
99  *
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
103  */
104 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
105 {
106   xbt_dict_t dict = (xbt_dict_t) cfg;
107   xbt_dict_cursor_t cursor = NULL;
108   xbt_cfgelm_t variable = NULL;
109   char *key = NULL;
110   int i;
111   int size;
112   int ival;
113   char *sval;
114   double dval;
115
116   if (name)
117     printf("%s>> Dumping of the config set '%s':\n", indent, name);
118
119   xbt_dict_foreach(dict, cursor, key, variable) {
120     printf("%s  %s:", indent, key);
121
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);
125
126     switch (variable->type) {
127     case xbt_cfgelm_int:
128       for (i = 0; i < size; i++) {
129         ival = xbt_dynar_get_as(variable->content, i, int);
130         printf("%s    %d\n", indent, ival);
131       }
132       break;
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);
137       }
138       break;
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);
143       }
144       break;
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);
149       }
150       break;
151     case xbt_cfgelm_alias:
152       /* no content */
153       break;
154     default:
155       printf("%s    Invalid type!!\n", indent);
156       break;
157     }
158   }
159
160   if (name)
161     printf("%s<< End of the config set '%s'\n", indent, name);
162   fflush(stdout);
163
164   xbt_dict_cursor_free(&cursor);
165 }
166
167 /*
168  * free an config element
169  */
170 void xbt_cfgelm_free(void *data)
171 {
172   xbt_cfgelm_t c = (xbt_cfgelm_t) data;
173
174   XBT_DEBUG("Frees cfgelm %p", c);
175   if (!c)
176     return;
177   xbt_free(c->desc);
178   if (c->type != xbt_cfgelm_alias)
179     xbt_dynar_free(&(c->content));
180   free(c);
181 }
182
183 /*----[ Registering stuff ]-----------------------------------------------*/
184 /** @brief Register an element within a config set
185  *
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
191  */
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)
193 {
194   if (*cfg == NULL)
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);
199
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);
202
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);
206
207   res->desc = xbt_strdup(desc);
208   res->type = type;
209   res->cb_set = cb_set;
210   res->isdefault = 1;
211
212   switch (type) {
213   case xbt_cfgelm_int:
214     res->content = xbt_dynar_new(sizeof(int), NULL);
215     break;
216   case xbt_cfgelm_double:
217     res->content = xbt_dynar_new(sizeof(double), NULL);
218     break;
219   case xbt_cfgelm_string:
220     res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
221     break;
222   case xbt_cfgelm_boolean:
223     res->content = xbt_dynar_new(sizeof(int), NULL);
224     break;
225   default:
226     XBT_ERROR("%d is an invalid type code", (int)type);
227     break;
228   }
229   xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
230 }
231
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);
235 }
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);
239 }
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);
243 }
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);
247 }
248
249 void xbt_cfg_register_alias(const char *newname, const char *oldname)
250 {
251   if (simgrid_config == NULL)
252     simgrid_config = xbt_cfg_new();
253
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);
256
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);
259
260   res = xbt_new0(s_xbt_cfgelm_t, 1);
261   XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
262
263   res->desc = bprintf("Deprecated alias for %s",newname);
264   res->type = xbt_cfgelm_alias;
265   res->isdefault = 1;
266   res->content = (xbt_dynar_t)newname;
267
268   xbt_dict_set(simgrid_config, oldname, res, NULL);
269 }
270
271 /**
272  * @brief Parse a string and register the stuff described.
273  *
274  * @param cfg the config set
275  * @param entry a string describing the element to register
276  *
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'.
280  *
281  * Note that this does not allow to set the description, so you should prefer the other interface
282  */
283 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
284 {
285   char *entrycpy = xbt_strdup(entry);
286   char *tok;
287
288   e_xbt_cfgelm_type_t type;
289   XBT_DEBUG("Register string '%s'", entry);
290
291   tok = strchr(entrycpy, ':');
292   xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
293   *(tok++) = '\0';
294
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);
298
299   xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
300
301   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
302 }
303
304 /** @brief Displays the declared aliases and their description */
305 void xbt_cfg_aliases(void)
306 {
307   xbt_dict_cursor_t dict_cursor;
308   unsigned int dynar_cursor;
309   xbt_cfgelm_t variable;
310   char *name;
311   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
312
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);
316
317   xbt_dynar_foreach(names, dynar_cursor, name) {
318     variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
319
320     if (variable->type == xbt_cfgelm_alias)
321       printf("   %s: %s\n", name, variable->desc);
322   }
323 }
324
325 /** @brief Displays the declared options and their description */
326 void xbt_cfg_help(void)
327 {
328   xbt_dict_cursor_t dict_cursor;
329   unsigned int dynar_cursor;
330   xbt_cfgelm_t variable;
331   char *name;
332   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
333
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);
337
338   xbt_dynar_foreach(names, dynar_cursor, name) {
339     int size;
340     variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
341     if (variable->type == xbt_cfgelm_alias)
342       continue;
343
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: ");
348
349     if (size != 1)
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"));
353
354       switch (variable->type) {
355       case xbt_cfgelm_int:
356         printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
357         break;
358       case xbt_cfgelm_double:
359         printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
360         break;
361       case xbt_cfgelm_string:
362         printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
363         break;
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);
369         else
370           printf("'%s/%d'%s", bs, b, sep);
371         break;
372       }
373       default:
374         printf("Invalid type!!%s", sep);
375         break;
376       }
377     }
378   }
379   xbt_dynar_free(&names);
380 }
381
382 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
383 {
384   xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
385
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);
391   }
392
393   if (!res) {
394     xbt_cfg_help();
395     fflush(stdout);
396     THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
397   }
398
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]);
402   return res;
403 }
404
405 /** @brief Get the type of this variable in that configuration set
406  *
407  * @param cfg the config set
408  * @param name the name of the element
409  *
410  * @return the type of the given element
411  */
412 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
413 {
414   xbt_cfgelm_t variable = NULL;
415
416   variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
417   if (!variable)
418     THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
419
420   XBT_DEBUG("type in variable = %d", (int)variable->type);
421   return variable->type;
422 }
423
424 /*----[ Setting ]---------------------------------------------------------*/
425 /**  @brief va_args version of xbt_cfg_set
426  *
427  * @param cfg config set to fill
428  * @param name  variable name
429  * @param pa  variable value
430  *
431  * Add some values to the config set.
432  */
433 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
434 {
435   char *str;
436   int i;
437   double d;
438   e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
439
440   xbt_ex_t e;
441
442   TRY {
443     type = xbt_cfg_get_type(cfg, name);
444   }
445   CATCH(e) {
446     if (e.category == not_found_error) {
447       xbt_ex_free(e);
448       THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
449     }
450     RETHROW;
451   }
452
453   switch (type) {
454   case xbt_cfgelm_string:
455     str = va_arg(pa, char *);
456     xbt_cfg_set_string(name, str);
457     break;
458   case xbt_cfgelm_int:
459     i = va_arg(pa, int);
460     xbt_cfg_set_int(name, i);
461     break;
462   case xbt_cfgelm_double:
463     d = va_arg(pa, double);
464     xbt_cfg_set_double(name, d);
465     break;
466   case xbt_cfgelm_boolean:
467     str = va_arg(pa, char *);
468     xbt_cfg_set_boolean(name, str);
469     break;
470   default:
471     xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
472   }
473 }
474
475 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
476  *
477  * @param cfg config set to fill
478  * @param name variable name
479  * @param ... variable value
480  */
481 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
482 {
483   va_list pa;
484
485   va_start(pa, name);
486   xbt_cfg_set_vargs(cfg, name, pa);
487   va_end(pa);
488 }
489
490 /** @brief Add values parsed from a string into a config set
491  *
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.
495  *
496  * @todo This is a crude manual parser, it should be a proper lexer.
497  */
498 void xbt_cfg_set_parse(const char *options)
499 {
500   if (!options || !strlen(options)) {   /* nothing to do */
501     return;
502   }
503   char *optionlist_cpy = xbt_strdup(options);
504
505   XBT_DEBUG("List to parse and set:'%s'", options);
506   char *option = optionlist_cpy;
507   while (1) {                   /* breaks in the code */
508     if (!option)
509       break;
510     char *name = option;
511     int len = strlen(name);
512     XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
513
514     /* Pass the value */
515     while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
516       XBT_DEBUG("Take %c.", *option);
517       option++;
518     }
519     if (option - name == len) {
520       XBT_DEBUG("Boundary=EOL");
521       option = NULL;            /* don't do next iteration */
522     } else {
523       XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
524       /* Pass the following blank chars */
525       *(option++) = '\0';
526       while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
527         /*      fprintf(stderr,"Ignore a blank char.\n"); */
528         option++;
529       }
530       if (option - name == len - 1)
531         option = NULL;          /* don't do next iteration */
532     }
533     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
534
535     if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
536       continue;
537     if (!strlen(name))
538       break;
539
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 */
543     *(val++) = '\0';
544
545     if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
546       XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
547
548     TRY {
549       xbt_cfg_set_as_string(name,val);
550     } CATCH_ANONYMOUS {
551       free(optionlist_cpy);
552       RETHROW;
553     }
554   }
555   free(optionlist_cpy);
556 }
557
558 /** @brief Set the value of a variable, using the string representation of that value
559  *
560  * @param key name of the variable to modify
561  * @param value string representation of the value to set
562  *
563  * @return the first char after the parsed value in val
564  */
565
566 void *xbt_cfg_set_as_string(const char *key, const char *value) {
567   xbt_ex_t e;
568
569   char *ret;
570   volatile xbt_cfgelm_t variable = NULL;
571   int i;
572   double d;
573
574   TRY {
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);
580         key = newname;
581         variable = NULL;
582       }
583     }
584   } CATCH(e) {
585     if (e.category == not_found_error) {
586       xbt_ex_free(e);
587       THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
588     }
589     RETHROW;
590   }
591
592   switch (variable->type) {
593   case xbt_cfgelm_string:
594     xbt_cfg_set_string(key, value);     /* throws */
595     break;
596   case xbt_cfgelm_int:
597     i = strtol(value, &ret, 0);
598     if (ret == value) {
599       xbt_die("Value of option %s not valid. Should be an integer", key);
600     }
601     xbt_cfg_set_int(key, i);  /* throws */
602     break;
603   case xbt_cfgelm_double:
604     d = strtod(value, &ret);
605     if (ret == value) {
606       xbt_die("Value of option %s not valid. Should be a double", key);
607     }
608     xbt_cfg_set_double(key, d);       /* throws */
609     break;
610   case xbt_cfgelm_boolean:
611     xbt_cfg_set_boolean(key, value);  /* throws */
612     ret = (char *)value + strlen(value);
613     break;
614   default:
615     THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
616     break;
617   }
618   return ret;
619 }
620
621 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
622  *
623  * This is useful to change the default value of a variable while allowing
624  * users to override it with command line arguments
625  */
626 void xbt_cfg_setdefault_int(const char *name, int val)
627 {
628   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
629
630   if (variable->isdefault){
631     xbt_cfg_set_int(name, val);
632     variable->isdefault = 1;
633   } else
634     XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
635 }
636
637 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
638  *
639  * This is useful to change the default value of a variable while allowing
640  * users to override it with command line arguments
641  */
642 void xbt_cfg_setdefault_double(const char *name, double val)
643 {
644   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
645
646   if (variable->isdefault) {
647     xbt_cfg_set_double(name, val);
648     variable->isdefault = 1;
649   } else
650     XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
651 }
652
653 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
654  *
655  * This is useful to change the default value of a variable while allowing
656  * users to override it with command line arguments
657  */
658 void xbt_cfg_setdefault_string(const char *name, const char *val)
659 {
660   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
661
662   if (variable->isdefault){
663     xbt_cfg_set_string(name, val);
664     variable->isdefault = 1;
665   } else
666     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
667 }
668
669 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
670  *
671  * This is useful to change the default value of a variable while allowing
672  * users to override it with command line arguments
673  */
674 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
675 {
676   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
677
678   if (variable->isdefault){
679     xbt_cfg_set_boolean(name, val);
680     variable->isdefault = 1;
681   }
682    else
683     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
684 }
685
686 /** @brief Set or add an integer value to \a name within \a cfg
687  *
688  * @param cfg the config set
689  * @param name the name of the variable
690  * @param val the value of the variable
691  */
692 void xbt_cfg_set_int(const char *name, int val)
693 {
694   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
695
696   xbt_dynar_set(variable->content, 0, &val);
697
698   if (variable->cb_set)
699     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
700   variable->isdefault = 0;
701 }
702
703 /** @brief Set or add a double value to \a name within \a cfg
704  *
705  * @param cfg the config set
706  * @param name the name of the variable
707  * @param val the doule to set
708  */
709 void xbt_cfg_set_double(const char *name, double val)
710 {
711   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
712
713   xbt_dynar_set(variable->content, 0, &val);
714
715   if (variable->cb_set)
716     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
717   variable->isdefault = 0;
718 }
719
720 /** @brief Set or add a string value to \a name within \a cfg
721  *
722  * @param cfg the config set
723  * @param name the name of the variable
724  * @param val the value to be added
725  *
726  */
727 void xbt_cfg_set_string(const char *name, const char *val)
728 {
729   char *newval = xbt_strdup(val);
730   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
731
732   if (!xbt_dynar_is_empty(variable->content)) {
733     char *sval = xbt_dynar_get_as(variable->content, 0, char *);
734     free(sval);
735   }
736
737   xbt_dynar_set(variable->content, 0, &newval);
738
739   if (variable->cb_set)
740     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
741   variable->isdefault = 0;
742 }
743
744 /** @brief Set or add a boolean value to \a name within \a cfg
745  *
746  * @param name the name of the variable
747  * @param val the value of the variable
748  */
749 void xbt_cfg_set_boolean(const char *name, const char *val)
750 {
751   int i, bval;
752   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
753
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){
756       bval = 1;
757       break;
758     }
759     if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
760       bval = 0;
761       break;
762     }
763   }
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);
766   }
767
768   xbt_dynar_set(variable->content, 0, &bval);
769
770   if (variable->cb_set)
771     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
772   variable->isdefault = 0;
773 }
774
775
776 /* Say if the value is the default value */
777 int xbt_cfg_is_default_value(const char *name)
778 {
779   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
780   return variable->isdefault;
781 }
782
783 /*----[ Getting ]---------------------------------------------------------*/
784 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
785  *
786  * @param name the name of the variable
787  *
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.
790  */
791 int xbt_cfg_get_int(const char *name)
792 {
793   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
794
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));
798   }
799
800   return xbt_dynar_get_as(variable->content, 0, int);
801 }
802
803 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
804  *
805  * @param cfg the config set
806  * @param name the name of the variable
807  *
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.
810  */
811 double xbt_cfg_get_double(const char *name)
812 {
813   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
814
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));
818   }
819
820   return xbt_dynar_get_as(variable->content, 0, double);
821 }
822
823 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
824  *
825  * @param cfg the config set
826  * @param name the name of the variable
827  *
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.
831  *
832  * \warning the returned value is the actual content of the config set
833  */
834 char *xbt_cfg_get_string(const char *name)
835 {
836   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
837
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)) {
842     return NULL;
843   }
844
845   return xbt_dynar_get_as(variable->content, 0, char *);
846 }
847
848 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
849  *
850  * @param cfg the config set
851  * @param name the name of the variable
852  *
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.
855  */
856 int xbt_cfg_get_boolean(const char *name)
857 {
858   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
859
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));
863   }
864
865   return xbt_dynar_get_as(variable->content, 0, int);
866 }
867
868 #ifdef SIMGRID_TEST
869 #include "xbt.h"
870 #include "xbt/ex.h"
871
872 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
873
874 XBT_TEST_SUITE("config", "Configuration support");
875
876 static xbt_cfg_t make_set()
877 {
878   xbt_cfg_t set = NULL;
879
880   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
881   xbt_cfg_register_str(&set, "speed:int");
882   xbt_cfg_register_str(&set, "peername:string");
883   xbt_cfg_register_str(&set, "user:string");
884
885   return set;
886 }                               /* end_of_make_set */
887
888 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
889
890 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
891 {
892   simgrid_config = make_set();
893   xbt_test_add("Alloc and free a config set");
894   xbt_cfg_set_parse("peername:veloce user:bidule");
895   xbt_cfg_free(&simgrid_config);
896 }
897
898 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
899 {
900   simgrid_config = make_set();
901   xbt_test_add("Get a single value");
902   {
903     /* get_single_value */
904     int ival;
905
906     xbt_cfg_set_parse("peername:toto:42 speed:42");
907     ival = xbt_cfg_get_int("speed");
908     if (ival != 42)
909       xbt_test_fail("Speed value = %d, I expected 42", ival);
910   }
911
912   xbt_test_add("Access to a non-existant entry");
913   {
914     xbt_ex_t e;
915
916     TRY {
917       xbt_cfg_set_parse("color:blue");
918     } CATCH(e) {
919       if (e.category != not_found_error)
920         xbt_test_exception(e);
921       xbt_ex_free(e);
922     }
923   }
924   xbt_cfg_free(&simgrid_config);
925 }
926 #endif                          /* SIMGRID_TEST */