Logo AND Algorithmique Numérique Distribuée

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