Logo AND Algorithmique Numérique Distribuée

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