Add support for GROUP_MATCH_COUNT regex matching on category

Current support for regex matching was previously only available on the group.
Also, error reporting for regex failures has been added. In addition to this
feature enhancement a unit test has been written to check the regular expression
logic to ensure the count operation is working as expected.

(closes issue #16642)
Reported by: kobaz
Patches: 
      groupmatch2.patch uploaded by kobaz (license 834)

Review: https://reviewboard.asterisk.org/r/503/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@247295 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Jeff Peeler
2010-02-17 19:51:53 +00:00
parent e8d201e870
commit 27a4cda821
4 changed files with 176 additions and 8 deletions

View File

@@ -1091,27 +1091,34 @@ int ast_app_group_get_count(const char *group, const char *category)
int ast_app_group_match_get_count(const char *groupmatch, const char *category)
{
struct ast_group_info *gi = NULL;
regex_t regexbuf;
regex_t regexbuf_group;
regex_t regexbuf_category;
int count = 0;
if (ast_strlen_zero(groupmatch)) {
if (ast_strlen_zero(groupmatch))
return 0;
/* if regex compilation fails, return zero matches */
if (regcomp(&regexbuf_group, groupmatch, REG_EXTENDED | REG_NOSUB)) {
ast_log(LOG_ERROR, "Regex compile failed on: %s\n", groupmatch);
return 0;
}
/* if regex compilation fails, return zero matches */
if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB)) {
if (regcomp(&regexbuf_category, category, REG_EXTENDED | REG_NOSUB)) {
ast_log(LOG_ERROR, "Regex compile failed on: %s\n", category);
return 0;
}
AST_RWLIST_RDLOCK(&groups);
AST_RWLIST_TRAVERSE(&groups, gi, group_list) {
if (!regexec(&regexbuf, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
if (!regexec(&regexbuf_group, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !regexec(&regexbuf_category, gi->category, 0, NULL, 0)))) {
count++;
}
}
AST_RWLIST_UNLOCK(&groups);
regfree(&regexbuf);
regfree(&regexbuf_group);
regfree(&regexbuf_category);
return count;
}