Logo AND Algorithmique Numérique Distribuée

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