[Change] Some code clean up and optimization.
This commit is contained in:
parent
38a0328d66
commit
6844e4f5b8
44
src/ship.c
44
src/ship.c
@ -23,10 +23,12 @@
|
|||||||
#define BUTTON_WIDTH 80
|
#define BUTTON_WIDTH 80
|
||||||
#define BUTTON_HEIGHT 30
|
#define BUTTON_HEIGHT 30
|
||||||
|
|
||||||
|
#define CHUNK_SIZE 32
|
||||||
|
|
||||||
static Ship* ship_stack = NULL;
|
static Ship* ship_stack = NULL;
|
||||||
static int ship_nstack = 0;
|
static int ship_nstack = 0;
|
||||||
|
|
||||||
static Ship* ship_parse(xmlNodePtr parent);
|
static int ship_parse(Ship* tmp, xmlNodePtr parent);
|
||||||
|
|
||||||
/* Get a ship based on it's name. */
|
/* Get a ship based on it's name. */
|
||||||
Ship* ship_get(const char* name) {
|
Ship* ship_get(const char* name) {
|
||||||
@ -221,17 +223,22 @@ int ship_basePrice(Ship* s) {
|
|||||||
return price;
|
return price;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Ship* ship_parse(xmlNodePtr parent) {
|
static int ship_parse(Ship* tmp, xmlNodePtr parent) {
|
||||||
xmlNodePtr cur, node;
|
xmlNodePtr cur, node;
|
||||||
Ship* tmp = CALLOC_L(Ship);
|
|
||||||
ShipOutfit* otmp, *ocur;
|
ShipOutfit* otmp, *ocur;
|
||||||
|
|
||||||
char str[PATH_MAX] = "\0";
|
char str[PATH_MAX] = "\0";
|
||||||
char* stmp;
|
char* stmp;
|
||||||
|
|
||||||
xmlr_attr(parent, "name", tmp->name);
|
/* Clear memory. */
|
||||||
if(tmp->name == NULL) WARN("Ship in "SHIP_DATA" has invalid or no name");
|
memset(tmp, 0, sizeof(Ship));
|
||||||
|
|
||||||
|
/* Get name. */
|
||||||
|
xmlr_attr(parent, "name", tmp->name);
|
||||||
|
if(tmp->name == NULL)
|
||||||
|
WARN("Ship in "SHIP_DATA" has invalid or no name.");
|
||||||
|
|
||||||
|
/* Load data. */
|
||||||
node = parent->xmlChildrenNode;
|
node = parent->xmlChildrenNode;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@ -365,18 +372,17 @@ static Ship* ship_parse(xmlNodePtr parent) {
|
|||||||
MELEMENT(tmp->cap_weapon==0, "cap_weapon");
|
MELEMENT(tmp->cap_weapon==0, "cap_weapon");
|
||||||
#undef MELEMENT
|
#undef MELEMENT
|
||||||
|
|
||||||
return tmp;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ships_load(void) {
|
int ships_load(void) {
|
||||||
|
int mem;
|
||||||
uint32_t bufsize;
|
uint32_t bufsize;
|
||||||
char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize);
|
char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize);
|
||||||
|
|
||||||
xmlNodePtr node;
|
xmlNodePtr node;
|
||||||
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
||||||
|
|
||||||
Ship* tmp = NULL;
|
|
||||||
|
|
||||||
node = doc->xmlChildrenNode; /* Ships node. */
|
node = doc->xmlChildrenNode; /* Ships node. */
|
||||||
if(strcmp((char*)node->name, XML_ID)) {
|
if(strcmp((char*)node->name, XML_ID)) {
|
||||||
ERR("Malformed "SHIP_DATA" file: missing root element '"XML_ID"'");
|
ERR("Malformed "SHIP_DATA" file: missing root element '"XML_ID"'");
|
||||||
@ -389,14 +395,24 @@ int ships_load(void) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mem = 0;
|
||||||
do {
|
do {
|
||||||
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) {
|
if(xml_isNode(node, XML_SHIP)) {
|
||||||
tmp = ship_parse(node);
|
ship_nstack++;
|
||||||
ship_stack = realloc(ship_stack, sizeof(Ship)*(++ship_nstack));
|
|
||||||
memcpy(ship_stack+ship_nstack-1, tmp, sizeof(Ship));
|
/* Check to see if we need to grow the stack. */
|
||||||
free(tmp);
|
if(ship_nstack > mem) {
|
||||||
|
mem += CHUNK_SIZE;
|
||||||
|
ship_stack = realloc(ship_stack, sizeof(Ship)*mem);
|
||||||
}
|
}
|
||||||
} while((node = node->next));
|
|
||||||
|
/* Load the ship. */
|
||||||
|
ship_parse(&ship_stack[ship_nstack-1], node);
|
||||||
|
}
|
||||||
|
} while(xml_nextNode(node));
|
||||||
|
|
||||||
|
/* Shrink to minimum size - Won't change later. */
|
||||||
|
ship_stack = realloc(ship_stack, sizeof(Ship) * ship_nstack);
|
||||||
|
|
||||||
xmlFreeDoc(doc);
|
xmlFreeDoc(doc);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
Loading…
Reference in New Issue
Block a user