Merge pull request #7600 from pandamicro/FileUtils_FILE_API

Refactor #5580: Improve searchFullPathForFilename and comments
This commit is contained in:
minggo 2014-07-28 11:34:13 +08:00
commit 9efbd6e4ef
2 changed files with 19 additions and 28 deletions

View File

@ -888,39 +888,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<FileUtils*>(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<FileUtils*>(this)->getPathForFilename(newFilename, *resolutionIt, *searchIt);
if (!fullpath.empty())
{
// Using the filename passed in as key.
const_cast<FileUtils*>(this)->_fullPathCache.insert(std::make_pair(filename, fullpath));
return fullpath;
}
}
return path;
}
return "";
}
bool FileUtils::isFileExist(const std::string& filename) const

View File

@ -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;