Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
xbt_cfg_set_vargs() and xbt_cfg_set() now handle only one variable at a time (they...
[simgrid.git] / src / xbt / config.c
1 /* $Id$ */
2
3 /* config - Dictionnary where the type of each variable is provided.            */
4
5 /* This is useful to build named structs, like option or property sets.     */
6
7 /* Copyright (c) 2001,2002,2003,2004 Martin Quinson. All rights reserved.   */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/misc.h"
13 #include "xbt/sysdep.h"
14 #include "xbt/log.h"
15 #include "xbt/error.h"
16 #include "xbt/dynar.h"
17 #include "xbt/dict.h"
18
19 #include "xbt/config.h" /* prototypes of this module */
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
22
23 /* xbt_cfgelm_t: the typedef corresponding to a config variable. 
24
25    Both data and DTD are mixed, but fixing it now would prevent me to ever
26    defend my thesis. */
27
28 typedef struct {
29   /* Allowed type of the variable */
30   e_xbt_cfgelm_type_t type;
31   int min,max;
32   
33   /* Callbacks */
34   xbt_cfg_cb_t cb_set;
35   xbt_cfg_cb_t cb_rm;
36
37   /* actual content 
38      (cannot be an union because type host uses both str and i) */
39   xbt_dynar_t content;
40 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
41
42 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
43   {"int","double","string","host","any"};
44
45 /* Internal stuff used in cache to free a variable */
46 static void xbt_cfgelm_free(void *data);
47
48 /* Retrieve the variable we'll modify */
49 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
50                                     e_xbt_cfgelm_type_t type,
51                                     /* OUT */ xbt_cfgelm_t *whereto);
52
53 static void xbt_cfg_str_free(void *d){
54   free(*(void**)d);
55 }
56 static void xbt_cfg_host_free(void *d){
57   xbt_host_t *h=(xbt_host_t*) *(void**)d; 
58   if (h) {
59     if (h->name) free(h->name);
60     free(h);
61   }
62 }
63
64 /*----[ Memory management ]-----------------------------------------------*/
65
66 /** @brief Constructor
67  *
68  * Initialise an config set
69  */
70
71
72 xbt_cfg_t xbt_cfg_new(void) {
73   return (xbt_cfg_t)xbt_dict_new();
74 }
75
76 /** \brief Copy an existing configuration set
77  *
78  * \arg whereto the config set to be created
79  * \arg tocopy the source data
80  *
81  * This only copy the registrations, not the actual content
82  */
83
84 void
85 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
86   xbt_dict_cursor_t cursor=NULL; 
87   xbt_cfgelm_t variable=NULL;
88   char *name=NULL;
89   
90   *whereto=NULL;
91   xbt_assert0(tocopy,"cannot copy NULL config");
92
93   xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
94     xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
95                      variable->cb_set, variable->cb_rm);
96   }
97 }
98
99 /** @brief Destructor */
100 void xbt_cfg_free(xbt_cfg_t *cfg) {
101   xbt_dict_free((xbt_dict_t*)cfg);
102 }
103
104 /** @brief Dump a config set for debuging purpose
105  *
106  * \arg name The name to give to this config set
107  * \arg indent what to write at the begining of each line (right number of spaces)
108  * \arg cfg the config set
109  */
110 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
111   xbt_dict_t dict = (xbt_dict_t) cfg;
112   xbt_dict_cursor_t cursor=NULL; 
113   xbt_cfgelm_t variable=NULL;
114   char *key=NULL;
115   int i; 
116   int size;
117   int ival;
118   char *sval;
119   double dval;
120   xbt_host_t *hval;
121
122   if (name)
123     printf("%s>> Dumping of the config set '%s':\n",indent,name);
124   xbt_dict_foreach(dict,cursor,key,variable) {
125
126     printf("%s  %s:",indent,key);
127
128     size = xbt_dynar_length(variable->content);
129     printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
130            variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
131            size,
132            variable->cb_rm, 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_host:
158       for (i=0; i<size; i++) {
159         hval = xbt_dynar_get_as(variable->content,i,xbt_host_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) printf("%s<< End of the config set '%s'\n",indent,name);
171   fflush(stdout);
172
173   xbt_dict_cursor_free(&cursor);
174   return;
175 }
176
177 /*
178  * free an config element
179  */
180
181 void xbt_cfgelm_free(void *data) {
182   xbt_cfgelm_t c=(xbt_cfgelm_t)data;
183
184   if (!c) return;
185   xbt_dynar_free(&(c->content));
186   free(c);
187 }
188
189 /*----[ Registering stuff ]-----------------------------------------------*/
190
191 /** @brief Register an element within a config set
192  *
193  *  @arg cfg the config set
194  *  @arg type the type of the config element
195  *  @arg min the minimum
196  *  @arg max the maximum
197  */
198
199 void
200 xbt_cfg_register(xbt_cfg_t cfg,
201                   const char *name, e_xbt_cfgelm_type_t type,
202                   int min, int max,
203                   xbt_cfg_cb_t cb_set,  xbt_cfg_cb_t cb_rm){
204   xbt_cfgelm_t res;
205   xbt_error_t errcode;
206
207   xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_host,
208               "type of %s not valid (%d should be between %d and %d)",
209               name,type,xbt_cfgelm_int, xbt_cfgelm_host);
210   DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",name,min,max,xbt_cfgelm_type_name[type],type);
211   errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
212
213   if (errcode == no_error) {
214     WARN1("Config elem %s registered twice.",name);
215     /* Will be removed by the insertion of the new one */
216   } 
217   xbt_assert_error(mismatch_error);
218
219   res=xbt_new(s_xbt_cfgelm_t,1);
220
221   res->type=type;
222   res->min=min;
223   res->max=max;
224   res->cb_set = cb_set;
225   res->cb_rm  = cb_rm;
226
227   switch (type) {
228   case xbt_cfgelm_int:
229     res->content = xbt_dynar_new(sizeof(int), NULL);
230     break;
231
232   case xbt_cfgelm_double:
233     res->content = xbt_dynar_new(sizeof(double), NULL);
234     break;
235
236   case xbt_cfgelm_string:
237    res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
238    break;
239
240   case xbt_cfgelm_host:
241    res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
242    break;
243
244   default:
245     ERROR1("%d is an invalide type code",type);
246   }
247     
248   xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
249 }
250
251 /** @brief Unregister an element from a config set. 
252  * 
253  *  @arg cfg the config set
254  *  @arg name the name of the elem to be freed
255  * 
256  *  Note that it removes both the description and the actual content.
257  */
258
259 xbt_error_t
260 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
261   return xbt_dict_remove((xbt_dict_t)cfg,name);
262 }
263
264 /**
265  * @brief Parse a string and register the stuff described.
266  *
267  * @arg cfg the config set
268  * @arg entry a string describing the element to register
269  *
270  * The string may consist in several variable descriptions separated by a space. 
271  * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
272  * with type being one of  'string','int', 'host' or 'double'.
273  */
274
275 xbt_error_t
276 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
277   char *entrycpy=xbt_strdup(entry);
278   char *tok;
279
280   int min,max;
281   e_xbt_cfgelm_type_t type;
282
283   tok=strchr(entrycpy, ':');
284   if (!tok) {
285     ERROR3("%s%s%s",
286           "Invalid config element descriptor: ",entry,
287           "; Should be <name>:<min nb>_to_<max nb>_<type>");
288     free(entrycpy);
289     xbt_abort();
290   }
291   *(tok++)='\0';
292
293   min=strtol(tok, &tok, 10);
294   if (!tok) {
295     ERROR1("Invalid minimum in config element descriptor %s",entry);
296     free(entrycpy);
297     xbt_abort();
298   }
299
300   if (!strcmp(tok,"_to_")){
301     ERROR3("%s%s%s",
302           "Invalid config element descriptor: ",entry,
303           "; Should be <name>:<min nb>_to_<max nb>_<type>");
304     free(entrycpy);
305     xbt_abort();
306   }
307   tok += strlen("_to_");
308
309   max=strtol(tok, &tok, 10);
310   if (!tok) {
311     ERROR1("Invalid maximum in config element descriptor %s",entry);
312     free(entrycpy);
313     xbt_abort();
314   }
315
316   if (*(tok++)!='_') {
317     ERROR3("%s%s%s",
318           "Invalid config element descriptor: ",entry,
319           "; Should be <name>:<min nb>_to_<max nb>_<type>");
320     free(entrycpy);
321     xbt_abort();
322   }
323
324   for (type=0; 
325        type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]); 
326        type++);
327   if (type == xbt_cfgelm_type_count) {
328     ERROR3("%s%s%s",
329           "Invalid type in config element descriptor: ",entry,
330           "; Should be one of 'string', 'int', 'host' or 'double'.");
331     free(entrycpy);
332     xbt_abort();
333   }
334
335   xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
336
337   free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
338   return no_error;
339 }
340
341 /** @brief Check that each variable have the right amount of values */
342
343 xbt_error_t
344 xbt_cfg_check(xbt_cfg_t cfg) {
345   xbt_dict_cursor_t cursor; 
346   xbt_cfgelm_t variable;
347   char *name;
348   int size;
349
350   xbt_assert0(cfg,"NULL config set.");
351
352   xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
353     size = xbt_dynar_length(variable->content);
354     if (variable->min > size) { 
355       ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
356              name,
357              variable->min,
358              xbt_cfgelm_type_name[variable->type],
359              size); 
360       xbt_dict_cursor_free(&cursor);
361       return mismatch_error;
362     }
363
364     if (variable->max > 0 && variable->max < size) {
365       ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
366              name,
367              variable->max,
368              xbt_cfgelm_type_name[variable->type],
369              size);
370       xbt_dict_cursor_free(&cursor);
371       return mismatch_error;
372     }
373
374   }
375
376   xbt_dict_cursor_free(&cursor);
377   return no_error;
378 }
379
380 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t  cfg,
381                                     const char *name,
382                                     e_xbt_cfgelm_type_t type,
383                                     /* OUT */ xbt_cfgelm_t *whereto){
384    
385   xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
386                                        (void**)whereto);
387
388   if (errcode == mismatch_error) {
389     RAISE1(mismatch_error,
390            "No registered variable '%s' in this config set",
391            name);
392   }
393   if (errcode != no_error)
394      return errcode;
395
396   xbt_assert3(type == xbt_cfgelm_any || (*whereto)->type == type,
397                "You tried to access to the config element %s as an %s, but its type is %s.",
398                name,
399                xbt_cfgelm_type_name[type],
400                xbt_cfgelm_type_name[(*whereto)->type]);
401
402   return no_error;
403 }
404
405 /** @brief Get the type of this variable in that configuration set
406  *
407  * \arg cfg the config set
408  * \arg name the name of the element 
409  * \arg type the result
410  *
411  */
412
413 xbt_error_t
414 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name, 
415                       /* OUT */e_xbt_cfgelm_type_t *type) {
416
417   xbt_cfgelm_t variable;
418   xbt_error_t errcode;
419
420   errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable);
421
422   if (errcode == mismatch_error) {
423     RAISE1(mismatch_error,"Can't get the type of '%s' since this variable does not exist",
424            name);
425   } else if (errcode != no_error) {
426     return errcode;
427   }
428
429   INFO1("type in variable = %d",variable->type);
430   *type=variable->type;
431
432   return no_error;
433 }
434
435 /*----[ Setting ]---------------------------------------------------------*/
436 /**  @brief va_args version of xbt_cfg_set
437  *
438  * \arg cfg config set to fill
439  * \arg n   variable name
440  * \arg pa  variable value
441  *
442  * Add some values to the config set.
443  */
444 xbt_error_t
445 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
446   char *str;
447   int i;
448   double d;
449   e_xbt_cfgelm_type_t type;
450
451   xbt_error_t errcode;
452   
453   errcode = xbt_cfg_get_type(cfg,name,&type);
454   if (errcode != no_error) {
455     ERROR1("Can't set the property '%s' since it's not registered",name);
456     return mismatch_error;
457   }
458
459   switch (type) {
460   case xbt_cfgelm_host:
461     str = va_arg(pa, char *);
462     i=va_arg(pa,int);
463     TRY(xbt_cfg_set_host(cfg,name,str,i));
464     break;
465       
466   case xbt_cfgelm_string:
467     str=va_arg(pa, char *);
468     TRY(xbt_cfg_set_string(cfg, name, str));
469     break;
470
471   case xbt_cfgelm_int:
472     i=va_arg(pa,int);
473     TRY(xbt_cfg_set_int(cfg,name,i));
474     break;
475
476   case xbt_cfgelm_double:
477     d=va_arg(pa,double);
478     TRY(xbt_cfg_set_double(cfg,name,d));
479     break;
480
481   default:
482     xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
483   }
484
485   return no_error;
486 }
487
488 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
489  *
490  * \arg cfg config set to fill
491  * \arg name variable name
492  * \arg varargs variable value
493  *
494  */
495 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
496   va_list pa;
497   xbt_error_t errcode;
498
499   va_start(pa,name);
500   errcode=xbt_cfg_set_vargs(cfg,name,pa);
501   va_end(pa);
502   return errcode;
503 }
504
505 /** @brief Add values parsed from a string into a config set
506  *
507  * \arg cfg config set to fill
508  * \arg options a string containing the content to add to the config set. This
509  * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
510  * like "[name]:[value]" where [name] is the name of an already registred 
511  * variable, and [value] conforms to the data type under which this variable was
512  * registred.
513  *
514  * @todo This is a crude manual parser, it should be a proper lexer.
515  */
516
517 xbt_error_t
518 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
519   int i;
520   double d;
521   char *str;
522
523   xbt_cfgelm_t variable;
524   char *optionlist_cpy;
525   char *option,  *name,*val;
526
527   int len;
528   xbt_error_t errcode;
529
530   XBT_IN;
531   if (!options || !strlen(options)) { /* nothing to do */
532     return no_error;
533   }
534   optionlist_cpy=xbt_strdup(options);
535
536   DEBUG1("List to parse and set:'%s'",options);
537   option=optionlist_cpy;
538   while (1) { /* breaks in the code */
539
540     if (!option) 
541       break;
542     name=option;
543     len=strlen(name);
544     DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
545            name,len,(long)(option-name));
546
547     /* Pass the value */
548     while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
549       DEBUG1("Take %c.",*option);
550       option++;
551     }
552     if (option-name == len) {
553       DEBUG0("Boundary=EOL");
554       option=NULL; /* don't do next iteration */
555
556     } else {
557       DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
558              *option,len,option-name);
559
560       /* Pass the following blank chars */
561       *(option++)='\0';
562       while (option-name<(len-1) && 
563              (*option == ' ' || *option == '\n' || *option == '\t')) {
564         /*      fprintf(stderr,"Ignore a blank char.\n");*/
565         option++;
566       }
567       if (option-name == len-1)
568         option=NULL; /* don't do next iteration */
569     }
570     DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
571
572     if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
573       continue;
574     if (!strlen(name))
575       break;
576     
577     val=strchr(name,':');
578     if (!val) {
579       free(optionlist_cpy);
580       xbt_assert1(FALSE,
581              "Malformated option: '%s'; Should be of the form 'name:value'",
582                   name);
583     }
584     *(val++)='\0';
585
586     DEBUG2("name='%s';val='%s'",name,val);
587
588     errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable);
589     switch (errcode) {
590     case no_error:
591       break;
592     case mismatch_error:
593       ERROR1("No registrated variable corresponding to '%s'.",name);
594       free(optionlist_cpy);
595       return mismatch_error;
596       break;
597     default:
598       free(optionlist_cpy);
599       return errcode;
600     }
601
602     switch (variable->type) {
603     case xbt_cfgelm_string:
604       errcode = xbt_cfg_set_string(cfg, name, val);
605       if (errcode != no_error) {
606          free(optionlist_cpy);
607          return errcode;
608       }
609       break;
610
611     case xbt_cfgelm_int:
612       i=strtol(val, &val, 0);
613       if (val==NULL) {
614         free(optionlist_cpy);   
615         xbt_assert1(FALSE,
616                      "Value of option %s not valid. Should be an integer",
617                      name);
618       }
619
620       errcode = xbt_cfg_set_int(cfg,name,i);
621       if (errcode != no_error) {
622          free(optionlist_cpy);
623          return errcode;
624       }
625       break;
626
627     case xbt_cfgelm_double:
628       d=strtod(val, &val);
629       if (val==NULL) {
630         free(optionlist_cpy);   
631         xbt_assert1(FALSE,
632                "Value of option %s not valid. Should be a double",
633                name);
634       }
635
636       errcode = xbt_cfg_set_double(cfg,name,d);
637       if (errcode != no_error) {
638          free(optionlist_cpy);
639          return errcode;
640       }
641       break;
642
643     case xbt_cfgelm_host:
644       str=val;
645       val=strchr(val,':');
646       if (!val) {
647         free(optionlist_cpy);   
648         xbt_assert1(FALSE,
649             "Value of option %s not valid. Should be an host (machine:port)",
650                name);
651       }
652
653       *(val++)='\0';
654       i=strtol(val, &val, 0);
655       if (val==NULL) {
656         free(optionlist_cpy);   
657         xbt_assert1(FALSE,
658             "Value of option %s not valid. Should be an host (machine:port)",
659                name);
660       }
661
662       errcode = xbt_cfg_set_host(cfg,name,str,i);
663       if (errcode != no_error) {
664          free(optionlist_cpy);
665          return errcode;
666       }
667       break;      
668
669     default: 
670       free(optionlist_cpy);
671       RAISE1(unknown_error,"Type of config element %s is not valid.",name);
672     }
673     
674   }
675   free(optionlist_cpy);
676   return no_error;
677 }
678
679 /** @brief Set or add an integer value to \a name within \a cfg
680  *
681  * \arg cfg the config set
682  * \arg name the name of the variable
683  * \arg val the value of the variable
684  */ 
685 xbt_error_t
686 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
687   xbt_cfgelm_t variable;
688   xbt_error_t errcode;
689
690   VERB2("Configuration setting: %s=%d",name,val);
691   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
692
693   if (variable->max == 1) {
694     if (variable->cb_rm && xbt_dynar_length(variable->content))
695       (*variable->cb_rm)(name, 0);
696           
697     xbt_dynar_set(variable->content,0,&val);
698   } else {
699     if (variable->max && xbt_dynar_length(variable->content) == variable->max)
700       RAISE3(mismatch_error,
701              "Cannot add value %d to the config element %s since it's already full (size=%d)",
702              val,name,variable->max); 
703              
704     xbt_dynar_push(variable->content,&val);
705   }
706
707   if (variable->cb_set)
708     (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
709   return no_error;
710 }
711
712 /** @brief Set or add a double value to \a name within \a cfg
713  * 
714  * \arg cfg the config set
715  * \arg name the name of the variable
716  * \arg val the doule to set
717  */ 
718
719 xbt_error_t
720 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
721   xbt_cfgelm_t variable;
722   xbt_error_t errcode;
723
724   VERB2("Configuration setting: %s=%f",name,val);
725   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
726
727   if (variable->max == 1) {
728     if (variable->cb_rm && xbt_dynar_length(variable->content))
729       (*variable->cb_rm)(name, 0);
730           
731     xbt_dynar_set(variable->content,0,&val);
732   } else {
733     if (variable->max && xbt_dynar_length(variable->content) == variable->max)
734       RAISE3(mismatch_error,
735              "Cannot add value %f to the config element %s since it's already full (size=%d)",
736              val,name,variable->max); 
737              
738     xbt_dynar_push(variable->content,&val);
739   }
740
741   if (variable->cb_set)
742     (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
743   return no_error;
744 }
745
746 /** @brief Set or add a string value to \a name within \a cfg
747  * 
748  * \arg cfg the config set
749  * \arg name the name of the variable
750  * \arg val the value to be added
751  *
752  */ 
753
754 xbt_error_t
755 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) { 
756   xbt_cfgelm_t variable;
757   xbt_error_t errcode;
758   char *newval = xbt_strdup(val);
759
760   VERB2("Configuration setting: %s=%s",name,val);
761   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
762
763   if (variable->max == 1) {
764     if (variable->cb_rm && xbt_dynar_length(variable->content))
765       (*variable->cb_rm)(name, 0);
766           
767     xbt_dynar_set(variable->content,0,&newval);
768   } else {
769     if (variable->max && xbt_dynar_length(variable->content) == variable->max)
770       RAISE3(mismatch_error,
771              "Cannot add value %s to the config element %s since it's already full (size=%d)",
772              name,val,variable->max); 
773              
774     xbt_dynar_push(variable->content,&newval);
775   }
776
777   if (variable->cb_set)
778     (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
779   return no_error;
780 }
781
782 /** @brief Set or add an host value to \a name within \a cfg
783  * 
784  * \arg cfg the config set
785  * \arg name the name of the variable
786  * \arg host the host
787  * \arg port the port number
788  *
789  * \e host values are composed of a string (hostname) and an integer (port)
790  */ 
791
792 xbt_error_t 
793 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name, 
794                   const char *host,int port) {
795   xbt_cfgelm_t variable;
796   xbt_error_t errcode;
797   xbt_host_t *val=xbt_new(xbt_host_t,1);
798
799   VERB3("Configuration setting: %s=%s:%d",name,host,port);
800
801   val->name = xbt_strdup(name);
802   val->port = port;
803
804   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
805
806   if (variable->max == 1) {
807     if (variable->cb_rm && xbt_dynar_length(variable->content))
808       (*variable->cb_rm)(name, 0);
809           
810     xbt_dynar_set(variable->content,0,&val);
811   } else {
812     if (variable->max && xbt_dynar_length(variable->content) == variable->max)
813       RAISE4(mismatch_error,
814              "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
815              host,port,name,variable->max); 
816              
817     xbt_dynar_push(variable->content,&val);
818   }
819
820   if (variable->cb_set)
821     (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
822   return no_error;
823 }
824
825 /* ---- [ Removing ] ---- */
826
827 /** @brief Remove the provided \e val integer value from a variable
828  *
829  * \arg cfg the config set
830  * \arg name the name of the variable
831  * \arg val the value to be removed
832  */
833 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
834
835   xbt_cfgelm_t variable;
836   int cpt,seen;
837   xbt_error_t errcode;
838
839   if (xbt_dynar_length(variable->content) == variable->min)
840     RAISE3(mismatch_error,
841            "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
842            val,name,variable->min); 
843
844   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
845   
846   xbt_dynar_foreach(variable->content,cpt,seen) {
847     if (seen == val) {
848       if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
849       xbt_dynar_cursor_rm(variable->content,&cpt);
850       return no_error;
851     }
852   }
853
854   ERROR2("Can't remove the value %d of config element %s: value not found.",
855          val,name);
856   return mismatch_error;
857 }
858
859 /** @brief Remove the provided \e val double value from a variable
860  *
861  * \arg cfg the config set
862  * \arg name the name of the variable
863  * \arg val the value to be removed
864  */
865
866 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
867   xbt_cfgelm_t variable;
868   int cpt;
869   double seen;
870   xbt_error_t errcode;
871
872   if (xbt_dynar_length(variable->content) == variable->min)
873     RAISE3(mismatch_error,
874            "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
875            val,name,variable->min); 
876
877   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
878   
879   xbt_dynar_foreach(variable->content,cpt,seen) {
880     if (seen == val) {
881       xbt_dynar_cursor_rm(variable->content,&cpt);
882       if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
883       return no_error;
884     }
885   }
886
887   ERROR2("Can't remove the value %f of config element %s: value not found.",
888          val,name);
889   return mismatch_error;
890 }
891
892 /** @brief Remove the provided \e val string value from a variable
893  *
894  * \arg cfg the config set
895  * \arg name the name of the variable
896  * \arg val the value of the string which will be removed
897  */
898 xbt_error_t
899 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
900   xbt_cfgelm_t variable;
901   int cpt;
902   char *seen;
903   xbt_error_t errcode;
904
905   if (xbt_dynar_length(variable->content) == variable->min)
906     RAISE3(mismatch_error,
907            "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
908            name,val,variable->min); 
909            
910   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
911   
912   xbt_dynar_foreach(variable->content,cpt,seen) {
913     if (!strcpy(seen,val)) {
914       if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
915       xbt_dynar_cursor_rm(variable->content,&cpt);
916       return no_error;
917     }
918   }
919
920   ERROR2("Can't remove the value %s of config element %s: value not found.",
921          val,name);
922   return mismatch_error;
923 }
924
925 /** @brief Remove the provided \e val host value from a variable
926  * 
927  * \arg cfg the config set
928  * \arg name the name of the variable
929  * \arg host the hostname
930  * \arg port the port number
931  */
932
933 xbt_error_t
934 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
935   xbt_cfgelm_t variable;
936   int cpt;
937   xbt_host_t *seen;
938   xbt_error_t errcode;
939
940   if (xbt_dynar_length(variable->content) == variable->min)
941     RAISE4(mismatch_error,
942            "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
943            host,port,name,variable->min); 
944            
945   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
946   
947   xbt_dynar_foreach(variable->content,cpt,seen) {
948     if (!strcpy(seen->name,host) && seen->port == port) {
949       if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
950       xbt_dynar_cursor_rm(variable->content,&cpt);
951       return no_error;
952     }
953   }
954
955   ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
956          host,port,name);
957   return mismatch_error;
958 }
959
960 /** @brief Remove the \e pos th value from the provided variable */
961
962 xbt_error_t xbt_cfg_rm_at   (xbt_cfg_t cfg, const char *name, int pos) {
963
964   xbt_cfgelm_t variable;
965   int cpt;
966   xbt_host_t *seen;
967   xbt_error_t errcode;
968
969   if (xbt_dynar_length(variable->content) == variable->min)
970     RAISE3(mismatch_error,
971            "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
972            pos,name,variable->min); 
973            
974   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_any,&variable));
975   
976   if (variable->cb_rm) (*variable->cb_rm)(name, pos);     
977   xbt_dynar_remove_at(variable->content, pos, NULL);
978
979   return mismatch_error;
980 }
981
982 /** @brief Remove all the values from a variable
983  * 
984  * \arg cfg the config set
985  * \arg name the name of the variable
986  */
987
988 xbt_error_t 
989 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
990   xbt_cfgelm_t variable;
991
992   xbt_error_t errcode;
993
994   TRYCATCH(mismatch_error,
995            xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable));
996   if (errcode == mismatch_error) {
997     ERROR1("Can't empty  '%s' since this config element does not exist",
998            name);
999     return mismatch_error;
1000   }
1001
1002   if (variable) {
1003     if (variable->cb_rm) {
1004       int cpt;
1005       void *ignored;
1006       xbt_dynar_foreach(variable->content,cpt,ignored) {
1007         (*variable->cb_rm)(name, cpt);
1008       }
1009     }
1010     xbt_dynar_reset(variable->content);
1011   }
1012   return no_error;
1013 }
1014
1015 /*----[ Getting ]---------------------------------------------------------*/
1016
1017 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1018  *
1019  * \arg cfg the config set
1020  * \arg name the name of the variable
1021  * \arg val the wanted value
1022  *
1023  * Returns the first value from the config set under the given name.
1024  * If there is more than one value, it will issue a warning. Consider using 
1025  * xbt_cfg_get_dynar() instead.
1026  *
1027  * \warning the returned value is the actual content of the config set
1028  */
1029 xbt_error_t
1030 xbt_cfg_get_int   (xbt_cfg_t  cfg,
1031                     const char *name,
1032                     int        *val) {
1033   xbt_cfgelm_t variable;
1034   xbt_error_t errcode;
1035
1036   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1037
1038   if (xbt_dynar_length(variable->content) > 1) {
1039     WARN2("You asked for the first value of the config element '%s', but there is %lu values",
1040              name, xbt_dynar_length(variable->content));
1041   }
1042
1043   *val = xbt_dynar_get_as(variable->content, 0, int);
1044   return no_error;
1045 }
1046
1047 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1048  * 
1049  * \arg cfg the config set
1050  * \arg name the name of the variable
1051  * \arg val the wanted value
1052  *
1053  * Returns the first value from the config set under the given name.
1054  * If there is more than one value, it will issue a warning. Consider using 
1055  * xbt_cfg_get_dynar() instead.
1056  *
1057  * \warning the returned value is the actual content of the config set
1058  */
1059
1060 xbt_error_t
1061 xbt_cfg_get_double(xbt_cfg_t  cfg,
1062                     const char *name,
1063                     double     *val) {
1064   xbt_cfgelm_t variable;
1065   xbt_error_t  errcode;
1066
1067   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
1068
1069   if (xbt_dynar_length(variable->content) > 1) {
1070     WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1071              name, xbt_dynar_length(variable->content));
1072   }
1073
1074   *val = xbt_dynar_get_as(variable->content, 0, double);
1075   return no_error;
1076 }
1077
1078 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1079  *
1080  * \arg th the config set
1081  * \arg name the name of the variable
1082  * \arg val the wanted value
1083  *
1084  * Returns the first value from the config set under the given name.
1085  * If there is more than one value, it will issue a warning. Consider using 
1086  * xbt_cfg_get_dynar() instead.
1087  *
1088  * \warning the returned value is the actual content of the config set
1089  */
1090
1091 xbt_error_t xbt_cfg_get_string(xbt_cfg_t  cfg,
1092                                  const char *name,
1093                                  char      **val) {
1094   xbt_cfgelm_t  variable;
1095   xbt_error_t errcode;
1096
1097   *val=NULL;
1098
1099   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1100
1101   if (xbt_dynar_length(variable->content) > 1) {
1102     WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1103              name, xbt_dynar_length(variable->content));
1104   }
1105
1106   *val = xbt_dynar_get_as(variable->content, 0, char *);
1107   return no_error;
1108 }
1109
1110 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1111  *
1112  * \arg cfg the config set
1113  * \arg name the name of the variable
1114  * \arg host the host
1115  * \arg port the port number
1116  *
1117  * Returns the first value from the config set under the given name.
1118  * If there is more than one value, it will issue a warning. Consider using
1119  * xbt_cfg_get_dynar() instead.
1120  *
1121  * \warning the returned value is the actual content of the config set
1122  */
1123
1124 xbt_error_t xbt_cfg_get_host  (xbt_cfg_t  cfg,
1125                                  const char *name,
1126                                  char      **host,
1127                                  int        *port) {
1128   xbt_cfgelm_t variable;
1129   xbt_error_t  errcode;
1130   xbt_host_t  *val;
1131
1132   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
1133
1134   if (xbt_dynar_length(variable->content) > 1) {
1135     WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1136              name, xbt_dynar_length(variable->content));
1137   }
1138
1139   val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1140   *host=val->name;
1141   *port=val->port;
1142   
1143   return no_error;
1144 }
1145
1146 /** @brief Retrieve the dynar of all the values stored in a variable
1147  * 
1148  * \arg cfg where to search in
1149  * \arg name what to search for
1150  * \arg dynar result
1151  *
1152  * Get the data stored in the config set. 
1153  *
1154  * \warning the returned value is the actual content of the config set
1155  */
1156 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t    cfg,
1157                                  const char   *name,
1158                                  xbt_dynar_t *dynar) {
1159   xbt_cfgelm_t variable;
1160   xbt_error_t  errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1161                                         (void**)&variable);
1162
1163   if (errcode == mismatch_error) {
1164     ERROR1("No registered variable %s in this config set",
1165            name);
1166     return mismatch_error;
1167   }
1168   if (errcode != no_error)
1169      return errcode;
1170
1171   *dynar = variable->content;
1172   return no_error;
1173 }
1174
1175
1176 /** @brief Retrieve one of the integer value of a variable */
1177 xbt_error_t
1178 xbt_cfg_get_int_at(xbt_cfg_t   cfg,
1179                    const char *name,
1180                    int         pos,
1181                    int        *val) {
1182                   
1183   xbt_cfgelm_t variable;
1184   xbt_error_t errcode;
1185
1186   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1187   *val = xbt_dynar_get_as(variable->content, pos, int);
1188   return no_error; 
1189 }
1190
1191 /** @brief Retrieve one of the double value of a variable */
1192 xbt_error_t
1193 xbt_cfg_get_double_at(xbt_cfg_t   cfg,
1194                       const char *name,
1195                       int         pos,
1196                       double     *val) {
1197                   
1198   xbt_cfgelm_t variable;
1199   xbt_error_t errcode;
1200
1201   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
1202   *val = xbt_dynar_get_as(variable->content, pos, double);
1203   return no_error; 
1204 }
1205
1206
1207 /** @brief Retrieve one of the string value of a variable */
1208 xbt_error_t
1209 xbt_cfg_get_string_at(xbt_cfg_t   cfg,
1210                       const char *name,
1211                       int         pos,
1212                       char      **val) {
1213                   
1214   xbt_cfgelm_t variable;
1215   xbt_error_t errcode;
1216
1217   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1218   *val = xbt_dynar_get_as(variable->content, pos, char*);
1219   return no_error; 
1220 }
1221
1222 /** @brief Retrieve one of the host value of a variable */
1223 xbt_error_t
1224 xbt_cfg_get_host_at(xbt_cfg_t   cfg,
1225                     const char *name,
1226                     int         pos,
1227                     char      **host,
1228                     int        *port) {
1229                   
1230   xbt_cfgelm_t variable;
1231   xbt_error_t errcode;
1232   xbt_host_t *val;
1233
1234   TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1235   val = xbt_dynar_get_ptr(variable->content, pos);
1236   *port = val->port;
1237   *host = val->name;
1238   return no_error; 
1239 }