Logo AND Algorithmique Numérique Distribuée

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