From 0ff85852ccbccad69713f535008cb283a04eddcb Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 24 Dec 2013 18:08:40 +0800 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20FileUtils::getInstance()-?= =?UTF-8?q?>getFileData,=20please=20use=20getStringFromFile=20and=20getDat?= =?UTF-8?q?aFromFile=20instead.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/2d/CCTextureCache.cpp | 7 ++--- cocos/2d/CCUserDefaultAndroid.cpp | 11 ++++---- .../cocostudio/CCDataReaderHelper.cpp | 10 ++----- .../cocostudio/CCSGUIReader.cpp | 15 +++++----- .../cocostudio/CCSSceneReader.cpp | 28 ++++++++----------- cocos/editor-support/spine/spine-cocos2dx.cpp | 24 ++++++++++++---- .../javascript/bindings/ScriptingCore.cpp | 25 ++++++++--------- .../lua/bindings/Cocos2dxLuaLoader.cpp | 8 ++---- 8 files changed, 61 insertions(+), 67 deletions(-) diff --git a/cocos/2d/CCTextureCache.cpp b/cocos/2d/CCTextureCache.cpp index 24ffcb5733..d840a2e093 100644 --- a/cocos/2d/CCTextureCache.cpp +++ b/cocos/2d/CCTextureCache.cpp @@ -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; diff --git a/cocos/2d/CCUserDefaultAndroid.cpp b/cocos/2d/CCUserDefaultAndroid.cpp index 01d41273f5..af82d29364 100644 --- a/cocos/2d/CCUserDefaultAndroid.cpp +++ b/cocos/2d/CCUserDefaultAndroid.cpp @@ -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) diff --git a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp index d5bbec47d5..779197b62d 100644 --- a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp +++ b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp @@ -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") { diff --git a/cocos/editor-support/cocostudio/CCSGUIReader.cpp b/cocos/editor-support/cocostudio/CCSGUIReader.cpp index 625627fff1..5d5a57e16c 100644 --- a/cocos/editor-support/cocostudio/CCSGUIReader.cpp +++ b/cocos/editor-support/cocostudio/CCSGUIReader.cpp @@ -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; } diff --git a/cocos/editor-support/cocostudio/CCSSceneReader.cpp b/cocos/editor-support/cocostudio/CCSSceneReader.cpp index d682c504b7..b84c2773ad 100644 --- a/cocos/editor-support/cocostudio/CCSSceneReader.cpp +++ b/cocos/editor-support/cocostudio/CCSSceneReader.cpp @@ -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 { diff --git a/cocos/editor-support/spine/spine-cocos2dx.cpp b/cocos/editor-support/spine/spine-cocos2dx.cpp index 8f895fcff4..734492a91e 100644 --- a/cocos/editor-support/spine/spine-cocos2dx.cpp +++ b/cocos/editor-support/spine/spine-cocos2dx.cpp @@ -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( - FileUtils::getInstance()->getFileData( FileUtils::getInstance()->fullPathForFilename(path).c_str(), "rb", &size)); - *length = static_cast(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(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; +} /**/ diff --git a/cocos/scripting/javascript/bindings/ScriptingCore.cpp b/cocos/scripting/javascript/bindings/ScriptingCore.cpp index a23101156e..bdbf4978e0 100644 --- a/cocos/scripting/javascript/bindings/ScriptingCore.cpp +++ b/cocos/scripting/javascript/bindings/ScriptingCore.cpp @@ -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(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()); diff --git a/cocos/scripting/lua/bindings/Cocos2dxLuaLoader.cpp b/cocos/scripting/lua/bindings/Cocos2dxLuaLoader.cpp index fa1f7cb01c..13c9c26e34 100644 --- a/cocos/scripting/lua/bindings/Cocos2dxLuaLoader.cpp +++ b/cocos/scripting/lua/bindings/Cocos2dxLuaLoader.cpp @@ -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 {