Browse code

core: xavp - helper function to serialize subfields in a params list

Daniel-Constantin Mierla authored on 20/04/2018 09:58:20
Showing 2 changed files
... ...
@@ -843,4 +843,65 @@ sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname)
843 843
 
844 844
 	return vavp;
845 845
 }
846
+
847
+
848
+/**
849
+ * serialize the values in subfields of an xavp in name=value; format
850
+ * - rname - name of the root list xavp
851
+ * - obuf - buffer were to write the output
852
+ * - olen - the size of obuf
853
+ * return: 0 - not found; -1 - error; >0 - length of output
854
+ */
855
+
856
+int xavp_serialize_fields(str *rname, char *obuf, int olen)
857
+{
858
+	sr_xavp_t *ravp = NULL;
859
+	sr_xavp_t *avp = NULL;
860
+	str ostr;
861
+	int rlen;
862
+
863
+	ravp = xavp_get(rname, NULL);
864
+	if(ravp==NULL || ravp->val.type!=SR_XTYPE_XAVP) {
865
+		/* not found or not holding subfields */
866
+		return 0;
867
+	}
868
+
869
+	rlen = 0;
870
+	ostr.s = obuf;
871
+	avp = ravp->val.v.xavp;
872
+	while(avp) {
873
+		switch(avp->val.type) {
874
+			case SR_XTYPE_INT:
875
+				LM_DBG("     XAVP int value: %d\n", avp->val.v.i);
876
+				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
877
+						avp->name.len, avp->name.s, (unsigned int)avp->val.v.i);
878
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
879
+					LM_ERR("failed to serialize int value (%d/%d\n",
880
+							ostr.len, olen-rlen);
881
+					return -1;
882
+				}
883
+			break;
884
+			case SR_XTYPE_STR:
885
+				LM_DBG("     XAVP str value: %s\n", avp->val.v.s.s);
886
+				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
887
+						avp->name.len, avp->name.s,
888
+						avp->val.v.s.len, avp->val.v.s.s);
889
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
890
+					LM_ERR("failed to serialize int value (%d/%d\n",
891
+							ostr.len, olen-rlen);
892
+					return -1;
893
+				}
894
+			break;
895
+			default:
896
+				LM_DBG("skipping value type: %d\n", avp->val.type);
897
+				ostr.len = 0;
898
+		}
899
+		if(ostr.len>0) {
900
+			ostr.s += ostr.len;
901
+			rlen += ostr.len;
902
+		}
903
+		avp = avp->next;
904
+	}
905
+	return rlen;
906
+}
846 907
 #endif
... ...
@@ -101,6 +101,8 @@ sr_xavp_t *xavp_clone_level_nodata(sr_xavp_t *xold);
101 101
 sr_xavp_t* xavp_get_child(str *rname, str *cname);
102 102
 sr_xavp_t* xavp_get_child_with_ival(str *rname, str *cname);
103 103
 sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname);
104
+int xavp_serialize_fields(str *rname, char *obuf, int olen);
105
+
104 106
 #endif
105 107
 
106 108
 #endif