3 /* config - Dictionnary where the type of each variable is provided. */
5 /* This is useful to build named structs, like option or property sets. */
7 /* Copyright (c) 2001,2002,2003,2004 Martin Quinson. All rights reserved. */
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. */
13 #include "xbt/sysdep.h"
15 #include "xbt/error.h"
16 #include "xbt/dynar.h"
19 #include "xbt/config.h" /* prototypes of this module */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
23 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
25 Both data and DTD are mixed, but fixing it now would prevent me to ever
29 /* Allowed type of the variable */
30 e_xbt_cfgelm_type_t type;
38 (cannot be an union because type host uses both str and i) */
40 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
42 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
43 {"int","double","string","host","any"};
45 /* Internal stuff used in cache to free a variable */
46 static void xbt_cfgelm_free(void *data);
48 /* Retrieve the variable we'll modify */
49 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
50 e_xbt_cfgelm_type_t type,
51 /* OUT */ xbt_cfgelm_t *whereto);
53 static void xbt_cfg_str_free(void *d){
56 static void xbt_cfg_host_free(void *d){
57 xbt_host_t *h=(xbt_host_t*) *(void**)d;
59 if (h->name) free(h->name);
64 /*----[ Memory management ]-----------------------------------------------*/
66 /** @brief Constructor
68 * Initialise an config set
72 xbt_cfg_t xbt_cfg_new(void) {
73 return (xbt_cfg_t)xbt_dict_new();
76 /** \brief Copy an existing configuration set
78 * \arg whereto the config set to be created
79 * \arg tocopy the source data
81 * This only copy the registrations, not the actual content
85 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
86 xbt_dict_cursor_t cursor=NULL;
87 xbt_cfgelm_t variable=NULL;
91 xbt_assert0(tocopy,"cannot copy NULL config");
93 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
94 xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
95 variable->cb_set, variable->cb_rm);
99 /** @brief Destructor */
100 void xbt_cfg_free(xbt_cfg_t *cfg) {
101 xbt_dict_free((xbt_dict_t*)cfg);
104 /** @brief Dump a config set for debuging purpose
106 * \arg name The name to give to this config set
107 * \arg indent what to write at the begining of each line (right number of spaces)
108 * \arg cfg the config set
110 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
111 xbt_dict_t dict = (xbt_dict_t) cfg;
112 xbt_dict_cursor_t cursor=NULL;
113 xbt_cfgelm_t variable=NULL;
123 printf("%s>> Dumping of the config set '%s':\n",indent,name);
124 xbt_dict_foreach(dict,cursor,key,variable) {
126 printf("%s %s:",indent,key);
128 size = xbt_dynar_length(variable->content);
129 printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
130 variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
132 variable->cb_rm, variable->cb_set);
134 switch (variable->type) {
137 for (i=0; i<size; i++) {
138 ival = xbt_dynar_get_as(variable->content,i,int);
139 printf ("%s %d\n",indent,ival);
143 case xbt_cfgelm_double:
144 for (i=0; i<size; i++) {
145 dval = xbt_dynar_get_as(variable->content,i,double);
146 printf ("%s %f\n",indent,dval);
150 case xbt_cfgelm_string:
151 for (i=0; i<size; i++) {
152 sval = xbt_dynar_get_as(variable->content,i,char*);
153 printf ("%s %s\n",indent,sval);
157 case xbt_cfgelm_host:
158 for (i=0; i<size; i++) {
159 hval = xbt_dynar_get_as(variable->content,i,xbt_host_t*);
160 printf ("%s %s:%d\n",indent,hval->name,hval->port);
165 printf("%s Invalid type!!\n",indent);
170 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
173 xbt_dict_cursor_free(&cursor);
178 * free an config element
181 void xbt_cfgelm_free(void *data) {
182 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
185 xbt_dynar_free(&(c->content));
189 /*----[ Registering stuff ]-----------------------------------------------*/
191 /** @brief Register an element within a config set
193 * @arg cfg the config set
194 * @arg type the type of the config element
195 * @arg min the minimum
196 * @arg max the maximum
200 xbt_cfg_register(xbt_cfg_t cfg,
201 const char *name, e_xbt_cfgelm_type_t type,
203 xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
207 xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_host,
208 "type of %s not valid (%d should be between %d and %d)",
209 name,type,xbt_cfgelm_int, xbt_cfgelm_host);
210 DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",name,min,max,xbt_cfgelm_type_name[type],type);
211 errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
213 if (errcode == no_error) {
214 WARN1("Config elem %s registered twice.",name);
215 /* Will be removed by the insertion of the new one */
217 xbt_assert_error(mismatch_error);
219 res=xbt_new(s_xbt_cfgelm_t,1);
224 res->cb_set = cb_set;
229 res->content = xbt_dynar_new(sizeof(int), NULL);
232 case xbt_cfgelm_double:
233 res->content = xbt_dynar_new(sizeof(double), NULL);
236 case xbt_cfgelm_string:
237 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
240 case xbt_cfgelm_host:
241 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
245 ERROR1("%d is an invalide type code",type);
248 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
251 /** @brief Unregister an element from a config set.
253 * @arg cfg the config set
254 * @arg name the name of the elem to be freed
256 * Note that it removes both the description and the actual content.
260 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
261 return xbt_dict_remove((xbt_dict_t)cfg,name);
265 * @brief Parse a string and register the stuff described.
267 * @arg cfg the config set
268 * @arg entry a string describing the element to register
270 * The string may consist in several variable descriptions separated by a space.
271 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
272 * with type being one of 'string','int', 'host' or 'double'.
276 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
277 char *entrycpy=xbt_strdup(entry);
281 e_xbt_cfgelm_type_t type;
283 tok=strchr(entrycpy, ':');
286 "Invalid config element descriptor: ",entry,
287 "; Should be <name>:<min nb>_to_<max nb>_<type>");
293 min=strtol(tok, &tok, 10);
295 ERROR1("Invalid minimum in config element descriptor %s",entry);
300 if (!strcmp(tok,"_to_")){
302 "Invalid config element descriptor: ",entry,
303 "; Should be <name>:<min nb>_to_<max nb>_<type>");
307 tok += strlen("_to_");
309 max=strtol(tok, &tok, 10);
311 ERROR1("Invalid maximum in config element descriptor %s",entry);
318 "Invalid config element descriptor: ",entry,
319 "; Should be <name>:<min nb>_to_<max nb>_<type>");
325 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
327 if (type == xbt_cfgelm_type_count) {
329 "Invalid type in config element descriptor: ",entry,
330 "; Should be one of 'string', 'int', 'host' or 'double'.");
335 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
337 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
341 /** @brief Check that each variable have the right amount of values */
344 xbt_cfg_check(xbt_cfg_t cfg) {
345 xbt_dict_cursor_t cursor;
346 xbt_cfgelm_t variable;
350 xbt_assert0(cfg,"NULL config set.");
352 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
353 size = xbt_dynar_length(variable->content);
354 if (variable->min > size) {
355 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
358 xbt_cfgelm_type_name[variable->type],
360 xbt_dict_cursor_free(&cursor);
361 return mismatch_error;
364 if (variable->max > 0 && variable->max < size) {
365 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
368 xbt_cfgelm_type_name[variable->type],
370 xbt_dict_cursor_free(&cursor);
371 return mismatch_error;
376 xbt_dict_cursor_free(&cursor);
380 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg,
382 e_xbt_cfgelm_type_t type,
383 /* OUT */ xbt_cfgelm_t *whereto){
385 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
388 if (errcode == mismatch_error) {
389 RAISE1(mismatch_error,
390 "No registered variable '%s' in this config set",
393 if (errcode != no_error)
396 xbt_assert3(type == xbt_cfgelm_any || (*whereto)->type == type,
397 "You tried to access to the config element %s as an %s, but its type is %s.",
399 xbt_cfgelm_type_name[type],
400 xbt_cfgelm_type_name[(*whereto)->type]);
405 /** @brief Get the type of this variable in that configuration set
407 * \arg cfg the config set
408 * \arg name the name of the element
409 * \arg type the result
414 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name,
415 /* OUT */e_xbt_cfgelm_type_t *type) {
417 xbt_cfgelm_t variable;
420 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable);
422 if (errcode == mismatch_error) {
423 RAISE1(mismatch_error,"Can't get the type of '%s' since this variable does not exist",
425 } else if (errcode != no_error) {
429 INFO1("type in variable = %d",variable->type);
430 *type=variable->type;
435 /*----[ Setting ]---------------------------------------------------------*/
436 /** @brief va_args version of xbt_cfg_set
438 * \arg cfg config set to fill
439 * \arg n variable name
440 * \arg pa variable value
442 * Add some values to the config set.
445 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
449 e_xbt_cfgelm_type_t type;
453 errcode = xbt_cfg_get_type(cfg,name,&type);
454 if (errcode != no_error) {
455 ERROR1("Can't set the property '%s' since it's not registered",name);
456 return mismatch_error;
460 case xbt_cfgelm_host:
461 str = va_arg(pa, char *);
463 TRY(xbt_cfg_set_host(cfg,name,str,i));
466 case xbt_cfgelm_string:
467 str=va_arg(pa, char *);
468 TRY(xbt_cfg_set_string(cfg, name, str));
473 TRY(xbt_cfg_set_int(cfg,name,i));
476 case xbt_cfgelm_double:
478 TRY(xbt_cfg_set_double(cfg,name,d));
482 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
488 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
490 * \arg cfg config set to fill
491 * \arg name variable name
492 * \arg varargs variable value
495 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
500 errcode=xbt_cfg_set_vargs(cfg,name,pa);
505 /** @brief Add values parsed from a string into a config set
507 * \arg cfg config set to fill
508 * \arg options a string containing the content to add to the config set. This
509 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
510 * like "[name]:[value]" where [name] is the name of an already registred
511 * variable, and [value] conforms to the data type under which this variable was
514 * @todo This is a crude manual parser, it should be a proper lexer.
518 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
523 xbt_cfgelm_t variable;
524 char *optionlist_cpy;
525 char *option, *name,*val;
531 if (!options || !strlen(options)) { /* nothing to do */
534 optionlist_cpy=xbt_strdup(options);
536 DEBUG1("List to parse and set:'%s'",options);
537 option=optionlist_cpy;
538 while (1) { /* breaks in the code */
544 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
545 name,len,(long)(option-name));
548 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
549 DEBUG1("Take %c.",*option);
552 if (option-name == len) {
553 DEBUG0("Boundary=EOL");
554 option=NULL; /* don't do next iteration */
557 DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
558 *option,len,option-name);
560 /* Pass the following blank chars */
562 while (option-name<(len-1) &&
563 (*option == ' ' || *option == '\n' || *option == '\t')) {
564 /* fprintf(stderr,"Ignore a blank char.\n");*/
567 if (option-name == len-1)
568 option=NULL; /* don't do next iteration */
570 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
572 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
577 val=strchr(name,':');
579 free(optionlist_cpy);
581 "Malformated option: '%s'; Should be of the form 'name:value'",
586 DEBUG2("name='%s';val='%s'",name,val);
588 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable);
593 ERROR1("No registrated variable corresponding to '%s'.",name);
594 free(optionlist_cpy);
595 return mismatch_error;
598 free(optionlist_cpy);
602 switch (variable->type) {
603 case xbt_cfgelm_string:
604 errcode = xbt_cfg_set_string(cfg, name, val);
605 if (errcode != no_error) {
606 free(optionlist_cpy);
612 i=strtol(val, &val, 0);
614 free(optionlist_cpy);
616 "Value of option %s not valid. Should be an integer",
620 errcode = xbt_cfg_set_int(cfg,name,i);
621 if (errcode != no_error) {
622 free(optionlist_cpy);
627 case xbt_cfgelm_double:
630 free(optionlist_cpy);
632 "Value of option %s not valid. Should be a double",
636 errcode = xbt_cfg_set_double(cfg,name,d);
637 if (errcode != no_error) {
638 free(optionlist_cpy);
643 case xbt_cfgelm_host:
647 free(optionlist_cpy);
649 "Value of option %s not valid. Should be an host (machine:port)",
654 i=strtol(val, &val, 0);
656 free(optionlist_cpy);
658 "Value of option %s not valid. Should be an host (machine:port)",
662 errcode = xbt_cfg_set_host(cfg,name,str,i);
663 if (errcode != no_error) {
664 free(optionlist_cpy);
670 free(optionlist_cpy);
671 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
675 free(optionlist_cpy);
679 /** @brief Set or add an integer value to \a name within \a cfg
681 * \arg cfg the config set
682 * \arg name the name of the variable
683 * \arg val the value of the variable
686 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
687 xbt_cfgelm_t variable;
690 VERB2("Configuration setting: %s=%d",name,val);
691 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
693 if (variable->max == 1) {
694 if (variable->cb_rm && xbt_dynar_length(variable->content))
695 (*variable->cb_rm)(name, 0);
697 xbt_dynar_set(variable->content,0,&val);
699 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
700 RAISE3(mismatch_error,
701 "Cannot add value %d to the config element %s since it's already full (size=%d)",
702 val,name,variable->max);
704 xbt_dynar_push(variable->content,&val);
707 if (variable->cb_set)
708 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
712 /** @brief Set or add a double value to \a name within \a cfg
714 * \arg cfg the config set
715 * \arg name the name of the variable
716 * \arg val the doule to set
720 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
721 xbt_cfgelm_t variable;
724 VERB2("Configuration setting: %s=%f",name,val);
725 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
727 if (variable->max == 1) {
728 if (variable->cb_rm && xbt_dynar_length(variable->content))
729 (*variable->cb_rm)(name, 0);
731 xbt_dynar_set(variable->content,0,&val);
733 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
734 RAISE3(mismatch_error,
735 "Cannot add value %f to the config element %s since it's already full (size=%d)",
736 val,name,variable->max);
738 xbt_dynar_push(variable->content,&val);
741 if (variable->cb_set)
742 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
746 /** @brief Set or add a string value to \a name within \a cfg
748 * \arg cfg the config set
749 * \arg name the name of the variable
750 * \arg val the value to be added
755 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
756 xbt_cfgelm_t variable;
758 char *newval = xbt_strdup(val);
760 VERB2("Configuration setting: %s=%s",name,val);
761 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
763 if (variable->max == 1) {
764 if (variable->cb_rm && xbt_dynar_length(variable->content))
765 (*variable->cb_rm)(name, 0);
767 xbt_dynar_set(variable->content,0,&newval);
769 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
770 RAISE3(mismatch_error,
771 "Cannot add value %s to the config element %s since it's already full (size=%d)",
772 name,val,variable->max);
774 xbt_dynar_push(variable->content,&newval);
777 if (variable->cb_set)
778 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
782 /** @brief Set or add an host value to \a name within \a cfg
784 * \arg cfg the config set
785 * \arg name the name of the variable
787 * \arg port the port number
789 * \e host values are composed of a string (hostname) and an integer (port)
793 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
794 const char *host,int port) {
795 xbt_cfgelm_t variable;
797 xbt_host_t *val=xbt_new(xbt_host_t,1);
799 VERB3("Configuration setting: %s=%s:%d",name,host,port);
801 val->name = xbt_strdup(name);
804 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
806 if (variable->max == 1) {
807 if (variable->cb_rm && xbt_dynar_length(variable->content))
808 (*variable->cb_rm)(name, 0);
810 xbt_dynar_set(variable->content,0,&val);
812 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
813 RAISE4(mismatch_error,
814 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
815 host,port,name,variable->max);
817 xbt_dynar_push(variable->content,&val);
820 if (variable->cb_set)
821 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
825 /* ---- [ Removing ] ---- */
827 /** @brief Remove the provided \e val integer value from a variable
829 * \arg cfg the config set
830 * \arg name the name of the variable
831 * \arg val the value to be removed
833 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
835 xbt_cfgelm_t variable;
839 if (xbt_dynar_length(variable->content) == variable->min)
840 RAISE3(mismatch_error,
841 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
842 val,name,variable->min);
844 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
846 xbt_dynar_foreach(variable->content,cpt,seen) {
848 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
849 xbt_dynar_cursor_rm(variable->content,&cpt);
854 ERROR2("Can't remove the value %d of config element %s: value not found.",
856 return mismatch_error;
859 /** @brief Remove the provided \e val double value from a variable
861 * \arg cfg the config set
862 * \arg name the name of the variable
863 * \arg val the value to be removed
866 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
867 xbt_cfgelm_t variable;
872 if (xbt_dynar_length(variable->content) == variable->min)
873 RAISE3(mismatch_error,
874 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
875 val,name,variable->min);
877 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
879 xbt_dynar_foreach(variable->content,cpt,seen) {
881 xbt_dynar_cursor_rm(variable->content,&cpt);
882 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
887 ERROR2("Can't remove the value %f of config element %s: value not found.",
889 return mismatch_error;
892 /** @brief Remove the provided \e val string value from a variable
894 * \arg cfg the config set
895 * \arg name the name of the variable
896 * \arg val the value of the string which will be removed
899 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
900 xbt_cfgelm_t variable;
905 if (xbt_dynar_length(variable->content) == variable->min)
906 RAISE3(mismatch_error,
907 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
908 name,val,variable->min);
910 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
912 xbt_dynar_foreach(variable->content,cpt,seen) {
913 if (!strcpy(seen,val)) {
914 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
915 xbt_dynar_cursor_rm(variable->content,&cpt);
920 ERROR2("Can't remove the value %s of config element %s: value not found.",
922 return mismatch_error;
925 /** @brief Remove the provided \e val host value from a variable
927 * \arg cfg the config set
928 * \arg name the name of the variable
929 * \arg host the hostname
930 * \arg port the port number
934 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
935 xbt_cfgelm_t variable;
940 if (xbt_dynar_length(variable->content) == variable->min)
941 RAISE4(mismatch_error,
942 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
943 host,port,name,variable->min);
945 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
947 xbt_dynar_foreach(variable->content,cpt,seen) {
948 if (!strcpy(seen->name,host) && seen->port == port) {
949 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
950 xbt_dynar_cursor_rm(variable->content,&cpt);
955 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
957 return mismatch_error;
960 /** @brief Remove the \e pos th value from the provided variable */
962 xbt_error_t xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
964 xbt_cfgelm_t variable;
969 if (xbt_dynar_length(variable->content) == variable->min)
970 RAISE3(mismatch_error,
971 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
972 pos,name,variable->min);
974 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_any,&variable));
976 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
977 xbt_dynar_remove_at(variable->content, pos, NULL);
979 return mismatch_error;
982 /** @brief Remove all the values from a variable
984 * \arg cfg the config set
985 * \arg name the name of the variable
989 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
990 xbt_cfgelm_t variable;
994 TRYCATCH(mismatch_error,
995 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable));
996 if (errcode == mismatch_error) {
997 ERROR1("Can't empty '%s' since this config element does not exist",
999 return mismatch_error;
1003 if (variable->cb_rm) {
1006 xbt_dynar_foreach(variable->content,cpt,ignored) {
1007 (*variable->cb_rm)(name, cpt);
1010 xbt_dynar_reset(variable->content);
1015 /*----[ Getting ]---------------------------------------------------------*/
1017 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1019 * \arg cfg the config set
1020 * \arg name the name of the variable
1021 * \arg val the wanted value
1023 * Returns the first value from the config set under the given name.
1024 * If there is more than one value, it will issue a warning. Consider using
1025 * xbt_cfg_get_dynar() instead.
1027 * \warning the returned value is the actual content of the config set
1030 xbt_cfg_get_int (xbt_cfg_t cfg,
1033 xbt_cfgelm_t variable;
1034 xbt_error_t errcode;
1036 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
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",
1040 name, xbt_dynar_length(variable->content));
1043 *val = xbt_dynar_get_as(variable->content, 0, int);
1047 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1049 * \arg cfg the config set
1050 * \arg name the name of the variable
1051 * \arg val the wanted value
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.
1057 * \warning the returned value is the actual content of the config set
1061 xbt_cfg_get_double(xbt_cfg_t cfg,
1064 xbt_cfgelm_t variable;
1065 xbt_error_t errcode;
1067 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
1069 if (xbt_dynar_length(variable->content) > 1) {
1070 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1071 name, xbt_dynar_length(variable->content));
1074 *val = xbt_dynar_get_as(variable->content, 0, double);
1078 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1080 * \arg th the config set
1081 * \arg name the name of the variable
1082 * \arg val the wanted value
1084 * Returns the first value from the config set under the given name.
1085 * If there is more than one value, it will issue a warning. Consider using
1086 * xbt_cfg_get_dynar() instead.
1088 * \warning the returned value is the actual content of the config set
1091 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1094 xbt_cfgelm_t variable;
1095 xbt_error_t errcode;
1099 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1101 if (xbt_dynar_length(variable->content) > 1) {
1102 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1103 name, xbt_dynar_length(variable->content));
1106 *val = xbt_dynar_get_as(variable->content, 0, char *);
1110 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1112 * \arg cfg the config set
1113 * \arg name the name of the variable
1114 * \arg host the host
1115 * \arg port the port number
1117 * Returns the first value from the config set under the given name.
1118 * If there is more than one value, it will issue a warning. Consider using
1119 * xbt_cfg_get_dynar() instead.
1121 * \warning the returned value is the actual content of the config set
1124 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1128 xbt_cfgelm_t variable;
1129 xbt_error_t errcode;
1132 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
1134 if (xbt_dynar_length(variable->content) > 1) {
1135 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1136 name, xbt_dynar_length(variable->content));
1139 val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1146 /** @brief Retrieve the dynar of all the values stored in a variable
1148 * \arg cfg where to search in
1149 * \arg name what to search for
1152 * Get the data stored in the config set.
1154 * \warning the returned value is the actual content of the config set
1156 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1158 xbt_dynar_t *dynar) {
1159 xbt_cfgelm_t variable;
1160 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1163 if (errcode == mismatch_error) {
1164 ERROR1("No registered variable %s in this config set",
1166 return mismatch_error;
1168 if (errcode != no_error)
1171 *dynar = variable->content;
1176 /** @brief Retrieve one of the integer value of a variable */
1178 xbt_cfg_get_int_at(xbt_cfg_t cfg,
1183 xbt_cfgelm_t variable;
1184 xbt_error_t errcode;
1186 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1187 *val = xbt_dynar_get_as(variable->content, pos, int);
1191 /** @brief Retrieve one of the double value of a variable */
1193 xbt_cfg_get_double_at(xbt_cfg_t cfg,
1198 xbt_cfgelm_t variable;
1199 xbt_error_t errcode;
1201 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
1202 *val = xbt_dynar_get_as(variable->content, pos, double);
1207 /** @brief Retrieve one of the string value of a variable */
1209 xbt_cfg_get_string_at(xbt_cfg_t cfg,
1214 xbt_cfgelm_t variable;
1215 xbt_error_t errcode;
1217 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1218 *val = xbt_dynar_get_as(variable->content, pos, char*);
1222 /** @brief Retrieve one of the host value of a variable */
1224 xbt_cfg_get_host_at(xbt_cfg_t cfg,
1230 xbt_cfgelm_t variable;
1231 xbt_error_t errcode;
1234 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1235 val = xbt_dynar_get_ptr(variable->content, pos);