Logo AND Algorithmique Numérique Distribuée

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