diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2007-04-22 13:39:22 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2007-04-22 13:39:22 +0000 |
commit | 9398626764574fa536dca3806490f88970d2f056 (patch) | |
tree | b8a5f938d6d6447846773a6270802c3595077222 /src/resources | |
parent | ccf4dd308bcb8a592ece2d86102bbaa7080f0f7e (diff) | |
download | mana-client-9398626764574fa536dca3806490f88970d2f056.tar.gz mana-client-9398626764574fa536dca3806490f88970d2f056.tar.bz2 mana-client-9398626764574fa536dca3806490f88970d2f056.tar.xz mana-client-9398626764574fa536dca3806490f88970d2f056.zip |
Added a macro for XML child node iterations to make the code more terse and easier to read.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/equipmentdb.cpp | 9 | ||||
-rw-r--r-- | src/resources/itemdb.cpp | 6 | ||||
-rw-r--r-- | src/resources/mapreader.cpp | 48 | ||||
-rw-r--r-- | src/resources/monsterdb.cpp | 9 | ||||
-rw-r--r-- | src/resources/spritedef.cpp | 18 |
5 files changed, 40 insertions, 50 deletions
diff --git a/src/resources/equipmentdb.cpp b/src/resources/equipmentdb.cpp index a6c26624..64982ce3 100644 --- a/src/resources/equipmentdb.cpp +++ b/src/resources/equipmentdb.cpp @@ -71,11 +71,8 @@ EquipmentDB::load() }
//iterate <equipment>s
- for ( xmlNodePtr equipmentNode = rootNode->xmlChildrenNode;
- equipmentNode != NULL;
- equipmentNode = equipmentNode->next)
+ for_each_xml_child_node(equipmentNode, rootNode)
{
-
if (!xmlStrEqual(equipmentNode->name, BAD_CAST "equipment"))
{
continue;
@@ -86,9 +83,7 @@ EquipmentDB::load() currentInfo->setSlot (XML::getProperty(equipmentNode, "slot", 0));
//iterate <sprite>s
- for ( xmlNodePtr spriteNode = equipmentNode->xmlChildrenNode;
- spriteNode != NULL;
- spriteNode = spriteNode->next)
+ for_each_xml_child_node(spriteNode, equipmentNode)
{
if (!xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
{
diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index f914af47..6e0ecce9 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -65,13 +65,13 @@ void ItemDB::load() logger->error("ItemDB: Error while parsing item database (items.xml)!");
}
- xmlNodePtr node = xmlDocGetRootElement(doc);
- if (!node || !xmlStrEqual(node->name, BAD_CAST "items"))
+ xmlNodePtr rootNode = xmlDocGetRootElement(doc);
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
{
logger->error("ItemDB: items.xml is not a valid database file!");
}
- for (node = node->xmlChildrenNode; node != NULL; node = node->next)
+ for_each_xml_child_node(node, rootNode)
{
if (!xmlStrEqual(node->name, BAD_CAST "item")) {
continue;
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 15a88b4d..fda8916d 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -212,24 +212,24 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) int layerNr = 0; Map *map = new Map(w, h, tilew, tileh); - for (node = node->xmlChildrenNode; node != NULL; node = node->next) + for_each_xml_child_node(childNode, node) { - if (xmlStrEqual(node->name, BAD_CAST "tileset")) + if (xmlStrEqual(childNode->name, BAD_CAST "tileset")) { - Tileset *tileset = readTileset(node, pathDir, map); + Tileset *tileset = readTileset(childNode, pathDir, map); if (tileset) { map->addTileset(tileset); } } - else if (xmlStrEqual(node->name, BAD_CAST "layer")) + else if (xmlStrEqual(childNode->name, BAD_CAST "layer")) { logger->log("- Loading layer %d", layerNr); - readLayer(node, map, layerNr); + readLayer(childNode, map, layerNr); layerNr++; } - else if (xmlStrEqual(node->name, BAD_CAST "properties")) + else if (xmlStrEqual(childNode->name, BAD_CAST "properties")) { - readProperties(node, map); + readProperties(childNode, map); } } @@ -241,13 +241,14 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) void MapReader::readProperties(xmlNodePtr node, Properties* props) { - for (node = node->xmlChildrenNode; node; node = node->next) { - if (!xmlStrEqual(node->name, BAD_CAST "property")) + for_each_xml_child_node(childNode, node) + { + if (!xmlStrEqual(childNode->name, BAD_CAST "property")) continue; // Example: <property name="name" value="value"/> - xmlChar *name = xmlGetProp(node, BAD_CAST "name"); - xmlChar *value = xmlGetProp(node, BAD_CAST "value"); + xmlChar *name = xmlGetProp(childNode, BAD_CAST "name"); + xmlChar *value = xmlGetProp(childNode, BAD_CAST "value"); if (name && value) { props->setProperty((const char*)name, (const char*)value); @@ -268,12 +269,13 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) // Load the tile data. Layers are assumed to be map size, with (0,0) as // origin. - for (node = node->xmlChildrenNode; node; node = node->next) { - if (!xmlStrEqual(node->name, BAD_CAST "data")) + for_each_xml_child_node(childNode, node) + { + if (!xmlStrEqual(childNode->name, BAD_CAST "data")) continue; - xmlChar *encoding = xmlGetProp(node, BAD_CAST "encoding"); - xmlChar *compression = xmlGetProp(node, BAD_CAST "compression"); + xmlChar *encoding = xmlGetProp(childNode, BAD_CAST "encoding"); + xmlChar *compression = xmlGetProp(childNode, BAD_CAST "compression"); if (encoding && xmlStrEqual(encoding, BAD_CAST "base64")) { @@ -286,7 +288,7 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) } // Read base64 encoded map file - xmlNodePtr dataChild = node->xmlChildrenNode; + xmlNodePtr dataChild = childNode->xmlChildrenNode; if (!dataChild) continue; @@ -350,11 +352,12 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) } else { // Read plain XML map file - for (xmlNodePtr n2 = node->xmlChildrenNode; n2; n2 = n2->next) { - if (!xmlStrEqual(n2->name, BAD_CAST "tile")) + for_each_xml_child_node(childNode2, childNode) + { + if (!xmlStrEqual(childNode2->name, BAD_CAST "tile")) continue; - int gid = XML::getProperty(n2, "gid", -1); + int gid = XML::getProperty(childNode2, "gid", -1); map->setTileWithGid(x, y, layer, gid); x++; @@ -391,11 +394,12 @@ MapReader::readTileset(xmlNodePtr node, int tw = XML::getProperty(node, "tilewidth", map->getTileWidth()); int th = XML::getProperty(node, "tileheight", map->getTileHeight()); - for (node = node->xmlChildrenNode; node; node = node->next) { - if (!xmlStrEqual(node->name, BAD_CAST "image")) + for_each_xml_child_node(childNode, node) + { + if (!xmlStrEqual(childNode->name, BAD_CAST "image")) continue; - xmlChar* source = xmlGetProp(node, BAD_CAST "source"); + xmlChar* source = xmlGetProp(childNode, BAD_CAST "source"); if (source) { diff --git a/src/resources/monsterdb.cpp b/src/resources/monsterdb.cpp index e773911d..ac3ac3bc 100644 --- a/src/resources/monsterdb.cpp +++ b/src/resources/monsterdb.cpp @@ -72,11 +72,8 @@ MonsterDB::load() }
//iterate <monster>s
- for ( xmlNodePtr monsterNode = rootNode->xmlChildrenNode;
- monsterNode != NULL;
- monsterNode = monsterNode->next)
+ for_each_xml_child_node(monsterNode, rootNode)
{
-
if (!xmlStrEqual(monsterNode->name, BAD_CAST "monster"))
{
continue;
@@ -87,9 +84,7 @@ MonsterDB::load() currentInfo->setName (XML::getProperty(monsterNode, "name", "unnamed"));
//iterate <sprite>s and <sound>s
- for ( xmlNodePtr spriteNode = monsterNode->xmlChildrenNode;
- spriteNode != NULL;
- spriteNode = spriteNode->next)
+ for_each_xml_child_node(spriteNode, monsterNode)
{
if (xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
{
diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index d29bd847..24156be1 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -76,22 +76,22 @@ SpriteDef::load(const std::string &animationFile, int variant) "Animation: Error while parsing animation definition file!"); } - xmlNodePtr node = xmlDocGetRootElement(doc); - if (!node || !xmlStrEqual(node->name, BAD_CAST "sprite")) { + xmlNodePtr rootNode = xmlDocGetRootElement(doc); + if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) { logger->error( "Animation: this is not a valid animation definition file!"); } // Get the variant - int variant_num = XML::getProperty(node, "variants", 0); + int variant_num = XML::getProperty(rootNode, "variants", 0); int variant_offset = 0; if (variant_num > 0 && variant < variant_num) { - variant_offset = variant * XML::getProperty(node, "variant_offset", 0); + variant_offset = variant * XML::getProperty(rootNode, "variant_offset", 0); } - for (node = node->xmlChildrenNode; node != NULL; node = node->next) + for_each_xml_child_node(node, rootNode) { if (xmlStrEqual(node->name, BAD_CAST "imageset")) { @@ -177,9 +177,7 @@ SpriteDef::loadAction(xmlNodePtr node, int variant_offset) } // Load animations - for (xmlNodePtr animationNode = node->xmlChildrenNode; - animationNode != NULL; - animationNode = animationNode->next) + for_each_xml_child_node(animationNode, node) { if (xmlStrEqual(animationNode->name, BAD_CAST "animation")) { @@ -208,9 +206,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, action->setAnimation(directionType, animation); // Get animation frames - for (xmlNodePtr frameNode = animationNode->xmlChildrenNode; - frameNode != NULL; - frameNode = frameNode->next) + for_each_xml_child_node(frameNode, animationNode) { int delay = XML::getProperty(frameNode, "delay", 0); int offsetX = XML::getProperty(frameNode, "offsetX", 0); |