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);
|
|
|
|
ADD_TEST_CASE(TestFileFuncs);
|
|
|
|
ADD_TEST_CASE(TestDirectoryFuncs);
|
|
|
|
ADD_TEST_CASE(TextWritePlist);
|
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;
|
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);
|
2013-01-28 15:17:20 +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");
|
|
|
|
|
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
|
|
|
|
|
|
|
for( int i=1; i<7; i++) {
|
2013-08-16 16:05:27 +08:00
|
|
|
auto filename = String::createWithFormat("test%d.txt", i);
|
2013-01-28 15:17:20 +08:00
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
|
2013-07-24 06:20:22 +08:00
|
|
|
log("%s -> %s", filename->getCString(), 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();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
// reset search path
|
2013-06-15 14:03:30 +08:00
|
|
|
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
|
|
|
|
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();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string ret;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-01-28 17:43:53 +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-ipad");
|
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
|
|
|
|
|
|
|
for( int i=1; i<3; i++) {
|
2013-08-16 16:05:27 +08:00
|
|
|
auto filename = String::createWithFormat("file%d.txt", i);
|
2013-01-28 15:17:20 +08:00
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
|
2013-07-24 06:20:22 +08:00
|
|
|
log("%s -> %s", filename->getCString(), ret.c_str());
|
2013-01-28 15:17:20 +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};
|
2014-03-20 16:24:55 +08:00
|
|
|
size_t read = fread(szReadBuf, 1, strlen(szBuf), 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()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
// reset search path
|
2013-06-15 14:03:30 +08:00
|
|
|
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
|
|
|
|
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();
|
|
|
|
|
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");
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
sharedFileUtils->setFilenameLookupDictionary(dict);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
// reset filename lookup
|
2013-12-04 17:46:57 +08:00
|
|
|
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
|
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();
|
2013-02-01 22:18:43 +08:00
|
|
|
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
2013-02-01 22:18:43 +08:00
|
|
|
bool isExist = false;
|
|
|
|
|
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.png");
|
|
|
|
|
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);
|
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()
|
|
|
|
{
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils *sharedFileUtils = FileUtils::getInstance();
|
2013-02-01 22:18:43 +08:00
|
|
|
|
|
|
|
// reset filename lookup
|
2013-12-04 17:46:57 +08:00
|
|
|
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
|
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
|
|
|
|
2014-06-24 14:51:35 +08:00
|
|
|
// TestFileFuncs
|
|
|
|
|
|
|
|
void TestFileFuncs::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto sharedFileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
int x = s.width/2,
|
|
|
|
y = s.height/5;
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
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;
|
|
|
|
|
|
|
|
FILE *out = fopen(filepath.c_str(), "w");
|
|
|
|
fputs(content.c_str(), out);
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
// 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);
|
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);
|
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);
|
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();
|
|
|
|
|
|
|
|
int x = s.width/2,
|
|
|
|
y = s.height/4;
|
2014-07-09 09:52:21 +08:00
|
|
|
Label* label = nullptr;
|
2014-06-24 14:51:35 +08:00
|
|
|
|
|
|
|
std::string dir = sharedFileUtils->getWritablePath() + "__test/";
|
|
|
|
std::string subDir = "dir1/dir2";
|
|
|
|
std::string msg;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Check whether dir can be created
|
|
|
|
ok = sharedFileUtils->createDirectory(dir);
|
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(dir))
|
|
|
|
{
|
|
|
|
msg = StringUtils::format("createDirectory: Directory '__test' created");
|
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);
|
2014-06-24 14:51:35 +08:00
|
|
|
|
|
|
|
// Create sub directories recursively
|
2014-07-21 16:51:48 +08:00
|
|
|
ok = sharedFileUtils->createDirectory(dir + subDir);
|
2014-06-24 14:51:35 +08:00
|
|
|
if (ok && sharedFileUtils->isDirectoryExist(dir + subDir))
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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 "";
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
// TestWritePlist
|
2013-04-28 00:36:37 +08:00
|
|
|
|
|
|
|
void TextWritePlist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
2013-08-16 16:05:27 +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");
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto array = Array::create();
|
2013-04-28 00:36:37 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto dictInArray = Dictionary::create();
|
2013-06-20 14:17:10 +08:00
|
|
|
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);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
array->addObject(String::create("string in array"));
|
2013-04-28 00:36:37 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto arrayInArray = Array::create();
|
2013-06-20 14:17:10 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
root->setObject(array, "array");
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto dictInDict = Dictionary::create();
|
2013-06-20 14:17:10 +08:00
|
|
|
dictInDict->setObject(String::create("string in dictInDict value"), "string in dictInDict key");
|
2014-03-18 14:59:59 +08:00
|
|
|
|
|
|
|
//add boolean to the plist
|
|
|
|
auto booleanObject = Bool::create(true);
|
|
|
|
dictInDict->setObject(booleanObject, "bool");
|
2013-04-28 00:36:37 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add interger to the plist
|
|
|
|
auto intObject = Integer::create(1024);
|
|
|
|
dictInDict->setObject(intObject, "integer");
|
|
|
|
|
|
|
|
//add float to the plist
|
|
|
|
auto floatObject = Float::create(1024.1024f);
|
|
|
|
dictInDict->setObject(floatObject, "float");
|
|
|
|
|
|
|
|
//add double to the plist
|
|
|
|
auto doubleObject = Double::create(1024.123);
|
|
|
|
dictInDict->setObject(doubleObject, "double");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root->setObject(dictInDict, "dictInDict, Hello World");
|
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");
|
2013-04-28 00:36:37 +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);
|
2014-03-19 11:58:11 +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");
|
|
|
|
}
|