Logo AND Algorithmique Numérique Distribuée

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