diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-05-28 20:31:09 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-05-28 20:31:09 +0300 |
commit | bb87f2911b63eaf80e49d689bf52ecf2042ae098 (patch) | |
tree | 0778087134ec3c5a2370d417bcadb64a1dc6a9bc /src/resources | |
parent | 4415cb66734e67dfcdf8924d354107d27fb70fee (diff) | |
download | manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.gz manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.bz2 manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.tar.xz manaplus-bb87f2911b63eaf80e49d689bf52ecf2042ae098.zip |
Move from resourcemanager functions related to files into other files.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/db/palettedb.cpp | 4 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 173 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 76 | ||||
-rw-r--r-- | src/resources/wallpaper.cpp | 10 |
4 files changed, 9 insertions, 254 deletions
diff --git a/src/resources/db/palettedb.cpp b/src/resources/db/palettedb.cpp index 09140d55b..1d940f0c1 100644 --- a/src/resources/db/palettedb.cpp +++ b/src/resources/db/palettedb.cpp @@ -22,6 +22,8 @@ #include "logger.h" +#include "utils/files.h" + #include "resources/dyecolor.h" #include "resources/resourcemanager.h" @@ -48,7 +50,7 @@ void PaletteDB::loadPalette() { mLoaded = true; StringVect lines; - ResourceManager::loadTextFile("palette.gpl", lines); + Files::loadTextFile("palette.gpl", lines); StringVectCIter it = lines.begin(); if (it == lines.end()) { diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 476149eb3..707834d1d 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -48,8 +48,6 @@ #include "utils/timer.h" #include <SDL_image.h> -#include <dirent.h> -#include <fstream> #include <sys/time.h> @@ -391,52 +389,6 @@ void ResourceManager::searchAndRemoveArchives(const std::string &restrict path, PhysFs::freeList(list); } -bool ResourceManager::mkdir(const std::string &path) const -{ - return static_cast<bool>(PhysFs::mkdir(path.c_str())); -} - -bool ResourceManager::exists(const std::string &path) const -{ - return PhysFs::exists(path.c_str()); -} - -bool ResourceManager::existsLocal(const std::string &path) -{ - bool flg(false); - std::fstream file; - file.open(path.c_str(), std::ios::in); - if (file.is_open()) - flg = true; - file.close(); - return flg; -} - -bool ResourceManager::isDirectory(const std::string &path) const -{ - return PhysFs::isDirectory(path.c_str()); -} - -std::string ResourceManager::getPath(const std::string &file) -{ - // get the real path to the file - const char *const tmp = PhysFs::getRealDir(file.c_str()); - std::string path; - - // if the file is not in the search path, then its nullptr - if (tmp) - { - path = std::string(tmp).append(dirSeparator).append(file); - } - else - { - // if not found in search path return the default path - path = getPackageDir().append(dirSeparator).append(file); - } - - return path; -} - bool ResourceManager::addResource(const std::string &idPath, Resource *const resource) { @@ -958,115 +910,6 @@ void ResourceManager::deleteInstance() delete2(instance); } -void *ResourceManager::loadFile(const std::string &fileName, int &fileSize) -{ - // Attempt to open the specified file using PhysicsFS - PHYSFS_file *const file = PhysFs::openRead(fileName.c_str()); - - if (!file) - { - logger->log("Warning: Failed to load %s: %s", - fileName.c_str(), PHYSFS_getLastError()); - return nullptr; - } - - logger->log("Loaded %s/%s", PhysFs::getRealDir(fileName.c_str()), - fileName.c_str()); - - fileSize = static_cast<int>(PHYSFS_fileLength(file)); - // Allocate memory and load the file - void *const buffer = calloc(fileSize, 1); - PHYSFS_read(file, buffer, 1, fileSize); - PHYSFS_close(file); - - return buffer; -} - -bool ResourceManager::copyFile(const std::string &restrict src, - const std::string &restrict dst) -{ - PHYSFS_file *const srcFile = PhysFs::openRead(src.c_str()); - if (!srcFile) - { - logger->log("Read error: %s", PHYSFS_getLastError()); - return false; - } - PHYSFS_file *const dstFile = PhysFs::openWrite(dst.c_str()); - if (!dstFile) - { - logger->log("Write error: %s", PHYSFS_getLastError()); - PHYSFS_close(srcFile); - return false; - } - - const int fileSize = static_cast<const int>(PHYSFS_fileLength(srcFile)); - char *buf = new char[static_cast<size_t>(fileSize)]; - PHYSFS_read(srcFile, buf, 1, fileSize); - PHYSFS_write(dstFile, buf, 1, fileSize); - - PHYSFS_close(srcFile); - PHYSFS_close(dstFile); - delete [] buf; - return true; -} - -bool ResourceManager::loadTextFile(const std::string &fileName, - StringVect &lines) -{ - int contentsLength; - char *fileContents = static_cast<char*>( - loadFile(fileName, contentsLength)); - - if (!fileContents) - { - logger->log("Couldn't load text file: %s", fileName.c_str()); - return false; - } - - std::istringstream iss(std::string(fileContents, contentsLength)); - std::string line; - - while (getline(iss, line)) - lines.push_back(line); - - free(fileContents); - return true; -} - -bool ResourceManager::loadTextFileLocal(const std::string &fileName, - StringVect &lines) -{ - std::ifstream file; - char line[501]; - - file.open(fileName.c_str(), std::ios::in); - - if (!file.is_open()) - { - logger->log("Couldn't load text file: %s", fileName.c_str()); - return false; - } - - while (file.getline(line, 500)) - lines.push_back(line); - - return true; -} - -void ResourceManager::saveTextFile(std::string path, - const std::string &restrict name, - const std::string &restrict text) -{ - if (!mkdir_r(path.c_str())) - { - std::ofstream file; - file.open((path.append("/").append(name)).c_str(), std::ios::out); - if (file.is_open()) - file << text << std::endl; - file.close(); - } -} - SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) const { if (SDL_RWops *const rw = MPHYSFSRWOPS_openRead(filename.c_str())) @@ -1133,22 +976,6 @@ Image *ResourceManager::getRescaled(const Image *const image, return img; } -void ResourceManager::deleteFilesInDirectory(std::string path) -{ - path += "/"; - struct dirent *next_file = nullptr; - DIR *const dir = opendir(path.c_str()); - - while ((next_file = readdir(dir))) - { - const std::string file = next_file->d_name; - if (file != "." && file != "..") - remove((path + file).c_str()); - } - if (dir) - closedir(dir); -} - void ResourceManager::clearCache() { cleanProtected(); diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 0e14e61bb..824fbddd5 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -106,36 +106,6 @@ class ResourceManager final const std::string &restrict ext) const; /** - * Creates a directory in the write path - */ - bool mkdir(const std::string &path) const; - - /** - * Checks whether the given file or directory exists in the search path - * (PhysFS) - */ - bool exists(const std::string &path) const A_WARN_UNUSED; - - /** - * Checks whether the given file or directory exists - */ - static bool existsLocal(const std::string &path) A_WARN_UNUSED; - - /** - * Checks whether the given path is a directory. - */ - bool isDirectory(const std::string &path) const A_WARN_UNUSED; - - /** - * Returns the real path to a file. Note that this method will always - * return a path, it does not check whether the file exists. - * - * @param file The file to get the real path to. - * @return The real path. - */ - static std::string getPath(const std::string &file) A_WARN_UNUSED; - - /** * Creates a resource and adds it to the resource map. * * @param idPath The resource identifier path. @@ -173,18 +143,6 @@ class ResourceManager final bool addResource(const std::string &idPath, Resource *const resource); /** - * Copies a file from one place to another (useful for extracting - * raw files from a zip archive, for example) - * - * @param src Source file name - * @param dst Destination file name - * @return true on success, false on failure. An error message should be - * in the log file. - */ - static bool copyFile(const std::string &restrict src, - const std::string &restrict dst); - - /** * Convenience wrapper around ResourceManager::get for loading * images. */ @@ -246,37 +204,9 @@ class ResourceManager final */ void moveToDeleted(Resource *const res); - /** - * Allocates data into a buffer pointer for raw data loading. The - * returned data is expected to be freed using <code>free()</code>. - * - * @param fileName The name of the file to be loaded. - * @param fileSize The size of the file that was loaded. - * - * @return An allocated byte array containing the data that was loaded, - * or <code>NULL</code> on fail. - */ - static void *loadFile(const std::string &fileName, - int &fileSize) A_WARN_UNUSED; - - /** - * Retrieves the contents of a text file (PhysFS). - */ - static bool loadTextFile(const std::string &fileName, - StringVect &lines); - - /** - * Retrieves the contents of a text file. - */ - static bool loadTextFileLocal(const std::string &fileName, - StringVect &lines); - - static void saveTextFile(std::string path, - const std::string &restrict name, - const std::string &restrict text); - Image *getRescaled(const Image *const image, - const int width, const int height) A_WARN_UNUSED; + const int width, + const int height) A_WARN_UNUSED; /** * Loads the given filename as an SDL surface. The returned surface is @@ -325,8 +255,6 @@ class ResourceManager final void clearCache(); - static void deleteFilesInDirectory(std::string path); - private: /** * Deletes the resource after logging a cleanup message. diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp index 4674035cf..979cf9844 100644 --- a/src/resources/wallpaper.cpp +++ b/src/resources/wallpaper.cpp @@ -46,26 +46,24 @@ static std::string wallpaperFile; // Search for the wallpaper path values sequentially.. static void initDefaultWallpaperPaths() { - const ResourceManager *const resman = ResourceManager::getInstance(); - // Init the path wallpaperPath = branding.getStringValue("wallpapersPath"); - if (wallpaperPath.empty() || !resman->isDirectory(wallpaperPath)) + if (wallpaperPath.empty() || !PhysFs::isDirectory(wallpaperPath.c_str())) wallpaperPath = paths.getValue("wallpapers", ""); - if (wallpaperPath.empty() || !resman->isDirectory(wallpaperPath)) + if (wallpaperPath.empty() || !PhysFs::isDirectory(wallpaperPath.c_str())) wallpaperPath = "graphics/images/"; // Init the default file wallpaperFile = branding.getStringValue("wallpaperFile"); - if (!wallpaperFile.empty() && !resman->isDirectory(wallpaperFile)) + if (!wallpaperFile.empty() && !PhysFs::isDirectory(wallpaperFile.c_str())) return; else wallpaperFile = paths.getValue("wallpaperFile", ""); - if (wallpaperFile.empty() || resman->isDirectory(wallpaperFile)) + if (wallpaperFile.empty() || PhysFs::isDirectory(wallpaperFile.c_str())) wallpaperFile = "login_wallpaper.png"; } |