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"
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_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
50 e_xbt_cfgelm_type_t type);
52 static void xbt_cfg_str_free(void *d){
55 static void xbt_cfg_host_free(void *d){
56 xbt_host_t *h=(xbt_host_t*) *(void**)d;
58 if (h->name) free(h->name);
63 /*----[ Memory management ]-----------------------------------------------*/
65 /** @brief Constructor
67 * Initialise an config set
71 xbt_cfg_t xbt_cfg_new(void) {
72 return (xbt_cfg_t)xbt_dict_new();
75 /** \brief Copy an existing configuration set
77 * \arg whereto the config set to be created
78 * \arg tocopy the source data
80 * This only copy the registrations, not the actual content
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;
90 xbt_assert0(tocopy,"cannot copy NULL config");
92 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
93 xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
94 variable->cb_set, variable->cb_rm);
98 /** @brief Destructor */
99 void xbt_cfg_free(xbt_cfg_t *cfg) {
100 xbt_dict_free((xbt_dict_t*)cfg);
103 /** @brief Dump a config set for debuging purpose
105 * \arg name The name to give to this config set
106 * \arg indent what to write at the begining of each line (right number of spaces)
107 * \arg cfg the config set
109 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
110 xbt_dict_t dict = (xbt_dict_t) cfg;
111 xbt_dict_cursor_t cursor=NULL;
112 xbt_cfgelm_t variable=NULL;
122 printf("%s>> Dumping of the config set '%s':\n",indent,name);
123 xbt_dict_foreach(dict,cursor,key,variable) {
125 printf("%s %s:",indent,key);
127 size = xbt_dynar_length(variable->content);
128 printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
129 variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
131 variable->cb_rm, variable->cb_set);
133 switch (variable->type) {
136 for (i=0; i<size; i++) {
137 ival = xbt_dynar_get_as(variable->content,i,int);
138 printf ("%s %d\n",indent,ival);
142 case xbt_cfgelm_double:
143 for (i=0; i<size; i++) {
144 dval = xbt_dynar_get_as(variable->content,i,double);
145 printf ("%s %f\n",indent,dval);
149 case xbt_cfgelm_string:
150 for (i=0; i<size; i++) {
151 sval = xbt_dynar_get_as(variable->content,i,char*);
152 printf ("%s %s\n",indent,sval);
156 case xbt_cfgelm_host:
157 for (i=0; i<size; i++) {
158 hval = xbt_dynar_get_as(variable->content,i,xbt_host_t*);
159 printf ("%s %s:%d\n",indent,hval->name,hval->port);
164 printf("%s Invalid type!!\n",indent);
169 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
172 xbt_dict_cursor_free(&cursor);
177 * free an config element
180 void xbt_cfgelm_free(void *data) {
181 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
184 xbt_dynar_free(&(c->content));
188 /*----[ Registering stuff ]-----------------------------------------------*/
190 /** @brief Register an element within a config set
192 * @arg cfg the config set
193 * @arg type the type of the config element
194 * @arg min the minimum
195 * @arg max the maximum
199 xbt_cfg_register(xbt_cfg_t cfg,
200 const char *name, e_xbt_cfgelm_type_t type,
202 xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
208 xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_host,
209 "type of %s not valid (%d should be between %d and %d)",
210 name,type,xbt_cfgelm_int, xbt_cfgelm_host);
211 DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",
212 name,min,max,xbt_cfgelm_type_name[type],type);
214 res = xbt_dict_get((xbt_dict_t)cfg,name);
216 if (e.category == not_found_error) {
225 WARN1("Config elem %s registered twice.",name);
226 /* Will be removed by the insertion of the new one */
229 res=xbt_new(s_xbt_cfgelm_t,1);
234 res->cb_set = cb_set;
239 res->content = xbt_dynar_new(sizeof(int), NULL);
242 case xbt_cfgelm_double:
243 res->content = xbt_dynar_new(sizeof(double), NULL);
246 case xbt_cfgelm_string:
247 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
250 case xbt_cfgelm_host:
251 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
255 ERROR1("%d is an invalide type code",type);
258 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
261 /** @brief Unregister an element from a config set.
263 * @arg cfg the config set
264 * @arg name the name of the elem to be freed
266 * Note that it removes both the description and the actual content.
267 * Throws not_found when no such element exists.
271 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
272 xbt_dict_remove((xbt_dict_t)cfg,name);
276 * @brief Parse a string and register the stuff described.
278 * @arg cfg the config set
279 * @arg entry a string describing the element to register
281 * The string may consist in several variable descriptions separated by a space.
282 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
283 * with type being one of 'string','int', 'host' or 'double'.
287 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
288 char *entrycpy=xbt_strdup(entry);
292 e_xbt_cfgelm_type_t type;
294 tok=strchr(entrycpy, ':');
295 xbt_assert2(tok,"Invalid config element descriptor: %s%s",
297 "; Should be <name>:<min nb>_to_<max nb>_<type>");
300 min=strtol(tok, &tok, 10);
301 xbt_assert1(tok,"Invalid minimum in config element descriptor %s",entry);
303 xbt_assert2(strcmp(tok,"_to_"),
304 "Invalid config element descriptor : %s%s",
306 "; Should be <name>:<min nb>_to_<max nb>_<type>");
307 tok += strlen("_to_");
309 max=strtol(tok, &tok, 10);
310 xbt_assert1(tok,"Invalid maximum in config element descriptor %s",entry);
312 xbt_assert2( *(tok++) =='_',
313 "Invalid config element descriptor: %s%s",entry,
314 "; Should be <name>:<min nb>_to_<max nb>_<type>");
317 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
319 xbt_assert2(type < xbt_cfgelm_type_count,
320 "Invalid type in config element descriptor: %s%s",entry,
321 "; Should be one of 'string', 'int', 'host' or 'double'.");
323 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
325 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
328 /** @brief Check that each variable have the right amount of values */
331 xbt_cfg_check(xbt_cfg_t cfg) {
332 xbt_dict_cursor_t cursor;
333 xbt_cfgelm_t variable;
337 xbt_assert0(cfg,"NULL config set.");
339 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
340 size = xbt_dynar_length(variable->content);
341 if (variable->min > size) {
342 xbt_dict_cursor_free(&cursor);
343 THROW4(mismatch_error,0,
344 "Config elem %s needs at least %d %s, but there is only %d values.",
347 xbt_cfgelm_type_name[variable->type],
351 if (variable->max > 0 && variable->max < size) {
352 xbt_dict_cursor_free(&cursor);
353 THROW4(mismatch_error,0,
354 "Config elem %s accepts at most %d %s, but there is %d values.",
357 xbt_cfgelm_type_name[variable->type],
362 xbt_dict_cursor_free(&cursor);
365 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
367 e_xbt_cfgelm_type_t type){
372 res = xbt_dict_get((xbt_dict_t)cfg,name);
374 if (e.category == not_found_error) {
376 THROW1(not_found_error,0,
377 "No registered variable '%s' in this config set",name);
382 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
383 "You tried to access to the config element %s as an %s, but its type is %s.",
385 xbt_cfgelm_type_name[type],
386 xbt_cfgelm_type_name[res->type]);
391 /** @brief Get the type of this variable in that configuration set
393 * \arg cfg the config set
394 * \arg name the name of the element
395 * \arg type the result
400 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
402 xbt_cfgelm_t variable = NULL;
406 variable = xbt_dict_get((xbt_dict_t)cfg,name);
408 if (e.category == not_found_error) {
410 THROW1(not_found_error,0,
411 "Can't get the type of '%s' since this variable does not exist",name);
416 INFO1("type in variable = %d",variable->type);
418 return variable->type;
421 /*----[ Setting ]---------------------------------------------------------*/
422 /** @brief va_args version of xbt_cfg_set
424 * \arg cfg config set to fill
425 * \arg n variable name
426 * \arg pa variable value
428 * Add some values to the config set.
431 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
435 e_xbt_cfgelm_type_t type;
440 type = xbt_cfg_get_type(cfg,name);
442 if (e.category == not_found_error) {
444 THROW1(not_found_error,0,"Can't set the property '%s' since it's not registered",name);
450 case xbt_cfgelm_host:
451 str = va_arg(pa, char *);
453 xbt_cfg_set_host(cfg,name,str,i);
456 case xbt_cfgelm_string:
457 str=va_arg(pa, char *);
458 xbt_cfg_set_string(cfg, name, str);
463 xbt_cfg_set_int(cfg,name,i);
466 case xbt_cfgelm_double:
468 xbt_cfg_set_double(cfg,name,d);
472 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
476 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
478 * \arg cfg config set to fill
479 * \arg name variable name
480 * \arg varargs variable value
483 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
487 xbt_cfg_set_vargs(cfg,name,pa);
491 /** @brief Add values parsed from a string into a config set
493 * \arg cfg config set to fill
494 * \arg options a string containing the content to add to the config set. This
495 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
496 * like "[name]:[value]" where [name] is the name of an already registred
497 * variable, and [value] conforms to the data type under which this variable was
500 * @todo This is a crude manual parser, it should be a proper lexer.
504 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
511 xbt_cfgelm_t variable;
512 char *optionlist_cpy;
513 char *option, *name,*val;
518 if (!options || !strlen(options)) { /* nothing to do */
521 optionlist_cpy=xbt_strdup(options);
523 DEBUG1("List to parse and set:'%s'",options);
524 option=optionlist_cpy;
525 while (1) { /* breaks in the code */
531 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
532 name,len,(long)(option-name));
535 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
536 DEBUG1("Take %c.",*option);
539 if (option-name == len) {
540 DEBUG0("Boundary=EOL");
541 option=NULL; /* don't do next iteration */
544 DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
545 *option,len,option-name);
547 /* Pass the following blank chars */
549 while (option-name<(len-1) &&
550 (*option == ' ' || *option == '\n' || *option == '\t')) {
551 /* fprintf(stderr,"Ignore a blank char.\n");*/
554 if (option-name == len-1)
555 option=NULL; /* don't do next iteration */
557 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
559 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
564 val=strchr(name,':');
566 free(optionlist_cpy);
568 "Malformated option: '%s'; Should be of the form 'name:value'",
573 DEBUG2("name='%s';val='%s'",name,val);
576 variable = xbt_dict_get((xbt_dict_t)cfg,name);
578 free(optionlist_cpy);
579 if (e.category == not_found_error) {
581 THROW1(not_found_error,0,"No registrated variable corresponding to '%s'.",name);
587 switch (variable->type) {
588 case xbt_cfgelm_string:
589 xbt_cfg_set_string(cfg, name, val); /* throws */
593 i=strtol(val, &val, 0);
595 free(optionlist_cpy);
597 "Value of option %s not valid. Should be an integer",
601 xbt_cfg_set_int(cfg,name,i); /* throws */
604 case xbt_cfgelm_double:
607 free(optionlist_cpy);
609 "Value of option %s not valid. Should be a double",
613 xbt_cfg_set_double(cfg,name,d); /* throws */
616 case xbt_cfgelm_host:
620 free(optionlist_cpy);
622 "Value of option %s not valid. Should be an host (machine:port)",
627 i=strtol(val, &val, 0);
629 free(optionlist_cpy);
631 "Value of option %s not valid. Should be an host (machine:port)",
635 xbt_cfg_set_host(cfg,name,str,i); /* throws */
639 THROW1(unknown_error,0,"Type of config element %s is not valid.",name);
642 free(optionlist_cpy);
646 free(optionlist_cpy);
650 /** @brief Set or add an integer value to \a name within \a cfg
652 * \arg cfg the config set
653 * \arg name the name of the variable
654 * \arg val the value of the variable
657 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
658 xbt_cfgelm_t variable;
660 VERB2("Configuration setting: %s=%d",name,val);
661 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
663 if (variable->max == 1) {
664 if (variable->cb_rm && xbt_dynar_length(variable->content))
665 (*variable->cb_rm)(name, 0);
667 xbt_dynar_set(variable->content,0,&val);
669 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
670 THROW3(mismatch_error,0,
671 "Cannot add value %d to the config element %s since it's already full (size=%d)",
672 val,name,variable->max);
674 xbt_dynar_push(variable->content,&val);
677 if (variable->cb_set)
678 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
681 /** @brief Set or add a double value to \a name within \a cfg
683 * \arg cfg the config set
684 * \arg name the name of the variable
685 * \arg val the doule to set
689 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
690 xbt_cfgelm_t variable;
692 VERB2("Configuration setting: %s=%f",name,val);
693 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
695 if (variable->max == 1) {
696 if (variable->cb_rm && xbt_dynar_length(variable->content))
697 (*variable->cb_rm)(name, 0);
699 xbt_dynar_set(variable->content,0,&val);
701 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
702 THROW3(mismatch_error,0,
703 "Cannot add value %f to the config element %s since it's already full (size=%d)",
704 val,name,variable->max);
706 xbt_dynar_push(variable->content,&val);
709 if (variable->cb_set)
710 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
713 /** @brief Set or add a string value to \a name within \a cfg
715 * \arg cfg the config set
716 * \arg name the name of the variable
717 * \arg val the value to be added
722 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
723 xbt_cfgelm_t variable;
724 char *newval = xbt_strdup(val);
726 VERB2("Configuration setting: %s=%s",name,val);
727 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
729 if (variable->max == 1) {
730 if (variable->cb_rm && xbt_dynar_length(variable->content))
731 (*variable->cb_rm)(name, 0);
733 xbt_dynar_set(variable->content,0,&newval);
735 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
736 THROW3(mismatch_error,0,
737 "Cannot add value %s to the config element %s since it's already full (size=%d)",
738 name,val,variable->max);
740 xbt_dynar_push(variable->content,&newval);
743 if (variable->cb_set)
744 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
747 /** @brief Set or add an host value to \a name within \a cfg
749 * \arg cfg the config set
750 * \arg name the name of the variable
752 * \arg port the port number
754 * \e host values are composed of a string (hostname) and an integer (port)
758 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
759 const char *host,int port) {
760 xbt_cfgelm_t variable;
761 xbt_host_t *val=xbt_new(xbt_host_t,1);
763 VERB3("Configuration setting: %s=%s:%d",name,host,port);
765 val->name = xbt_strdup(name);
768 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
770 if (variable->max == 1) {
771 if (variable->cb_rm && xbt_dynar_length(variable->content))
772 (*variable->cb_rm)(name, 0);
774 xbt_dynar_set(variable->content,0,&val);
776 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
777 THROW4(mismatch_error,0,
778 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
779 host,port,name,variable->max);
781 xbt_dynar_push(variable->content,&val);
784 if (variable->cb_set)
785 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
788 /* ---- [ Removing ] ---- */
790 /** @brief Remove the provided \e val integer value from a variable
792 * \arg cfg the config set
793 * \arg name the name of the variable
794 * \arg val the value to be removed
796 void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
798 xbt_cfgelm_t variable;
801 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
803 if (xbt_dynar_length(variable->content) == variable->min)
804 THROW3(mismatch_error,0,
805 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
806 val,name,variable->min);
808 xbt_dynar_foreach(variable->content,cpt,seen) {
810 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
811 xbt_dynar_cursor_rm(variable->content,&cpt);
816 THROW2(not_found_error,0,
817 "Can't remove the value %d of config element %s: value not found.",val,name);
820 /** @brief Remove the provided \e val double value from a variable
822 * \arg cfg the config set
823 * \arg name the name of the variable
824 * \arg val the value to be removed
827 void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
828 xbt_cfgelm_t variable;
832 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
834 if (xbt_dynar_length(variable->content) == variable->min)
835 THROW3(mismatch_error,0,
836 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
837 val,name,variable->min);
839 xbt_dynar_foreach(variable->content,cpt,seen) {
841 xbt_dynar_cursor_rm(variable->content,&cpt);
843 (*variable->cb_rm)(name, cpt);
848 THROW2(not_found_error,0,
849 "Can't remove the value %f of config element %s: value not found.",val,name);
852 /** @brief Remove the provided \e val string value from a variable
854 * \arg cfg the config set
855 * \arg name the name of the variable
856 * \arg val the value of the string which will be removed
859 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
860 xbt_cfgelm_t variable;
864 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
866 if (xbt_dynar_length(variable->content) == variable->min)
867 THROW3(mismatch_error,0,
868 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
869 name,val,variable->min);
871 xbt_dynar_foreach(variable->content,cpt,seen) {
872 if (!strcpy(seen,val)) {
874 (*variable->cb_rm)(name, cpt);
875 xbt_dynar_cursor_rm(variable->content,&cpt);
880 THROW2(not_found_error,0,
881 "Can't remove the value %s of config element %s: value not found.",val,name);
884 /** @brief Remove the provided \e val host value from a variable
886 * \arg cfg the config set
887 * \arg name the name of the variable
888 * \arg host the hostname
889 * \arg port the port number
893 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
894 xbt_cfgelm_t variable;
898 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
900 if (xbt_dynar_length(variable->content) == variable->min)
901 THROW4(mismatch_error,0,
902 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
903 host,port,name,variable->min);
905 xbt_dynar_foreach(variable->content,cpt,seen) {
906 if (!strcpy(seen->name,host) && seen->port == port) {
907 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
908 xbt_dynar_cursor_rm(variable->content,&cpt);
913 THROW3(not_found_error,0,
914 "Can't remove the value %s:%d of config element %s: value not found.",
918 /** @brief Remove the \e pos th value from the provided variable */
920 void xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
922 xbt_cfgelm_t variable;
924 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_any);
926 if (xbt_dynar_length(variable->content) == variable->min)
927 THROW3(mismatch_error,0,
928 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
929 pos,name,variable->min);
931 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
932 xbt_dynar_remove_at(variable->content, pos, NULL);
935 /** @brief Remove all the values from a variable
937 * \arg cfg the config set
938 * \arg name the name of the variable
942 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
943 xbt_cfgelm_t variable;
947 variable = xbt_dict_get((xbt_dict_t)cfg,name);
949 if (e.category != not_found_error)
953 THROW1(not_found_error,0,
954 "Can't empty '%s' since this config element does not exist", name);
958 if (variable->cb_rm) {
961 xbt_dynar_foreach(variable->content,cpt,ignored) {
962 (*variable->cb_rm)(name, cpt);
965 xbt_dynar_reset(variable->content);
969 /*----[ Getting ]---------------------------------------------------------*/
971 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
973 * \arg cfg the config set
974 * \arg name the name of the variable
975 * \arg val the wanted value
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.
981 * \warning the returned value is the actual content of the config set
984 xbt_cfg_get_int (xbt_cfg_t cfg, const char *name) {
985 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
987 if (xbt_dynar_length(variable->content) > 1) {
988 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
989 name, xbt_dynar_length(variable->content));
992 return xbt_dynar_get_as(variable->content, 0, int);
995 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
997 * \arg cfg the config set
998 * \arg name the name of the variable
999 * \arg val the wanted value
1001 * Returns the first value from the config set under the given name.
1002 * If there is more than one value, it will issue a warning. Consider using
1003 * xbt_cfg_get_dynar() instead.
1005 * \warning the returned value is the actual content of the config set
1009 xbt_cfg_get_double(xbt_cfg_t cfg, const char *name) {
1010 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1012 if (xbt_dynar_length(variable->content) > 1) {
1013 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1014 name, xbt_dynar_length(variable->content));
1017 return xbt_dynar_get_as(variable->content, 0, double);
1020 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1022 * \arg th the config set
1023 * \arg name the name of the variable
1024 * \arg val the wanted value
1026 * Returns the first value from the config set under the given name.
1027 * If there is more than one value, it will issue a warning. Consider using
1028 * xbt_cfg_get_dynar() instead.
1030 * \warning the returned value is the actual content of the config set
1033 char* xbt_cfg_get_string(xbt_cfg_t cfg, const char *name) {
1034 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1036 if (xbt_dynar_length(variable->content) > 1) {
1037 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1038 name, xbt_dynar_length(variable->content));
1041 return xbt_dynar_get_as(variable->content, 0, char *);
1044 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1046 * \arg cfg the config set
1047 * \arg name the name of the variable
1048 * \arg host the host
1049 * \arg port the port number
1051 * Returns the first value from the config set under the given name.
1052 * If there is more than one value, it will issue a warning. Consider using
1053 * xbt_cfg_get_dynar() instead.
1055 * \warning the returned value is the actual content of the config set
1058 void xbt_cfg_get_host (xbt_cfg_t cfg, const char *name,
1059 char **host, int *port) {
1060 xbt_cfgelm_t variable;
1063 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
1065 if (xbt_dynar_length(variable->content) > 1) {
1066 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1067 name, xbt_dynar_length(variable->content));
1070 val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1075 /** @brief Retrieve the dynar of all the values stored in a variable
1077 * \arg cfg where to search in
1078 * \arg name what to search for
1081 * Get the data stored in the config set.
1083 * \warning the returned value is the actual content of the config set
1085 xbt_dynar_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name) {
1086 xbt_cfgelm_t variable;
1090 variable = xbt_dict_get((xbt_dict_t)cfg,name);
1092 if (e.category == not_found_error) {
1094 THROW1(not_found_error,0,
1095 "No registered variable %s in this config set",name);
1100 return variable->content;
1104 /** @brief Retrieve one of the integer value of a variable */
1106 xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos) {
1108 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1109 return xbt_dynar_get_as(variable->content, pos, int);
1112 /** @brief Retrieve one of the double value of a variable */
1114 xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos) {
1116 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1117 return xbt_dynar_get_as(variable->content, pos, double);
1121 /** @brief Retrieve one of the string value of a variable */
1123 xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos) {
1125 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1126 return xbt_dynar_get_as(variable->content, pos, char*);
1129 /** @brief Retrieve one of the host value of a variable */
1131 xbt_cfg_get_host_at(xbt_cfg_t cfg, const char *name, int pos,
1132 char **host, int *port) {
1134 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1135 xbt_host_t *val = xbt_dynar_get_ptr(variable->content, pos);