2013-01-28 15:17:20 +08:00
|
|
|
#include "FileUtilsTest.h"
|
|
|
|
|
2015-04-09 12:23:47 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
FileUtilsTests::FileUtilsTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(TestResolutionDirectories);
|
|
|
|
ADD_TEST_CASE(TestSearchPath);
|
|
|
|
ADD_TEST_CASE(TestFilenameLookup);
|
|
|
|
ADD_TEST_CASE(TestIsFileExist);
|
2015-07-13 22:44:40 +08:00
|
|
|
ADD_TEST_CASE(TestIsDirectoryExist);
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(TestFileFuncs);
|
|
|
|
ADD_TEST_CASE(TestDirectoryFuncs);
|
|
|
|
ADD_TEST_CASE(TextWritePlist);
|
2015-07-07 14:06:59 +08:00
|
|
|
ADD_TEST_CASE(TestWriteString);
|
2016-04-26 13:37:22 +08:00
|
|
|
ADD_TEST_CASE(TestGetContents);
|
2015-07-07 14:06:59 +08:00
|
|
|
ADD_TEST_CASE(TestWriteData);
|
|
|
|
ADD_TEST_CASE(TestWriteValueMap);
|
|
|
|
ADD_TEST_CASE(TestWriteValueVector);
|
2015-07-13 22:44:40 +08:00
|
|
|
ADD_TEST_CASE(TestUnicodePath);
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// TestResolutionDirectories
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
void TestResolutionDirectories::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string ret;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
|
2013-12-19 03:45:30 +08:00
|
|
|
std::vector<std::string> searchPaths = _defaultSearchPathArray;
|
2013-01-28 15:17:20 +08:00
|
|
|
searchPaths.insert(searchPaths.begin(), "Misc");
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(searchPaths);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
|
2013-12-19 03:45:30 +08:00
|
|
|
std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray;
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
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");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
for( int i=1; i<7; i++) {
|
2015-07-15 12:04:48 +08:00
|
|
|
auto filename = StringUtils::format("test%d.txt", i);
|
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename);
|
|
|
|
log("%s -> %s", filename.c_str(), ret.c_str());
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestResolutionDirectories::onExit()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
// reset search path
|
|
|
|
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
|
2013-06-15 14:03:30 +08:00
|
|
|
sharedFileUtils->setSearchResolutionsOrder(_defaultResolutionsOrderArray);
|
2013-01-28 15:17:20 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestResolutionDirectories::title() const
|
2013-01-28 15:17:20 +08:00
|
|
|
{
|
|
|
|
return "FileUtils: resolutions in directories";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestResolutionDirectories::subtitle() const
|
2013-01-28 15:17:20 +08:00
|
|
|
{
|
|
|
|
return "See the console";
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// TestSearchPath
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
void TestSearchPath::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string ret;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
|
2013-12-19 03:45:30 +08:00
|
|
|
std::vector<std::string> searchPaths = _defaultSearchPathArray;
|
|
|
|
std::string writablePath = sharedFileUtils->getWritablePath();
|
|
|
|
std::string fileName = writablePath+"external.txt";
|
2013-01-28 17:43:53 +08:00
|
|
|
char szBuf[100] = "Hello Cocos2d-x!";
|
|
|
|
FILE* fp = fopen(fileName.c_str(), "wb");
|
|
|
|
if (fp)
|
|
|
|
{
|
2013-06-01 08:06:11 +08:00
|
|
|
size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp);
|
2013-07-28 10:22:12 +08:00
|
|
|
CCASSERT(ret != 0, "fwrite function returned zero value");
|
2013-01-28 17:43:53 +08:00
|
|
|
fclose(fp);
|
2013-07-28 10:22:12 +08:00
|
|
|
if (ret != 0)
|
2013-07-24 06:20:22 +08:00
|
|
|
log("Writing file to writable path succeed.");
|
2013-01-28 17:43:53 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 17:43:53 +08:00
|
|
|
searchPaths.insert(searchPaths.begin(), writablePath);
|
|
|
|
searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1");
|
|
|
|
searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2");
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(searchPaths);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
|
2013-12-19 03:45:30 +08:00
|
|
|
std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad");
|
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
for( int i=1; i<3; i++) {
|
2015-07-15 12:04:48 +08:00
|
|
|
auto filename = StringUtils::format("file%d.txt", i);
|
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename);
|
|
|
|
log("%s -> %s", filename.c_str(), ret.c_str());
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 17:43:53 +08:00
|
|
|
// Gets external.txt from writable path
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string fullPath = sharedFileUtils->fullPathForFilename("external.txt");
|
2013-07-24 06:20:22 +08:00
|
|
|
log("external file path = %s", fullPath.c_str());
|
2013-03-03 10:32:09 +08:00
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-01-28 17:43:53 +08:00
|
|
|
fp = fopen(fullPath.c_str(), "rb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
char szReadBuf[100] = {0};
|
2015-07-13 22:44:40 +08:00
|
|
|
size_t read = fread(szReadBuf, 1, strlen(szReadBuf), fp);
|
2013-03-03 10:32:09 +08:00
|
|
|
if (read > 0)
|
2013-07-24 06:20:22 +08:00
|
|
|
log("The content of file from writable path: %s", szReadBuf);
|
2013-01-28 17:43:53 +08:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestSearchPath::onExit()
|
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
// reset search path
|
|
|
|
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
|
2013-06-15 14:03:30 +08:00
|
|
|
sharedFileUtils->setSearchResolutionsOrder(_defaultResolutionsOrderArray);
|
2013-01-28 15:17:20 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestSearchPath::title() const
|
2013-01-28 15:17:20 +08:00
|
|
|
{
|
|
|
|
return "FileUtils: search path";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestSearchPath::subtitle() const
|
2013-01-28 15:17:20 +08:00
|
|
|
{
|
|
|
|
return "See the console";
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// TestFilenameLookup
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
void TestFilenameLookup::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap dict;
|
2013-12-03 14:47:35 +08:00
|
|
|
dict["grossini.bmp"] = Value("Images/grossini.png");
|
|
|
|
dict["grossini.xcf"] = Value("Images/grossini.png");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->setFilenameLookupDictionary(dict);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
// Instead of loading carlitos.xcf, it will load grossini.png
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = Sprite::create("grossini.xcf");
|
2013-01-28 15:17:20 +08:00
|
|
|
this->addChild(sprite);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-08-28 11:41:18 +08:00
|
|
|
sprite->setPosition(s.width/2, s.height/2);
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestFilenameLookup::onExit()
|
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
// reset filename lookup
|
2013-12-04 17:46:57 +08:00
|
|
|
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestFilenameLookup::title() const
|
2013-01-28 15:17:20 +08:00
|
|
|
{
|
|
|
|
return "FileUtils: filename lookup";
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// TestIsFileExist
|
2013-02-01 22:18:43 +08:00
|
|
|
|
|
|
|
void TestIsFileExist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
2013-02-01 22:18:43 +08:00
|
|
|
bool isExist = false;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 22:18:43 +08:00
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.png");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(s.width/2, s.height/3);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 22:18:43 +08:00
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.xcf");
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(s.width/2, s.height/3*2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2013-02-01 22:18:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExist::onExit()
|
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
// reset filename lookup
|
2013-12-04 17:46:57 +08:00
|
|
|
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 22:18:43 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestIsFileExist::title() const
|
2013-02-01 22:18:43 +08:00
|
|
|
{
|
|
|
|
return "FileUtils: check whether the file exists";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TestIsFileExist::subtitle() const
|
2013-02-01 22:18:43 +08:00
|
|
|
{
|
|
|
|
return "";
|
2013-03-03 10:32:09 +08:00
|
|
|
}
|
2013-04-28 00:36:37 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
// TestIsDirectoryExist
|
|
|
|
|
|
|
|
void TestIsDirectoryExist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto util = FileUtils::getInstance();
|
|
|
|
int x = s.width/2, y = s.height/3;
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
Label* label = nullptr;
|
|
|
|
std::string dir;
|
2015-07-17 15:20:49 +08:00
|
|
|
auto getMsg = [&dir](bool b)-> std::string
|
2015-07-13 22:44:40 +08:00
|
|
|
{
|
2015-07-17 15:20:49 +08:00
|
|
|
char msg[512];
|
2015-07-13 22:44:40 +08:00
|
|
|
snprintf((char *)msg, 512, "%s for dir: \"%s\"", b ? "success" : "failed", dir.c_str());
|
2015-07-17 15:20:49 +08:00
|
|
|
return std::string(msg);
|
2015-07-13 22:44:40 +08:00
|
|
|
};
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
dir = "Images";
|
|
|
|
label = Label::createWithSystemFont(getMsg(util->isDirectoryExist(dir)), "", 20);
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsDirectoryExist::onExit()
|
|
|
|
{
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
// reset filename lookup
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExist::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: check whether the directory exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestIsDirectoryExist::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// TestFileFuncs
|
|
|
|
|
|
|
|
void TestFileFuncs::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
int x = s.width/2,
|
|
|
|
y = s.height/5;
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
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;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
FILE *out = fopen(filepath.c_str(), "w");
|
|
|
|
fputs(content.c_str(), out);
|
|
|
|
fclose(out);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// Check whether file can be created
|
|
|
|
if (sharedFileUtils->isFileExist(filepath))
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("Test file '__test.test' created", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 4);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// getFileSize Test
|
|
|
|
long size = sharedFileUtils->getFileSize(filepath);
|
|
|
|
msg = StringUtils::format("getFileSize: Test file size equals %ld", size);
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 3);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// renameFile Test
|
|
|
|
if (sharedFileUtils->renameFile(sharedFileUtils->getWritablePath(), filename, filename2))
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("renameFile: Test file renamed to '__newtest.test'", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// removeFile Test
|
|
|
|
filepath = sharedFileUtils->getWritablePath() + filename2;
|
|
|
|
if (sharedFileUtils->removeFile(filepath))
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("removeFile: Test file removed", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 1);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("removeFile: Failed to remove test file", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 1);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("renameFile: Failed to rename test file to '__newtest.test', further test skipped", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont("Test file can not be created, test skipped", "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 4);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncs::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: file control functions";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestFileFuncs::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDirectoryFuncs
|
|
|
|
|
|
|
|
void TestDirectoryFuncs::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
int x = s.width/2,
|
|
|
|
y = s.height/4;
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-07-07 17:41:36 +08:00
|
|
|
std::string dir = sharedFileUtils->getWritablePath() + "__test";
|
2014-06-24 14:51:35 +08:00
|
|
|
std::string subDir = "dir1/dir2";
|
2016-07-07 17:41:36 +08:00
|
|
|
std::string fullSubDir = dir + "/" + subDir;
|
2014-06-24 14:51:35 +08:00
|
|
|
std::string msg;
|
|
|
|
bool ok;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// Check whether dir can be created
|
|
|
|
ok = sharedFileUtils->createDirectory(dir);
|
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(dir))
|
|
|
|
{
|
|
|
|
msg = StringUtils::format("createDirectory: Directory '__test' created");
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 3);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// Create sub directories recursively
|
2016-07-07 17:41:36 +08:00
|
|
|
ok = sharedFileUtils->createDirectory(fullSubDir);
|
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(fullSubDir))
|
2014-06-24 14:51:35 +08:00
|
|
|
{
|
2014-07-21 16:51:48 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Sub directories '%s' created", subDir.c_str());
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 16:51:48 +08:00
|
|
|
msg = StringUtils::format("createDirectory: Failed to create sub directories '%s'", subDir.c_str());
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// Remove directory
|
|
|
|
ok = sharedFileUtils->removeDirectory(dir);
|
|
|
|
if (ok && !sharedFileUtils->isDirectoryExist(dir))
|
|
|
|
{
|
|
|
|
msg = StringUtils::format("removeDirectory: Directory '__test' removed");
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = StringUtils::format("removeDirectory: Failed to remove directory '__test'");
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = StringUtils::format("createDirectory: Directory '__test' can not be created");
|
2014-07-09 09:52:21 +08:00
|
|
|
label = Label::createWithSystemFont(msg, "", 20);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(x, y * 2);
|
2014-07-09 09:52:21 +08:00
|
|
|
this->addChild(label);
|
2014-06-24 14:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestDirectoryFuncs::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: directory control functions";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestDirectoryFuncs::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
// TextWritePlist
|
2013-04-28 00:36:37 +08:00
|
|
|
|
|
|
|
void TextWritePlist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2015-07-15 12:04:48 +08:00
|
|
|
auto root = __Dictionary::create();
|
|
|
|
auto string = __String::create("string element value");
|
2013-04-28 00:36:37 +08:00
|
|
|
root->setObject(string, "string element key");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
auto array = __Array::create();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
auto dictInArray = __Dictionary::create();
|
|
|
|
dictInArray->setObject(__String::create("string in dictInArray value 0"), "string in dictInArray key 0");
|
|
|
|
dictInArray->setObject(__String::create("string in dictInArray value 1"), "string in dictInArray key 1");
|
2013-04-28 00:36:37 +08:00
|
|
|
array->addObject(dictInArray);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
array->addObject(__String::create("string in array"));
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
auto arrayInArray = __Array::create();
|
|
|
|
arrayInArray->addObject(__String::create("string 0 in arrayInArray"));
|
|
|
|
arrayInArray->addObject(__String::create("string 1 in arrayInArray"));
|
2013-04-28 00:36:37 +08:00
|
|
|
array->addObject(arrayInArray);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
root->setObject(array, "array");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
auto dictInDict = __Dictionary::create();
|
|
|
|
dictInDict->setObject(__String::create("string in dictInDict value"), "string in dictInDict key");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add boolean to the plist
|
2015-07-15 12:04:48 +08:00
|
|
|
auto booleanObject = __Bool::create(true);
|
2014-03-18 14:59:59 +08:00
|
|
|
dictInDict->setObject(booleanObject, "bool");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add interger to the plist
|
2015-07-15 12:04:48 +08:00
|
|
|
auto intObject = __Integer::create(1024);
|
2014-03-18 14:59:59 +08:00
|
|
|
dictInDict->setObject(intObject, "integer");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add float to the plist
|
2015-07-15 12:04:48 +08:00
|
|
|
auto floatObject = __Float::create(1024.1024f);
|
2014-03-18 14:59:59 +08:00
|
|
|
dictInDict->setObject(floatObject, "float");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add double to the plist
|
2015-07-15 12:04:48 +08:00
|
|
|
auto doubleObject = __Double::create(1024.123);
|
2014-03-18 14:59:59 +08:00
|
|
|
dictInDict->setObject(doubleObject, "double");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
root->setObject(dictInDict, "dictInDict, Hello World");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
// end with /
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
2013-04-28 00:36:37 +08:00
|
|
|
std::string fullPath = writablePath + "text.plist";
|
|
|
|
if(root->writeToFile(fullPath.c_str()))
|
2013-07-24 06:20:22 +08:00
|
|
|
log("see the plist file at %s", fullPath.c_str());
|
2013-04-28 00:36:37 +08:00
|
|
|
else
|
2013-07-24 06:20:22 +08:00
|
|
|
log("write plist file failed");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF(fullPath.c_str(), "fonts/Thonburi.ttf", 6);
|
2013-04-28 00:36:37 +08:00
|
|
|
this->addChild(label);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(winSize.width/2, winSize.height/3);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-26 15:04:45 +08:00
|
|
|
auto loadDict = __Dictionary::createWithContentsOfFile(fullPath.c_str());
|
|
|
|
auto loadDictInDict = (__Dictionary*)loadDict->objectForKey("dictInDict, Hello World");
|
|
|
|
auto boolValue = (__String*)loadDictInDict->objectForKey("bool");
|
2014-11-27 05:31:44 +08:00
|
|
|
log("%s",boolValue->getCString());
|
2014-03-26 15:04:45 +08:00
|
|
|
auto floatValue = (__String*)loadDictInDict->objectForKey("float");
|
2014-11-27 05:31:44 +08:00
|
|
|
log("%s",floatValue->getCString());
|
2014-03-26 15:04:45 +08:00
|
|
|
auto intValue = (__String*)loadDictInDict->objectForKey("integer");
|
2014-11-27 05:31:44 +08:00
|
|
|
log("%s",intValue->getCString());
|
2014-03-26 15:04:45 +08:00
|
|
|
auto doubleValue = (__String*)loadDictInDict->objectForKey("double");
|
2014-11-27 05:31:44 +08:00
|
|
|
log("%s",doubleValue->getCString());
|
2014-03-19 14:31:15 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextWritePlist::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TextWritePlist::title() const
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
return "FileUtils: Dictionary to plist";
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TextWritePlist::subtitle() const
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath().c_str();
|
2013-04-28 00:36:37 +08:00
|
|
|
return ("See plist file at your writablePath");
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
std::string fileName = "writeStringTest.txt";
|
|
|
|
|
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the string data will be write into a file";
|
|
|
|
std::string fullPath = writablePath + fileName;
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
class CustomBuffer : public ResizableBuffer {};
|
|
|
|
|
|
|
|
struct AlreadyExistsBuffer {};
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
template<>
|
|
|
|
class ResizableBufferAdapter<AlreadyExistsBuffer> : public ResizableBuffer {
|
|
|
|
public:
|
|
|
|
explicit ResizableBufferAdapter(AlreadyExistsBuffer* buffer) {
|
|
|
|
}
|
|
|
|
virtual void resize(size_t size) override {
|
|
|
|
|
|
|
|
}
|
|
|
|
virtual void* buffer() const override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
static void saveAsBinaryText(const std::string& filename, const std::vector<char>& binary){
|
|
|
|
auto fs = FileUtils::getInstance();
|
|
|
|
std::string text(binary.begin(), binary.end());
|
|
|
|
fs->writeStringToFile(text, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string FileErrors[] = {
|
|
|
|
"OK",
|
|
|
|
"NotExists",
|
|
|
|
"OpenFailed",
|
2016-06-25 11:58:55 +08:00
|
|
|
"ReadFailed",
|
2016-04-26 13:37:22 +08:00
|
|
|
"NotInitialized",
|
|
|
|
"TooLarge",
|
|
|
|
"ObtainSizeFailed",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void TestGetContents::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto fs = FileUtils::getInstance();
|
|
|
|
|
|
|
|
auto testIfCompiles = [fs]() {
|
|
|
|
fs->getContents("", (CustomBuffer*)(nullptr));
|
|
|
|
AlreadyExistsBuffer buf;
|
|
|
|
fs->getContents("", &buf);
|
|
|
|
};
|
|
|
|
|
|
|
|
(void)(testIfCompiles);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::vector<char> binary = {'\r','\n','\r','\n','\0','\0','\r','\n'};
|
|
|
|
_generatedFile = fs->getWritablePath() + "file-with-zeros-and-crlf";
|
|
|
|
saveAsBinaryText(_generatedFile, binary);
|
|
|
|
|
|
|
|
|
|
|
|
auto runTests = [&]() {
|
|
|
|
// Test read string in binary mode
|
|
|
|
std::string bs;
|
|
|
|
fs->getContents(_generatedFile, &bs);
|
|
|
|
if ( bs.size() != binary.size() || !std::equal( bs.begin(), bs.end(), binary.begin() ) )
|
|
|
|
return std::string("failed: read as binary string");
|
|
|
|
|
|
|
|
// Text read string in text mode
|
|
|
|
std::string ts = fs->getStringFromFile(_generatedFile);
|
|
|
|
if (ts != "\r\n\r\n")
|
|
|
|
return std::string("failed: read as zero terminated string");
|
|
|
|
|
|
|
|
|
|
|
|
std::string files[] = {_generatedFile, "background.wav", "fileLookup.plist"};
|
|
|
|
for (auto& file : files) {
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
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();
|
|
|
|
std::string fileName = "writeDataTest.txt";
|
|
|
|
|
|
|
|
// writeTest
|
|
|
|
std::string writeDataStr = "the binary data will be write into a file";
|
|
|
|
Data writeData;
|
|
|
|
writeData.copy((unsigned char *)writeDataStr.c_str(), writeDataStr.size());
|
|
|
|
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;
|
|
|
|
Data readData = FileUtils::getInstance()->getDataFromFile(fullPath);
|
|
|
|
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 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";
|
2016-06-10 13:52:35 +08:00
|
|
|
mapInValueMap["none"].getType();
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
valueMap["data0"] = Value(mapInValueMap);
|
|
|
|
|
|
|
|
valueMap["data1"] = Value("string in array");
|
|
|
|
|
|
|
|
ValueVector arrayInMap;
|
|
|
|
arrayInMap.push_back(Value("string 0 in arrayInMap"));
|
|
|
|
arrayInMap.push_back(Value("string 1 in arrayInMap"));
|
|
|
|
valueMap["data2"] = arrayInMap;
|
|
|
|
|
|
|
|
//add boolean to the plist
|
|
|
|
auto booleanObject = Value(true);
|
|
|
|
valueMap["data3"] = booleanObject;
|
|
|
|
|
|
|
|
//add interger to the plist
|
|
|
|
auto intObject = Value(1024);
|
|
|
|
valueMap["data4"] = intObject;
|
|
|
|
|
|
|
|
//add float to the plist
|
|
|
|
auto floatObject = Value(1024.1024f);
|
|
|
|
valueMap["data5"] = floatObject;
|
|
|
|
|
|
|
|
//add double to the plist
|
|
|
|
auto doubleObject = Value(1024.123);
|
|
|
|
valueMap["data6"] = doubleObject;
|
|
|
|
|
|
|
|
|
|
|
|
// end with /
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
|
|
|
std::string fullPath = writablePath + "testWriteValueMap.plist";
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap readValueMap = FileUtils::getInstance()->getValueMapFromFile(fullPath.c_str());
|
|
|
|
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
|
|
|
|
readDataStr += " boolValue:" + StringUtils::format("%d", readValueMap["data3"].asBool()) + "\n";
|
|
|
|
|
|
|
|
// read int data
|
|
|
|
readDataStr += " intValue:" + StringUtils::format("%d", readValueMap["data4"].asInt()) + "\n";
|
|
|
|
|
|
|
|
// read float data
|
|
|
|
readDataStr += " floatValue:" + StringUtils::format("%f", readValueMap["data5"].asFloat()) + "\n";
|
|
|
|
|
|
|
|
// read double data
|
|
|
|
readDataStr += " doubleValue:" + StringUtils::format("%f", readValueMap["data6"].asDouble()) + "\n";
|
|
|
|
|
|
|
|
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";
|
|
|
|
array.push_back(Value(mapInArray));
|
|
|
|
|
|
|
|
array.push_back(Value("string in array"));
|
|
|
|
|
|
|
|
ValueVector arrayInArray;
|
|
|
|
arrayInArray.push_back(Value("string 0 in arrayInArray"));
|
|
|
|
arrayInArray.push_back(Value("string 1 in arrayInArray"));
|
|
|
|
array.push_back(Value(arrayInArray));
|
|
|
|
|
|
|
|
//add boolean to the plist
|
|
|
|
auto booleanObject = Value(true);
|
|
|
|
array.push_back(booleanObject);
|
|
|
|
|
|
|
|
//add interger to the plist
|
|
|
|
auto intObject = Value(1024);
|
|
|
|
array.push_back(intObject);
|
|
|
|
|
|
|
|
//add float to the plist
|
|
|
|
auto floatObject = Value(1024.1024f);
|
|
|
|
array.push_back(floatObject);
|
|
|
|
|
|
|
|
//add double to the plist
|
|
|
|
auto doubleObject = Value(1024.123);
|
|
|
|
array.push_back(doubleObject);
|
|
|
|
|
|
|
|
|
|
|
|
// end with /
|
|
|
|
std::string writablePath = FileUtils::getInstance()->getWritablePath();
|
|
|
|
std::string fullPath = writablePath + "testWriteValueVector.plist";
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueVector readArray = FileUtils::getInstance()->getValueVectorFromFile(fullPath.c_str());
|
|
|
|
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
|
|
|
|
readDataStr += " boolValue:" + StringUtils::format("%d", readArray.at(3).asBool()) + "\n";
|
|
|
|
|
|
|
|
// read int data
|
|
|
|
readDataStr += " intValue:" + StringUtils::format("%d", readArray.at(4).asInt()) + "\n";
|
|
|
|
|
|
|
|
// read float data
|
|
|
|
readDataStr += " floatValue:" + StringUtils::format("%f", readArray.at(5).asFloat()) + "\n";
|
|
|
|
|
|
|
|
// read double data
|
|
|
|
readDataStr += " doubleValue:" + StringUtils::format("%f", readArray.at(6).asDouble()) + "\n";
|
|
|
|
|
|
|
|
readResult->setString(readDataStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWriteValueVector::onExit()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueVector::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: TestWriteValueVector to files";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestWriteValueVector::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2015-07-13 22:44:40 +08:00
|
|
|
|
|
|
|
// TestUnicodePath
|
|
|
|
|
|
|
|
void TestUnicodePath::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto util = FileUtils::getInstance();
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
int x = s.width/2,
|
|
|
|
y = s.height/5;
|
|
|
|
Label* label = nullptr;
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
std::string dir = "中文路径/";
|
|
|
|
std::string filename = "测试文件.test";
|
2015-07-17 15:20:49 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
std::string act;
|
2015-07-17 15:20:49 +08:00
|
|
|
auto getMsg = [&act](bool b, const std::string& path)-> std::string
|
2015-07-13 22:44:40 +08:00
|
|
|
{
|
2015-07-17 15:20:49 +08:00
|
|
|
char msg[512];
|
2015-07-13 22:44:40 +08:00
|
|
|
snprintf((char *)msg, 512, "%s for %s path: \"%s\"", b ? "success" : "failed", act.c_str(), path.c_str());
|
2015-07-17 15:20:49 +08:00
|
|
|
return std::string(msg);
|
2015-07-13 22:44:40 +08:00
|
|
|
};
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
// Check whether unicode dir should be create or not
|
|
|
|
std::string dirPath = util->getWritablePath() + dir;
|
|
|
|
if (!util->isDirectoryExist(dirPath))
|
|
|
|
{
|
|
|
|
util->createDirectory(dirPath);
|
|
|
|
}
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
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);
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
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);
|
|
|
|
}
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
isExist = util->isFileExist(filePath);
|
|
|
|
label = Label::createWithSystemFont(getMsg(isExist, filePath), "", 12, Size(s.width, 0));
|
|
|
|
label->setPosition(x, y * 3);
|
|
|
|
this->addChild(label);
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
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);
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
// remove test file
|
|
|
|
label = Label::createWithSystemFont(getMsg(util->removeFile(filePath), filePath), "", 12, Size(s.width, 0));
|
|
|
|
label->setPosition(x, y * 2);
|
|
|
|
this->addChild(label);
|
|
|
|
}
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
// 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()
|
|
|
|
{
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
|
|
|
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestUnicodePath::title() const
|
|
|
|
{
|
|
|
|
return "FileUtils: check unicode path";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestUnicodePath::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|