Logo AND Algorithmique Numérique Distribuée

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