Logo AND Algorithmique Numérique Distribuée

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