Logo AND Algorithmique Numérique Distribuée

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