Browse code

textopsx: bl iterator functions to append/insert text

Daniel-Constantin Mierla authored on 04/08/2021 07:07:36
Showing 2 changed files
... ...
@@ -787,6 +787,64 @@ bl_iterator_end("b1");
787 787
     }
788 788
     bl_iterator_end("b1");
789 789
 ...
790
+</programlisting>
791
+		</example>
792
+	</section>
793
+	<section id="textopsx.f.bl_iterator_append">
794
+		<title>
795
+		<function moreinfo="none">bl_iterator_append(iname, text)</function>
796
+		</title>
797
+		<para>
798
+			Add text after the line at the current iterator possition.
799
+		</para>
800
+		<para>
801
+			The parameters can be dynamic strings with variables.
802
+		</para>
803
+		<para>
804
+			This function can be used from ANY_ROUTE.
805
+		</para>
806
+		<example>
807
+		<title><function>bl_iterator_append</function> usage</title>
808
+		<programlisting format="linespecific">
809
+...
810
+    bl_iterator_start("b1");
811
+    while(bl_iterator_next("b1")) {
812
+        if($blitval(b1)=~"^a=info:xyz") {
813
+            bl_iterator_append("b1", "a=info:abc\r\n");
814
+            break;
815
+        }
816
+    }
817
+    bl_iterator_end("b1");
818
+...
819
+</programlisting>
820
+		</example>
821
+	</section>
822
+	<section id="textopsx.f.bl_iterator_insert">
823
+		<title>
824
+		<function moreinfo="none">bl_iterator_insert(iname, text)</function>
825
+		</title>
826
+		<para>
827
+			Add text before the line at the current iterator possition.
828
+		</para>
829
+		<para>
830
+			The parameters can be dynamic strings with variables.
831
+		</para>
832
+		<para>
833
+			This function can be used from ANY_ROUTE.
834
+		</para>
835
+		<example>
836
+		<title><function>bl_iterator_insert</function> usage</title>
837
+		<programlisting format="linespecific">
838
+...
839
+    bl_iterator_start("b1");
840
+    while(bl_iterator_next("b1")) {
841
+        if($blitval(b1)=~"^a=info:xyz") {
842
+            bl_iterator_insert("b1", "a=info:abc\r\n");
843
+            break;
844
+        }
845
+    }
846
+    bl_iterator_end("b1");
847
+...
790 848
 </programlisting>
791 849
 		</example>
792 850
 	</section>
... ...
@@ -85,6 +85,8 @@ static int w_bl_iterator_start(sip_msg_t *msg, char *piname, char *p2);
85 85
 static int w_bl_iterator_next(sip_msg_t *msg, char *piname, char *p2);
86 86
 static int w_bl_iterator_end(sip_msg_t *msg, char *piname, char *p2);
87 87
 static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
88
+static int w_bl_iterator_append(sip_msg_t *msg, char *piname, char *ptext);
89
+static int w_bl_iterator_insert(sip_msg_t *msg, char *piname, char *ptext);
88 90
 
89 91
 static int bind_textopsx(textopsx_api_t *tob);
90 92
 
... ...
@@ -168,6 +170,10 @@ static cmd_export_t cmds[] = {
168 170
 			fixup_free_spve_null, ANY_ROUTE},
169 171
 	{"bl_iterator_rm", w_bl_iterator_rm, 1, fixup_spve_null,
170 172
 			fixup_free_spve_null, ANY_ROUTE},
173
+	{"bl_iterator_append", w_bl_iterator_append, 2, fixup_spve_spve,
174
+			fixup_free_spve_spve, ANY_ROUTE},
175
+	{"bl_iterator_insert", w_bl_iterator_insert, 2, fixup_spve_spve,
176
+			fixup_free_spve_spve, ANY_ROUTE},
171 177
 
172 178
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
173 179
 
... ...
@@ -2599,6 +2605,120 @@ static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2)
2599 2605
 	return ki_bl_iterator_rm(msg, &iname);
2600 2606
 }
2601 2607
 
2608
+/**
2609
+ *
2610
+ */
2611
+static int ki_bl_iterator_append(sip_msg_t *msg, str *iname, str *text)
2612
+{
2613
+	int k;
2614
+	sr_lump_t *anchor;
2615
+	str sval = STR_NULL;
2616
+
2617
+	k = ki_bl_iterator_index(msg, iname);
2618
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2619
+		return -1;
2620
+	}
2621
+	anchor = anchor_lump(msg, _bl_iterators[k].it.s
2622
+			+ _bl_iterators[k].it.len - msg->buf, 0, 0);
2623
+	if (anchor==0) {
2624
+		LM_ERR("cannot append text after %.*s\n", _bl_iterators[k].it.len,
2625
+				_bl_iterators[k].it.s);
2626
+		return -1;
2627
+	}
2628
+	sval.s = (char*)pkg_malloc(text->len + 1);
2629
+	if(sval.s==NULL) {
2630
+		LM_ERR("failed append text after %.*s\n", _bl_iterators[k].it.len,
2631
+				_bl_iterators[k].it.s);
2632
+		return -1;
2633
+	}
2634
+	memcpy(sval.s, text->s, text->len);
2635
+	sval.len = text->len;
2636
+	sval.s[sval.len] = '\0';
2637
+
2638
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2639
+		LM_ERR("cannot insert lump\n");
2640
+		pkg_free(sval.s);
2641
+		return -1;
2642
+	}
2643
+	return 1;
2644
+}
2645
+
2646
+/**
2647
+ *
2648
+ */
2649
+static int w_bl_iterator_append(sip_msg_t *msg, char *piname, char *ptext)
2650
+{
2651
+	str iname = STR_NULL;
2652
+	str text = STR_NULL;
2653
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2654
+		LM_ERR("failed to get iterator name\n");
2655
+		return -1;
2656
+	}
2657
+	if(fixup_get_svalue(msg, (gparam_t*)ptext, &text)<0) {
2658
+		LM_ERR("failed to get text\n");
2659
+		return -1;
2660
+	}
2661
+
2662
+	return ki_bl_iterator_append(msg, &iname, &text);
2663
+}
2664
+
2665
+/**
2666
+ *
2667
+ */
2668
+static int ki_bl_iterator_insert(sip_msg_t *msg, str *iname, str *text)
2669
+{
2670
+	int k;
2671
+	sr_lump_t *anchor;
2672
+	str sval = STR_NULL;
2673
+
2674
+	k = ki_bl_iterator_index(msg, iname);
2675
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2676
+		return -1;
2677
+	}
2678
+	anchor = anchor_lump(msg, _bl_iterators[k].it.s - msg->buf, 0, 0);
2679
+	if (anchor==0) {
2680
+		LM_ERR("cannot insert text after %.*s\n", _bl_iterators[k].it.len,
2681
+				_bl_iterators[k].it.s);
2682
+		return -1;
2683
+	}
2684
+	sval.s = (char*)pkg_malloc(text->len + 1);
2685
+	if(sval.s==NULL) {
2686
+		LM_ERR("failed to insert text after %.*s\n", _bl_iterators[k].it.len,
2687
+				_bl_iterators[k].it.s);
2688
+		return -1;
2689
+	}
2690
+	memcpy(sval.s, text->s, text->len);
2691
+	sval.len = text->len;
2692
+	sval.s[sval.len] = '\0';
2693
+
2694
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2695
+		LM_ERR("cannot insert lump\n");
2696
+		pkg_free(sval.s);
2697
+		return -1;
2698
+	}
2699
+	return 1;
2700
+}
2701
+
2702
+/**
2703
+ *
2704
+ */
2705
+static int w_bl_iterator_insert(sip_msg_t *msg, char *piname, char *ptext)
2706
+{
2707
+	str iname = STR_NULL;
2708
+	str text = STR_NULL;
2709
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2710
+		LM_ERR("failed to get iterator name\n");
2711
+		return -1;
2712
+	}
2713
+	if(fixup_get_svalue(msg, (gparam_t*)ptext, &text)<0) {
2714
+		LM_ERR("failed to get text\n");
2715
+		return -1;
2716
+	}
2717
+
2718
+	return ki_bl_iterator_insert(msg, &iname, &text);
2719
+}
2720
+
2721
+
2602 2722
 /**
2603 2723
  *
2604 2724
  */
... ...
@@ -3221,6 +3341,16 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
3221 3341
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3222 3342
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3223 3343
 	},
3344
+	{ str_init("textopsx"), str_init("bl_iterator_append"),
3345
+		SR_KEMIP_INT, ki_bl_iterator_append,
3346
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3347
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3348
+	},
3349
+	{ str_init("textopsx"), str_init("bl_iterator_insert"),
3350
+		SR_KEMIP_INT, ki_bl_iterator_insert,
3351
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3352
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3353
+	},
3224 3354
 
3225 3355
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
3226 3356
 };