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. */
12 #include <stdio.h> /* FIXME: killme */
15 #include "xbt/sysdep.h"
18 #include "xbt/dynar.h"
21 #include "xbt/config.h" /* prototypes of this module */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
25 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
27 Both data and DTD are mixed, but fixing it now would prevent me to ever
31 /* Allowed type of the variable */
32 e_xbt_cfgelm_type_t type;
40 (cannot be an union because type host uses both str and i) */
42 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
44 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
45 {"int","double","string","host","any"};
47 /* Internal stuff used in cache to free a variable */
48 static void xbt_cfgelm_free(void *data);
50 /* Retrieve the variable we'll modify */
51 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
52 e_xbt_cfgelm_type_t type);
54 static void xbt_cfg_str_free(void *d){
57 static void xbt_cfg_host_free(void *d){
58 xbt_host_t *h=(xbt_host_t*) *(void**)d;
60 if (h->name) free(h->name);
65 /*----[ Memory management ]-----------------------------------------------*/
67 /** @brief Constructor
69 * Initialise an config set
73 xbt_cfg_t xbt_cfg_new(void) {
74 return (xbt_cfg_t)xbt_dict_new();
77 /** \brief Copy an existing configuration set
79 * \arg whereto the config set to be created
80 * \arg tocopy the source data
82 * This only copy the registrations, not the actual content
86 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
87 xbt_dict_cursor_t cursor=NULL;
88 xbt_cfgelm_t variable=NULL;
92 xbt_assert0(tocopy,"cannot copy NULL config");
94 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
95 xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
96 variable->cb_set, variable->cb_rm);
100 /** @brief Destructor */
101 void xbt_cfg_free(xbt_cfg_t *cfg) {
102 xbt_dict_free((xbt_dict_t*)cfg);
105 /** @brief Dump a config set for debuging purpose
107 * \arg name The name to give to this config set
108 * \arg indent what to write at the begining of each line (right number of spaces)
109 * \arg cfg the config set
111 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
112 xbt_dict_t dict = (xbt_dict_t) cfg;
113 xbt_dict_cursor_t cursor=NULL;
114 xbt_cfgelm_t variable=NULL;
124 printf("%s>> Dumping of the config set '%s':\n",indent,name);
125 xbt_dict_foreach(dict,cursor,key,variable) {
127 printf("%s %s:",indent,key);
129 size = xbt_dynar_length(variable->content);
130 printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
131 variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
133 variable->cb_rm, variable->cb_set);
135 switch (variable->type) {
138 for (i=0; i<size; i++) {
139 ival = xbt_dynar_get_as(variable->content,i,int);
140 printf ("%s %d\n",indent,ival);
144 case xbt_cfgelm_double:
145 for (i=0; i<size; i++) {
146 dval = xbt_dynar_get_as(variable->content,i,double);
147 printf ("%s %f\n",indent,dval);
151 case xbt_cfgelm_string:
152 for (i=0; i<size; i++) {
153 sval = xbt_dynar_get_as(variable->content,i,char*);
154 printf ("%s %s\n",indent,sval);
158 case xbt_cfgelm_host:
159 for (i=0; i<size; i++) {
160 hval = xbt_dynar_get_as(variable->content,i,xbt_host_t*);
161 printf ("%s %s:%d\n",indent,hval->name,hval->port);
166 printf("%s Invalid type!!\n",indent);
171 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
174 xbt_dict_cursor_free(&cursor);
179 * free an config element
182 void xbt_cfgelm_free(void *data) {
183 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
186 xbt_dynar_free(&(c->content));
190 /*----[ Registering stuff ]-----------------------------------------------*/
192 /** @brief Register an element within a config set
194 * @arg cfg the config set
195 * @arg type the type of the config element
196 * @arg min the minimum
197 * @arg max the maximum
201 xbt_cfg_register(xbt_cfg_t cfg,
202 const char *name, e_xbt_cfgelm_type_t type,
204 xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
210 xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_host,
211 "type of %s not valid (%d should be between %d and %d)",
212 name,type,xbt_cfgelm_int, xbt_cfgelm_host);
213 DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",name,min,max,xbt_cfgelm_type_name[type],type);
215 res = xbt_dict_get((xbt_dict_t)cfg,name);
217 if (e.category == not_found_error) {
226 WARN1("Config elem %s registered twice.",name);
227 /* Will be removed by the insertion of the new one */
230 res=xbt_new(s_xbt_cfgelm_t,1);
235 res->cb_set = cb_set;
240 res->content = xbt_dynar_new(sizeof(int), NULL);
243 case xbt_cfgelm_double:
244 res->content = xbt_dynar_new(sizeof(double), NULL);
247 case xbt_cfgelm_string:
248 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
251 case xbt_cfgelm_host:
252 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
256 ERROR1("%d is an invalide type code",type);
259 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
262 /** @brief Unregister an element from a config set.
264 * @arg cfg the config set
265 * @arg name the name of the elem to be freed
267 * Note that it removes both the description and the actual content.
268 * Throws not_found when no such element exists.
272 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
273 xbt_dict_remove((xbt_dict_t)cfg,name);
277 * @brief Parse a string and register the stuff described.
279 * @arg cfg the config set
280 * @arg entry a string describing the element to register
282 * The string may consist in several variable descriptions separated by a space.
283 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
284 * with type being one of 'string','int', 'host' or 'double'.
288 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
289 char *entrycpy=xbt_strdup(entry);
293 e_xbt_cfgelm_type_t type;
295 tok=strchr(entrycpy, ':');
298 "Invalid config element descriptor: ",entry,
299 "; Should be <name>:<min nb>_to_<max nb>_<type>");
305 min=strtol(tok, &tok, 10);
307 ERROR1("Invalid minimum in config element descriptor %s",entry);
312 if (!strcmp(tok,"_to_")){
314 "Invalid config element descriptor: ",entry,
315 "; Should be <name>:<min nb>_to_<max nb>_<type>");
319 tok += strlen("_to_");
321 max=strtol(tok, &tok, 10);
323 ERROR1("Invalid maximum in config element descriptor %s",entry);
330 "Invalid config element descriptor: ",entry,
331 "; Should be <name>:<min nb>_to_<max nb>_<type>");
337 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
339 if (type == xbt_cfgelm_type_count) {
341 "Invalid type in config element descriptor: ",entry,
342 "; Should be one of 'string', 'int', 'host' or 'double'.");
347 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
349 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
353 /** @brief Check that each variable have the right amount of values */
356 xbt_cfg_check(xbt_cfg_t cfg) {
357 xbt_dict_cursor_t cursor;
358 xbt_cfgelm_t variable;
362 xbt_assert0(cfg,"NULL config set.");
364 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
365 size = xbt_dynar_length(variable->content);
366 if (variable->min > size) {
367 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
370 xbt_cfgelm_type_name[variable->type],
372 xbt_dict_cursor_free(&cursor);
373 return mismatch_error;
376 if (variable->max > 0 && variable->max < size) {
377 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
380 xbt_cfgelm_type_name[variable->type],
382 xbt_dict_cursor_free(&cursor);
383 return mismatch_error;
388 xbt_dict_cursor_free(&cursor);
392 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
394 e_xbt_cfgelm_type_t type){
399 res = xbt_dict_get((xbt_dict_t)cfg,name);
401 if (e.category == not_found_error) {
403 THROW1(not_found_error,0,
404 "No registered variable '%s' in this config set",name);
409 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
410 "You tried to access to the config element %s as an %s, but its type is %s.",
412 xbt_cfgelm_type_name[type],
413 xbt_cfgelm_type_name[res->type]);
418 /** @brief Get the type of this variable in that configuration set
420 * \arg cfg the config set
421 * \arg name the name of the element
422 * \arg type the result
427 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
429 xbt_cfgelm_t variable = NULL;
433 variable = xbt_dict_get((xbt_dict_t)cfg,name);
435 if (e.category == not_found_error) {
437 THROW1(not_found_error,0,
438 "Can't get the type of '%s' since this variable does not exist",name);
443 INFO1("type in variable = %d",variable->type);
445 return variable->type;
448 /*----[ Setting ]---------------------------------------------------------*/
449 /** @brief va_args version of xbt_cfg_set
451 * \arg cfg config set to fill
452 * \arg n variable name
453 * \arg pa variable value
455 * Add some values to the config set.
458 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
462 e_xbt_cfgelm_type_t type;
467 type = xbt_cfg_get_type(cfg,name);
469 if (e.category == not_found_error) {
471 THROW1(not_found_error,0,"Can't set the property '%s' since it's not registered",name);
477 case xbt_cfgelm_host:
478 str = va_arg(pa, char *);
480 xbt_cfg_set_host(cfg,name,str,i);
483 case xbt_cfgelm_string:
484 str=va_arg(pa, char *);
485 xbt_cfg_set_string(cfg, name, str);
490 xbt_cfg_set_int(cfg,name,i);
493 case xbt_cfgelm_double:
495 xbt_cfg_set_double(cfg,name,d);
499 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
503 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
505 * \arg cfg config set to fill
506 * \arg name variable name
507 * \arg varargs variable value
510 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
514 xbt_cfg_set_vargs(cfg,name,pa);
518 /** @brief Add values parsed from a string into a config set
520 * \arg cfg config set to fill
521 * \arg options a string containing the content to add to the config set. This
522 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
523 * like "[name]:[value]" where [name] is the name of an already registred
524 * variable, and [value] conforms to the data type under which this variable was
527 * @todo This is a crude manual parser, it should be a proper lexer.
531 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
538 xbt_cfgelm_t variable;
539 char *optionlist_cpy;
540 char *option, *name,*val;
545 if (!options || !strlen(options)) { /* nothing to do */
548 optionlist_cpy=xbt_strdup(options);
550 DEBUG1("List to parse and set:'%s'",options);
551 option=optionlist_cpy;
552 while (1) { /* breaks in the code */
558 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
559 name,len,(long)(option-name));
562 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
563 DEBUG1("Take %c.",*option);
566 if (option-name == len) {
567 DEBUG0("Boundary=EOL");
568 option=NULL; /* don't do next iteration */
571 DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
572 *option,len,option-name);
574 /* Pass the following blank chars */
576 while (option-name<(len-1) &&
577 (*option == ' ' || *option == '\n' || *option == '\t')) {
578 /* fprintf(stderr,"Ignore a blank char.\n");*/
581 if (option-name == len-1)
582 option=NULL; /* don't do next iteration */
584 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
586 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
591 val=strchr(name,':');
593 free(optionlist_cpy);
595 "Malformated option: '%s'; Should be of the form 'name:value'",
600 DEBUG2("name='%s';val='%s'",name,val);
603 variable = xbt_dict_get((xbt_dict_t)cfg,name);
605 free(optionlist_cpy);
606 if (e.category == not_found_error) {
608 THROW1(not_found_error,0,"No registrated variable corresponding to '%s'.",name);
614 switch (variable->type) {
615 case xbt_cfgelm_string:
616 xbt_cfg_set_string(cfg, name, val); /* throws */
620 i=strtol(val, &val, 0);
622 free(optionlist_cpy);
624 "Value of option %s not valid. Should be an integer",
628 xbt_cfg_set_int(cfg,name,i); /* throws */
631 case xbt_cfgelm_double:
634 free(optionlist_cpy);
636 "Value of option %s not valid. Should be a double",
640 xbt_cfg_set_double(cfg,name,d); /* throws */
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 xbt_cfg_set_host(cfg,name,str,i); /* throws */
666 THROW1(unknown_error,0,"Type of config element %s is not valid.",name);
669 free(optionlist_cpy);
673 free(optionlist_cpy);
677 /** @brief Set or add an integer value to \a name within \a cfg
679 * \arg cfg the config set
680 * \arg name the name of the variable
681 * \arg val the value of the variable
684 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
685 xbt_cfgelm_t variable;
687 VERB2("Configuration setting: %s=%d",name,val);
688 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
690 if (variable->max == 1) {
691 if (variable->cb_rm && xbt_dynar_length(variable->content))
692 (*variable->cb_rm)(name, 0);
694 xbt_dynar_set(variable->content,0,&val);
696 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
697 THROW3(mismatch_error,0,
698 "Cannot add value %d to the config element %s since it's already full (size=%d)",
699 val,name,variable->max);
701 xbt_dynar_push(variable->content,&val);
704 if (variable->cb_set)
705 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
708 /** @brief Set or add a double value to \a name within \a cfg
710 * \arg cfg the config set
711 * \arg name the name of the variable
712 * \arg val the doule to set
716 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
717 xbt_cfgelm_t variable;
719 VERB2("Configuration setting: %s=%f",name,val);
720 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
722 if (variable->max == 1) {
723 if (variable->cb_rm && xbt_dynar_length(variable->content))
724 (*variable->cb_rm)(name, 0);
726 xbt_dynar_set(variable->content,0,&val);
728 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
729 THROW3(mismatch_error,0,
730 "Cannot add value %f to the config element %s since it's already full (size=%d)",
731 val,name,variable->max);
733 xbt_dynar_push(variable->content,&val);
736 if (variable->cb_set)
737 (*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;
751 char *newval = xbt_strdup(val);
753 VERB2("Configuration setting: %s=%s",name,val);
754 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
756 if (variable->max == 1) {
757 if (variable->cb_rm && xbt_dynar_length(variable->content))
758 (*variable->cb_rm)(name, 0);
760 xbt_dynar_set(variable->content,0,&newval);
762 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
763 THROW3(mismatch_error,0,
764 "Cannot add value %s to the config element %s since it's already full (size=%d)",
765 name,val,variable->max);
767 xbt_dynar_push(variable->content,&newval);
770 if (variable->cb_set)
771 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
774 /** @brief Set or add an host value to \a name within \a cfg
776 * \arg cfg the config set
777 * \arg name the name of the variable
779 * \arg port the port number
781 * \e host values are composed of a string (hostname) and an integer (port)
785 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
786 const char *host,int port) {
787 xbt_cfgelm_t variable;
788 xbt_host_t *val=xbt_new(xbt_host_t,1);
790 VERB3("Configuration setting: %s=%s:%d",name,host,port);
792 val->name = xbt_strdup(name);
795 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
797 if (variable->max == 1) {
798 if (variable->cb_rm && xbt_dynar_length(variable->content))
799 (*variable->cb_rm)(name, 0);
801 xbt_dynar_set(variable->content,0,&val);
803 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
804 THROW4(mismatch_error,0,
805 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
806 host,port,name,variable->max);
808 xbt_dynar_push(variable->content,&val);
811 if (variable->cb_set)
812 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
815 /* ---- [ Removing ] ---- */
817 /** @brief Remove the provided \e val integer value from a variable
819 * \arg cfg the config set
820 * \arg name the name of the variable
821 * \arg val the value to be removed
823 void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
825 xbt_cfgelm_t variable;
828 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
830 if (xbt_dynar_length(variable->content) == variable->min)
831 THROW3(mismatch_error,0,
832 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
833 val,name,variable->min);
835 xbt_dynar_foreach(variable->content,cpt,seen) {
837 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
838 xbt_dynar_cursor_rm(variable->content,&cpt);
843 THROW2(not_found_error,0,
844 "Can't remove the value %d of config element %s: value not found.",val,name);
847 /** @brief Remove the provided \e val double value from a variable
849 * \arg cfg the config set
850 * \arg name the name of the variable
851 * \arg val the value to be removed
854 void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
855 xbt_cfgelm_t variable;
859 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
861 if (xbt_dynar_length(variable->content) == variable->min)
862 THROW3(mismatch_error,0,
863 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
864 val,name,variable->min);
866 xbt_dynar_foreach(variable->content,cpt,seen) {
868 xbt_dynar_cursor_rm(variable->content,&cpt);
870 (*variable->cb_rm)(name, cpt);
875 THROW2(not_found_error,0,
876 "Can't remove the value %f of config element %s: value not found.",val,name);
879 /** @brief Remove the provided \e val string value from a variable
881 * \arg cfg the config set
882 * \arg name the name of the variable
883 * \arg val the value of the string which will be removed
886 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
887 xbt_cfgelm_t variable;
891 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
893 if (xbt_dynar_length(variable->content) == variable->min)
894 THROW3(mismatch_error,0,
895 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
896 name,val,variable->min);
898 xbt_dynar_foreach(variable->content,cpt,seen) {
899 if (!strcpy(seen,val)) {
901 (*variable->cb_rm)(name, cpt);
902 xbt_dynar_cursor_rm(variable->content,&cpt);
907 THROW2(not_found_error,0,
908 "Can't remove the value %s of config element %s: value not found.",val,name);
911 /** @brief Remove the provided \e val host value from a variable
913 * \arg cfg the config set
914 * \arg name the name of the variable
915 * \arg host the hostname
916 * \arg port the port number
920 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
921 xbt_cfgelm_t variable;
925 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
927 if (xbt_dynar_length(variable->content) == variable->min)
928 THROW4(mismatch_error,0,
929 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
930 host,port,name,variable->min);
932 xbt_dynar_foreach(variable->content,cpt,seen) {
933 if (!strcpy(seen->name,host) && seen->port == port) {
934 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
935 xbt_dynar_cursor_rm(variable->content,&cpt);
940 THROW3(not_found_error,0,
941 "Can't remove the value %s:%d of config element %s: value not found.",
945 /** @brief Remove the \e pos th value from the provided variable */
947 void xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
949 xbt_cfgelm_t variable;
951 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_any);
953 if (xbt_dynar_length(variable->content) == variable->min)
954 THROW3(mismatch_error,0,
955 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
956 pos,name,variable->min);
958 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
959 xbt_dynar_remove_at(variable->content, pos, NULL);
962 /** @brief Remove all the values from a variable
964 * \arg cfg the config set
965 * \arg name the name of the variable
969 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
970 xbt_cfgelm_t variable;
974 variable = xbt_dict_get((xbt_dict_t)cfg,name);
976 if (e.category != not_found_error)
980 THROW1(not_found_error,0,
981 "Can't empty '%s' since this config element does not exist", name);
985 if (variable->cb_rm) {
988 xbt_dynar_foreach(variable->content,cpt,ignored) {
989 (*variable->cb_rm)(name, cpt);
992 xbt_dynar_reset(variable->content);
996 /*----[ Getting ]---------------------------------------------------------*/
998 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
1000 * \arg cfg the config set
1001 * \arg name the name of the variable
1002 * \arg val the wanted value
1004 * Returns the first value from the config set under the given name.
1005 * If there is more than one value, it will issue a warning. Consider using
1006 * xbt_cfg_get_dynar() instead.
1008 * \warning the returned value is the actual content of the config set
1011 xbt_cfg_get_int (xbt_cfg_t cfg, const char *name) {
1012 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1014 if (xbt_dynar_length(variable->content) > 1) {
1015 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
1016 name, xbt_dynar_length(variable->content));
1019 return xbt_dynar_get_as(variable->content, 0, int);
1022 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1024 * \arg cfg the config set
1025 * \arg name the name of the variable
1026 * \arg val the wanted value
1028 * Returns the first value from the config set under the given name.
1029 * If there is more than one value, it will issue a warning. Consider using
1030 * xbt_cfg_get_dynar() instead.
1032 * \warning the returned value is the actual content of the config set
1036 xbt_cfg_get_double(xbt_cfg_t cfg, const char *name) {
1037 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1039 if (xbt_dynar_length(variable->content) > 1) {
1040 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1041 name, xbt_dynar_length(variable->content));
1044 return xbt_dynar_get_as(variable->content, 0, double);
1047 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1049 * \arg th 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
1060 char* xbt_cfg_get_string(xbt_cfg_t cfg, const char *name) {
1061 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
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 return xbt_dynar_get_as(variable->content, 0, char *);
1071 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1073 * \arg cfg the config set
1074 * \arg name the name of the variable
1075 * \arg host the host
1076 * \arg port the port number
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 void xbt_cfg_get_host (xbt_cfg_t cfg, const char *name,
1086 char **host, int *port) {
1087 xbt_cfgelm_t variable;
1090 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
1092 if (xbt_dynar_length(variable->content) > 1) {
1093 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1094 name, xbt_dynar_length(variable->content));
1097 val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1102 /** @brief Retrieve the dynar of all the values stored in a variable
1104 * \arg cfg where to search in
1105 * \arg name what to search for
1108 * Get the data stored in the config set.
1110 * \warning the returned value is the actual content of the config set
1112 xbt_dynar_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name) {
1113 xbt_cfgelm_t variable;
1117 variable = xbt_dict_get((xbt_dict_t)cfg,name);
1119 if (e.category == not_found_error) {
1121 THROW1(not_found_error,0,
1122 "No registered variable %s in this config set",name);
1127 return variable->content;
1131 /** @brief Retrieve one of the integer value of a variable */
1133 xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos) {
1135 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1136 return xbt_dynar_get_as(variable->content, pos, int);
1139 /** @brief Retrieve one of the double value of a variable */
1141 xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos) {
1143 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1144 return xbt_dynar_get_as(variable->content, pos, double);
1148 /** @brief Retrieve one of the string value of a variable */
1150 xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos) {
1152 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1153 return xbt_dynar_get_as(variable->content, pos, char*);
1156 /** @brief Retrieve one of the host value of a variable */
1158 xbt_cfg_get_host_at(xbt_cfg_t cfg, const char *name, int pos,
1159 char **host, int *port) {
1161 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1162 xbt_host_t *val = xbt_dynar_get_ptr(variable->content, pos);