Logo AND Algorithmique Numérique Distribuée

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