Refactoring some logic of CCFileUtils.

This commit is contained in:
James Chen 2013-01-26 14:19:14 +08:00
parent a28a5907c3
commit 3328cfd6a4
6 changed files with 200 additions and 250 deletions

View File

@ -88,10 +88,13 @@ const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
if (pszFileName == NULL || pszFileName[0] == '\0' || pszFileName[0] == '/') {
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
// Return directly if it's an absolute path.
if (pszFileName[0] == '/')
{
return pszFileName;
}
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end())
@ -136,14 +139,16 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
}
if (bFound)
{
s_fullPathCache.insert(std::pair<std::string, std::string>(newFilename, fullpath));
// Using the filename passed in as key.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
CCLOG("Returning path: %s", fullpath.c_str());
return fullpath;
}
}
}
return newFilename;
// The file wasn't found, return the file name passed in.
return pszFileName;
}
const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)

View File

@ -32,6 +32,7 @@ NS_CC_BEGIN;
#define MAX_PATH 256
static CCFileUtils *s_pFileUtils = 0;
static std::map<std::string, std::string> s_fullPathCache;
CCFileUtils *CCFileUtils::sharedFileUtils()
{
@ -70,15 +71,13 @@ bool CCFileUtils::init()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{
std::string ret;
const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath();
std::string ret = CCApplication::sharedApplication()->getResourceRootPath();;
ret = resourceRootPath;
if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
{
ret += "/";
@ -112,60 +111,56 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
return ret;
}
const char* CCFileUtils::fullPathForFilename(const char* pszFileName)
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
if (pszFileName && pszFileName[0] == '/')
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
// Return directly if it's absolute path.
if (pszFileName[0] == '/')
{
return pszFileName;
}
bool bFound = false;
CCString* pRet = CCString::create("");
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
{
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
if (access(fullpath.c_str(), F_OK) != -1)
{
pRet->m_sString = fullpath;
bFound = true;
break;
}
}
if (bFound)
{
break;
}
}
}while(false);
if (!bFound)
{ // Can't find the file, return the relative path.
pRet->m_sString = newFileName;
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
return pRet->getCString();
// in Lookup Filename dictionary ?
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
if (access(fullpath.c_str(), F_OK) != -1)
{
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
return fullpath;
}
}
}
// The file wasn't found, return the file name passed in.
return pszFileName;
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
return fullPathForFilename(pszRelativePath);
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
}
@ -178,27 +173,6 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const
return pRet->m_sString.c_str();
}
void CCFileUtils::loadFilenameLookupDictionaryFromFile(const char* filename)
{
const char* pFullPath = this->fullPathForFilename(filename);
if (pFullPath)
{
CCDictionary* pDict = CCDictionary::createWithContentsOfFile(filename);
if (pDict)
{
CCDictionary* pMetadata = (CCDictionary*)pDict->objectForKey("metadata");
int version = ((CCString*)pMetadata->objectForKey("version"))->intValue();
if (version != 1)
{
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %s", (long)version, filename);
return;
}
setFilenameLookupDictionary((CCDictionary*)pDict->objectForKey("filenames"));
}
}
}
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
unsigned char *buffer = 0;

View File

@ -245,8 +245,6 @@ std::string CCFileUtils::getNewFilename(const char* pszFileName)
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{
std::string ret = "";
std::string file = filename;
std::string file_path = "";
size_t pos = filename.find_last_of("/");
@ -270,56 +268,60 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
inDirectory:[NSString stringWithUTF8String:path.c_str()]];
if (fullpath != nil)
{
ret = [fullpath UTF8String];
if (fullpath != nil) {
return [fullpath UTF8String];
}
return ret;
// Return empty string when file wasn't found.
return "";
}
std::string CCFileUtils::fullPathForFilename(const char* filename)
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
CCAssert(filename != NULL, "CCFileUtils: Invalid path");
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
NSString *relPath = [NSString stringWithUTF8String:pszFileName];
// Return directly if it's an absolute path.
if ([relPath isAbsolutePath]) {
return pszFileName;
}
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(filename);
if (cacheIter != s_fullPathCache.end())
{
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
//CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
std::string fullpath = "";
NSString *relPath = [NSString stringWithUTF8String:filename];
// only if it is not an absolute path
if( ! [relPath isAbsolutePath] ) {
// in Lookup Filename dictionary ?
std::string newfilename = this->getNewFilename(pszFileName);
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
std::string newfilename = this->getNewFilename(filename);
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
fullpath = this->getPathForFilename(newfilename, pResourceDirectory->getCString(), pSearchPath->getCString());
if (fullpath.length() > 0)
{
s_fullPathCache.insert(std::pair<std::string, std::string>(filename, fullpath));
return fullpath;
}
fullpath = this->getPathForFilename(newfilename, pResourceDirectory->getCString(), pSearchPath->getCString());
if (fullpath.length() > 0)
{
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
return fullpath;
}
}
}
return filename;
// The file wasn't found, return the file name passed in.
return pszFileName;
}
void CCFileUtils::loadFilenameLookupDictionaryFromFile(const char* filename)

View File

@ -21,6 +21,7 @@ using namespace std;
NS_CC_BEGIN
static CCFileUtils* s_pFileUtils = NULL;
static std::map<std::string, std::string> s_fullPathCache;
CCFileUtils* CCFileUtils::sharedFileUtils()
{
@ -58,15 +59,13 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{
std::string ret;
const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath();
std::string ret = CCApplication::sharedApplication()->getResourceRootPath();
ret = resourceRootPath;
if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
{
ret += "/";
@ -100,61 +99,58 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
return ret;
}
const char* CCFileUtils::fullPathForFilename(const char* pszFileName)
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
if (pszFileName && pszFileName[0] == '/')
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
// Return directly if it's an absolute path.
if (pszFileName[0] == '/')
{
return pszFileName;
}
bool bFound = false;
CCString* pRet = CCString::create("");
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
{
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
struct stat sts;
if (stat(fullpath.c_str(), &sts) != -1)
{
pRet->m_sString = fullpath;
bFound = true;
break;
}
}
if (bFound)
{
break;
}
}
}while(false);
if (!bFound)
{ // Can't find the file, return the relative path.
pRet->m_sString = newFileName;
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
return pRet->getCString();
// in Lookup Filename dictionary ?
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
struct stat sts;
if (stat(fullpath.c_str(), &sts) != -1)
{
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
return fullpath;
}
}
}
// The file wasn't found, return the file name passed in.
return pszFileName;
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
return fullPathForFilename(pszRelativePath);
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
}
@ -167,27 +163,6 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const
return pRet->m_sString.c_str();
}
void CCFileUtils::loadFilenameLookupDictionaryFromFile(const char* filename)
{
const char* pFullPath = this->fullPathForFilename(filename);
if (pFullPath)
{
CCDictionary* pDict = CCDictionary::createWithContentsOfFile(filename);
if (pDict)
{
CCDictionary* pMetadata = (CCDictionary*)pDict->objectForKey("metadata");
int version = ((CCString*)pMetadata->objectForKey("version"))->intValue();
if (version != 1)
{
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %s", (long)version, filename);
return;
}
setFilenameLookupDictionary((CCDictionary*)pDict->objectForKey("filenames"));
}
}
}
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
unsigned char * pData = 0;

View File

@ -38,6 +38,7 @@ NS_CC_BEGIN;
static CCFileUtils* s_pFileUtils = NULL;
static std::map<std::string, std::string> s_fullPathCache;
CCFileUtils* CCFileUtils::sharedFileUtils()
{
@ -75,7 +76,7 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
@ -115,63 +116,56 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
return ret;
}
const char* CCFileUtils::fullPathForFilename(const char* pszFileName)
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
// TODO HOW ARE WE SUPPOSED TO WRITE BACK TO THE "ignore" REFERENCE?
IwAssert(GAME, pszFileName);
if (pszFileName && pszFileName[0] == '/')
// Return directly if it's an absolute path.
if (pszFileName[0] == '/')
{
return pszFileName;
}
bool bFound = false;
CCString* pRet = CCString::create("");
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
// check if file or path exist
if (s3eFileCheckExists(fullpath.c_str()) == S3E_TRUE)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
if (s3eFileCheckExists(fullpath.c_str()) == S3E_TRUE)
{
pRet->m_sString = fullpath;
bFound = true;
break;
}
}
if (bFound)
{
break;
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
return fullpath;
}
}
}while(false);
if (!bFound)
{ // Can't find the file, return the relative path.
pRet->m_sString = newFileName;
}
return pRet->getCString();
// The file wasn't found, return the file name passed in.
return pszFileName;
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
return fullPathForFilename(pszRelativePath);
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
}

View File

@ -33,6 +33,7 @@ NS_CC_BEGIN
// record the resource path
static char s_pszResourcePath[MAX_PATH] = {0};
static std::map<std::string, std::string> s_fullPathCache;
static void _CheckPath()
{
@ -74,7 +75,7 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
bool CCFileUtils::init()
@ -96,17 +97,10 @@ const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{
std::string ret;
const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath();
if (filename.length() > 0
&& ('/' == filename[0] || '\\' == filename[0]))
{
// path start with '/' or '\', is absolute path without driver name
char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0};
ret = szDriver;
ret += "/";
}
else if (resourceRootPath.length() > 0)
if (resourceRootPath.length() > 0)
{
ret = resourceRootPath;
if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
@ -149,46 +143,52 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
{
bool bFound = false;
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
// Return directly if it's an absolute path.
if (strlen(pszFileName) > 3
&& pszFileName[0] >= 'a' && pszFileName[0] <= 'z'
&& pszFileName[0] >= 'A' && pszFileName[0] <= 'Z'
&& (pszFileName[1] == ':')
&& (pszFileName[2] == '\\' || pszFileName[2] == '/')
)
{
CCLOG("Probably invoking fullPathForFilename recursively, return the full path: %s", pszFileName);
return pszFileName;
}
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
if ((newFileName.length() > 1 && newFileName[1] == ':'))
{
// path start with "x:", is absolute path, return directly
return newFileName;
}
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{
CCString* pSearchPath = (CCString*)pSearchObj;
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
if (GetFileAttributesA(fullpath.c_str()) != -1)
{
bFound = true;
break;
}
}
if (bFound)
if (GetFileAttributesA(fullpath.c_str()) != -1)
{
break;
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
return fullpath;
}
}
}
}while(false);
return bFound ? fullpath : newFileName;
// The file wasn't found, return the file name passed in.
return pszFileName;
}
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)