Skip to content

Commit

Permalink
pv: Add pv-operator, to remove all non-numeric parts of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbock committed Jun 19, 2015
1 parent dc2d3c3 commit 5900736
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions modules/pv/pv_trans.c
Expand Up @@ -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 */

/*!
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/pv/pv_trans.h
Expand Up @@ -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,
Expand Down

0 comments on commit 5900736

Please sign in to comment.