aqhome: more work on path and vars modules.

This commit is contained in:
Martin Preuss
2024-05-09 00:49:57 +02:00
parent 3e5bff90d1
commit b473d62cdc
3 changed files with 150 additions and 11 deletions

View File

@@ -14,18 +14,16 @@
#include <aqhome/api.h> #include <aqhome/api.h>
#define AQH_PATH_FLAGS_PATHMUSTEXIST 0x00000001 #define AQH_PATH_FLAGS_PATHMUSTEXIST 0x00100000
#define AQH_PATH_FLAGS_PATHMUSTNOTEXIST 0x00000002 #define AQH_PATH_FLAGS_PATHMUSTNOTEXIST 0x00200000
#define AQH_PATH_FLAGS_PATHCREATE 0x00000004 #define AQH_PATH_FLAGS_PATHCREATE 0x00400000
#define AQH_PATH_FLAGS_NAMEMUSTEXIST 0x00000008 #define AQH_PATH_FLAGS_LASTMUSTNOTEXIST 0x00800000
#define AQH_PATH_FLAGS_NAMEMUSTNOTEXIST 0x00000010 #define AQH_PATH_FLAGS_LASTCREATE 0x01000000
#define AQH_PATH_FLAGS_CREATE_GROUP 0x00000020 #define AQH_PATH_FLAGS_VARIABLE 0x02000000
#define AQH_PATH_FLAGS_CREATE_VAR 0x00000040 #define AQH_PATH_FLAGS_PARSEIDX 0x04000000
#define AQH_PATH_FLAGS_VARIABLE 0x00000080
#define AQH_PATH_FLAGS_PARSEIDX 0x00000100
#define AQH_PATH_FLAGS_ROOT 0x10000000 #define AQH_PATH_FLAGS_ROOT 0x40000000
#define AQH_PATH_FLAGS_LAST 0x20000000 #define AQH_PATH_FLAGS_LAST 0x80000000

View File

@@ -13,6 +13,7 @@
#include "./vars_p.h" #include "./vars_p.h"
#include "aqhome/data/path.h"
#include <gwenhywfar/misc.h> #include <gwenhywfar/misc.h>
#include <gwenhywfar/debug.h> #include <gwenhywfar/debug.h>
@@ -30,6 +31,9 @@ GWEN_TREE2_FUNCTIONS(AQH_VARS, AQH_Vars);
static AQH_VARS *_newData(void); static AQH_VARS *_newData(void);
static void _releaseData(AQH_VARS *vt); static void _releaseData(AQH_VARS *vt);
static AQH_VARS *_newStringElement(char *s, AQH_VARS_DATATYPE dt); static AQH_VARS *_newStringElement(char *s, AQH_VARS_DATATYPE dt);
static AQH_VARS *_getPath(AQH_VARS *vt, const char *s, uint32_t flags);
static void *_pathHandlerFn(const char *entry, void *data, int idx, uint32_t flags);
static AQH_VARS *_getNodeByTypeAndName(const AQH_VARS *vt, AQH_VARS_DATATYPE dt, const char *name, int idx);
@@ -261,6 +265,135 @@ 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)
{
if (vt) {
AQH_VARS *vtChild;
AQH_VARS *vtValue;
vtChild=_getPath(vt, path, flags | AQH_PATH_FLAGS_VARIABLE);
if (vtChild==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Error getting path for var \"%s\"", path);
return GWEN_ERROR_GENERIC;
}
vtValue=AQH_Vars_CreateStringValue(value);
AQH_Vars_Tree2_AddChild(vtChild, vtValue);
return 0;
}
return GWEN_ERROR_INVALID;
}
const char *AQH_Vars_GetCharValue(AQH_VARS *vt, const char *path, int idx, const char *defaultValue)
{
if (vt) {
AQH_VARS *vtChild;
AQH_VARS *vtValue;
int i;
vtChild=_getPath(vt, path, AQH_PATH_FLAGS_PATHMUSTEXIST | AQH_PATH_FLAGS_VARIABLE);
if (vtChild==NULL) {
DBG_INFO(AQH_LOGDOMAIN, "Path for var \"%s\" not found", path);
return defaultValue;
}
vtValue=AQH_Vars_Tree2_GetFirstChild(vtChild);
i=idx;
while(vtValue && i) {
i--;
vtValue=AQH_Vars_Tree2_GetNext(vtValue);
}
if (vtValue)
return AQH_Vars_GetStringData(vtValue, defaultValue);
}
return defaultValue;
}
AQH_VARS *_getPath(AQH_VARS *vt, const char *s, uint32_t flags)
{
return (AQH_VARS *)AQH_Path_Handle(s, (void*) vt, flags, "/", _pathHandlerFn);
}
void *_pathHandlerFn(const char *entry, void *data, int idx, uint32_t flags)
{
AQH_VARS *vt;
AQH_VARS *vtChild;
AQH_VARS_DATATYPE dt;
vt=(AQH_VARS*) data;
dt=((flags & AQH_PATH_FLAGS_LAST) && (flags & AQH_PATH_FLAGS_VARIABLE))?AQH_Vars_DataType_Variable:AQH_Vars_DataType_Group;
if (
!(
(flags & AQH_PATH_FLAGS_PATHCREATE) ||
((flags & AQH_PATH_FLAGS_LAST) && (flags & AQH_PATH_FLAGS_LASTCREATE))
)
) {
/* look it up */
vtChild=_getNodeByTypeAndName(vt, dt, entry, idx);
if (vtChild) {
/* already exists, should it? */
if (
(flags & AQH_PATH_FLAGS_PATHMUSTNOTEXIST) ||
((flags & AQH_PATH_FLAGS_LAST) && (flags & AQH_PATH_FLAGS_LASTMUSTNOTEXIST))
) {
DBG_INFO(AQH_LOGDOMAIN, "Element \"%s[%d]\" exists when it should not", entry, idx);
return NULL;
}
/* already exists, should we overwrite it (i.e. remove children)? */
if (
(flags & AQH_PATH_FLAGS_LAST) &&
(
((dt==AQH_Vars_DataType_Group) && (flags & AQH_VARS_PATHFLAGS_OVERWRITE_GROUPS)) ||
((dt==AQH_Vars_DataType_Variable) && (flags & AQH_VARS_PATHFLAGS_OVERWRITE_VARS))
)
)
AQH_Vars_Tree2_ClearChildren(vtChild);
return vtChild;
}
else {
/* does not exist, should it? */
if (flags & AQH_PATH_FLAGS_PATHMUSTEXIST) {
DBG_INFO(AQH_LOGDOMAIN, "Element \"%s[%d]\" does not exist when it should", entry, idx);
return NULL;
}
}
}
/* create new string element (either group or var) */
vtChild=_newStringElement(strdup(entry), dt);
AQH_Vars_Tree2_AddChild(vt, vtChild);
return vtChild;
}
AQH_VARS *_getNodeByTypeAndName(const AQH_VARS *vt, AQH_VARS_DATATYPE dt, const char *name, int idx)
{
AQH_VARS *vtChild=NULL;
if (idx>=0) {
/* look it up */
vtChild=AQH_Vars_GetFirstChildByType(vt, dt);
while(vtChild) {
if (vtChild->data.dataString && strcasecmp(vtChild->data.dataString, name)!=-1) {
if (idx==0)
break;
else
idx--;
}
vtChild=AQH_Vars_GetNextByType(vtChild, dt);
}
}
return vtChild;
}

View File

@@ -13,6 +13,10 @@
#include <gwenhywfar/tree2.h> #include <gwenhywfar/tree2.h>
#define AQH_VARS_PATHFLAGS_OVERWRITE_VARS 0x0001
#define AQH_VARS_PATHFLAGS_OVERWRITE_GROUPS 0x0002
typedef enum { typedef enum {
AQH_Vars_DataType_Unknown=0, AQH_Vars_DataType_Unknown=0,
@@ -55,6 +59,10 @@ 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); 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);
const char *AQH_Vars_GetCharValue(AQH_VARS *vt, const char *path, int idx, const char *defaultValue);