2010-11-03 11:01:17 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-26 17:24:37 +08:00
|
|
|
NS_CC_BEGIN;
|
2010-11-03 11:01:17 +08:00
|
|
|
|
2011-03-26 17:24:37 +08:00
|
|
|
#include "CCCommon.h"
|
2011-07-25 14:12:30 +08:00
|
|
|
#include "jni/SystemInfoJni.h"
|
2010-11-03 11:01:17 +08:00
|
|
|
|
2012-03-20 17:27:06 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2010-11-03 11:01:17 +08:00
|
|
|
// record the resource path
|
2011-08-04 17:19:58 +08:00
|
|
|
static string s_strResourcePath = "";
|
2010-12-06 09:51:21 +08:00
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
/*
|
|
|
|
* This function is implemented for jni to set apk path.
|
|
|
|
*/
|
2011-08-04 17:19:58 +08:00
|
|
|
void CCFileUtils::setResourcePath(const char* pszResourcePath)
|
2010-12-06 09:51:21 +08:00
|
|
|
{
|
2011-08-04 17:19:58 +08:00
|
|
|
CCAssert(pszResourcePath != NULL, "[FileUtils setRelativePath] -- wrong relative path");
|
2010-12-06 09:51:21 +08:00
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
string tmp(pszResourcePath);
|
|
|
|
|
|
|
|
if ((! pszResourcePath) || tmp.find(".apk") == string::npos)
|
2010-12-06 09:51:21 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-08-04 17:19:58 +08:00
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
s_strResourcePath = pszResourcePath;
|
2010-12-06 09:51:21 +08:00
|
|
|
}
|
2010-11-03 11:01:17 +08:00
|
|
|
|
2012-04-08 14:52:47 +08:00
|
|
|
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath,
|
|
|
|
ccResolutionType *pResolutionType)
|
2010-11-03 11:01:17 +08:00
|
|
|
{
|
2011-08-25 11:24:31 +08:00
|
|
|
return pszRelativePath;
|
2010-11-03 11:01:17 +08:00
|
|
|
}
|
2010-12-06 09:51:21 +08:00
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
|
2010-11-03 11:01:17 +08:00
|
|
|
{
|
2010-12-06 09:51:21 +08:00
|
|
|
std::string relativeFile = pszRelativeFile;
|
2011-03-08 13:49:58 +08:00
|
|
|
CCString *pRet = new CCString();
|
2010-11-03 11:01:17 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
|
|
|
|
pRet->m_sString += pszFilename;
|
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
2010-12-06 09:51:21 +08:00
|
|
|
|
2011-03-26 17:24:37 +08:00
|
|
|
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
|
2011-08-25 11:24:31 +08:00
|
|
|
{
|
2011-08-04 17:19:58 +08:00
|
|
|
unsigned char * pData = 0;
|
2011-08-25 11:24:31 +08:00
|
|
|
string fullPath(pszFileName);
|
2011-08-04 17:19:58 +08:00
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
if ((! pszFileName) || (! pszMode))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pszFileName[0] != '/')
|
2011-08-04 17:19:58 +08:00
|
|
|
{
|
|
|
|
// read from apk
|
|
|
|
fullPath.insert(0, "assets/");
|
|
|
|
pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// read rrom other path than user set it
|
|
|
|
FILE *fp = fopen(pszFileName, pszMode);
|
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
2011-08-25 11:24:31 +08:00
|
|
|
unsigned long size;
|
2011-08-04 17:19:58 +08:00
|
|
|
fseek(fp,0,SEEK_END);
|
2011-08-25 11:24:31 +08:00
|
|
|
size = ftell(fp);
|
2011-08-04 17:19:58 +08:00
|
|
|
fseek(fp,0,SEEK_SET);
|
2012-03-05 09:55:50 +08:00
|
|
|
pData = new unsigned char[size];
|
2011-08-25 11:24:31 +08:00
|
|
|
size = fread(pData,sizeof(unsigned char), size,fp);
|
2011-08-04 17:19:58 +08:00
|
|
|
fclose(fp);
|
2011-08-25 11:24:31 +08:00
|
|
|
|
|
|
|
if (pSize)
|
|
|
|
{
|
|
|
|
*pSize = size;
|
|
|
|
}
|
2011-08-04 17:19:58 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2011-03-29 15:05:53 +08:00
|
|
|
if (! pData && getIsPopupNotify())
|
|
|
|
{
|
|
|
|
std::string title = "Notification";
|
|
|
|
std::string msg = "Get data from file(";
|
2011-08-04 17:19:58 +08:00
|
|
|
msg.append(fullPath.c_str()).append(") failed!");
|
2011-03-29 15:05:53 +08:00
|
|
|
CCMessageBox(msg.c_str(), title.c_str());
|
|
|
|
}
|
2011-08-25 11:24:31 +08:00
|
|
|
|
2011-03-29 15:05:53 +08:00
|
|
|
return pData;
|
2011-03-26 17:24:37 +08:00
|
|
|
}
|
|
|
|
|
2011-04-25 09:40:10 +08:00
|
|
|
void CCFileUtils::setResource(const char* pszZipFileName)
|
2010-11-03 11:01:17 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
CCAssert(0, "Have not implement!");
|
2010-11-03 11:01:17 +08:00
|
|
|
}
|
2010-12-06 09:51:21 +08:00
|
|
|
|
2011-03-26 17:24:37 +08:00
|
|
|
int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out)
|
|
|
|
{
|
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
return 0;
|
2010-12-06 09:51:21 +08:00
|
|
|
}
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
string CCFileUtils::getWriteablePath()
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
// the path is: /data/data/ + package name
|
2011-05-12 18:32:13 +08:00
|
|
|
string dir("/data/data/");
|
2011-08-18 16:09:49 +08:00
|
|
|
const char *tmp = getPackageNameJNI();
|
2011-05-12 18:55:32 +08:00
|
|
|
|
2011-05-18 13:56:35 +08:00
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
dir.append(tmp).append("/");
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2011-05-06 15:31:51 +08:00
|
|
|
}
|
|
|
|
|
2011-03-26 17:24:37 +08:00
|
|
|
NS_CC_END;
|