diff --git a/src/faction.c b/src/faction.c index c7a7450..2d17f10 100644 --- a/src/faction.c +++ b/src/faction.c @@ -1,15 +1,12 @@ #include #include -#include #include "main.h" #include "log.h" #include "pack.h" +#include "xml.h" #include "faction.h" -#define XML_NODE_START 1 -#define XML_NODE_TEXT 3 - #define XML_FACTION_ID "Factions" // XML section id. #define XML_FACTION_TAG "faction" #define XML_ALLIANCE_ID "Alliances" diff --git a/src/outfit.c b/src/outfit.c index c893934..e733b75 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -1,15 +1,12 @@ -#include #include #include #include "main.h" #include "log.h" #include "pack.h" +#include "xml.h" #include "outfit.h" -#define XML_NODE_START 1 -#define XML_NODE_TEXT 3 - #define XML_OUTFIT_ID "Outfits" #define XML_OUTFIT_TAG "outfit" @@ -82,7 +79,7 @@ static void outfit_parseSWeapon(Outfit* tmp, const xmlNodePtr parent) { char str[PATH_MAX] = "\0"; - while((node = node->next)) { + do { // Load all the things. if(strcmp((char*)node->name, "speed")==0) tmp->speed = (double)atoi((char*)node->children->content); @@ -106,7 +103,7 @@ static void outfit_parseSWeapon(Outfit* tmp, const xmlNodePtr parent) { tmp->damage_shield = atof((char*)cur->children->content); } } - } + } while((node = node->next)); #define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name) MELEMENT(tmp->accuracy, "tech"); MELEMENT(tmp->delay, "delay"); @@ -128,8 +125,7 @@ static Outfit* outfit_parse(const xmlNodePtr parent) { if(tmp->name == NULL) WARN("Outfit in "OUTFIT_DATA" has invalid or no name"); node = parent->xmlChildrenNode; - - while((node = node->next)) { + do { // Load all the things. if(strcmp((char*)node->name, "general")==0) { cur = node->children; @@ -160,7 +156,7 @@ static Outfit* outfit_parse(const xmlNodePtr parent) { break; } } - } + } while((node = node->next)); #define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name) MELEMENT(tmp->max, "max"); MELEMENT(tmp->tech, "tech"); diff --git a/src/pilot.c b/src/pilot.c index a674544..a5b8e9e 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -3,17 +3,13 @@ #include #include // We don't need this? -#include - #include "main.h" #include "log.h" #include "weapon.h" #include "pack.h" +#include "xml.h" #include "pilot.h" -#define XML_NODE_START 1 -#define XML_NODE_TEXT 3 - #define XML_ID "Fleets" // XML section identifier. #define XML_FLEET "fleet" @@ -341,8 +337,8 @@ static Fleet* fleet_parse(const xmlNodePtr parent) { tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); // Already mallocs. if(tmp->name == NULL) WARN("Fleet in "FLEET_DATA" has invalid or no name"); - - while((node = node->next)) { + + do { // Load all the data. if(strcmp((char*)node->name, "faction")==0) tmp->faction = faction_get((char*)node->children->content); @@ -350,7 +346,7 @@ static Fleet* fleet_parse(const xmlNodePtr parent) { tmp->ai = ai_getProfile((char*)node->children->content); else if(strcmp((char*)node->name, "pilots")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "pilot")==0) { tmp->npilots++; // Pilot count. pilot = MALLOC_L(FleetPilot); @@ -373,9 +369,9 @@ static Fleet* fleet_parse(const xmlNodePtr parent) { memcpy(tmp->pilots+(tmp->npilots-1), pilot, sizeof(FleetPilot)); free(pilot); } - } + } while((cur = cur->next)); } - } + } while((node = node->next)); #define MELEMENT(o,s) if((o) == NULL) WARN("Fleet '%s' missing '"s"' element", tmp->name) MELEMENT(tmp->ai, "ai"); MELEMENT(tmp->faction, "faction"); diff --git a/src/player.c b/src/player.c index 6a98bf6..e152881 100644 --- a/src/player.c +++ b/src/player.c @@ -1,11 +1,11 @@ #include -#include #include "main.h" #include "pilot.h" #include "log.h" #include "opengl.h" #include "pack.h" +#include "xml.h" #include "player.h" #define XML_NODE_START 1 @@ -340,7 +340,7 @@ int gui_load(const char* name) { xmlDocPtr doc = xmlParseMemory(buf, bufsize); node = doc->xmlChildrenNode; - if(strcmp((char*)node->name, XML_GUI_ID)) { + if(!xml_isNode(node, XML_GUI_ID)) { ERR("Malformed '"GUI_DATA"' file: missing root element '"XML_GUI_ID"'"); return -1; } @@ -352,8 +352,8 @@ int gui_load(const char* name) { } do { - if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_GUI_TAG)==0) { - tmp = (char*)xmlGetProp(node, (xmlChar*)"name"); // Mallocs. + if(xml_isNode(node, XML_GUI_TAG)) { + tmp = xml_nodeProp(node, "name"); // Mallocs. // Is this the gui we are looking for? if(strcmp(tmp, name)==0) { @@ -388,28 +388,28 @@ static void rect_parse(const xmlNodePtr parent, double* x, double* y, double* w, cur = parent->children; do { - if(strcmp((char*)cur->name, "x")==0) { + if(xml_isNode(cur, "x")) { if(x != NULL) { *x = (double)atoi((char*)cur->children->content); param |= (1<<0); } else WARN("Extra parameter 'x' found for GUI node '%s'", parent->name); } - else if(strcmp((char*)cur->name, "y")==0) { + else if(xml_isNode(cur, "y")) { if(y != NULL) { *y = (double)atoi((char*)cur->children->content); param |= (1<<1); } else WARN("Extra parameter 'y' found for GUI node '%s'", parent->name); } - else if(strcmp((char*)cur->name, "w")==0) { + else if(xml_isNode(cur, "w")) { if(w != NULL) { *w = (double)atoi((char*)cur->children->content); param |= (1<<2); } else WARN("Extra parameter 'w' found for GUI node '%s'", parent->name); } - else if(strcmp((char*)cur->name, "h")==0) { + else if(xml_isNode(cur, "h")) { if(h != NULL) { *h = (double)atoi((char*)cur->children->content); param |= (1<<3); @@ -437,7 +437,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { // Gfx. // Set a property and not a node because it must be loaded first. - tmp2 = (char*)xmlGetProp(parent, (xmlChar*)"gfx"); + tmp2 = xml_nodeProp(parent, "gfx"); if(tmp2 == NULL) { ERR("GUI '%s' has no gfx property", name); return -1; @@ -465,14 +465,13 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { node = parent->children; do { // Offset. - if(strcmp((char*)node->name, "offset")==0) { + if(xml_isNode(node, "offset")) { rect_parse(node, &x, &y, NULL, NULL); gui_xoff = x; gui_yoff = y; } - else if(strcmp((char*)node->name, "radar")==0) { - tmp = (char*)xmlGetProp(node, (xmlChar*)"type"); - + else if(xml_isNode(node, "radar")) { + tmp = xml_nodeProp(node,"type"); // Make sure type is valid. if(strcmp(tmp, "rectangle")==0) gui.radar.shape = RADAR_RECT; else if(strcmp(tmp, "circle")==0) gui.radar.shape = RADAR_CIRCLE; @@ -492,24 +491,24 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { VY(gui.pos_frame) + gui.gfx_frame->h - y); } // Health bars. - else if(strcmp((char*)node->name, "health")==0) { + else if(xml_isNode(node, "health")) { cur = node->children; do { - if(strcmp((char*)cur->name, "shield")==0) { + if(xml_isNode(cur, "shield")) { rect_parse(cur, &x, &y, &gui.shield.w, &gui.shield.h); vect_csetmin(&gui.pos_shield, VX(gui.pos_frame) + x, VY(gui.pos_frame) + gui.gfx_frame->h - y); } - if(strcmp((char*)cur->name, "armor")==0) { + if(xml_isNode(cur, "armor")) { rect_parse(cur, &x, &y, &gui.armor.w, &gui.armor.h); vect_csetmin(&gui.pos_armor, VX(gui.pos_frame) + x, VY(gui.pos_frame) + gui.gfx_frame->h - y); } - if(strcmp((char*)cur->name, "energy")==0) { + if(xml_isNode(cur, "energy")) { rect_parse(cur, &x, &y, &gui.energy.w, &gui.energy.h); vect_csetmin(&gui.pos_energy, VX(gui.pos_frame) + x, @@ -518,31 +517,31 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { } while((cur = cur->next)); } // Target. - else if(strcmp((char*)node->name, "target")==0) { + else if(xml_isNode(node, "target")) { cur = node->children; do { - if(strcmp((char*)cur->name, "gfx")==0) { + if(xml_isNode(cur, "gfx")) { rect_parse(cur, &x, &y, NULL, NULL); vect_csetmin(&gui.pos_target, VX(gui.pos_frame) + x, VY(gui.pos_frame) + gui.gfx_frame->h - y - SHIP_TARGET_H); } - if(strcmp((char*)cur->name, "name")==0) { + if(xml_isNode(cur, "name")) { rect_parse(cur, &x, &y, NULL, NULL); vect_csetmin(&gui.pos_target_name, VX(gui.pos_frame) + x, VY(gui.pos_frame) + gui.gfx_frame->h - y - gl_defFont.h); } - if(strcmp((char*)cur->name, "faction")==0) { + if(xml_isNode(cur, "faction")) { rect_parse(cur, &x, &y, NULL, NULL); vect_csetmin(&gui.pos_target_faction, VX(gui.pos_frame) + x, VY(gui.pos_frame) + gui.gfx_frame->h - y - gui.smallFont.h); } - if(strcmp((char*)cur->name, "health")==0) { + if(xml_isNode(cur, "health")) { rect_parse(cur, &x, &y, NULL, NULL); vect_csetmin(&gui.pos_target_health, VX(gui.pos_frame) + x, diff --git a/src/ship.c b/src/ship.c index 39084b1..b9e654f 100644 --- a/src/ship.c +++ b/src/ship.c @@ -1,14 +1,11 @@ #include -#include #include "main.h" #include "log.h" #include "pack.h" +#include "xml.h" #include "ship.h" -#define XML_NODE_START 1 -#define XML_NODE_TEXT 3 - #define XML_ID "Ships" // XML section identifier. #define XML_SHIP "ship" @@ -48,7 +45,8 @@ static Ship* ship_parse(xmlNodePtr parent) { node = parent->xmlChildrenNode; - while((node = node->next)) { // Load all the data. + do { + // Load all the data. if(strcmp((char*)node->name, "GFX")==0) { snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX)+sizeof(SHIP_EXT), SHIP_GFX"%s"SHIP_EXT, (char*)node->children->content); @@ -65,18 +63,18 @@ static Ship* ship_parse(xmlNodePtr parent) { tmp->class = atoi((char*)node->children->content); else if(strcmp((char*)node->name, "movement")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "thrust")==0) tmp->thrust = atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "turn")==0) tmp->turn = atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "speed")==0) tmp->speed = atoi((char*)cur->children->content); - } - } - else if(strcmp((char*)node->name, "health")==0) { - cur = node->children; - while((cur = cur->next)) { + } while((cur = cur->next)); + } + else if(strcmp((char*)node->name, "health")==0) { + cur = node->children; + do { if(strcmp((char*)cur->name, "armor")==0) tmp->armor = (double)atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "shield")==0) @@ -89,24 +87,24 @@ static Ship* ship_parse(xmlNodePtr parent) { tmp->shield_regen = (double)(atoi((char*)cur->children->content))/60.0; else if(strcmp((char*)cur->name, "energy_regen")==0) tmp->energy_regen = (double)(atoi((char*)cur->children->content))/60.0; - } - } - else if(strcmp((char*)node->name, "characteristics")==0) { - cur = node->children; - while((cur = cur->next)) { - if(strcmp((char*)cur->name, "crew")==0) - tmp->crew = atoi((char*)cur->children->content); - else if(strcmp((char*)cur->name, "mass")==0) - tmp->mass = (double)atoi((char*)cur->children->content); - else if(strcmp((char*)cur->name, "cap_weapon")==0) - tmp->cap_weapon = atoi((char*)cur->children->content); - else if(strcmp((char*)cur->name, "cap_cargo")==0) - tmp->cap_cargo = atoi((char*)cur->children->content); - } + } while((cur = cur->next)); + } + else if(strcmp((char*)node->name, "characteristics")==0) { + cur = node->children; + do { + if(strcmp((char*)cur->name, "crew")==0) + tmp->crew = atoi((char*)cur->children->content); + else if(strcmp((char*)cur->name, "mass")==0) + tmp->mass = (double)atoi((char*)cur->children->content); + else if(strcmp((char*)cur->name, "cap_weapon")==0) + tmp->cap_weapon = atoi((char*)cur->children->content); + else if(strcmp((char*)cur->name, "cap_cargo")==0) + tmp->cap_cargo = atoi((char*)cur->children->content); + } while((cur = cur->next)); } else if(strcmp((char*)node->name, "outfits")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "outfit")==0) { otmp = MALLOC_L(ShipOutfit); otmp->data = outfit_get((char*)cur->children->content); @@ -123,9 +121,9 @@ static Ship* ship_parse(xmlNodePtr parent) { ocur->next = otmp; } } - } + } while((cur = cur->next)); } - } + } while((node = node->next)); tmp->thrust *= tmp->mass; // Helps keep number sane. diff --git a/src/space.c b/src/space.c index 7df67d3..19380e4 100644 --- a/src/space.c +++ b/src/space.c @@ -1,4 +1,3 @@ -#include #include #include @@ -8,11 +7,9 @@ #include "pack.h" #include "space.h" #include "faction.h" +#include "xml.h" #include "player.h" -#define XML_NODE_START 1 -#define XML_NODE_TEST 3 - #define XML_PLANET_ID "Planets" #define XML_PLANET_TAG "planet" @@ -246,7 +243,7 @@ static Planet* planet_get(const char* name) { node = node->xmlChildrenNode; - while((node = node->next)) { + do { if(strcmp((char*)node->name, "GFX")==0) { cur = node->children; if(strcmp((char*)cur->name, "text")==0) { @@ -257,7 +254,7 @@ static Planet* planet_get(const char* name) { } else if(strcmp((char*)node->name, "pos")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "x")==0) { flags |= FLAG_XSET; tmp->pos.x = atof((char*)cur->children->content); @@ -266,18 +263,18 @@ static Planet* planet_get(const char* name) { flags |= FLAG_YSET; tmp->pos.y = atof((char*)cur->children->content); } - } + } while((cur = cur->next)); } else if(strcmp((char*)node->name, "general")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "class")==0) tmp->class = planetclass_get(cur->children->content[0]); else if(strcmp((char*)cur->name, "faction")==0) tmp->faction = faction_get((char*)cur->children->content); - } + } while((cur = cur->next)); } - } + } while((node = node->next)); break; } else free(tstr); // xmlGetProp mallocs the string. @@ -317,11 +314,11 @@ static StarSystem* system_parse(const xmlNodePtr parent) { node = parent->xmlChildrenNode; - while((node = node->next)) { + do { // Load all the things! if(strcmp((char*)node->name, "pos")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "x")==0) { flags |= FLAG_XSET; tmp->pos.x = atof((char*)cur->children->content); @@ -330,11 +327,11 @@ static StarSystem* system_parse(const xmlNodePtr parent) { flags |= FLAG_YSET; tmp->pos.y = atof((char*)cur->children->content); } - } + } while((cur = cur->next)); } else if(strcmp((char*)node->name, "general")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "stars")==0) // Non-zero. tmp->stars = atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "asteroids")==0) { @@ -345,24 +342,24 @@ static StarSystem* system_parse(const xmlNodePtr parent) { flags |= FLAG_INTEFERENCESET; tmp->interference = atof((char*)cur->children->content); } - } + }while((cur = cur->next)); } // Load all the planets. else if(strcmp((char*)node->name, "planets")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "planet")==0) { planet = planet_get((const char*)cur->children->content); tmp->planets = realloc(tmp->planets, sizeof(Planet)*(++tmp->nplanets)); memcpy(tmp->planets+(tmp->nplanets-1), planet, sizeof(Planet)); free(planet); } - } + } while((cur = cur->next)); } // Load all the fleets. else if(strcmp((char*)node->name, "fleets")==0) { cur = node->children; - while((cur = cur->next)) { + do { if(strcmp((char*)cur->name, "fleet")==0) { fleet = CALLOC_L(SystemFleet); fleet->fleet = fleet_get((const char*)cur->children->content); @@ -380,9 +377,9 @@ static StarSystem* system_parse(const xmlNodePtr parent) { memcpy(tmp->fleets+(tmp->nfleets-1), fleet, sizeof(SystemFleet)); free(fleet); } - } + } while((cur = cur->next)); } - } + } while((node = node->next)); // Check elements. #define MELEMENT(o,s) if((o) == 0) WARN("Star System '%s' missing '"s"' element", tmp->name) MELEMENT(flags&FLAG_XSET, "x"); diff --git a/src/xml.h b/src/xml.h new file mode 100644 index 0000000..053300e --- /dev/null +++ b/src/xml.h @@ -0,0 +1,13 @@ +#pragma once + +#include "libxml/parser.h" + +#define XML_NODE_START 1 +#define XML_NODE_TEXT 3 + +// Check if node n is of name s. +#define xml_isNode(n,s) ((n)->type == XML_NODE_START) && \ + (strcmp((char*)(n)->name, s)==0) + +#define xml_nodeProp(n,s) (char*)xmlGetProp(n, (xmlChar*)s) +