aqhome: more work on path and vars modules.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
|
||||
|
||||
#include "./vars_p.h"
|
||||
#include "aqhome/data/path.h"
|
||||
|
||||
#include <gwenhywfar/misc.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
@@ -30,6 +31,9 @@ GWEN_TREE2_FUNCTIONS(AQH_VARS, AQH_Vars);
|
||||
static AQH_VARS *_newData(void);
|
||||
static void _releaseData(AQH_VARS *vt);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user