2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "FileUtilsTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FileUtilsTests::FileUtilsTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(TestSearchPath);
|
|
|
|
ADD_TEST_CASE(TestIsFileExist);
|
|
|
|
ADD_TEST_CASE(TestIsDirectoryExist);
|
|
|
|
ADD_TEST_CASE(TestFileFuncs);
|
|
|
|
ADD_TEST_CASE(TestDirectoryFuncs);
|
|
|
|
ADD_TEST_CASE(TestWriteString);
|
|
|
|
ADD_TEST_CASE(TestGetContents);
|
|
|
|
ADD_TEST_CASE(TestWriteData);
|
|
|
|
ADD_TEST_CASE(TestWriteValueMap);
|
|
|
|
ADD_TEST_CASE(TestWriteValueVector);
|
|
|
|
ADD_TEST_CASE(TestUnicodePath);
|
|
|
|
ADD_TEST_CASE(TestIsFileExistAsync);
|
|
|
|
ADD_TEST_CASE(TestIsDirectoryExistAsync);
|
|
|
|
ADD_TEST_CASE(TestFileFuncsAsync);
|
|
|
|
ADD_TEST_CASE(TestWriteStringAsync);
|
|
|
|
ADD_TEST_CASE(TestWriteDataAsync);
|
|
|
|
ADD_TEST_CASE(TestListFiles);
|
|
|
|
ADD_TEST_CASE(TestIsFileExistRejectFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSearchPath
|
|
|
|
|
|
|
|
void TestSearchPath::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2021-12-31 12:12:40 +08:00
|
|
|
_defaultSearchPathArray = sharedFileUtils->getOriginalSearchPaths();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<std::string> searchPaths = _defaultSearchPathArray;
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string writablePath = sharedFileUtils->getWritablePath();
|
|
|
|
std::string fileName = writablePath + "external.txt";
|
|
|
|
char szBuf[100] = "Hello Cocos2d-x!";
|
2023-09-01 16:31:14 +08:00
|
|
|
auto fp = fopen(fileName.c_str(), "wb");
|
2019-11-23 20:27:39 +08:00
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(ret != 0, "fwrite function returned zero value");
|
2019-11-23 20:27:39 +08:00
|
|
|
fclose(fp);
|
|
|
|
if (ret != 0)
|
|
|
|
log("Writing file to writable path succeed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
searchPaths.insert(searchPaths.begin(), writablePath);
|
2021-12-31 12:12:40 +08:00
|
|
|
searchPaths.insert(searchPaths.begin() + 1, "Misc/searchpath1");
|
|
|
|
searchPaths.insert(searchPaths.begin() + 2, "Misc/searchpath2");
|
2019-11-23 20:27:39 +08:00
|
|
|
sharedFileUtils->setSearchPaths(searchPaths);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 1; i < 3; i++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto filename = StringUtils::format("file%d.txt", i);
|
2021-12-31 12:12:40 +08:00
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
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};
|
2021-12-31 12:12:40 +08:00
|
|
|
size_t read = fread(szReadBuf, 1, strlen(szReadBuf), fp);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (read > 0)
|
|
|
|
log("The content of file from writable path: %s", szReadBuf);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: should fix the issue on Android
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Save old resource root path
|
|
|
|
std::string oldDefaultRootPath = sharedFileUtils->getDefaultResourceRootPath();
|
|
|
|
sharedFileUtils->setDefaultResourceRootPath(oldDefaultRootPath + "extensions");
|
|
|
|
auto sp1 = Sprite::create("orange_edit.png");
|
|
|
|
sp1->setPosition(VisibleRect::center());
|
|
|
|
addChild(sp1);
|
|
|
|
|
|
|
|
// Recover resource root path
|
|
|
|
sharedFileUtils->setDefaultResourceRootPath(oldDefaultRootPath);
|
|
|
|
|
|
|
|
auto oldSearchPaths = sharedFileUtils->getOriginalSearchPaths();
|
|
|
|
sharedFileUtils->addSearchPath("Images");
|
|
|
|
auto sp2 = Sprite::create("btn-about-normal.png");
|
|
|
|
sp2->setPosition(VisibleRect::center() + Vec2(0, -50));
|
|
|
|
addChild(sp2);
|
|
|
|
|
|
|
|
// Recover old search paths
|
|
|
|
sharedFileUtils->setSearchPaths(oldSearchPaths);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSearchPath::onExit()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils* sharedFileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// reset search path
|
|
|
|
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestSearchPath::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: search path";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestSearchPath::subtitle() const
|
|
|
|
{
|
|
|
|
return "See the console, can see a orange box and a 'about' picture, except Android";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestIsFileExist
|
|
|
|
|
|
|
|
void TestIsFileExist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
Label* label = nullptr;
|
|
|
|
bool isExist = false;
|
|
|
|
|
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.png");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist",
|
|
|
|
"", 20);
|
|
|
|
label->setPosition(s.width / 2, s.height / 3);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.xcf");
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist",
|
|
|
|
"", 20);
|
|
|
|
label->setPosition(s.width / 2, s.height / 3 * 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExist::onExit()
|
|
|
|
{
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils* sharedFileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExist::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: check whether the file exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExist::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestIsDirectoryExist
|
|
|
|
|
|
|
|
void TestIsDirectoryExist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto util = FileUtils::getInstance();
|
2021-12-31 12:12:40 +08:00
|
|
|
int x = s.width / 2, y = s.height / 3;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Label* label = nullptr;
|
|
|
|
std::string dir;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto getMsg = [&dir](bool b) -> std::string {
|
2019-11-23 20:27:39 +08:00
|
|
|
char msg[512];
|
2021-12-31 12:12:40 +08:00
|
|
|
snprintf((char*)msg, 512, "%s for dir: \"%s\"", b ? "success" : "failed", dir.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
return std::string(msg);
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
dir = "Images";
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
dir = util->getWritablePath();
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20);
|
|
|
|
label->setPosition(x, y * 1);
|
|
|
|
this->addChild(label);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
dir = util->getWritablePath();
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20);
|
|
|
|
label->setPosition(x, y * 1);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsDirectoryExist::onExit()
|
|
|
|
{
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils* sharedFileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// reset filename lookup
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
|
|
|
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExist::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: check whether the directory exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExist::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFileFuncs
|
|
|
|
|
|
|
|
void TestFileFuncs::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int x = s.width / 2, y = s.height / 5;
|
2019-11-23 20:27:39 +08:00
|
|
|
Label* label = nullptr;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string filename = "__test.test";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string filename2 = "__newtest.test";
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string filepath = sharedFileUtils->getWritablePath() + filename;
|
|
|
|
std::string content = "Test string content to put into created file";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string msg;
|
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
auto out = fopen(filepath.c_str(), "w");
|
2019-11-23 20:27:39 +08:00
|
|
|
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
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t size = sharedFileUtils->getFileSize(filepath);
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("getFileSize: Test file size equals %d", size);
|
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2019-11-23 20:27:39 +08:00
|
|
|
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
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(
|
|
|
|
"renameFile: Failed to rename test file to '__newtest.test', further test skipped", "", 20);
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncs::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: file control functions";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncs::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDirectoryFuncs
|
|
|
|
|
|
|
|
void TestDirectoryFuncs::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int x = s.width / 2, y = s.height / 4;
|
2019-11-23 20:27:39 +08:00
|
|
|
Label* label = nullptr;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string dir = sharedFileUtils->getWritablePath() + "__test";
|
|
|
|
std::string subDir = "dir1/dir2";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fullSubDir = dir + "/" + subDir;
|
|
|
|
std::string msg;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Check whether dir can be created
|
|
|
|
ok = sharedFileUtils->createDirectory(dir);
|
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(dir))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Directory '__test' created");
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y * 3);
|
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
// Create sub directories recursively
|
|
|
|
ok = sharedFileUtils->createDirectory(fullSubDir);
|
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(fullSubDir))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Sub directories '%s' created", subDir.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Failed to create sub directories '%s'", subDir.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove directory
|
|
|
|
ok = sharedFileUtils->removeDirectory(dir);
|
|
|
|
if (ok && !sharedFileUtils->isDirectoryExist(dir))
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("removeDirectory: Directory '__test' removed");
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("removeDirectory: Failed to remove directory '__test'");
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Directory '__test' can not be created");
|
2019-11-23 20:27:39 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestDirectoryFuncs::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: directory control functions";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestDirectoryFuncs::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteString::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fileName = "writeStringTest.txt";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the string data will be write into a file";
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fullPath = writablePath + fileName;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (FileUtils::getInstance()->writeStringToFile(writeDataStr, fullPath.c_str()))
|
|
|
|
{
|
|
|
|
log("see the plist file at %s", fullPath.c_str());
|
|
|
|
writeResult->setString("write success:" + writeDataStr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("write plist file failed");
|
|
|
|
writeResult->setString("write fail");
|
|
|
|
}
|
|
|
|
|
|
|
|
// readTest
|
|
|
|
std::string readDataStr = FileUtils::getInstance()->getStringFromFile(fullPath);
|
|
|
|
readResult->setString("read success:" + readDataStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteString::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteString::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestWriteString to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteString::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static void saveAsBinaryText(std::string_view filename, const std::vector<char>& binary)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto fs = FileUtils::getInstance();
|
|
|
|
std::string text(binary.begin(), binary.end());
|
|
|
|
fs->writeStringToFile(text, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string FileErrors[] = {
|
2021-12-31 12:12:40 +08:00
|
|
|
"OK", "NotExists", "OpenFailed", "ReadFailed", "NotInitialized", "TooLarge", "ObtainSizeFailed",
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void TestGetContents::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto fs = FileUtils::getInstance();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 16);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 2);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::vector<char> binary = {'\r', '\n', '\r', '\n', '\0', '\0', '\r', '\n'};
|
|
|
|
_generatedFile = fs->getWritablePath() + "file-with-zeros-and-crlf";
|
2019-11-23 20:27:39 +08:00
|
|
|
saveAsBinaryText(_generatedFile, binary);
|
|
|
|
|
|
|
|
auto runTests = [&]() {
|
|
|
|
// Test read string in binary mode
|
|
|
|
std::string bs;
|
|
|
|
fs->getContents(_generatedFile, &bs);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (bs.size() != binary.size() || !std::equal(bs.begin(), bs.end(), binary.begin()))
|
2019-11-23 20:27:39 +08:00
|
|
|
return std::string("failed: read as binary string");
|
|
|
|
|
|
|
|
// Text read string in text mode
|
|
|
|
std::string ts = fs->getStringFromFile(_generatedFile);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (strcmp(ts.c_str(), "\r\n\r\n") != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
return std::string("failed: read as zero terminated string");
|
|
|
|
|
|
|
|
std::string files[] = {_generatedFile, "background.wav", "fileLookup.plist"};
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& file : files)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string sbuf;
|
|
|
|
|
|
|
|
auto serr = fs->getContents(file, &sbuf);
|
|
|
|
if (serr != FileUtils::Status::OK)
|
|
|
|
return std::string("failed: error: " + FileErrors[(int)serr]);
|
|
|
|
|
|
|
|
std::vector<int> vbuf;
|
|
|
|
auto verr = fs->getContents(file, &vbuf);
|
|
|
|
if (verr != FileUtils::Status::OK)
|
|
|
|
return std::string("failed: error: " + FileErrors[(int)verr]);
|
|
|
|
|
|
|
|
Data dbuf;
|
|
|
|
auto derr = fs->getContents(file, &dbuf);
|
|
|
|
if (derr != FileUtils::Status::OK)
|
|
|
|
return std::string("failed: error: " + FileErrors[(int)derr]);
|
|
|
|
|
|
|
|
if (memcmp(&sbuf.front(), &vbuf.front(), sbuf.size()) != 0)
|
|
|
|
return std::string("failed: error: sbuf != vbuf");
|
|
|
|
|
|
|
|
if (dbuf.getSize() != sbuf.size())
|
|
|
|
return std::string("failed: error: sbuf.size() != dbuf.getSize()");
|
|
|
|
|
|
|
|
if (memcmp(&sbuf.front(), dbuf.getBytes(), sbuf.size()) != 0)
|
|
|
|
return std::string("failed: error: sbuf != dbuf");
|
|
|
|
}
|
|
|
|
return std::string("read success");
|
|
|
|
};
|
|
|
|
readResult->setString("FileUtils::getContents() " + runTests());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGetContents::onExit()
|
|
|
|
{
|
|
|
|
if (!_generatedFile.empty())
|
|
|
|
FileUtils::getInstance()->removeFile(_generatedFile);
|
|
|
|
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestGetContents::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestGetContents";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestGetContents::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteData::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fileName = "writeDataTest.txt";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the binary data will be write into a file";
|
|
|
|
Data writeData;
|
2021-12-31 12:12:40 +08:00
|
|
|
writeData.copy((unsigned char*)writeDataStr.c_str(), writeDataStr.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fullPath = writablePath + fileName;
|
|
|
|
if (FileUtils::getInstance()->writeDataToFile(writeData, fullPath.c_str()))
|
|
|
|
{
|
|
|
|
log("see the plist file at %s", fullPath.c_str());
|
|
|
|
writeResult->setString("write success:" + writeDataStr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("write plist file failed");
|
|
|
|
writeResult->setString("write fail");
|
|
|
|
}
|
|
|
|
|
|
|
|
// readTest
|
|
|
|
unsigned char* buffer = nullptr;
|
2021-12-31 12:12:40 +08:00
|
|
|
Data readData = FileUtils::getInstance()->getDataFromFile(fullPath);
|
|
|
|
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (readData.getSize() + 1));
|
2019-11-23 20:27:39 +08:00
|
|
|
memcpy(buffer, readData.getBytes(), readData.getSize());
|
|
|
|
buffer[readData.getSize()] = '\0';
|
|
|
|
std::string readDataStr((const char*)buffer);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
readResult->setString("read success:" + readDataStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteData::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteData::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestWriteData to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteData::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteValueMap::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
|
|
|
|
ValueMap valueMap;
|
|
|
|
|
|
|
|
ValueMap mapInValueMap;
|
|
|
|
mapInValueMap["string1"] = "string in dictInMap key 0";
|
|
|
|
mapInValueMap["string2"] = "string in dictInMap key 1";
|
|
|
|
mapInValueMap["none"].getType();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
valueMap["data0"] = Value(mapInValueMap);
|
|
|
|
|
|
|
|
valueMap["data1"] = Value("string in array");
|
|
|
|
|
|
|
|
ValueVector arrayInMap;
|
2022-08-08 13:18:33 +08:00
|
|
|
arrayInMap.emplace_back(Value("string 0 in arrayInMap"));
|
|
|
|
arrayInMap.emplace_back(Value("string 1 in arrayInMap"));
|
2019-11-23 20:27:39 +08:00
|
|
|
valueMap["data2"] = arrayInMap;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add boolean to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto booleanObject = Value(true);
|
2021-12-31 12:12:40 +08:00
|
|
|
valueMap["data3"] = booleanObject;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add integer to the plist
|
|
|
|
auto intObject = Value(1024);
|
2019-11-23 20:27:39 +08:00
|
|
|
valueMap["data4"] = intObject;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add float to the plist
|
|
|
|
auto floatObject = Value(1024.1024f);
|
2019-11-23 20:27:39 +08:00
|
|
|
valueMap["data5"] = floatObject;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add double to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto doubleObject = Value(1024.123);
|
|
|
|
valueMap["data6"] = doubleObject;
|
|
|
|
|
|
|
|
// end with /
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fullPath = writablePath + "testWriteValueMap.plist";
|
2019-11-23 20:27:39 +08:00
|
|
|
if (FileUtils::getInstance()->writeValueMapToFile(valueMap, fullPath.c_str()))
|
|
|
|
{
|
|
|
|
log("see the plist file at %s", fullPath.c_str());
|
|
|
|
writeResult->setString("write success");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("write plist file failed");
|
|
|
|
writeResult->setString("write failed");
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueMap readValueMap = FileUtils::getInstance()->getValueMapFromFile(fullPath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string readDataStr = "read data:\n";
|
|
|
|
// read value map data
|
|
|
|
ValueMap readMapInMap = readValueMap["data0"].asValueMap();
|
|
|
|
readDataStr += " mapValue:[\"string1\"][" + readMapInMap["string1"].asString() + "]\n";
|
|
|
|
readDataStr += " mapValue:[\"string2\"][" + readMapInMap["string2"].asString() + "]\n";
|
|
|
|
|
|
|
|
// read string data
|
|
|
|
readDataStr += " stringValue:" + readValueMap["data1"].asString() + "\n";
|
|
|
|
|
|
|
|
// read value vector data
|
|
|
|
ValueVector readVectorInMap = readValueMap["data2"].asValueVector();
|
|
|
|
readDataStr += " vectorValue:[1]" + readVectorInMap.at(0).asString() + "\n";
|
|
|
|
readDataStr += " vectorValue:[2]" + readVectorInMap.at(1).asString() + "\n";
|
|
|
|
|
|
|
|
// read bool data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " boolValue:" + StringUtils::format("%d", readValueMap["data3"].asBool()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read int data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " intValue:" + StringUtils::format("%d", readValueMap["data4"].asInt()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read float data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " floatValue:" + StringUtils::format("%f", readValueMap["data5"].asFloat()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read double data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " doubleValue:" + StringUtils::format("%f", readValueMap["data6"].asDouble()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
readResult->setString(readDataStr);
|
|
|
|
}
|
|
|
|
void TestWriteValueMap::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueMap::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestWriteValueMap to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueMap::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteValueVector::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
|
|
|
|
ValueVector array;
|
|
|
|
|
|
|
|
ValueMap mapInArray;
|
|
|
|
mapInArray["string1"] = "string in dictInArray key 0";
|
|
|
|
mapInArray["string2"] = "string in dictInArray key 1";
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(Value(mapInArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(Value("string in array"));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ValueVector arrayInArray;
|
2022-08-08 13:18:33 +08:00
|
|
|
arrayInArray.emplace_back(Value("string 0 in arrayInArray"));
|
|
|
|
arrayInArray.emplace_back(Value("string 1 in arrayInArray"));
|
|
|
|
array.emplace_back(Value(arrayInArray));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add boolean to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto booleanObject = Value(true);
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(booleanObject);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add integer to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto intObject = Value(1024);
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(intObject);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add float to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto floatObject = Value(1024.1024f);
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(floatObject);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// add double to the plist
|
2019-11-23 20:27:39 +08:00
|
|
|
auto doubleObject = Value(1024.123);
|
2022-08-08 13:18:33 +08:00
|
|
|
array.emplace_back(doubleObject);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// end with /
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fullPath = writablePath + "testWriteValueVector.plist";
|
2019-11-23 20:27:39 +08:00
|
|
|
if (FileUtils::getInstance()->writeValueVectorToFile(array, fullPath.c_str()))
|
|
|
|
{
|
|
|
|
log("see the plist file at %s", fullPath.c_str());
|
|
|
|
writeResult->setString("write success");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("write plist file failed");
|
|
|
|
writeResult->setString("write failed");
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ValueVector readArray = FileUtils::getInstance()->getValueVectorFromFile(fullPath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string readDataStr = "read data:\n";
|
|
|
|
// read value map data
|
|
|
|
ValueMap readMapInArray = readArray.at(0).asValueMap();
|
|
|
|
readDataStr += " mapValue:[\"string1\"][" + readMapInArray["string1"].asString() + "]\n";
|
|
|
|
readDataStr += " mapValue:[\"string2\"][" + readMapInArray["string2"].asString() + "]\n";
|
|
|
|
|
|
|
|
// read string data
|
|
|
|
readDataStr += " stringValue:" + readArray.at(1).asString() + "\n";
|
|
|
|
|
|
|
|
// read value vector data
|
|
|
|
ValueVector readVectorInArray = readArray.at(2).asValueVector();
|
|
|
|
readDataStr += " vectorValue:[1]" + readVectorInArray.at(0).asString() + "\n";
|
|
|
|
readDataStr += " vectorValue:[2]" + readVectorInArray.at(1).asString() + "\n";
|
|
|
|
|
|
|
|
// read bool data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " boolValue:" + StringUtils::format("%d", readArray.at(3).asBool()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read int data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " intValue:" + StringUtils::format("%d", readArray.at(4).asInt()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read float data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " floatValue:" + StringUtils::format("%f", readArray.at(5).asFloat()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// read double data
|
2020-08-18 11:33:18 +08:00
|
|
|
readDataStr += " doubleValue:" + StringUtils::format("%f", readArray.at(6).asDouble()) + "\n";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
readResult->setString(readDataStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteValueVector::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueVector::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestWriteValueVector to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueVector::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnicodePath
|
|
|
|
|
|
|
|
void TestUnicodePath::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto util = FileUtils::getInstance();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
int x = s.width / 2, y = s.height / 5;
|
2019-11-23 20:27:39 +08:00
|
|
|
Label* label = nullptr;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string dir = "中文路径/";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string filename = "测试文件.test";
|
|
|
|
|
|
|
|
std::string act;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto getMsg = [&act](bool b, std::string_view path) -> std::string {
|
2019-11-23 20:27:39 +08:00
|
|
|
char msg[512];
|
2021-12-31 12:12:40 +08:00
|
|
|
snprintf((char*)msg, 512, "%s for %s path: \"%s\"", b ? "success" : "failed", act.c_str(), path.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
act = "create";
|
2019-11-23 20:27:39 +08:00
|
|
|
bool isExist = util->isDirectoryExist(dirPath);
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(getMsg(isExist, dirPath), "", 12, Size(s.width, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setPosition(x, y * 4);
|
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
if (isExist)
|
|
|
|
{
|
|
|
|
// Check whether unicode file should be create or not
|
|
|
|
std::string filePath = dirPath + filename;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!util->isFileExist(filePath))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string writeDataStr = " 测试字符串.";
|
|
|
|
Data writeData;
|
2021-12-31 12:12:40 +08:00
|
|
|
writeData.copy((unsigned char*)writeDataStr.c_str(), writeDataStr.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
util->writeDataToFile(writeData, filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
isExist = util->isFileExist(filePath);
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(getMsg(isExist, filePath), "", 12, Size(s.width, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setPosition(x, y * 3);
|
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
act = "remove";
|
|
|
|
if (isExist)
|
|
|
|
{
|
|
|
|
// read file content and log it
|
|
|
|
unsigned char* buffer = nullptr;
|
2021-12-31 12:12:40 +08:00
|
|
|
Data readData = util->getDataFromFile(filePath);
|
|
|
|
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (readData.getSize() + 1));
|
2019-11-23 20:27:39 +08:00
|
|
|
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 TestUnicodePath::onExit()
|
|
|
|
{
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils* sharedFileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
sharedFileUtils->purgeCachedEntries();
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestUnicodePath::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: check unicode path";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestUnicodePath::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestIsFileExist
|
|
|
|
|
|
|
|
void TestIsFileExistAsync::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sharedFileUtils->isFileExist("Images/grossini.png", [=](bool isExist) {
|
2022-10-12 07:04:36 +08:00
|
|
|
AXASSERT(std::this_thread::get_id() == Director::getInstance()->getAxmolThreadId(),
|
2021-12-31 12:12:40 +08:00
|
|
|
"Callback should be on cocos thread");
|
|
|
|
auto label = Label::createWithSystemFont(
|
|
|
|
isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);
|
|
|
|
label->setPosition(s.width / 2, s.height / 3);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(label);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.xcf");
|
2021-12-31 12:12:40 +08:00
|
|
|
label = Label::createWithSystemFont(
|
|
|
|
isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist", "", 20);
|
|
|
|
label->setPosition(s.width / 2, s.height / 3 * 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(label);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExistAsync::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExistAsync::title() const
|
|
|
|
{
|
|
|
|
return "FileUtilsAsync: check whether the file exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExistAsync::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsDirectoryExistAsync::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto util = FileUtils::getInstance();
|
2021-12-31 12:12:40 +08:00
|
|
|
int x = s.width / 2, y = s.height / 3;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string dir;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto getMsg = [](bool b, std::string_view dir) -> std::string {
|
2019-11-23 20:27:39 +08:00
|
|
|
char msg[512];
|
2021-12-31 12:12:40 +08:00
|
|
|
snprintf((char*)msg, 512, "%s for dir: \"%s\"", b ? "success" : "failed", dir.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return std::string(msg);
|
|
|
|
};
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dir = util->getWritablePath();
|
|
|
|
util->isDirectoryExist(dir, [=](bool exists) {
|
2023-12-08 16:30:34 +08:00
|
|
|
AXASSERT(exists, "Writable path should exist");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::createWithSystemFont(getMsg(exists, dir), "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsDirectoryExistAsync::onExit()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
FileUtils* sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// reset filename lookup
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExistAsync::title() const
|
|
|
|
{
|
|
|
|
return "FileUtilsAsync: check whether the directory exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExistAsync::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFileFuncsAsync::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
int x = s.width / 2, y = s.height / 5;
|
|
|
|
|
|
|
|
std::string filename = "__test.test";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string filename2 = "__newtest.test";
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string filepath = sharedFileUtils->getWritablePath() + filename;
|
|
|
|
std::string content = "Test string content to put into created file";
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string msg;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
auto out = fopen(filepath.c_str(), "w");
|
2019-11-23 20:27:39 +08:00
|
|
|
fputs(content.c_str(), out);
|
|
|
|
fclose(out);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sharedFileUtils->isFileExist(filepath, [=](bool exists) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(exists, "File could not be found");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::createWithSystemFont("Test file '__test.test' created", "", 20);
|
|
|
|
label->setPosition(x, y * 4);
|
|
|
|
this->addChild(label);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2021-09-05 20:45:32 +08:00
|
|
|
sharedFileUtils->getFileSize(filepath, [=](int32_t size) {
|
2021-12-31 12:12:40 +08:00
|
|
|
auto msg = StringUtils::format("getFileSize: Test file size equals %d", size);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::createWithSystemFont(msg, "", 20);
|
|
|
|
label->setPosition(x, y * 3);
|
|
|
|
this->addChild(label);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
sharedFileUtils->renameFile(sharedFileUtils->getWritablePath(), filename, filename2, [=](bool success) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(success, "Was not able to properly rename file");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::createWithSystemFont("renameFile: Test file renamed to '__newtest.test'", "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sharedFileUtils->removeFile(sharedFileUtils->getWritablePath() + filename2, [=](bool success) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(success, "Was not able to remove file");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::createWithSystemFont("removeFile: Test file removed", "", 20);
|
|
|
|
label->setPosition(x, y * 1);
|
|
|
|
this->addChild(label);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncsAsync::title() const
|
|
|
|
{
|
|
|
|
return "FileUtilsAsync: file control functions";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncsAsync::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteStringAsync::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fileName = "writeStringTest.txt";
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the string data will be write into a file";
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fullPath = writablePath + fileName;
|
|
|
|
|
|
|
|
FileUtils::getInstance()->writeStringToFile(writeDataStr, fullPath, [=](bool success) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(success, "Write String to data failed");
|
2019-11-23 20:27:39 +08:00
|
|
|
writeResult->setString("write success:" + writeDataStr);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FileUtils::getInstance()->getStringFromFile(fullPath, [=](std::string_view value) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!value.empty(), "String should be readable");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
std::string strVal = "read success: ";
|
|
|
|
readResult->setString(strVal.append(value));
|
2019-11-23 20:27:39 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteStringAsync::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteStringAsync::title() const
|
|
|
|
{
|
|
|
|
return "FileUtilsAsync: TestWriteString to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteStringAsync::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteDataAsync::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto writeResult = Label::createWithTTF("show writeResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(writeResult);
|
|
|
|
writeResult->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto readResult = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(readResult);
|
|
|
|
readResult->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string fileName = "writeDataTest.txt";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the binary data will be write into a file";
|
|
|
|
Data writeData;
|
2021-12-31 12:12:40 +08:00
|
|
|
writeData.copy((unsigned char*)writeDataStr.c_str(), writeDataStr.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fullPath = writablePath + fileName;
|
|
|
|
|
|
|
|
FileUtils::getInstance()->writeDataToFile(writeData, fullPath, [=](bool success) {
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
writeResult->setString("Write result success");
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
writeResult->setString("Write result failure");
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
FileUtils::getInstance()->getDataFromFile(fullPath, [=](const Data& readData) {
|
|
|
|
auto buffer = (unsigned char*)malloc(sizeof(unsigned char) * (readData.getSize() + 1));
|
|
|
|
memcpy(buffer, readData.getBytes(), readData.getSize());
|
|
|
|
buffer[readData.getSize()] = '\0';
|
|
|
|
std::string readDataStr((const char*)buffer);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
readResult->setString("read success:" + readDataStr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteDataAsync::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteDataAsync::title() const
|
|
|
|
{
|
|
|
|
return "FileUtilsAsync: TestWriteData to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteDataAsync::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestListFiles::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto infoLabel = Label::createWithTTF("show file count, should not be 0", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(infoLabel);
|
|
|
|
infoLabel->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto cntLabel = Label::createWithTTF("show readResult", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(cntLabel);
|
|
|
|
cntLabel->setPosition(winSize.width / 2, winSize.height / 3);
|
|
|
|
// writeTest
|
|
|
|
std::vector<std::string> listFonts = FileUtils::getInstance()->listFiles("fonts");
|
2021-12-31 12:12:40 +08:00
|
|
|
auto defaultPath = FileUtils::getInstance()->getDefaultResourceRootPath();
|
|
|
|
std::vector<std::string> list = FileUtils::getInstance()->listFiles(defaultPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
char cntBuffer[200] = {0};
|
|
|
|
snprintf(cntBuffer, 200, "'fonts/' %zu, $defaultResourceRootPath %zu", listFonts.size(), list.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < listFonts.size(); i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("fonts/ %d: \t %s", i, listFonts[i].c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < list.size(); i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("defResRootPath %d: \t %s", i, list[i].c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cntLabel->setString(cntBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestListFiles::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestListFiles::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: list files of directory";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestListFiles::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExistRejectFolder::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto infoLabel = Label::createWithTTF("tests folder 'NavMesh/maps', expect to be false", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(infoLabel);
|
|
|
|
infoLabel->setPosition(winSize.width / 2, winSize.height * 3 / 4);
|
|
|
|
|
|
|
|
auto cntLabel = Label::createWithTTF("waiting...", "fonts/Thonburi.ttf", 18);
|
|
|
|
this->addChild(cntLabel);
|
|
|
|
cntLabel->setPosition(winSize.width / 2, winSize.height / 3);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
auto exists = FileUtils::getInstance()->isFileExist("NavMesh/maps");
|
2019-11-23 20:27:39 +08:00
|
|
|
auto isDirectory = FileUtils::getInstance()->isDirectoryExist("NavMesh/maps");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
char cntBuffer[200] = {0};
|
|
|
|
snprintf(cntBuffer, 200, "isDir: %s, isFile: %s", isDirectory ? "true" : "false", exists ? "true" : "false");
|
2019-11-23 20:27:39 +08:00
|
|
|
cntLabel->setString(cntBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExistRejectFolder::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExistRejectFolder::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: isFileExist(direname)";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsFileExistRejectFolder::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|