Logo AND Algorithmique Numérique Distribuée

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