Don’t use FileUtils::getInstance()->getFileData, please use getStringFromFile and getDataFromFile instead.

This commit is contained in:
James Chen 2013-12-24 18:08:40 +08:00
parent f664a72d6e
commit 0ff85852cc
8 changed files with 61 additions and 67 deletions

View File

@ -629,10 +629,10 @@ void VolatileTextureMgr::reloadAllTextures()
case VolatileTexture::kImageFile:
{
Image* image = new Image();
ssize_t size = 0;
unsigned char* pBuffer = FileUtils::getInstance()->getFileData(vt->_fileName.c_str(), "rb", &size);
if (image && image->initWithImageData(pBuffer, size))
Data data = FileUtils::getInstance()->getDataFromFile(vt->_fileName);
if (image && image->initWithImageData(data->getBytes(), data->getSize())
{
Texture2D::PixelFormat oldPixelFormat = Texture2D::getDefaultAlphaPixelFormat();
Texture2D::setDefaultAlphaPixelFormat(vt->_pixelFormat);
@ -640,7 +640,6 @@ void VolatileTextureMgr::reloadAllTextures()
Texture2D::setDefaultAlphaPixelFormat(oldPixelFormat);
}
free(pBuffer);
CC_SAFE_RELEASE(image);
}
break;

View File

@ -75,15 +75,16 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDoc
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
ssize_t size;
char* pXmlBuffer = (char*)FileUtils::getInstance()->getFileData(UserDefault::getInstance()->getXMLFilePath().c_str(), "rb", &size);
//const char* pXmlBuffer = (const char*)data.getBuffer();
if(nullptr == pXmlBuffer)
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath().c_str());
if (xmlBuffer.empty())
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(pXmlBuffer);
free(pXmlBuffer);
xmlDoc->Parse(xmlBuffer.c_str());
// get root node
rootNode = xmlDoc->RootElement();
if (nullptr == rootNode)

View File

@ -288,11 +288,8 @@ void DataReaderHelper::addDataFromFile(const std::string& filePath)
std::string str = &filePathStr[startPos];
// Read content from file
ssize_t size;
std::string fullPath = CCFileUtils::getInstance()->fullPathForFilename(filePath);
unsigned char *pTempContent = (unsigned char *)CCFileUtils::getInstance()->getFileData(fullPath.c_str() , "r", &size);
std::string contentStr = std::string((const char*)pTempContent, size);
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fullPath);
DataInfo dataInfo;
dataInfo.filename = filePathStr;
@ -307,8 +304,6 @@ void DataReaderHelper::addDataFromFile(const std::string& filePath)
{
DataReaderHelper::addDataFromJsonCache(contentStr, &dataInfo);
}
free(pTempContent);
}
void DataReaderHelper::addDataFromFileAsync(const std::string& imagePath, const std::string& plistPath, const std::string& filePath, Object *target, SEL_SCHEDULE selector)
@ -391,10 +386,9 @@ void DataReaderHelper::addDataFromFileAsync(const std::string& imagePath, const
std::string str = &filePathStr[startPos];
std::string fullPath = CCFileUtils::getInstance()->fullPathForFilename(filePath);
ssize_t size;
// XXX fileContent is being leaked
data->fileContent = (char *)CCFileUtils::getInstance()->getFileData(fullPath.c_str() , "r", &size);
data->fileContent = FileUtils::getInstance()->getStringFromFile(fullPath);
if (str == ".xml")
{

View File

@ -122,22 +122,22 @@ const cocos2d::Size GUIReader::getFileDesignSize(const char* fileName) const
UIWidget* GUIReader::widgetFromJsonFile(const char *fileName)
{
DictionaryHelper* dicHelper = DICTOOL;
char *des = nullptr;
std::string jsonpath;
JsonDictionary *jsonDict = nullptr;
jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
int pos = jsonpath.find_last_of('/');
m_strFilePath = jsonpath.substr(0,pos+1);
ssize_t size = 0;
des = (char*)(CCFileUtils::getInstance()->getFileData(jsonpath.c_str(),"r" , &size));
if(nullptr == des || strcmp(des, "") == 0)
std::string des = FileUtils::getInstance()->getStringFromFile(jsonpath);
if (des.empty())
{
printf("read json file[%s] error!\n", fileName);
CCLOG("read json file[%s] error!\n", fileName);
return nullptr;
}
std::string strDes(des);
jsonDict = new JsonDictionary();
jsonDict->initWithDescription(strDes.c_str());
jsonDict->initWithDescription(des.c_str());
UIWidget* widget = nullptr;
const char* fileVersion = dicHelper->getStringValue_json(jsonDict, "version");
@ -164,7 +164,6 @@ UIWidget* GUIReader::widgetFromJsonFile(const char *fileName)
CC_SAFE_DELETE(pReader);
CC_SAFE_DELETE(jsonDict);
free(des);
return widget;
}

View File

@ -47,19 +47,16 @@ namespace cocostudio {
cocos2d::Node* SceneReader::createNodeWithSceneFile(const char* pszFileName)
{
ssize_t size = 0;
char* pData = 0;
cocos2d::Node *pNode = nullptr;
do
{
CC_BREAK_IF(pszFileName == nullptr);
pData = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pszFileName, "r", &size));
CC_BREAK_IF(pData == nullptr || strcmp(pData, "") == 0);
std::string fileContent = cocos2d::FileUtils::getInstance()->getStringFromFile(pszFileName);
CC_BREAK_IF(fileContent.empty());
JsonDictionary *jsonDict = new JsonDictionary();
jsonDict->initWithDescription(pData);
jsonDict->initWithDescription(fileContent.c_str());
pNode = createObject(jsonDict,nullptr);
CC_SAFE_DELETE(jsonDict);
free(pData);
} while (0);
return pNode;
@ -215,11 +212,12 @@ namespace cocostudio {
{
file_path = reDir.substr(0, pos+1);
}
ssize_t size = 0;
char *des = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(),"r" , &size));
std::string des = cocos2d::FileUtils::getInstance()->getStringFromFile(pPath);
JsonDictionary *jsonDict = new JsonDictionary();
jsonDict->initWithDescription(des);
if(nullptr == des || strcmp(des, "") == 0)
jsonDict->initWithDescription(des.c_str());
if (des.empty())
{
CCLOG("read json file[%s] error!\n", pPath.c_str());
}
@ -263,7 +261,6 @@ namespace cocostudio {
CC_SAFE_DELETE(jsonDict);
CC_SAFE_DELETE(subData);
free(des);
}
else if(comName != nullptr && strcmp(comName, "CCComAudio") == 0)
{
@ -285,14 +282,11 @@ namespace cocostudio {
if (nResType == 0)
{
pAttribute = ComAttribute::create();
ssize_t size = 0;
char* pData = 0;
pData = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(), "r", &size));
if(pData != nullptr && strcmp(pData, "") != 0)
std::string fileContent = cocos2d::FileUtils::getInstance()->getStringFromFile(pPath);
if (!fileContent.empty())
{
pAttribute->getDict()->initWithDescription(pData);
pAttribute->getDict()->initWithDescription(fileContent.c_str());
}
free(pData);
}
else
{

View File

@ -52,12 +52,24 @@ void _spAtlasPage_disposeTexture (spAtlasPage* self) {
((TextureAtlas*)self->rendererObject)->release();
}
char* _spUtil_readFile (const char* path, int* length) {
ssize_t size;
char* data = reinterpret_cast<char*>(
FileUtils::getInstance()->getFileData( FileUtils::getInstance()->fullPathForFilename(path).c_str(), "rb", &size));
*length = static_cast<int>(size);
return data;}
char* _spUtil_readFile (const char* path, int* length)
{
char* ret = nullptr;
int size = 0;
Data data = FileUtils::getInstance()->getDataFromFile(path);
if (!data.isNull())
{
size = static_cast<int>(data.getSize());
*length = size;
// Allocates one more byte for string terminal, it will be safe when parsing JSON file in Spine runtime.
ret = (char*)malloc(size + 1);
ret[size] = '\0';
memcpy(ret, data.getBytes(), size);
}
return ret;
}
/**/

View File

@ -533,18 +533,17 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c
// a) check jsc file first
std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT;
ssize_t length = 0;
unsigned char* data = futil->getFileData(byteCodePath.c_str(),
"rb",
&length);
Data data = futil->getDataFromFile(byteCodePath);
if (data) {
script = JS_DecodeScript(cx, data, length, NULL, NULL);
free(data);
if (!data.isNull())
{
script = JS_DecodeScript(cx, data.getBytes(), static_cast<uint32_t>(data.getSize()), nullptr, nullptr);
}
// b) no jsc file, check js file
if (!script) {
if (!script)
{
/* Clear any pending exception from previous failed decoding. */
ReportException(cx);
@ -553,12 +552,10 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c
options.setUTF8(true).setFileAndLine(fullPath.c_str(), 1);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
String* content = String::createWithContentsOfFile(path);
if (content) {
// Not supported in SpiderMonkey 19.0
//JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1);
const char* contentCStr = content->getCString();
script = JS::Compile(cx, obj, options, contentCStr, strlen(contentCStr));
std::string jsFileContent = futil->getStringFromFile(fullPath);
if (!jsFileContent.empty())
{
script = JS::Compile(cx, obj, options, jsFileContent.c_str(), jsFileContent.size());
}
#else
script = JS::Compile(cx, obj, options, fullPath.c_str());

View File

@ -46,17 +46,15 @@ extern "C"
}
filename.append(".lua");
long codeBufferSize = 0;
unsigned char* codeBuffer = FileUtils::getInstance()->getFileData(filename.c_str(), "rb", &codeBufferSize);
Data data = FileUtils::getInstance()->getDataFromFile(filename);
if (codeBuffer)
if (!data.isNull())
{
if (luaL_loadbuffer(L, (char*)codeBuffer, codeBufferSize, filename.c_str()) != 0)
if (luaL_loadbuffer(L, (char*)data.getBytes(), data.getSize(), filename.c_str()) != 0)
{
luaL_error(L, "error loading module %s from file %s :\n\t%s",
lua_tostring(L, 1), filename.c_str(), lua_tostring(L, -1));
}
free(codeBuffer);
}
else
{