Logo AND Algorithmique Numérique Distribuée

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