Mercurial > hg > th-libs
changeset 504:3a0864eb358f
Fix the functionality of th_cfg_find().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 26 Dec 2019 06:34:48 +0200 |
parents | 12dbe0102d72 |
children | 50d71fc84831 |
files | th_config.c |
diffstat | 1 files changed, 24 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/th_config.c Thu Dec 26 06:33:35 2019 +0200 +++ b/th_config.c Thu Dec 26 06:34:48 2019 +0200 @@ -826,39 +826,37 @@ } -/* Find a configuration item based on section, name, type. - * Name MUST be defined. Section can be NULL and type -1, - * first matching item will be returned. +/* Find a configuration item based on section and/or name (and/or) type. + * The first matching item will be returned. */ -static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, BOOL *sect, const char *section, const char *name, const int type) +static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, const char *name, const int type) { while (item != NULL) { BOOL match = TRUE; - if (item->type == ITEM_SECTION) + // Has type check been set, and does it match? + if (type != -1 && item->type != type) + match = FALSE; + + // Check item name + if (name != NULL && item->name != NULL && + strcmp(name, item->name) != 0) + match = FALSE; + + // Recurse to section + if (!match && item->type == ITEM_SECTION) { - // Check section name if set - if (section != NULL && strcmp(section, item->name) == 0) - *sect = TRUE; - - // Recurse to sub-section - th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, sect, section, name, type); + th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, name, type); if (tmp != NULL) return tmp; } - else - // Has type check been set, and does it match? - if (type != -1 && item->type != type) - match = FALSE; - else - // Check name (not section name, tho) - if (strcmp(name, item->name) != 0) - match = FALSE; // Do we have a match? - if (*sect && match) + if (match) return item; + + item = (th_cfgitem_t *) item->node.next; } return NULL; @@ -867,10 +865,12 @@ th_cfgitem_t *th_cfg_find(th_cfgitem_t *cfg, const char *section, const char *name, const int type) { - BOOL sect = FALSE; + th_cfgitem_t *node; - if (section == NULL) - sect = TRUE; + if (section != NULL) + node = th_cfg_find_do(cfg, section, ITEM_SECTION); + else + node = cfg; - return th_cfg_find_do(cfg, §, section, name, type); + return th_cfg_find_do(node, name, type); }