Browse code

Revert refactoring

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

Nicolas C authored on 18/01/2021 15:28:48
Showing 2 changed files
... ...
@@ -1030,14 +1030,21 @@ int xavp_set_child_sval(str *rname, str *cname, str *sval)
1030 1030
 }
1031 1031
 
1032 1032
 /**
1033
- * internal function to serialize fields of an xavp (from any of the lists: XAVP, XAUV, or XAVI).
1033
+ * serialize the values in subfields of an xavp in name=value; format
1034
+ * - rname - name of the root list xavp
1035
+ * - obuf - buffer were to write the output
1036
+ * - olen - the size of obuf
1037
+ * return: 0 - not found; -1 - error; >0 - length of output
1034 1038
  */
1035
-int xavx_serialize_fields(sr_xavp_t *ravp, char *obuf, int olen)
1039
+
1040
+int xavp_serialize_fields(str *rname, char *obuf, int olen)
1036 1041
 {
1042
+	sr_xavp_t *ravp = NULL;
1037 1043
 	sr_xavp_t *avp = NULL;
1038 1044
 	str ostr;
1039 1045
 	int rlen;
1040 1046
 
1047
+	ravp = xavp_get(rname, NULL);
1041 1048
 	if(ravp==NULL || ravp->val.type!=SR_XTYPE_XAVP) {
1042 1049
 		/* not found or not holding subfields */
1043 1050
 		return 0;
... ...
@@ -1087,18 +1094,6 @@ int xavx_serialize_fields(sr_xavp_t *ravp, char *obuf, int olen)
1087 1094
 	return rlen;
1088 1095
 }
1089 1096
 
1090
-/**
1091
- * serialize the values in subfields of an xavp in name=value; format
1092
- * - rname - name of the root list xavp
1093
- * - obuf - buffer were to write the output
1094
- * - olen - the size of obuf
1095
- * return: 0 - not found; -1 - error; >0 - length of output
1096
- */
1097
-int xavp_serialize_fields(str *rname, char *obuf, int olen)
1098
-{
1099
-	return xavx_serialize_fields(xavp_get(rname, NULL), obuf, olen);
1100
-}
1101
-
1102 1097
 /**
1103 1098
  *
1104 1099
  */
... ...
@@ -1460,9 +1455,60 @@ sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval)
1460 1455
  */
1461 1456
 int xavu_serialize_fields(str *rname, char *obuf, int olen)
1462 1457
 {
1463
-	return xavx_serialize_fields(xavu_get(rname, NULL), obuf, olen);
1464
-}
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
+	}
1465 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
+}
1466 1512
 
1467 1513
 /**
1468 1514
  *
... ...
@@ -2390,5 +2436,57 @@ int xavi_set_child_sval(str *rname, str *cname, str *sval)
2390 2436
  */
2391 2437
 int xavi_serialize_fields(str *rname, char *obuf, int olen)
2392 2438
 {
2393
-	return xavx_serialize_fields(xavi_get(rname, NULL), obuf, olen);
2439
+	sr_xavp_t *ravi = NULL;
2440
+	sr_xavp_t *avi = NULL;
2441
+	str ostr;
2442
+	int rlen;
2443
+
2444
+	ravi = xavi_get(rname, NULL);
2445
+	if(ravi==NULL || ravi->val.type!=SR_XTYPE_XAVP) {
2446
+		/* not found or not holding subfields */
2447
+		return 0;
2448
+	}
2449
+
2450
+	rlen = 0;
2451
+	ostr.s = obuf;
2452
+	avi = ravi->val.v.xavp;
2453
+	while(avi) {
2454
+		switch(avi->val.type) {
2455
+			case SR_XTYPE_INT:
2456
+				LM_DBG("     XAVP int value: %d\n", avi->val.v.i);
2457
+				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
2458
+						avi->name.len, avi->name.s, (unsigned int)avi->val.v.i);
2459
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
2460
+					LM_ERR("failed to serialize int value (%d/%d\n",
2461
+							ostr.len, olen-rlen);
2462
+					return -1;
2463
+				}
2464
+			break;
2465
+			case SR_XTYPE_STR:
2466
+				LM_DBG("     XAVP str value: %s\n", avi->val.v.s.s);
2467
+				if(avi->val.v.s.len == 0) {
2468
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s;",
2469
+						avi->name.len, avi->name.s);
2470
+				} else {
2471
+					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
2472
+						avi->name.len, avi->name.s,
2473
+						avi->val.v.s.len, avi->val.v.s.s);
2474
+				}
2475
+				if(ostr.len<=0 || ostr.len>=olen-rlen) {
2476
+					LM_ERR("failed to serialize int value (%d/%d\n",
2477
+							ostr.len, olen-rlen);
2478
+					return -1;
2479
+				}
2480
+			break;
2481
+			default:
2482
+				LM_DBG("skipping value type: %d\n", avi->val.type);
2483
+				ostr.len = 0;
2484
+		}
2485
+		if(ostr.len>0) {
2486
+			ostr.s += ostr.len;
2487
+			rlen += ostr.len;
2488
+		}
2489
+		avi = avi->next;
2490
+	}
2491
+	return rlen;
2394 2492
 }
... ...
@@ -107,7 +107,6 @@ sr_xavp_t *xavp_clone_level_nodata_with_new_name(sr_xavp_t *xold, str *dst_name)
107 107
 sr_xavp_t* xavp_get_child(str *rname, str *cname);
108 108
 sr_xavp_t* xavp_get_child_with_ival(str *rname, str *cname);
109 109
 sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname);
110
-int xavx_serialize_fields(sr_xavp_t *ravp, char *obuf, int olen);
111 110
 int xavp_serialize_fields(str *rname, char *obuf, int olen);
112 111
 
113 112
 int xavp_set_child_ival(str *rname, str *cname, int ival);