Browse code

core: cfg_get_default_value_by_name function has been implemented

- As cfg_get_by_name function uses handle to reach variable values,
this function will use instead orig_handle in order to reach
the initial values when the config group was registered. Read-only
variables will not be returned since cfg_reset function uses this function
in order to set back the default values, and this cannot be implemented on read-only variables.

Huseyin Dikme authored on 04/07/2017 12:10:38
Showing 2 changed files
... ...
@@ -1359,6 +1359,74 @@ int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str
1359 1359
 	return 0;
1360 1360
 }
1361 1361
 
1362
+/* retrieves the default value of a variable
1363
+ * Return value:
1364
+ *  0 - success
1365
+ * -1 - error
1366
+ *  1 - variable exists, but it is not readable
1367
+ */
1368
+int cfg_get_default_value_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
1369
+                       void **val, unsigned int *val_type)
1370
+{
1371
+	cfg_group_t	*group;
1372
+	cfg_mapping_t	*var;
1373
+	void	*p;
1374
+	static str	s;	/* we need the value even
1375
+				after the function returns */
1376
+	cfg_group_inst_t	*group_inst;
1377
+
1378
+	/* verify the context even if we do not need it now
1379
+	to make sure that a cfg driver has called the function
1380
+	(very very weak security) */
1381
+	if (!ctx) {
1382
+		LOG(L_ERR, "ERROR: cfg_get_by_name(): context is undefined\n");
1383
+		return -1;
1384
+	}
1385
+
1386
+	/* look-up the group and the variable */
1387
+	if (cfg_lookup_var(group_name, var_name, &group, &var))
1388
+		return -1;
1389
+	else
1390
+	{
1391
+		if(var->def->type & CFG_READONLY)  /* if variables exist then prevents resetting the read-only ones */
1392
+			return -1;
1393
+	}
1394
+
1395
+	if (var->def->on_change_cb) {
1396
+		/* The variable cannot be retrieved, because the fixup
1397
+		function may have changed it, and it is better to return
1398
+		an error than an incorrect value */
1399
+		return 1;
1400
+	}
1401
+
1402
+	/* use the module's orig_handle to access the default registered value of the variable for any group*/
1403
+	p = (group->orig_handle) + var->offset;
1404
+
1405
+	switch (CFG_VAR_TYPE(var)) {
1406
+		case CFG_VAR_INT:
1407
+			*val = (void *)(long)*(int *)p;
1408
+			break;
1409
+
1410
+		case CFG_VAR_STRING:
1411
+			*val = (void *)*(char **)p;
1412
+			break;
1413
+
1414
+		case CFG_VAR_STR:
1415
+			memcpy(&s, p, sizeof(str));
1416
+			*val = (void *)&s;
1417
+			break;
1418
+
1419
+		case CFG_VAR_POINTER:
1420
+			*val = *(void **)p;
1421
+			break;
1422
+
1423
+	}
1424
+	*val_type = CFG_VAR_TYPE(var);
1425
+
1426
+	return 0;
1427
+}
1428
+
1429
+
1362 1430
 /* returns the description of a variable */
1363 1431
 int cfg_help(cfg_ctx_t *ctx, str *group_name, str *var_name,
1364 1432
 			char **ch, unsigned int *input_type)
... ...
@@ -119,6 +119,9 @@ int cfg_rollback(cfg_ctx_t *ctx);
119 119
 int cfg_get_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
120 120
 			void **val, unsigned int *val_type);
121 121
 
122
+int cfg_get_default_value_by_name(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *var_name,
123
+			void **val, unsigned int *val_type);
124
+
122 125
 /*! \brief returns the description of a variable */
123 126
 int cfg_help(cfg_ctx_t *ctx, str *group_name, str *var_name,
124 127
 			char **ch, unsigned int *input_type);