core[xavp]: Added helper function to get a list of keys from a xavp variable.
authorVictor Seva <linuxmaniac@torreviejawireless.org>
Mon, 22 Apr 2013 08:46:17 +0000 (10:46 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Thu, 25 Apr 2013 08:05:46 +0000 (10:05 +0200)
xavp.c
xavp.h

diff --git a/xavp.c b/xavp.c
index 6c8566d..adce5d1 100644 (file)
--- a/xavp.c
+++ b/xavp.c
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "mem/mem.h"
 #include "mem/shm_mem.h"
 #include "dprint.h"
 #include "hashes.h"
@@ -538,6 +539,67 @@ void xavp_print_list(sr_xavp_t **head)
 }
 
 /**
+ * returns a list of str with key names.
+ * Example:
+ * If we have this structure
+ * $xavp(test=>one) = 1
+ * $xavp(test[0]=>two) = "2"
+ * $xavp(test[0]=>three) = 3
+ * $xavp(test[0]=>four) = $xavp(whatever)
+ *
+ * xavp_get_list_keys_names(test[0]) returns
+ * {"one", "two", "three", "four"}
+ */
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp)
+{
+       sr_xavp_t *avp = NULL;
+       struct str_list *result = NULL;
+       struct str_list *r = NULL;
+       int total = 0;
+
+       if(xavp==NULL){
+               LM_ERR("xavp is NULL\n");
+               return 0;
+       }
+
+       if(xavp->val.type!=SR_XTYPE_XAVP){
+               LM_ERR("%s not xavp?\n", xavp->name.s);
+               return 0;
+       }
+
+       avp = xavp->val.v.xavp;
+
+       if (avp)
+       {
+               result = (struct str_list*)pkg_malloc(sizeof(struct str_list));
+               if (result==NULL) {
+                       PKG_MEM_ERROR;
+                       return 0;
+               }
+               r = result;
+               r->s.s = avp->name.s;
+               r->s.len = avp->name.len;
+               r->next = NULL;
+               avp = avp->next;
+       }
+
+       while(avp)
+       {
+               r = append_str_list(avp->name.s, avp->name.len, &r, &total);
+               if(r==NULL){
+                       while(result){
+                               r = result;
+                               result = result->next;
+                               pkg_free(r);
+                       }
+                       return 0;
+               }
+               avp = avp->next;
+       }
+       return result;
+}
+
+/**
  * clone the xavp without values that are custom data
  * - only one list level is cloned, other sublists are ignored
  */
diff --git a/xavp.h b/xavp.h
index b581c08..7d5d517 100644 (file)
--- a/xavp.h
+++ b/xavp.h
@@ -29,6 +29,7 @@
 
 #include <time.h>
 #include "str.h"
+#include "str_list.h"
 
 struct _sr_xavp;
 
@@ -95,6 +96,7 @@ void xavp_destroy_list(sr_xavp_t **head);
 void xavp_reset_list(void);
 sr_xavp_t **xavp_set_list(sr_xavp_t **head);
 sr_xavp_t **xavp_get_crt_list(void);
+struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp);
 
 int xavp_insert(sr_xavp_t *xavp, int idx, sr_xavp_t **list);
 sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list);