void TestResolutionDirectories::onEnter() { FileUtilsDemo::onEnter(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); string ret; sharedFileUtils->purgeCachedEntries(); m_defaultSearchPathArray = sharedFileUtils->getSearchPaths(); vector<string> searchPaths = m_defaultSearchPathArray; searchPaths.insert(searchPaths.begin(), "Misc"); sharedFileUtils->setSearchPaths(searchPaths); m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); vector<string> resolutionsOrder = m_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++) { CCString *filename = CCString::createWithFormat("test%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename->getCString()); CCLog("%s -> %s", filename->getCString(), ret.c_str()); } }
void TestSearchPath::onEnter() { FileUtilsDemo::onEnter(); CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); string ret; sharedFileUtils->purgeCachedEntries(); m_defaultSearchPathArray = sharedFileUtils->getSearchPaths(); vector<string> searchPaths = m_defaultSearchPathArray; string writablePath = sharedFileUtils->getWritablePath(); string fileName = writablePath+"external.txt"; char szBuf[100] = "Hello Cocos2d-x!"; FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { fwrite(szBuf, 1, strlen(szBuf), fp); fclose(fp); CCLog("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); m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); vector<string> resolutionsOrder = m_defaultResolutionsOrderArray; resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for( int i=1; i<3; i++) { CCString *filename = CCString::createWithFormat("file%d.txt", i); ret = sharedFileUtils->fullPathForFilename(filename->getCString()); CCLog("%s -> %s", filename->getCString(), ret.c_str()); } // Gets external.txt from writable path string fullPath = sharedFileUtils->fullPathForFilename("external.txt"); CCLog("\nexternal file path = %s\n", fullPath.c_str()); if (fullPath.length() > 0) { fp = fopen(fullPath.c_str(), "rb"); if (fp) { char szReadBuf[100] = {0}; fread(szReadBuf, 1, strlen(szBuf), fp); CCLog("The content of file from writable path: %s", szReadBuf); fclose(fp); } } }