Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't produce more output that it used to be
[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   while (res && res->type == xbt_cfgelm_alias) {
503     const char* newname = (const char *)res->content;
504     XBT_INFO("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     fflush(stdout);
511     THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
512   }
513
514   xbt_assert(type == xbt_cfgelm_any || res->type == type,
515               "You tried to access to the config element %s as an %s, but its type is %s.",
516               name,
517               xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
518
519   return res;
520 }
521
522 /** @brief Get the type of this variable in that configuration set
523  *
524  * @param cfg the config set
525  * @param name the name of the element
526  *
527  * @return the type of the given element
528  */
529
530 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
531 {
532
533   xbt_cfgelm_t variable = NULL;
534
535   variable = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
536   if (!variable)
537     THROWF(not_found_error, 0,
538            "Can't get the type of '%s' since this variable does not exist",
539            name);
540
541   XBT_DEBUG("type in variable = %d", (int)variable->type);
542
543   return variable->type;
544 }
545
546 /*----[ Setting ]---------------------------------------------------------*/
547 /**  @brief va_args version of xbt_cfg_set
548  *
549  * @param cfg config set to fill
550  * @param name  variable name
551  * @param pa  variable value
552  *
553  * Add some values to the config set.
554  */
555 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
556 {
557   char *str;
558   int i;
559   double d;
560   e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
561
562   xbt_ex_t e;
563
564   TRY {
565     type = xbt_cfg_get_type(cfg, name);
566   }
567   CATCH(e) {
568     if (e.category == not_found_error) {
569       xbt_ex_free(e);
570       THROWF(not_found_error, 0,
571              "Can't set the property '%s' since it's not registered",
572              name);
573     }
574     RETHROW;
575   }
576
577   switch (type) {
578   case xbt_cfgelm_string:
579     str = va_arg(pa, char *);
580     xbt_cfg_set_string(cfg, name, str);
581     break;
582
583   case xbt_cfgelm_int:
584     i = va_arg(pa, int);
585     xbt_cfg_set_int(cfg, name, i);
586     break;
587
588   case xbt_cfgelm_double:
589     d = va_arg(pa, double);
590     xbt_cfg_set_double(cfg, name, d);
591     break;
592
593   case xbt_cfgelm_boolean:
594     str = va_arg(pa, char *);
595     xbt_cfg_set_boolean(cfg, name, str);
596     break;
597
598   default:
599     xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
600   }
601 }
602
603 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
604  *
605  * @param cfg config set to fill
606  * @param name variable name
607  * @param ... variable value
608  *
609  */
610 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
611 {
612   va_list pa;
613
614   va_start(pa, name);
615   xbt_cfg_set_vargs(cfg, name, pa);
616   va_end(pa);
617 }
618
619 /** @brief Add values parsed from a string into a config set
620  *
621  * @param cfg config set to fill
622  * @param options a string containing the content to add to the config set. This
623  * is a '\\t',' ' or '\\n' or ',' separated list of variables. Each individual variable is
624  * like "[name]:[value]" where [name] is the name of an already registred
625  * variable, and [value] conforms to the data type under which this variable was
626  * registred.
627  *
628  * @todo This is a crude manual parser, it should be a proper lexer.
629  */
630
631 void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
632
633   char *optionlist_cpy;
634   char *option, *name, *val;
635
636   int len;
637
638   XBT_IN();
639   if (!options || !strlen(options)) {   /* nothing to do */
640     return;
641   }
642   optionlist_cpy = xbt_strdup(options);
643
644   XBT_DEBUG("List to parse and set:'%s'", options);
645   option = optionlist_cpy;
646   while (1) {                   /* breaks in the code */
647
648     if (!option)
649       break;
650     name = option;
651     len = strlen(name);
652     XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld",
653            name, len, (long) (option - name));
654
655     /* Pass the value */
656     while (option - name <= (len - 1) && *option != ' ' && *option != '\n'
657            && *option != '\t' && *option != ',') {
658       XBT_DEBUG("Take %c.", *option);
659       option++;
660     }
661     if (option - name == len) {
662       XBT_DEBUG("Boundary=EOL");
663       option = NULL;            /* don't do next iteration */
664
665     } else {
666       XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld",
667              *option, len, (long) (option - name));
668
669       /* Pass the following blank chars */
670       *(option++) = '\0';
671       while (option - name < (len - 1) &&
672              (*option == ' ' || *option == '\n' || *option == '\t')) {
673         /*      fprintf(stderr,"Ignore a blank char.\n"); */
674         option++;
675       }
676       if (option - name == len - 1)
677         option = NULL;          /* don't do next iteration */
678     }
679     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
680
681     if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
682       continue;
683     if (!strlen(name))
684       break;
685
686     val = strchr(name, ':');
687     if (!val) {
688       /* don't free(optionlist_cpy) here, 'name' points inside it */
689       xbt_die("Option '%s' badly formatted. Should be of the form 'name:value'",
690               name);
691     }
692     *(val++) = '\0';
693
694     if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
695       XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
696
697     TRY {
698       xbt_cfg_set_as_string(cfg,name,val);
699     } CATCH_ANONYMOUS {
700       free(optionlist_cpy);
701       RETHROW;
702     }
703   }
704   free(optionlist_cpy);
705 }
706
707 /** @brief Set the value of a variable, using the string representation of that value
708  *
709  * @param cfg config set to modify
710  * @param key name of the variable to modify
711  * @param value string representation of the value to set
712  *
713  * @return the first char after the parsed value in val
714  */
715
716 void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
717   xbt_ex_t e;
718
719   char *ret;
720   volatile xbt_cfgelm_t variable = NULL;
721   int i;
722   double d;
723
724   TRY {
725     while (variable == NULL) {
726       variable = xbt_dict_get((xbt_dict_t) cfg, key);
727       if (variable->type == xbt_cfgelm_alias) {
728         const char *newname = (const char*)variable->content;
729         XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
730         key = newname;
731         variable = NULL;
732       }
733     }
734   }
735   CATCH(e) {
736     if (e.category == not_found_error) {
737       xbt_ex_free(e);
738       THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
739     }
740     RETHROW;
741   }
742
743   switch (variable->type) {
744   case xbt_cfgelm_string:
745     xbt_cfg_set_string(cfg, key, value);     /* throws */
746     break;
747
748   case xbt_cfgelm_int:
749     i = strtol(value, &ret, 0);
750     if (ret == value) {
751       xbt_die("Value of option %s not valid. Should be an integer", key);
752     }
753
754     xbt_cfg_set_int(cfg, key, i);  /* throws */
755     break;
756
757   case xbt_cfgelm_double:
758     d = strtod(value, &ret);
759     if (ret == value) {
760       xbt_die("Value of option %s not valid. Should be a double", key);
761     }
762
763     xbt_cfg_set_double(cfg, key, d);       /* throws */
764     break;
765
766   case xbt_cfgelm_boolean:
767     xbt_cfg_set_boolean(cfg, key, value);  /* throws */
768     ret = (char *)value + strlen(value);
769     break;
770
771   default:
772     THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
773     break;
774   }
775
776   return ret;
777 }
778
779 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
780  *
781  * This is useful to change the default value of a variable while allowing
782  * users to override it with command line arguments
783  */
784 void xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val)
785 {
786   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
787
788   if (variable->isdefault){
789     xbt_cfg_set_int(cfg, name, val);
790     variable->isdefault = 1;
791   }
792    else
793     XBT_DEBUG
794         ("Do not override configuration variable '%s' with value '%d' because it was already set.",
795          name, val);
796 }
797
798 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
799  *
800  * This is useful to change the default value of a variable while allowing
801  * users to override it with command line arguments
802  */
803 void xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val)
804 {
805   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
806
807   if (variable->isdefault) {
808     xbt_cfg_set_double(cfg, name, val);
809     variable->isdefault = 1;
810   }
811   else
812     XBT_DEBUG
813         ("Do not override configuration variable '%s' with value '%f' because it was already set.",
814          name, val);
815 }
816
817 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
818  *
819  * This is useful to change the default value of a variable while allowing
820  * users to override it with command line arguments
821  */
822 void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
823                                const char *val)
824 {
825   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
826
827   if (variable->isdefault){
828     xbt_cfg_set_string(cfg, name, val);
829     variable->isdefault = 1;
830   }
831   else
832     XBT_DEBUG
833         ("Do not override configuration variable '%s' with value '%s' because it was already set.",
834          name, val);
835 }
836
837
838 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
839  *
840  * This is useful to change the default value of a variable while allowing
841  * users to override it with command line arguments
842  */
843 void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
844 {
845   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
846
847   if (variable->isdefault){
848     xbt_cfg_set_boolean(cfg, name, val);
849     variable->isdefault = 1;
850   }
851    else
852     XBT_DEBUG
853         ("Do not override configuration variable '%s' with value '%s' because it was already set.",
854          name, val);
855 }
856
857 /** @brief Set or add an integer value to \a name within \a cfg
858  *
859  * @param cfg the config set
860  * @param name the name of the variable
861  * @param val the value of the variable
862  */
863 void xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val)
864 {
865
866   XBT_VERB("Configuration setting: %s=%d", name, val);
867   xbt_cfgelm_t 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
900   XBT_VERB("Configuration setting: %s=%f", name, val);
901   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
902
903   if (variable->max == 1) {
904     if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
905       variable->cb_rm(name, 0);
906
907     xbt_dynar_set(variable->content, 0, &val);
908   } else {
909     if (variable->max
910         && xbt_dynar_length(variable->content) == variable->max)
911       THROWF(mismatch_error, 0,
912              "Cannot add value %f to the config element %s since it's already full (size=%d)",
913              val, name, variable->max);
914
915     xbt_dynar_push(variable->content, &val);
916   }
917
918   if (variable->cb_set)
919     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
920   variable->isdefault = 0;
921 }
922
923 /** @brief Set or add a string value to \a name within \a cfg
924  *
925  * @param cfg the config set
926  * @param name the name of the variable
927  * @param val the value to be added
928  *
929  */
930
931 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
932 {
933   char *newval = xbt_strdup(val);
934
935   XBT_VERB("Configuration setting: %s=%s", name, val);
936   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
937
938   XBT_DEBUG("Variable: %d to %d %s (=%d) @%p",
939          variable->min, variable->max, xbt_cfgelm_type_name[variable->type], (int)variable->type, variable);
940
941   if (variable->max == 1) {
942     if (!xbt_dynar_is_empty(variable->content)) {
943       if (variable->cb_rm)
944         variable->cb_rm(name, 0);
945       else if (variable->type == xbt_cfgelm_string) {
946         char *sval = xbt_dynar_get_as(variable->content, 0, char *);
947         free(sval);
948       }
949     }
950
951     xbt_dynar_set(variable->content, 0, &newval);
952   } else {
953     if (variable->max
954         && xbt_dynar_length(variable->content) == variable->max)
955       THROWF(mismatch_error, 0,
956              "Cannot add value %s to the config element %s since it's already full (size=%d)",
957              name, val, variable->max);
958
959     xbt_dynar_push(variable->content, &newval);
960   }
961
962   if (variable->cb_set)
963     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
964   variable->isdefault = 0;
965 }
966
967 /** @brief Set or add a boolean value to \a name within \a cfg
968  *
969  * @param cfg the config set
970  * @param name the name of the variable
971  * @param val the value of the variable
972  */
973 void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
974 {
975   int i, bval;
976
977   XBT_VERB("Configuration setting: %s=%s", name, val);
978   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
979
980   for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
981   if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
982     bval = 1;
983     break;
984   }
985   if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
986     bval = 0;
987     break;
988   }
989   }
990   if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
991     xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
992   }
993
994   if (variable->max == 1) {
995     if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
996       variable->cb_rm(name, 0);
997
998     xbt_dynar_set(variable->content, 0, &bval);
999   } else {
1000     if (variable->max
1001         && xbt_dynar_length(variable->content) ==
1002         (unsigned long) variable->max)
1003       THROWF(mismatch_error, 0,
1004              "Cannot add value %s to the config element %s since it's already full (size=%d)",
1005              val, name, variable->max);
1006
1007     xbt_dynar_push(variable->content, &bval);
1008   }
1009
1010   if (variable->cb_set)
1011     variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
1012   variable->isdefault = 0;
1013 }
1014
1015 /* ---- [ Removing ] ---- */
1016
1017 /** @brief Remove the provided \e val integer value from a variable
1018  *
1019  * @param cfg the config set
1020  * @param name the name of the variable
1021  * @param val the value to be removed
1022  */
1023 void xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val)
1024 {
1025
1026   unsigned int cpt;
1027   int seen;
1028
1029   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1030
1031   if (xbt_dynar_length(variable->content) == variable->min)
1032     THROWF(mismatch_error, 0,
1033            "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1034            val, name, variable->min);
1035
1036   xbt_dynar_foreach(variable->content, cpt, seen) {
1037     if (seen == val) {
1038       if (variable->cb_rm)
1039         variable->cb_rm(name, cpt);
1040       xbt_dynar_cursor_rm(variable->content, &cpt);
1041       return;
1042     }
1043   }
1044
1045   THROWF(not_found_error, 0,
1046          "Can't remove the value %d of config element %s: value not found.",
1047          val, name);
1048 }
1049
1050 /** @brief Remove the provided \e val double value from a variable
1051  *
1052  * @param cfg the config set
1053  * @param name the name of the variable
1054  * @param val the value to be removed
1055  */
1056
1057 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val)
1058 {
1059   unsigned int cpt;
1060   double seen;
1061
1062   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1063
1064   if (xbt_dynar_length(variable->content) == variable->min)
1065     THROWF(mismatch_error, 0,
1066            "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
1067            val, name, variable->min);
1068
1069   xbt_dynar_foreach(variable->content, cpt, seen) {
1070     if (seen == val) {
1071       xbt_dynar_cursor_rm(variable->content, &cpt);
1072       if (variable->cb_rm)
1073         variable->cb_rm(name, cpt);
1074       return;
1075     }
1076   }
1077
1078   THROWF(not_found_error, 0,
1079          "Can't remove the value %f of config element %s: value not found.",
1080          val, name);
1081 }
1082
1083 /** @brief Remove the provided \e val string value from a variable
1084  *
1085  * @param cfg the config set
1086  * @param name the name of the variable
1087  * @param val the value of the string which will be removed
1088  */
1089 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
1090 {
1091   unsigned int cpt;
1092   char *seen;
1093   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1094
1095   if (xbt_dynar_length(variable->content) == variable->min)
1096     THROWF(mismatch_error, 0,
1097            "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
1098            name, val, variable->min);
1099
1100   xbt_dynar_foreach(variable->content, cpt, seen) {
1101     if (!strcpy(seen, val)) {
1102       if (variable->cb_rm)
1103         variable->cb_rm(name, cpt);
1104       xbt_dynar_cursor_rm(variable->content, &cpt);
1105       return;
1106     }
1107   }
1108
1109   THROWF(not_found_error, 0,
1110          "Can't remove the value %s of config element %s: value not found.",
1111          val, name);
1112 }
1113
1114 /** @brief Remove the provided \e val boolean value from a variable
1115  *
1116  * @param cfg the config set
1117  * @param name the name of the variable
1118  * @param val the value to be removed
1119  */
1120 void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
1121 {
1122
1123   unsigned int cpt;
1124   int seen;
1125   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1126
1127   if (xbt_dynar_length(variable->content) == variable->min)
1128     THROWF(mismatch_error, 0,
1129            "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
1130            val, name, variable->min);
1131
1132   xbt_dynar_foreach(variable->content, cpt, seen) {
1133     if (seen == val) {
1134       if (variable->cb_rm)
1135         variable->cb_rm(name, cpt);
1136       xbt_dynar_cursor_rm(variable->content, &cpt);
1137       return;
1138     }
1139   }
1140
1141   THROWF(not_found_error, 0,
1142          "Can't remove the value %d of config element %s: value not found.",
1143          val, name);
1144 }
1145
1146 /** @brief Remove the \e pos th value from the provided variable */
1147
1148 void xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos)
1149 {
1150
1151   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1152
1153   if (xbt_dynar_length(variable->content) == variable->min)
1154     THROWF(mismatch_error, 0,
1155            "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
1156            pos, name, variable->min);
1157
1158   if (variable->cb_rm)
1159     variable->cb_rm(name, pos);
1160   xbt_dynar_remove_at(variable->content, pos, NULL);
1161 }
1162
1163 /** @brief Remove all the values from a variable
1164  *
1165  * @param cfg the config set
1166  * @param name the name of the variable
1167  */
1168
1169 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name)
1170 {
1171   xbt_cfgelm_t variable = NULL;
1172   xbt_ex_t e;
1173
1174   TRY {
1175     variable = xbt_dict_get((xbt_dict_t) cfg, name);
1176   }
1177   CATCH(e) {
1178     if (e.category != not_found_error)
1179       RETHROW;
1180
1181     xbt_ex_free(e);
1182     THROWF(not_found_error, 0,
1183            "Can't empty  '%s' since this config element does not exist",
1184            name);
1185   }
1186
1187   if (variable) {
1188     if (variable->cb_rm) {
1189       unsigned int cpt;
1190       void *ignored;
1191       xbt_dynar_foreach(variable->content, cpt, ignored) {
1192         variable->cb_rm(name, cpt);
1193       }
1194     }
1195     xbt_dynar_reset(variable->content);
1196   }
1197 }
1198 /*
1199  * Say if the value is the default value
1200  */
1201 int xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name)
1202 {
1203   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_any);
1204   return variable->isdefault;
1205 }
1206
1207 /*----[ Getting ]---------------------------------------------------------*/
1208
1209 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1210  *
1211  * @param cfg the config set
1212  * @param name the name of the variable
1213  *
1214  * Returns the first value from the config set under the given name.
1215  * If there is more than one value, it will issue a warning. Consider using
1216  * xbt_cfg_get_dynar() instead.
1217  *
1218  * \warning the returned value is the actual content of the config set
1219  */
1220 int xbt_cfg_get_int(xbt_cfg_t cfg, const char *name)
1221 {
1222   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1223
1224   if (xbt_dynar_length(variable->content) > 1) {
1225     XBT_WARN
1226         ("You asked for the first value of the config element '%s', but there is %lu values",
1227          name, xbt_dynar_length(variable->content));
1228   }
1229
1230   return xbt_dynar_get_as(variable->content, 0, int);
1231 }
1232
1233 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1234  *
1235  * @param cfg the config set
1236  * @param name the name of the variable
1237  *
1238  * Returns the first value from the config set under the given name.
1239  * If there is more than one value, it will issue a warning. Consider using
1240  * xbt_cfg_get_dynar() instead.
1241  *
1242  * \warning the returned value is the actual content of the config set
1243  */
1244
1245 double xbt_cfg_get_double(xbt_cfg_t cfg, const char *name)
1246 {
1247   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1248
1249   if (xbt_dynar_length(variable->content) > 1) {
1250     XBT_WARN
1251         ("You asked for the first value of the config element '%s', but there is %lu values\n",
1252          name, xbt_dynar_length(variable->content));
1253   }
1254
1255   return xbt_dynar_get_as(variable->content, 0, double);
1256 }
1257
1258 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1259  *
1260  * @param cfg the config set
1261  * @param name the name of the variable
1262  *
1263  * Returns the first value from the config set under the given name.
1264  * If there is more than one value, it will issue a warning. Consider using
1265  * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
1266  *
1267  * \warning the returned value is the actual content of the config set
1268  */
1269
1270 char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
1271 {
1272   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1273
1274   if (xbt_dynar_length(variable->content) > 1) {
1275     XBT_WARN
1276         ("You asked for the first value of the config element '%s', but there is %lu values\n",
1277          name, xbt_dynar_length(variable->content));
1278   } else if (xbt_dynar_is_empty(variable->content)) {
1279     return NULL;
1280   }
1281
1282   return xbt_dynar_get_as(variable->content, 0, char *);
1283 }
1284
1285 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
1286  *
1287  * @param cfg the config set
1288  * @param name the name of the variable
1289  *
1290  * Returns the first value from the config set under the given name.
1291  * If there is more than one value, it will issue a warning. Consider using
1292  * xbt_cfg_get_dynar() instead.
1293  *
1294  * \warning the returned value is the actual content of the config set
1295  */
1296 int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
1297 {
1298   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1299
1300   if (xbt_dynar_length(variable->content) > 1) {
1301     XBT_WARN
1302         ("You asked for the first value of the config element '%s', but there is %lu values",
1303          name, xbt_dynar_length(variable->content));
1304   }
1305
1306   return xbt_dynar_get_as(variable->content, 0, int);
1307 }
1308
1309 /** @brief Retrieve the dynar of all the values stored in a variable
1310  *
1311  * @param cfg where to search in
1312  * @param name what to search for
1313  *
1314  * Get the data stored in the config set.
1315  *
1316  * \warning the returned value is the actual content of the config set
1317  */
1318 xbt_dynar_t xbt_cfg_get_dynar(xbt_cfg_t cfg, const char *name)
1319 {
1320   xbt_cfgelm_t variable = NULL;
1321   xbt_ex_t e;
1322
1323   TRY {
1324     variable = xbt_dict_get((xbt_dict_t) cfg, name);
1325   }
1326   CATCH(e) {
1327     if (e.category == not_found_error) {
1328       xbt_ex_free(e);
1329       THROWF(not_found_error, 0,
1330              "No registered variable %s in this config set", name);
1331     }
1332     RETHROW;
1333   }
1334
1335   return variable->content;
1336 }
1337
1338
1339 /** @brief Retrieve one of the integer value of a variable */
1340 int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
1341 {
1342
1343   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
1344   return xbt_dynar_get_as(variable->content, pos, int);
1345 }
1346
1347 /** @brief Retrieve one of the double value of a variable */
1348 double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
1349 {
1350
1351   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
1352   return xbt_dynar_get_as(variable->content, pos, double);
1353 }
1354
1355
1356 /** @brief Retrieve one of the string value of a variable */
1357 char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
1358 {
1359
1360   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
1361   return xbt_dynar_get_as(variable->content, pos, char *);
1362 }
1363
1364 /** @brief Retrieve one of the boolean value of a variable */
1365 int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
1366 {
1367
1368   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
1369   return xbt_dynar_get_as(variable->content, pos, int);
1370 }
1371
1372
1373 #ifdef SIMGRID_TEST
1374 #include "xbt.h"
1375 #include "xbt/ex.h"
1376
1377 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
1378
1379 XBT_TEST_SUITE("config", "Configuration support");
1380
1381 static xbt_cfg_t make_set()
1382 {
1383   xbt_cfg_t set = NULL;
1384
1385   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
1386   xbt_cfg_register_str(&set, "speed:1_to_2_int");
1387   xbt_cfg_register_str(&set, "peername:1_to_1_string");
1388   xbt_cfg_register_str(&set, "user:1_to_10_string");
1389
1390   return set;
1391 }                               /* end_of_make_set */
1392
1393 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
1394 {
1395   xbt_cfg_t set = make_set();
1396   xbt_test_add("Alloc and free a config set");
1397   xbt_cfg_set_parse(set,
1398                     "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1399   xbt_cfg_free(&set);
1400   xbt_cfg_free(&set);
1401 }
1402
1403 XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
1404 {
1405   xbt_cfg_t set = set = make_set();
1406   xbt_ex_t e;
1407
1408   xbt_test_add("Having too few elements for speed");
1409   xbt_cfg_set_parse(set,
1410                     "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1411   TRY {
1412     xbt_cfg_check(set);
1413   }
1414   CATCH(e) {
1415     if (e.category != mismatch_error ||
1416         strncmp(e.msg, "Config elem speed needs",
1417                 strlen("Config elem speed needs")))
1418       xbt_test_fail("Got an exception. msg=%s", e.msg);
1419     xbt_ex_free(e);
1420   }
1421   xbt_cfg_free(&set);
1422   xbt_cfg_free(&set);
1423
1424
1425
1426   xbt_test_add("Having too much values of 'speed'");
1427   set = make_set();
1428   xbt_cfg_set_parse(set, "peername:toto:42 user:alegrand");
1429   TRY {
1430     xbt_cfg_set_parse(set, "speed:42 speed:24 speed:34");
1431   }
1432   CATCH(e) {
1433     if (e.category != mismatch_error ||
1434         strncmp(e.msg, "Cannot add value 34 to the config elem speed",
1435                 strlen("Config elem speed needs")))
1436       xbt_test_fail("Got an exception. msg=%s", e.msg);
1437     xbt_ex_free(e);
1438   }
1439   xbt_cfg_check(set);
1440   xbt_cfg_free(&set);
1441   xbt_cfg_free(&set);
1442
1443 }
1444
1445 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
1446 {
1447
1448   xbt_test_add("Get a single value");
1449   {
1450     /* get_single_value */
1451     int ival;
1452     xbt_cfg_t myset = make_set();
1453
1454     xbt_cfg_set_parse(myset, "peername:toto:42 speed:42");
1455     ival = xbt_cfg_get_int(myset, "speed");
1456     if (ival != 42)
1457       xbt_test_fail("Speed value = %d, I expected 42", ival);
1458     xbt_cfg_free(&myset);
1459   }
1460
1461   xbt_test_add("Get multiple values");
1462   {
1463     /* get_multiple_value */
1464     xbt_dynar_t dyn;
1465     xbt_cfg_t myset = make_set();
1466
1467     xbt_cfg_set_parse(myset,
1468                       "peername:veloce user:foo\nuser:bar\tuser:toto");
1469     xbt_cfg_set_parse(myset, "speed:42");
1470     xbt_cfg_check(myset);
1471     dyn = xbt_cfg_get_dynar(myset, "user");
1472
1473     if (xbt_dynar_length(dyn) != 3)
1474       xbt_test_fail("Dynar length = %lu, I expected 3",
1475                      xbt_dynar_length(dyn));
1476
1477     if (strcmp(xbt_dynar_get_as(dyn, 0, char *), "foo"))
1478        xbt_test_fail("Dynar[0] = %s, I expected foo",
1479                       xbt_dynar_get_as(dyn, 0, char *));
1480
1481     if (strcmp(xbt_dynar_get_as(dyn, 1, char *), "bar"))
1482        xbt_test_fail("Dynar[1] = %s, I expected bar",
1483                       xbt_dynar_get_as(dyn, 1, char *));
1484
1485     if (strcmp(xbt_dynar_get_as(dyn, 2, char *), "toto"))
1486        xbt_test_fail("Dynar[2] = %s, I expected toto",
1487                       xbt_dynar_get_as(dyn, 2, char *));
1488     xbt_cfg_free(&myset);
1489   }
1490
1491   xbt_test_add("Access to a non-existant entry");
1492   {
1493     /* non-existant_entry */
1494     xbt_cfg_t myset = make_set();
1495     xbt_ex_t e;
1496
1497     TRY {
1498       xbt_cfg_set_parse(myset, "color:blue");
1499     }
1500     CATCH(e) {
1501       if (e.category != not_found_error)
1502         xbt_test_exception(e);
1503       xbt_ex_free(e);
1504     }
1505     xbt_cfg_free(&myset);
1506   }
1507 }
1508 #endif                          /* SIMGRID_TEST */