Logo AND Algorithmique Numérique Distribuée

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