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 == mismatch_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.
271 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
272 return 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, ':');
297 "Invalid config element descriptor: ",entry,
298 "; Should be <name>:<min nb>_to_<max nb>_<type>");
304 min=strtol(tok, &tok, 10);
306 ERROR1("Invalid minimum in config element descriptor %s",entry);
311 if (!strcmp(tok,"_to_")){
313 "Invalid config element descriptor: ",entry,
314 "; Should be <name>:<min nb>_to_<max nb>_<type>");
318 tok += strlen("_to_");
320 max=strtol(tok, &tok, 10);
322 ERROR1("Invalid maximum in config element descriptor %s",entry);
329 "Invalid config element descriptor: ",entry,
330 "; Should be <name>:<min nb>_to_<max nb>_<type>");
336 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
338 if (type == xbt_cfgelm_type_count) {
340 "Invalid type in config element descriptor: ",entry,
341 "; Should be one of 'string', 'int', 'host' or 'double'.");
346 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
348 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
352 /** @brief Check that each variable have the right amount of values */
355 xbt_cfg_check(xbt_cfg_t cfg) {
356 xbt_dict_cursor_t cursor;
357 xbt_cfgelm_t variable;
361 xbt_assert0(cfg,"NULL config set.");
363 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
364 size = xbt_dynar_length(variable->content);
365 if (variable->min > size) {
366 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
369 xbt_cfgelm_type_name[variable->type],
371 xbt_dict_cursor_free(&cursor);
372 return mismatch_error;
375 if (variable->max > 0 && variable->max < size) {
376 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
379 xbt_cfgelm_type_name[variable->type],
381 xbt_dict_cursor_free(&cursor);
382 return mismatch_error;
387 xbt_dict_cursor_free(&cursor);
391 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
393 e_xbt_cfgelm_type_t type){
398 res = xbt_dict_get((xbt_dict_t)cfg,name);
400 if (e.category == mismatch_error) {
401 THROW1(mismatch_error,0,
402 "No registered variable '%s' in this config set",name);
408 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
409 "You tried to access to the config element %s as an %s, but its type is %s.",
411 xbt_cfgelm_type_name[type],
412 xbt_cfgelm_type_name[res->type]);
417 /** @brief Get the type of this variable in that configuration set
419 * \arg cfg the config set
420 * \arg name the name of the element
421 * \arg type the result
426 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
428 xbt_cfgelm_t variable = NULL;
432 variable = xbt_dict_get((xbt_dict_t)cfg,name);
434 if (e.category == mismatch_error) {
435 THROW1(mismatch_error,0,
436 "Can't get the type of '%s' since this variable does not exist",name);
442 INFO1("type in variable = %d",variable->type);
444 return variable->type;
447 /*----[ Setting ]---------------------------------------------------------*/
448 /** @brief va_args version of xbt_cfg_set
450 * \arg cfg config set to fill
451 * \arg n variable name
452 * \arg pa variable value
454 * Add some values to the config set.
457 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
461 e_xbt_cfgelm_type_t type;
466 type = xbt_cfg_get_type(cfg,name);
468 if (e.category == mismatch_error) {
470 THROW1(mismatch_error,0,"Can't set the property '%s' since it's not registered",name);
476 case xbt_cfgelm_host:
477 str = va_arg(pa, char *);
479 xbt_cfg_set_host(cfg,name,str,i);
482 case xbt_cfgelm_string:
483 str=va_arg(pa, char *);
484 xbt_cfg_set_string(cfg, name, str);
489 xbt_cfg_set_int(cfg,name,i);
492 case xbt_cfgelm_double:
494 xbt_cfg_set_double(cfg,name,d);
498 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
502 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
504 * \arg cfg config set to fill
505 * \arg name variable name
506 * \arg varargs variable value
509 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
513 xbt_cfg_set_vargs(cfg,name,pa);
517 /** @brief Add values parsed from a string into a config set
519 * \arg cfg config set to fill
520 * \arg options a string containing the content to add to the config set. This
521 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
522 * like "[name]:[value]" where [name] is the name of an already registred
523 * variable, and [value] conforms to the data type under which this variable was
526 * @todo This is a crude manual parser, it should be a proper lexer.
530 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
537 xbt_cfgelm_t variable;
538 char *optionlist_cpy;
539 char *option, *name,*val;
544 if (!options || !strlen(options)) { /* nothing to do */
547 optionlist_cpy=xbt_strdup(options);
549 DEBUG1("List to parse and set:'%s'",options);
550 option=optionlist_cpy;
551 while (1) { /* breaks in the code */
557 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
558 name,len,(long)(option-name));
561 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
562 DEBUG1("Take %c.",*option);
565 if (option-name == len) {
566 DEBUG0("Boundary=EOL");
567 option=NULL; /* don't do next iteration */
570 DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
571 *option,len,option-name);
573 /* Pass the following blank chars */
575 while (option-name<(len-1) &&
576 (*option == ' ' || *option == '\n' || *option == '\t')) {
577 /* fprintf(stderr,"Ignore a blank char.\n");*/
580 if (option-name == len-1)
581 option=NULL; /* don't do next iteration */
583 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
585 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
590 val=strchr(name,':');
592 free(optionlist_cpy);
594 "Malformated option: '%s'; Should be of the form 'name:value'",
599 DEBUG2("name='%s';val='%s'",name,val);
602 variable = xbt_dict_get((xbt_dict_t)cfg,name);
604 free(optionlist_cpy);
605 if (e.category == mismatch_error) {
607 THROW1(mismatch_error,0,"No registrated variable corresponding to '%s'.",name);
613 switch (variable->type) {
614 case xbt_cfgelm_string:
615 xbt_cfg_set_string(cfg, name, val); /* throws */
619 i=strtol(val, &val, 0);
621 free(optionlist_cpy);
623 "Value of option %s not valid. Should be an integer",
627 xbt_cfg_set_int(cfg,name,i); /* throws */
630 case xbt_cfgelm_double:
633 free(optionlist_cpy);
635 "Value of option %s not valid. Should be a double",
639 xbt_cfg_set_double(cfg,name,d); /* throws */
642 case xbt_cfgelm_host:
646 free(optionlist_cpy);
648 "Value of option %s not valid. Should be an host (machine:port)",
653 i=strtol(val, &val, 0);
655 free(optionlist_cpy);
657 "Value of option %s not valid. Should be an host (machine:port)",
661 xbt_cfg_set_host(cfg,name,str,i); /* throws */
665 THROW1(unknown_error,0,"Type of config element %s is not valid.",name);
668 free(optionlist_cpy);
672 free(optionlist_cpy);
676 /** @brief Set or add an integer value to \a name within \a cfg
678 * \arg cfg the config set
679 * \arg name the name of the variable
680 * \arg val the value of the variable
683 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
684 xbt_cfgelm_t variable;
686 VERB2("Configuration setting: %s=%d",name,val);
687 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
689 if (variable->max == 1) {
690 if (variable->cb_rm && xbt_dynar_length(variable->content))
691 (*variable->cb_rm)(name, 0);
693 xbt_dynar_set(variable->content,0,&val);
695 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
696 THROW3(mismatch_error,0,
697 "Cannot add value %d to the config element %s since it's already full (size=%d)",
698 val,name,variable->max);
700 xbt_dynar_push(variable->content,&val);
703 if (variable->cb_set)
704 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
707 /** @brief Set or add a double value to \a name within \a cfg
709 * \arg cfg the config set
710 * \arg name the name of the variable
711 * \arg val the doule to set
715 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
716 xbt_cfgelm_t variable;
718 VERB2("Configuration setting: %s=%f",name,val);
719 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
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 THROW3(mismatch_error,0,
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);
739 /** @brief Set or add a string value to \a name within \a cfg
741 * \arg cfg the config set
742 * \arg name the name of the variable
743 * \arg val the value to be added
748 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
749 xbt_cfgelm_t variable;
750 char *newval = xbt_strdup(val);
752 VERB2("Configuration setting: %s=%s",name,val);
753 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
755 if (variable->max == 1) {
756 if (variable->cb_rm && xbt_dynar_length(variable->content))
757 (*variable->cb_rm)(name, 0);
759 xbt_dynar_set(variable->content,0,&newval);
761 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
762 THROW3(mismatch_error,0,
763 "Cannot add value %s to the config element %s since it's already full (size=%d)",
764 name,val,variable->max);
766 xbt_dynar_push(variable->content,&newval);
769 if (variable->cb_set)
770 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
773 /** @brief Set or add an host value to \a name within \a cfg
775 * \arg cfg the config set
776 * \arg name the name of the variable
778 * \arg port the port number
780 * \e host values are composed of a string (hostname) and an integer (port)
784 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
785 const char *host,int port) {
786 xbt_cfgelm_t variable;
787 xbt_host_t *val=xbt_new(xbt_host_t,1);
789 VERB3("Configuration setting: %s=%s:%d",name,host,port);
791 val->name = xbt_strdup(name);
794 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
796 if (variable->max == 1) {
797 if (variable->cb_rm && xbt_dynar_length(variable->content))
798 (*variable->cb_rm)(name, 0);
800 xbt_dynar_set(variable->content,0,&val);
802 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
803 THROW4(mismatch_error,0,
804 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
805 host,port,name,variable->max);
807 xbt_dynar_push(variable->content,&val);
810 if (variable->cb_set)
811 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
814 /* ---- [ Removing ] ---- */
816 /** @brief Remove the provided \e val integer value from a variable
818 * \arg cfg the config set
819 * \arg name the name of the variable
820 * \arg val the value to be removed
822 void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
824 xbt_cfgelm_t variable;
827 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
829 if (xbt_dynar_length(variable->content) == variable->min)
830 THROW3(mismatch_error,0,
831 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
832 val,name,variable->min);
834 xbt_dynar_foreach(variable->content,cpt,seen) {
836 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
837 xbt_dynar_cursor_rm(variable->content,&cpt);
842 THROW2(mismatch_error,0,
843 "Can't remove the value %d of config element %s: value not found.",val,name);
846 /** @brief Remove the provided \e val double value from a variable
848 * \arg cfg the config set
849 * \arg name the name of the variable
850 * \arg val the value to be removed
853 void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
854 xbt_cfgelm_t variable;
858 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
860 if (xbt_dynar_length(variable->content) == variable->min)
861 THROW3(mismatch_error,0,
862 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
863 val,name,variable->min);
865 xbt_dynar_foreach(variable->content,cpt,seen) {
867 xbt_dynar_cursor_rm(variable->content,&cpt);
869 (*variable->cb_rm)(name, cpt);
874 THROW2(mismatch_error,0,
875 "Can't remove the value %f of config element %s: value not found.",val,name);
878 /** @brief Remove the provided \e val string value from a variable
880 * \arg cfg the config set
881 * \arg name the name of the variable
882 * \arg val the value of the string which will be removed
885 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
886 xbt_cfgelm_t variable;
890 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
892 if (xbt_dynar_length(variable->content) == variable->min)
893 THROW3(mismatch_error,0,
894 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
895 name,val,variable->min);
897 xbt_dynar_foreach(variable->content,cpt,seen) {
898 if (!strcpy(seen,val)) {
900 (*variable->cb_rm)(name, cpt);
901 xbt_dynar_cursor_rm(variable->content,&cpt);
906 THROW2(mismatch_error,0,
907 "Can't remove the value %s of config element %s: value not found.",val,name);
910 /** @brief Remove the provided \e val host value from a variable
912 * \arg cfg the config set
913 * \arg name the name of the variable
914 * \arg host the hostname
915 * \arg port the port number
919 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
920 xbt_cfgelm_t variable;
924 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
926 if (xbt_dynar_length(variable->content) == variable->min)
927 THROW4(mismatch_error,0,
928 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
929 host,port,name,variable->min);
931 xbt_dynar_foreach(variable->content,cpt,seen) {
932 if (!strcpy(seen->name,host) && seen->port == port) {
933 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
934 xbt_dynar_cursor_rm(variable->content,&cpt);
939 THROW3(mismatch_error,0,
940 "Can't remove the value %s:%d of config element %s: value not found.",
944 /** @brief Remove the \e pos th value from the provided variable */
946 void xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
948 xbt_cfgelm_t variable;
950 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_any);
952 if (xbt_dynar_length(variable->content) == variable->min)
953 THROW3(mismatch_error,0,
954 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
955 pos,name,variable->min);
957 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
958 xbt_dynar_remove_at(variable->content, pos, NULL);
961 /** @brief Remove all the values from a variable
963 * \arg cfg the config set
964 * \arg name the name of the variable
968 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
969 xbt_cfgelm_t variable;
973 variable = xbt_dict_get((xbt_dict_t)cfg,name);
975 if (e.category == mismatch_error) {
977 THROW1(mismatch_error,0,
978 "Can't empty '%s' since this config element does not exist", name);
984 if (variable->cb_rm) {
987 xbt_dynar_foreach(variable->content,cpt,ignored) {
988 (*variable->cb_rm)(name, cpt);
991 xbt_dynar_reset(variable->content);
995 /*----[ Getting ]---------------------------------------------------------*/
997 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
999 * \arg cfg the config set
1000 * \arg name the name of the variable
1001 * \arg val the wanted value
1003 * Returns the first value from the config set under the given name.
1004 * If there is more than one value, it will issue a warning. Consider using
1005 * xbt_cfg_get_dynar() instead.
1007 * \warning the returned value is the actual content of the config set
1010 xbt_cfg_get_int (xbt_cfg_t cfg, const char *name) {
1011 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1013 if (xbt_dynar_length(variable->content) > 1) {
1014 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
1015 name, xbt_dynar_length(variable->content));
1018 return xbt_dynar_get_as(variable->content, 0, int);
1021 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
1023 * \arg cfg the config set
1024 * \arg name the name of the variable
1025 * \arg val the wanted value
1027 * Returns the first value from the config set under the given name.
1028 * If there is more than one value, it will issue a warning. Consider using
1029 * xbt_cfg_get_dynar() instead.
1031 * \warning the returned value is the actual content of the config set
1035 xbt_cfg_get_double(xbt_cfg_t cfg, const char *name) {
1036 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
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\n",
1040 name, xbt_dynar_length(variable->content));
1043 return xbt_dynar_get_as(variable->content, 0, double);
1046 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1048 * \arg th the config set
1049 * \arg name the name of the variable
1050 * \arg val the wanted value
1052 * Returns the first value from the config set under the given name.
1053 * If there is more than one value, it will issue a warning. Consider using
1054 * xbt_cfg_get_dynar() instead.
1056 * \warning the returned value is the actual content of the config set
1059 char* xbt_cfg_get_string(xbt_cfg_t cfg, const char *name) {
1060 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1062 if (xbt_dynar_length(variable->content) > 1) {
1063 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1064 name, xbt_dynar_length(variable->content));
1067 return xbt_dynar_get_as(variable->content, 0, char *);
1070 /** @brief Retrieve an host value of a variable (get a warning if not uniq)
1072 * \arg cfg the config set
1073 * \arg name the name of the variable
1074 * \arg host the host
1075 * \arg port the port number
1077 * Returns the first value from the config set under the given name.
1078 * If there is more than one value, it will issue a warning. Consider using
1079 * xbt_cfg_get_dynar() instead.
1081 * \warning the returned value is the actual content of the config set
1084 void xbt_cfg_get_host (xbt_cfg_t cfg, const char *name,
1085 char **host, int *port) {
1086 xbt_cfgelm_t variable;
1089 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
1091 if (xbt_dynar_length(variable->content) > 1) {
1092 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1093 name, xbt_dynar_length(variable->content));
1096 val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
1101 /** @brief Retrieve the dynar of all the values stored in a variable
1103 * \arg cfg where to search in
1104 * \arg name what to search for
1107 * Get the data stored in the config set.
1109 * \warning the returned value is the actual content of the config set
1111 xbt_dynar_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name) {
1112 xbt_cfgelm_t variable;
1116 variable = xbt_dict_get((xbt_dict_t)cfg,name);
1118 if (e.category == mismatch_error) {
1120 THROW1(mismatch_error,0,
1121 "No registered variable %s in this config set",name);
1126 return variable->content;
1130 /** @brief Retrieve one of the integer value of a variable */
1132 xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos) {
1134 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1135 return xbt_dynar_get_as(variable->content, pos, int);
1138 /** @brief Retrieve one of the double value of a variable */
1140 xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos) {
1142 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1143 return xbt_dynar_get_as(variable->content, pos, double);
1147 /** @brief Retrieve one of the string value of a variable */
1149 xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos) {
1151 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1152 return xbt_dynar_get_as(variable->content, pos, char*);
1155 /** @brief Retrieve one of the host value of a variable */
1157 xbt_cfg_get_host_at(xbt_cfg_t cfg, const char *name, int pos,
1158 char **host, int *port) {
1160 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1161 xbt_host_t *val = xbt_dynar_get_ptr(variable->content, pos);