summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorPrzemysław Grzywacz <nexather@gmail.com>2013-04-28 16:15:20 +0200
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-04-29 22:43:54 +0200
commit647cebfdf11c2482e5b4bbaad4aa411cdd4bb2cc (patch)
treeed63cbbde80f2d39175cac4544217ec7d6cdf8e6 /src/common
parenta8defa22243de756842a78fe36a4b76091915987 (diff)
downloadmanaserv-647cebfdf11c2482e5b4bbaad4aa411cdd4bb2cc.tar.gz
manaserv-647cebfdf11c2482e5b4bbaad4aa411cdd4bb2cc.tar.xz
manaserv-647cebfdf11c2482e5b4bbaad4aa411cdd4bb2cc.zip
Single xml solution
Mana-mantis: #506.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/defines.h8
-rw-r--r--src/common/resourcemanager.cpp107
-rw-r--r--src/common/resourcemanager.h4
3 files changed, 112 insertions, 7 deletions
diff --git a/src/common/defines.h b/src/common/defines.h
index 4ba77a1..40be064 100644
--- a/src/common/defines.h
+++ b/src/common/defines.h
@@ -30,16 +30,10 @@
#define WORLD_TICK_MS 100
// Files
+#define DEFAULT_SETTINGS_FILE "settings.xml"
#define DEFAULT_MAPSDB_FILE "maps.xml"
#define DEFAULT_ITEMSDB_FILE "items.xml"
-#define DEFAULT_EQUIPDB_FILE "equip.xml"
-#define DEFAULT_SKILLSDB_FILE "skills.xml"
-#define DEFAULT_ATTRIBUTEDB_FILE "attributes.xml"
-#define DEFAULT_MONSTERSDB_FILE "monsters.xml"
-#define DEFAULT_STATUSDB_FILE "status-effects.xml"
#define DEFAULT_PERMISSION_FILE "permissions.xml"
-#define DEFAULT_SPECIALSDB_FILE "specials.xml"
-#define DEFAULT_EMOTESDB_FILE "emotes.xml"
/**
* Exit value codes are thrown back at servers exit to reflect their exit state.
diff --git a/src/common/resourcemanager.cpp b/src/common/resourcemanager.cpp
index 92a8591..82675c7 100644
--- a/src/common/resourcemanager.cpp
+++ b/src/common/resourcemanager.cpp
@@ -27,6 +27,7 @@
#include <sys/stat.h>
#include <cstdlib>
#include <cstring>
+#include <vector>
#ifdef _WIN32
#include <io.h>
@@ -133,3 +134,109 @@ ResourceManager::splittedPath ResourceManager::splitFileNameAndPath(
return splittedFilePath;
}
+
+/**
+ * Join two path elements into one.
+ *
+ * This function helps build relative paths.
+ *
+ * Examples:
+ *
+ * /foo + bar = /foo/bar
+ * /foo/ + bar = /foo/bar
+ * /foo + /bar = /bar
+ *
+ * This will work for PhysFS paths. Windows style paths (prefixed with drive letters) won't work.
+ *
+ * @return Joined paths or path2 if path2 was an absolute path.
+ */
+std::string ResourceManager::joinPaths(const std::string& path1, const std::string& path2)
+{
+ if (path2.empty())
+ return path1;
+
+ if (path1.empty())
+ return path2;
+
+ // check if path2 is an absolute path that cannot be joined
+ if (path2[0] == '/' || path2[0] == '\\')
+ return path2;
+
+ char p1end = path1[path1.size()-1];
+ if (p1end == '/' || p1end == '\\')
+ {
+ return path1 + path2;
+ }
+ else
+ {
+ return path1 + "/" + path2;
+ }
+}
+
+/**
+ * Removes relative elements from the path.
+ */
+std::string ResourceManager::cleanPath(const std::string& path)
+{
+ size_t prev, cur;
+ std::string part, result;
+ std::vector<std::string> pathStack;
+
+ prev = 0;
+ while (true)
+ {
+ cur = path.find_first_of("/\\", prev);
+ if (cur == std::string::npos)
+ {
+ // FIXME add everything from prev to the end
+ pathStack.push_back(path.substr(prev));
+ break;
+ }
+
+ part = path.substr(prev, cur - prev);
+ if (part == "..")
+ {
+ // go back one level
+ if (!pathStack.empty())
+ {
+ pathStack.pop_back();
+ }
+ }
+ else if (part == ".")
+ {
+ // do nothing
+ }
+ else if (part == "")
+ {
+ if (pathStack.empty() && cur == 0)
+ {
+ // handle first empty match before the root slash
+ pathStack.push_back(std::string());
+ }
+ else
+ {
+ // empty match in the middle of the path should be ignored
+ }
+ }
+ else
+ {
+ // normal path element
+ pathStack.push_back(part);
+ }
+
+ cur++;
+ prev = cur;
+ }
+
+ // join the pathStack into a normal path
+ unsigned int i = 0;
+ for (i = 0; i < pathStack.size(); i++)
+ {
+ result += pathStack[i];
+ if (i < pathStack.size() - 1) {
+ result += "/";
+ }
+ }
+
+ return result;
+}
diff --git a/src/common/resourcemanager.h b/src/common/resourcemanager.h
index 569428d..eea701a 100644
--- a/src/common/resourcemanager.h
+++ b/src/common/resourcemanager.h
@@ -67,6 +67,10 @@ namespace ResourceManager
* and the file name alone.
*/
splittedPath splitFileNameAndPath(const std::string &fullFilePath);
+
+ std::string joinPaths(const std::string& path1, const std::string& path2);
+
+ std::string cleanPath(const std::string& path);
}
#endif