diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-10-14 21:19:07 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-10-14 21:19:07 +0200 |
commit | 1d9374215d8e6ad1160596dcd52fca201aeb2195 (patch) | |
tree | 6f4a5e280e9953ec6de8909a93e90d4d83f9fe3c /src/common/resourcemanager.cpp | |
parent | dd703d614620846176e832eda0f37b5e20704c81 (diff) | |
download | manaserv-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/resourcemanager.cpp')
-rw-r--r-- | src/common/resourcemanager.cpp | 52 |
1 files changed, 51 insertions, 1 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; +} |