Browse code

core[xavp]: Added helper function to get a list of keys from a xavp variable.

Victor Seva authored on 22/04/2013 08:46:17 • Daniel-Constantin Mierla committed on 25/04/2013 08:05:46
Showing 2 changed files
... ...
@@ -27,6 +27,7 @@
27 27
 #include <stdio.h>
28 28
 #include <string.h>
29 29
 
30
+#include "mem/mem.h"
30 31
 #include "mem/shm_mem.h"
31 32
 #include "dprint.h"
32 33
 #include "hashes.h"
... ...
@@ -537,6 +538,67 @@ void xavp_print_list(sr_xavp_t **head)
537 538
 	xavp_print_list_content(head, 0);
538 539
 }
539 540
 
541
+/**
542
+ * returns a list of str with key names.
543
+ * Example:
544
+ * If we have this structure
545
+ * $xavp(test=>one) = 1
546
+ * $xavp(test[0]=>two) = "2"
547
+ * $xavp(test[0]=>three) = 3
548
+ * $xavp(test[0]=>four) = $xavp(whatever)
549
+ *
550
+ * xavp_get_list_keys_names(test[0]) returns
551
+ * {"one", "two", "three", "four"}
552
+ */
553
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp)
554
+{
555
+	sr_xavp_t *avp = NULL;
556
+	struct str_list *result = NULL;
557
+	struct str_list *r = NULL;
558
+	int total = 0;
559
+
560
+	if(xavp==NULL){
561
+		LM_ERR("xavp is NULL\n");
562
+		return 0;
563
+	}
564
+
565
+	if(xavp->val.type!=SR_XTYPE_XAVP){
566
+		LM_ERR("%s not xavp?\n", xavp->name.s);
567
+		return 0;
568
+	}
569
+
570
+	avp = xavp->val.v.xavp;
571
+
572
+	if (avp)
573
+	{
574
+		result = (struct str_list*)pkg_malloc(sizeof(struct str_list));
575
+		if (result==NULL) {
576
+			PKG_MEM_ERROR;
577
+			return 0;
578
+		}
579
+		r = result;
580
+		r->s.s = avp->name.s;
581
+		r->s.len = avp->name.len;
582
+		r->next = NULL;
583
+		avp = avp->next;
584
+	}
585
+
586
+	while(avp)
587
+	{
588
+		r = append_str_list(avp->name.s, avp->name.len, &r, &total);
589
+		if(r==NULL){
590
+			while(result){
591
+				r = result;
592
+				result = result->next;
593
+				pkg_free(r);
594
+			}
595
+			return 0;
596
+		}
597
+		avp = avp->next;
598
+	}
599
+	return result;
600
+}
601
+
540 602
 /**
541 603
  * clone the xavp without values that are custom data
542 604
  * - only one list level is cloned, other sublists are ignored
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include <time.h>
31 31
 #include "str.h"
32
+#include "str_list.h"
32 33
 
33 34
 struct _sr_xavp;
34 35
 
... ...
@@ -95,6 +96,7 @@ void xavp_destroy_list(sr_xavp_t **head);
95 96
 void xavp_reset_list(void);
96 97
 sr_xavp_t **xavp_set_list(sr_xavp_t **head);
97 98
 sr_xavp_t **xavp_get_crt_list(void);
99
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp);
98 100
 
99 101
 int xavp_insert(sr_xavp_t *xavp, int idx, sr_xavp_t **list);
100 102
 sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list);