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 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,xbt_cfgelm_type_name[type]);
208 errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
210 if (errcode == no_error) {
211 WARN1("Config elem %s registered twice.",name);
212 /* Will be removed by the insertion of the new one */
214 xbt_assert_error(mismatch_error);
216 res=xbt_new(s_xbt_cfgelm_t,1);
221 res->cb_set = cb_set;
226 res->content = xbt_dynar_new(sizeof(int), NULL);
229 case xbt_cfgelm_double:
230 res->content = xbt_dynar_new(sizeof(double), NULL);
233 case xbt_cfgelm_string:
234 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
237 case xbt_cfgelm_host:
238 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
242 ERROR1("%d is an invalide type code",type);
245 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
248 /** @brief Unregister an element from a config set.
250 * @arg cfg the config set
251 * @arg name the name of the elem to be freed
253 * Note that it removes both the description and the actual content.
257 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
258 return xbt_dict_remove((xbt_dict_t)cfg,name);
262 * @brief Parse a string and register the stuff described.
264 * @arg cfg the config set
265 * @arg entry a string describing the element to register
267 * The string may consist in several variable descriptions separated by a space.
268 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
269 * with type being one of 'string','int', 'host' or 'double'.
273 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
274 char *entrycpy=xbt_strdup(entry);
278 e_xbt_cfgelm_type_t type;
280 tok=strchr(entrycpy, ':');
283 "Invalid config element descriptor: ",entry,
284 "; Should be <name>:<min nb>_to_<max nb>_<type>");
290 min=strtol(tok, &tok, 10);
292 ERROR1("Invalid minimum in config element descriptor %s",entry);
297 if (!strcmp(tok,"_to_")){
299 "Invalid config element descriptor: ",entry,
300 "; Should be <name>:<min nb>_to_<max nb>_<type>");
304 tok += strlen("_to_");
306 max=strtol(tok, &tok, 10);
308 ERROR1("Invalid maximum in config element descriptor %s",entry);
315 "Invalid config element descriptor: ",entry,
316 "; Should be <name>:<min nb>_to_<max nb>_<type>");
322 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
324 if (type == xbt_cfgelm_type_count) {
326 "Invalid type in config element descriptor: ",entry,
327 "; Should be one of 'string', 'int', 'host' or 'double'.");
332 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
334 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
338 /** @brief Check that each variable have the right amount of values */
341 xbt_cfg_check(xbt_cfg_t cfg) {
342 xbt_dict_cursor_t cursor;
343 xbt_cfgelm_t variable;
347 xbt_assert0(cfg,"NULL config set.");
349 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
350 size = xbt_dynar_length(variable->content);
351 if (variable->min > size) {
352 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
355 xbt_cfgelm_type_name[variable->type],
357 xbt_dict_cursor_free(&cursor);
358 return mismatch_error;
361 if (variable->max > 0 && variable->max < size) {
362 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
365 xbt_cfgelm_type_name[variable->type],
367 xbt_dict_cursor_free(&cursor);
368 return mismatch_error;
373 xbt_dict_cursor_free(&cursor);
377 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg,
379 e_xbt_cfgelm_type_t type,
380 /* OUT */ xbt_cfgelm_t *whereto){
382 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
385 if (errcode == mismatch_error) {
386 RAISE1(mismatch_error,
387 "No registered variable '%s' in this config set",
390 if (errcode != no_error)
393 xbt_assert3(type == xbt_cfgelm_any || (*whereto)->type == type,
394 "You tried to access to the config element %s as an %s, but its type is %s.",
396 xbt_cfgelm_type_name[type],
397 xbt_cfgelm_type_name[(*whereto)->type]);
402 /** @brief Get the type of this variable in that configuration set
404 * \arg cfg the config set
405 * \arg name the name of the element
406 * \arg type the result
411 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name,
412 /* OUT */e_xbt_cfgelm_type_t *type) {
414 xbt_cfgelm_t variable;
417 TRYCATCH(mismatch_error,xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable));
419 if (errcode == mismatch_error)
420 RAISE1(mismatch_error,"Can't get the type of '%s' since this variable does not exist",
423 *type=variable->type;
428 /*----[ Setting ]---------------------------------------------------------*/
429 /** @brief va_args version of xbt_cfg_set
431 * \arg cfg config set to fill
432 * \arg varargs NULL-terminated list of pairs {(const char*)key, value}
434 * Add some values to the config set.
435 * \warning if the list isn't NULL terminated, it will segfault.
438 xbt_cfg_set_vargs(xbt_cfg_t cfg, va_list pa) {
442 e_xbt_cfgelm_type_t type;
446 while ((name=va_arg(pa,char *))) {
448 if (!xbt_cfg_get_type(cfg,name,&type)) {
449 ERROR1("Can't set the property '%s' since it's not registered",name);
450 return mismatch_error;
454 case xbt_cfgelm_host:
455 str = va_arg(pa, char *);
457 TRY(xbt_cfg_set_host(cfg,name,str,i));
460 case xbt_cfgelm_string:
461 str=va_arg(pa, char *);
462 TRY(xbt_cfg_set_string(cfg, name, str));
467 TRY(xbt_cfg_set_int(cfg,name,i));
470 case xbt_cfgelm_double:
472 TRY(xbt_cfg_set_double(cfg,name,d));
476 RAISE1(unknown_error,"Config element variable %s not valid.",name);
482 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
484 * \arg cfg config set to fill
485 * \arg varargs NULL-terminated list of pairs {(const char*)key, value}
487 * \warning if the list isn't NULL terminated, it will segfault.
489 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, ...) {
494 errcode=xbt_cfg_set_vargs(cfg,pa);
499 /** @brief Add values parsed from a string into a config set
501 * \arg cfg config set to fill
502 * \arg options a string containing the content to add to the config set. This
503 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
504 * like "[name]:[value]" where [name] is the name of an already registred
505 * variable, and [value] conforms to the data type under which this variable was
508 * @todo This is a crude manual parser, it should be a proper lexer.
512 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
517 xbt_cfgelm_t variable;
518 char *optionlist_cpy;
519 char *option, *name,*val;
525 if (!options || !strlen(options)) { /* nothing to do */
528 optionlist_cpy=xbt_strdup(options);
530 DEBUG1("List to parse and set:'%s'",options);
531 option=optionlist_cpy;
532 while (1) { /* breaks in the code */
538 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
539 name,len,(long)(option-name));
542 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
543 DEBUG1("Take %c.",*option);
546 if (option-name == len) {
547 DEBUG0("Boundary=EOL");
548 option=NULL; /* don't do next iteration */
551 DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
552 *option,len,option-name);
554 /* Pass the following blank chars */
556 while (option-name<(len-1) &&
557 (*option == ' ' || *option == '\n' || *option == '\t')) {
558 /* fprintf(stderr,"Ignore a blank char.\n");*/
561 if (option-name == len-1)
562 option=NULL; /* don't do next iteration */
564 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
566 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
571 val=strchr(name,':');
573 free(optionlist_cpy);
575 "Malformated option: '%s'; Should be of the form 'name:value'",
580 DEBUG2("name='%s';val='%s'",name,val);
582 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable);
587 ERROR1("No registrated variable corresponding to '%s'.",name);
588 free(optionlist_cpy);
589 return mismatch_error;
592 free(optionlist_cpy);
596 switch (variable->type) {
597 case xbt_cfgelm_string:
598 errcode = xbt_cfg_set_string(cfg, name, val);
599 if (errcode != no_error) {
600 free(optionlist_cpy);
606 i=strtol(val, &val, 0);
608 free(optionlist_cpy);
610 "Value of option %s not valid. Should be an integer",
614 errcode = xbt_cfg_set_int(cfg,name,i);
615 if (errcode != no_error) {
616 free(optionlist_cpy);
621 case xbt_cfgelm_double:
624 free(optionlist_cpy);
626 "Value of option %s not valid. Should be a double",
630 errcode = xbt_cfg_set_double(cfg,name,d);
631 if (errcode != no_error) {
632 free(optionlist_cpy);
637 case xbt_cfgelm_host:
641 free(optionlist_cpy);
643 "Value of option %s not valid. Should be an host (machine:port)",
648 i=strtol(val, &val, 0);
650 free(optionlist_cpy);
652 "Value of option %s not valid. Should be an host (machine:port)",
656 errcode = xbt_cfg_set_host(cfg,name,str,i);
657 if (errcode != no_error) {
658 free(optionlist_cpy);
664 free(optionlist_cpy);
665 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
669 free(optionlist_cpy);
673 /** @brief Set or add an integer value to \a name within \a cfg
675 * \arg cfg the config set
676 * \arg name the name of the variable
677 * \arg val the value of the variable
680 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
681 xbt_cfgelm_t variable;
684 VERB2("Configuration setting: %s=%d",name,val);
685 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
687 if (variable->max == 1) {
688 if (variable->cb_rm && xbt_dynar_length(variable->content))
689 (*variable->cb_rm)(name, 0);
691 xbt_dynar_set(variable->content,0,&val);
693 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
694 RAISE3(mismatch_error,
695 "Cannot add value %d to the config element %s since it's already full (size=%d)",
696 val,name,variable->max);
698 xbt_dynar_push(variable->content,&val);
701 if (variable->cb_set)
702 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
706 /** @brief Set or add a double value to \a name within \a cfg
708 * \arg cfg the config set
709 * \arg name the name of the variable
710 * \arg val the doule to set
714 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
715 xbt_cfgelm_t variable;
718 VERB2("Configuration setting: %s=%f",name,val);
719 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
721 if (variable->max == 1) {
722 if (variable->cb_rm && xbt_dynar_length(variable->content))
723 (*variable->cb_rm)(name, 0);
725 xbt_dynar_set(variable->content,0,&val);
727 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
728 RAISE3(mismatch_error,
729 "Cannot add value %f to the config element %s since it's already full (size=%d)",
730 val,name,variable->max);
732 xbt_dynar_push(variable->content,&val);
735 if (variable->cb_set)
736 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
740 /** @brief Set or add a string value to \a name within \a cfg
742 * \arg cfg the config set
743 * \arg name the name of the variable
744 * \arg val the value to be added
749 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
750 xbt_cfgelm_t variable;
752 char *newval = xbt_strdup(val);
754 VERB2("Configuration setting: %s=%s",name,val);
755 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
757 if (variable->max == 1) {
758 if (variable->cb_rm && xbt_dynar_length(variable->content))
759 (*variable->cb_rm)(name, 0);
761 xbt_dynar_set(variable->content,0,&newval);
763 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
764 RAISE3(mismatch_error,
765 "Cannot add value %s to the config element %s since it's already full (size=%d)",
766 name,val,variable->max);
768 xbt_dynar_push(variable->content,&newval);
771 if (variable->cb_set)
772 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
776 /** @brief Set or add an host value to \a name within \a cfg
778 * \arg cfg the config set
779 * \arg name the name of the variable
781 * \arg port the port number
783 * \e host values are composed of a string (hostname) and an integer (port)
787 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
788 const char *host,int port) {
789 xbt_cfgelm_t variable;
791 xbt_host_t *val=xbt_new(xbt_host_t,1);
793 VERB3("Configuration setting: %s=%s:%d",name,host,port);
795 val->name = xbt_strdup(name);
798 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
800 if (variable->max == 1) {
801 if (variable->cb_rm && xbt_dynar_length(variable->content))
802 (*variable->cb_rm)(name, 0);
804 xbt_dynar_set(variable->content,0,&val);
806 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
807 RAISE4(mismatch_error,
808 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
809 host,port,name,variable->max);
811 xbt_dynar_push(variable->content,&val);
814 if (variable->cb_set)
815 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
819 /* ---- [ Removing ] ---- */
821 /** @brief Remove the provided \e val integer value from a variable
823 * \arg cfg the config set
824 * \arg name the name of the variable
825 * \arg val the value to be removed
827 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
829 xbt_cfgelm_t variable;
833 if (xbt_dynar_length(variable->content) == variable->min)
834 RAISE3(mismatch_error,
835 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
836 val,name,variable->min);
838 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
840 xbt_dynar_foreach(variable->content,cpt,seen) {
842 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
843 xbt_dynar_cursor_rm(variable->content,&cpt);
848 ERROR2("Can't remove the value %d of config element %s: value not found.",
850 return mismatch_error;
853 /** @brief Remove the provided \e val double value from a variable
855 * \arg cfg the config set
856 * \arg name the name of the variable
857 * \arg val the value to be removed
860 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
861 xbt_cfgelm_t variable;
866 if (xbt_dynar_length(variable->content) == variable->min)
867 RAISE3(mismatch_error,
868 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
869 val,name,variable->min);
871 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
873 xbt_dynar_foreach(variable->content,cpt,seen) {
875 xbt_dynar_cursor_rm(variable->content,&cpt);
876 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
881 ERROR2("Can't remove the value %f of config element %s: value not found.",
883 return mismatch_error;
886 /** @brief Remove the provided \e val string value from a variable
888 * \arg cfg the config set
889 * \arg name the name of the variable
890 * \arg val the value of the string which will be removed
893 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
894 xbt_cfgelm_t variable;
899 if (xbt_dynar_length(variable->content) == variable->min)
900 RAISE3(mismatch_error,
901 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
902 name,val,variable->min);
904 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
906 xbt_dynar_foreach(variable->content,cpt,seen) {
907 if (!strcpy(seen,val)) {
908 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
909 xbt_dynar_cursor_rm(variable->content,&cpt);
914 ERROR2("Can't remove the value %s of config element %s: value not found.",
916 return mismatch_error;
919 /** @brief Remove the provided \e val host value from a variable
921 * \arg cfg the config set
922 * \arg name the name of the variable
923 * \arg host the hostname
924 * \arg port the port number
928 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
929 xbt_cfgelm_t variable;
934 if (xbt_dynar_length(variable->content) == variable->min)
935 RAISE4(mismatch_error,
936 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
937 host,port,name,variable->min);
939 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
941 xbt_dynar_foreach(variable->content,cpt,seen) {
942 if (!strcpy(seen->name,host) && seen->port == port) {
943 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
944 xbt_dynar_cursor_rm(variable->content,&cpt);
949 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
951 return mismatch_error;
954 /** @brief Remove the \e pos th value from the provided variable */
956 xbt_error_t xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
958 xbt_cfgelm_t variable;
963 if (xbt_dynar_length(variable->content) == variable->min)
964 RAISE3(mismatch_error,
965 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
966 pos,name,variable->min);
968 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_any,&variable));
970 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
971 xbt_dynar_remove_at(variable->content, pos, NULL);
973 return mismatch_error;
976 /** @brief Remove all the values from a variable
978 * \arg cfg the config set
979 * \arg name the name of the variable
983 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
984 xbt_cfgelm_t variable;
988 TRYCATCH(mismatch_error,
989 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&variable));
990 if (errcode == mismatch_error) {
991 ERROR1("Can't empty '%s' since this config element does not exist",
993 return mismatch_error;
997 if (variable->cb_rm) {
1000 xbt_dynar_foreach(variable->content,cpt,ignored) {
1001 (*variable->cb_rm)(name, cpt);
1004 xbt_dynar_reset(variable->content);
1009 /*----[ Getting ]---------------------------------------------------------*/
1011 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1013 * \arg cfg the config set
1014 * \arg name the name of the variable
1015 * \arg val the wanted value
1017 * Returns the first value from the config set under the given name.
1018 * If there is more than one value, it will issue a warning. Consider using
1019 * xbt_cfg_get_dynar() instead.
1021 * \warning the returned value is the actual content of the config set
1024 xbt_cfg_get_int (xbt_cfg_t cfg,
1027 xbt_cfgelm_t variable;
1028 xbt_error_t errcode;
1030 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1032 if (xbt_dynar_length(variable->content) > 1) {
1033 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
1034 name, xbt_dynar_length(variable->content));
1037 *val = xbt_dynar_get_as(variable->content, 0, int);
1041 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1043 * \arg cfg the config set
1044 * \arg name the name of the variable
1045 * \arg val the wanted value
1047 * Returns the first value from the config set under the given name.
1048 * If there is more than one value, it will issue a warning. Consider using
1049 * xbt_cfg_get_dynar() instead.
1051 * \warning the returned value is the actual content of the config set
1055 xbt_cfg_get_double(xbt_cfg_t cfg,
1058 xbt_cfgelm_t variable;
1059 xbt_error_t errcode;
1061 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
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));
1068 *val = xbt_dynar_get_as(variable->content, 0, double);
1072 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1074 * \arg th the config set
1075 * \arg name the name of the variable
1076 * \arg val the wanted value
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.
1082 * \warning the returned value is the actual content of the config set
1085 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1088 xbt_cfgelm_t variable;
1089 xbt_error_t errcode;
1093 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1095 if (xbt_dynar_length(variable->content) > 1) {
1096 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1097 name, xbt_dynar_length(variable->content));
1100 *val = xbt_dynar_get_as(variable->content, 0, char *);
1104 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1106 * \arg cfg the config set
1107 * \arg name the name of the variable
1108 * \arg host the host
1109 * \arg port the port number
1111 * Returns the first value from the config set under the given name.
1112 * If there is more than one value, it will issue a warning. Consider using
1113 * xbt_cfg_get_dynar() instead.
1115 * \warning the returned value is the actual content of the config set
1118 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1122 xbt_cfgelm_t variable;
1123 xbt_error_t errcode;
1126 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&variable));
1128 if (xbt_dynar_length(variable->content) > 1) {
1129 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1130 name, xbt_dynar_length(variable->content));
1133 val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1140 /** @brief Retrieve the dynar of all the values stored in a variable
1142 * \arg cfg where to search in
1143 * \arg name what to search for
1146 * Get the data stored in the config set.
1148 * \warning the returned value is the actual content of the config set
1150 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1152 xbt_dynar_t *dynar) {
1153 xbt_cfgelm_t variable;
1154 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1157 if (errcode == mismatch_error) {
1158 ERROR1("No registered variable %s in this config set",
1160 return mismatch_error;
1162 if (errcode != no_error)
1165 *dynar = variable->content;
1170 /** @brief Retrieve one of the integer value of a variable */
1172 xbt_cfg_get_int_at(xbt_cfg_t cfg,
1177 xbt_cfgelm_t variable;
1178 xbt_error_t errcode;
1180 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1181 *val = xbt_dynar_get_as(variable->content, pos, int);
1185 /** @brief Retrieve one of the double value of a variable */
1187 xbt_cfg_get_double_at(xbt_cfg_t cfg,
1192 xbt_cfgelm_t variable;
1193 xbt_error_t errcode;
1195 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&variable));
1196 *val = xbt_dynar_get_as(variable->content, pos, double);
1201 /** @brief Retrieve one of the string value of a variable */
1203 xbt_cfg_get_string_at(xbt_cfg_t cfg,
1208 xbt_cfgelm_t variable;
1209 xbt_error_t errcode;
1211 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&variable));
1212 *val = xbt_dynar_get_as(variable->content, pos, char*);
1216 /** @brief Retrieve one of the host value of a variable */
1218 xbt_cfg_get_host_at(xbt_cfg_t cfg,
1224 xbt_cfgelm_t variable;
1225 xbt_error_t errcode;
1228 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&variable));
1229 val = xbt_dynar_get_ptr(variable->content, pos);