summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-09-13 18:31:30 +0300
committerAndrei Karas <akaras@inbox.ru>2016-09-13 18:31:30 +0300
commitdca3de26978bf0a9d7ac9df8524c90b85f8920da (patch)
tree0d575e647028c1f3d75e5ee1e4d93603bcda1783
parent0dbd25bacb312932e343f154001531b6be84a6ac (diff)
downloadplus-dca3de26978bf0a9d7ac9df8524c90b85f8920da.tar.gz
plus-dca3de26978bf0a9d7ac9df8524c90b85f8920da.tar.bz2
plus-dca3de26978bf0a9d7ac9df8524c90b85f8920da.tar.xz
plus-dca3de26978bf0a9d7ac9df8524c90b85f8920da.zip
Add into xmlutils function for read xml file into int map.
-rw-r--r--data/test/CMakeLists.txt1
-rw-r--r--data/test/Makefile.am1
-rw-r--r--data/test/testintmap.xml11
-rw-r--r--src/utils/xmlutils.cpp54
-rw-r--r--src/utils/xmlutils.h9
-rw-r--r--src/utils/xmlutils_unittest.cc29
6 files changed, 105 insertions, 0 deletions
diff --git a/data/test/CMakeLists.txt b/data/test/CMakeLists.txt
index e300301d8..4a3dbfb63 100644
--- a/data/test/CMakeLists.txt
+++ b/data/test/CMakeLists.txt
@@ -11,6 +11,7 @@ SET(FILES
quests.xml
serverlistplus.xml
simplefile.txt
+ testintmap.xml
units.xml
)
diff --git a/data/test/Makefile.am b/data/test/Makefile.am
index 5000dbadb..8a3f82be9 100644
--- a/data/test/Makefile.am
+++ b/data/test/Makefile.am
@@ -13,6 +13,7 @@ test_DATA = \
quests.xml \
serverlistplus.xml \
simplefile.txt \
+ testintmap.xml \
units.xml
EXTRA_DIST = \
diff --git a/data/test/testintmap.xml b/data/test/testintmap.xml
new file mode 100644
index 000000000..342591f9c
--- /dev/null
+++ b/data/test/testintmap.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Author: 4144
+Copyright (C) 2016 ManaPlus developers -->
+
+<tests>
+ <sub>
+ <item id="1" val="2"/>
+ <item id="10" val="20"/>
+ <item id="3"/>
+ </sub>
+</tests>
diff --git a/src/utils/xmlutils.cpp b/src/utils/xmlutils.cpp
index fdcc272ec..5cc14f517 100644
--- a/src/utils/xmlutils.cpp
+++ b/src/utils/xmlutils.cpp
@@ -130,3 +130,57 @@ void readXmlStringMap(const std::string &fileName,
}
}
}
+
+void readXmlIntMap(const std::string &fileName,
+ const std::string &rootName,
+ const std::string &sectionName,
+ const std::string &itemName,
+ const std::string &attributeKeyName,
+ const std::string &attributeValueName,
+ std::map<int32_t, int32_t> &arr,
+ const SkipError skipError)
+{
+ XML::Document doc(fileName, UseResman_true, skipError);
+ const XmlNodePtrConst rootNode = doc.rootNode();
+
+ if (!rootNode || !xmlNameEqual(rootNode, rootName.c_str()))
+ {
+ logger->log("Error while loading %s!", fileName.c_str());
+ return;
+ }
+
+ for_each_xml_child_node(sectionNode, rootNode)
+ {
+ if (!xmlNameEqual(sectionNode, sectionName.c_str()))
+ continue;
+ for_each_xml_child_node(childNode, sectionNode)
+ {
+ if (xmlNameEqual(childNode, itemName.c_str()))
+ {
+ const std::string key = XML::getProperty(childNode,
+ attributeKeyName.c_str(), "");
+ if (key.empty())
+ continue;
+ const int32_t val = XML::getProperty(childNode,
+ attributeValueName.c_str(), 0);
+ arr[atoi(key.c_str())] = val;
+ }
+ else if (xmlNameEqual(childNode, "include"))
+ {
+ const std::string name = XML::getProperty(
+ childNode, "name", "");
+ if (!name.empty())
+ {
+ readXmlIntMap(name,
+ rootName,
+ sectionName,
+ itemName,
+ attributeKeyName,
+ attributeValueName,
+ arr,
+ skipError);
+ }
+ }
+ }
+ }
+}
diff --git a/src/utils/xmlutils.h b/src/utils/xmlutils.h
index 928519267..d80758848 100644
--- a/src/utils/xmlutils.h
+++ b/src/utils/xmlutils.h
@@ -44,4 +44,13 @@ void readXmlStringMap(const std::string &fileName,
std::map<std::string, std::string> &arr,
const SkipError skipError);
+void readXmlIntMap(const std::string &fileName,
+ const std::string &rootName,
+ const std::string &sectionName,
+ const std::string &itemName,
+ const std::string &attributeKeyName,
+ const std::string &attributeValueName,
+ std::map<int32_t, int32_t> &arr,
+ const SkipError skipError);
+
#endif // UTILS_XMLUTILS_H
diff --git a/src/utils/xmlutils_unittest.cc b/src/utils/xmlutils_unittest.cc
index 3fc27c598..55e6397d2 100644
--- a/src/utils/xmlutils_unittest.cc
+++ b/src/utils/xmlutils_unittest.cc
@@ -89,3 +89,32 @@ TEST_CASE("xmlutils readXmlStringMap 1")
REQUIRE(arr["Metal"] == "26");
ResourceManager::deleteInstance();
}
+
+TEST_CASE("xmlutils readXmlIntMap 1")
+{
+ client = new Client;
+ PHYSFS_init("manaplus");
+ dirSeparator = "/";
+ XML::initXML();
+ logger = new Logger();
+ ResourceManager::init();
+ resourceManager->addToSearchPath("data/test", Append_false);
+ resourceManager->addToSearchPath("../data/test", Append_false);
+
+ std::map<int32_t, int32_t> arr;
+
+ readXmlIntMap("testintmap.xml",
+ "tests",
+ "sub",
+ "item",
+ "id",
+ "val",
+ arr,
+ SkipError_false);
+
+ REQUIRE(arr.size() == 3);
+ REQUIRE(arr[1] == 2);
+ REQUIRE(arr[10] == 20);
+ REQUIRE(arr[3] == 0);
+ ResourceManager::deleteInstance();
+}