Logo AND Algorithmique Numérique Distribuée

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