Logo AND Algorithmique Numérique Distribuée

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