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. */
14 #include "xbt/sysdep.h"
17 #include "xbt/dynar.h"
21 #include "xbt/config.h" /* prototypes of this module */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg,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 peer 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","peer","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){
58 /*----[ Memory management ]-----------------------------------------------*/
60 /** @brief Constructor
62 * Initialise an config set
66 xbt_cfg_t xbt_cfg_new(void) {
67 return (xbt_cfg_t)xbt_dict_new();
70 /** \brief Copy an existing configuration set
72 * \arg whereto the config set to be created
73 * \arg tocopy the source data
75 * This only copy the registrations, not the actual content
79 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
80 xbt_dict_cursor_t cursor=NULL;
81 xbt_cfgelm_t variable=NULL;
85 xbt_assert0(tocopy,"cannot copy NULL config");
87 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,variable) {
88 xbt_cfg_register(*whereto, name, variable->type, variable->min, variable->max,
89 variable->cb_set, variable->cb_rm);
93 /** @brief Destructor */
94 void xbt_cfg_free(xbt_cfg_t *cfg) {
95 xbt_dict_free((xbt_dict_t*)cfg);
98 /** @brief Dump a config set for debuging purpose
100 * \arg name The name to give to this config set
101 * \arg indent what to write at the begining of each line (right number of spaces)
102 * \arg cfg the config set
104 void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
105 xbt_dict_t dict = (xbt_dict_t) cfg;
106 xbt_dict_cursor_t cursor=NULL;
107 xbt_cfgelm_t variable=NULL;
117 printf("%s>> Dumping of the config set '%s':\n",indent,name);
118 xbt_dict_foreach(dict,cursor,key,variable) {
120 printf("%s %s:",indent,key);
122 size = xbt_dynar_length(variable->content);
123 printf("%d_to_%d_%s. Actual size=%d. prerm=%p,postset=%p, List of values:\n",
124 variable->min,variable->max,xbt_cfgelm_type_name[variable->type],
126 variable->cb_rm, variable->cb_set);
128 switch (variable->type) {
131 for (i=0; i<size; i++) {
132 ival = xbt_dynar_get_as(variable->content,i,int);
133 printf ("%s %d\n",indent,ival);
137 case xbt_cfgelm_double:
138 for (i=0; i<size; i++) {
139 dval = xbt_dynar_get_as(variable->content,i,double);
140 printf ("%s %f\n",indent,dval);
144 case xbt_cfgelm_string:
145 for (i=0; i<size; i++) {
146 sval = xbt_dynar_get_as(variable->content,i,char*);
147 printf ("%s %s\n",indent,sval);
151 case xbt_cfgelm_peer:
152 for (i=0; i<size; i++) {
153 hval = xbt_dynar_get_as(variable->content,i,xbt_peer_t);
154 printf ("%s %s:%d\n",indent,hval->name,hval->port);
159 printf("%s Invalid type!!\n",indent);
164 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
167 xbt_dict_cursor_free(&cursor);
172 * free an config element
175 void xbt_cfgelm_free(void *data) {
176 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
179 xbt_dynar_free(&(c->content));
183 /*----[ Registering stuff ]-----------------------------------------------*/
185 /** @brief Register an element within a config set
187 * @arg cfg the config set
188 * @arg type the type of the config element
189 * @arg min the minimum
190 * @arg max the maximum
194 xbt_cfg_register(xbt_cfg_t cfg,
195 const char *name, e_xbt_cfgelm_type_t type,
197 xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
203 xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_peer,
204 "type of %s not valid (%d should be between %d and %d)",
205 name,type,xbt_cfgelm_int, xbt_cfgelm_peer);
206 DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",
207 name,min,max,xbt_cfgelm_type_name[type],type);
209 res = xbt_dict_get((xbt_dict_t)cfg,name);
211 if (e.category == not_found_error) {
220 WARN1("Config elem %s registered twice.",name);
221 /* Will be removed by the insertion of the new one */
224 res=xbt_new(s_xbt_cfgelm_t,1);
229 res->cb_set = cb_set;
234 res->content = xbt_dynar_new(sizeof(int), NULL);
237 case xbt_cfgelm_double:
238 res->content = xbt_dynar_new(sizeof(double), NULL);
241 case xbt_cfgelm_string:
242 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
245 case xbt_cfgelm_peer:
246 res->content = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
250 ERROR1("%d is an invalide type code",type);
253 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
256 /** @brief Unregister an element from a config set.
258 * @arg cfg the config set
259 * @arg name the name of the elem to be freed
261 * Note that it removes both the description and the actual content.
262 * Throws not_found when no such element exists.
266 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
267 xbt_dict_remove((xbt_dict_t)cfg,name);
271 * @brief Parse a string and register the stuff described.
273 * @arg cfg the config set
274 * @arg entry a string describing the element to register
276 * The string may consist in several variable descriptions separated by a space.
277 * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
278 * with type being one of 'string','int', 'peer' or 'double'.
282 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
283 char *entrycpy=xbt_strdup(entry);
287 e_xbt_cfgelm_type_t type;
289 tok=strchr(entrycpy, ':');
290 xbt_assert2(tok,"Invalid config element descriptor: %s%s",
292 "; Should be <name>:<min nb>_to_<max nb>_<type>");
295 min=strtol(tok, &tok, 10);
296 xbt_assert1(tok,"Invalid minimum in config element descriptor %s",entry);
298 xbt_assert2(strcmp(tok,"_to_"),
299 "Invalid config element descriptor : %s%s",
301 "; Should be <name>:<min nb>_to_<max nb>_<type>");
302 tok += strlen("_to_");
304 max=strtol(tok, &tok, 10);
305 xbt_assert1(tok,"Invalid maximum in config element descriptor %s",entry);
307 xbt_assert2( *(tok++) =='_',
308 "Invalid config element descriptor: %s%s",entry,
309 "; Should be <name>:<min nb>_to_<max nb>_<type>");
312 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
314 xbt_assert2(type < xbt_cfgelm_type_count,
315 "Invalid type in config element descriptor: %s%s",entry,
316 "; Should be one of 'string', 'int', 'peer' or 'double'.");
318 xbt_cfg_register(cfg,entrycpy,type,min,max,NULL,NULL);
320 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
323 /** @brief Check that each variable have the right amount of values */
326 xbt_cfg_check(xbt_cfg_t cfg) {
327 xbt_dict_cursor_t cursor;
328 xbt_cfgelm_t variable;
332 xbt_assert0(cfg,"NULL config set.");
334 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
335 size = xbt_dynar_length(variable->content);
336 if (variable->min > size) {
337 xbt_dict_cursor_free(&cursor);
338 THROW4(mismatch_error,0,
339 "Config elem %s needs at least %d %s, but there is only %d values.",
342 xbt_cfgelm_type_name[variable->type],
346 if (variable->max > 0 && variable->max < size) {
347 xbt_dict_cursor_free(&cursor);
348 THROW4(mismatch_error,0,
349 "Config elem %s accepts at most %d %s, but there is %d values.",
352 xbt_cfgelm_type_name[variable->type],
357 xbt_dict_cursor_free(&cursor);
360 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
362 e_xbt_cfgelm_type_t type){
363 xbt_cfgelm_t res=NULL;
367 res = xbt_dict_get((xbt_dict_t)cfg,name);
369 if (e.category == not_found_error) {
371 THROW1(not_found_error,0,
372 "No registered variable '%s' in this config set",name);
377 xbt_assert3(type == xbt_cfgelm_any || res->type == type,
378 "You tried to access to the config element %s as an %s, but its type is %s.",
380 xbt_cfgelm_type_name[type],
381 xbt_cfgelm_type_name[res->type]);
386 /** @brief Get the type of this variable in that configuration set
388 * \arg cfg the config set
389 * \arg name the name of the element
390 * \arg type the result
395 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
397 xbt_cfgelm_t variable = NULL;
401 variable = xbt_dict_get((xbt_dict_t)cfg,name);
403 if (e.category == not_found_error) {
405 THROW1(not_found_error,0,
406 "Can't get the type of '%s' since this variable does not exist",name);
411 INFO1("type in variable = %d",variable->type);
413 return variable->type;
416 /*----[ Setting ]---------------------------------------------------------*/
417 /** @brief va_args version of xbt_cfg_set
419 * \arg cfg config set to fill
420 * \arg n variable name
421 * \arg pa variable value
423 * Add some values to the config set.
426 xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa) {
430 e_xbt_cfgelm_type_t type=0; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
435 type = xbt_cfg_get_type(cfg,name);
437 if (e.category == not_found_error) {
439 THROW1(not_found_error,0,"Can't set the property '%s' since it's not registered",name);
445 case xbt_cfgelm_peer:
446 str = va_arg(pa, char *);
448 xbt_cfg_set_peer(cfg,name,str,i);
451 case xbt_cfgelm_string:
452 str=va_arg(pa, char *);
453 xbt_cfg_set_string(cfg, name, str);
458 xbt_cfg_set_int(cfg,name,i);
461 case xbt_cfgelm_double:
463 xbt_cfg_set_double(cfg,name,d);
467 xbt_assert2(0,"Config element variable %s not valid (type=%d)",name,type);
471 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
473 * \arg cfg config set to fill
474 * \arg name variable name
475 * \arg varargs variable value
478 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...) {
482 xbt_cfg_set_vargs(cfg,name,pa);
486 /** @brief Add values parsed from a string into a config set
488 * \arg cfg config set to fill
489 * \arg options a string containing the content to add to the config set. This
490 * is a '\\t',' ' or '\\n' separated list of variables. Each individual variable is
491 * like "[name]:[value]" where [name] is the name of an already registred
492 * variable, and [value] conforms to the data type under which this variable was
495 * @todo This is a crude manual parser, it should be a proper lexer.
499 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
506 xbt_cfgelm_t variable=NULL;
507 char *optionlist_cpy;
508 char *option, *name,*val;
513 if (!options || !strlen(options)) { /* nothing to do */
516 optionlist_cpy=xbt_strdup(options);
518 DEBUG1("List to parse and set:'%s'",options);
519 option=optionlist_cpy;
520 while (1) { /* breaks in the code */
526 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
527 name,len,(long)(option-name));
530 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
531 DEBUG1("Take %c.",*option);
534 if (option-name == len) {
535 DEBUG0("Boundary=EOL");
536 option=NULL; /* don't do next iteration */
539 DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
540 *option,len,(long)(option-name));
542 /* Pass the following blank chars */
544 while (option-name<(len-1) &&
545 (*option == ' ' || *option == '\n' || *option == '\t')) {
546 /* fprintf(stderr,"Ignore a blank char.\n");*/
549 if (option-name == len-1)
550 option=NULL; /* don't do next iteration */
552 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
554 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
559 val=strchr(name,':');
561 free(optionlist_cpy);
563 "Option '%s' badly formated. Should be of the form 'name:value'",
568 DEBUG2("name='%s';val='%s'",name,val);
571 variable = xbt_dict_get((xbt_dict_t)cfg,name);
573 /* put it back on what won't get freed, ie within "options" and out of "optionlist_cpy" */
574 name = (char*) (optionlist_cpy-name + options);
575 free(optionlist_cpy);
576 if (e.category == not_found_error) {
578 THROW1(not_found_error,0,"No registrated variable corresponding to '%s'.",name);
584 switch (variable->type) {
585 case xbt_cfgelm_string:
586 xbt_cfg_set_string(cfg, name, val); /* throws */
590 i=strtol(val, &val, 0);
592 free(optionlist_cpy);
594 "Value of option %s not valid. Should be an integer",
598 xbt_cfg_set_int(cfg,name,i); /* throws */
601 case xbt_cfgelm_double:
604 free(optionlist_cpy);
606 "Value of option %s not valid. Should be a double",
610 xbt_cfg_set_double(cfg,name,d); /* throws */
613 case xbt_cfgelm_peer:
617 free(optionlist_cpy);
619 "Value of option %s not valid. Should be an peer (machine:port)",
624 i=strtol(val, &val, 0);
626 free(optionlist_cpy);
628 "Value of option %s not valid. Should be an peer (machine:port)",
632 xbt_cfg_set_peer(cfg,name,str,i); /* throws */
636 THROW1(unknown_error,0,"Type of config element %s is not valid.",name);
639 free(optionlist_cpy);
643 free(optionlist_cpy);
647 /** @brief Set or add an integer value to \a name within \a cfg
649 * \arg cfg the config set
650 * \arg name the name of the variable
651 * \arg val the value of the variable
654 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
655 xbt_cfgelm_t variable;
657 VERB2("Configuration setting: %s=%d",name,val);
658 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
660 if (variable->max == 1) {
661 if (variable->cb_rm && xbt_dynar_length(variable->content))
662 (*variable->cb_rm)(name, 0);
664 xbt_dynar_set(variable->content,0,&val);
666 if (variable->max && xbt_dynar_length(variable->content) == (unsigned long)variable->max)
667 THROW3(mismatch_error,0,
668 "Cannot add value %d to the config element %s since it's already full (size=%d)",
669 val,name,variable->max);
671 xbt_dynar_push(variable->content,&val);
674 if (variable->cb_set)
675 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
678 /** @brief Set or add a double value to \a name within \a cfg
680 * \arg cfg the config set
681 * \arg name the name of the variable
682 * \arg val the doule to set
686 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
687 xbt_cfgelm_t variable;
689 VERB2("Configuration setting: %s=%f",name,val);
690 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
692 if (variable->max == 1) {
693 if (variable->cb_rm && xbt_dynar_length(variable->content))
694 (*variable->cb_rm)(name, 0);
696 xbt_dynar_set(variable->content,0,&val);
698 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
699 THROW3(mismatch_error,0,
700 "Cannot add value %f to the config element %s since it's already full (size=%d)",
701 val,name,variable->max);
703 xbt_dynar_push(variable->content,&val);
706 if (variable->cb_set)
707 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
710 /** @brief Set or add a string value to \a name within \a cfg
712 * \arg cfg the config set
713 * \arg name the name of the variable
714 * \arg val the value to be added
719 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
720 xbt_cfgelm_t variable;
721 char *newval = xbt_strdup(val);
723 VERB2("Configuration setting: %s=%s",name,val);
724 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
726 if (variable->max == 1) {
727 if (xbt_dynar_length(variable->content)) {
729 (*variable->cb_rm)(name, 0);
730 else if (variable->type == xbt_cfgelm_string) {
731 char * sval=xbt_dynar_get_as(variable->content,0,char*);
736 xbt_dynar_set(variable->content,0,&newval);
738 if (variable->max && xbt_dynar_length(variable->content) == variable->max)
739 THROW3(mismatch_error,0,
740 "Cannot add value %s to the config element %s since it's already full (size=%d)",
741 name,val,variable->max);
743 xbt_dynar_push(variable->content,&newval);
746 if (variable->cb_set)
747 (*variable->cb_set)(name, xbt_dynar_length(variable->content) -1);
750 /** @brief Set or add an peer value to \a name within \a cfg
752 * \arg cfg the config set
753 * \arg name the name of the variable
755 * \arg port the port number
757 * \e peer values are composed of a string (peername) and an integer (port)
761 xbt_cfg_set_peer(xbt_cfg_t cfg,const char*name,
762 const char *peer,int port) {
763 xbt_cfgelm_t variable;
764 xbt_peer_t val=xbt_peer_new(peer,port);
766 VERB3("Configuration setting: %s=%s:%d",name,peer,port);
768 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
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 peer,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 peer value from a variable
886 * \arg cfg the config set
887 * \arg name the name of the variable
888 * \arg peer the peername
889 * \arg port the port number
893 xbt_cfg_rm_peer(xbt_cfg_t cfg,const char*name, const char *peer,int port) {
894 xbt_cfgelm_t variable;
898 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
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 peer,port,name,variable->min);
905 xbt_dynar_foreach(variable->content,cpt,seen) {
906 if (!strcpy(seen->name,peer) && 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=NULL;
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 peer 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 peer the peer
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_peer (xbt_cfg_t cfg, const char *name,
1059 char **peer, int *port) {
1060 xbt_cfgelm_t variable;
1063 variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
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_peer_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=NULL;
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 peer value of a variable */
1131 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
1132 char **peer, int *port) {
1134 xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
1135 xbt_peer_t val = xbt_dynar_get_ptr(variable->content, pos);
1146 XBT_TEST_SUITE("config","Configuration support");
1148 static xbt_cfg_t make_set(){
1151 set = xbt_cfg_new();
1152 xbt_cfg_register_str(set,"speed:1_to_2_int");
1153 xbt_cfg_register_str(set,"peername:1_to_1_string");
1154 xbt_cfg_register_str(set,"user:1_to_10_string");
1157 } /* end_of_make_set */
1159 XBT_TEST_UNIT("memuse",test_config_memuse,"Alloc and free a config set") {
1160 xbt_cfg_t set=make_set();
1161 xbt_test_add0("Alloc and free a config set");
1162 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1167 XBT_TEST_UNIT("validation",test_config_validation,"Validation tests") {
1168 xbt_cfg_t set = set=make_set();
1171 xbt_test_add0("Having too few elements for speed");
1172 xbt_cfg_set_parse(set, "peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
1176 if (e.category != mismatch_error ||
1177 strncmp(e.msg,"Config elem speed needs",strlen("Config elem speed needs")))
1178 xbt_test_fail1("Got an exception. msg=%s",e.msg);
1186 xbt_test_add0("Having too much values of 'speed'");
1188 xbt_cfg_set_parse(set,"peername:toto:42 user:alegrand");
1190 xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
1192 if (e.category != mismatch_error ||
1193 strncmp(e.msg,"Cannot add value 34 to the config elem speed",
1194 strlen("Config elem speed needs")))
1195 xbt_test_fail1("Got an exception. msg=%s",e.msg);
1204 XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
1206 xbt_test_add0("Get a single value");
1208 /* get_single_value */
1210 xbt_cfg_t myset=make_set();
1212 xbt_cfg_set_parse(myset,"peername:toto:42 speed:42");
1213 ival = xbt_cfg_get_int(myset,"speed");
1215 xbt_test_fail1("Speed value = %d, I expected 42",ival);
1216 xbt_cfg_free(&myset);
1219 xbt_test_add0("Get multiple values");
1221 /* get_multiple_value */
1223 xbt_cfg_t myset=make_set();
1225 xbt_cfg_set_parse(myset, "peername:veloce user:foo\nuser:bar\tuser:toto");
1226 xbt_cfg_set_parse(myset,"speed:42");
1227 xbt_cfg_check(myset);
1228 dyn = xbt_cfg_get_dynar(myset,"user");
1230 if (xbt_dynar_length(dyn) != 3)
1231 xbt_test_fail1("Dynar length = %d, I expected 3", (int)xbt_dynar_length(dyn));
1233 if (strcmp(xbt_dynar_get_as(dyn,0,char*),"foo"))
1234 xbt_test_fail1("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn,0,char*));
1236 if (strcmp(xbt_dynar_get_as(dyn,1,char*),"bar"))
1237 xbt_test_fail1("Dynar[1] = %s, I expected bar", xbt_dynar_get_as(dyn,1,char*));
1239 if (strcmp(xbt_dynar_get_as(dyn,2,char*),"toto"))
1240 xbt_test_fail1("Dynar[2] = %s, I expected toto", xbt_dynar_get_as(dyn,2,char*));
1241 xbt_cfg_free(&myset);
1244 xbt_test_add0("Access to a non-existant entry");
1246 /* non-existant_entry */
1247 xbt_cfg_t myset=make_set();
1251 xbt_cfg_set_parse(myset, "color:blue");
1253 if (e.category != not_found_error)
1254 xbt_test_exception(e);
1257 xbt_cfg_free(&myset);
1260 #endif /* SIMGRID_TEST */