void TestIsDirectoryExist::onEnter() { FileUtilsDemo::onEnter(); auto s = Director::getInstance()->getWinSize(); auto util = FileUtils::getInstance(); int x = s.width/2, y = s.height/3; Label* label = nullptr; std::string dir; auto getMsg = [&dir](bool b)-> std::string { char msg[512]; snprintf((char *)msg, 512, "%s for dir: \"%s\"", b ? "success" : "failed", dir.c_str()); return std::string(msg); }; dir = "Images"; label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20); label->setPosition(x, y * 2); this->addChild(label); dir = util->getWritablePath(); label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20); label->setPosition(x, y * 1); this->addChild(label); dir = util->getWritablePath(); label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20); label->setPosition(x, y * 1); this->addChild(label); }
CCDictionary* GlobalData::getCardProfileByName(std::string name) { sqlite3 *pDB = NULL; char* errMsg = NULL; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string dbPath = getWritablePath(); #else std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db"); #endif int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READONLY, NULL); if (result!=SQLITE_OK) { return NULL; } std::string szSql = "select c.cardHeadImg,c.cardBodyImg,c.cardProfileImg,c.game_group_id,r.roleName,r.starGrade,r.beginGrade,r.blood,r.attack,r.defence,r.crit,r.dodge,r.roleDescription from card c left join game_role r on c.cardProfileImg = r.roleImageId where c.cardProfileImg = '"; szSql.append(name); szSql.append("'"); const char *argc = QUERY_CARD_PROFILE_BY_NAME; result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg); if (result != SQLITE_OK) { return NULL; } return dictCard; }
CCArray* GlobalData::getFraction(std::string name) { if (arrayFraction!=NULL) { return arrayFraction; } arrayFraction = CCArray::create(); sqlite3 *pDB = NULL; char* errMsg = NULL; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string dbPath = getWritablePath(); #else std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db"); #endif int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READWRITE, NULL); if (result!=SQLITE_OK) { return NULL; } std::string szSql = "select * from game_group"; const char *argc = QUERY_CATEGORY_FRACTION; result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg); if (result != SQLITE_OK) { return NULL; } return arrayFraction; }
/** * ファイルDL正常終了コールバック */ void HelloWorld::onHttpRequestCompleted(extension::HttpClient* sender, extension::HttpResponse* response) { Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); auto fileUtils = FileUtils::sharedFileUtils(); std::string filename = fileUtils->getWritablePath() + "hoge.jpg"; if (fileUtils->isFileExist(filename)) { log("file exist. use hoge.jpg."); auto sprite = Sprite::create(filename.c_str()); sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(sprite, 0); } else { log("no file exist. use default image, and save the file."); if (response->isSucceed()) { std::vector<char>* buffer = response->getResponseData(); auto* img = new Image(); img->initWithImageData(reinterpret_cast<unsigned char*>(&(buffer->front())), buffer->size()); img->saveToFile(filename.c_str()); auto* texture = new Texture2D(); texture->initWithImage(img); auto sprite = Sprite::createWithTexture(texture); sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(sprite, 0); } } }
CCDictionary* GlobalData::getCardInfoById(std::string cardId) { if (dictCard==NULL) { dictCard = CCDictionary::create(); } sqlite3 *pDB = NULL; char* errMsg = NULL; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string dbPath = getWritablePath(); #else std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db"); #endif int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READONLY, NULL); if (result!=SQLITE_OK) { return NULL; } std::string szSql = "select * from game_role where roleid = "; const char *argc = QUERY_CATEGORY_CARDINFO; result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg); if (result != SQLITE_OK) { return NULL; } return dictCard; }
string NoteDAO::dbDirectoryFile() { auto sharedFileUtils = FileUtils::getInstance(); string writablePath = sharedFileUtils->getWritablePath(); string fullPath = writablePath + "NotesList.sqlite3"; return fullPath; }
std::string ConsoleUploadFile::subtitle() const { auto sharedFileUtils = FileUtils::getInstance(); std::string writablePath = sharedFileUtils->getWritablePath(); return "file uploaded to:" + writablePath + _target_file_name; }
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); } } }
CCArray* GlobalData::getAllCardProfile(CardQueryCriteria *query) { arrayCards = CCArray::create(); sqlite3 *pDB = NULL; char* errMsg = NULL; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string dbPath = getWritablePath(); #else std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db"); #endif int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READWRITE, NULL); if (result!=SQLITE_OK) { return NULL; } std::string szSql = "select c.cardHeadImg,c.cardBodyImg,c.cardProfileImg,c.game_group_id,r.roleName,r.starGrade,r.beginGrade,r.blood,r.attack,r.defence,r.crit,r.dodge,r.roleDescription from card c left join game_role r on c.cardProfileImg = r.roleImageId where r.starGrade is not null"; if (query != NULL) { if (query->groupid >= 0) { } if (query->cardName.length()>0) { std::vector<std::string> slist = split(query->cardName,';'); if (slist.size()<=1) { szSql.append(" AND c.cardProfileImg = '"); szSql.append(slist[0]); szSql.append("'"); } else { szSql.append(" AND c.cardProfileImg in (\'"); std::string sresult = vectorToString(slist,"\',\'"); szSql.append(sresult); szSql.append("\')"); } } } const char *argc = QUERY_CARD_PROFILE_ALL; result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg); if (result != SQLITE_OK) { return NULL; } return arrayCards; }
void FileUtilsLayer::OnClickMenu4(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); //std::string ret; std::string writablePath = sharedFileUtils->getWritablePath(); std::string fileName = writablePath+"test.txt"; char szBuf[100] = "Testing Write to file."; 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::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 DataModel::serverCallback(HttpClient* client, HttpResponse* response) { if (response->getResponseCode() == 200) { auto fileUtils = FileUtils::getInstance(); std::string filePath = fileUtils->getWritablePath() + "gunbound.db3"; auto responseData = response->getResponseData(); /* Log response data */ /* for (unsigned int i = 0; i < responseData->size(); i++){ log("%c", (*responseData)[i]); } */ /* ModeType: W:Tao file moi hoac neu file co san thi se duoc ghi moi hoan toan filePath: ten file hoac duong dan den file can mo Trong truong hop nay no la duong dan den file can mo WB: ghi vao file nhi phan chu khong phai file txt nhu binh thuong */ FILE* file = fopen(filePath.c_str(), "wb"); if (file == NULL) // Neu fopen() tra ve NULL nghia la ko mo duoc file hoac file ko ton tai { log("Can not create file %s ", filePath.c_str()); return; } // Ghi du lieu tu responseData vao tap tin file // Ham fwrite(du lieu muon ghi vao file , kich thuoc du lieu, so luong can ghi , file duoc ghi vao ) fwrite(responseData->data(), 1, responseData->size(), file); fclose(file); log("Succesfull !!! Save file into %s ", filePath.c_str()); } else{ log("Not connect database"); } }
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()); }
CCArray* GlobalData::getCardProfile(int group) { arrayCardProfile = CCArray::create(); sqlite3 *pDB = NULL; char* errMsg = NULL; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string dbPath = getWritablePath(); #else std::string dbPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("card.s3db"); #endif int result = sqlite3_open_v2(dbPath.c_str(),&pDB,SQLITE_OPEN_READONLY, NULL); if (result!=SQLITE_OK) { CCLOG("status=================:%d ",result); return arrayCardProfile; } else { CCLOG("status=================:%d ",result); } std::string szSql = "select * from card"; if (group>=0) { szSql.append(" where game_group_id = "); szSql.append(IntToString(group)); } const char *argc = QUERY_CATEGORY_CARDBYGROUP; result = sqlite3_exec(pDB,szSql.c_str(), sqliteExecCallBack, (void *)argc, &errMsg); if (result!=SQLITE_OK) { CCLOG("status=================:%d ",result); return arrayCardProfile; } else { CCLOG("status=================:%d ",result); } return arrayCardProfile; }
void Log::setOutPutType(int output) { m_outputType = output; if(m_outfileHandle && !(m_outputType & File)) { fclose(m_outfileHandle); } if(m_outputType & File) { static char filename[LOGNAME_SIZE]; time_t now = time(0); strftime(filename, sizeof(filename), LOGNAME_FORMAT, localtime(&now)); std::string path = getWritablePath() +"log/"; if(!createDirectory(path.c_str())) m_outputType |= ~File; m_outfileHandle = fopen((path + filename).c_str(), "wb"); if(!m_outfileHandle) m_outputType |= ~File; } }
void FileUtilsLayer::OnClickMenu2(Ref* pSender) { auto sharedFileUtils = FileUtils::getInstance(); std::string writablePath = sharedFileUtils->getWritablePath(); log("writablePath = %s", writablePath.c_str()); }
std::string CCFileUtils::getCachePath() { return m_strCachePath.length() > 0 ? m_strCachePath : getWritablePath(); }
void TestUnicodePath::onEnter() { FileUtilsDemo::onEnter(); auto s = Director::getInstance()->getWinSize(); auto util = FileUtils::getInstance(); int x = s.width/2, y = s.height/5; Label* label = nullptr; std::string dir = "中文路径/"; std::string filename = "测试文件.test"; std::string act; auto getMsg = [&act](bool b, const std::string& path)-> std::string { char msg[512]; snprintf((char *)msg, 512, "%s for %s path: \"%s\"", b ? "success" : "failed", act.c_str(), path.c_str()); return std::string(msg); }; // Check whether unicode dir should be create or not std::string dirPath = util->getWritablePath() + dir; if (!util->isDirectoryExist(dirPath)) { util->createDirectory(dirPath); } act = "create"; bool isExist = util->isDirectoryExist(dirPath); label = Label::createWithSystemFont(getMsg(isExist, dirPath), "", 12, Size(s.width, 0)); label->setPosition(x, y * 4); this->addChild(label); if (isExist) { // Check whether unicode file should be create or not std::string filePath = dirPath + filename; if (! util->isFileExist(filePath)) { std::string writeDataStr = " 测试字符串."; Data writeData; writeData.copy((unsigned char *)writeDataStr.c_str(), writeDataStr.size()); util->writeDataToFile(writeData, filePath); } isExist = util->isFileExist(filePath); label = Label::createWithSystemFont(getMsg(isExist, filePath), "", 12, Size(s.width, 0)); label->setPosition(x, y * 3); this->addChild(label); act = "remove"; if (isExist) { // read file content and log it unsigned char* buffer = nullptr; Data readData = util->getDataFromFile(filePath); buffer = (unsigned char*)malloc(sizeof(unsigned char) * (readData.getSize() + 1)); memcpy(buffer, readData.getBytes(), readData.getSize()); buffer[readData.getSize()] = '\0'; // vc can't treat unicode string correctly, don't use unicode string in code log("The content of file from writable path: %s", buffer); free(buffer); // remove test file label = Label::createWithSystemFont(getMsg(util->removeFile(filePath), filePath), "", 12, Size(s.width, 0)); label->setPosition(x, y * 2); this->addChild(label); } // remove test dir label = Label::createWithSystemFont(getMsg(util->removeDirectory(dirPath), dirPath), "", 12, Size(s.width, 0)); label->setPosition(x, y * 1); this->addChild(label); } }
void TestDirectoryFuncs::onEnter() { FileUtilsDemo::onEnter(); auto s = Director::getInstance()->getWinSize(); auto sharedFileUtils = FileUtils::getInstance(); int x = s.width/2, y = s.height/4; Label* label = nullptr; std::string dir = sharedFileUtils->getWritablePath() + "__test/"; std::string subDir = "dir1/dir2"; std::string msg; bool ok; // Check whether dir can be created ok = sharedFileUtils->createDirectory(dir); if (ok && sharedFileUtils->isDirectoryExist(dir)) { msg = StringUtils::format("createDirectory: Directory '__test' created"); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y * 3); this->addChild(label); // Create sub directories recursively ok = sharedFileUtils->createDirectory(dir + subDir); if (ok && sharedFileUtils->isDirectoryExist(dir + subDir)) { msg = StringUtils::format("createDirectory: Sub directories '%s' created", subDir.c_str()); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y * 2); this->addChild(label); } else { msg = StringUtils::format("createDirectory: Failed to create sub directories '%s'", subDir.c_str()); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y * 2); this->addChild(label); } // Remove directory ok = sharedFileUtils->removeDirectory(dir); if (ok && !sharedFileUtils->isDirectoryExist(dir)) { msg = StringUtils::format("removeDirectory: Directory '__test' removed"); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y); this->addChild(label); } else { msg = StringUtils::format("removeDirectory: Failed to remove directory '__test'"); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y); this->addChild(label); } } else { msg = StringUtils::format("createDirectory: Directory '__test' can not be created"); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y * 2); this->addChild(label); } }
void TestFileFuncs::onEnter() { FileUtilsDemo::onEnter(); auto s = Director::getInstance()->getWinSize(); auto sharedFileUtils = FileUtils::getInstance(); int x = s.width/2, y = s.height/5; Label* label = nullptr; std::string filename = "__test.test"; std::string filename2 = "__newtest.test"; std::string filepath = sharedFileUtils->getWritablePath() + filename; std::string content = "Test string content to put into created file"; std::string msg; FILE *out = fopen(filepath.c_str(), "w"); fputs(content.c_str(), out); fclose(out); // Check whether file can be created if (sharedFileUtils->isFileExist(filepath)) { label = Label::createWithSystemFont("Test file '__test.test' created", "", 20); label->setPosition(x, y * 4); this->addChild(label); // getFileSize Test long size = sharedFileUtils->getFileSize(filepath); msg = StringUtils::format("getFileSize: Test file size equals %ld", size); label = Label::createWithSystemFont(msg, "", 20); label->setPosition(x, y * 3); this->addChild(label); // renameFile Test if (sharedFileUtils->renameFile(sharedFileUtils->getWritablePath(), filename, filename2)) { label = Label::createWithSystemFont("renameFile: Test file renamed to '__newtest.test'", "", 20); label->setPosition(x, y * 2); this->addChild(label); // removeFile Test filepath = sharedFileUtils->getWritablePath() + filename2; if (sharedFileUtils->removeFile(filepath)) { label = Label::createWithSystemFont("removeFile: Test file removed", "", 20); label->setPosition(x, y * 1); this->addChild(label); } else { label = Label::createWithSystemFont("removeFile: Failed to remove test file", "", 20); label->setPosition(x, y * 1); this->addChild(label); } } else { label = Label::createWithSystemFont("renameFile: Failed to rename test file to '__newtest.test', further test skipped", "", 20); label->setPosition(x, y * 2); this->addChild(label); } } else { label = Label::createWithSystemFont("Test file can not be created, test skipped", "", 20); label->setPosition(x, y * 4); this->addChild(label); } }