From 2c584bbff9720d4bd36da1d9f1dfea4cbca65c78 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 9 May 2024 14:56:46 +0200 Subject: [PATCH] vars: use const in most api functions The idea of not using const was to reduce copy operations. However, it is not very intuitive to know when and which arguments are const so to simplify working with this new module and make it as close as possible to GWEN_DB we use const now as in GWEN_DB. At least AQH_Vars_SetStringData() still doesn't use const so if the need arises to avoid copying we can. --- aqhome/data/vars.c | 19 +++++++------------ aqhome/data/vars.h | 6 +++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/aqhome/data/vars.c b/aqhome/data/vars.c index 7e5ccef..f00f144 100644 --- a/aqhome/data/vars.c +++ b/aqhome/data/vars.c @@ -97,16 +97,16 @@ void AQH_Vars_free(AQH_VARS *vt) -AQH_VARS *AQH_Vars_CreateGroup(char *s) +AQH_VARS *AQH_Vars_CreateGroup(const char *s) { - return _newStringElement(s, AQH_Vars_DataType_Group); + return _newStringElement(s?strdup(s):NULL, AQH_Vars_DataType_Group); } -AQH_VARS *AQH_Vars_CreateVariable(char *s) +AQH_VARS *AQH_Vars_CreateVariable(const char *s) { - return _newStringElement(s, AQH_Vars_DataType_Variable); + return _newStringElement(s?strdup(s):NULL, AQH_Vars_DataType_Variable); } @@ -265,7 +265,7 @@ AQH_VARS *AQH_Vars_GetNextByType(const AQH_VARS *vt, AQH_VARS_DATATYPE dt) -int AQH_Vars_SetCharValue(AQH_VARS *vt, uint32_t flags, const char *path, char *value) +int AQH_Vars_SetCharValue(AQH_VARS *vt, uint32_t flags, const char *path, const char *value) { if (vt) { AQH_VARS *vtChild; @@ -276,7 +276,7 @@ int AQH_Vars_SetCharValue(AQH_VARS *vt, uint32_t flags, const char *path, char * DBG_ERROR(AQH_LOGDOMAIN, "Error getting path for var \"%s\"", path); return GWEN_ERROR_GENERIC; } - vtValue=AQH_Vars_CreateStringValue(value); + vtValue=AQH_Vars_CreateStringValue(value?strdup(value):NULL); AQH_Vars_Tree2_AddChild(vtChild, vtValue); return 0; } @@ -397,10 +397,5 @@ AQH_VARS *_getNodeByTypeAndName(const AQH_VARS *vt, AQH_VARS_DATATYPE dt, const - - - - - - +#include "./vars-t.c" diff --git a/aqhome/data/vars.h b/aqhome/data/vars.h index 0b50786..21d8036 100644 --- a/aqhome/data/vars.h +++ b/aqhome/data/vars.h @@ -33,8 +33,8 @@ typedef struct AQH_VARS AQH_VARS; GWEN_TREE2_FUNCTION_DEFS(AQH_VARS, AQH_Vars); -AQH_VARS *AQH_Vars_CreateGroup(char *s); -AQH_VARS *AQH_Vars_CreateVariable(char *s); +AQH_VARS *AQH_Vars_CreateGroup(const char *s); +AQH_VARS *AQH_Vars_CreateVariable(const char *s); AQH_VARS *AQH_Vars_CreateStringValue(char *s); AQH_VARS *AQH_Vars_CreateIntValue(int d); AQH_VARS *AQH_Vars_CreateDoubleValue(double d); @@ -59,7 +59,7 @@ AQH_VARS *AQH_Vars_GetFirstChildByType(const AQH_VARS *vt, AQH_VARS_DATATYPE dt) AQH_VARS *AQH_Vars_GetNextByType(const AQH_VARS *vt, AQH_VARS_DATATYPE dt); -int AQH_Vars_SetCharValue(AQH_VARS *vt, uint32_t flags, const char *path, char *value); +int AQH_Vars_SetCharValue(AQH_VARS *vt, uint32_t flags, const char *path, const char *value); const char *AQH_Vars_GetCharValue(AQH_VARS *vt, const char *path, int idx, const char *defaultValue);