X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fconf.c;h=c37bb98b81b7982d4409c77f4d1f962e4875f8ae;hb=8299c27047b1a992e2b38463421ec4a4a69f8cec;hp=b6cf5237297953fe2a6b04ab133e0815aff8639d;hpb=ddd63d22bcda367a5fad4941bd1c96334de9367d;p=libucw.git diff --git a/lib/conf.c b/lib/conf.c index b6cf5237..c37bb98b 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -24,6 +24,8 @@ static struct cfitem *cfsection; static struct mempool *cfpool; +byte *cfdeffile = DEFAULT_CONFIG; + static void CONSTRUCTOR conf_init(void) { @@ -47,24 +49,50 @@ cfg_stralloc(byte *s) void cf_register(struct cfitem *items) { - if(items[0].type!=CT_SECTION) - die("Invalid configuration section, first item must be of type CT_SECTION"); + if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION) + die("cf_register: Invalid section type"); items[0].var=cfsection; cfsection=items; } -byte *cf_set_item(byte *sect, byte *name, byte *value) +int cf_item_count(void) { - struct cfitem *item; - byte *msg=NULL; + struct cfitem *sect, *item; + int count = 0; + for (sect = cfsection; sect; sect = sect->var) + for (item = sect+1; item->type; item++) + count++; + return count; +} + +struct cfitem *cf_get_item(byte *sect, byte *name) +{ + struct cfitem *item, *section; item=cfsection; while(item && strcasecmp(item->name,sect)) item=item->var; - if(!item) /* ignore unknown section */ + if(!item) /* unknown section */ return NULL; + section = item; for(item++; item->type && strcasecmp(item->name,name); item++); + if (!item->type && section->type == CT_INCOMPLETE_SECTION) + return NULL; + + return item; /* item->type == 0 if not found */ +} + +byte *cf_set_item(byte *sect, byte *name, byte *value) +{ + struct cfitem *item; + byte *msg=NULL; + + if (!*sect) + return "Empty section name"; + item=cf_get_item(sect,name); + if(!item) /* ignore unknown section */ + return NULL; switch(item->type){ case CT_INT: @@ -189,6 +217,7 @@ void cf_read(byte *filename) { if(!cf_subread(filename,0)) die("Reading config file %s failed",filename); + cfdeffile = NULL; } int cf_getopt(int argc,char * const argv[], @@ -222,6 +251,8 @@ int cf_getopt(int argc,char * const argv[], name=c; } + if (cfdeffile) + cf_read(cfdeffile); msg=cf_set_item(sect,name,value); } if(msg) @@ -229,9 +260,11 @@ int cf_getopt(int argc,char * const argv[], }else if(res=='C'){ cf_read(optarg); - }else{ /* unhandled option */ + }else{ + /* unhandled option or end of options */ + if(cfdeffile) + cf_read(cfdeffile); return res; } }while(1); } -