Logo AND Algorithmique Numérique Distribuée

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