refactor construction list.

This commit is contained in:
Enno Rehling 2018-05-01 10:53:12 +02:00
parent bddf4bff39
commit dddbf5287a

View file

@ -214,15 +214,11 @@ xml_readrequirements(xmlNodePtr * nodeTab, int nodeNr, requirement ** reqArray)
}
}
static void
xml_readconstruction(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
construction **consPtr, bool is_building)
static construction *
xml_readconstruction(xmlXPathContextPtr xpath, xmlNodePtr node, bool is_building)
{
int k;
for (k = 0; k != nodeSet->nodeNr; ++k) {
xmlNodePtr node = nodeSet->nodeTab[k];
xmlChar *propValue;
construction *con;
xmlChar *propValue;
xmlXPathObjectPtr req;
skill_t sk = NOSKILL;
@ -232,15 +228,12 @@ xml_readconstruction(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
if (sk == NOSKILL) {
log_error("construction requires skill '%s' that does not exist.\n", (const char *)propValue);
xmlFree(propValue);
continue;
return NULL;
}
xmlFree(propValue);
}
assert(*consPtr == NULL);
*consPtr = con = (construction *)calloc(sizeof(construction), 1);
consPtr = &con->improvement;
con = (construction *)calloc(sizeof(construction), 1);
con->skill = sk;
con->maxsize = xml_ivalue(node, "maxsize", -1);
@ -261,6 +254,23 @@ xml_readconstruction(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
xml_readrequirements(req->nodesetval->nodeTab,
req->nodesetval->nodeNr, &con->materials);
xmlXPathFreeObject(req);
return con;
}
static void
xml_readconstructions(xmlXPathContextPtr xpath, xmlNodeSetPtr nodeSet,
construction **consPtr, bool is_building)
{
int k;
for (k = 0; k != nodeSet->nodeNr; ++k) {
xmlNodePtr node = nodeSet->nodeTab[k];
construction *con = xml_readconstruction(xpath, node, is_building);
if (con) {
*consPtr = con;
consPtr = &con->improvement;
}
}
}
@ -339,7 +349,7 @@ static int parse_buildings(xmlDocPtr doc)
/* reading eressea/buildings/building/construction */
xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "construction", xpath);
xml_readconstruction(xpath, result->nodesetval, &btype->construction, true);
xml_readconstructions(xpath, result->nodesetval, &btype->construction, true);
xmlXPathFreeObject(result);
/* reading eressea/buildings/building/function */
@ -439,7 +449,7 @@ static int parse_ships(xmlDocPtr doc)
/* reading eressea/ships/ship/construction */
xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "construction", xpath);
xml_readconstruction(xpath, result->nodesetval, &st->construction, false);
xml_readconstructions(xpath, result->nodesetval, &st->construction, false);
xmlXPathFreeObject(result);
for (child = node->children; child; child = child->next) {
@ -685,7 +695,7 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype)
/* reading item/construction */
xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "construction", xpath);
xml_readconstruction(xpath, result->nodesetval, &itype->construction, false);
xml_readconstructions(xpath, result->nodesetval, &itype->construction, false);
xmlXPathFreeObject(result);
/* reading item/weapon */