Logo AND Algorithmique Numérique Distribuée

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