[sr-dev] git:master:f9261f10: core: added xavu_serialize_fields function (#2603)

GitHub noreply at github.com
Mon Jan 18 17:36:17 CET 2021


Module: kamailio
Branch: master
Commit: f9261f10188eabc7b4e5cd1fc7964fc01fedb9e0
URL: https://github.com/kamailio/kamailio/commit/f9261f10188eabc7b4e5cd1fc7964fc01fedb9e0

Author: Nicolas C <nchaigne at capgemini.fr>
Committer: GitHub <noreply at github.com>
Date: 2021-01-18T17:36:12+01:00

core: added xavu_serialize_fields function (#2603)

* core: added xavu_serialize_fields function

Also added a function called by the three "*_serialize_fields" to reduce code duplication.

The interface is not modified.

* Revert refactoring

We can add xavu_serialize_fields, but do not change the other functions.

---

Modified: src/core/xavp.c
Modified: src/core/xavp.h

---

Diff:  https://github.com/kamailio/kamailio/commit/f9261f10188eabc7b4e5cd1fc7964fc01fedb9e0.diff
Patch: https://github.com/kamailio/kamailio/commit/f9261f10188eabc7b4e5cd1fc7964fc01fedb9e0.patch

---

diff --git a/src/core/xavp.c b/src/core/xavp.c
index efa7c32cb8..de6da0c858 100644
--- a/src/core/xavp.c
+++ b/src/core/xavp.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com) 
+ * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -1446,6 +1446,69 @@ sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval)
 	return xavu_set_child_xval(rname, cname, &xval);
 }
 
+/**
+ * serialize the values in subfields of an xavu in name=value; format
+ * - rname - name of the root list xavu
+ * - obuf - buffer were to write the output
+ * - olen - the size of obuf
+ * return: 0 - not found; -1 - error; >0 - length of output
+ */
+int xavu_serialize_fields(str *rname, char *obuf, int olen)
+{
+	sr_xavp_t *ravu = NULL;
+	sr_xavp_t *avu = NULL;
+	str ostr;
+	int rlen;
+
+	ravu = xavu_get(rname, NULL);
+	if(ravu==NULL || ravu->val.type!=SR_XTYPE_XAVP) {
+		/* not found or not holding subfields */
+		return 0;
+	}
+
+	rlen = 0;
+	ostr.s = obuf;
+	avu = ravu->val.v.xavp;
+	while(avu) {
+		switch(avu->val.type) {
+			case SR_XTYPE_INT:
+				LM_DBG("     XAVP int value: %d\n", avu->val.v.i);
+				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
+						avu->name.len, avu->name.s, (unsigned int)avu->val.v.i);
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
+					LM_ERR("failed to serialize int value (%d/%d\n",
+							ostr.len, olen-rlen);
+					return -1;
+				}
+			break;
+			case SR_XTYPE_STR:
+				LM_DBG("     XAVP str value: %s\n", avu->val.v.s.s);
+				if(avu->val.v.s.len == 0) {
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s;",
+						avu->name.len, avu->name.s);
+				} else {
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
+						avu->name.len, avu->name.s,
+						avu->val.v.s.len, avu->val.v.s.s);
+				}
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
+					LM_ERR("failed to serialize int value (%d/%d\n",
+							ostr.len, olen-rlen);
+					return -1;
+				}
+			break;
+			default:
+				LM_DBG("skipping value type: %d\n", avu->val.type);
+				ostr.len = 0;
+		}
+		if(ostr.len>0) {
+			ostr.s += ostr.len;
+			rlen += ostr.len;
+		}
+		avu = avu->next;
+	}
+	return rlen;
+}
 
 /**
  *
diff --git a/src/core/xavp.h b/src/core/xavp.h
index 50ae165c13..198f0c09c4 100644
--- a/src/core/xavp.h
+++ b/src/core/xavp.h
@@ -131,6 +131,7 @@ sr_xavp_t *xavu_set_sval(str *rname, str *sval);
 sr_xavp_t *xavu_set_child_xval(str *rname, str *cname, sr_xval_t *xval);
 sr_xavp_t *xavu_set_child_ival(str *rname, str *cname, int ival);
 sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval);
+int xavu_serialize_fields(str *rname, char *obuf, int olen);
 
 /** xavi api */
 int xavi_init_head(void);




More information about the sr-dev mailing list