aqhome-react: extended reading of network files.

network files can now extend a template network making it much easier
to create new networks based on existing one.
This commit is contained in:
Martin Preuss
2024-04-13 00:03:08 +02:00
parent a94ebeca29
commit a7267c061a

View File

@@ -37,6 +37,8 @@ static AQHREACT_UNIT_NET_LIST *_readUnitNetFiles(AQHOME_REACT *aqh, const GWEN_S
static int _readAllNetworksFromFileIntoList(AQHOME_REACT *aqh, const char *sFilename, AQHREACT_UNIT_NET_LIST *unitNetList);
static GWEN_XMLNODE *_readNetworkFromDatadirIntoXml(AQHOME_REACT *aqh, const char *networkName);
static AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static AQHREACT_UNIT_NET *_readUnitNetWithTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static AQHREACT_UNIT_NET *_readUnitNetNoTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static GWEN_XMLNODE *_readUnitNetFileToXml(AQHOME_REACT *aqh, const char *sFilename);
static void _readNetParamDefsWithList(AQHREACT_PARAM_LIST *paramList, GWEN_XMLNODE *unitNetNode);
static int _readUnits(AQHOME_REACT *aqh, AQHREACT_UNIT_NET *unitNet, GWEN_XMLNODE *unitNetNode, GWEN_DB_NODE *dbNetParams);
@@ -239,33 +241,49 @@ AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetN
{
AQHREACT_UNIT_NET *unitNet;
const char *s;
s=GWEN_XMLNode_GetProperty(unitNetNode, "type", NULL);
if (s && *s)
unitNet=_readUnitNetWithTemplate(aqh, unitNetNode);
else
unitNet=_readUnitNetNoTemplate(aqh, unitNetNode);
if (unitNet==NULL) {
DBG_INFO(NULL, "Error reading unit");
return NULL;
}
return unitNet;
}
AQHREACT_UNIT_NET *_readUnitNetWithTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetXml)
{
AQHREACT_UNIT_NET *unitNet;
GWEN_XMLNODE *templateNetXml;
const char *s;
int rv;
GWEN_DB_NODE *dbNetParams=NULL;
unitNet=AQHREACT_UnitNet_new();
s=GWEN_XMLNode_GetProperty(unitNetNode, "id", NULL);
s=GWEN_XMLNode_GetProperty(unitNetXml, "id", NULL);
AQHREACT_UnitNet_SetName(unitNet, s);
s=GWEN_XMLNode_GetProperty(unitNetNode, "type", NULL);
if (s && *s) {
GWEN_XMLNODE *baseNetXml;
/* uses a template file, load that and only set params from non-template file */
s=GWEN_XMLNode_GetProperty(unitNetXml, "type", NULL);
DBG_INFO(NULL, "Loading base network \"%s\"", s);
baseNetXml=_readNetworkFromDatadirIntoXml(aqh, s);
if (baseNetXml==NULL) {
templateNetXml=_readNetworkFromDatadirIntoXml(aqh, s);
if (templateNetXml==NULL) {
DBG_ERROR(NULL, "Base network \"%s\" not available (error or missing)", s);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), baseNetXml);
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), templateNetXml);
/* also read netParams from this file (after reading from template) */
rv=_readParamValuesForList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetNode, NULL);
rv=_readParamValuesForList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetXml, NULL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
@@ -275,31 +293,69 @@ AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetN
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
rv=_readUnits(aqh, unitNet, baseNetXml, dbNetParams);
/* first read units from this file (if any) */
rv=_readUnits(aqh, unitNet, unitNetXml, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
rv=_readLinks(aqh, unitNet, baseNetXml);
/* then read units from template file */
rv=_readUnits(aqh, unitNet, templateNetXml, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
/* read links from template file */
rv=_readLinks(aqh, unitNet, templateNetXml);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
GWEN_XMLNode_free(baseNetXml);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
else {
/* read additional links from this file (if any) */
rv=_readLinks(aqh, unitNet, unitNetXml);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
GWEN_DB_Group_free(dbNetParams);
GWEN_XMLNode_free(templateNetXml);
return unitNet;
}
AQHREACT_UNIT_NET *_readUnitNetNoTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode)
{
AQHREACT_UNIT_NET *unitNet;
const char *s;
int rv;
GWEN_DB_NODE *dbNetParams=NULL;
unitNet=AQHREACT_UnitNet_new();
s=GWEN_XMLNode_GetProperty(unitNetNode, "id", NULL);
AQHREACT_UnitNet_SetName(unitNet, s);
/* just directly read network from given XML node (no need to load a template file) */
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetNode);
@@ -328,7 +384,6 @@ AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetN
return NULL;
}
GWEN_DB_Group_free(dbNetParams);
}
return unitNet;
}