Logo AND Algorithmique Numérique Distribuée

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