diff --git a/src/core/cfg/cfg.c b/src/core/cfg/cfg.c index 488d72f35a6..80e3fe3e55d 100644 --- a/src/core/cfg/cfg.c +++ b/src/core/cfg/cfg.c @@ -285,3 +285,39 @@ void **cfg_get_handle(char *gname) return group->handle; } + + +/* Set the group_id pointer based on the group string. + * The string is either "group_name", or "group_name[group_id]" + * *group_id is set to null in the former case. + * Warning: changes the group string + */ +int cfg_get_group_id(str *group, unsigned int **group_id) +{ + static unsigned int id; + str s; + + if (!group->s || (group->s[group->len-1] != ']')) { + *group_id = NULL; + return 0; + } + + s.s = group->s + group->len - 2; + s.len = 0; + while ((s.s > group->s) && (*s.s != '[')) { + s.s--; + s.len++; + } + if (s.s == group->s) /* '[' not found */ + return -1; + group->len = s.s - group->s; + s.s++; + if (!group->len || !s.len) + return -1; + if (str2int(&s, &id)) + return -1; + + *group_id = &id; + return 0; +} + diff --git a/src/core/cfg/cfg.h b/src/core/cfg/cfg.h index b3c5a1ba298..19cb824055c 100644 --- a/src/core/cfg/cfg.h +++ b/src/core/cfg/cfg.h @@ -102,4 +102,7 @@ int cfg_new_ginst(char *group_name, unsigned int group_id); /*! \brief returns the handle of a cfg group */ void **cfg_get_handle(char *gname); +/*! \brief get the id of a cfg group */ +int cfg_get_group_id(str *group, unsigned int **group_id); + #endif /* _CFG_H */