[Change] Thrown XML crap into a single header.

-- player.c currently uses this.
This commit is contained in:
Allanis 2013-02-13 15:48:36 +00:00
parent dc76d96432
commit c4e7a8a9ee
7 changed files with 89 additions and 93 deletions

View File

@ -1,15 +1,12 @@
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include <libxml/parser.h>
#include "main.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
#include "xml.h"
#include "faction.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_ID "Factions" // XML section id.
#define XML_FACTION_TAG "faction" #define XML_FACTION_TAG "faction"
#define XML_ALLIANCE_ID "Alliances" #define XML_ALLIANCE_ID "Alliances"

View File

@ -1,15 +1,12 @@
#include <libxml/parser.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include "main.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
#include "xml.h"
#include "outfit.h" #include "outfit.h"
#define XML_NODE_START 1
#define XML_NODE_TEXT 3
#define XML_OUTFIT_ID "Outfits" #define XML_OUTFIT_ID "Outfits"
#define XML_OUTFIT_TAG "outfit" #define XML_OUTFIT_TAG "outfit"
@ -82,7 +79,7 @@ static void outfit_parseSWeapon(Outfit* tmp, const xmlNodePtr parent) {
char str[PATH_MAX] = "\0"; char str[PATH_MAX] = "\0";
while((node = node->next)) { do {
// Load all the things. // Load all the things.
if(strcmp((char*)node->name, "speed")==0) if(strcmp((char*)node->name, "speed")==0)
tmp->speed = (double)atoi((char*)node->children->content); 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); 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) #define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name)
MELEMENT(tmp->accuracy, "tech"); MELEMENT(tmp->accuracy, "tech");
MELEMENT(tmp->delay, "delay"); 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"); if(tmp->name == NULL) WARN("Outfit in "OUTFIT_DATA" has invalid or no name");
node = parent->xmlChildrenNode; node = parent->xmlChildrenNode;
do {
while((node = node->next)) {
// Load all the things. // Load all the things.
if(strcmp((char*)node->name, "general")==0) { if(strcmp((char*)node->name, "general")==0) {
cur = node->children; cur = node->children;
@ -160,7 +156,7 @@ static Outfit* outfit_parse(const xmlNodePtr parent) {
break; break;
} }
} }
} } while((node = node->next));
#define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name) #define MELEMENT(o,s) if((o) == 0) WARN("Outfit '%s' missing '"s"' element", tmp->name)
MELEMENT(tmp->max, "max"); MELEMENT(tmp->max, "max");
MELEMENT(tmp->tech, "tech"); MELEMENT(tmp->tech, "tech");

View File

@ -3,17 +3,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> // We don't need this? #include <assert.h> // We don't need this?
#include <libxml/parser.h>
#include "main.h" #include "main.h"
#include "log.h" #include "log.h"
#include "weapon.h" #include "weapon.h"
#include "pack.h" #include "pack.h"
#include "xml.h"
#include "pilot.h" #include "pilot.h"
#define XML_NODE_START 1
#define XML_NODE_TEXT 3
#define XML_ID "Fleets" // XML section identifier. #define XML_ID "Fleets" // XML section identifier.
#define XML_FLEET "fleet" #define XML_FLEET "fleet"
@ -342,7 +338,7 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); // Already mallocs. tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); // Already mallocs.
if(tmp->name == NULL) WARN("Fleet in "FLEET_DATA" has invalid or no name"); if(tmp->name == NULL) WARN("Fleet in "FLEET_DATA" has invalid or no name");
while((node = node->next)) { do {
// Load all the data. // Load all the data.
if(strcmp((char*)node->name, "faction")==0) if(strcmp((char*)node->name, "faction")==0)
tmp->faction = faction_get((char*)node->children->content); 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); tmp->ai = ai_getProfile((char*)node->children->content);
else if(strcmp((char*)node->name, "pilots")==0) { else if(strcmp((char*)node->name, "pilots")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "pilot")==0) { if(strcmp((char*)cur->name, "pilot")==0) {
tmp->npilots++; // Pilot count. tmp->npilots++; // Pilot count.
pilot = MALLOC_L(FleetPilot); pilot = MALLOC_L(FleetPilot);
@ -373,9 +369,9 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
memcpy(tmp->pilots+(tmp->npilots-1), pilot, sizeof(FleetPilot)); memcpy(tmp->pilots+(tmp->npilots-1), pilot, sizeof(FleetPilot));
free(pilot); 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) #define MELEMENT(o,s) if((o) == NULL) WARN("Fleet '%s' missing '"s"' element", tmp->name)
MELEMENT(tmp->ai, "ai"); MELEMENT(tmp->ai, "ai");
MELEMENT(tmp->faction, "faction"); MELEMENT(tmp->faction, "faction");

View File

@ -1,11 +1,11 @@
#include <malloc.h> #include <malloc.h>
#include <libxml/parser.h>
#include "main.h" #include "main.h"
#include "pilot.h" #include "pilot.h"
#include "log.h" #include "log.h"
#include "opengl.h" #include "opengl.h"
#include "pack.h" #include "pack.h"
#include "xml.h"
#include "player.h" #include "player.h"
#define XML_NODE_START 1 #define XML_NODE_START 1
@ -340,7 +340,7 @@ int gui_load(const char* name) {
xmlDocPtr doc = xmlParseMemory(buf, bufsize); xmlDocPtr doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode; 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"'"); ERR("Malformed '"GUI_DATA"' file: missing root element '"XML_GUI_ID"'");
return -1; return -1;
} }
@ -352,8 +352,8 @@ int gui_load(const char* name) {
} }
do { do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_GUI_TAG)==0) { if(xml_isNode(node, XML_GUI_TAG)) {
tmp = (char*)xmlGetProp(node, (xmlChar*)"name"); // Mallocs. tmp = xml_nodeProp(node, "name"); // Mallocs.
// Is this the gui we are looking for? // Is this the gui we are looking for?
if(strcmp(tmp, name)==0) { 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; cur = parent->children;
do { do {
if(strcmp((char*)cur->name, "x")==0) { if(xml_isNode(cur, "x")) {
if(x != NULL) { if(x != NULL) {
*x = (double)atoi((char*)cur->children->content); *x = (double)atoi((char*)cur->children->content);
param |= (1<<0); param |= (1<<0);
} else } else
WARN("Extra parameter 'x' found for GUI node '%s'", parent->name); 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) { if(y != NULL) {
*y = (double)atoi((char*)cur->children->content); *y = (double)atoi((char*)cur->children->content);
param |= (1<<1); param |= (1<<1);
} else } else
WARN("Extra parameter 'y' found for GUI node '%s'", parent->name); 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) { if(w != NULL) {
*w = (double)atoi((char*)cur->children->content); *w = (double)atoi((char*)cur->children->content);
param |= (1<<2); param |= (1<<2);
} else } else
WARN("Extra parameter 'w' found for GUI node '%s'", parent->name); 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) { if(h != NULL) {
*h = (double)atoi((char*)cur->children->content); *h = (double)atoi((char*)cur->children->content);
param |= (1<<3); param |= (1<<3);
@ -437,7 +437,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
// Gfx. // Gfx.
// Set a property and not a node because it must be loaded first. // 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) { if(tmp2 == NULL) {
ERR("GUI '%s' has no gfx property", name); ERR("GUI '%s' has no gfx property", name);
return -1; return -1;
@ -465,14 +465,13 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
node = parent->children; node = parent->children;
do { do {
// Offset. // Offset.
if(strcmp((char*)node->name, "offset")==0) { if(xml_isNode(node, "offset")) {
rect_parse(node, &x, &y, NULL, NULL); rect_parse(node, &x, &y, NULL, NULL);
gui_xoff = x; gui_xoff = x;
gui_yoff = y; gui_yoff = y;
} }
else if(strcmp((char*)node->name, "radar")==0) { else if(xml_isNode(node, "radar")) {
tmp = (char*)xmlGetProp(node, (xmlChar*)"type"); tmp = xml_nodeProp(node,"type");
// Make sure type is valid. // Make sure type is valid.
if(strcmp(tmp, "rectangle")==0) gui.radar.shape = RADAR_RECT; if(strcmp(tmp, "rectangle")==0) gui.radar.shape = RADAR_RECT;
else if(strcmp(tmp, "circle")==0) gui.radar.shape = RADAR_CIRCLE; 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); VY(gui.pos_frame) + gui.gfx_frame->h - y);
} }
// Health bars. // Health bars.
else if(strcmp((char*)node->name, "health")==0) { else if(xml_isNode(node, "health")) {
cur = node->children; cur = node->children;
do { do {
if(strcmp((char*)cur->name, "shield")==0) { if(xml_isNode(cur, "shield")) {
rect_parse(cur, &x, &y, &gui.shield.w, &gui.shield.h); rect_parse(cur, &x, &y, &gui.shield.w, &gui.shield.h);
vect_csetmin(&gui.pos_shield, vect_csetmin(&gui.pos_shield,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
VY(gui.pos_frame) + gui.gfx_frame->h - y); 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); rect_parse(cur, &x, &y, &gui.armor.w, &gui.armor.h);
vect_csetmin(&gui.pos_armor, vect_csetmin(&gui.pos_armor,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
VY(gui.pos_frame) + gui.gfx_frame->h - y); 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); rect_parse(cur, &x, &y, &gui.energy.w, &gui.energy.h);
vect_csetmin(&gui.pos_energy, vect_csetmin(&gui.pos_energy,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
@ -518,31 +517,31 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
} while((cur = cur->next)); } while((cur = cur->next));
} }
// Target. // Target.
else if(strcmp((char*)node->name, "target")==0) { else if(xml_isNode(node, "target")) {
cur = node->children; cur = node->children;
do { do {
if(strcmp((char*)cur->name, "gfx")==0) { if(xml_isNode(cur, "gfx")) {
rect_parse(cur, &x, &y, NULL, NULL); rect_parse(cur, &x, &y, NULL, NULL);
vect_csetmin(&gui.pos_target, vect_csetmin(&gui.pos_target,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
VY(gui.pos_frame) + gui.gfx_frame->h - y - SHIP_TARGET_H); 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); rect_parse(cur, &x, &y, NULL, NULL);
vect_csetmin(&gui.pos_target_name, vect_csetmin(&gui.pos_target_name,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
VY(gui.pos_frame) + gui.gfx_frame->h - y - gl_defFont.h); 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); rect_parse(cur, &x, &y, NULL, NULL);
vect_csetmin(&gui.pos_target_faction, vect_csetmin(&gui.pos_target_faction,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,
VY(gui.pos_frame) + gui.gfx_frame->h - y - gui.smallFont.h); 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); rect_parse(cur, &x, &y, NULL, NULL);
vect_csetmin(&gui.pos_target_health, vect_csetmin(&gui.pos_target_health,
VX(gui.pos_frame) + x, VX(gui.pos_frame) + x,

View File

@ -1,14 +1,11 @@
#include <string.h> #include <string.h>
#include <libxml/parser.h>
#include "main.h" #include "main.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
#include "xml.h"
#include "ship.h" #include "ship.h"
#define XML_NODE_START 1
#define XML_NODE_TEXT 3
#define XML_ID "Ships" // XML section identifier. #define XML_ID "Ships" // XML section identifier.
#define XML_SHIP "ship" #define XML_SHIP "ship"
@ -48,7 +45,8 @@ static Ship* ship_parse(xmlNodePtr parent) {
node = parent->xmlChildrenNode; node = parent->xmlChildrenNode;
while((node = node->next)) { // Load all the data. do {
// Load all the data.
if(strcmp((char*)node->name, "GFX")==0) { if(strcmp((char*)node->name, "GFX")==0) {
snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX)+sizeof(SHIP_EXT), snprintf(str, strlen((char*)node->children->content)+sizeof(SHIP_GFX)+sizeof(SHIP_EXT),
SHIP_GFX"%s"SHIP_EXT, (char*)node->children->content); 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); tmp->class = atoi((char*)node->children->content);
else if(strcmp((char*)node->name, "movement")==0) { else if(strcmp((char*)node->name, "movement")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "thrust")==0) if(strcmp((char*)cur->name, "thrust")==0)
tmp->thrust = atoi((char*)cur->children->content); tmp->thrust = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "turn")==0) else if(strcmp((char*)cur->name, "turn")==0)
tmp->turn = atoi((char*)cur->children->content); tmp->turn = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "speed")==0) else if(strcmp((char*)cur->name, "speed")==0)
tmp->speed = atoi((char*)cur->children->content); tmp->speed = atoi((char*)cur->children->content);
} } while((cur = cur->next));
} }
else if(strcmp((char*)node->name, "health")==0) { else if(strcmp((char*)node->name, "health")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "armor")==0) if(strcmp((char*)cur->name, "armor")==0)
tmp->armor = (double)atoi((char*)cur->children->content); tmp->armor = (double)atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "shield")==0) 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; tmp->shield_regen = (double)(atoi((char*)cur->children->content))/60.0;
else if(strcmp((char*)cur->name, "energy_regen")==0) else if(strcmp((char*)cur->name, "energy_regen")==0)
tmp->energy_regen = (double)(atoi((char*)cur->children->content))/60.0; tmp->energy_regen = (double)(atoi((char*)cur->children->content))/60.0;
} } while((cur = cur->next));
} }
else if(strcmp((char*)node->name, "characteristics")==0) { else if(strcmp((char*)node->name, "characteristics")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "crew")==0) if(strcmp((char*)cur->name, "crew")==0)
tmp->crew = atoi((char*)cur->children->content); tmp->crew = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "mass")==0) else if(strcmp((char*)cur->name, "mass")==0)
tmp->mass = (double)atoi((char*)cur->children->content); tmp->mass = (double)atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "cap_weapon")==0) else if(strcmp((char*)cur->name, "cap_weapon")==0)
tmp->cap_weapon = atoi((char*)cur->children->content); tmp->cap_weapon = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "cap_cargo")==0) else if(strcmp((char*)cur->name, "cap_cargo")==0)
tmp->cap_cargo = atoi((char*)cur->children->content); tmp->cap_cargo = atoi((char*)cur->children->content);
} } while((cur = cur->next));
} }
else if(strcmp((char*)node->name, "outfits")==0) { else if(strcmp((char*)node->name, "outfits")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "outfit")==0) { if(strcmp((char*)cur->name, "outfit")==0) {
otmp = MALLOC_L(ShipOutfit); otmp = MALLOC_L(ShipOutfit);
otmp->data = outfit_get((char*)cur->children->content); otmp->data = outfit_get((char*)cur->children->content);
@ -123,9 +121,9 @@ static Ship* ship_parse(xmlNodePtr parent) {
ocur->next = otmp; ocur->next = otmp;
} }
} }
} } while((cur = cur->next));
} }
} } while((node = node->next));
tmp->thrust *= tmp->mass; // Helps keep number sane. tmp->thrust *= tmp->mass; // Helps keep number sane.

View File

@ -1,4 +1,3 @@
#include <libxml/parser.h>
#include <malloc.h> #include <malloc.h>
#include <math.h> #include <math.h>
@ -8,11 +7,9 @@
#include "pack.h" #include "pack.h"
#include "space.h" #include "space.h"
#include "faction.h" #include "faction.h"
#include "xml.h"
#include "player.h" #include "player.h"
#define XML_NODE_START 1
#define XML_NODE_TEST 3
#define XML_PLANET_ID "Planets" #define XML_PLANET_ID "Planets"
#define XML_PLANET_TAG "planet" #define XML_PLANET_TAG "planet"
@ -246,7 +243,7 @@ static Planet* planet_get(const char* name) {
node = node->xmlChildrenNode; node = node->xmlChildrenNode;
while((node = node->next)) { do {
if(strcmp((char*)node->name, "GFX")==0) { if(strcmp((char*)node->name, "GFX")==0) {
cur = node->children; cur = node->children;
if(strcmp((char*)cur->name, "text")==0) { 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) { else if(strcmp((char*)node->name, "pos")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "x")==0) { if(strcmp((char*)cur->name, "x")==0) {
flags |= FLAG_XSET; flags |= FLAG_XSET;
tmp->pos.x = atof((char*)cur->children->content); tmp->pos.x = atof((char*)cur->children->content);
@ -266,18 +263,18 @@ static Planet* planet_get(const char* name) {
flags |= FLAG_YSET; flags |= FLAG_YSET;
tmp->pos.y = atof((char*)cur->children->content); tmp->pos.y = atof((char*)cur->children->content);
} }
} } while((cur = cur->next));
} }
else if(strcmp((char*)node->name, "general")==0) { else if(strcmp((char*)node->name, "general")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "class")==0) if(strcmp((char*)cur->name, "class")==0)
tmp->class = planetclass_get(cur->children->content[0]); tmp->class = planetclass_get(cur->children->content[0]);
else if(strcmp((char*)cur->name, "faction")==0) else if(strcmp((char*)cur->name, "faction")==0)
tmp->faction = faction_get((char*)cur->children->content); tmp->faction = faction_get((char*)cur->children->content);
} } while((cur = cur->next));
} }
} } while((node = node->next));
break; break;
} else } else
free(tstr); // xmlGetProp mallocs the string. free(tstr); // xmlGetProp mallocs the string.
@ -317,11 +314,11 @@ static StarSystem* system_parse(const xmlNodePtr parent) {
node = parent->xmlChildrenNode; node = parent->xmlChildrenNode;
while((node = node->next)) { do {
// Load all the things! // Load all the things!
if(strcmp((char*)node->name, "pos")==0) { if(strcmp((char*)node->name, "pos")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "x")==0) { if(strcmp((char*)cur->name, "x")==0) {
flags |= FLAG_XSET; flags |= FLAG_XSET;
tmp->pos.x = atof((char*)cur->children->content); tmp->pos.x = atof((char*)cur->children->content);
@ -330,11 +327,11 @@ static StarSystem* system_parse(const xmlNodePtr parent) {
flags |= FLAG_YSET; flags |= FLAG_YSET;
tmp->pos.y = atof((char*)cur->children->content); tmp->pos.y = atof((char*)cur->children->content);
} }
} } while((cur = cur->next));
} }
else if(strcmp((char*)node->name, "general")==0) { else if(strcmp((char*)node->name, "general")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "stars")==0) // Non-zero. if(strcmp((char*)cur->name, "stars")==0) // Non-zero.
tmp->stars = atoi((char*)cur->children->content); tmp->stars = atoi((char*)cur->children->content);
else if(strcmp((char*)cur->name, "asteroids")==0) { else if(strcmp((char*)cur->name, "asteroids")==0) {
@ -345,24 +342,24 @@ static StarSystem* system_parse(const xmlNodePtr parent) {
flags |= FLAG_INTEFERENCESET; flags |= FLAG_INTEFERENCESET;
tmp->interference = atof((char*)cur->children->content); tmp->interference = atof((char*)cur->children->content);
} }
} }while((cur = cur->next));
} }
// Load all the planets. // Load all the planets.
else if(strcmp((char*)node->name, "planets")==0) { else if(strcmp((char*)node->name, "planets")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "planet")==0) { if(strcmp((char*)cur->name, "planet")==0) {
planet = planet_get((const char*)cur->children->content); planet = planet_get((const char*)cur->children->content);
tmp->planets = realloc(tmp->planets, sizeof(Planet)*(++tmp->nplanets)); tmp->planets = realloc(tmp->planets, sizeof(Planet)*(++tmp->nplanets));
memcpy(tmp->planets+(tmp->nplanets-1), planet, sizeof(Planet)); memcpy(tmp->planets+(tmp->nplanets-1), planet, sizeof(Planet));
free(planet); free(planet);
} }
} } while((cur = cur->next));
} }
// Load all the fleets. // Load all the fleets.
else if(strcmp((char*)node->name, "fleets")==0) { else if(strcmp((char*)node->name, "fleets")==0) {
cur = node->children; cur = node->children;
while((cur = cur->next)) { do {
if(strcmp((char*)cur->name, "fleet")==0) { if(strcmp((char*)cur->name, "fleet")==0) {
fleet = CALLOC_L(SystemFleet); fleet = CALLOC_L(SystemFleet);
fleet->fleet = fleet_get((const char*)cur->children->content); 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)); memcpy(tmp->fleets+(tmp->nfleets-1), fleet, sizeof(SystemFleet));
free(fleet); free(fleet);
} }
} } while((cur = cur->next));
} }
} } while((node = node->next));
// Check elements. // Check elements.
#define MELEMENT(o,s) if((o) == 0) WARN("Star System '%s' missing '"s"' element", tmp->name) #define MELEMENT(o,s) if((o) == 0) WARN("Star System '%s' missing '"s"' element", tmp->name)
MELEMENT(flags&FLAG_XSET, "x"); MELEMENT(flags&FLAG_XSET, "x");

13
src/xml.h Normal file
View File

@ -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)