Logo AND Algorithmique Numérique Distribuée

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