Logo AND Algorithmique Numérique Distribuée

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