Browse code

core: xavp - added functions to set first xavp with first child to int or str

- similar to $xavp(x=>y) = v; - if that xavp doesn not exist, or
$xavp(x[0]=>y[0]) = v; - if that xavp exists

Daniel-Constantin Mierla authored on 16/04/2019 07:00:37
Showing 2 changed files
... ...
@@ -896,6 +896,77 @@ sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname)
896 896
 	return vavp;
897 897
 }
898 898
 
899
+/**
900
+ * Set the value of the first xavp rname with first child xavp cname
901
+ * - replace if it exits; add if it doesn't exist
902
+ * - config operations:
903
+ *   $xavp(rxname=>cname) = xval;
904
+ *     or:
905
+ *   $xavp(rxname[0]=>cname[0]) = xval;
906
+ */
907
+int xavp_set_child_xval(str *rname, str *cname, sr_xval_t *xval)
908
+{
909
+	sr_xavp_t *ravp=NULL;
910
+	sr_xavp_t *cavp=NULL;
911
+
912
+	ravp = xavp_get(rname, NULL);
913
+	if(ravp) {
914
+		if(ravp->val.type != SR_XTYPE_XAVP) {
915
+			/* first root xavp does not have xavp list value - remove it */
916
+			xavp_rm(ravp, NULL);
917
+			/* add a new xavp in the root list with a child */
918
+			if(xavp_add_xavp_value(rname, cname, xval, NULL)==NULL) {
919
+				return -1;
920
+			}
921
+		} else {
922
+			/* first root xavp has an xavp list value */
923
+			cavp = xavp_get(cname, ravp->val.v.xavp);
924
+			if(cavp) {
925
+				/* child xavp with same name - remove it */
926
+				/* todo: update in place for int or if allocated size fits */
927
+				xavp_rm(cavp, &ravp->val.v.xavp);
928
+			}
929
+			if(xavp_add_value(cname, xval, &ravp->val.v.xavp)==NULL) {
930
+				return -1;
931
+			}
932
+		}
933
+	} else {
934
+		/* no xavp with rname in root list found */
935
+		if(xavp_add_xavp_value(rname, cname, xval, NULL)==NULL) {
936
+			return -1;
937
+		}
938
+	}
939
+
940
+	return 0;
941
+}
942
+
943
+/**
944
+ *
945
+ */
946
+int xavp_set_child_ival(str *rname, str *cname, int ival)
947
+{
948
+	sr_xval_t xval;
949
+
950
+	memset(&xval, 0, sizeof(sr_xval_t));
951
+	xval.type = SR_XTYPE_INT;
952
+	xval.v.i = ival;
953
+
954
+	return xavp_set_child_xval(rname, cname, &xval);
955
+}
956
+
957
+/**
958
+ *
959
+ */
960
+int xavp_set_child_sval(str *rname, str *cname, str *sval)
961
+{
962
+	sr_xval_t xval;
963
+
964
+	memset(&xval, 0, sizeof(sr_xval_t));
965
+	xval.type = SR_XTYPE_STR;
966
+	xval.v.s = *sval;
967
+
968
+	return xavp_set_child_xval(rname, cname, &xval);
969
+}
899 970
 
900 971
 /**
901 972
  * serialize the values in subfields of an xavp in name=value; format
... ...
@@ -108,6 +108,8 @@ sr_xavp_t* xavp_get_child_with_ival(str *rname, str *cname);
108 108
 sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname);
109 109
 int xavp_serialize_fields(str *rname, char *obuf, int olen);
110 110
 
111
+int xavp_set_child_ival(str *rname, str *cname, int ival);
112
+int xavp_set_child_sval(str *rname, str *cname, str *sval);
111 113
 #endif
112 114
 
113 115
 #endif