Logo AND Algorithmique Numérique Distribuée

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