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) 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 * @whereto: the config set to be created
82 * @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 * @name: The name to give to this config set
107 * @indent: what to write at the begining of each line (right number of spaces)
108 * @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 * @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 * @cfg: the config set
200 * @type: the type of the config element
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 * @cfg: the config set
257 * @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 * @cfg: the config set
272 * @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 * @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 * @cfg: the config set
417 * @name: the name of the element
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 * @cfg: config set to fill
447 * @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 * @cfg: config set to fill
500 * @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 * @cfg: config set to fill
518 * @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 TRYCLEAN(xbt_cfg_set_string(cfg, name, val),
613 xbt_free(optionlist_cpy));
617 i=strtol(val, &val, 0);
619 xbt_free(optionlist_cpy);
621 "Value of option %s not valid. Should be an integer",
625 TRYCLEAN(xbt_cfg_set_int(cfg,name,i),
626 xbt_free(optionlist_cpy));
629 case xbt_cfgelm_double:
632 xbt_free(optionlist_cpy);
634 "Value of option %s not valid. Should be a double",
638 TRYCLEAN(xbt_cfg_set_double(cfg,name,d),
639 xbt_free(optionlist_cpy));
642 case xbt_cfgelm_host:
646 xbt_free(optionlist_cpy);
648 "Value of option %s not valid. Should be an host (machine:port)",
653 i=strtol(val, &val, 0);
655 xbt_free(optionlist_cpy);
657 "Value of option %s not valid. Should be an host (machine:port)",
661 TRYCLEAN(xbt_cfg_set_host(cfg,name,str,i),
662 xbt_free(optionlist_cpy));
666 xbt_free(optionlist_cpy);
667 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
671 xbt_free(optionlist_cpy);
678 * @cfg: the config set
679 * @name: the name of the cell
680 * @val: the value of the cell
682 * Set the value of the cell @name in @cfg with the provided value.
685 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
689 VERB2("Configuration setting: %s=%d",name,val);
690 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
693 xbt_dynar_push(cell->content,&val);
695 xbt_dynar_set(cell->content,0,&val);
701 * xbt_cfg_set_double:
702 * @cfg: the config set
703 * @name: the name of the cell
704 * @val: the doule to set
706 * Set the value of the cell @name in @cfg with the provided value.
710 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
714 VERB2("Configuration setting: %s=%f",name,val);
715 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
718 xbt_dynar_push(cell->content,&val);
720 xbt_dynar_set(cell->content,0,&val);
726 * xbt_cfg_set_string:
728 * @cfg: the config set
729 * @name: the name of the cell
730 * @val: the value to be added
732 * Set the value of the cell @name in @cfg with the provided value.
736 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
739 char *newval = xbt_strdup(val);
741 VERB2("Configuration setting: %s=%s",name,val);
742 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
745 xbt_dynar_push(cell->content,&newval);
747 xbt_dynar_set(cell->content,0,&newval);
755 * @cfg: the config set
756 * @name: the name of the cell
758 * @port: the port number
760 * Set the value of the cell @name in @cfg with the provided value
761 * on the given @host to the given @port
765 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
766 const char *host,int port) {
769 xbt_host_t *val=xbt_new(xbt_host_t,1);
771 VERB3("Configuration setting: %s=%s:%d",name,host,port);
773 val->name = xbt_strdup(name);
776 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
779 xbt_dynar_push(cell->content,&val);
781 xbt_dynar_set(cell->content,0,&val);
786 /* ---- [ Removing ] ---- */
791 * @cfg: the config set
792 * @name: the name of the cell
793 * @val: the value to be removed
795 * Remove the provided @val from the cell @name in @cfg.
797 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
803 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
805 xbt_dynar_foreach(cell->content,cpt,seen) {
807 xbt_dynar_cursor_rm(cell->content,&cpt);
812 ERROR2("Can't remove the value %d of config element %s: value not found.",
814 return mismatch_error;
820 * @cfg: the config set
821 * @name: the name of the cell
822 * @val: the value to be removed
824 * Remove the provided @val from the cell @name in @cfg.
827 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
833 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
835 xbt_dynar_foreach(cell->content,cpt,seen) {
837 xbt_dynar_cursor_rm(cell->content,&cpt);
842 ERROR2("Can't remove the value %f of config element %s: value not found.",
844 return mismatch_error;
850 * @cfg: the config set
851 * @name: the name of the cell
852 * @val: the value of the string which will be removed
854 * Remove the provided @val from the cell @name in @cfg.
857 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
863 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
865 xbt_dynar_foreach(cell->content,cpt,seen) {
866 if (!strcpy(seen,val)) {
867 xbt_dynar_cursor_rm(cell->content,&cpt);
872 ERROR2("Can't remove the value %s of config element %s: value not found.",
874 return mismatch_error;
880 * @cfg: the config set
881 * @name: the name of the cell
882 * @host: the hostname
883 * @port: the port number
885 * Remove the provided @host:@port from the cell @name in @cfg.
889 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
895 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
897 xbt_dynar_foreach(cell->content,cpt,seen) {
898 if (!strcpy(seen->name,host) && seen->port == port) {
899 xbt_dynar_cursor_rm(cell->content,&cpt);
904 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
906 return mismatch_error;
914 * @cfg: the config set
915 * @name: the name of the cell
921 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
926 TRYCATCH(mismatch_error,
927 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
928 if (errcode == mismatch_error) {
929 ERROR1("Can't empty '%s' since this config element does not exist",
931 return mismatch_error;
935 xbt_dynar_reset(cell->content);
940 /*----[ Getting ]---------------------------------------------------------*/
944 * @cfg: the config set
945 * @name: the name of the cell
946 * @val: the wanted value
948 * Returns the first value from the config set under the given name.
949 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
952 * @warning the returned value is the actual content of the config set
955 xbt_cfg_get_int (xbt_cfg_t cfg,
961 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
963 if (xbt_dynar_length(cell->content) > 1) {
964 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
965 name, xbt_dynar_length(cell->content));
968 *val = xbt_dynar_get_as(cell->content, 0, int);
973 * xbt_cfg_get_double:
974 * @cfg: the config set
975 * @name: the name of the cell
976 * @val: the wanted value
978 * Returns the first value from the config set under the given name.
979 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
982 * @warning the returned value is the actual content of the config set
986 xbt_cfg_get_double(xbt_cfg_t cfg,
992 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
994 if (xbt_dynar_length(cell->content) > 1) {
995 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
996 name, xbt_dynar_length(cell->content));
999 *val = xbt_dynar_get_as(cell->content, 0, double);
1004 * xbt_cfg_get_string:
1006 * @th: the config set
1007 * @name: the name of the cell
1008 * @val: the wanted value
1010 * Returns the first value from the config set under the given name.
1011 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1014 * @warning the returned value is the actual content of the config set
1017 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1021 xbt_error_t errcode;
1025 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
1027 if (xbt_dynar_length(cell->content) > 1) {
1028 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1029 name, xbt_dynar_length(cell->content));
1032 *val = xbt_dynar_get_as(cell->content, 0, char *);
1039 * @cfg: the config set
1040 * @name: the name of the cell
1042 * @port: the port number
1044 * Returns the first value from the config set under the given name.
1045 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1048 * @warning the returned value is the actual content of the config set
1051 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1056 xbt_error_t errcode;
1059 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
1061 if (xbt_dynar_length(cell->content) > 1) {
1062 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1063 name, xbt_dynar_length(cell->content));
1066 val = xbt_dynar_get_as(cell->content, 0, xbt_host_t*);
1074 * xbt_cfg_get_dynar:
1075 * @cfg: where to search in
1076 * @name: what to search for
1079 * Get the data stored in the config bag.
1081 * @warning the returned value is the actual content of the config set
1083 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1085 xbt_dynar_t *dynar) {
1087 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1090 if (errcode == mismatch_error) {
1091 ERROR1("No registered cell %s in this config set",
1093 return mismatch_error;
1095 if (errcode != no_error)
1098 *dynar = cell->content;