Logo AND Algorithmique Numérique Distribuée

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