Logo AND Algorithmique Numérique Distribuée

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