From 823bd437d896e346903c5ba12a5cc85fe57bfa50 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 28 Jul 2014 11:14:11 +0800 Subject: [PATCH] Refactor #5580: Improve searchFullPathForFilename and comments --- cocos/platform/CCFileUtils.cpp | 30 +++++------------------------- cocos/platform/CCFileUtils.h | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index 181df15699..9b4a9c0953 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -880,39 +880,19 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir std::string FileUtils::searchFullPathForFilename(const std::string& filename) const { - // If filename is absolute path, we don't need to consider 'search paths' and 'resolution orders'. if (isAbsolutePath(filename)) { return filename; } - - // Already Cached ? - auto cacheIter = _fullPathCache.find(filename); - if( cacheIter != _fullPathCache.end() ) + std::string path = const_cast(this)->fullPathForFilename(filename); + if (0 == path.compare(filename)) { - return cacheIter->second; + return ""; } - - // Get the new file name. - const std::string newFilename( getNewFilename(filename) ); - - std::string fullpath; - - for (auto searchIt = _searchPathArray.cbegin(); searchIt != _searchPathArray.cend(); ++searchIt) + else { - for (auto resolutionIt = _searchResolutionsOrderArray.cbegin(); resolutionIt != _searchResolutionsOrderArray.cend(); ++resolutionIt) - { - fullpath = const_cast(this)->getPathForFilename(newFilename, *resolutionIt, *searchIt); - - if (!fullpath.empty()) - { - // Using the filename passed in as key. - const_cast(this)->_fullPathCache.insert(std::make_pair(filename, fullpath)); - return fullpath; - } - } + return path; } - return ""; } bool FileUtils::isFileExist(const std::string& filename) const diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index c90a889733..7419dcea23 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -407,12 +407,16 @@ protected: virtual std::string getNewFilename(const std::string &filename) const; /** - * Checks whether file exists without considering search paths and resolution orders. + * Checks whether a file exists without considering search paths and resolution orders. + * @param The file (with absolute path) to look up for + * @return Returns true if the file found at the given absolute path, otherwise returns false */ virtual bool isFileExistInternal(const std::string& filename) const = 0; /** - * Checks whether file exists without considering search paths and resolution orders. + * Checks whether a directory exists without considering search paths and resolution orders. + * @param The directory (with absolute path) to look up for + * @return Returns true if the directory found at the given absolute path, otherwise returns false */ virtual bool isDirectoryExistInternal(const std::string& dirPath) const; @@ -438,7 +442,14 @@ protected: */ virtual std::string getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename); - + /** + * Returns the fullpath for a given filename. + * This is an alternative for fullPathForFilename, there are two main differences: + * First, it returns empty string instead of the original filename when no file found for the given name. + * Secondly, it's a const function. + * @param filename The file name to look up for + * @return The full path for the file, if not found, the return value will be an empty string + */ virtual std::string searchFullPathForFilename(const std::string& filename) const;