From bc3a7cf853510ffe1e42dc683f854d360f6528c7 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 30 Nov 2015 13:59:45 +0300 Subject: Add pugixml backend incomplete. Add some defines for xml nodes manipulation. With pugixml backend no xml writing. --- src/Makefile.am | 27 ++- src/configuration.cpp | 12 +- src/gui/theme.cpp | 6 +- src/gui/windows/questswindow.cpp | 2 +- src/gui/windows/serverdialog.cpp | 20 +- src/particle/particle.cpp | 14 +- src/particle/particleemitter.cpp | 5 +- src/resources/animation/simpleanimation.cpp | 3 +- src/resources/beingcommon.cpp | 15 +- src/resources/db/avatardb.cpp | 6 +- src/resources/db/deaddb.cpp | 2 +- src/resources/db/emotedb.cpp | 16 +- src/resources/db/horsedb.cpp | 9 +- src/resources/db/itemdb.cpp | 25 +-- src/resources/db/npcdb.cpp | 10 +- src/resources/db/petdb.cpp | 8 +- src/resources/db/skillunitdb.cpp | 8 +- src/resources/mapreader.cpp | 17 +- src/utils/xml.h | 4 + src/utils/xml/libxml.h | 16 ++ src/utils/xml/pugixml.cpp | 335 ++++++++++++++++++++++++++++ src/utils/xml/pugixml.h | 181 +++++++++++++++ src/utils/xml_unittest.cc | 43 +++- 23 files changed, 659 insertions(+), 125 deletions(-) create mode 100644 src/utils/xml/pugixml.cpp create mode 100644 src/utils/xml/pugixml.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 467afc24e..3d36ed226 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -475,8 +475,6 @@ SRC += events/actionevent.h \ utils/xml.h \ utils/xmlutils.cpp \ utils/xmlutils.h \ - utils/xml/libxml.cpp \ - utils/xml/libxml.h \ test/testlauncher.cpp \ test/testlauncher.h \ test/testmain.cpp \ @@ -692,6 +690,15 @@ SRC += events/actionevent.h \ input/inputactionmap.h \ winver.h +if ENABLE_PUGIXML +SRC += utils/xml/pugixml.cpp \ + utils/xml/pugixml.h +endif +if ENABLE_LIBXML +SRC += utils/xml/libxml.cpp \ + utils/xml/libxml.h +endif + dyecmd_SOURCES += ${SRC} manaplus_SOURCES += ${SRC} @@ -1705,20 +1712,20 @@ check_PROGRAMS = manaplustests manaplustests_CXXFLAGS = ${manaplus_CXXFLAGS} \ -DUNITTESTS manaplustests_SOURCES = ${manaplus_SOURCES} \ - resources/sprite/animatedsprite_unittest.cc \ enums/enums_unittest.cc \ - gui/windowmanager_unittest.cc \ - gui/fonts/textchunklist_unittest.cc \ - gui/widgets/browserbox_unittest.cc \ - utils/files_unittest.cc \ - utils/mathutils_unittest.cc \ - utils/stringutils_unittest.cc \ utils/xml_unittest.cc \ utils/xmlutils_unittest.cc \ + utils/mathutils_unittest.cc \ + utils/stringutils_unittest.cc \ + utils/files_unittest.cc \ + resources/mstack_unittest.cc \ utils/translation/poparser_unittest.cc \ + resources/sprite/animatedsprite_unittest.cc \ + gui/fonts/textchunklist_unittest.cc \ + gui/widgets/browserbox_unittest.cc \ resources/dye/dye_unittest.cc \ resources/dye/dyepalette_unittest.cc \ - resources/mstack_unittest.cc + gui/windowmanager_unittest.cc endif EXTRA_DIST = CMakeLists.txt \ diff --git a/src/configuration.cpp b/src/configuration.cpp index fcc6bfbdf..583ea2d83 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -706,7 +706,7 @@ void ConfigurationObject::initFromXML(const XmlNodePtrConst parentNode) for_each_xml_child_node(subnode, node) { if (xmlNameEqual(subnode, name.c_str()) && - subnode->type == XML_ELEMENT_NODE) + xmlTypeEqual(subnode, XML_ELEMENT_NODE)) { ConfigurationObject *const cobj = new ConfigurationObject; cobj->initFromXML(subnode); // recurse @@ -855,7 +855,7 @@ void Configuration::write() fclose(testFile); } - const XmlTextWriterPtr writer = xmlNewTextWriterFilename( + const XmlTextWriterPtr writer = XmlNewTextWriterFilename( mConfigPath.c_str(), 0); if (!writer) @@ -867,15 +867,15 @@ void Configuration::write() logger->log1("Configuration::write() writing configuration..."); - xmlTextWriterSetIndent(writer, 1); - xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr); + XmlTextWriterSetIndent(writer, 1); + XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr); // xmlTextWriterStartDocument(writer, nullptr, "utf8", nullptr); XmlTextWriterStartElement(writer, "configuration"); writeToXML(writer); - xmlTextWriterEndDocument(writer); - xmlFreeTextWriter(writer); + XmlTextWriterEndDocument(writer); + XmlFreeTextWriter(writer); BLOCK_END("Configuration::write") } diff --git a/src/gui/theme.cpp b/src/gui/theme.cpp index 8a1eea3f3..d588ecdb3 100644 --- a/src/gui/theme.cpp +++ b/src/gui/theme.cpp @@ -1156,15 +1156,15 @@ ImageSet *Theme::getImageSetFromThemeXml(const std::string &name, #define readValue(name) \ info->name = reinterpret_cast(\ - xmlNodeGetContent(infoNode)) + XmlNodeGetContent(infoNode)) #define readIntValue(name) \ info->name = atoi(reinterpret_cast(\ - xmlNodeGetContent(infoNode))) + XmlNodeGetContent(infoNode))) #define readFloatValue(name) \ info->name = static_cast(atof(reinterpret_cast(\ - xmlNodeGetContent(infoNode)))) + XmlNodeGetContent(infoNode)))) ThemeInfo *Theme::loadInfo(const std::string &themeName) { diff --git a/src/gui/windows/questswindow.cpp b/src/gui/windows/questswindow.cpp index 1c23b9400..3bea8577f 100644 --- a/src/gui/windows/questswindow.cpp +++ b/src/gui/windows/questswindow.cpp @@ -228,7 +228,7 @@ void QuestsWindow::loadQuest(const int var, const XmlNodePtr node) if (!xmlTypeEqual(dataNode, XML_ELEMENT_NODE)) continue; const char *const data = reinterpret_cast( - xmlNodeGetContent(dataNode)); + XmlNodeGetContent(dataNode)); if (!data) continue; std::string str = translator->getStr(data); diff --git a/src/gui/windows/serverdialog.cpp b/src/gui/windows/serverdialog.cpp index c61b58f6d..1049085b4 100644 --- a/src/gui/windows/serverdialog.cpp +++ b/src/gui/windows/serverdialog.cpp @@ -530,7 +530,7 @@ void ServerDialog::loadServers(const bool addNew) server.port = defaultPortForServerType(server.type); } } - else if (!subNode->xmlChildrenNode) + else if (!XmlHaveChildContent(subNode)) { continue; } @@ -539,34 +539,28 @@ void ServerDialog::loadServers(const bool addNew) && server.description.empty()) || (!lang.empty() && xmlNameEqual(subNode, description2.c_str()))) { - server.description = reinterpret_cast( - subNode->xmlChildrenNode->content); + server.description = XmlChildContent(subNode); } else if (xmlNameEqual(subNode, "registerurl")) { - server.registerUrl = reinterpret_cast( - subNode->xmlChildrenNode->content); + server.registerUrl = XmlChildContent(subNode); } else if (xmlNameEqual(subNode, "onlineListUrl")) { - server.onlineListUrl = reinterpret_cast( - subNode->xmlChildrenNode->content); + server.onlineListUrl = XmlChildContent(subNode); } else if (xmlNameEqual(subNode, "support")) { - server.supportUrl = reinterpret_cast( - subNode->xmlChildrenNode->content); + server.supportUrl = XmlChildContent(subNode); } else if (xmlNameEqual(subNode, "persistentIp")) { - std::string text = reinterpret_cast( - subNode->xmlChildrenNode->content); + std::string text = XmlChildContent(subNode); server.persistentIp = (text == "1" || text == "true"); } else if (xmlNameEqual(subNode, "updateMirror")) { - server.updateMirrors.push_back(reinterpret_cast( - subNode->xmlChildrenNode->content)); + server.updateMirrors.push_back(XmlChildContent(subNode)); } } diff --git a/src/particle/particle.cpp b/src/particle/particle.cpp index 2555cfb23..f22f11252 100644 --- a/src/particle/particle.cpp +++ b/src/particle/particle.cpp @@ -338,11 +338,8 @@ Particle *Particle::addEffect(const std::string &restrict particleEffectFile, else if ((node = XML::findFirstChildByName(effectChildNode, "image"))) { std::string imageSrc; - if (node && node->xmlChildrenNode) - { - imageSrc = reinterpret_cast( - node->xmlChildrenNode->content); - } + if (node && XmlHaveChildContent(node)) + imageSrc = XmlChildContent(node); if (!imageSrc.empty() && !dyePalettes.empty()) Dye::instantiate(imageSrc, dyePalettes); Image *const img = resourceManager->getImage(imageSrc); @@ -393,11 +390,8 @@ Particle *Particle::addEffect(const std::string &restrict particleEffectFile, else if (xmlNameEqual(emitterNode, "deatheffect")) { std::string deathEffect; - if (node && node->xmlChildrenNode) - { - deathEffect = reinterpret_cast( - emitterNode->xmlChildrenNode->content); - } + if (node && XmlHaveChildContent(node)) + deathEffect = XmlChildContent(emitterNode); char deathEffectConditions = 0x00; if (XML::getBoolProperty(emitterNode, "on-floor", true)) diff --git a/src/particle/particleemitter.cpp b/src/particle/particleemitter.cpp index 7ee3a4252..a512cff94 100644 --- a/src/particle/particleemitter.cpp +++ b/src/particle/particleemitter.cpp @@ -317,10 +317,9 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtrConst emitterNode, } else if (xmlNameEqual(propertyNode, "deatheffect")) { - if (!propertyNode->xmlChildrenNode) + if (!XmlHaveChildContent(propertyNode)) continue; - mDeathEffect = reinterpret_cast( - propertyNode->xmlChildrenNode->content); + mDeathEffect = XmlChildContent(propertyNode); mDeathEffectConditions = 0x00; if (XML::getBoolProperty(propertyNode, "on-floor", true)) { diff --git a/src/resources/animation/simpleanimation.cpp b/src/resources/animation/simpleanimation.cpp index 4d1add3be..1a1d30094 100644 --- a/src/resources/animation/simpleanimation.cpp +++ b/src/resources/animation/simpleanimation.cpp @@ -170,8 +170,7 @@ void SimpleAnimation::initializeAnimation(const XmlNodePtr animationNode, const int y1 = imageset->getHeight() - mapTileSize; // Get animation frames - for (XmlNodePtr frameNode = animationNode->xmlChildrenNode; - frameNode; frameNode = frameNode->next) + for_each_xml_child_node (frameNode, animationNode) { const int delay = XML::getIntProperty( frameNode, "delay", 0, 0, 100000); diff --git a/src/resources/beingcommon.cpp b/src/resources/beingcommon.cpp index 92411a895..999b19980 100644 --- a/src/resources/beingcommon.cpp +++ b/src/resources/beingcommon.cpp @@ -105,12 +105,11 @@ bool BeingCommon::readObjectNodes(XmlNodePtrConst &spriteNode, { if (xmlNameEqual(spriteNode, "sprite")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) return true; SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty( spriteNode, "variant", 0); @@ -119,15 +118,14 @@ bool BeingCommon::readObjectNodes(XmlNodePtrConst &spriteNode, } else if (xmlNameEqual(spriteNode, "sound")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) return true; const std::string event = XML::getProperty( spriteNode, "event", ""); const int delay = XML::getProperty( spriteNode, "delay", 0); - const char *const filename = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + const char *const filename = XmlChildContent(spriteNode); if (event == "hit") { @@ -209,11 +207,10 @@ bool BeingCommon::readObjectNodes(XmlNodePtrConst &spriteNode, } else if (xmlNameEqual(spriteNode, "particlefx")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) return true; - display.particles.push_back(reinterpret_cast( - spriteNode->xmlChildrenNode->content)); + display.particles.push_back(XmlChildContent(spriteNode)); return true; } return false; diff --git a/src/resources/db/avatardb.cpp b/src/resources/db/avatardb.cpp index 783954006..5b377b173 100644 --- a/src/resources/db/avatardb.cpp +++ b/src/resources/db/avatardb.cpp @@ -107,13 +107,11 @@ void AvatarDB::loadXmlFile(const std::string &fileName) { if (xmlNameEqual(spriteNode, "sprite")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); - + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty( spriteNode, "variant", 0); display.sprites.push_back(currentSprite); diff --git a/src/resources/db/deaddb.cpp b/src/resources/db/deaddb.cpp index dcd658f0e..a2a6a6494 100644 --- a/src/resources/db/deaddb.cpp +++ b/src/resources/db/deaddb.cpp @@ -73,7 +73,7 @@ void DeadDB::loadXmlFile(const std::string &fileName) else if (xmlNameEqual(node, "message")) { const char *const data = reinterpret_cast( - xmlNodeGetContent(node)); + XmlNodeGetContent(node)); if (!data || !*data) continue; mMessages.push_back(data); diff --git a/src/resources/db/emotedb.cpp b/src/resources/db/emotedb.cpp index eeaaaf0a2..b2f9a0581 100644 --- a/src/resources/db/emotedb.cpp +++ b/src/resources/db/emotedb.cpp @@ -116,7 +116,7 @@ void EmoteDB::loadXmlFile(const std::string &fileName) for_each_xml_child_node(spriteNode, emoteNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) @@ -124,8 +124,7 @@ void EmoteDB::loadXmlFile(const std::string &fileName) EmoteSprite *const currentSprite = new EmoteSprite; currentSprite->sprite = AnimatedSprite::load( paths.getStringValue("sprites").append(std::string( - reinterpret_cast( - spriteNode->xmlChildrenNode->content))), + XmlChildContent(spriteNode))), XML::getProperty(spriteNode, "variant", 0)); currentSprite->name = XML::langProperty( spriteNode, "name", ""); @@ -134,8 +133,7 @@ void EmoteDB::loadXmlFile(const std::string &fileName) } else if (xmlNameEqual(spriteNode, "particlefx")) { - currentInfo->particles.push_back(reinterpret_cast( - spriteNode->xmlChildrenNode->content)); + currentInfo->particles.push_back(XmlChildContent(spriteNode)); } } mEmoteInfos[id] = currentInfo; @@ -190,7 +188,7 @@ void EmoteDB::loadSpecialXmlFile(const std::string &fileName) for_each_xml_child_node(spriteNode, emoteNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) @@ -198,8 +196,7 @@ void EmoteDB::loadSpecialXmlFile(const std::string &fileName) EmoteSprite *const currentSprite = new EmoteSprite; currentSprite->sprite = AnimatedSprite::load( paths.getStringValue("sprites").append(std::string( - reinterpret_cast( - spriteNode->xmlChildrenNode->content))), + XmlChildContent(spriteNode))), XML::getProperty(spriteNode, "variant", 0)); currentSprite->name = XML::langProperty( spriteNode, "name", ""); @@ -208,8 +205,7 @@ void EmoteDB::loadSpecialXmlFile(const std::string &fileName) } else if (xmlNameEqual(spriteNode, "particlefx")) { - currentInfo->particles.push_back(reinterpret_cast( - spriteNode->xmlChildrenNode->content)); + currentInfo->particles.push_back(XmlChildContent(spriteNode)); } } mEmoteInfos[id] = currentInfo; diff --git a/src/resources/db/horsedb.cpp b/src/resources/db/horsedb.cpp index 011548b1f..27987c657 100644 --- a/src/resources/db/horsedb.cpp +++ b/src/resources/db/horsedb.cpp @@ -42,8 +42,7 @@ namespace { \ SpriteReference *const currentSprite = new SpriteReference; \ currentSprite->sprite = paths.getStringValue("sprites").append( \ - std::string(reinterpret_cast( \ - spriteNode->xmlChildrenNode->content))); \ + std::string(XmlChildContent(spriteNode))); \ currentSprite->variant = XML::getProperty( \ spriteNode, "variant", 0); \ currentInfo->name.push_back(currentSprite); \ @@ -142,7 +141,7 @@ void HorseDB::loadXmlFile(const std::string &fileName) for_each_xml_child_node(spriteNode, horseNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) @@ -162,7 +161,7 @@ static void loadDownSprites(XmlNodePtrConst parentNode, { for_each_xml_child_node(spriteNode, parentNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) @@ -175,7 +174,7 @@ static void loadUpSprites(XmlNodePtrConst parentNode, { for_each_xml_child_node(spriteNode, parentNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) diff --git a/src/resources/db/itemdb.cpp b/src/resources/db/itemdb.cpp index 9a815bed6..6a3766cc6 100644 --- a/src/resources/db/itemdb.cpp +++ b/src/resources/db/itemdb.cpp @@ -545,11 +545,8 @@ void ItemDB::loadXmlFile(const std::string &fileName, int &tagNum) } else if (xmlNameEqual(itemChild, "particlefx")) { - if (itemChild->xmlChildrenNode) - { - display.particles.push_back(reinterpret_cast( - itemChild->xmlChildrenNode->content)); - } + if (XmlHaveChildContent(itemChild)) + display.particles.push_back(XmlChildContent(itemChild)); } else if (xmlNameEqual(itemChild, "sound")) { @@ -844,11 +841,10 @@ static int parseDirectionName(const std::string &name) static void loadSpriteRef(ItemInfo *const itemInfo, const XmlNodePtr node) { const std::string gender = XML::getProperty(node, "gender", "unisex"); - if (!node || !node->xmlChildrenNode) + if (!node || !XmlHaveChildContent(node)) return; - const std::string filename = reinterpret_cast( - node->xmlChildrenNode->content); + const std::string filename = XmlChildContent(node); const int race = XML::getProperty(node, "race", 0); if (gender == "male" || gender == "unisex") @@ -861,11 +857,10 @@ static void loadSpriteRef(ItemInfo *const itemInfo, const XmlNodePtr node) static void loadSoundRef(ItemInfo *const itemInfo, const XmlNodePtr node) { - if (!node || !node->xmlChildrenNode) + if (!node || !XmlHaveChildContent(node)) return; const std::string event = XML::getProperty(node, "event", ""); - const std::string filename = reinterpret_cast( - node->xmlChildrenNode->content); + const std::string filename = XmlChildContent(node); const int delay = XML::getProperty(node, "delay", 0); const std::map::const_iterator @@ -888,21 +883,19 @@ static void loadFloorSprite(SpriteDisplay &display, return; for_each_xml_child_node(spriteNode, floorNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) { SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty(spriteNode, "variant", 0); display.sprites.push_back(currentSprite); } else if (xmlNameEqual(spriteNode, "particlefx")) { - display.particles.push_back(reinterpret_cast( - spriteNode->xmlChildrenNode->content)); + display.particles.push_back(XmlChildContent(spriteNode)); } } } diff --git a/src/resources/db/npcdb.cpp b/src/resources/db/npcdb.cpp index 3362c02c9..1827cd859 100644 --- a/src/resources/db/npcdb.cpp +++ b/src/resources/db/npcdb.cpp @@ -118,23 +118,21 @@ void NPCDB::loadXmlFile(const std::string &fileName) { if (xmlNameEqual(spriteNode, "sprite")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty(spriteNode, "variant", 0); display.sprites.push_back(currentSprite); } else if (xmlNameEqual(spriteNode, "particlefx")) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; - display.particles.push_back(reinterpret_cast( - spriteNode->xmlChildrenNode->content)); + display.particles.push_back(XmlChildContent(spriteNode)); } else if (xmlNameEqual(spriteNode, "menu")) { diff --git a/src/resources/db/petdb.cpp b/src/resources/db/petdb.cpp index 53f34fae8..160306d71 100644 --- a/src/resources/db/petdb.cpp +++ b/src/resources/db/petdb.cpp @@ -153,22 +153,20 @@ void PETDB::loadXmlFile(const std::string &fileName) SpriteDisplay display; for_each_xml_child_node(spriteNode, petNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) { SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty(spriteNode, "variant", 0); display.sprites.push_back(currentSprite); } else if (xmlNameEqual(spriteNode, "particlefx")) { - std::string particlefx = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + std::string particlefx = XmlChildContent(spriteNode); display.particles.push_back(particlefx); } } diff --git a/src/resources/db/skillunitdb.cpp b/src/resources/db/skillunitdb.cpp index d89eca896..30d0b3918 100644 --- a/src/resources/db/skillunitdb.cpp +++ b/src/resources/db/skillunitdb.cpp @@ -111,22 +111,20 @@ void SkillUnitDb::loadXmlFile(const std::string &fileName) SpriteDisplay display; for_each_xml_child_node(spriteNode, skillUnitNode) { - if (!spriteNode->xmlChildrenNode) + if (!XmlHaveChildContent(spriteNode)) continue; if (xmlNameEqual(spriteNode, "sprite")) { SpriteReference *const currentSprite = new SpriteReference; - currentSprite->sprite = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + currentSprite->sprite = XmlChildContent(spriteNode); currentSprite->variant = XML::getProperty(spriteNode, "variant", 0); display.sprites.push_back(currentSprite); } else if (xmlNameEqual(spriteNode, "particlefx")) { - std::string particlefx = reinterpret_cast( - spriteNode->xmlChildrenNode->content); + std::string particlefx = XmlChildContent(spriteNode); display.particles.push_back(particlefx); } } diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 1c9e5af9f..9da094d54 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -614,14 +614,12 @@ bool MapReader::readBase64Layer(const XmlNodePtrConst childNode, } // Read base64 encoded map file - XmlNodePtrConst dataChild = childNode->xmlChildrenNode; - if (!dataChild) + if (!XmlHaveChildContent(childNode)) return true; - const size_t len = strlen( - reinterpret_cast(dataChild->content)) + 1; + const size_t len = strlen(XmlChildContent(childNode)) + 1; unsigned char *charData = new unsigned char[len + 1]; - xmlChar *const xmlChars = xmlNodeGetContent(dataChild); + const char *const xmlChars = XmlChildContent(childNode); const char *charStart = reinterpret_cast(xmlChars); if (!charStart) { @@ -649,7 +647,7 @@ bool MapReader::readBase64Layer(const XmlNodePtrConst childNode, charData))), &binLen); delete [] charData; - xmlFree(xmlChars); +// XmlFree(const_cast(xmlChars)); if (binData) { @@ -710,11 +708,10 @@ bool MapReader::readCsvLayer(const XmlNodePtrConst childNode, if (!map || !childNode) return false; - XmlNodePtrConst dataChild = childNode->xmlChildrenNode; - if (!dataChild) + if (!XmlHaveChildContent(childNode)) return true; - xmlChar *const xmlChars = xmlNodeGetContent(dataChild); + const char *const xmlChars = XmlChildContent(childNode); const char *const data = reinterpret_cast(xmlChars); if (!data) return false; @@ -747,7 +744,7 @@ bool MapReader::readCsvLayer(const XmlNodePtrConst childNode, oldPos = pos + 1; } - xmlFree(xmlChars); + //XmlFree(const_cast(xmlChars)); return true; } diff --git a/src/utils/xml.h b/src/utils/xml.h index 055f93cbf..556455f7b 100644 --- a/src/utils/xml.h +++ b/src/utils/xml.h @@ -21,6 +21,10 @@ #ifndef UTILS_XML_H #define UTILS_XML_H +#ifdef ENABLE_PUGIXML +#include "utils/xml/pugixml.h" +#else // ENABLE_PUGIXML #include "utils/xml/libxml.h" +#endif // ENABLE_PUGIXML #endif // UTILS_XML_H diff --git a/src/utils/xml/libxml.h b/src/utils/xml/libxml.h index a783c09c2..6d8c35cab 100644 --- a/src/utils/xml/libxml.h +++ b/src/utils/xml/libxml.h @@ -49,10 +49,26 @@ reinterpret_cast(name)) #define XmlTextWriterStartElement(writer, name) \ xmlTextWriterStartElement(writer, reinterpret_cast(name)) +#define XmlTextWriterEndElement(writer) xmlTextWriterEndElement(writer) #define XmlTextWriterWriteAttribute(writer, name, content) \ xmlTextWriterWriteAttribute(writer, \ reinterpret_cast(name), \ reinterpret_cast(content)) +#define XmlNodeGetContent(node) xmlNodeGetContent(node) +#define XmlNewTextWriterFilename(name, flags) \ + xmlNewTextWriterFilename(name, flags) +#define XmlTextWriterSetIndent(writer, flags) \ + xmlTextWriterSetIndent(writer, flags) +#define XmlTextWriterStartDocument(writer, p1, p2, p3) \ + xmlTextWriterStartDocument(writer, p1, p2, p3) +#define XmlTextWriterEndDocument(writer) xmlTextWriterEndDocument(writer) +#define XmlFreeTextWriter(writer) xmlFreeTextWriter(writer) +#define XmlHaveChildContent(node) ((node)->xmlChildrenNode != nullptr && \ + (node)->xmlChildrenNode->content != nullptr) +#define XmlChildContent(node) reinterpret_cast(\ + (node)->xmlChildrenNode->content) +#define XmlFree(ptr) xmlFree(ptr) +#define XmlNodeDefault nullptr /** * XML helper functions. diff --git a/src/utils/xml/pugixml.cpp b/src/utils/xml/pugixml.cpp new file mode 100644 index 000000000..7bff15f6c --- /dev/null +++ b/src/utils/xml/pugixml.cpp @@ -0,0 +1,335 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2015 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef ENABLE_PUGIXML + +#include "utils/xml/pugixml.h" + +#include "logger.h" + +#include "utils/delete2.h" +#include "utils/fuzzer.h" +#include "utils/physfstools.h" +#include "utils/stringutils.h" + +#include "utils/translation/podict.h" + +#include "debug.h" + +namespace +{ + bool valid = false; +} // namespace + +namespace XML +{ + static void showErrorStatus(pugi::xml_parse_result &result) + { +// switch (result.status) +// { +// case pugi::status_ok: +// break; +// case pugi::status_file_not_found: +// logger->log("xml error: %s", result.description()); +// break; +// } + logger->log("xml error: %s", result.description()); + } + + Document::Document(const std::string &filename, + const UseResman useResman, + const SkipError skipError) : + mDoc(), + mRoot(), + mData(nullptr), + mIsValid(false) + { +#ifdef USE_FUZZER + if (Fuzzer::conditionTerminate(filename.c_str())) + return; +#endif + BLOCK_START("XML::Document::Document") + int size = 0; + char *data = nullptr; + valid = true; + if (useResman == UseResman_true) + { + data = static_cast(PhysFs::loadFile( + filename.c_str(), size)); + } + else + { + std::ifstream file; + file.open(filename.c_str(), std::ios::in); + + if (file.is_open()) + { + // Get length of file + file.seekg(0, std::ios::end); + size = static_cast(file.tellg()); + if (size < 0) + { + logger->log("Error loading XML file %s", filename.c_str()); + } + else + { + file.seekg(0, std::ios::beg); + data = static_cast(malloc(size)); + file.read(data, size); + } + file.close(); + } + else + { + logger->log("Error loading XML file %s", filename.c_str()); + } + } + + if (data) + { + // +++ use other pugi::parse_* flags + pugi::xml_parse_result result = mDoc.load_buffer_inplace(data, + size, + pugi::parse_default, + pugi::encoding_utf8); + if (result.status != pugi::status_ok) + { + showErrorStatus(result); + free(data); + } + else + { + mData = data; + } + + mRoot = mDoc.first_child(); +// if (!mDoc) +// logger->log("Error parsing XML file %s", filename.c_str()); + } + else if (skipError == SkipError_false) + { + logger->log("Error loading %s", filename.c_str()); + } + mIsValid = valid; + BLOCK_END("XML::Document::Document") + } + + Document::Document(const char *const data, const int size) : + mDoc(), + mRoot(), + mData(nullptr), + mIsValid(true) + { + if (!data) + return; + + char *buf = static_cast(calloc(size + 1, 1)); + strncpy(buf, data, size); + buf[size] = 0; + pugi::xml_parse_result result = mDoc.load_buffer_inplace(buf, + size, + pugi::parse_default, + pugi::encoding_utf8); + if (result.status != pugi::status_ok) + { + showErrorStatus(result); + free(buf); + } + else + { + mRoot = mDoc.first_child(); + mData = buf; + } + } + + Document::~Document() + { + free(mData); + mData = nullptr; +// if (mDoc) +// xmlFreeDoc(mDoc); + } + + XmlNodePtr Document::rootNode() + { + return mDoc.first_child(); + } + + int getProperty(const XmlNodePtr node, + const char *const name, + int def) + { + int &ret = def; + + if (!node) + return ret; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + ret = atoi(attr.value()); + + return ret; + } + + int getIntProperty(const XmlNodePtr node, + const char *const name, + int def, + const int min, + const int max) + { + int &ret = def; + + if (!node) + return ret; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + ret = atoi(attr.value()); + + if (ret < min) + ret = min; + else if (ret > max) + ret = max; + return ret; + } + + double getFloatProperty(const XmlNodePtr node, + const char *const name, + double def) + { + double &ret = def; + + if (!node) + return ret; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + ret = atof(attr.value()); + + return ret; + } + + std::string getProperty(const XmlNodePtr node, + const char *const name, + const std::string &def) + { + if (!node) + return def; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + return attr.value(); + + return def; + } + + std::string langProperty(const XmlNodePtr node, + const char *const name, + const std::string &def) + { + std::string str = getProperty(node, name, def); + if (!translator) + return str; + + return translator->getStr(str); + } + + bool getBoolProperty(const XmlNodePtr node, + const char *const name, + const bool def) + { + if (!node) + return def; + const pugi::xml_attribute &attr = node.attribute(name); + if (!attr.empty()) + { + std::string val = attr.value(); + if (val == "true") + return true; + if (val == "false") + return false; + } + + return def; + } + + XmlNodePtr findFirstChildByName(const XmlNodePtrConst parent, + const char *const name) + { + if (!parent || !name) + return pugi::xml_node(); + for_each_xml_child_node(child, parent) + { + if (!strcmp(child.name(), name)) + return child; + } + + return pugi::xml_node(); + } + + // Initialize libxml2 and check for potential ABI mismatches between + // compiled version and the shared library actually used. + void initXML() + { +// xmlInitParser(); +// LIBXML_TEST_VERSION; + + // Suppress libxml2 error messages +// xmlSetGenericErrorFunc(nullptr, &xmlErrorLogger); + } + + // Shutdown libxml + void cleanupXML() + { +// xmlCleanupParser(); + } + + bool Document::validateXml(const std::string &fileName) + { + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_file(fileName.c_str(), + pugi::parse_default, + pugi::encoding_utf8); + + if (result.status != pugi::status_ok) + { + showErrorStatus(result); + return false; + } + + std::ifstream file; + file.open(fileName.c_str(), std::ios::in); + if (!file.is_open()) + { + file.close(); + return false; + } + char line[101]; + if (!file.getline(line, 100)) + return false; + file.close(); + + const std::string str = line; + if (!strStartWith(str, ". + */ + +#ifndef UTILS_XML_PUGIXML_H +#define UTILS_XML_PUGIXML_H + +#ifdef ENABLE_PUGIXML + +#include "enums/simpletypes/skiperror.h" +#include "enums/simpletypes/useresman.h" + +#include + +#include + +#include "localconsts.h" + +#define XML_ELEMENT_NODE pugi::node_element + +#define XmlNodePtr pugi::xml_node +#define XmlNodePtrConst pugi::xml_node +#define xmlNameEqual(node, str) !strcmp((node).name(), str) +#define xmlTypeEqual(node, typ) ((node).type() == (typ)) +#define XmlHasProp(node, name) (!((node).attribute(name).empty())) +#define XmlHaveChildContent(node) ((node).child_value() != nullptr && \ + *(node).child_value()) +#define XmlChildContent(node) (node).child_value() +#define xmlChar char +#define XmlFree(ptr) +#define XmlNodeDefault pugi::xml_node() + +// +++ need impliment get context +#define XmlNodeGetContent(node) (node).child_value() + +// +++ need impliment writing code +#define XmlTextWriterPtr pugi::xml_writer* +#define XmlTextWriterStartElement(writer, name) +#define XmlTextWriterEndElement(writer) +#define XmlTextWriterWriteAttribute(writer, name, content) +#define XmlNewTextWriterFilename(name, flags) nullptr; +#define XmlTextWriterSetIndent(writer, flags) +#define XmlTextWriterStartDocument(writer, p1, p2, p3) +#define XmlTextWriterEndDocument(writer) +#define XmlFreeTextWriter(writer) + +/** + * XML helper functions. + */ +namespace XML +{ + /** + * A helper class for parsing an XML document, which also cleans it up + * again (RAII). + */ + class Document final + { + public: + /** + * Constructor that attempts to load the given file through the + * resource manager. Logs errors. + */ + Document(const std::string &filename, + const UseResman useResman, + const SkipError skipError); + + /** + * Constructor that attempts to load an XML document from memory. + * Does not log errors. + * + * @param data the string to parse as XML + * @param size the length of the string in bytes + */ + Document(const char *const data, const int size); + + A_DELETE_COPY(Document) + + /** + * Destructor. Frees the loaded XML file. + */ + ~Document(); + + /** + * Returns the root node of the document (or NULL if there was a + * load error). + */ + XmlNodePtr rootNode() A_WARN_UNUSED; + + bool isLoaded() const + { return !mDoc.empty(); } + + bool isValid() const + { return mIsValid; } + + static bool validateXml(const std::string &fileName); + + private: + pugi::xml_document mDoc; + pugi::xml_node mRoot; + char *mData; + bool mIsValid; + }; + + /** + * Gets an floating point property from an XmlNodePtr. + */ + double getFloatProperty(const XmlNodePtr node, + const char *const name, + double def) A_WARN_UNUSED; + + /** + * Gets an integer property from an XmlNodePtr. + */ + int getProperty(const XmlNodePtr node, + const char *const name, + int def) A_WARN_UNUSED; + + /** + * Gets an integer property from an XmlNodePtr. + */ + int getIntProperty(const XmlNodePtr node, + const char *const name, + int def, + const int min, + const int max) A_WARN_UNUSED; + + /** + * Gets a string property from an XmlNodePtr. + */ + std::string getProperty(const XmlNodePtr node, + const char *const name, + const std::string &def) A_WARN_UNUSED; + + /** + * Gets a translated string property from an XmlNodePtr. + */ + std::string langProperty(const XmlNodePtr node, + const char *const name, + const std::string &def) A_WARN_UNUSED; + + /** + * Gets a boolean property from an XmlNodePtr. + */ + bool getBoolProperty(const XmlNodePtr node, + const char *const name, + const bool def) A_WARN_UNUSED; + + /** + * Finds the first child node with the given name + */ + XmlNodePtr findFirstChildByName(const XmlNodePtrConst parent, + const char *const name) A_WARN_UNUSED; + + void initXML(); + + void cleanupXML(); +} // namespace XML + +#define for_each_xml_child_node(var, parent) \ + for (pugi::xml_node var = parent.first_child(); var; var = var.next_sibling()) + +#endif // ENABLE_PUGIXML +#endif // UTILS_XML_PUGIXML_H diff --git a/src/utils/xml_unittest.cc b/src/utils/xml_unittest.cc index ba89a3b70..24a025e4a 100644 --- a/src/utils/xml_unittest.cc +++ b/src/utils/xml_unittest.cc @@ -51,13 +51,15 @@ TEST_CASE("xml doc") REQUIRE(xmlNameEqual(doc.rootNode(), "skinset") == true); REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false); REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); +// REQUIRE(XmlHaveChildContent(doc.rootNode()) == true); } SECTION("load2") { - const std::string xml = "" - ""; - XML::Document doc(xml.c_str(), xml.size()); + const char *xml = "" + "" + "this is test"; + XML::Document doc(xml, strlen(xml)); REQUIRE(doc.isLoaded() == true); REQUIRE(doc.isValid() == true); REQUIRE(doc.rootNode() != nullptr); @@ -66,6 +68,8 @@ TEST_CASE("xml doc") REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false); REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false); + logger->log("content: '%s'", doc.rootNode().first_child().child_value()); + REQUIRE(XmlHaveChildContent(doc.rootNode()) == false); } SECTION("load3") @@ -82,6 +86,24 @@ TEST_CASE("xml doc") REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false); REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false); + REQUIRE(XmlHaveChildContent(doc.rootNode()) == false); + } + + SECTION("load4") + { + const char *xml = "" + "this is test"; + XML::Document doc(xml, strlen(xml)); + REQUIRE(doc.isLoaded() == true); + REQUIRE(doc.isValid() == true); + REQUIRE(doc.rootNode() != nullptr); + REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true); + REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false); + REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true); + REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false); + REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false); + REQUIRE(XmlHaveChildContent(doc.rootNode()) == true); + REQUIRE(!strcmp(XmlChildContent(doc.rootNode()), "this is test")); } SECTION("properties") @@ -101,7 +123,8 @@ TEST_CASE("xml doc") SkipError_false); const XmlNodePtr rootNode = doc.rootNode(); - XmlNodePtr node = nullptr; +// REQUIRE(XmlHaveChildContent(rootNode) == true); + XmlNodePtr node = XmlNodeDefault; for_each_xml_child_node(widgetNode, rootNode) { node = widgetNode; @@ -111,6 +134,7 @@ TEST_CASE("xml doc") REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "widget") == true); +// REQUIRE(XmlHaveChildContent(node) == true); for_each_xml_child_node(optionNode, node) { node = optionNode; @@ -120,6 +144,7 @@ TEST_CASE("xml doc") REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "option") == true); + REQUIRE(XmlHaveChildContent(node) == false); REQUIRE(XmlHasProp(node, "name") == true); REQUIRE(XmlHasProp(node, "value") == true); REQUIRE(XmlHasProp(node, "option123") == false); @@ -142,10 +167,12 @@ TEST_CASE("xml doc") REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "widget") == true); +// REQUIRE(XmlHaveChildContent(node) == true); node = XML::findFirstChildByName(node, "option"); REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "option") == true); + REQUIRE(XmlHaveChildContent(node) == false); REQUIRE(XmlHasProp(node, "name") == true); REQUIRE(XmlHasProp(node, "value") == true); REQUIRE(XmlHasProp(node, "option123") == false); @@ -163,15 +190,17 @@ TEST_CASE("xml doc") SECTION("child2") { - const std::string xml = "" + const char *xml = "" ""; - XML::Document doc(xml.c_str(), xml.size()); + XML::Document doc(xml, strlen(xml)); const XmlNodePtr rootNode = doc.rootNode(); + REQUIRE(XmlHaveChildContent(rootNode) == false); XmlNodePtr node = XML::findFirstChildByName(rootNode, "data"); REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "data") == true); + REQUIRE(XmlHaveChildContent(node) == false); REQUIRE(XmlHasProp(node, "option1") == true); REQUIRE(XmlHasProp(node, "option123") == false); REQUIRE(XML::getBoolProperty(node, "option1", true) == false); @@ -189,10 +218,12 @@ TEST_CASE("xml doc") "option3=\"10.5\"/>"; XML::Document doc(xml.c_str(), xml.size()); const XmlNodePtr rootNode = doc.rootNode(); +// REQUIRE(XmlHaveChildContent(rootNode) == true); XmlNodePtr node = XML::findFirstChildByName(rootNode, "data"); REQUIRE(node != nullptr); REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true); REQUIRE(xmlNameEqual(node, "data") == true); + REQUIRE(XmlHaveChildContent(node) == false); REQUIRE(XmlHasProp(node, "option1") == true); REQUIRE(XmlHasProp(node, "option123") == false); REQUIRE(XML::getBoolProperty(node, "option1", true) == false); -- cgit v1.2.3-60-g2f50