Browse code

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.

Nicolas C authored on 18/01/2021 14:51:01
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
... ...
@@ -1030,21 +1030,14 @@ int xavp_set_child_sval(str *rname, str *cname, str *sval)
1030 1030
 }
1031 1031
 
1032 1032
 /**
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
1033
+ * internal function to serialize fields of an xavp (from any of the lists: XAVP, XAUV, or XAVI).
1038 1034
  */
1039
-
1040
-int xavp_serialize_fields(str *rname, char *obuf, int olen)
1035
+int xavx_serialize_fields(sr_xavp_t *ravp, char *obuf, int olen)
1041 1036
 {
1042
-	sr_xavp_t *ravp = NULL;
1043 1037
 	sr_xavp_t *avp = NULL;
1044 1038
 	str ostr;
1045 1039
 	int rlen;
1046 1040
 
1047
-	ravp = xavp_get(rname, NULL);
1048 1041
 	if(ravp==NULL || ravp->val.type!=SR_XTYPE_XAVP) {
1049 1042
 		/* not found or not holding subfields */
1050 1043
 		return 0;
... ...
@@ -1094,6 +1087,18 @@ int xavp_serialize_fields(str *rname, char *obuf, int olen)
1094 1087
 	return rlen;
1095 1088
 }
1096 1089
 
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
+
1097 1102
 /**
1098 1103
  *
1099 1104
  */
... ...
@@ -1446,6 +1451,18 @@ sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval)
1446 1451
 	return xavu_set_child_xval(rname, cname, &xval);
1447 1452
 }
1448 1453
 
1454
+/**
1455
+ * serialize the values in subfields of an xavu in name=value; format
1456
+ * - rname - name of the root list xavu
1457
+ * - obuf - buffer were to write the output
1458
+ * - olen - the size of obuf
1459
+ * return: 0 - not found; -1 - error; >0 - length of output
1460
+ */
1461
+int xavu_serialize_fields(str *rname, char *obuf, int olen)
1462
+{
1463
+	return xavx_serialize_fields(xavu_get(rname, NULL), obuf, olen);
1464
+}
1465
+
1449 1466
 
1450 1467
 /**
1451 1468
  *
... ...
@@ -2373,57 +2390,5 @@ int xavi_set_child_sval(str *rname, str *cname, str *sval)
2373 2390
  */
2374 2391
 int xavi_serialize_fields(str *rname, char *obuf, int olen)
2375 2392
 {
2376
-	sr_xavp_t *ravi = NULL;
2377
-	sr_xavp_t *avi = NULL;
2378
-	str ostr;
2379
-	int rlen;
2380
-
2381
-	ravi = xavi_get(rname, NULL);
2382
-	if(ravi==NULL || ravi->val.type!=SR_XTYPE_XAVP) {
2383
-		/* not found or not holding subfields */
2384
-		return 0;
2385
-	}
2386
-
2387
-	rlen = 0;
2388
-	ostr.s = obuf;
2389
-	avi = ravi->val.v.xavp;
2390
-	while(avi) {
2391
-		switch(avi->val.type) {
2392
-			case SR_XTYPE_INT:
2393
-				LM_DBG("     XAVP int value: %d\n", avi->val.v.i);
2394
-				ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
2395
-						avi->name.len, avi->name.s, (unsigned int)avi->val.v.i);
2396
-				if(ostr.len<=0 || ostr.len>=olen-rlen) {
2397
-					LM_ERR("failed to serialize int value (%d/%d\n",
2398
-							ostr.len, olen-rlen);
2399
-					return -1;
2400
-				}
2401
-			break;
2402
-			case SR_XTYPE_STR:
2403
-				LM_DBG("     XAVP str value: %s\n", avi->val.v.s.s);
2404
-				if(avi->val.v.s.len == 0) {
2405
-					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s;",
2406
-						avi->name.len, avi->name.s);
2407
-				} else {
2408
-					ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
2409
-						avi->name.len, avi->name.s,
2410
-						avi->val.v.s.len, avi->val.v.s.s);
2411
-				}
2412
-				if(ostr.len<=0 || ostr.len>=olen-rlen) {
2413
-					LM_ERR("failed to serialize int value (%d/%d\n",
2414
-							ostr.len, olen-rlen);
2415
-					return -1;
2416
-				}
2417
-			break;
2418
-			default:
2419
-				LM_DBG("skipping value type: %d\n", avi->val.type);
2420
-				ostr.len = 0;
2421
-		}
2422
-		if(ostr.len>0) {
2423
-			ostr.s += ostr.len;
2424
-			rlen += ostr.len;
2425
-		}
2426
-		avi = avi->next;
2427
-	}
2428
-	return rlen;
2393
+	return xavx_serialize_fields(xavi_get(rname, NULL), obuf, olen);
2429 2394
 }
... ...
@@ -107,6 +107,7 @@ 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);
110 111
 int xavp_serialize_fields(str *rname, char *obuf, int olen);
111 112
 
112 113
 int xavp_set_child_ival(str *rname, str *cname, int ival);
... ...
@@ -131,6 +132,7 @@ sr_xavp_t *xavu_set_sval(str *rname, str *sval);
131 132
 sr_xavp_t *xavu_set_child_xval(str *rname, str *cname, sr_xval_t *xval);
132 133
 sr_xavp_t *xavu_set_child_ival(str *rname, str *cname, int ival);
133 134
 sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval);
135
+int xavu_serialize_fields(str *rname, char *obuf, int olen);
134 136
 
135 137
 /** xavi api */
136 138
 int xavi_init_head(void);