[sr-dev] git:master:88c25b39: textops: added ends_with(str, suffix) function

Daniel-Constantin Mierla miconda at gmail.com
Thu Jan 7 09:03:28 CET 2021


Module: kamailio
Branch: master
Commit: 88c25b39c5bc1c7390a29176f76b8ede5b27802d
URL: https://github.com/kamailio/kamailio/commit/88c25b39c5bc1c7390a29176f76b8ede5b27802d

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-01-06T15:29:45+01:00

textops: added ends_with(str, suffix) function

- exported to kemi

---

Modified: src/modules/textops/textops.c

---

Diff:  https://github.com/kamailio/kamailio/commit/88c25b39c5bc1c7390a29176f76b8ede5b27802d.diff
Patch: https://github.com/kamailio/kamailio/commit/88c25b39c5bc1c7390a29176f76b8ede5b27802d.patch

---

diff --git a/src/modules/textops/textops.c b/src/modules/textops/textops.c
index 5f9361f5b2..6f3c6f9b5e 100644
--- a/src/modules/textops/textops.c
+++ b/src/modules/textops/textops.c
@@ -136,6 +136,7 @@ static int in_list_prefix_f(struct sip_msg* _msg, char* _subject, char* _list,
 static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
 static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
 static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
+static int ends_with_f(struct sip_msg *msg, char *str1, char *str2 );
 static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
 static int remove_hf_exp_f(sip_msg_t* msg, char* ematch, char* eskip);
 static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
@@ -316,6 +317,9 @@ static cmd_export_t cmds[]={
 	{"starts_with",  (cmd_function)starts_with_f, 2,
 		fixup_spve_spve, 0,
 		ANY_ROUTE},
+	{"ends_with",  (cmd_function)ends_with_f, 2,
+		fixup_spve_spve, 0,
+		ANY_ROUTE},
 	{"is_audio_on_hold",  (cmd_function)is_audio_on_hold_f, 0,
 		0, 0,
 		ANY_ROUTE},
@@ -4220,6 +4224,49 @@ static int ki_starts_with(sip_msg_t *msg, str *s1, str *s2 )
 	return -2;
 }
 
+static int ends_with_f(struct sip_msg *msg, char *str1, char *str2 )
+{
+	str s1;
+	str s2;
+	int ret;
+
+	if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0) {
+		LM_ERR("cannot get first parameter\n");
+		return -8;
+	}
+	if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0) {
+		LM_ERR("cannot get second parameter\n");
+		return -8;
+	}
+	if(s2.len > s1.len) {
+		return -1;
+	}
+	ret = strncmp(s1.s + s1.len - s2.len, s2.s, s2.len);
+	if(ret==0)
+		return 1;
+	if(ret>0)
+		return -1;
+	return -2;
+}
+
+static int ki_ends_with(sip_msg_t *msg, str *vstr, str *vsuffix )
+{
+	int ret;
+
+	if(vstr==NULL || vsuffix==NULL) {
+		return -1;
+	}
+	if(vsuffix->len > vstr->len) {
+		return -1;
+	}
+	ret = strncmp(vstr->s + vstr->len - vsuffix->len, vsuffix->s, vsuffix->len);
+	if(ret==0)
+		return 1;
+	if(ret>0)
+		return -1;
+	return -2;
+}
+
 static int ki_is_audio_on_hold(sip_msg_t *msg)
 {
 	int sdp_session_num = 0, sdp_stream_num;
@@ -4903,6 +4950,11 @@ static sr_kemi_t sr_kemi_textops_exports[] = {
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
 	},
+	{ str_init("textops"), str_init("ends_with"),
+		SR_KEMIP_INT, ki_ends_with,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
 	{ str_init("textops"), str_init("is_audio_on_hold"),
 		SR_KEMIP_INT, ki_is_audio_on_hold,
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,




More information about the sr-dev mailing list