summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-10-14 21:19:07 +0200
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-10-14 21:19:07 +0200
commit1d9374215d8e6ad1160596dcd52fca201aeb2195 (patch)
tree6f4a5e280e9953ec6de8909a93e90d4d83f9fe3c /src/common
parentdd703d614620846176e832eda0f37b5e20704c81 (diff)
downloadmanaserv-1d9374215d8e6ad1160596dcd52fca201aeb2195.tar.gz
manaserv-1d9374215d8e6ad1160596dcd52fca201aeb2195.tar.bz2
manaserv-1d9374215d8e6ad1160596dcd52fca201aeb2195.tar.xz
manaserv-1d9374215d8e6ad1160596dcd52fca201aeb2195.zip
Hopefully fix the logging rotation this time.
Added an extension to the ResMan::exist() function in order to get file existence even not in search path. Reviewed-by: CodyMartin.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/resourcemanager.cpp52
-rw-r--r--src/common/resourcemanager.hpp16
2 files changed, 66 insertions, 2 deletions
diff --git a/src/common/resourcemanager.cpp b/src/common/resourcemanager.cpp
index 5d2a8cf4..9c52a8e7 100644
--- a/src/common/resourcemanager.cpp
+++ b/src/common/resourcemanager.cpp
@@ -24,6 +24,7 @@
#include "utils/logger.h"
+#include <sys/stat.h>
#include <cstdlib>
#include <cstring>
@@ -53,8 +54,42 @@ void ResourceManager::initialize()
PHYSFS_addToSearchPath(serverDataPath.c_str(), 1);
}
-bool ResourceManager::exists(const std::string &path)
+/**
+ * This function tries to check if a file exists
+ * based on the existence of stats about it.
+ * Because simply trying to open it and check for failure
+ * is a bad thing, as you don't know if you weren't able
+ * to open it because it doesn't exist or because
+ * you don't have the right to.
+ */
+static bool fileExists(const std::string& filename)
{
+ struct stat fileStat;
+ bool fileExist = false;
+ int statValue = -1;
+
+ // Try to get the file attributes
+ statValue = stat(filename.c_str(), &fileStat);
+
+ if(statValue == 0)
+ {
+ // The file attributes exists, so the file does, too.
+ fileExist = true;
+ }
+ else
+ {
+ // Impossible to get the file attributes.
+ fileExist = false;
+ }
+
+ return fileExist;
+}
+
+bool ResourceManager::exists(const std::string &path, bool lookInSearchPath)
+{
+ if (!lookInSearchPath)
+ return fileExists(path);
+
return PHYSFS_exists(path.c_str());
}
@@ -100,3 +135,18 @@ char *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
buffer[fileSize] = 0;
return buffer;
}
+
+ResourceManager::splittedPath ResourceManager::splitFileNameAndPath(
+ const std::string &fullFilePath)
+{
+ // We'll reversed-search for '/' or'\' and extract the substrings
+ // corresponding to the filename and the path separately.
+ size_t slashPos = fullFilePath.find_last_of("/\\");
+
+ ResourceManager::splittedPath splittedFilePath;
+ // Note the last slash is kept in the path name.
+ splittedFilePath.path = fullFilePath.substr(0, slashPos + 1);
+ splittedFilePath.file = fullFilePath.substr(slashPos + 1);
+
+ return splittedFilePath;
+}
diff --git a/src/common/resourcemanager.hpp b/src/common/resourcemanager.hpp
index ffbefd10..3d722a46 100644
--- a/src/common/resourcemanager.hpp
+++ b/src/common/resourcemanager.hpp
@@ -25,6 +25,13 @@
namespace ResourceManager
{
+ // A structure retaining the path and file names separately.
+ struct splittedPath
+ {
+ std::string path;
+ std::string file;
+ };
+
/**
* Searches for zip files and adds them to PhysFS search path.
*/
@@ -33,7 +40,7 @@ namespace ResourceManager
/**
* Checks whether the given file or directory exists in the search path
*/
- bool exists(const std::string &path);
+ bool exists(const std::string &path, bool lookInSearchPath = true);
/**
* Returns the real file-system path of the resource with the given
@@ -53,6 +60,13 @@ namespace ResourceManager
* @note The array contains an extra \0 character at position fileSize.
*/
char *loadFile(const std::string &fileName, int &fileSize);
+
+ /**
+ * Returns the filePath sub-part corresponding to the filename only.
+ * @return splittedPath: the file path ending with '/' or '\'
+ * and the file name alone.
+ */
+ splittedPath splitFileNameAndPath(const std::string &fullFilePath);
}
#endif