[sr-dev] git:master:5900736c: pv: Add pv-operator, to remove all non-numeric parts of a string

Carsten Bock carsten at ng-voice.com
Fri Jun 19 13:33:01 CEST 2015


Module: kamailio
Branch: master
Commit: 5900736cea1da98952f23b0f92aa51bc928b5b46
URL: https://github.com/kamailio/kamailio/commit/5900736cea1da98952f23b0f92aa51bc928b5b46

Author: Carsten Bock <carsten at ng-voice.com>
Committer: Carsten Bock <carsten at ng-voice.com>
Date: 2015-06-19T13:32:49+02:00

pv: Add pv-operator, to remove all non-numeric parts of a string

---

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

---

Diff:  https://github.com/kamailio/kamailio/commit/5900736cea1da98952f23b0f92aa51bc928b5b46.diff
Patch: https://github.com/kamailio/kamailio/commit/5900736cea1da98952f23b0f92aa51bc928b5b46.patch

---

diff --git a/modules/pv/pv_trans.c b/modules/pv/pv_trans.c
index 42798d8..17ad35d 100644
--- a/modules/pv/pv_trans.c
+++ b/modules/pv/pv_trans.c
@@ -233,6 +233,19 @@ static int pdu_7bit_decode(str sin) {
 	return j;	
 }
 
+/* Get only the numeric part of string, e.g.
+   040/123-456 => 040123456 */
+static int getNumericValue(str sin) {
+	int i, j = 0;
+	for(i = 0; i < sin.len; i ++) {
+		if (sin.s[i] >= '0' && sin.s[i] <= '9') {
+			_tr_buffer[j++] = sin.s[i];
+		}
+	}
+	_tr_buffer[j] = '\0';
+	return j;
+}
+
 /* -- transformations functions */
 
 /*!
@@ -388,6 +401,17 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
 			val->rs.s = _tr_buffer;
 			val->rs.len = i;
 			break;
+		case TR_S_NUMERIC:
+			if(!(val->flags&PV_VAL_STR))
+				return -1;
+			if(val->rs.len>TR_BUFFER_SIZE)
+				return -1;
+			i = getNumericValue(val->rs);
+			memset(val, 0, sizeof(pv_value_t));
+			val->flags = PV_VAL_STR;
+			val->rs.s = _tr_buffer;
+			val->rs.len = i;
+			break;
 		case TR_S_ENCODEBASE64:
 			if(!(val->flags&PV_VAL_STR))
 				val->rs.s = int2str(val->ri, &val->rs.len);
@@ -2030,6 +2054,9 @@ char* tr_parse_string(str* in, trans_t *t)
 	} else if(name.len==7 && strncasecmp(name.s, "toupper", 7)==0) {
 		t->subtype = TR_S_TOUPPER;
 		goto done;
+	} else if(name.len==7 && strncasecmp(name.s, "numeric", 7)==0) {
+		t->subtype = TR_S_NUMERIC;
+		goto done;
 	} else if(name.len==11 && strncasecmp(name.s, "encode.hexa", 11)==0) {
 		t->subtype = TR_S_ENCODEHEXA;
 		goto done;
diff --git a/modules/pv/pv_trans.h b/modules/pv/pv_trans.h
index a49ad53..0d61c8b 100644
--- a/modules/pv/pv_trans.h
+++ b/modules/pv/pv_trans.h
@@ -40,7 +40,7 @@ enum _tr_s_subtype {
 	TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER,
 	TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_REPLACE,
 	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_URLENCODEPARAM, TR_S_URLDECODEPARAM, TR_S_NUMERIC
 };
 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