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;
127 printf("%s>> Dumping of the config set '%s':\n",indent,name);
128 xbt_dict_foreach(dict,cursor,key,cell) {
130 printf("%s %s:",indent,key);
132 size = xbt_dynar_length(cell->content);
133 printf("%d_to_%d_%s. Actual size=%d. List of values:\n",
134 cell->min,cell->max,xbt_cfgelm_type_name[cell->type],
137 switch (cell->type) {
140 for (i=0; i<size; i++) {
141 ival = xbt_dynar_get_as(cell->content,i,int);
142 printf ("%s %d\n",indent,ival);
146 case xbt_cfgelm_double:
147 for (i=0; i<size; i++) {
148 dval = xbt_dynar_get_as(cell->content,i,double);
149 printf ("%s %f\n",indent,dval);
153 case xbt_cfgelm_string:
154 for (i=0; i<size; i++) {
155 sval = xbt_dynar_get_as(cell->content,i,char*);
156 printf ("%s %s\n",indent,sval);
160 case xbt_cfgelm_host:
161 for (i=0; i<size; i++) {
162 hval = xbt_dynar_get_as(cell->content,i,xbt_host_t*);
163 printf ("%s %s:%d\n",indent,hval->name,hval->port);
168 printf("%s Invalid type!!\n",indent);
173 if (name) printf("%s<< End of the config set '%s'\n",indent,name);
176 xbt_dict_cursor_free(&cursor);
183 * @data: the data to be freed (typed as void* to be usable as free funct in dict)
185 * free an config element
188 void xbt_cfgelm_free(void *data) {
189 xbt_cfgelm_t c=(xbt_cfgelm_t)data;
192 xbt_dynar_free(&(c->content));
196 /*----[ Registering stuff ]-----------------------------------------------*/
200 * @cfg: the config set
201 * @type: the type of the config element
205 * register an element within a config set
209 xbt_cfg_register(xbt_cfg_t cfg,
210 const char *name, e_xbt_cfgelm_type_t type,
215 DEBUG4("Register cfg elm %s (%d to %d %s)",name,min,max,xbt_cfgelm_type_name[type]);
216 errcode = xbt_dict_get((xbt_dict_t)cfg,name,(void**)&res);
218 if (errcode == no_error) {
219 WARN1("Config elem %s registered twice.",name);
220 /* Will be removed by the insertion of the new one */
222 xbt_assert_error(mismatch_error);
224 res=xbt_new(s_xbt_cfgelm_t,1);
232 res->content = xbt_dynar_new(sizeof(int), NULL);
235 case xbt_cfgelm_double:
236 res->content = xbt_dynar_new(sizeof(double), NULL);
239 case xbt_cfgelm_string:
240 res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
243 case xbt_cfgelm_host:
244 res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
248 ERROR1("%d is an invalide type code",type);
251 xbt_dict_set((xbt_dict_t)cfg,name,res,&xbt_cfgelm_free);
255 * xbt_cfg_unregister:
257 * @cfg: the config set
258 * @name: the name of the elem to be freed
260 * unregister an element from a config set.
261 * Note that it removes both the DTD and the actual content.
265 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
266 return xbt_dict_remove((xbt_dict_t)cfg,name);
270 * xbt_cfg_register_str:
272 * @cfg: the config set
273 * @entry: a string describing the element to register
275 * Parse a string and register the stuff described.
279 xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
280 char *entrycpy=xbt_strdup(entry);
284 e_xbt_cfgelm_type_t type;
286 tok=strchr(entrycpy, ':');
289 "Invalid config element descriptor: ",entry,
290 "; Should be <name>:<min nb>_to_<max nb>_<type>");
296 min=strtol(tok, &tok, 10);
298 ERROR1("Invalid minimum in config element descriptor %s",entry);
303 if (!strcmp(tok,"_to_")){
305 "Invalid config element descriptor: ",entry,
306 "; Should be <name>:<min nb>_to_<max nb>_<type>");
310 tok += strlen("_to_");
312 max=strtol(tok, &tok, 10);
314 ERROR1("Invalid maximum in config element descriptor %s",entry);
321 "Invalid config element descriptor: ",entry,
322 "; Should be <name>:<min nb>_to_<max nb>_<type>");
328 type<xbt_cfgelm_type_count && strcmp(tok,xbt_cfgelm_type_name[type]);
330 if (type == xbt_cfgelm_type_count) {
332 "Invalid type in config element descriptor: ",entry,
333 "; Should be one of 'string', 'int', 'host' or 'double'.");
338 xbt_cfg_register(cfg,entrycpy,type,min,max);
340 xbt_free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
347 * @cfg: the config set
349 * Check the config set
353 xbt_cfg_check(xbt_cfg_t cfg) {
354 xbt_dict_cursor_t cursor;
359 xbt_assert0(cfg,"NULL config set.");
361 xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,cell) {
362 size = xbt_dynar_length(cell->content);
363 if (cell->min > size) {
364 ERROR4("Config elem %s needs at least %d %s, but there is only %d values.",
367 xbt_cfgelm_type_name[cell->type],
369 xbt_dict_cursor_free(&cursor);
370 return mismatch_error;
373 if (cell->max < size) {
374 ERROR4("Config elem %s accepts at most %d %s, but there is %d values.",
377 xbt_cfgelm_type_name[cell->type],
379 xbt_dict_cursor_free(&cursor);
380 return mismatch_error;
385 xbt_dict_cursor_free(&cursor);
389 static xbt_error_t xbt_cfgelm_get(xbt_cfg_t cfg,
391 e_xbt_cfgelm_type_t type,
392 /* OUT */ xbt_cfgelm_t *whereto){
394 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
397 if (errcode == mismatch_error) {
398 ERROR1("No registered cell %s in this config set",
400 return mismatch_error;
402 if (errcode != no_error)
405 xbt_assert3((*whereto)->type == type,
406 "You tried to access to the config element %s as an %s, but its type is %s.",
408 xbt_cfgelm_type_name[type],
409 xbt_cfgelm_type_name[(*whereto)->type]);
417 * @cfg: the config set
418 * @name: the name of the element
421 * Give the type of the config element
425 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name,
426 /* OUT */e_xbt_cfgelm_type_t *type) {
431 TRYCATCH(mismatch_error,xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
433 if (errcode == mismatch_error) {
434 ERROR1("Can't get the type of '%s' since this cell does not exist",
436 return mismatch_error;
444 /*----[ Setting ]---------------------------------------------------------*/
446 * xbt_cfg_set_vargs():
447 * @cfg: config set to fill
448 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
450 * Add some values to the config set.
451 * @warning: if the list isn't NULL terminated, it will segfault.
454 xbt_cfg_set_vargs(xbt_cfg_t cfg, va_list pa) {
458 e_xbt_cfgelm_type_t type;
462 while ((name=va_arg(pa,char *))) {
464 if (!xbt_cfg_get_type(cfg,name,&type)) {
465 ERROR1("Can't set the property '%s' since it's not registered",name);
466 return mismatch_error;
470 case xbt_cfgelm_host:
471 str = va_arg(pa, char *);
473 TRY(xbt_cfg_set_host(cfg,name,str,i));
476 case xbt_cfgelm_string:
477 str=va_arg(pa, char *);
478 TRY(xbt_cfg_set_string(cfg, name, str));
483 TRY(xbt_cfg_set_int(cfg,name,i));
486 case xbt_cfgelm_double:
488 TRY(xbt_cfg_set_double(cfg,name,d));
492 RAISE1(unknown_error,"Config element cell %s not valid.",name);
500 * @cfg: config set to fill
501 * @varargs: NULL-terminated list of pairs {(const char*)key, value}
503 * Add some values to the config set.
504 * @warning: if the list isn't NULL terminated, it will segfault.
506 xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, ...) {
511 errcode=xbt_cfg_set_vargs(cfg,pa);
517 * xbt_cfg_set_parse():
518 * @cfg: config set to fill
519 * @options: a string containing the content to add to the config set. This
520 * is a '\t',' ' or '\n' separated list of cells. Each individual cell is
521 * like "[name]:[value]" where [name] is the name of an already registred
522 * cell, and [value] conforms to the data type under which this cell was
525 * Add the cells described in a string to a config set.
529 xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
535 char *optionlist_cpy;
536 char *option, *name,*val;
542 if (!options || !strlen(options)) { /* nothing to do */
545 optionlist_cpy=xbt_strdup(options);
547 DEBUG1("List to parse and set:'%s'",options);
548 option=optionlist_cpy;
549 while (1) { /* breaks in the code */
555 DEBUG3("Still to parse and set: '%s'. len=%d; option-name=%ld",
556 name,len,(long)(option-name));
559 while (option-name<=(len-1) && *option != ' ' && *option != '\n' && *option != '\t') {
560 /*fprintf(stderr,"Take %c.\n",*option);*/
563 if (option-name == len) {
564 /*fprintf(stderr,"Boundary=EOL\n");*/
565 option=NULL; /* don't do next iteration */
568 /*fprintf(stderr,"Boundary on '%c'. len=%d;option-name=%d\n",*option,len,option-name);*/
570 /* Pass the following blank chars */
572 while (option-name<(len-1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
573 /* fprintf(stderr,"Ignore a blank char.\n");*/
576 if (option-name == len-1)
577 option=NULL; /* don't do next iteration */
579 DEBUG2("parse now:'%s'; parse later:'%s'",name,option);
581 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
586 val=strchr(name,':');
588 xbt_free(optionlist_cpy);
590 "Malformated option: '%s'; Should be of the form 'name:value'",
595 DEBUG2("name='%s';val='%s'",name,val);
597 errcode=xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell);
602 ERROR1("No registrated cell corresponding to '%s'.",name);
603 xbt_free(optionlist_cpy);
604 return mismatch_error;
607 xbt_free(optionlist_cpy);
611 switch (cell->type) {
612 case xbt_cfgelm_string:
613 TRYCLEAN(xbt_cfg_set_string(cfg, name, val),
614 xbt_free(optionlist_cpy));
618 i=strtol(val, &val, 0);
620 xbt_free(optionlist_cpy);
622 "Value of option %s not valid. Should be an integer",
626 TRYCLEAN(xbt_cfg_set_int(cfg,name,i),
627 xbt_free(optionlist_cpy));
630 case xbt_cfgelm_double:
633 xbt_free(optionlist_cpy);
635 "Value of option %s not valid. Should be a double",
639 TRYCLEAN(xbt_cfg_set_double(cfg,name,d),
640 xbt_free(optionlist_cpy));
643 case xbt_cfgelm_host:
647 xbt_free(optionlist_cpy);
649 "Value of option %s not valid. Should be an host (machine:port)",
654 i=strtol(val, &val, 0);
656 xbt_free(optionlist_cpy);
658 "Value of option %s not valid. Should be an host (machine:port)",
662 TRYCLEAN(xbt_cfg_set_host(cfg,name,str,i),
663 xbt_free(optionlist_cpy));
667 xbt_free(optionlist_cpy);
668 RAISE1(unknown_error,"Type of config element %s is not valid.",name);
672 xbt_free(optionlist_cpy);
679 * @cfg: the config set
680 * @name: the name of the cell
681 * @val: the value of the cell
683 * Set the value of the cell @name in @cfg with the provided value.
686 xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
690 VERB2("Configuration setting: %s=%d",name,val);
691 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
694 xbt_dynar_push(cell->content,&val);
696 xbt_dynar_set(cell->content,0,&val);
702 * xbt_cfg_set_double:
703 * @cfg: the config set
704 * @name: the name of the cell
705 * @val: the doule to set
707 * Set the value of the cell @name in @cfg with the provided value.
711 xbt_cfg_set_double(xbt_cfg_t cfg,const char*name, double val) {
715 VERB2("Configuration setting: %s=%f",name,val);
716 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
719 xbt_dynar_push(cell->content,&val);
721 xbt_dynar_set(cell->content,0,&val);
727 * xbt_cfg_set_string:
729 * @cfg: the config set
730 * @name: the name of the cell
731 * @val: the value to be added
733 * Set the value of the cell @name in @cfg with the provided value.
737 xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
740 char *newval = xbt_strdup(val);
742 VERB2("Configuration setting: %s=%s",name,val);
743 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
746 xbt_dynar_push(cell->content,&newval);
748 xbt_dynar_set(cell->content,0,&newval);
756 * @cfg: the config set
757 * @name: the name of the cell
759 * @port: the port number
761 * Set the value of the cell @name in @cfg with the provided value
762 * on the given @host to the given @port
766 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name,
767 const char *host,int port) {
770 xbt_host_t *val=xbt_new(xbt_host_t,1);
772 VERB3("Configuration setting: %s=%s:%d",name,host,port);
774 val->name = xbt_strdup(name);
777 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
780 xbt_dynar_push(cell->content,&val);
782 xbt_dynar_set(cell->content,0,&val);
787 /* ---- [ Removing ] ---- */
792 * @cfg: the config set
793 * @name: the name of the cell
794 * @val: the value to be removed
796 * Remove the provided @val from the cell @name in @cfg.
798 xbt_error_t xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
804 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
806 xbt_dynar_foreach(cell->content,cpt,seen) {
808 xbt_dynar_cursor_rm(cell->content,&cpt);
813 ERROR2("Can't remove the value %d of config element %s: value not found.",
815 return mismatch_error;
821 * @cfg: the config set
822 * @name: the name of the cell
823 * @val: the value to be removed
825 * Remove the provided @val from the cell @name in @cfg.
828 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
834 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
836 xbt_dynar_foreach(cell->content,cpt,seen) {
838 xbt_dynar_cursor_rm(cell->content,&cpt);
843 ERROR2("Can't remove the value %f of config element %s: value not found.",
845 return mismatch_error;
851 * @cfg: the config set
852 * @name: the name of the cell
853 * @val: the value of the string which will be removed
855 * Remove the provided @val from the cell @name in @cfg.
858 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
864 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
866 xbt_dynar_foreach(cell->content,cpt,seen) {
867 if (!strcpy(seen,val)) {
868 xbt_dynar_cursor_rm(cell->content,&cpt);
873 ERROR2("Can't remove the value %s of config element %s: value not found.",
875 return mismatch_error;
881 * @cfg: the config set
882 * @name: the name of the cell
883 * @host: the hostname
884 * @port: the port number
886 * Remove the provided @host:@port from the cell @name in @cfg.
890 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
896 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
898 xbt_dynar_foreach(cell->content,cpt,seen) {
899 if (!strcpy(seen->name,host) && seen->port == port) {
900 xbt_dynar_cursor_rm(cell->content,&cpt);
905 ERROR3("Can't remove the value %s:%d of config element %s: value not found.",
907 return mismatch_error;
915 * @cfg: the config set
916 * @name: the name of the cell
922 xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
927 TRYCATCH(mismatch_error,
928 xbt_dict_get((xbt_dict_t)cfg,name,(void**)&cell));
929 if (errcode == mismatch_error) {
930 ERROR1("Can't empty '%s' since this config element does not exist",
932 return mismatch_error;
936 xbt_dynar_reset(cell->content);
941 /*----[ Getting ]---------------------------------------------------------*/
945 * @cfg: the config set
946 * @name: the name of the cell
947 * @val: the wanted value
949 * Returns the first value from the config set under the given name.
950 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
953 * @warning the returned value is the actual content of the config set
956 xbt_cfg_get_int (xbt_cfg_t cfg,
962 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_int,&cell));
964 if (xbt_dynar_length(cell->content) > 1) {
965 WARN2("You asked for the first value of the config element '%s', but there is %lu values",
966 name, xbt_dynar_length(cell->content));
969 *val = xbt_dynar_get_as(cell->content, 0, int);
974 * xbt_cfg_get_double:
975 * @cfg: the config set
976 * @name: the name of the cell
977 * @val: the wanted value
979 * Returns the first value from the config set under the given name.
980 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
983 * @warning the returned value is the actual content of the config set
987 xbt_cfg_get_double(xbt_cfg_t cfg,
993 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_double,&cell));
995 if (xbt_dynar_length(cell->content) > 1) {
996 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
997 name, xbt_dynar_length(cell->content));
1000 *val = xbt_dynar_get_as(cell->content, 0, double);
1005 * xbt_cfg_get_string:
1007 * @th: the config set
1008 * @name: the name of the cell
1009 * @val: the wanted value
1011 * Returns the first value from the config set under the given name.
1012 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1015 * @warning the returned value is the actual content of the config set
1018 xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg,
1022 xbt_error_t errcode;
1026 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_string,&cell));
1028 if (xbt_dynar_length(cell->content) > 1) {
1029 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1030 name, xbt_dynar_length(cell->content));
1033 *val = xbt_dynar_get_as(cell->content, 0, char *);
1040 * @cfg: the config set
1041 * @name: the name of the cell
1043 * @port: the port number
1045 * Returns the first value from the config set under the given name.
1046 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar()
1049 * @warning the returned value is the actual content of the config set
1052 xbt_error_t xbt_cfg_get_host (xbt_cfg_t cfg,
1057 xbt_error_t errcode;
1060 TRY (xbt_cfgelm_get(cfg,name,xbt_cfgelm_host,&cell));
1062 if (xbt_dynar_length(cell->content) > 1) {
1063 WARN2("You asked for the first value of the config element '%s', but there is %lu values\n",
1064 name, xbt_dynar_length(cell->content));
1067 val = xbt_dynar_get_as(cell->content, 0, xbt_host_t*);
1075 * xbt_cfg_get_dynar:
1076 * @cfg: where to search in
1077 * @name: what to search for
1080 * Get the data stored in the config bag.
1082 * @warning the returned value is the actual content of the config set
1084 xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg,
1086 xbt_dynar_t *dynar) {
1088 xbt_error_t errcode = xbt_dict_get((xbt_dict_t)cfg,name,
1091 if (errcode == mismatch_error) {
1092 ERROR1("No registered cell %s in this config set",
1094 return mismatch_error;
1096 if (errcode != no_error)
1099 *dynar = cell->content;