[SR-Dev] git:master: htable: new pv counting items matching key by a regexp

Elena-Ramona Modroiu ramona at rosdev.ro
Sat Apr 18 21:55:26 CEST 2009


Module: sip-router
Branch: master
Commit: d78800223909dd0c458defbb304f7c56821d1bc0
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d78800223909dd0c458defbb304f7c56821d1bc0

Author: Elena-Ramona Modroiu <ramona at rosdev.ro>
Committer: Elena-Ramona Modroiu <ramona at rosdev.ro>
Date:   Sat Apr 18 22:53:08 2009 +0300

htable: new pv counting items matching key by a regexp

- $shtcn(ht->regexp) to return number of items that match the key by
  regexp

---

 modules_k/htable/ht_api.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 modules_k/htable/ht_api.h |    1 +
 modules_k/htable/ht_var.c |   28 ++++++++++++++++++++++++++++
 modules_k/htable/ht_var.h |    2 ++
 modules_k/htable/htable.c |    2 ++
 5 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/modules_k/htable/ht_api.c b/modules_k/htable/ht_api.c
index 0cf755e..5932f52 100644
--- a/modules_k/htable/ht_api.c
+++ b/modules_k/htable/ht_api.c
@@ -855,3 +855,47 @@ int ht_rm_cell_re(str *sre, ht_t *ht, int mode)
 	return 0;
 }
 
+int ht_count_cells_re(str *sre, ht_t *ht, int mode)
+{
+	ht_cell_t *it;
+	ht_cell_t *it0;
+	int i;
+	regex_t re;
+	regmatch_t pmatch;
+	int cnt = 0;
+
+	if(sre==NULL || sre->len<=0 || ht==NULL)
+		return 0;
+
+	if (regcomp(&re, sre->s, REG_EXTENDED|REG_ICASE|REG_NEWLINE))
+	{
+		LM_ERR("bad re %s\n", sre->s);
+		return 0;
+	}
+
+	for(i=0; i<ht->htsize; i++)
+	{
+		/* free entries */
+		lock_get(&ht->entries[i].lock);
+		it = ht->entries[i].first;
+		while(it)
+		{
+			it0 = it->next;
+			if(mode==0)
+			{
+				/* match by name */
+				if (regexec(&re, it->name.s, 1, &pmatch, 0)==0)
+					cnt++;
+			} else {
+				if(it->flags&AVP_VAL_STR)
+					if (regexec(&re, it->value.s.s, 1, &pmatch, 0)==0)
+						cnt++;
+			}
+			it = it0;
+		}
+		lock_release(&ht->entries[i].lock);
+	}
+	regfree(&re);
+	return cnt;
+}
+
diff --git a/modules_k/htable/ht_api.h b/modules_k/htable/ht_api.h
index c47fbff..99ff84a 100644
--- a/modules_k/htable/ht_api.h
+++ b/modules_k/htable/ht_api.h
@@ -86,5 +86,6 @@ int ht_set_cell_expire(ht_t *ht, str *name, int type, int_str *val);
 int ht_get_cell_expire(ht_t *ht, str *name, unsigned int *val);
 
 int ht_rm_cell_re(str *sre, ht_t *ht, int mode);
+int ht_count_cells_re(str *sre, ht_t *ht, int mode);
 
 #endif
diff --git a/modules_k/htable/ht_var.c b/modules_k/htable/ht_var.c
index 8019719..fae9fc5 100644
--- a/modules_k/htable/ht_var.c
+++ b/modules_k/htable/ht_var.c
@@ -235,3 +235,31 @@ int pv_set_ht_cell_expire(struct sip_msg* msg, pv_param_t *param,
 	return 0;
 }
 
+int pv_get_ht_cn(struct sip_msg *msg,  pv_param_t *param,
+		pv_value_t *res)
+{
+	str htname;
+	ht_pv_t *hpv;
+	int cnt = 0;
+
+	hpv = (ht_pv_t*)param->pvn.u.dname;
+
+	if(hpv->ht==NULL)
+	{
+		hpv->ht = ht_get_table(&hpv->htname);
+		if(hpv->ht==NULL)
+			return pv_get_null(msg, param, res);
+	}
+	if(pv_printf_s(msg, hpv->pve, &htname)!=0)
+	{
+		LM_ERR("cannot get $ht name\n");
+		return -1;
+	}
+	
+	cnt = ht_count_cells_re(&htname, hpv->ht, 0);
+
+	/* integer */
+	return pv_get_sintval(msg, param, res, cnt);
+}
+
+
diff --git a/modules_k/htable/ht_var.h b/modules_k/htable/ht_var.h
index 472e813..4f4f021 100644
--- a/modules_k/htable/ht_var.h
+++ b/modules_k/htable/ht_var.h
@@ -34,5 +34,7 @@ int pv_get_ht_cell_expire(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
 int pv_set_ht_cell_expire(struct sip_msg* msg, pv_param_t *param,
 		int op, pv_value_t *val);
+int pv_get_ht_cn(struct sip_msg *msg,  pv_param_t *param,
+		pv_value_t *res);
 
 #endif
diff --git a/modules_k/htable/htable.c b/modules_k/htable/htable.c
index 796038e..d4e52e6 100644
--- a/modules_k/htable/htable.c
+++ b/modules_k/htable/htable.c
@@ -62,6 +62,8 @@ static pv_export_t mod_pvs[] = {
 	{ {"shtex", sizeof("shtex")-1}, PVT_OTHER, pv_get_ht_cell_expire,
 		pv_set_ht_cell_expire,
 		pv_parse_ht_name, 0, 0, 0 },
+	{ {"shtcn", sizeof("shtcn")-1}, PVT_OTHER, pv_get_ht_cn, 0,
+		pv_parse_ht_name, 0, 0, 0 },
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
 };
 




More information about the sr-dev mailing list