Browse code

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.

Nicolas C authored on 18/01/2021 16:36:12 • GitHub committed on 18/01/2021 16:36:12
Showing 2 changed files
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com) 
2
+ * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
3 3
  *
4 4
  * Permission to use, copy, modify, and distribute this software for any
5 5
  * 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)
1446 1446
 	return xavu_set_child_xval(rname, cname, &xval);
1447 1447
 }
1448 1448
 
1449
+/**
1450
+ * serialize the values in subfields of an xavu in name=value; format
1451
+ * - rname - name of the root list xavu
1452
+ * - obuf - buffer were to write the output
1453
+ * - olen - the size of obuf
1454
+ * return: 0 - not found; -1 - error; >0 - length of output
1455
+ */
1456
+int xavu_serialize_fields(str *rname, char *obuf, int olen)
1457
+{
1458
+	sr_xavp_t *ravu = NULL;
1459
+	sr_xavp_t *avu = NULL;
1460
+	str ostr;
1461
+	int rlen;
1462
+
1463
+	ravu = xavu_get(rname, NULL);
1464
+	if(ravu==NULL || ravu->val.type!=SR_XTYPE_XAVP) {
1465
+		/* not found or not holding subfields */
1466
+		return 0;
1467
+	}
1468
+
1469
+	rlen = 0;
1470
+	ostr.s = obuf;
1471
+	avu = ravu->val.v.xavp;
1472
+	while(avu) {
1473
+		switch(avu->val.type) {
1474
+			case SR_XTYPE_INT:
1475
+				LM_DBG("     XAVP int value: %d\n", avu->val.v.i);
1476
+				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
1477
+						avu->name.len, avu->name.s, (unsigned int)avu->val.v.i);
1478
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
1479
+					LM_ERR("failed to serialize int value (%d/%d\n",
1480
+							ostr.len, olen-rlen);
1481
+					return -1;
1482
+				}
1483
+			break;
1484
+			case SR_XTYPE_STR:
1485
+				LM_DBG("     XAVP str value: %s\n", avu->val.v.s.s);
1486
+				if(avu->val.v.s.len == 0) {
1487
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s;",
1488
+						avu->name.len, avu->name.s);
1489
+				} else {
1490
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
1491
+						avu->name.len, avu->name.s,
1492
+						avu->val.v.s.len, avu->val.v.s.s);
1493
+				}
1494
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
1495
+					LM_ERR("failed to serialize int value (%d/%d\n",
1496
+							ostr.len, olen-rlen);
1497
+					return -1;
1498
+				}
1499
+			break;
1500
+			default:
1501
+				LM_DBG("skipping value type: %d\n", avu->val.type);
1502
+				ostr.len = 0;
1503
+		}
1504
+		if(ostr.len>0) {
1505
+			ostr.s += ostr.len;
1506
+			rlen += ostr.len;
1507
+		}
1508
+		avu = avu->next;
1509
+	}
1510
+	return rlen;
1511
+}
1449 1512
 
1450 1513
 /**
1451 1514
  *
... ...
@@ -131,6 +131,7 @@ sr_xavp_t *xavu_set_sval(str *rname, str *sval);
131 131
 sr_xavp_t *xavu_set_child_xval(str *rname, str *cname, sr_xval_t *xval);
132 132
 sr_xavp_t *xavu_set_child_ival(str *rname, str *cname, int ival);
133 133
 sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval);
134
+int xavu_serialize_fields(str *rname, char *obuf, int olen);
134 135
 
135 136
 /** xavi api */
136 137
 int xavi_init_head(void);