void TestResolutionDirectories::onEnter() { FileUtilsDemo::onEnter(); auto sharedFileUtils = FileUtils::getInstance(); std::string ret; sharedFileUtils->purgeCachedEntries(); _defaultSearchPathArray = sharedFileUtils->getSearchPaths(); std::vector<std::string> searchPaths = _defaultSearchPathArray; searchPaths.insert(searchPaths.begin(), "Misc"); sharedFileUtils->setSearchPaths(searchPaths); _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<7; i++) { auto filename = StringUtils::format("test%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename); log("%s -> %s", filename.c_str(), ret.c_str()); } }
void TestSearchPath::onEnter() { FileUtilsDemo::onEnter(); auto sharedFileUtils = FileUtils::getInstance(); std::string ret; sharedFileUtils->purgeCachedEntries(); _defaultSearchPathArray = sharedFileUtils->getSearchPaths(); std::vector<std::string> searchPaths = _defaultSearchPathArray; std::string writablePath = sharedFileUtils->getWritablePath(); std::string fileName = writablePath+"external.txt"; char szBuf[100] = "Hello Cocos2d-x!"; FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp); CCASSERT(ret != 0, "fwrite function returned zero value"); fclose(fp); if (ret != 0) log("Writing file to writable path succeed."); } searchPaths.insert(searchPaths.begin(), writablePath); searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1"); searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2"); sharedFileUtils->setSearchPaths(searchPaths); _defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<3; i++) { auto filename = StringUtils::format("file%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename); log("%s -> %s", filename.c_str(), ret.c_str()); } // Gets external.txt from writable path std::string fullPath = sharedFileUtils->fullPathForFilename("external.txt"); log("external file path = %s", fullPath.c_str()); if (fullPath.length() > 0) { fp = fopen(fullPath.c_str(), "rb"); if (fp) { char szReadBuf[100] = {0}; size_t read = fread(szReadBuf, 1, strlen(szReadBuf), fp); if (read > 0) log("The content of file from writable path: %s", szReadBuf); fclose(fp); } } }
void FileUtilsLayer::OnClickMenu5(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); std::string ret; sharedFileUtils->purgeCachedEntries(); std::string writablePath = sharedFileUtils->getWritablePath(); std::string fileName = writablePath+"test.txt"; char szBuf[100] = "The file is in the resources directory"; FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp); fclose(fp); if (ret != 0) log("Writing file to path succeed."); } }
void FileUtilsLayer::OnClickMenu6(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); sharedFileUtils->purgeCachedEntries(); std::vector<std::string> searchPaths = sharedFileUtils->getSearchPaths(); std::string writablePath = sharedFileUtils->getWritablePath(); searchPaths.insert(searchPaths.begin(), "dir1"); searchPaths.insert(searchPaths.begin()+1, writablePath); sharedFileUtils->setSearchPaths(searchPaths); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("test.txt"); log("test.txt 's fullPathForFilename is : %s",fullPathForFilename.c_str()); Data data = sharedFileUtils->getDataFromFile(fullPathForFilename); std::string content = sharedFileUtils->getStringFromFile(fullPathForFilename); log("File content is : %s",content.c_str()); }