3 /* config - Dictionnary where the type of each cell 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"
15 #include "xbt/error.h"
16 #include "xbt/dynar.h"
19 #include "xbt/config.h" /* prototypes of this module */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
23 /* xbt_cfgelm_t: the typedef corresponding to a config cell.
25 Both data and DTD are mixed, but fixing it now would prevent me to ever
29 /* Allowed type of the cell */
30 e_xbt_cfgelm_type_t type;
34 (cannot be an union because type host uses both str and i) */
36 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
38 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
39 {"int","double","string","host"};
41 /* Internal stuff used in cache to free a cell */
42 static void xbt_cfgelm_free(void *data);
44 /* Retrieve the cell we'll modify */
45 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
46 e_xbt_cfgelm_type_t type,
47 /* OUT */ xbt_cfgelm_t *whereto);
49 void xbt_cfg_str_free(void *d);
50 void xbt_cfg_host_free(void *d);
52 void xbt_cfg_str_free(void *d){
55 void xbt_cfg_host_free(void *d){
56 xbt_host_t *h=(xbt_host_t*) *(void**)d;
58 if (h->name) xbt_free(h->name);
63 /*----[ Memory management ]-----------------------------------------------*/
70 * Initialise an config set
74 xbt_cfg_t xbt_cfg_new(void) {
75 return (xbt_cfg_t)xbt_dict_new();
81 * \arg whereto the config set to be created
82 * \arg tocopy the source data
87 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
88 xbt_dict_cursor_t cursor=NULL;
89 xbt_cfgelm_t cell=NULL;
93 xbt_assert0(tocopy,"cannot copy NULL config");
95 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,cell) {
96 xbt_cfg_register(*whereto, name, cell->type, cell->min, cell->max);
100 void xbt_cfg_free(xbt_cfg_t *cfg) {
101 xbt_dict_free((xbt_dict_t*)cfg);
106 * \arg name The name to give to this config set
107 * \arg indent what to write at the begining of each line (right number of spaces)
108 * \arg cfg the config set
110 * Dumps a config set for debuging purpose
113 xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
114 xbt_dict_t dict = (xbt_dict_t) cfg;
115 xbt_dict_cursor_t cursor=NULL;
116 xbt_cfgelm_t cell=NULL;
126 printf("%s>> Dumping of the config set '%s':\n",indent,name);
127 xbt_dict_foreach(dict,cursor,key,cell) {
129 printf("%s %s:",indent,key);
131 size = xbt_dynar_length(cell->content);
132 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
133 cell->min,cell->max,xbt_cfgelm_type_name[cell->type],
136 switch (cell->type) {
139 for (i=0; i<size; i++) {
140 ival = xbt_dynar_get_as(cell->content,i,int);
141 printf ("%s %d\n",indent,ival);
145 case xbt_cfgelm_double:
146 for (i=0; i<size; i++) {
147 dval = xbt_dynar_get_as(cell->content,i,double);
148 printf ("%s %f\n",indent,dval);
152 case xbt_cfgelm_string:
153 for (i=0; i<size; i++) {
154 sval = xbt_dynar_get_as(cell->content,i,char*);
155 printf ("%s %s\n",indent,sval);
159 case xbt_cfgelm_host:
160 for (i=0; i<size; i++) {
161 hval = xbt_dynar_get_as(cell->content,i,xbt_host_t*);
162 printf ("%s %s:%d\n",indent,hval->name,hval->port);
167 printf("%s Invalid type!!\n",indent);
172 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
175 xbt_dict_cursor_free(&cursor);
182 * \arg data the data to be freed (typed as void* to be usable as free funct in dict)
184 * free an config element
187 void xbt_cfgelm_free(void *data) {
188 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
191 xbt_dynar_free(&(c->content));
195 /*----[ Registering stuff ]-----------------------------------------------*/
199 * \arg cfg the config set
200 * \arg type the type of the config element
201 * \arg min the minimum
202 * \arg max the maximum
204 * register an element within a config set
208 xbt_cfg_register(xbt_cfg_t cfg,
209 const char *name, e_xbt_cfgelm_type_t type,
214 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,xbt_cfgelm_type_name[type]);
215 errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
217 if (errcode == no_error) {
218 WARN1("Config elem %s registered twice.",name);
219 /* Will be removed by the insertion of the new one */
221 xbt_assert_error(mismatch_error);
223 res=xbt_new(s_xbt_cfgelm_t,1);
231 res->content = xbt_dynar_new(sizeof(int), NULL);
234 case xbt_cfgelm_double:
235 res->content = xbt_dynar_new(sizeof(double), NULL);
238 case xbt_cfgelm_string:
239 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
242 case xbt_cfgelm_host:
243 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
247 ERROR1("%d is an invalide type code",type);
250 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
254 * xbt_cfg_unregister:
256 * \arg cfg the config set
257 * \arg name the name of the elem to be freed
259 * unregister an element from a config set.
260 * Note that it removes both the DTD and the actual content.
264 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
265 return xbt_dict_remove((xbt_dict_t)cfg,name);
269 * xbt_cfg_register_str:
271 * \arg cfg the config set
272 * \arg entry a string describing the element to register
274 * Parse a string and register the stuff described.
278 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
279 char *entrycpy=xbt_strdup(entry);
283 e_xbt_cfgelm_type_t type;
285 tok=strchr(entrycpy, ':');
288 "Invalid config element descriptor: ",entry,
289 "; Should be <name>:<min nb>_to_<max nb>_<type>");
295 min=strtol(tok, &tok, 10);
297 ERROR1("Invalid minimum in config element descriptor %s",entry);
302 if (!strcmp(tok,"_to_")){
304 "Invalid config element descriptor: ",entry,
305 "; Should be <name>:<min nb>_to_<max nb>_<type>");
309 tok += strlen("_to_");
311 max=strtol(tok, &tok, 10);
313 ERROR1("Invalid maximum in config element descriptor %s",entry);
320 "Invalid config element descriptor: ",entry,
321 "; Should be <name>:<min nb>_to_<max nb>_<type>");
327 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
329 if (type == xbt_cfgelm_type_count) {
331 "Invalid type in config element descriptor: ",entry,
332 "; Should be one of 'string', 'int', 'host' or 'double'.");
337 xbt_cfg_register(cfg,entrycpy,type,min,max);
339 xbt_free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
346 * \arg cfg the config set
348 * Check the config set
352 xbt_cfg_check(xbt_cfg_t cfg) {
353 xbt_dict_cursor_t cursor;
358 xbt_assert0(cfg,"NULL config set.");
360 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,cell) {
361 size = xbt_dynar_length(cell->content);
362 if (cell->min > size) {
363 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
366 xbt_cfgelm_type_name[cell->type],
368 xbt_dict_cursor_free(&cursor);
369 return mismatch_error;
372 if (cell->max < size) {
373 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
376 xbt_cfgelm_type_name[cell->type],
378 xbt_dict_cursor_free(&cursor);
379 return mismatch_error;
384 xbt_dict_cursor_free(&cursor);
388 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg,
390 e_xbt_cfgelm_type_t type,
391 /* OUT */ xbt_cfgelm_t *whereto){
393 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
396 if (errcode == mismatch_error) {
397 ERROR1("No registered cell %s in this config set",
399 return mismatch_error;
401 if (errcode != no_error)
404 xbt_assert3((*whereto)->type == type,
405 "You tried to access to the config element %s as an %s, but its type is %s.",
407 xbt_cfgelm_type_name[type],
408 xbt_cfgelm_type_name[(*whereto)->type]);
416 * \arg cfg the config set
417 * \arg name the name of the element
418 * \arg type the result
420 * Give the type of the config element
424 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name,
425 /* OUT */e_xbt_cfgelm_type_t *type) {
430 TRYCATCH(mismatch_error,xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
432 if (errcode == mismatch_error) {
433 ERROR1("Can't get the type of '%s' since this cell does not exist",
435 return mismatch_error;
443 /*----[ Setting ]---------------------------------------------------------*/
445 * xbt_cfg_set_vargs():
446 * \arg cfg config set to fill
447 * \arg varargs NULL-terminated list of pairs {(const char*)key, value}
449 * Add some values to the config set.
450 * \warning if the list isn't NULL terminated, it will segfault.
453 xbt_cfg_set_vargs(xbt_cfg_t cfg, va_list pa) {
457 e_xbt_cfgelm_type_t type;
461 while ((name=va_arg(pa,char *))) {
463 if (!xbt_cfg_get_type(cfg,name,&type)) {
464 ERROR1("Can't set the property '%s' since it's not registered",name);
465 return mismatch_error;
469 case xbt_cfgelm_host:
470 str = va_arg(pa, char *);
472 TRY(xbt_cfg_set_host(cfg,name,str,i));
475 case xbt_cfgelm_string:
476 str=va_arg(pa, char *);
477 TRY(xbt_cfg_set_string(cfg, name, str));
482 TRY(xbt_cfg_set_int(cfg,name,i));
485 case xbt_cfgelm_double:
487 TRY(xbt_cfg_set_double(cfg,name,d));
491 RAISE1(unknown_error,"Config element cell %s not valid.",name);
499 * \arg cfg config set to fill
500 * \arg varargs NULL-terminated list of pairs {(const char*)key, value}
502 * Add some values to the config set.
503 * \warning if the list isn't NULL terminated, it will segfault.
505 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, ...) {
510 errcode=xbt_cfg_set_vargs(cfg,pa);
516 * xbt_cfg_set_parse():
517 * \arg cfg config set to fill
518 * \arg options a string containing the content to add to the config set. This
519 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
520 * like "[name]:[value]" where [name] is the name of an already registred
521 * cell, and [value] conforms to the data type under which this cell was
524 * Add the cells described in a string to a config set.
528 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
534 char *optionlist_cpy;
535 char *option, *name,*val;
541 if (!options || !strlen(options)) { /* nothing to do */
544 optionlist_cpy=xbt_strdup(options);
546 DEBUG1("List to parse and set:'%s'",options);
547 option=optionlist_cpy;
548 while (1) { /* breaks in the code */
554 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
555 name,len,(long)(option-name));
558 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
559 /*fprintf(stderr,"Take %c.\n",*option);*/
562 if (option-name == len) {
563 /*fprintf(stderr,"Boundary=EOL\n");*/
564 option=NULL; /* don't do next iteration */
567 /*fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);*/
569 /* Pass the following blank chars */
571 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
572 /* fprintf(stderr,"Ignore a blank char.\n");*/
575 if (option-name == len-1)
576 option=NULL; /* don't do next iteration */
578 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
580 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
585 val=strchr(name,':');
587 xbt_free(optionlist_cpy);
589 "Malformated option: '%s'; Should be of the form 'name:value'",
594 DEBUG2("name='%s';val='%s'",name,val);
596 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell);
601 ERROR1("No registrated cell corresponding to '%s'.",name);
602 xbt_free(optionlist_cpy);
603 return mismatch_error;
606 xbt_free(optionlist_cpy);
610 switch (cell->type) {
611 case xbt_cfgelm_string:
612 errcode = xbt_cfg_set_string(cfg, name, val);
613 if (errcode != no_error) {
614 xbt_free(optionlist_cpy);
620 i=strtol(val, &val, 0);
622 xbt_free(optionlist_cpy);
624 "Value of option %s not valid. Should be an integer",
628 errcode = xbt_cfg_set_int(cfg,name,i);
629 if (errcode != no_error) {
630 xbt_free(optionlist_cpy);
635 case xbt_cfgelm_double:
638 xbt_free(optionlist_cpy);
640 "Value of option %s not valid. Should be a double",
644 errcode = xbt_cfg_set_double(cfg,name,d);
645 if (errcode != no_error) {
646 xbt_free(optionlist_cpy);
651 case xbt_cfgelm_host:
655 xbt_free(optionlist_cpy);
657 "Value of option %s not valid. Should be an host (machine:port)",
662 i=strtol(val, &val, 0);
664 xbt_free(optionlist_cpy);
666 "Value of option %s not valid. Should be an host (machine:port)",
670 errcode = xbt_cfg_set_host(cfg,name,str,i);
671 if (errcode != no_error) {
672 xbt_free(optionlist_cpy);
678 xbt_free(optionlist_cpy);
679 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
683 xbt_free(optionlist_cpy);
690 * \arg cfg the config set
691 * \arg name the name of the cell
692 * \arg val the value of the cell
694 * Set the value of the cell #name in #cfg with the provided value.
697 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
701 VERB2("Configuration setting: %s=%d",name,val);
702 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
705 xbt_dynar_push(cell->content,&val);
707 xbt_dynar_set(cell->content,0,&val);
713 * xbt_cfg_set_double:
714 * \arg cfg the config set
715 * \arg name the name of the cell
716 * \arg val the doule to set
718 * Set the value of the cell #name in #cfg with the provided value.
722 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
726 VERB2("Configuration setting: %s=%f",name,val);
727 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
730 xbt_dynar_push(cell->content,&val);
732 xbt_dynar_set(cell->content,0,&val);
738 * xbt_cfg_set_string:
740 * \arg cfg the config set
741 * \arg name the name of the cell
742 * \arg val the value to be added
744 * Set the value of the cell #name in #cfg with the provided value.
748 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
751 char *newval = xbt_strdup(val);
753 VERB2("Configuration setting: %s=%s",name,val);
754 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
757 xbt_dynar_push(cell->content,&newval);
759 xbt_dynar_set(cell->content,0,&newval);
767 * \arg cfg the config set
768 * \arg name the name of the cell
770 * \arg port the port number
772 * Set the value of the cell #name in #cfg with the provided value
773 * on the given #host to the given #port
777 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
778 const char *host,int port) {
781 xbt_host_t *val=xbt_new(xbt_host_t,1);
783 VERB3("Configuration setting: %s=%s:%d",name,host,port);
785 val->name = xbt_strdup(name);
788 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
791 xbt_dynar_push(cell->content,&val);
793 xbt_dynar_set(cell->content,0,&val);
798 /* ---- [ Removing ] ---- */
803 * \arg cfg the config set
804 * \arg name the name of the cell
805 * \arg val the value to be removed
807 * Remove the provided #val from the cell #name in #cfg.
809 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
815 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
817 xbt_dynar_foreach(cell->content,cpt,seen) {
819 xbt_dynar_cursor_rm(cell->content,&cpt);
824 ERROR2("Can't remove the value %d of config element %s: value not found.",
826 return mismatch_error;
832 * \arg cfg the config set
833 * \arg name the name of the cell
834 * \arg val the value to be removed
836 * Remove the provided #val from the cell #name in #cfg.
839 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
845 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
847 xbt_dynar_foreach(cell->content,cpt,seen) {
849 xbt_dynar_cursor_rm(cell->content,&cpt);
854 ERROR2("Can't remove the value %f of config element %s: value not found.",
856 return mismatch_error;
862 * \arg cfg the config set
863 * \arg name the name of the cell
864 * \arg val the value of the string which will be removed
866 * Remove the provided #val from the cell #name in #cfg.
869 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
875 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
877 xbt_dynar_foreach(cell->content,cpt,seen) {
878 if (!strcpy(seen,val)) {
879 xbt_dynar_cursor_rm(cell->content,&cpt);
884 ERROR2("Can't remove the value %s of config element %s: value not found.",
886 return mismatch_error;
892 * \arg cfg the config set
893 * \arg name the name of the cell
894 * \arg host the hostname
895 * \arg port the port number
897 * Remove the provided #host:#port from the cell #name in #cfg.
901 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
907 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
909 xbt_dynar_foreach(cell->content,cpt,seen) {
910 if (!strcpy(seen->name,host) && seen->port == port) {
911 xbt_dynar_cursor_rm(cell->content,&cpt);
916 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
918 return mismatch_error;
926 * \arg cfg the config set
927 * \arg name the name of the cell
933 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
938 TRYCATCH(mismatch_error,
939 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
940 if (errcode == mismatch_error) {
941 ERROR1("Can't empty '%s' since this config element does not exist",
943 return mismatch_error;
947 xbt_dynar_reset(cell->content);
952 /*----[ Getting ]---------------------------------------------------------*/
956 * \arg cfg the config set
957 * \arg name the name of the cell
958 * \arg val the wanted value
960 * Returns the first value from the config set under the given name.
961 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
964 * \warning the returned value is the actual content of the config set
967 xbt_cfg_get_int (xbt_cfg_t cfg,
973 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
975 if (xbt_dynar_length(cell->content) > 1) {
976 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
977 name, xbt_dynar_length(cell->content));
980 *val = xbt_dynar_get_as(cell->content, 0, int);
985 * xbt_cfg_get_double:
986 * \arg cfg the config set
987 * \arg name the name of the cell
988 * \arg val the wanted value
990 * Returns the first value from the config set under the given name.
991 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
994 * \warning the returned value is the actual content of the config set
998 xbt_cfg_get_double(xbt_cfg_t cfg,
1002 xbt_error_t errcode;
1004 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
1006 if (xbt_dynar_length(cell->content) > 1) {
1007 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1008 name, xbt_dynar_length(cell->content));
1011 *val = xbt_dynar_get_as(cell->content, 0, double);
1016 * xbt_cfg_get_string:
1018 * \arg th the config set
1019 * \arg name the name of the cell
1020 * \arg val the wanted value
1022 * Returns the first value from the config set under the given name.
1023 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1026 * \warning the returned value is the actual content of the config set
1029 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1033 xbt_error_t errcode;
1037 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
1039 if (xbt_dynar_length(cell->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(cell->content));
1044 *val = xbt_dynar_get_as(cell->content, 0, char *);
1051 * \arg cfg the config set
1052 * \arg name the name of the cell
1053 * \arg host the host
1054 * \arg port the port number
1056 * Returns the first value from the config set under the given name.
1057 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1060 * \warning the returned value is the actual content of the config set
1063 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1068 xbt_error_t errcode;
1071 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
1073 if (xbt_dynar_length(cell->content) > 1) {
1074 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1075 name, xbt_dynar_length(cell->content));
1078 val = xbt_dynar_get_as(cell->content, 0, xbt_host_t*);
1086 * xbt_cfg_get_dynar:
1087 * \arg cfg where to search in
1088 * \arg name what to search for
1091 * Get the data stored in the config bag.
1093 * \warning the returned value is the actual content of the config set
1095 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1097 xbt_dynar_t *dynar) {
1099 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1102 if (errcode == mismatch_error) {
1103 ERROR1("No registered cell %s in this config set",
1105 return mismatch_error;
1107 if (errcode != no_error)
1110 *dynar = cell->content;