vars: added more functions.

This commit is contained in:
Martin Preuss
2024-05-11 01:07:10 +02:00
parent 36e9909060
commit 516ac4e34e
2 changed files with 110 additions and 7 deletions

View File

@@ -31,6 +31,7 @@ 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 *_getValueNodeByIdx(const AQH_VARS *vt, int idx);
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);
@@ -290,19 +291,13 @@ const char *AQH_Vars_GetCharValue(AQH_VARS *vt, const char *path, int idx, const
if (vt && idx>=0) {
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);
}
vtValue=_getValueNodeByIdx(vtChild, idx);
if (vtValue)
return AQH_Vars_GetStringData(vtValue, defaultValue);
}
@@ -311,6 +306,106 @@ const char *AQH_Vars_GetCharValue(AQH_VARS *vt, const char *path, int idx, const
int AQH_Vars_SetIntValue(AQH_VARS *vt, uint32_t flags, const char *path, int 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_CreateIntValue(value);
AQH_Vars_Tree2_AddChild(vtChild, vtValue);
return 0;
}
return GWEN_ERROR_INVALID;
}
int AQH_Vars_GetIntValue(AQH_VARS *vt, const char *path, int idx, int defaultValue)
{
if (vt && idx>=0) {
AQH_VARS *vtChild;
AQH_VARS *vtValue;
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=_getValueNodeByIdx(vtChild, idx);
if (vtValue)
return AQH_Vars_GetIntData(vtValue, defaultValue);
}
return defaultValue;
}
int AQH_Vars_SetDoubleValue(AQH_VARS *vt, uint32_t flags, const char *path, double 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_CreateDoubleValue(value);
AQH_Vars_Tree2_AddChild(vtChild, vtValue);
return 0;
}
return GWEN_ERROR_INVALID;
}
double AQH_Vars_GetDoubleValue(AQH_VARS *vt, const char *path, int idx, double defaultValue)
{
if (vt && idx>=0) {
AQH_VARS *vtChild;
AQH_VARS *vtValue;
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=_getValueNodeByIdx(vtChild, idx);
if (vtValue)
return AQH_Vars_GetDoubleData(vtValue, defaultValue);
}
return defaultValue;
}
AQH_VARS *_getValueNodeByIdx(const AQH_VARS *vt, int idx)
{
if (vt && idx>=0) {
AQH_VARS *vtValue;
vtValue=AQH_Vars_Tree2_GetFirstChild(vt);
while(vtValue && idx) {
idx--;
vtValue=AQH_Vars_Tree2_GetNext(vtValue);
}
return vtValue;
}
return NULL;
}
AQH_VARS *_getPath(AQH_VARS *vt, const char *s, uint32_t flags)
{
return (AQH_VARS *)AQH_Path_Handle(s, (void*) vt, flags, "/", _pathHandlerFn);