issue #1687: CCFileUtils::isFileExist now support relative path.

This commit is contained in:
James Chen 2013-02-01 22:19:58 +08:00
parent c362f7e439
commit e25c79f1ec
15 changed files with 73 additions and 173 deletions

View File

@ -255,16 +255,20 @@ public:
/**
* Checks whether a file exists.
*
* @param strFullpathOfFile The full path of file.
* @note If a relative path was passed in, it will be inserted a default root path at the beginning.
* @param strFilePath The path of the file, it could be a relative or absolute path.
* @return true if the file exists, otherwise it will return false.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile) = 0;
virtual bool isFileExist(const std::string& strFilePath) = 0;
/**
* Checks whether the path is an absolute path.
*
* @note On Android, if the parameter passed in is relative to "assets/", this method will treat it as an absolute path.
* Also on Blackberry, path starts with "app/native/Resources/" is treated as an absolute path.
*
* @param strPath The path that needs to be checked.
* @return true if it's an absolute path, otherwise it will return false.
*/
virtual bool isAbsolutePath(const std::string& strPath);
@ -305,8 +309,7 @@ protected:
* @param filename The file name.
* @param resolutionDirectory The resolution directory.
* @param searchPath The search path.
* @param retFullPath The return value of full path.
* @return Whether the file exists.
* @return The full path of the file. It will return an empty string if the full path of the file doesn't exist.
*/
virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath);
@ -335,11 +338,11 @@ protected:
virtual CCArray* createCCArrayWithContentsOfFile(const std::string& filename);
/** Dictionary used to lookup filenames based on a key.
It is used internally by the following methods:
std::string fullPathForFilename(const char*);
@since v2.1
* It is used internally by the following methods:
*
* std::string fullPathForFilename(const char*);
*
* @since v2.1
*/
CCDictionary* m_pFilenameLookupDict;

View File

@ -60,17 +60,27 @@ bool CCFileUtilsAndroid::init()
return CCFileUtils::init();
}
bool CCFileUtilsAndroid::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsAndroid::isFileExist(const std::string& strFilePath)
{
bool bFound = false;
// Check whether file exists in apk.
if (s_pZipFile->fileExists(strFullpathOfFile))
if (strFilePath[0] != '/')
{
bFound = true;
}
std::string strPath = strFilePath;
if (strPath.find(m_strDefaultResRootPath) != 0)
{// Didn't find "assets/" at the beginning of the path, adding it.
strPath.insert(0, m_strDefaultResRootPath);
}
if (s_pZipFile->fileExists(strPath))
{
bFound = true;
}
}
else
{
FILE *fp = fopen(strFullpathOfFile.c_str(), "r");
FILE *fp = fopen(strFilePath.c_str(), "r");
if(fp)
{
bFound = true;
@ -105,7 +115,7 @@ unsigned char* CCFileUtilsAndroid::getFileData(const char* pszFileName, const ch
if (pszFileName[0] != '/')
{
CCLOG("GETTING FILE RELATIVE DATA: %s", pszFileName);
//CCLOG("GETTING FILE RELATIVE DATA: %s", pszFileName);
string fullPath = fullPathForFilename(pszFileName);
pData = s_pZipFile->getFileData(fullPath.c_str(), pSize);
}
@ -114,7 +124,7 @@ unsigned char* CCFileUtilsAndroid::getFileData(const char* pszFileName, const ch
do
{
// read rrom other path than user set it
CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName);
//CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName);
FILE *fp = fopen(pszFileName, pszMode);
CC_BREAK_IF(!fp);

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -47,28 +45,12 @@ class CC_DLL CCFileUtilsAndroid : public CCFileUtils
CCFileUtilsAndroid();
public:
virtual ~CCFileUtilsAndroid();
/* override funtions */
bool init();
virtual unsigned char* getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize);
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
/**
* Checks whether the path is an absolute path.
*
* @param strPath The path that needs to be checked.
*/
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
};

View File

@ -51,9 +51,18 @@ bool CCFileUtilsBlackberry::isAbsolutePath(const std::string& strPath)
return false;
}
bool CCFileUtilsBlackberry::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsBlackberry::isFileExist(const std::string& strFilePath)
{
return access(strFullpathOfFile.c_str(), F_OK) != -1 ? true : false;
std::string strPath = strFilePath;
if (strPath[0] != '/')
{ // Not absolute path, add the default root path at the beginning.
if (strPath.find(m_strDefaultResRootPath) != 0)
{// Didn't find "assets/" at the beginning of the path, adding it.
strPath.insert(0, m_strDefaultResRootPath);
}
}
return access(strPath.c_str(), F_OK) != -1 ? true : false;
}
NS_CC_END

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -48,20 +46,8 @@ class CC_DLL CCFileUtilsBlackberry : public CCFileUtils
public:
/* override funtions */
bool init();
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
};

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -45,37 +43,9 @@ class CC_DLL CCFileUtilsIOS : public CCFileUtils
{
public:
/* override funtions */
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
/**
* Checks whether the path is an absolute path.
*
* @param strPath The path that needs to be checked.
*/
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
/**
* Gets full path for the directory and the filename.
*
* @note Only iOS and Mac need to override this method since they are using
* `[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]` to make a full path.
* Other platforms will use the default implementation of this method.
* @param strDirectory The directory contains the file we are looking for.
* @param strFilename The name of the file.
* @return The full path of the file, if the file can't be found, it will return an empty string.
*/
virtual std::string getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename);
virtual CCDictionary* createCCDictionaryWithContentsOfFile(const std::string& filename);

View File

@ -62,13 +62,13 @@ std::string CCFileUtilsIOS::getWriteablePath()
return strRet;
}
bool CCFileUtilsIOS::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsIOS::isFileExist(const std::string& strFilePath)
{
bool bRet = false;
if (strFullpathOfFile[0] != '/')
if (strFilePath[0] != '/')
{
std::string path = strFullpathOfFile;
std::string path = strFilePath;
std::string file;
size_t pos = path.find_last_of("/");
if (pos != std::string::npos)
@ -86,7 +86,7 @@ bool CCFileUtilsIOS::isFileExist(const std::string& strFullpathOfFile)
else
{
// Search path is an absolute path.
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFullpathOfFile.c_str()]]) {
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFilePath.c_str()]]) {
bRet = true;
}
}

View File

@ -53,10 +53,16 @@ string CCFileUtilsLinux::getWriteablePath()
return m_strDefaultResRootPath;
}
bool CCFileUtilsLinux::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsLinux::isFileExist(const std::string& strFilePath)
{
std::string strPath = strFilePath;
if (!isAbsolutePath(strPath))
{ // Not absolute path, add the default root path at the beginning.
strPath.insert(0, m_strDefaultResRootPath);
}
struct stat sts;
return (stat(strFullpathOfFile.c_str(), &sts) != -1) ? true : false;
return (stat(strPath.c_str(), &sts) != -1) ? true : false;
}
NS_CC_END

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -48,19 +46,8 @@ class CC_DLL CCFileUtilsLinux : public CCFileUtils
public:
/* override funtions */
bool init();
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
virtual bool isFileExist(const std::string& strFilePath);
};
// end of platform group

View File

@ -32,9 +32,6 @@
#include "ccTypeInfo.h"
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -45,30 +42,9 @@ class CC_DLL CCFileUtilsMac : public CCFileUtils
{
public:
/* override funtions */
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
/**
* Checks whether the path is an absolute path.
*
* @param strPath The path that needs to be checked.
*/
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
/**
*
*/
virtual std::string getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename);
virtual CCDictionary* createCCDictionaryWithContentsOfFile(const std::string& filename);

View File

@ -59,13 +59,13 @@ std::string CCFileUtilsMac::getWriteablePath()
return strRet;
}
bool CCFileUtilsMac::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsMac::isFileExist(const std::string& strFilePath)
{
bool bRet = false;
if (strFullpathOfFile[0] != '/')
if (strFilePath[0] != '/')
{
std::string path = strFullpathOfFile;
std::string path = strFilePath;
std::string file;
size_t pos = path.find_last_of("/");
if (pos != std::string::npos)
@ -83,7 +83,7 @@ bool CCFileUtilsMac::isFileExist(const std::string& strFullpathOfFile)
else
{
// Search path is an absolute path.
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFullpathOfFile.c_str()]]) {
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFilePath.c_str()]]) {
bRet = true;
}
}

View File

@ -28,9 +28,9 @@ string CCFileUtilsMarmalade::getWriteablePath()
return std::string("ram://");
}
bool CCFileUtilsMarmalade::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsMarmalade::isFileExist(const std::string& strFilePath)
{
return s3eFileCheckExists(strFullpathOfFile.c_str()) == S3E_TRUE ? true : false;
return s3eFileCheckExists(strFilePath.c_str()) == S3E_TRUE ? true : false;
}
unsigned char* CCFileUtilsMarmalade::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -47,22 +45,9 @@ class CC_DLL CCFileUtilsMarmalade : public CCFileUtils
CCFileUtilsMarmalade();
public:
/* override funtions */
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
virtual unsigned char* getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize);
};

View File

@ -64,9 +64,14 @@ bool CCFileUtilsWin32::init()
return CCFileUtils::init();
}
bool CCFileUtilsWin32::isFileExist(const std::string& strFullpathOfFile)
bool CCFileUtilsWin32::isFileExist(const std::string& strFilePath)
{
return GetFileAttributesA(strFullpathOfFile.c_str()) != -1 ? true : false;
std::string strPath = strFilePath;
if (!isAbsolutePath(strPath))
{ // Not absolute path, add the default root path at the beginning.
strPath.insert(0, m_strDefaultResRootPath);
}
return GetFileAttributesA(strPath.c_str()) != -1 ? true : false;
}
bool CCFileUtilsWin32::isAbsolutePath(const std::string& strPath)

View File

@ -33,8 +33,6 @@
NS_CC_BEGIN
class CCDictionary;
class CCArray;
/**
* @addtogroup platform
* @{
@ -48,25 +46,8 @@ class CC_DLL CCFileUtilsWin32 : public CCFileUtils
public:
/* override funtions */
bool init();
/**
* Gets the writeable path
* @return The path that can write/read file
*/
virtual std::string getWriteablePath();
/**
* Checks whether file exists.
*
* @param strFullpathOfFile The full path of file.
*/
virtual bool isFileExist(const std::string& strFullpathOfFile);
/**
* Checks whether the path is an absolute path.
*
* @param strPath The path that needs to be checked.
*/
virtual bool isFileExist(const std::string& strFilePath);
virtual bool isAbsolutePath(const std::string& strPath);
};