Logo AND Algorithmique Numérique Distribuée

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