Module: sip-router
Branch: master
Commit: 6ad59b21c54865e45144b84a3d0569553891da4c
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6ad59b2…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Thu Dec 15 16:19:18 2011 +0100
pv: added transformations for trim, right trim and left trim
- remove whitespaces (' ', '\t', '\r', '\n') from start or
end of a string value
---
modules_k/pv/pv_trans.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
modules_k/pv/pv_trans.h | 2 +-
2 files changed, 46 insertions(+), 1 deletions(-)
diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c
index da5dbfd..139275b 100644
--- a/modules_k/pv/pv_trans.c
+++ b/modules_k/pv/pv_trans.c
@@ -592,6 +592,42 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
val->rs.s = _tr_buffer;
break;
+ case TR_S_TRIM:
+ if(!(val->flags&PV_VAL_STR))
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ if(val->rs.len>TR_BUFFER_SIZE-2)
+ return -1;
+ memcpy(_tr_buffer, val->rs.s, val->rs.len);
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ trim(&val->rs);
+ val->rs.s[val->rs.len] = '\0';
+ break;
+
+ case TR_S_RTRIM:
+ if(!(val->flags&PV_VAL_STR))
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ if(val->rs.len>TR_BUFFER_SIZE-2)
+ return -1;
+ memcpy(_tr_buffer, val->rs.s, val->rs.len);
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ trim_trailing(&val->rs);
+ val->rs.s[val->rs.len] = '\0';
+ break;
+
+ case TR_S_LTRIM:
+ if(!(val->flags&PV_VAL_STR))
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ if(val->rs.len>TR_BUFFER_SIZE-2)
+ return -1;
+ memcpy(_tr_buffer, val->rs.s, val->rs.len);
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ trim_leading(&val->rs);
+ val->rs.s[val->rs.len] = '\0';
+ break;
+
default:
LM_ERR("unknown subtype %d\n",
subtype);
@@ -1580,6 +1616,15 @@ char* tr_parse_string(str* in, trans_t *t)
goto error;
}
goto done;
+ } else if(name.len==4 && strncasecmp(name.s, "trim", 4)==0) {
+ t->subtype = TR_S_TRIM;
+ goto done;
+ } else if(name.len==5 && strncasecmp(name.s, "rtrim", 5)==0) {
+ t->subtype = TR_S_RTRIM;
+ goto done;
+ } else if(name.len==5 && strncasecmp(name.s, "ltrim", 5)==0) {
+ t->subtype = TR_S_LTRIM;
+ goto done;
}
LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
diff --git a/modules_k/pv/pv_trans.h b/modules_k/pv/pv_trans.h
index 0938ddc..24f4bc2 100644
--- a/modules_k/pv/pv_trans.h
+++ b/modules_k/pv/pv_trans.h
@@ -40,7 +40,7 @@ enum _tr_s_subtype {
TR_S_ESCAPECOMMON, TR_S_UNESCAPECOMMON, TR_S_ESCAPEUSER, TR_S_UNESCAPEUSER,
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_TIMEFORMAT, TR_S_TRIM, TR_S_RTRIM, TR_S_LTRIM
};
enum _tr_uri_subtype {
TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,