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 /* Authors: Martin Quinson */
8 /* Copyright (C) 2001,2002,2003,2004 the OURAGAN project. */
10 /* This program is free software; you can redistribute it and/or modify it
11 under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "xbt/sysdep.h"
16 #include "xbt/error.h"
17 #include "xbt/dynar.h"
20 #include "xbt/config.h" /* prototypes of this module */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(config,xbt,"configuration support");
24 /* xbt_cfgelm_t: the typedef corresponding to a config cell.
26 Both data and DTD are mixed, but fixing it now would prevent me to ever
30 /* Allowed type of the cell */
31 e_xbt_cfgelm_type_t type;
35 (cannot be an union because type host uses both str and i) */
37 } s_xbt_cfgelm_t,*xbt_cfgelm_t;
39 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count]=
40 {"int","double","string","host"};
42 /* Internal stuff used in cache to free a cell */
43 static void xbt_cfgelm_free(void *data);
45 /* Retrieve the cell we'll modify */
46 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
47 e_xbt_cfgelm_type_t type,
48 /* OUT */ xbt_cfgelm_t *whereto);
50 void xbt_cfg_str_free(void *d);
51 void xbt_cfg_host_free(void *d);
53 void xbt_cfg_str_free(void *d){
56 void xbt_cfg_host_free(void *d){
57 xbt_host_t *h=(xbt_host_t*) *(void**)d;
59 if (h->name) xbt_free(h->name);
64 /*----[ Memory management ]-----------------------------------------------*/
71 * Initialise an config set
75 xbt_cfg_t xbt_cfg_new(void) {
76 return (xbt_cfg_t)xbt_dict_new();
82 * @whereto: the config set to be created
83 * @tocopy: the source data
88 xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
89 xbt_dict_cursor_t cursor=NULL;
90 xbt_cfgelm_t cell=NULL;
94 xbt_assert0(tocopy,"cannot copy NULL config");
96 xbt_dict_foreach((xbt_dict_t)tocopy,cursor,name,cell) {
97 xbt_cfg_register(*whereto, name, cell->type, cell->min, cell->max);
101 void xbt_cfg_free(xbt_cfg_t *cfg) {
102 xbt_dict_free((xbt_dict_t*)cfg);
107 * @name: The name to give to this config set
108 * @indent: what to write at the begining of each line (right number of spaces)
109 * @cfg: the config set
111 * Dumps a config set for debuging purpose
114 xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
115 xbt_dict_t dict = (xbt_dict_t) cfg;
116 xbt_dict_cursor_t cursor=NULL;
117 xbt_cfgelm_t cell=NULL;
128 printf("%s>> Dumping of the config set '%s':\n",indent,name);
129 xbt_dict_foreach(dict,cursor,key,cell) {
131 printf("%s %s:",indent,key);
133 size = xbt_dynar_length(cell->content);
134 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
135 cell->min,cell->max,xbt_cfgelm_type_name[cell->type],
138 switch (cell->type) {
141 for (i=0; i<size; i++) {
142 ival = xbt_dynar_get_as(cell->content,i,int);
143 printf ("%s %d\n",indent,ival);
147 case xbt_cfgelm_double:
148 for (i=0; i<size; i++) {
149 dval = xbt_dynar_get_as(cell->content,i,double);
150 printf ("%s %f\n",indent,dval);
154 case xbt_cfgelm_string:
155 for (i=0; i<size; i++) {
156 sval = xbt_dynar_get_as(cell->content,i,char*);
157 printf ("%s %s\n",indent,sval);
161 case xbt_cfgelm_host:
162 for (i=0; i<size; i++) {
163 hval = xbt_dynar_get_as(cell->content,i,xbt_host_t*);
164 printf ("%s %s:%d\n",indent,hval->name,hval->port);
169 printf("%s Invalid type!!\n",indent);
174 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
177 xbt_dict_cursor_free(&cursor);
184 * @data: the data to be freed (typed as void* to be usable as free funct in dict)
186 * free an config element
189 void xbt_cfgelm_free(void *data) {
190 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
193 xbt_dynar_free(&(c->content));
197 /*----[ Registering stuff ]-----------------------------------------------*/
201 * @cfg: the config set
202 * @type: the type of the config element
206 * register an element within a config set
210 xbt_cfg_register(xbt_cfg_t cfg,
211 const char *name, e_xbt_cfgelm_type_t type,
216 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,xbt_cfgelm_type_name[type]);
217 errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
219 if (errcode == no_error) {
220 WARN1("Config elem %s registered twice.",name);
221 /* Will be removed by the insertion of the new one */
223 xbt_assert_error(mismatch_error);
225 res=xbt_new(s_xbt_cfgelm_t,1);
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_host:
245 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
249 ERROR1("%d is an invalide type code",type);
252 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
256 * xbt_cfg_unregister:
258 * @cfg: the config set
259 * @name: the name of the elem to be freed
261 * unregister an element from a config set.
262 * Note that it removes both the DTD and the actual content.
266 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
267 return xbt_dict_remove((xbt_dict_t)cfg,name);
271 * xbt_cfg_register_str:
273 * @cfg: the config set
274 * @entry: a string describing the element to register
276 * Parse a string and register the stuff described.
280 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
281 char *entrycpy=xbt_strdup(entry);
285 e_xbt_cfgelm_type_t type;
287 tok=strchr(entrycpy, ':');
290 "Invalid config element descriptor: ",entry,
291 "; Should be <name>:<min nb>_to_<max nb>_<type>");
297 min=strtol(tok, &tok, 10);
299 ERROR1("Invalid minimum in config element descriptor %s",entry);
304 if (!strcmp(tok,"_to_")){
306 "Invalid config element descriptor: ",entry,
307 "; Should be <name>:<min nb>_to_<max nb>_<type>");
311 tok += strlen("_to_");
313 max=strtol(tok, &tok, 10);
315 ERROR1("Invalid maximum in config element descriptor %s",entry);
322 "Invalid config element descriptor: ",entry,
323 "; Should be <name>:<min nb>_to_<max nb>_<type>");
329 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
331 if (type == xbt_cfgelm_type_count) {
333 "Invalid type in config element descriptor: ",entry,
334 "; Should be one of 'string', 'int', 'host' or 'double'.");
339 xbt_cfg_register(cfg,entrycpy,type,min,max);
341 xbt_free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
348 * @cfg: the config set
350 * Check the config set
354 xbt_cfg_check(xbt_cfg_t cfg) {
355 xbt_dict_cursor_t cursor;
360 xbt_assert0(cfg,"NULL config set.");
362 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,cell) {
363 size = xbt_dynar_length(cell->content);
364 if (cell->min > size) {
365 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
368 xbt_cfgelm_type_name[cell->type],
370 xbt_dict_cursor_free(&cursor);
371 return mismatch_error;
374 if (cell->max < size) {
375 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
378 xbt_cfgelm_type_name[cell->type],
380 xbt_dict_cursor_free(&cursor);
381 return mismatch_error;
386 xbt_dict_cursor_free(&cursor);
390 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg,
392 e_xbt_cfgelm_type_t type,
393 /* OUT */ xbt_cfgelm_t *whereto){
395 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
398 if (errcode == mismatch_error) {
399 ERROR1("No registered cell %s in this config set",
401 return mismatch_error;
403 if (errcode != no_error)
406 xbt_assert3((*whereto)->type == type,
407 "You tried to access to the config element %s as an %s, but its type is %s.",
409 xbt_cfgelm_type_name[type],
410 xbt_cfgelm_type_name[(*whereto)->type]);
418 * @cfg: the config set
419 * @name: the name of the element
422 * Give the type of the config element
426 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name,
427 /* OUT */e_xbt_cfgelm_type_t *type) {
432 TRYCATCH(mismatch_error,xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
434 if (errcode == mismatch_error) {
435 ERROR1("Can't get the type of '%s' since this cell does not exist",
437 return mismatch_error;
445 /*----[ Setting ]---------------------------------------------------------*/
447 * xbt_cfg_set_vargs():
448 * @cfg: config set to fill
449 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
451 * Add some values to the config set.
452 * @warning: if the list isn't NULL terminated, it will segfault.
455 xbt_cfg_set_vargs(xbt_cfg_t cfg, va_list pa) {
459 e_xbt_cfgelm_type_t type;
463 while ((name=va_arg(pa,char *))) {
465 if (!xbt_cfg_get_type(cfg,name,&type)) {
466 ERROR1("Can't set the property '%s' since it's not registered",name);
467 return mismatch_error;
471 case xbt_cfgelm_host:
472 str = va_arg(pa, char *);
474 TRY(xbt_cfg_set_host(cfg,name,str,i));
477 case xbt_cfgelm_string:
478 str=va_arg(pa, char *);
479 TRY(xbt_cfg_set_string(cfg, name, str));
484 TRY(xbt_cfg_set_int(cfg,name,i));
487 case xbt_cfgelm_double:
489 TRY(xbt_cfg_set_double(cfg,name,d));
493 RAISE1(unknown_error,"Config element cell %s not valid.",name);
501 * @cfg: config set to fill
502 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
504 * Add some values to the config set.
505 * @warning: if the list isn't NULL terminated, it will segfault.
507 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, ...) {
512 errcode=xbt_cfg_set_vargs(cfg,pa);
518 * xbt_cfg_set_parse():
519 * @cfg: config set to fill
520 * @options: a string containing the content to add to the config set. This
521 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
522 * like "[name]:[value]" where [name] is the name of an already registred
523 * cell, and [value] conforms to the data type under which this cell was
526 * Add the cells described in a string to a config set.
530 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
536 char *optionlist_cpy;
537 char *option, *name,*val;
543 if (!options || !strlen(options)) { /* nothing to do */
546 optionlist_cpy=xbt_strdup(options);
548 DEBUG1("List to parse and set:'%s'",options);
549 option=optionlist_cpy;
550 while (1) { /* breaks in the code */
556 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
557 name,len,(long)(option-name));
560 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
561 /*fprintf(stderr,"Take %c.\n",*option);*/
564 if (option-name == len) {
565 /*fprintf(stderr,"Boundary=EOL\n");*/
566 option=NULL; /* don't do next iteration */
569 /*fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);*/
571 /* Pass the following blank chars */
573 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
574 /* fprintf(stderr,"Ignore a blank char.\n");*/
577 if (option-name == len-1)
578 option=NULL; /* don't do next iteration */
580 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
582 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
587 val=strchr(name,':');
589 xbt_free(optionlist_cpy);
591 "Malformated option: '%s'; Should be of the form 'name:value'",
596 DEBUG2("name='%s';val='%s'",name,val);
598 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell);
603 ERROR1("No registrated cell corresponding to '%s'.",name);
604 xbt_free(optionlist_cpy);
605 return mismatch_error;
608 xbt_free(optionlist_cpy);
612 switch (cell->type) {
613 case xbt_cfgelm_string:
614 TRYCLEAN(xbt_cfg_set_string(cfg, name, val),
615 xbt_free(optionlist_cpy));
619 i=strtol(val, &val, 0);
621 xbt_free(optionlist_cpy);
623 "Value of option %s not valid. Should be an integer",
627 TRYCLEAN(xbt_cfg_set_int(cfg,name,i),
628 xbt_free(optionlist_cpy));
631 case xbt_cfgelm_double:
634 xbt_free(optionlist_cpy);
636 "Value of option %s not valid. Should be a double",
640 TRYCLEAN(xbt_cfg_set_double(cfg,name,d),
641 xbt_free(optionlist_cpy));
644 case xbt_cfgelm_host:
648 xbt_free(optionlist_cpy);
650 "Value of option %s not valid. Should be an host (machine:port)",
655 i=strtol(val, &val, 0);
657 xbt_free(optionlist_cpy);
659 "Value of option %s not valid. Should be an host (machine:port)",
663 TRYCLEAN(xbt_cfg_set_host(cfg,name,str,i),
664 xbt_free(optionlist_cpy));
668 xbt_free(optionlist_cpy);
669 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
673 xbt_free(optionlist_cpy);
680 * @cfg: the config set
681 * @name: the name of the cell
682 * @val: the value of the cell
684 * Set the value of the cell @name in @cfg with the provided value.
687 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
691 VERB2("Configuration setting: %s=%d",name,val);
692 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
695 xbt_dynar_push(cell->content,&val);
697 xbt_dynar_set(cell->content,0,&val);
703 * xbt_cfg_set_double:
704 * @cfg: the config set
705 * @name: the name of the cell
706 * @val: the doule to set
708 * Set the value of the cell @name in @cfg with the provided value.
712 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
716 VERB2("Configuration setting: %s=%f",name,val);
717 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
720 xbt_dynar_push(cell->content,&val);
722 xbt_dynar_set(cell->content,0,&val);
728 * xbt_cfg_set_string:
730 * @cfg: the config set
731 * @name: the name of the cell
732 * @val: the value to be added
734 * Set the value of the cell @name in @cfg with the provided value.
738 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
741 char *newval = xbt_strdup(val);
743 VERB2("Configuration setting: %s=%s",name,val);
744 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
747 xbt_dynar_push(cell->content,&newval);
749 xbt_dynar_set(cell->content,0,&newval);
757 * @cfg: the config set
758 * @name: the name of the cell
760 * @port: the port number
762 * Set the value of the cell @name in @cfg with the provided value
763 * on the given @host to the given @port
767 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
768 const char *host,int port) {
771 xbt_host_t *val=xbt_new(xbt_host_t,1);
773 VERB3("Configuration setting: %s=%s:%d",name,host,port);
775 val->name = xbt_strdup(name);
778 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
781 xbt_dynar_push(cell->content,&val);
783 xbt_dynar_set(cell->content,0,&val);
788 /* ---- [ Removing ] ---- */
793 * @cfg: the config set
794 * @name: the name of the cell
795 * @val: the value to be removed
797 * Remove the provided @val from the cell @name in @cfg.
799 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
805 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
807 xbt_dynar_foreach(cell->content,cpt,seen) {
809 xbt_dynar_cursor_rm(cell->content,&cpt);
814 ERROR2("Can't remove the value %d of config element %s: value not found.",
816 return mismatch_error;
822 * @cfg: the config set
823 * @name: the name of the cell
824 * @val: the value to be removed
826 * Remove the provided @val from the cell @name in @cfg.
829 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
835 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
837 xbt_dynar_foreach(cell->content,cpt,seen) {
839 xbt_dynar_cursor_rm(cell->content,&cpt);
844 ERROR2("Can't remove the value %f of config element %s: value not found.",
846 return mismatch_error;
852 * @cfg: the config set
853 * @name: the name of the cell
854 * @val: the value of the string which will be removed
856 * Remove the provided @val from the cell @name in @cfg.
859 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
865 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
867 xbt_dynar_foreach(cell->content,cpt,seen) {
868 if (!strcpy(seen,val)) {
869 xbt_dynar_cursor_rm(cell->content,&cpt);
874 ERROR2("Can't remove the value %s of config element %s: value not found.",
876 return mismatch_error;
882 * @cfg: the config set
883 * @name: the name of the cell
884 * @host: the hostname
885 * @port: the port number
887 * Remove the provided @host:@port from the cell @name in @cfg.
891 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
897 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
899 xbt_dynar_foreach(cell->content,cpt,seen) {
900 if (!strcpy(seen->name,host) && seen->port == port) {
901 xbt_dynar_cursor_rm(cell->content,&cpt);
906 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
908 return mismatch_error;
916 * @cfg: the config set
917 * @name: the name of the cell
923 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
928 TRYCATCH(mismatch_error,
929 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
930 if (errcode == mismatch_error) {
931 ERROR1("Can't empty '%s' since this config element does not exist",
933 return mismatch_error;
937 xbt_dynar_reset(cell->content);
942 /*----[ Getting ]---------------------------------------------------------*/
946 * @cfg: the config set
947 * @name: the name of the cell
948 * @val: the wanted value
950 * Returns the first value from the config set under the given name.
951 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
954 * @warning the returned value is the actual content of the config set
957 xbt_cfg_get_int (xbt_cfg_t cfg,
963 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
965 if (xbt_dynar_length(cell->content) > 1) {
966 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
967 name, xbt_dynar_length(cell->content));
970 *val = xbt_dynar_get_as(cell->content, 0, int);
975 * xbt_cfg_get_double:
976 * @cfg: the config set
977 * @name: the name of the cell
978 * @val: the wanted value
980 * Returns the first value from the config set under the given name.
981 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
984 * @warning the returned value is the actual content of the config set
988 xbt_cfg_get_double(xbt_cfg_t cfg,
994 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
996 if (xbt_dynar_length(cell->content) > 1) {
997 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
998 name, xbt_dynar_length(cell->content));
1001 *val = xbt_dynar_get_as(cell->content, 0, double);
1006 * xbt_cfg_get_string:
1008 * @th: the config set
1009 * @name: the name of the cell
1010 * @val: the wanted value
1012 * Returns the first value from the config set under the given name.
1013 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1016 * @warning the returned value is the actual content of the config set
1019 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1023 xbt_error_t errcode;
1027 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
1029 if (xbt_dynar_length(cell->content) > 1) {
1030 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1031 name, xbt_dynar_length(cell->content));
1034 *val = xbt_dynar_get_as(cell->content, 0, char *);
1041 * @cfg: the config set
1042 * @name: the name of the cell
1044 * @port: the port number
1046 * Returns the first value from the config set under the given name.
1047 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1050 * @warning the returned value is the actual content of the config set
1053 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1058 xbt_error_t errcode;
1061 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
1063 if (xbt_dynar_length(cell->content) > 1) {
1064 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1065 name, xbt_dynar_length(cell->content));
1068 val = xbt_dynar_get_as(cell->content, 0, xbt_host_t*);
1076 * xbt_cfg_get_dynar:
1077 * @cfg: where to search in
1078 * @name: what to search for
1081 * Get the data stored in the config bag.
1083 * @warning the returned value is the actual content of the config set
1085 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1087 xbt_dynar_t *dynar) {
1089 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1092 if (errcode == mismatch_error) {
1093 ERROR1("No registered cell %s in this config set",
1095 return mismatch_error;
1097 if (errcode != no_error)
1100 *dynar = cell->content;