Logo AND Algorithmique Numérique Distribuée

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