Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further rework the config handling. Still much to do
[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_cfg_t simgrid_config = NULL;
25
26 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27
28    Both data and DTD are mixed, but fixing it now would prevent me to ever defend my thesis. */
29
30 typedef struct {
31   /* Description */
32   char *desc;
33
34   /* Allowed type of the variable */
35   e_xbt_cfgelm_type_t type;
36   int min, max;
37   unsigned isdefault:1;
38
39   /* Callbacks */
40   xbt_cfg_cb_t cb_set;
41
42   /* actual content (could be an union or something) */
43   xbt_dynar_t content;
44 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
45
46 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
47
48 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
49   { "yes",    "no"},
50   {  "on",   "off"},
51   {"true", "false"},
52   {   "1",     "0"},
53   {  NULL,    NULL}
54 };
55
56 /* Internal stuff used in cache to free a variable */
57 static void xbt_cfgelm_free(void *data);
58
59 /* Retrieve the variable we'll modify */
60 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
61
62 /*----[ Memory management ]-----------------------------------------------*/
63 /** @brief Constructor
64  *
65  * Initialise a config set
66  */
67 xbt_cfg_t xbt_cfg_new(void)
68 {
69   return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
70 }
71
72 /** \brief Copy an existing configuration set
73  *
74  * @param whereto the config set to be created
75  * @param tocopy the source data
76  *
77  * This only copy the registrations, not the actual content
78  */
79 void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
80 {
81   xbt_dict_cursor_t cursor = NULL;
82   xbt_cfgelm_t variable = NULL;
83   char *name = NULL;
84
85   XBT_DEBUG("Copy cfg set %p", tocopy);
86   *whereto = NULL;
87   xbt_assert(tocopy, "cannot copy NULL config");
88
89   xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
90     xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->max, variable->cb_set);
91   }
92 }
93
94 /** @brief Destructor */
95 void xbt_cfg_free(xbt_cfg_t * cfg)
96 {
97   XBT_DEBUG("Frees cfg set %p", cfg);
98   xbt_dict_free((xbt_dict_t *) cfg);
99 }
100
101 /** @brief Dump a config set for debuging purpose
102  *
103  * @param name The name to give to this config set
104  * @param indent what to write at the beginning of each line (right number of spaces)
105  * @param cfg the config set
106  */
107 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
108 {
109   xbt_dict_t dict = (xbt_dict_t) cfg;
110   xbt_dict_cursor_t cursor = NULL;
111   xbt_cfgelm_t variable = NULL;
112   char *key = NULL;
113   int i;
114   int size;
115   int ival;
116   char *sval;
117   double dval;
118
119   if (name)
120     printf("%s>> Dumping of the config set '%s':\n", indent, name);
121
122   xbt_dict_foreach(dict, cursor, key, variable) {
123     printf("%s  %s:", indent, key);
124
125     size = xbt_dynar_length(variable->content);
126     printf ("%d_to_%d_%s. Actual size=%d. postset=%p, List of values:\n",
127             variable->min, variable->max, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
128
129     switch (variable->type) {
130     case xbt_cfgelm_int:
131       for (i = 0; i < size; i++) {
132         ival = xbt_dynar_get_as(variable->content, i, int);
133         printf("%s    %d\n", indent, ival);
134       }
135       break;
136     case xbt_cfgelm_double:
137       for (i = 0; i < size; i++) {
138         dval = xbt_dynar_get_as(variable->content, i, double);
139         printf("%s    %f\n", indent, dval);
140       }
141       break;
142     case xbt_cfgelm_string:
143       for (i = 0; i < size; i++) {
144         sval = xbt_dynar_get_as(variable->content, i, char *);
145         printf("%s    %s\n", indent, sval);
146       }
147       break;
148     case xbt_cfgelm_boolean:
149       for (i = 0; i < size; i++) {
150         ival = xbt_dynar_get_as(variable->content, i, int);
151         printf("%s    %d\n", indent, ival);
152       }
153       break;
154     case xbt_cfgelm_alias:
155       /* no content */
156       break;
157     default:
158       printf("%s    Invalid type!!\n", indent);
159       break;
160     }
161   }
162
163   if (name)
164     printf("%s<< End of the config set '%s'\n", indent, name);
165   fflush(stdout);
166
167   xbt_dict_cursor_free(&cursor);
168 }
169
170 /*
171  * free an config element
172  */
173 void xbt_cfgelm_free(void *data)
174 {
175   xbt_cfgelm_t c = (xbt_cfgelm_t) data;
176
177   XBT_DEBUG("Frees cfgelm %p", c);
178   if (!c)
179     return;
180   xbt_free(c->desc);
181   if (c->type != xbt_cfgelm_alias)
182     xbt_dynar_free(&(c->content));
183   free(c);
184 }
185
186 /*----[ Registering stuff ]-----------------------------------------------*/
187 /** @brief Register an element within a config set
188  *
189  *  @param cfg the config set
190  *  @param name the name of the config element
191  *  @param desc a description for this item (used by xbt_cfg_help())
192  *  @param type the type of the config element
193  *  @param min the minimum number of values for this config element
194  *  @param max the maximum number of values for this config element
195  *  @param cb_set callback function called when a value is set
196  */
197 void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min,
198                       int max, xbt_cfg_cb_t cb_set)
199 {
200   xbt_cfgelm_t res;
201
202   if (*cfg == NULL)
203     *cfg = xbt_cfg_new();
204   xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
205               "type of %s not valid (%d should be between %d and %d)",
206              name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
207   res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
208   xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
209
210   res = xbt_new(s_xbt_cfgelm_t, 1);
211   XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
212             name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
213
214   res->desc = xbt_strdup(desc);
215   res->type = type;
216   res->min = min;
217   res->max = max;
218   res->cb_set = cb_set;
219   res->isdefault = 1;
220
221   switch (type) {
222   case xbt_cfgelm_int:
223     res->content = xbt_dynar_new(sizeof(int), NULL);
224     break;
225   case xbt_cfgelm_double:
226     res->content = xbt_dynar_new(sizeof(double), NULL);
227     break;
228   case xbt_cfgelm_string:
229     res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
230     break;
231   case xbt_cfgelm_boolean:
232     res->content = xbt_dynar_new(sizeof(int), NULL);
233     break;
234   default:
235     XBT_ERROR("%d is an invalid type code", (int)type);
236     break;
237   }
238   xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
239 }
240
241 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
242   xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_double,1,1,cb_set);
243   xbt_cfg_setdefault_double(name, default_value);
244 }
245 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
246   xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_int,1,1,cb_set);
247   xbt_cfg_setdefault_int(name, default_value);
248 }
249 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
250   xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_string,1,1,cb_set);
251   xbt_cfg_setdefault_string(name, default_value);
252 }
253 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
254   xbt_cfg_register(simgrid_config,name,desc,xbt_cfgelm_boolean,1,1,cb_set);
255   xbt_cfg_setdefault_boolean(name, default_value);
256 }
257
258 void xbt_cfg_register_alias(const char *newname, const char *oldname)
259 {
260   if (simgrid_config == NULL)
261     simgrid_config = xbt_cfg_new();
262
263   xbt_cfgelm_t res = xbt_dict_get_or_null(simgrid_config, oldname);
264   xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
265
266   res = xbt_dict_get_or_null(simgrid_config, newname);
267   xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
268
269   res = xbt_new0(s_xbt_cfgelm_t, 1);
270   XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
271
272   res->desc = bprintf("Deprecated alias for %s",newname);
273   res->type = xbt_cfgelm_alias;
274   res->min = 1;
275   res->max = 1;
276   res->isdefault = 1;
277   res->content = (xbt_dynar_t)newname;
278
279   xbt_dict_set(simgrid_config, oldname, res, NULL);
280 }
281
282 /** @brief Unregister an element from a config set.
283  *
284  *  @param cfg the config set
285  *  @param name the name of the element to be freed
286  *
287  *  Note that it removes both the description and the actual content.
288  *  Throws not_found when no such element exists.
289  */
290 void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
291 {
292   XBT_DEBUG("Unregister elm '%s' from set %p", name, cfg);
293   xbt_dict_remove((xbt_dict_t) cfg, name);
294 }
295
296 /**
297  * @brief Parse a string and register the stuff described.
298  *
299  * @param cfg the config set
300  * @param entry a string describing the element to register
301  *
302  * The string may consist in several variable descriptions separated by a space.
303  * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
304  * with type being one of  'string','int' or 'double'.
305  *
306  * Note that this does not allow to set the description, so you should prefer the other interface
307  */
308 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
309 {
310   char *entrycpy = xbt_strdup(entry);
311   char *tok;
312
313   int min, max;
314   e_xbt_cfgelm_type_t type;
315   XBT_DEBUG("Register string '%s'", entry);
316
317   tok = strchr(entrycpy, ':');
318   xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
319   *(tok++) = '\0';
320
321   min = strtol(tok, &tok, 10);
322   xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
323
324   xbt_assert(strcmp(tok, "_to_"), "Invalid config element descriptor : %s%s",
325               entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
326   tok += strlen("_to_");
327
328   max = strtol(tok, &tok, 10);
329   xbt_assert(tok, "Invalid maximum in config element descriptor %s", entry);
330
331   xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
332               "; Should be <name>:<min nb>_to_<max nb>_<type>");
333   tok++;
334
335   for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
336   xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
337               "; Should be one of 'string', 'int' or 'double'.");
338
339   xbt_cfg_register(cfg, entrycpy, NULL, type, min, max, NULL);
340
341   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
342 }
343
344 /** @brief Displays the declared aliases and their description */
345 void xbt_cfg_aliases(xbt_cfg_t cfg)
346 {
347   xbt_dict_cursor_t dict_cursor;
348   unsigned int dynar_cursor;
349   xbt_cfgelm_t variable;
350   char *name;
351   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
352
353   xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
354     xbt_dynar_push(names, &name);
355   xbt_dynar_sort_strings(names);
356
357   xbt_dynar_foreach(names, dynar_cursor, name) {
358     variable = xbt_dict_get((xbt_dict_t )cfg, name);
359
360     if (variable->type == xbt_cfgelm_alias)
361       printf("   %s: %s\n", name, variable->desc);
362   }
363 }
364
365 /** @brief Displays the declared options and their description */
366 void xbt_cfg_help(xbt_cfg_t cfg)
367 {
368   xbt_dict_cursor_t dict_cursor;
369   unsigned int dynar_cursor;
370   xbt_cfgelm_t variable;
371   char *name;
372   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
373
374   xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
375     xbt_dynar_push(names, &name);
376   xbt_dynar_sort_strings(names);
377
378   xbt_dynar_foreach(names, dynar_cursor, name) {
379     int i;
380     int size;
381     variable = xbt_dict_get((xbt_dict_t )cfg, name);
382     if (variable->type == xbt_cfgelm_alias)
383       continue;
384
385     printf("   %s: %s\n", name, variable->desc);
386     printf("       Type: %s; ", xbt_cfgelm_type_name[variable->type]);
387     if (variable->min != 1 || variable->max != 1) {
388       printf("Arity: min:%d to max:", variable->min);
389       if (variable->max == 0)
390         printf("(no bound); ");
391       else
392         printf("%d; ", variable->max);
393     }
394     size = xbt_dynar_length(variable->content);
395     printf("Current value%s: ", (size <= 1 ? "" : "s"));
396
397     if (size != 1)
398       printf(size == 0 ? "n/a\n" : "{ ");
399     for (i = 0; i < size; i++) {
400       const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
401
402       switch (variable->type) {
403       case xbt_cfgelm_int:
404         printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
405         break;
406       case xbt_cfgelm_double:
407         printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
408         break;
409       case xbt_cfgelm_string:
410         printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
411         break;
412       case xbt_cfgelm_boolean: {
413         int b = xbt_dynar_get_as(variable->content, i, int);
414         const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
415         if (b == 0 || b == 1)
416           printf("'%s'%s", bs, sep);
417         else
418           printf("'%s/%d'%s", bs, b, sep);
419         break;
420       }
421       default:
422         printf("Invalid type!!%s", sep);
423       }
424     }
425   }
426   xbt_dynar_free(&names);
427 }
428
429 /** @brief Check that each variable have the right amount of values */
430 void xbt_cfg_check(xbt_cfg_t cfg)
431 {
432   xbt_dict_cursor_t cursor;
433   xbt_cfgelm_t variable;
434   char *name;
435   int size;
436
437   xbt_assert(cfg, "NULL config set.");
438   XBT_DEBUG("Check cfg set %p", cfg);
439
440   xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
441     if (variable->type == xbt_cfgelm_alias)
442       continue;
443
444     size = xbt_dynar_length(variable->content);
445     if (variable->min > size) {
446       xbt_dict_cursor_free(&cursor);
447       THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
448              name, variable->min, xbt_cfgelm_type_name[variable->type], size);
449     }
450
451     if (variable->isdefault && size > variable->min) {
452       xbt_dict_cursor_free(&cursor);
453       THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
454              name, variable->min, xbt_cfgelm_type_name[variable->type], size);
455     }
456
457     if (variable->max > 0 && variable->max < size) {
458       xbt_dict_cursor_free(&cursor);
459       THROWF(mismatch_error, 0, "Config elem %s accepts at most %d %s, but there is %d values.",
460              name, variable->max, xbt_cfgelm_type_name[variable->type], size);
461     }
462   }
463   xbt_dict_cursor_free(&cursor);
464 }
465
466 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
467 {
468   xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
469
470   // The user used the old name. Switch to the new one after a short warning
471   while (res && res->type == xbt_cfgelm_alias) {
472     const char* newname = (const char *)res->content;
473     XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
474     res = xbt_cfgelm_get(cfg, newname, type);
475   }
476
477   if (!res) {
478     xbt_cfg_help(cfg);
479     fflush(stdout);
480     THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
481   }
482
483   xbt_assert(type == xbt_cfgelm_any || res->type == type,
484               "You tried to access to the config element %s as an %s, but its type is %s.",
485               name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
486   return res;
487 }
488
489 /** @brief Get the type of this variable in that configuration set
490  *
491  * @param cfg the config set
492  * @param name the name of the element
493  *
494  * @return the type of the given element
495  */
496 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
497 {
498   xbt_cfgelm_t variable = NULL;
499
500   variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
501   if (!variable)
502     THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
503
504   XBT_DEBUG("type in variable = %d", (int)variable->type);
505   return variable->type;
506 }
507
508 /*----[ Setting ]---------------------------------------------------------*/
509 /**  @brief va_args version of xbt_cfg_set
510  *
511  * @param cfg config set to fill
512  * @param name  variable name
513  * @param pa  variable value
514  *
515  * Add some values to the config set.
516  */
517 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
518 {
519   char *str;
520   int i;
521   double d;
522   e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
523
524   xbt_ex_t e;
525
526   TRY {
527     type = xbt_cfg_get_type(cfg, name);
528   }
529   CATCH(e) {
530     if (e.category == not_found_error) {
531       xbt_ex_free(e);
532       THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
533     }
534     RETHROW;
535   }
536
537   switch (type) {
538   case xbt_cfgelm_string:
539     str = va_arg(pa, char *);
540     xbt_cfg_set_string(name, str);
541     break;
542   case xbt_cfgelm_int:
543     i = va_arg(pa, int);
544     xbt_cfg_set_int(name, i);
545     break;
546   case xbt_cfgelm_double:
547     d = va_arg(pa, double);
548     xbt_cfg_set_double(name, d);
549     break;
550   case xbt_cfgelm_boolean:
551     str = va_arg(pa, char *);
552     xbt_cfg_set_boolean(name, str);
553     break;
554   default:
555     xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
556   }
557 }
558
559 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
560  *
561  * @param cfg config set to fill
562  * @param name variable name
563  * @param ... variable value
564  */
565 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
566 {
567   va_list pa;
568
569   va_start(pa, name);
570   xbt_cfg_set_vargs(cfg, name, pa);
571   va_end(pa);
572 }
573
574 /** @brief Add values parsed from a string into a config set
575  *
576  * @param cfg config set to fill
577  * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
578  * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
579  * already registered variable, and [value] conforms to the data type under which this variable was registered.
580  *
581  * @todo This is a crude manual parser, it should be a proper lexer.
582  */
583 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
584 {
585   if (!options || !strlen(options)) {   /* nothing to do */
586     return;
587   }
588   char *optionlist_cpy = xbt_strdup(options);
589
590   XBT_DEBUG("List to parse and set:'%s'", options);
591   char *option = optionlist_cpy;
592   while (1) {                   /* breaks in the code */
593     if (!option)
594       break;
595     char *name = option;
596     int len = strlen(name);
597     XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
598
599     /* Pass the value */
600     while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
601       XBT_DEBUG("Take %c.", *option);
602       option++;
603     }
604     if (option - name == len) {
605       XBT_DEBUG("Boundary=EOL");
606       option = NULL;            /* don't do next iteration */
607     } else {
608       XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
609       /* Pass the following blank chars */
610       *(option++) = '\0';
611       while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
612         /*      fprintf(stderr,"Ignore a blank char.\n"); */
613         option++;
614       }
615       if (option - name == len - 1)
616         option = NULL;          /* don't do next iteration */
617     }
618     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
619
620     if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
621       continue;
622     if (!strlen(name))
623       break;
624
625     char *val = strchr(name, ':');
626     xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
627     /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
628     *(val++) = '\0';
629
630     if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
631       XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
632
633     TRY {
634       xbt_cfg_set_as_string(name,val);
635     } CATCH_ANONYMOUS {
636       free(optionlist_cpy);
637       RETHROW;
638     }
639   }
640   free(optionlist_cpy);
641 }
642
643 /** @brief Set the value of a variable, using the string representation of that value
644  *
645  * @param key name of the variable to modify
646  * @param value string representation of the value to set
647  *
648  * @return the first char after the parsed value in val
649  */
650
651 void *xbt_cfg_set_as_string(const char *key, const char *value) {
652   xbt_ex_t e;
653
654   char *ret;
655   volatile xbt_cfgelm_t variable = NULL;
656   int i;
657   double d;
658
659   TRY {
660     while (variable == NULL) {
661       variable = xbt_dict_get((xbt_dict_t) simgrid_config, key);
662       if (variable->type == xbt_cfgelm_alias) {
663         const char *newname = (const char*)variable->content;
664         XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
665         key = newname;
666         variable = NULL;
667       }
668     }
669   } CATCH(e) {
670     if (e.category == not_found_error) {
671       xbt_ex_free(e);
672       THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
673     }
674     RETHROW;
675   }
676
677   switch (variable->type) {
678   case xbt_cfgelm_string:
679     xbt_cfg_set_string(key, value);     /* throws */
680     break;
681   case xbt_cfgelm_int:
682     i = strtol(value, &ret, 0);
683     if (ret == value) {
684       xbt_die("Value of option %s not valid. Should be an integer", key);
685     }
686     xbt_cfg_set_int(key, i);  /* throws */
687     break;
688   case xbt_cfgelm_double:
689     d = strtod(value, &ret);
690     if (ret == value) {
691       xbt_die("Value of option %s not valid. Should be a double", key);
692     }
693     xbt_cfg_set_double(key, d);       /* throws */
694     break;
695   case xbt_cfgelm_boolean:
696     xbt_cfg_set_boolean(key, value);  /* throws */
697     ret = (char *)value + strlen(value);
698     break;
699   default:
700     THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
701     break;
702   }
703   return ret;
704 }
705
706 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
707  *
708  * This is useful to change the default value of a variable while allowing
709  * users to override it with command line arguments
710  */
711 void xbt_cfg_setdefault_int(const char *name, int val)
712 {
713   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
714
715   if (variable->isdefault){
716     xbt_cfg_set_int(name, val);
717     variable->isdefault = 1;
718   } else
719     XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
720 }
721
722 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
723  *
724  * This is useful to change the default value of a variable while allowing
725  * users to override it with command line arguments
726  */
727 void xbt_cfg_setdefault_double(const char *name, double val)
728 {
729   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
730
731   if (variable->isdefault) {
732     xbt_cfg_set_double(name, val);
733     variable->isdefault = 1;
734   } else
735     XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
736 }
737
738 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
739  *
740  * This is useful to change the default value of a variable while allowing
741  * users to override it with command line arguments
742  */
743 void xbt_cfg_setdefault_string(const char *name, const char *val)
744 {
745   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
746
747   if (variable->isdefault){
748     xbt_cfg_set_string(name, val);
749     variable->isdefault = 1;
750   } else
751     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
752 }
753
754 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
755  *
756  * This is useful to change the default value of a variable while allowing
757  * users to override it with command line arguments
758  */
759 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
760 {
761   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
762
763   if (variable->isdefault){
764     xbt_cfg_set_boolean(name, val);
765     variable->isdefault = 1;
766   }
767    else
768     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
769 }
770
771 /** @brief Set or add an integer value to \a name within \a cfg
772  *
773  * @param cfg the config set
774  * @param name the name of the variable
775  * @param val the value of the variable
776  */
777 void xbt_cfg_set_int(const char *name, int val)
778 {
779   XBT_VERB("Configuration setting: %s=%d", name, val);
780   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
781
782   if (variable->max == 1) {
783     xbt_dynar_set(variable->content, 0, &val);
784   } else {
785     if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
786       THROWF(mismatch_error, 0, "Cannot add value %d to the config element %s since it's already full (size=%d)",
787              val, name, variable->max);
788
789     xbt_dynar_push(variable->content, &val);
790   }
791
792   if (variable->cb_set)
793     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
794   variable->isdefault = 0;
795 }
796
797 /** @brief Set or add a double value to \a name within \a cfg
798  *
799  * @param cfg the config set
800  * @param name the name of the variable
801  * @param val the doule to set
802  */
803 void xbt_cfg_set_double(const char *name, double val)
804 {
805   XBT_VERB("Configuration setting: %s=%f", name, val);
806   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
807
808   if (variable->max == 1) {
809     xbt_dynar_set(variable->content, 0, &val);
810   } else {
811     if (variable->max && xbt_dynar_length(variable->content) == variable->max)
812       THROWF(mismatch_error, 0, "Cannot add value %f to the config element %s since it's already full (size=%d)",
813              val, name, variable->max);
814
815     xbt_dynar_push(variable->content, &val);
816   }
817
818   if (variable->cb_set)
819     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
820   variable->isdefault = 0;
821 }
822
823 /** @brief Set or add a string value to \a name within \a cfg
824  *
825  * @param cfg the config set
826  * @param name the name of the variable
827  * @param val the value to be added
828  *
829  */
830 void xbt_cfg_set_string(const char *name, const char *val)
831 {
832   char *newval = xbt_strdup(val);
833
834   XBT_VERB("Configuration setting: %s=%s", name, val);
835   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
836
837   XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
838          variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
839
840   if (variable->max == 1) {
841     if (!xbt_dynar_is_empty(variable->content)) {
842       char *sval = xbt_dynar_get_as(variable->content, 0, char *);
843       free(sval);
844     }
845
846     xbt_dynar_set(variable->content, 0, &newval);
847   } else {
848     if (variable->max
849         && xbt_dynar_length(variable->content) == variable->max)
850       THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
851              name, val, variable->max);
852
853     xbt_dynar_push(variable->content, &newval);
854   }
855
856   if (variable->cb_set)
857     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
858   variable->isdefault = 0;
859 }
860
861 /** @brief Set or add a boolean value to \a name within \a cfg
862  *
863  * @param cfg the config set
864  * @param name the name of the variable
865  * @param val the value of the variable
866  */
867 void xbt_cfg_set_boolean(const char *name, const char *val)
868 {
869   int i, bval;
870
871   XBT_VERB("Configuration setting: %s=%s", name, val);
872   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
873
874   for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
875     if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
876       bval = 1;
877       break;
878     }
879     if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
880       bval = 0;
881       break;
882     }
883   }
884   if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
885     xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
886   }
887
888   if (variable->max == 1) {
889     xbt_dynar_set(variable->content, 0, &bval);
890   } else {
891     if (variable->max && xbt_dynar_length(variable->content) == (unsigned long) variable->max)
892       THROWF(mismatch_error, 0, "Cannot add value %s to the config element %s since it's already full (size=%d)",
893              val, name, variable->max);
894
895     xbt_dynar_push(variable->content, &bval);
896   }
897
898   if (variable->cb_set)
899     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
900   variable->isdefault = 0;
901 }
902
903 /* ---- [ Removing ] ---- */
904 /** @brief Remove the provided \e val integer value from a variable
905  *
906  * @param cfg the config set
907  * @param name the name of the variable
908  * @param val the value to be removed
909  */
910 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
911 {
912   unsigned int cpt;
913   int seen;
914
915   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
916
917   if (xbt_dynar_length(variable->content) == variable->min)
918     THROWF(mismatch_error, 0,
919            "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
920            val, name, variable->min);
921
922   xbt_dynar_foreach(variable->content, cpt, seen) {
923     if (seen == val) {
924       xbt_dynar_cursor_rm(variable->content, &cpt);
925       return;
926     }
927   }
928   THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
929 }
930
931 /** @brief Remove the provided \e val double value from a variable
932  *
933  * @param cfg the config set
934  * @param name the name of the variable
935  * @param val the value to be removed
936  */
937 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
938 {
939   unsigned int cpt;
940   double seen;
941
942   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
943
944   if (xbt_dynar_length(variable->content) == variable->min)
945     THROWF(mismatch_error, 0,
946            "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
947            val, name, variable->min);
948
949   xbt_dynar_foreach(variable->content, cpt, seen) {
950     if (seen == val) {
951       xbt_dynar_cursor_rm(variable->content, &cpt);
952       return;
953     }
954   }
955
956   THROWF(not_found_error, 0,"Can't remove the value %f of config element %s: value not found.", val, name);
957 }
958
959 /** @brief Remove the provided \e val string value from a variable
960  *
961  * @param cfg the config set
962  * @param name the name of the variable
963  * @param val the value of the string which will be removed
964  */
965 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
966 {
967   unsigned int cpt;
968   char *seen;
969   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
970
971   if (xbt_dynar_length(variable->content) == variable->min)
972     THROWF(mismatch_error, 0,
973            "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
974            name, val, variable->min);
975
976   xbt_dynar_foreach(variable->content, cpt, seen) {
977     if (!strcpy(seen, val)) {
978       xbt_dynar_cursor_rm(variable->content, &cpt);
979       return;
980     }
981   }
982
983   THROWF(not_found_error, 0, "Can't remove the value %s of config element %s: value not found.", val, name);
984 }
985
986 /** @brief Remove the provided \e val boolean value from a variable
987  *
988  * @param cfg the config set
989  * @param name the name of the variable
990  * @param val the value to be removed
991  */
992 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
993 {
994   unsigned int cpt;
995   int seen;
996   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
997
998   if (xbt_dynar_length(variable->content) == variable->min)
999     THROWF(mismatch_error, 0,
1000            "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1001            val, name, variable->min);
1002
1003   xbt_dynar_foreach(variable->content, cpt, seen) {
1004     if (seen == val) {
1005       xbt_dynar_cursor_rm(variable->content, &cpt);
1006       return;
1007     }
1008   }
1009
1010   THROWF(not_found_error, 0, "Can't remove the value %d of config element %s: value not found.", val, name);
1011 }
1012
1013 /** @brief Remove the \e pos th value from the provided variable */
1014 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1015 {
1016   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1017
1018   if (xbt_dynar_length(variable->content) == variable->min)
1019     THROWF(mismatch_error, 0,
1020            "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1021            pos, name, variable->min);
1022
1023   xbt_dynar_remove_at(variable->content, pos, NULL);
1024 }
1025
1026 /** @brief Remove all the values from a variable
1027  *
1028  * @param cfg the config set
1029  * @param name the name of the variable
1030  */
1031 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1032 {
1033   xbt_cfgelm_t variable = NULL;
1034   xbt_ex_t e;
1035
1036   TRY {
1037     variable = xbt_dict_get((xbt_dict_t) cfg, name);
1038   } CATCH(e) {
1039     if (e.category != not_found_error)
1040       RETHROW;
1041
1042     xbt_ex_free(e);
1043     THROWF(not_found_error, 0, "Can't empty  '%s' since this option does not exist", name);
1044   }
1045
1046   if (variable)
1047     xbt_dynar_reset(variable->content);
1048 }
1049
1050 /* Say if the value is the default value */
1051 int xbt_cfg_is_default_value(const char *name)
1052 {
1053   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
1054   return variable->isdefault;
1055 }
1056
1057 /*----[ Getting ]---------------------------------------------------------*/
1058 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1059  *
1060  * @param name the name of the variable
1061  *
1062  * Returns the first value from the config set under the given name.
1063  * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1064  */
1065 int xbt_cfg_get_int(const char *name)
1066 {
1067   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
1068
1069   if (xbt_dynar_length(variable->content) > 1) {
1070     XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1071          name, xbt_dynar_length(variable->content));
1072   }
1073
1074   return xbt_dynar_get_as(variable->content, 0, int);
1075 }
1076
1077 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1078  *
1079  * @param cfg the config set
1080  * @param name the name of the variable
1081  *
1082  * Returns the first value from the config set under the given name.
1083  * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1084  */
1085 double xbt_cfg_get_double(const char *name)
1086 {
1087   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
1088
1089   if (xbt_dynar_length(variable->content) > 1) {
1090     XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
1091          name, xbt_dynar_length(variable->content));
1092   }
1093
1094   return xbt_dynar_get_as(variable->content, 0, double);
1095 }
1096
1097 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1098  *
1099  * @param cfg the config set
1100  * @param name the name of the variable
1101  *
1102  * Returns the first value from the config set under the given name.
1103  * If there is more than one value, it will issue a warning. Consider using
1104  * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1105  *
1106  * \warning the returned value is the actual content of the config set
1107  */
1108 char *xbt_cfg_get_string(const char *name)
1109 {
1110   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
1111
1112   if (xbt_dynar_length(variable->content) > 1) {
1113     XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
1114          name, xbt_dynar_length(variable->content));
1115   } else if (xbt_dynar_is_empty(variable->content)) {
1116     return NULL;
1117   }
1118
1119   return xbt_dynar_get_as(variable->content, 0, char *);
1120 }
1121
1122 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1123  *
1124  * @param cfg the config set
1125  * @param name the name of the variable
1126  *
1127  * Returns the first value from the config set under the given name.
1128  * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
1129  */
1130 int xbt_cfg_get_boolean(const char *name)
1131 {
1132   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
1133
1134   if (xbt_dynar_length(variable->content) > 1) {
1135     XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
1136          name, xbt_dynar_length(variable->content));
1137   }
1138
1139   return xbt_dynar_get_as(variable->content, 0, int);
1140 }
1141
1142 /** @brief Retrieve the dynar of all the values stored in a variable
1143  *
1144  * @param cfg where to search in
1145  * @param name what to search for
1146  *
1147  * Get the data stored in the config set.
1148  *
1149  * \warning the returned value is the actual content of the config set
1150  */
1151 xbt_dynar_t xbt_cfg_get_dynar(const char *name)
1152 {
1153   xbt_cfgelm_t variable = NULL;
1154   xbt_ex_t e;
1155
1156   TRY {
1157     variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
1158   } CATCH(e) {
1159     if (e.category == not_found_error) {
1160       xbt_ex_free(e);
1161       THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
1162     }
1163     RETHROW;
1164   }
1165
1166   return variable->content;
1167 }
1168
1169 /** @brief Retrieve one of the integer value of a variable */
1170 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1171 {
1172   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1173   return xbt_dynar_get_as(variable->content, pos, int);
1174 }
1175
1176 /** @brief Retrieve one of the double value of a variable */
1177 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1178 {
1179   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1180   return xbt_dynar_get_as(variable->content, pos, double);
1181 }
1182
1183 /** @brief Retrieve one of the string value of a variable */
1184 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1185 {
1186   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1187   return xbt_dynar_get_as(variable->content, pos, char *);
1188 }
1189
1190 /** @brief Retrieve one of the boolean value of a variable */
1191 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1192 {
1193   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1194   return xbt_dynar_get_as(variable->content, pos, int);
1195 }
1196
1197 #ifdef SIMGRID_TEST
1198 #include "xbt.h"
1199 #include "xbt/ex.h"
1200
1201 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1202
1203 XBT_TEST_SUITE("config", "Configuration support");
1204
1205 static xbt_cfg_t make_set()
1206 {
1207   xbt_cfg_t set = NULL;
1208
1209   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1210   xbt_cfg_register_str(&set, "speed:1_to_2_int");
1211   xbt_cfg_register_str(&set, "peername:1_to_1_string");
1212   xbt_cfg_register_str(&set, "user:1_to_10_string");
1213
1214   return set;
1215 }                               /* end_of_make_set */
1216
1217 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1218 {
1219   xbt_cfg_t set = make_set();
1220   xbt_test_add("Alloc and free a config set");
1221   xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1222   xbt_cfg_free(&set);
1223   xbt_cfg_free(&set);
1224 }
1225
1226 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1227 {
1228   xbt_cfg_t set = set = make_set();
1229   xbt_ex_t e;
1230
1231   xbt_test_add("Having too few elements for speed");
1232   xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1233   TRY {
1234     xbt_cfg_check(set);
1235   } CATCH(e) {
1236     if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
1237       xbt_test_fail("Got an exception. msg=%s", e.msg);
1238     xbt_ex_free(e);
1239   }
1240   xbt_cfg_free(&set);
1241   xbt_cfg_free(&set);
1242
1243   xbt_test_add("Having too much values of 'speed'");
1244   set = make_set();
1245   xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1246   TRY {
1247     xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1248   } CATCH(e) {
1249     if (e.category != mismatch_error ||
1250         strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
1251       xbt_test_fail("Got an exception. msg=%s", e.msg);
1252     xbt_ex_free(e);
1253   }
1254   xbt_cfg_check(set);
1255   xbt_cfg_free(&set);
1256   xbt_cfg_free(&set);
1257 }
1258
1259 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1260 {
1261   xbt_test_add("Get a single value");
1262   {
1263     /* get_single_value */
1264     int ival;
1265     xbt_cfg_t myset = make_set();
1266
1267     xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1268     ival = xbt_cfg_get_int("speed");
1269     if (ival != 42)
1270       xbt_test_fail("Speed value = %d, I expected 42", ival);
1271     xbt_cfg_free(&myset);
1272   }
1273
1274   xbt_test_add("Get multiple values");
1275   {
1276     /* get_multiple_value */
1277     xbt_dynar_t dyn;
1278     xbt_cfg_t myset = make_set();
1279
1280     xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1281     xbt_cfg_set_parse(myset, "speed:42");
1282     xbt_cfg_check(myset);
1283     dyn = xbt_cfg_get_dynar("user");
1284
1285     if (xbt_dynar_length(dyn) != 3)
1286       xbt_test_fail("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
1287
1288     if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1289        xbt_test_fail("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn, 0, char *));
1290
1291     if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1292        xbt_test_fail("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn, 1, char *));
1293
1294     if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1295        xbt_test_fail("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn, 2, char *));
1296     xbt_cfg_free(&myset);
1297   }
1298
1299   xbt_test_add("Access to a non-existant entry");
1300   {
1301     /* non-existant_entry */
1302     xbt_cfg_t myset = make_set();
1303     xbt_ex_t e;
1304
1305     TRY {
1306       xbt_cfg_set_parse(myset, "color:blue");
1307     } CATCH(e) {
1308       if (e.category != not_found_error)
1309         xbt_test_exception(e);
1310       xbt_ex_free(e);
1311     }
1312     xbt_cfg_free(&myset);
1313   }
1314 }
1315 #endif                          /* SIMGRID_TEST */