Merge v3 branch.

This commit is contained in:
Vincent Yang 2015-07-10 16:39:36 +08:00
commit 1711a70869
11 changed files with 888 additions and 383 deletions

View File

@ -370,8 +370,12 @@ static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxm
*/
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
{
//CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str());
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
return writeValueMapToFile(dict, fullPath);
}
bool FileUtils::writeValueMapToFile(ValueMap& dict, const std::string& fullPath)
{
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
if (nullptr == doc)
return false;
@ -409,6 +413,46 @@ bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
return ret;
}
bool FileUtils::writeValueVectorToFile(ValueVector vecData, const std::string& fullPath)
{
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
if (nullptr == doc)
return false;
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (nullptr == declaration)
{
delete doc;
return false;
}
doc->LinkEndChild(declaration);
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
doc->LinkEndChild(docType);
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
rootEle->SetAttribute("version", "1.0");
if (nullptr == rootEle)
{
delete doc;
return false;
}
doc->LinkEndChild(rootEle);
tinyxml2::XMLElement *innerDict = generateElementForArray(vecData, doc);
if (nullptr == innerDict)
{
delete doc;
return false;
}
rootEle->LinkEndChild(innerDict);
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
delete doc;
return ret;
}
/*
* Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
*/
@ -530,7 +574,39 @@ FileUtils::~FileUtils()
{
}
bool FileUtils::writeStringToFile(std::string dataStr, const std::string& fullPath)
{
Data retData;
retData.copy((unsigned char*)dataStr.c_str(), dataStr.size());
return writeDataToFile(retData, fullPath);
}
bool FileUtils::writeDataToFile(Data retData, const std::string& fullPath)
{
unsigned char* buffer = nullptr;
size_t size = 0;
const char* mode = "wb";
CCASSERT(!fullPath.empty() && retData.getSize() != 0, "Invalid parameters.");
auto fileutils = FileUtils::getInstance();
do
{
// Read the file from hardware
FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
CC_BREAK_IF(!fp);
size = retData.getSize();
fwrite(retData.getBytes(), size, 1, fp);
fclose(fp);
return true;
} while (0);
return false;
}
bool FileUtils::init()
{
@ -1375,6 +1451,7 @@ std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
{
return filenameUtf8;
}
#endif
NS_CC_END

View File

@ -321,15 +321,58 @@ public:
*/
virtual ValueMap getValueMapFromFile(const std::string& filename);
// Converts the contents of a file to a ValueMap.
// This method is used internally.
/** Converts the contents of a file to a ValueMap.
* This method is used internally.
*/
virtual ValueMap getValueMapFromData(const char* filedata, int filesize);
// Write a ValueMap to a plist file.
// This method is used internally.
/**
* write a ValueMap into a plist file
*
*@param dict the ValueMap want to save
*@param fullPath The full path to the file you want to save a string
*@return bool
*/
virtual bool writeToFile(ValueMap& dict, const std::string& fullPath);
/**
* write a string into a file
*
* @param dataStr the string want to save
* @param fullPath The full path to the file you want to save a string
* @return bool True if write success
*/
virtual bool writeStringToFile(std::string dataStr, const std::string& fullPath);
/**
* write Data into a file
*
*@param retData the data want to save
*@param fullPath The full path to the file you want to save a string
*@return bool
*/
virtual bool writeDataToFile(Data retData, const std::string& fullPath);
/**
* write ValueMap into a plist file
*
*@param dict the ValueMap want to save
*@param fullPath The full path to the file you want to save a string
*@return bool
*/
virtual bool writeValueMapToFile(ValueMap& dict, const std::string& fullPath);
/**
* write ValueVector into a plist file
*
*@param vecData the ValueVector want to save
*@param fullPath The full path to the file you want to save a string
*@return bool
*/
virtual bool writeValueVectorToFile(ValueVector vecData, const std::string& fullPath);
/**
* Windows fopen can't support UTF-8 filename
* Need convert all parameters fopen and other 3rd-party libs

View File

@ -467,6 +467,12 @@ ValueMap FileUtilsApple::getValueMapFromData(const char* filedata, int filesize)
bool FileUtilsApple::writeToFile(ValueMap& dict, const std::string &fullPath)
{
return writeValueMapToFile(dict, fullPath);
}
bool FileUtils::writeValueMapToFile(ValueMap& dict, const std::string& fullPath)
{
//CCLOG("iOS||Mac Dictionary %d write to file %s", dict->_ID, fullPath.c_str());
NSMutableDictionary *nsDict = [NSMutableDictionary dictionary];
@ -482,6 +488,20 @@ bool FileUtilsApple::writeToFile(ValueMap& dict, const std::string &fullPath)
return true;
}
bool FileUtils::writeValueVectorToFile(ValueVector vecData, const std::string& fullPath)
{
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
NSMutableArray* array = [NSMutableArray array];
for (const auto &e : vecData)
{
addObjectToNSArray(e, array);
}
[array writeToFile:path atomically:YES];
return true;
}
ValueVector FileUtilsApple::getValueVectorFromFile(const std::string& filename)
{
// NSString* pPath = [NSString stringWithUTF8String:pFileName];

View File

@ -11,6 +11,10 @@ FileUtilsTests::FileUtilsTests()
ADD_TEST_CASE(TestFileFuncs);
ADD_TEST_CASE(TestDirectoryFuncs);
ADD_TEST_CASE(TextWritePlist);
ADD_TEST_CASE(TestWriteString);
ADD_TEST_CASE(TestWriteData);
ADD_TEST_CASE(TestWriteValueMap);
ADD_TEST_CASE(TestWriteValueVector);
}
// TestResolutionDirectories
@ -390,7 +394,7 @@ std::string TestDirectoryFuncs::subtitle() const
return "";
}
// TestWritePlist
// TextWritePlist
void TextWritePlist::onEnter()
{
@ -479,3 +483,321 @@ std::string TextWritePlist::subtitle() const
std::string writablePath = FileUtils::getInstance()->getWritablePath().c_str();
return ("See plist file at your writablePath");
}
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 "";
}
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";
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 "";
}

View File

@ -91,4 +91,47 @@ public:
virtual std::string subtitle() const override;
};
class TestWriteString : public FileUtilsDemo
{
public:
CREATE_FUNC(TestWriteString);
virtual void onEnter() override;
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class TestWriteData : public FileUtilsDemo
{
public:
CREATE_FUNC(TestWriteData);
virtual void onEnter() override;
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class TestWriteValueMap : public FileUtilsDemo
{
public:
CREATE_FUNC(TestWriteValueMap);
virtual void onEnter() override;
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class TestWriteValueVector : public FileUtilsDemo
{
public:
CREATE_FUNC(TestWriteValueVector);
virtual void onEnter() override;
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
#endif /* __FILEUTILSTEST_H__ */

View File

@ -104,7 +104,7 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat
Scheduler::[pause resume ^unschedule$ unscheduleUpdate unscheduleAllForTarget schedule isTargetPaused isScheduled],
TextureCache::[addPVRTCImage],
*::[copyWith.* onEnter.* onExit.* ^description$ getObjectType onTouch.* onAcc.* onKey.* onRegisterTouchListener operator.+],
FileUtils::[getFileData getDataFromFile setFilenameLookupDictionary destroyInstance getFullPathCache],
FileUtils::[getFileData getDataFromFile writeDataToFile setFilenameLookupDictionary destroyInstance getFullPathCache],
Application::[^application.* ^run$ getCurrentLanguageCode setAnimationInterval],
Camera::[getEyeXYZ getCenterXYZ getUpXYZ],
ccFontDefinition::[*],

View File

@ -99,7 +99,7 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
TextureCache::[addPVRTCImage addImageAsync],
Timer::[getSelector createWithScriptHandler],
*::[copyWith.* onEnter.* onExit.* ^description$ getObjectType (g|s)etDelegate onTouch.* onAcc.* onKey.* onRegisterTouchListener],
FileUtils::[getFileData getDataFromFile getFullPathCache],
FileUtils::[getFileData getDataFromFile writeDataToFile getFullPathCache],
Application::[^application.* ^run$],
Camera::[getEyeXYZ getCenterXYZ getUpXYZ],
ccFontDefinition::[*],