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
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"
... ...
@@ -538,6 +539,67 @@ void xavp_print_list(sr_xavp_t **head)
538 538
 }
539 539
 
540 540
 /**
541
+ * returns a list of str with key names.
542
+ * Example:
543
+ * If we have this structure
544
+ * $xavp(test=>one) = 1
545
+ * $xavp(test[0]=>two) = "2"
546
+ * $xavp(test[0]=>three) = 3
547
+ * $xavp(test[0]=>four) = $xavp(whatever)
548
+ *
549
+ * xavp_get_list_keys_names(test[0]) returns
550
+ * {"one", "two", "three", "four"}
551
+ */
552
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp)
553
+{
554
+	sr_xavp_t *avp = NULL;
555
+	struct str_list *result = NULL;
556
+	struct str_list *r = NULL;
557
+	int total = 0;
558
+
559
+	if(xavp==NULL){
560
+		LM_ERR("xavp is NULL\n");
561
+		return 0;
562
+	}
563
+
564
+	if(xavp->val.type!=SR_XTYPE_XAVP){
565
+		LM_ERR("%s not xavp?\n", xavp->name.s);
566
+		return 0;
567
+	}
568
+
569
+	avp = xavp->val.v.xavp;
570
+
571
+	if (avp)
572
+	{
573
+		result = (struct str_list*)pkg_malloc(sizeof(struct str_list));
574
+		if (result==NULL) {
575
+			PKG_MEM_ERROR;
576
+			return 0;
577
+		}
578
+		r = result;
579
+		r->s.s = avp->name.s;
580
+		r->s.len = avp->name.len;
581
+		r->next = NULL;
582
+		avp = avp->next;
583
+	}
584
+
585
+	while(avp)
586
+	{
587
+		r = append_str_list(avp->name.s, avp->name.len, &r, &total);
588
+		if(r==NULL){
589
+			while(result){
590
+				r = result;
591
+				result = result->next;
592
+				pkg_free(r);
593
+			}
594
+			return 0;
595
+		}
596
+		avp = avp->next;
597
+	}
598
+	return result;
599
+}
600
+
601
+/**
541 602
  * clone the xavp without values that are custom data
542 603
  * - only one list level is cloned, other sublists are ignored
543 604
  */
... ...
@@ -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 95
 void xavp_reset_list(void);
96 96
 sr_xavp_t **xavp_set_list(sr_xavp_t **head);
97 97
 sr_xavp_t **xavp_get_crt_list(void);
98
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp);
98 99
 
99 100
 int xavp_insert(sr_xavp_t *xavp, int idx, sr_xavp_t **list);
100 101
 sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list);