[sr-dev] git:master:18eb1c97: pv: added {s.count, c} transformation

Daniel-Constantin Mierla miconda at gmail.com
Tue Aug 20 11:21:20 CEST 2019


Module: kamailio
Branch: master
Commit: 18eb1c973c5d863a562864c2b77b4e1d520f9406
URL: https://github.com/kamailio/kamailio/commit/18eb1c973c5d863a562864c2b77b4e1d520f9406

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-08-20T11:17:17+02:00

pv: added {s.count,c} transformation

- count how many times c appears in the pv value
   "abababa"{s.count,a} => 4

---

Modified: src/modules/pv/pv_trans.c
Modified: src/modules/pv/pv_trans.h

---

Diff:  https://github.com/kamailio/kamailio/commit/18eb1c973c5d863a562864c2b77b4e1d520f9406.diff
Patch: https://github.com/kamailio/kamailio/commit/18eb1c973c5d863a562864c2b77b4e1d520f9406.patch

---

diff --git a/src/modules/pv/pv_trans.c b/src/modules/pv/pv_trans.c
index 8ab46d328f..ea693bffd4 100644
--- a/src/modules/pv/pv_trans.c
+++ b/src/modules/pv/pv_trans.c
@@ -1207,6 +1207,42 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
 			val->rs.s[val->rs.len] = '\0';
 			break;
 
+		case TR_S_COUNT:
+			if(tp==NULL)
+			{
+				LM_ERR("invalid parameters (cfg line: %d)\n",
+						get_cfg_crt_line());
+				return -1;
+			}
+			if(!(val->flags&PV_VAL_STR)) {
+				val->rs.s = int2str(val->ri, &val->rs.len);
+			}
+			if(tp->type==TR_PARAM_STRING)
+			{
+				st = tp->v.s;
+			} else {
+				if(pv_get_spec_value(msg, (pv_spec_p)tp->v.data, &v)!=0
+						|| (!(v.flags&PV_VAL_STR)) || v.rs.len<=0)
+				{
+					LM_ERR("cannot get parameter value (cfg line: %d)\n",
+							get_cfg_crt_line());
+					return -1;
+				}
+				st = v.rs;
+			}
+			LM_DBG("counting [%.*s](%d) in [%.*s](%d)\n",
+					st.len, st.s, st.len, val->rs.len, val->rs.s, val->rs.len);
+			j = 0;
+			for(i=0; i<val->rs.len; i++) {
+				if(val->rs.s[i]==st.s[0]) {
+					j++;
+				}
+			}
+			val->flags = PV_TYPE_INT|PV_VAL_INT|PV_VAL_STR;
+			val->ri = j;
+			val->rs.s = int2str(j, &val->rs.len);
+			break;
+
 		default:
 			LM_ERR("unknown subtype %d (cfg line: %d)\n",
 					subtype, get_cfg_crt_line());
@@ -2575,6 +2611,26 @@ char* tr_parse_string(str* in, trans_t *t)
 	} else if(name.len==9 && strncasecmp(name.s, "unbracket", 9)==0) {
 		t->subtype = TR_S_UNBRACKET;
 		goto done;
+	} else if(name.len==5 && strncasecmp(name.s, "count", 5)==0) {
+		t->subtype = TR_S_COUNT;
+		if(*p!=TR_PARAM_MARKER)
+		{
+			LM_ERR("invalid count transformation: %.*s!\n",
+					in->len, in->s);
+			goto error;
+		}
+		p++;
+		_tr_parse_sparamx(p, p0, tp, spec, ps, in, s, 1);
+		t->params = tp;
+		tp = 0;
+		while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++;
+		if(*p!=TR_RBRACKET)
+		{
+			LM_ERR("invalid count transformation: %.*s!!\n",
+					in->len, in->s);
+			goto error;
+		}
+		goto done;
 	}
 
 	LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
diff --git a/src/modules/pv/pv_trans.h b/src/modules/pv/pv_trans.h
index da2cb87624..0b579e4b27 100644
--- a/src/modules/pv/pv_trans.h
+++ b/src/modules/pv/pv_trans.h
@@ -42,7 +42,7 @@ enum _tr_s_subtype {
 	TR_S_TIMEFORMAT, TR_S_TRIM, TR_S_RTRIM, TR_S_LTRIM, TR_S_RM, TR_S_STRIPTO,
 	TR_S_URLENCODEPARAM, TR_S_URLDECODEPARAM, TR_S_NUMERIC, TR_S_ESCAPECSV,
 	TR_S_ENCODEBASE58, TR_S_DECODEBASE58, TR_S_COREHASH, TR_S_UNQUOTE,
-	TR_S_UNBRACKET
+	TR_S_UNBRACKET, TR_S_COUNT
 };
 enum _tr_uri_subtype {
 	TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,




More information about the sr-dev mailing list