Ver Fonte

htable: new pv counting items matching key by a regexp

- $shtcn(ht->regexp) to return number of items that match the key by
  regexp
Elena-Ramona Modroiu há 16 anos atrás
pai
commit
d788002239

+ 44 - 0
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;
+}
+

+ 1 - 0
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

+ 28 - 0
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);
+}
+
+

+ 2 - 0
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

+ 2 - 0
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 }
 };