Browse code

pv: new function xavp_slist_explode("slist", "sep", "mode", "xname")

- break a string in tokens by separators, storing them in xavp

Daniel-Constantin Mierla authored on 14/10/2020 13:45:08
Showing 3 changed files
... ...
@@ -559,6 +559,8 @@ static int w_xavp_copy_dst(sip_msg_t *msg, char *src_name, char *src_idx,
559 559
 		char *dst_name, char *dst_idx);
560 560
 static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
561 561
 static int w_xavp_params_implode(sip_msg_t *msg, char *pxname, char *pvname);
562
+static int w_xavp_slist_explode(sip_msg_t *msg, char *pslist, char *psep,
563
+		char *pmode, char *pxname);
562 564
 static int w_xavp_child_seti(sip_msg_t *msg, char *prname, char *pcname,
563 565
 		char *pval);
564 566
 static int w_xavp_child_sets(sip_msg_t *msg, char *prname, char *pcname,
... ...
@@ -611,6 +613,9 @@ static cmd_export_t cmds[]={
611 613
 	{"not_empty", (cmd_function)pv_not_empty, 1, fixup_pvar_null,
612 614
 		fixup_free_pvar_null,
613 615
 		ANY_ROUTE},
616
+	{"xavp_slist_explode", (cmd_function)w_xavp_slist_explode,
617
+		4, fixup_spve_all, fixup_free_spve_all,
618
+		ANY_ROUTE},
614 619
 	{"xavp_params_explode", (cmd_function)w_xavp_params_explode,
615 620
 		2, fixup_spve_spve, fixup_free_spve_spve,
616 621
 		ANY_ROUTE},
... ...
@@ -1111,6 +1116,53 @@ static int w_xavp_params_implode(sip_msg_t *msg, char *pxname, char *pvname)
1111 1116
 	return ki_xavp_params_implode(msg, &sxname, (str*)pvname);
1112 1117
 }
1113 1118
 
1119
+/**
1120
+ *
1121
+ */
1122
+static int w_xavp_slist_explode(sip_msg_t *msg, char *pslist, char *psep,
1123
+		char *pmode, char *pxname)
1124
+{
1125
+	str slist;
1126
+	str sep;
1127
+	str smode;
1128
+	str sxname;
1129
+
1130
+	if(fixup_get_svalue(msg, (gparam_t*)pslist, &slist)!=0) {
1131
+		LM_ERR("cannot get the params\n");
1132
+		return -1;
1133
+	}
1134
+	if(fixup_get_svalue(msg, (gparam_t*)psep, &sep)!=0) {
1135
+		LM_ERR("cannot get the params\n");
1136
+		return -1;
1137
+	}
1138
+	if(fixup_get_svalue(msg, (gparam_t*)pmode, &smode)!=0) {
1139
+		LM_ERR("cannot get the params\n");
1140
+		return -1;
1141
+	}
1142
+	if(fixup_get_svalue(msg, (gparam_t*)pxname, &sxname)!=0) {
1143
+		LM_ERR("cannot get the xavp name\n");
1144
+		return -1;
1145
+	}
1146
+
1147
+	if(xavp_slist_explode(&slist, &sep, &smode, &sxname)<0)
1148
+		return -1;
1149
+
1150
+	return 1;
1151
+}
1152
+
1153
+/**
1154
+ *
1155
+ */
1156
+static int ki_xavp_slist_explode(sip_msg_t *msg, str *slist, str *sep, str *mode,
1157
+		str *sxname)
1158
+{
1159
+	if(xavp_slist_explode(slist, sep, mode, sxname)<0)
1160
+		return -1;
1161
+
1162
+	return 1;
1163
+}
1164
+
1165
+
1114 1166
 /**
1115 1167
  *
1116 1168
  */
... ...
@@ -580,6 +580,69 @@ int pv_xavi_print(sip_msg_t* msg, char* s1, char *s2)
580 580
 	return 1;
581 581
 }
582 582
 
583
+/**
584
+ *
585
+ */
586
+int xavp_slist_explode(str *slist, str *sep, str *mode, str *xname)
587
+{
588
+	str s;
589
+	sr_xavp_t *xavp=NULL;
590
+	sr_xval_t xval;
591
+	int i;
592
+	int j;
593
+	int sfound;
594
+
595
+	if(slist==NULL || xname==NULL || slist->s==NULL || xname->s==NULL
596
+			|| slist->len<=0 || xname->len<=0 || sep==NULL  || sep->s==NULL
597
+			|| sep->len<=0 || mode==NULL) {
598
+		LM_ERR("invalid parameters\n");
599
+		return -1;
600
+	}
601
+
602
+	s.s = slist->s;
603
+	for(i=0; i<slist->len; i++) {
604
+		LM_DBG("==== %d = %c\n", i, slist->s[i]);
605
+		sfound = 0;
606
+		for(j=0; j<sep->len; j++) {
607
+			if(slist->s[i]==sep->s[j]) {
608
+				sfound = 1;
609
+			}
610
+		}
611
+		if(sfound) {
612
+			s.len = slist->s + i - s.s;
613
+			if(s.len > 0 && mode->len > 0) {
614
+				if(mode->s[0]=='t') {
615
+					trim(&s);
616
+				}
617
+			}
618
+			if(s.len>0) {
619
+				LM_DBG("token found: [%.*s]\n", s.len, s.s);
620
+				memset(&xval, 0, sizeof(sr_xval_t));
621
+				xval.type = SR_XTYPE_STR;
622
+				xval.v.s = s;
623
+				xavp = xavp_add_value_after(xname, &xval, xavp);
624
+			}
625
+			s.s = slist->s + i + 1;
626
+		}
627
+	}
628
+	/* last tocken */
629
+	s.len = slist->s + i - s.s;
630
+	if(s.len > 0 && mode->len > 0) {
631
+		if(mode->s[0]=='t') {
632
+			trim(&s);
633
+		}
634
+	}
635
+	if(s.len>0) {
636
+		LM_DBG("last token found: [%.*s]\n", s.len, s.s);
637
+		memset(&xval, 0, sizeof(sr_xval_t));
638
+		xval.type = SR_XTYPE_STR;
639
+		xval.v.s = s;
640
+		xavp = xavp_add_value_after(xname, &xval, xavp);
641
+	}
642
+
643
+	return 0;
644
+}
645
+
583 646
 /**
584 647
  *
585 648
  */
... ...
@@ -42,6 +42,7 @@ int pv_xavp_print(struct sip_msg* msg, char* s1, char *s2);
42 42
 int pv_xavu_print(struct sip_msg* msg, char* s1, char *s2);
43 43
 int pv_xavi_print(struct sip_msg* msg, char* s1, char *s2);
44 44
 
45
+int xavp_slist_explode(str *slist, str *sep, str *mode, str *xname);
45 46
 int xavp_params_explode(str *params, str *xname);
46 47
 
47 48
 int pv_var_to_xavp(str *varname, str *xname);