在3中,我们又分析了几个函数,在这一篇中我们继续分析其他一些函数。
1、android平台
unsigned char* CCFileUtilsAndroid::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
return doGetFileData(pszFileName, pszMode, pSize, false);
}
-->>
//pszFileName 文件名
//pszMode 读取模式,只有对绝对路径有用,参数就是C API的类型
//pSize 读出的字节大小
//forAsync 同步还是异步
unsigned char* CCFileUtilsAndroid::doGetFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize, bool forAsync)
{
unsigned char * pData = 0;
if ((! pszFileName) || (! pszMode) || 0 == strlen(pszFileName))
{
return 0;
}
//先获取文件的全路径
string fullPath = fullPathForFilename(pszFileName);
if (fullPath[0] != '/')
{
//如果以"assets/"开头,即文件在安装包里,那么通过压缩文件类从压缩包中读取,
//其实android的安装包就是压缩包。
if (forAsync)
{
pData = s_pZipFile->getFileData(fullPath.c_str(), pSize, s_pZipFile->_dataThread);
}
else
{
pData = s_pZipFile->getFileData(fullPath.c_str(), pSize);
}
}
else
{
do
{
//如果是绝对路径,则通过C API读取。
// read rrom other path than user set it
//CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName);
FILE *fp = fopen(fullPath.c_str(), pszMode);
CC_BREAK_IF(!fp);
unsigned long size;
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,SEEK_SET);
pData = new unsigned char[size];
size = fread(pData,sizeof(unsigned char), size,fp);
fclose(fp);
if (pSize)
{
*pSize = size;
}
} while (0);
}
if (! pData)
{
std::string msg = "Get data from file(";
msg.append(pszFileName).append(") failed!");
CCLOG("%s", msg.c_str());
}
return pData;
}
总结:到此为止,cocos2dx-2.X android平台的文件读出我已经通过源码的形式分析完了,做个记录,
以防忘记。
cocos2dx-2.x CCFileUtils文件管理类分析(4)
最新推荐文章于 2017-02-15 18:51:34 发布