Browse code

core: xavu api - like xavp but with single value items

- simplified version without indexes

Daniel-Constantin Mierla authored on 01/04/2020 08:39:24
Showing 2 changed files
... ...
@@ -34,6 +34,11 @@ static sr_xavp_t *_xavp_list_head = 0;
34 34
 /*! Pointer to XAVP current list */
35 35
 static sr_xavp_t **_xavp_list_crt = &_xavp_list_head;
36 36
 
37
+/*! XAVU list head */
38
+static sr_xavp_t *_xavu_list_head = 0;
39
+/*! Pointer to XAVP current list */
40
+static sr_xavp_t **_xavu_list_crt = &_xavu_list_head;
41
+
37 42
 /*! Helper functions */
38 43
 static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv);
39 44
 static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx);
... ...
@@ -1077,3 +1082,328 @@ int xavp_serialize_fields(str *rname, char *obuf, int olen)
1077 1082
 	}
1078 1083
 	return rlen;
1079 1084
 }
1085
+
1086
+/**
1087
+ *
1088
+ */
1089
+/*** XAVU - eXtended Attribute Value Unique pair - implementation ***/
1090
+
1091
+
1092
+/**
1093
+ *
1094
+ */
1095
+void xavu_reset_list(void)
1096
+{
1097
+	assert(_xavu_list_crt!=0 );
1098
+
1099
+	if (_xavu_list_crt!=&_xavu_list_head)
1100
+		_xavu_list_crt=&_xavu_list_head;
1101
+	xavp_destroy_list(_xavu_list_crt);
1102
+}
1103
+
1104
+/**
1105
+ *
1106
+ */
1107
+sr_xavp_t **xavu_set_list(sr_xavp_t **head)
1108
+{
1109
+	sr_xavp_t **avu;
1110
+
1111
+	assert(_xavu_list_crt!=0);
1112
+
1113
+	avu = _xavu_list_crt;
1114
+	_xavu_list_crt = head;
1115
+	return avu;
1116
+}
1117
+
1118
+/**
1119
+ *
1120
+ */
1121
+sr_xavp_t **xavu_get_crt_list(void)
1122
+{
1123
+	assert(_xavu_list_crt!=0);
1124
+	return _xavu_list_crt;
1125
+}
1126
+
1127
+/**
1128
+ *
1129
+ */
1130
+static sr_xavp_t *xavu_get_internal(str *name, sr_xavp_t **list, sr_xavp_t **prv)
1131
+{
1132
+	sr_xavp_t *avu;
1133
+	unsigned int id;
1134
+
1135
+	if(name==NULL || name->s==NULL) {
1136
+		return NULL;
1137
+	}
1138
+
1139
+	id = get_hash1_raw(name->s, name->len);
1140
+
1141
+	if(list && *list) {
1142
+		avu = *list;
1143
+	} else {
1144
+		avu = *_xavu_list_crt;
1145
+	}
1146
+	while(avu) {
1147
+		if(avu->id==id && avu->name.len==name->len
1148
+				&& strncmp(avu->name.s, name->s, name->len)==0) {
1149
+			return avu;
1150
+		}
1151
+		if(prv) {
1152
+			*prv = avu;
1153
+		}
1154
+		avu = avu->next;
1155
+	}
1156
+	return NULL;
1157
+}
1158
+
1159
+/**
1160
+ *
1161
+ */
1162
+sr_xavp_t *xavu_get(str *name, sr_xavp_t *start)
1163
+{
1164
+	return xavu_get_internal(name, (start)?&start:NULL, NULL);
1165
+}
1166
+
1167
+sr_xavp_t *xavp_lookup(str *name, sr_xavp_t **start)
1168
+{
1169
+	return xavu_get_internal(name, start, NULL);
1170
+}
1171
+
1172
+/**
1173
+ *
1174
+ */
1175
+int xavu_rm(sr_xavp_t *xa, sr_xavp_t **head)
1176
+{
1177
+	sr_xavp_t *avu;
1178
+	sr_xavp_t *prv=0;
1179
+
1180
+	if(head!=NULL)
1181
+		avu = *head;
1182
+	else
1183
+		avu=*_xavu_list_crt;
1184
+
1185
+	while(avu) {
1186
+		if(avu==xa) {
1187
+			if(prv) {
1188
+				prv->next=avu->next;
1189
+			} else if(head!=NULL) {
1190
+				*head = avu->next;
1191
+			} else {
1192
+				*_xavu_list_crt = avu->next;
1193
+			}
1194
+			xavp_free(avu);
1195
+			return 1;
1196
+		}
1197
+		prv=avu; avu=avu->next;
1198
+	}
1199
+	return 0;
1200
+}
1201
+
1202
+/**
1203
+ *
1204
+ */
1205
+int xavu_rm_by_name(str *name, sr_xavp_t **head)
1206
+{
1207
+	sr_xavp_t *avu;
1208
+	sr_xavp_t *foo;
1209
+	sr_xavp_t *prv=0;
1210
+	unsigned int id;
1211
+
1212
+
1213
+	if(name==NULL || name->s==NULL) {
1214
+		return -1;
1215
+	}
1216
+
1217
+	id = get_hash1_raw(name->s, name->len);
1218
+	if(head!=NULL) {
1219
+		avu = *head;
1220
+	} else {
1221
+		avu = *_xavu_list_crt;
1222
+	}
1223
+	while(avu) {
1224
+		foo = avu;
1225
+		avu = avu->next;
1226
+		if(foo->id==id && foo->name.len==name->len
1227
+				&& strncmp(foo->name.s, name->s, name->len)==0) {
1228
+			if(prv!=NULL) {
1229
+				prv->next=foo->next;
1230
+			} else if(head!=NULL) {
1231
+				*head = foo->next;
1232
+			} else {
1233
+				*_xavu_list_crt = foo->next;
1234
+			}
1235
+			xavp_free(foo);
1236
+		} else {
1237
+			prv = foo;
1238
+		}
1239
+	}
1240
+	return 0;
1241
+}
1242
+
1243
+/**
1244
+ *
1245
+ */
1246
+sr_xavp_t *xavu_set_xval(str *name, sr_xval_t *val, sr_xavp_t **list)
1247
+{
1248
+	sr_xavp_t *avu;
1249
+	sr_xavp_t *crt;
1250
+	sr_xavp_t *prv=0;
1251
+
1252
+	if(val==NULL) {
1253
+		return NULL;
1254
+	}
1255
+
1256
+	avu = xavp_new_value(name, val);
1257
+	if (avu==NULL) {
1258
+		return NULL;
1259
+	}
1260
+
1261
+	/* find the current value */
1262
+	crt = xavu_get_internal(name, list, &prv);
1263
+	if(crt==NULL) {
1264
+		/* add a new one in the list */
1265
+		avu->next = *_xavu_list_crt;
1266
+		*_xavu_list_crt = avu;
1267
+		return avu;
1268
+	}
1269
+
1270
+	/* replace the current value with the new */
1271
+	avu->next = crt->next;
1272
+	if(prv) {
1273
+		prv->next = avu;
1274
+	} else if(list) {
1275
+		*list = avu;
1276
+	} else {
1277
+		*_xavu_list_crt = avu;
1278
+	}
1279
+
1280
+	xavp_free(crt);
1281
+
1282
+	return avu;
1283
+}
1284
+
1285
+/**
1286
+ *
1287
+ */
1288
+sr_xavp_t *xavu_set_ival(str *rname, int ival)
1289
+{
1290
+	sr_xval_t xval;
1291
+
1292
+	memset(&xval, 0, sizeof(sr_xval_t));
1293
+	xval.type = SR_XTYPE_INT;
1294
+	xval.v.i = ival;
1295
+
1296
+	return xavu_set_xval(rname, &xval, NULL);
1297
+}
1298
+
1299
+/**
1300
+ *
1301
+ */
1302
+sr_xavp_t *xavu_set_sval(str *rname, str *sval)
1303
+{
1304
+	sr_xval_t xval;
1305
+
1306
+	memset(&xval, 0, sizeof(sr_xval_t));
1307
+	xval.type = SR_XTYPE_STR;
1308
+	xval.v.s = *sval;
1309
+
1310
+	return xavu_set_xval(rname, &xval, NULL);
1311
+}
1312
+
1313
+/**
1314
+ *
1315
+ */
1316
+sr_xavp_t *xavu_set_xavu_value(str *rname, str *name, sr_xval_t *val, sr_xavp_t **list)
1317
+{
1318
+	sr_xavp_t *ravu=0;
1319
+	sr_xavp_t *cavu=0;
1320
+	sr_xval_t rval;
1321
+
1322
+	cavu = xavp_new_value(name, val);
1323
+	if (cavu==NULL) {
1324
+		return NULL;
1325
+	}
1326
+
1327
+	memset(&rval, 0, sizeof(sr_xval_t));
1328
+	rval.type = SR_XTYPE_XAVP;
1329
+	rval.v.xavp = cavu;
1330
+
1331
+	ravu = xavp_new_value(rname, &rval);
1332
+	if (ravu==NULL) {
1333
+		xavp_destroy_list(&cavu);
1334
+		return NULL;
1335
+	}
1336
+
1337
+	/* Prepend new value to the list */
1338
+	if(list) {
1339
+		ravu->next = *list;
1340
+		*list = ravu;
1341
+	} else {
1342
+		ravu->next = *_xavu_list_crt;
1343
+		*_xavu_list_crt = ravu;
1344
+	}
1345
+
1346
+	return ravu;
1347
+}
1348
+
1349
+/**
1350
+ * Set the value of the  xavu rname with child xavu cname
1351
+ * - set if it exits; add if it doesn't exist
1352
+ * - config operations:
1353
+ *   $xavu(rxname=>cname) = xval;
1354
+ */
1355
+sr_xavp_t *xavu_set_child_xval(str *rname, str *cname, sr_xval_t *xval)
1356
+{
1357
+	sr_xavp_t *ravu=NULL;
1358
+	sr_xavp_t *cavu=NULL;
1359
+
1360
+	ravu = xavu_get(rname, NULL);
1361
+	if(ravu) {
1362
+		if(ravu->val.type != SR_XTYPE_XAVP) {
1363
+			/* first root xavp does not have xavp list value - remove it */
1364
+			xavp_rm(ravu, NULL);
1365
+			/* add a new xavp in the root list with a child */
1366
+			return xavu_set_xavu_value(rname, cname, xval, NULL);
1367
+		} else {
1368
+			/* first root xavp has an xavp list value */
1369
+			cavu = xavu_get(cname, ravu->val.v.xavp);
1370
+			if(cavu) {
1371
+				/* child xavp with same name - remove it */
1372
+				/* todo: update in place for int or if allocated size fits */
1373
+				xavp_rm(cavu, &ravu->val.v.xavp);
1374
+			}
1375
+			return xavp_add_value(cname, xval, &ravu->val.v.xavp);
1376
+		}
1377
+	} else {
1378
+		/* no xavp with rname in root list found */
1379
+		return xavu_set_xavu_value(rname, cname, xval, NULL);
1380
+	}
1381
+}
1382
+
1383
+/**
1384
+ *
1385
+ */
1386
+sr_xavp_t *xavu_set_child_ival(str *rname, str *cname, int ival)
1387
+{
1388
+	sr_xval_t xval;
1389
+
1390
+	memset(&xval, 0, sizeof(sr_xval_t));
1391
+	xval.type = SR_XTYPE_INT;
1392
+	xval.v.i = ival;
1393
+
1394
+	return xavu_set_child_xval(rname, cname, &xval);
1395
+}
1396
+
1397
+/**
1398
+ *
1399
+ */
1400
+sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval)
1401
+{
1402
+	sr_xval_t xval;
1403
+
1404
+	memset(&xval, 0, sizeof(sr_xval_t));
1405
+	xval.type = SR_XTYPE_STR;
1406
+	xval.v.s = *sval;
1407
+
1408
+	return xavu_set_child_xval(rname, cname, &xval);
1409
+}
... ...
@@ -112,4 +112,21 @@ int xavp_serialize_fields(str *rname, char *obuf, int olen);
112 112
 int xavp_set_child_ival(str *rname, str *cname, int ival);
113 113
 int xavp_set_child_sval(str *rname, str *cname, str *sval);
114 114
 
115
+/** xavu api */
116
+#define xavu_destroy_list_unsafe xavp_destroy_list_unsafe
117
+#define xavu_destroy_list xavp_destroy_list
118
+void xavu_reset_list(void);
119
+sr_xavp_t **xavu_set_list(sr_xavp_t **head);
120
+sr_xavp_t **xavu_get_crt_list(void);
121
+sr_xavp_t *xavu_get(str *name, sr_xavp_t *start);
122
+sr_xavp_t *xavu_lookup(str *name, sr_xavp_t **start);
123
+int xavu_rm(sr_xavp_t *xa, sr_xavp_t **head);
124
+int xavu_rm_by_name(str *name, sr_xavp_t **head);
125
+sr_xavp_t *xavu_set_xval(str *name, sr_xval_t *val, sr_xavp_t **list);
126
+sr_xavp_t *xavu_set_ival(str *rname, int ival);
127
+sr_xavp_t *xavu_set_sval(str *rname, str *sval);
128
+sr_xavp_t *xavu_set_child_xval(str *rname, str *cname, sr_xval_t *xval);
129
+sr_xavp_t *xavu_set_child_ival(str *rname, str *cname, int ival);
130
+sr_xavp_t *xavu_set_child_sval(str *rname, str *cname, str *sval);
131
+
115 132
 #endif