Logo AND Algorithmique Numérique Distribuée

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