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"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg,xbt,"configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config variable.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
30 /* Allowed type of the variable */
31 e_xbt_cfgelm_type_t type;
39 (cannot be an union because type peer uses both str and i) */
41 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
43 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
44 {"int","double","string","peer","any"};
46 /* Internal stuff used in cache to free a variable */
47 static void xbt_cfgelm_free(void *data);
49 /* Retrieve the variable we'll modify */
50 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
51 e_xbt_cfgelm_type_t type);
53 static void xbt_cfg_str_free(void *d){
57 /*----[ Memory management ]-----------------------------------------------*/
59 /** @brief Constructor
61 * Initialise an config set
65 xbt_cfg_t xbt_cfg_new(void) {
66 return (xbt_cfg_t)xbt_dict_new();
69 /** \brief Copy an existing configuration set
71 * \arg whereto the config set to be created
72 * \arg tocopy the source data
74 * This only copy the registrations, not the actual content
78 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
79 xbt_dict_cursor_t cursor=NULL;
80 xbt_cfgelm_t variable=NULL;
84 xbt_assert0(tocopy,"cannot copy NULL config");
86 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
87 xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
88 variable->cb_set, variable->cb_rm);
92 /** @brief Destructor */
93 void xbt_cfg_free(xbt_cfg_t *cfg) {
94 xbt_dict_free((xbt_dict_t*)cfg);
97 /** @brief Dump a config set for debuging purpose
99 * \arg name The name to give to this config set
100 * \arg indent what to write at the begining of each line (right number of spaces)
101 * \arg cfg the config set
103 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
104 xbt_dict_t dict = (xbt_dict_t) cfg;
105 xbt_dict_cursor_t cursor=NULL;
106 xbt_cfgelm_t variable=NULL;
116 printf("%s>> Dumping of the config set '%s':\n",indent,name);
117 xbt_dict_foreach(dict,cursor,key,variable) {
119 printf("%s %s:",indent,key);
121 size = xbt_dynar_length(variable->content);
122 printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
123 variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
125 variable->cb_rm, variable->cb_set);
127 switch (variable->type) {
130 for (i=0; i<size; i++) {
131 ival = xbt_dynar_get_as(variable->content,i,int);
132 printf ("%s %d\n",indent,ival);
136 case xbt_cfgelm_double:
137 for (i=0; i<size; i++) {
138 dval = xbt_dynar_get_as(variable->content,i,double);
139 printf ("%s %f\n",indent,dval);
143 case xbt_cfgelm_string:
144 for (i=0; i<size; i++) {
145 sval = xbt_dynar_get_as(variable->content,i,char*);
146 printf ("%s %s\n",indent,sval);
150 case xbt_cfgelm_peer:
151 for (i=0; i<size; i++) {
152 hval = xbt_dynar_get_as(variable->content,i,xbt_peer_t);
153 printf ("%s %s:%d\n",indent,hval->name,hval->port);
158 printf("%s Invalid type!!\n",indent);
163 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
166 xbt_dict_cursor_free(&cursor);
171 * free an config element
174 void xbt_cfgelm_free(void *data) {
175 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
178 xbt_dynar_free(&(c->content));
182 /*----[ Registering stuff ]-----------------------------------------------*/
184 /** @brief Register an element within a config set
186 * @arg cfg the config set
187 * @arg type the type of the config element
188 * @arg min the minimum
189 * @arg max the maximum
193 xbt_cfg_register(xbt_cfg_t cfg,
194 const char *name, e_xbt_cfgelm_type_t type,
196 xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
202 xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_peer,
203 "type of %s not valid (%d should be between %d and %d)",
204 name,type,xbt_cfgelm_int, xbt_cfgelm_peer);
205 DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",
206 name,min,max,xbt_cfgelm_type_name[type],type);
208 res = xbt_dict_get((xbt_dict_t)cfg,name);
210 if (e.category == not_found_error) {
219 WARN1("Config elem %s registered twice.",name);
220 /* Will be removed by the insertion of the new one */
223 res=xbt_new(s_xbt_cfgelm_t,1);
228 res->cb_set = cb_set;
233 res->content = xbt_dynar_new(sizeof(int), NULL);
236 case xbt_cfgelm_double:
237 res->content = xbt_dynar_new(sizeof(double), NULL);
240 case xbt_cfgelm_string:
241 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
244 case xbt_cfgelm_peer:
245 res->content = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
249 ERROR1("%d is an invalide type code",type);
252 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
255 /** @brief Unregister an element from a config set.
257 * @arg cfg the config set
258 * @arg name the name of the elem to be freed
260 * Note that it removes both the description and the actual content.
261 * Throws not_found when no such element exists.
265 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
266 xbt_dict_remove((xbt_dict_t)cfg,name);
270 * @brief Parse a string and register the stuff described.
272 * @arg cfg the config set
273 * @arg entry a string describing the element to register
275 * The string may consist in several variable descriptions separated by a space.
276 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
277 * with type being one of 'string','int', 'peer' or 'double'.
281 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
282 char *entrycpy=xbt_strdup(entry);
286 e_xbt_cfgelm_type_t type;
288 tok=strchr(entrycpy, ':');
289 xbt_assert2(tok,"Invalid config element descriptor: %s%s",
291 "; Should be <name>:<min nb>_to_<max nb>_<type>");
294 min=strtol(tok, &tok, 10);
295 xbt_assert1(tok,"Invalid minimum in config element descriptor %s",entry);
297 xbt_assert2(strcmp(tok,"_to_"),
298 "Invalid config element descriptor : %s%s",
300 "; Should be <name>:<min nb>_to_<max nb>_<type>");
301 tok += strlen("_to_");
303 max=strtol(tok, &tok, 10);
304 xbt_assert1(tok,"Invalid maximum in config element descriptor %s",entry);
306 xbt_assert2( *(tok++) =='_',
307 "Invalid config element descriptor: %s%s",entry,
308 "; Should be <name>:<min nb>_to_<max nb>_<type>");
311 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
313 xbt_assert2(type < xbt_cfgelm_type_count,
314 "Invalid type in config element descriptor: %s%s",entry,
315 "; Should be one of 'string', 'int', 'peer' or 'double'.");
317 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
319 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
322 /** @brief Check that each variable have the right amount of values */
325 xbt_cfg_check(xbt_cfg_t cfg) {
326 xbt_dict_cursor_t cursor;
327 xbt_cfgelm_t variable;
331 xbt_assert0(cfg,"NULL config set.");
333 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
334 size = xbt_dynar_length(variable->content);
335 if (variable->min > size) {
336 xbt_dict_cursor_free(&cursor);
337 THROW4(mismatch_error,0,
338 "Config elem %s needs at least %d %s, but there is only %d values.",
341 xbt_cfgelm_type_name[variable->type],
345 if (variable->max > 0 && variable->max < size) {
346 xbt_dict_cursor_free(&cursor);
347 THROW4(mismatch_error,0,
348 "Config elem %s accepts at most %d %s, but there is %d values.",
351 xbt_cfgelm_type_name[variable->type],
356 xbt_dict_cursor_free(&cursor);
359 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
361 e_xbt_cfgelm_type_t type){
362 xbt_cfgelm_t res=NULL;
366 res = xbt_dict_get((xbt_dict_t)cfg,name);
368 if (e.category == not_found_error) {
370 THROW1(not_found_error,0,
371 "No registered variable '%s' in this config set",name);
376 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
377 "You tried to access to the config element %s as an %s, but its type is %s.",
379 xbt_cfgelm_type_name[type],
380 xbt_cfgelm_type_name[res->type]);
385 /** @brief Get the type of this variable in that configuration set
387 * \arg cfg the config set
388 * \arg name the name of the element
389 * \arg type the result
394 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
396 xbt_cfgelm_t variable = NULL;
400 variable = xbt_dict_get((xbt_dict_t)cfg,name);
402 if (e.category == not_found_error) {
404 THROW1(not_found_error,0,
405 "Can't get the type of '%s' since this variable does not exist",name);
410 INFO1("type in variable = %d",variable->type);
412 return variable->type;
415 /*----[ Setting ]---------------------------------------------------------*/
416 /** @brief va_args version of xbt_cfg_set
418 * \arg cfg config set to fill
419 * \arg n variable name
420 * \arg pa variable value
422 * Add some values to the config set.
425 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
429 e_xbt_cfgelm_type_t type=0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
434 type = xbt_cfg_get_type(cfg,name);
436 if (e.category == not_found_error) {
438 THROW1(not_found_error,0,"Can't set the property '%s' since it's not registered",name);
444 case xbt_cfgelm_peer:
445 str = va_arg(pa, char *);
447 xbt_cfg_set_peer(cfg,name,str,i);
450 case xbt_cfgelm_string:
451 str=va_arg(pa, char *);
452 xbt_cfg_set_string(cfg, name, str);
457 xbt_cfg_set_int(cfg,name,i);
460 case xbt_cfgelm_double:
462 xbt_cfg_set_double(cfg,name,d);
466 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
470 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
472 * \arg cfg config set to fill
473 * \arg name variable name
474 * \arg varargs variable value
477 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
481 xbt_cfg_set_vargs(cfg,name,pa);
485 /** @brief Add values parsed from a string into a config set
487 * \arg cfg config set to fill
488 * \arg options a string containing the content to add to the config set. This
489 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
490 * like "[name]:[value]" where [name] is the name of an already registred
491 * variable, and [value] conforms to the data type under which this variable was
494 * @todo This is a crude manual parser, it should be a proper lexer.
498 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
505 xbt_cfgelm_t variable=NULL;
506 char *optionlist_cpy;
507 char *option, *name,*val;
512 if (!options || !strlen(options)) { /* nothing to do */
515 optionlist_cpy=xbt_strdup(options);
517 DEBUG1("List to parse and set:'%s'",options);
518 option=optionlist_cpy;
519 while (1) { /* breaks in the code */
525 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
526 name,len,(long)(option-name));
529 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
530 DEBUG1("Take %c.",*option);
533 if (option-name == len) {
534 DEBUG0("Boundary=EOL");
535 option=NULL; /* don't do next iteration */
538 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
539 *option,len,(long)(option-name));
541 /* Pass the following blank chars */
543 while (option-name<(len-1) &&
544 (*option == ' ' || *option == '\n' || *option == '\t')) {
545 /* fprintf(stderr,"Ignore a blank char.\n");*/
548 if (option-name == len-1)
549 option=NULL; /* don't do next iteration */
551 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
553 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
558 val=strchr(name,':');
560 free(optionlist_cpy);
562 "Option '%s' badly formated. Should be of the form 'name:value'",
567 DEBUG2("name='%s';val='%s'",name,val);
570 variable = xbt_dict_get((xbt_dict_t)cfg,name);
572 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
573 name = (char*) (optionlist_cpy-name + options);
574 free(optionlist_cpy);
575 if (e.category == not_found_error) {
577 THROW1(not_found_error,0,"No registrated variable corresponding to '%s'.",name);
583 switch (variable->type) {
584 case xbt_cfgelm_string:
585 xbt_cfg_set_string(cfg, name, val); /* throws */
589 i=strtol(val, &val, 0);
591 free(optionlist_cpy);
593 "Value of option %s not valid. Should be an integer",
597 xbt_cfg_set_int(cfg,name,i); /* throws */
600 case xbt_cfgelm_double:
603 free(optionlist_cpy);
605 "Value of option %s not valid. Should be a double",
609 xbt_cfg_set_double(cfg,name,d); /* throws */
612 case xbt_cfgelm_peer:
616 free(optionlist_cpy);
618 "Value of option %s not valid. Should be an peer (machine:port)",
623 i=strtol(val, &val, 0);
625 free(optionlist_cpy);
627 "Value of option %s not valid. Should be an peer (machine:port)",
631 xbt_cfg_set_peer(cfg,name,str,i); /* throws */
635 THROW1(unknown_error,0,"Type of config element %s is not valid.",name);
638 free(optionlist_cpy);
642 free(optionlist_cpy);
646 /** @brief Set or add an integer value to \a name within \a cfg
648 * \arg cfg the config set
649 * \arg name the name of the variable
650 * \arg val the value of the variable
653 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
654 xbt_cfgelm_t variable;
656 VERB2("Configuration setting: %s=%d",name,val);
657 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
659 if (variable->max == 1) {
660 if (variable->cb_rm && xbt_dynar_length(variable->content))
661 (*variable->cb_rm)(name, 0);
663 xbt_dynar_set(variable->content,0,&val);
665 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
666 THROW3(mismatch_error,0,
667 "Cannot add value %d to the config element %s since it's already full (size=%d)",
668 val,name,variable->max);
670 xbt_dynar_push(variable->content,&val);
673 if (variable->cb_set)
674 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
677 /** @brief Set or add a double 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 doule to set
685 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
686 xbt_cfgelm_t variable;
688 VERB2("Configuration setting: %s=%f",name,val);
689 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
691 if (variable->max == 1) {
692 if (variable->cb_rm && xbt_dynar_length(variable->content))
693 (*variable->cb_rm)(name, 0);
695 xbt_dynar_set(variable->content,0,&val);
697 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
698 THROW3(mismatch_error,0,
699 "Cannot add value %f to the config element %s since it's already full (size=%d)",
700 val,name,variable->max);
702 xbt_dynar_push(variable->content,&val);
705 if (variable->cb_set)
706 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
709 /** @brief Set or add a string value to \a name within \a cfg
711 * \arg cfg the config set
712 * \arg name the name of the variable
713 * \arg val the value to be added
718 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
719 xbt_cfgelm_t variable;
720 char *newval = xbt_strdup(val);
722 VERB2("Configuration setting: %s=%s",name,val);
723 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
725 if (variable->max == 1) {
726 if (xbt_dynar_length(variable->content)) {
728 (*variable->cb_rm)(name, 0);
729 else if (variable->type == xbt_cfgelm_string) {
730 char * sval=xbt_dynar_get_as(variable->content,0,char*);
735 xbt_dynar_set(variable->content,0,&newval);
737 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
738 THROW3(mismatch_error,0,
739 "Cannot add value %s to the config element %s since it's already full (size=%d)",
740 name,val,variable->max);
742 xbt_dynar_push(variable->content,&newval);
745 if (variable->cb_set)
746 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
749 /** @brief Set or add an peer value to \a name within \a cfg
751 * \arg cfg the config set
752 * \arg name the name of the variable
754 * \arg port the port number
756 * \e peer values are composed of a string (peername) and an integer (port)
760 xbt_cfg_set_peer(xbt_cfg_t cfg,const char*name,
761 const char *peer,int port) {
762 xbt_cfgelm_t variable;
763 xbt_peer_t val=xbt_peer_new(peer,port);
765 VERB3("Configuration setting: %s=%s:%d",name,peer,port);
767 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
769 if (variable->max == 1) {
770 if (variable->cb_rm && xbt_dynar_length(variable->content))
771 (*variable->cb_rm)(name, 0);
773 xbt_dynar_set(variable->content,0,&val);
775 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
776 THROW4(mismatch_error,0,
777 "Cannot add value %s:%d to the config element %s since it's already full (size=%d)",
778 peer,port,name,variable->max);
780 xbt_dynar_push(variable->content,&val);
783 if (variable->cb_set)
784 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
787 /* ---- [ Removing ] ---- */
789 /** @brief Remove the provided \e val integer value from a variable
791 * \arg cfg the config set
792 * \arg name the name of the variable
793 * \arg val the value to be removed
795 void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
797 xbt_cfgelm_t variable;
800 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
802 if (xbt_dynar_length(variable->content) == variable->min)
803 THROW3(mismatch_error,0,
804 "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
805 val,name,variable->min);
807 xbt_dynar_foreach(variable->content,cpt,seen) {
809 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
810 xbt_dynar_cursor_rm(variable->content,&cpt);
815 THROW2(not_found_error,0,
816 "Can't remove the value %d of config element %s: value not found.",val,name);
819 /** @brief Remove the provided \e val double value from a variable
821 * \arg cfg the config set
822 * \arg name the name of the variable
823 * \arg val the value to be removed
826 void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
827 xbt_cfgelm_t variable;
831 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
833 if (xbt_dynar_length(variable->content) == variable->min)
834 THROW3(mismatch_error,0,
835 "Cannot remove value %f from the config element %s since it's already at its minimal size (=%d)",
836 val,name,variable->min);
838 xbt_dynar_foreach(variable->content,cpt,seen) {
840 xbt_dynar_cursor_rm(variable->content,&cpt);
842 (*variable->cb_rm)(name, cpt);
847 THROW2(not_found_error,0,
848 "Can't remove the value %f of config element %s: value not found.",val,name);
851 /** @brief Remove the provided \e val string value from a variable
853 * \arg cfg the config set
854 * \arg name the name of the variable
855 * \arg val the value of the string which will be removed
858 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
859 xbt_cfgelm_t variable;
863 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
865 if (xbt_dynar_length(variable->content) == variable->min)
866 THROW3(mismatch_error,0,
867 "Cannot remove value %s from the config element %s since it's already at its minimal size (=%d)",
868 name,val,variable->min);
870 xbt_dynar_foreach(variable->content,cpt,seen) {
871 if (!strcpy(seen,val)) {
873 (*variable->cb_rm)(name, cpt);
874 xbt_dynar_cursor_rm(variable->content,&cpt);
879 THROW2(not_found_error,0,
880 "Can't remove the value %s of config element %s: value not found.",val,name);
883 /** @brief Remove the provided \e val peer value from a variable
885 * \arg cfg the config set
886 * \arg name the name of the variable
887 * \arg peer the peername
888 * \arg port the port number
892 xbt_cfg_rm_peer(xbt_cfg_t cfg,const char*name, const char *peer,int port) {
893 xbt_cfgelm_t variable;
897 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
899 if (xbt_dynar_length(variable->content) == variable->min)
900 THROW4(mismatch_error,0,
901 "Cannot remove value %s:%d from the config element %s since it's already at its minimal size (=%d)",
902 peer,port,name,variable->min);
904 xbt_dynar_foreach(variable->content,cpt,seen) {
905 if (!strcpy(seen->name,peer) && seen->port == port) {
906 if (variable->cb_rm) (*variable->cb_rm)(name, cpt);
907 xbt_dynar_cursor_rm(variable->content,&cpt);
912 THROW3(not_found_error,0,
913 "Can't remove the value %s:%d of config element %s: value not found.",
917 /** @brief Remove the \e pos th value from the provided variable */
919 void xbt_cfg_rm_at (xbt_cfg_t cfg, const char *name, int pos) {
921 xbt_cfgelm_t variable;
923 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_any);
925 if (xbt_dynar_length(variable->content) == variable->min)
926 THROW3(mismatch_error,0,
927 "Cannot remove %dth value from the config element %s since it's already at its minimal size (=%d)",
928 pos,name,variable->min);
930 if (variable->cb_rm) (*variable->cb_rm)(name, pos);
931 xbt_dynar_remove_at(variable->content, pos, NULL);
934 /** @brief Remove all the values from a variable
936 * \arg cfg the config set
937 * \arg name the name of the variable
941 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
942 xbt_cfgelm_t variable=NULL;
946 variable = xbt_dict_get((xbt_dict_t)cfg,name);
948 if (e.category != not_found_error)
952 THROW1(not_found_error,0,
953 "Can't empty '%s' since this config element does not exist", name);
957 if (variable->cb_rm) {
960 xbt_dynar_foreach(variable->content,cpt,ignored) {
961 (*variable->cb_rm)(name, cpt);
964 xbt_dynar_reset(variable->content);
968 /*----[ Getting ]---------------------------------------------------------*/
970 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
972 * \arg cfg the config set
973 * \arg name the name of the variable
974 * \arg val the wanted value
976 * Returns the first value from the config set under the given name.
977 * If there is more than one value, it will issue a warning. Consider using
978 * xbt_cfg_get_dynar() instead.
980 * \warning the returned value is the actual content of the config set
983 xbt_cfg_get_int (xbt_cfg_t cfg, const char *name) {
984 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
986 if (xbt_dynar_length(variable->content) > 1) {
987 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
988 name, xbt_dynar_length(variable->content));
991 return xbt_dynar_get_as(variable->content, 0, int);
994 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
996 * \arg cfg the config set
997 * \arg name the name of the variable
998 * \arg val the wanted value
1000 * Returns the first value from the config set under the given name.
1001 * If there is more than one value, it will issue a warning. Consider using
1002 * xbt_cfg_get_dynar() instead.
1004 * \warning the returned value is the actual content of the config set
1008 xbt_cfg_get_double(xbt_cfg_t cfg, const char *name) {
1009 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1011 if (xbt_dynar_length(variable->content) > 1) {
1012 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1013 name, xbt_dynar_length(variable->content));
1016 return xbt_dynar_get_as(variable->content, 0, double);
1019 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
1021 * \arg th the config set
1022 * \arg name the name of the variable
1023 * \arg val the wanted value
1025 * Returns the first value from the config set under the given name.
1026 * If there is more than one value, it will issue a warning. Consider using
1027 * xbt_cfg_get_dynar() instead.
1029 * \warning the returned value is the actual content of the config set
1032 char* xbt_cfg_get_string(xbt_cfg_t cfg, const char *name) {
1033 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1035 if (xbt_dynar_length(variable->content) > 1) {
1036 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1037 name, xbt_dynar_length(variable->content));
1040 return xbt_dynar_get_as(variable->content, 0, char *);
1043 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
1045 * \arg cfg the config set
1046 * \arg name the name of the variable
1047 * \arg peer the peer
1048 * \arg port the port number
1050 * Returns the first value from the config set under the given name.
1051 * If there is more than one value, it will issue a warning. Consider using
1052 * xbt_cfg_get_dynar() instead.
1054 * \warning the returned value is the actual content of the config set
1057 void xbt_cfg_get_peer (xbt_cfg_t cfg, const char *name,
1058 char **peer, int *port) {
1059 xbt_cfgelm_t variable;
1062 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
1064 if (xbt_dynar_length(variable->content) > 1) {
1065 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1066 name, xbt_dynar_length(variable->content));
1069 val = xbt_dynar_get_as(variable->content, 0, xbt_peer_t);
1074 /** @brief Retrieve the dynar of all the values stored in a variable
1076 * \arg cfg where to search in
1077 * \arg name what to search for
1080 * Get the data stored in the config set.
1082 * \warning the returned value is the actual content of the config set
1084 xbt_dynar_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name) {
1085 xbt_cfgelm_t variable=NULL;
1089 variable = xbt_dict_get((xbt_dict_t)cfg,name);
1091 if (e.category == not_found_error) {
1093 THROW1(not_found_error,0,
1094 "No registered variable %s in this config set",name);
1099 return variable->content;
1103 /** @brief Retrieve one of the integer value of a variable */
1105 xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos) {
1107 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1108 return xbt_dynar_get_as(variable->content, pos, int);
1111 /** @brief Retrieve one of the double value of a variable */
1113 xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos) {
1115 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
1116 return xbt_dynar_get_as(variable->content, pos, double);
1120 /** @brief Retrieve one of the string value of a variable */
1122 xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos) {
1124 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
1125 return xbt_dynar_get_as(variable->content, pos, char*);
1128 /** @brief Retrieve one of the peer value of a variable */
1130 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1131 char **peer, int *port) {
1133 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1134 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1145 XBT_TEST_SUITE("config","Configuration support");
1147 static xbt_cfg_t make_set(){
1150 set = xbt_cfg_new();
1151 xbt_cfg_register_str(set,"speed:1_to_2_int");
1152 xbt_cfg_register_str(set,"peername:1_to_1_string");
1153 xbt_cfg_register_str(set,"user:1_to_10_string");
1156 } /* end_of_make_set */
1158 XBT_TEST_UNIT("memuse",test_config_memuse,"Alloc and free a config set") {
1159 xbt_cfg_t set=make_set();
1160 xbt_test_add0("Alloc and free a config set");
1161 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1166 XBT_TEST_UNIT("validation",test_config_validation,"Validation tests") {
1167 xbt_cfg_t set = set=make_set();
1170 xbt_test_add0("Having too few elements for speed");
1171 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1175 if (e.category != mismatch_error ||
1176 strncmp(e.msg,"Config elem speed needs",strlen("Config elem speed needs")))
1177 xbt_test_fail1("Got an exception. msg=%s",e.msg);
1185 xbt_test_add0("Having too much values of 'speed'");
1187 xbt_cfg_set_parse(set,"peername:toto:42 user:alegrand");
1189 xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
1191 if (e.category != mismatch_error ||
1192 strncmp(e.msg,"Cannot add value 34 to the config elem speed",
1193 strlen("Config elem speed needs")))
1194 xbt_test_fail1("Got an exception. msg=%s",e.msg);
1203 XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
1205 xbt_test_add0("Get a single value");
1207 /* get_single_value */
1209 xbt_cfg_t myset=make_set();
1211 xbt_cfg_set_parse(myset,"peername:toto:42 speed:42");
1212 ival = xbt_cfg_get_int(myset,"speed");
1214 xbt_test_fail1("Speed value = %d, I expected 42",ival);
1215 xbt_cfg_free(&myset);
1218 xbt_test_add0("Get multiple values");
1220 /* get_multiple_value */
1222 xbt_cfg_t myset=make_set();
1224 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1225 xbt_cfg_set_parse(myset,"speed:42");
1226 xbt_cfg_check(myset);
1227 dyn = xbt_cfg_get_dynar(myset,"user");
1229 if (xbt_dynar_length(dyn) != 3)
1230 xbt_test_fail1("Dynar length = %d, I expected 3", (int)xbt_dynar_length(dyn));
1232 if (strcmp(xbt_dynar_get_as(dyn,0,char*),"foo"))
1233 xbt_test_fail1("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn,0,char*));
1235 if (strcmp(xbt_dynar_get_as(dyn,1,char*),"bar"))
1236 xbt_test_fail1("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn,1,char*));
1238 if (strcmp(xbt_dynar_get_as(dyn,2,char*),"toto"))
1239 xbt_test_fail1("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn,2,char*));
1240 xbt_cfg_free(&myset);
1243 xbt_test_add0("Access to a non-existant entry");
1245 /* non-existant_entry */
1246 xbt_cfg_t myset=make_set();
1250 xbt_cfg_set_parse(myset, "color:blue");
1252 if (e.category != not_found_error)
1253 xbt_test_exception(e);
1256 xbt_cfg_free(&myset);
1259 #endif /* SIMGRID_TEST */