Logo AND Algorithmique Numérique Distribuée

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