mirror of https://github.com/axmolengine/axmol.git
Make sure XMLDocument is deleted; Code clean up; Add test;
This commit is contained in:
parent
9a04eee8d1
commit
2ba5035e5a
|
@ -338,15 +338,17 @@ static tinyxml2::XMLElement* generateElementForDict(cocos2d::CCDictionary *dict,
|
|||
*/
|
||||
bool CCFileUtils::writeToFile(cocos2d::CCDictionary *dict, const std::string &fullPath)
|
||||
{
|
||||
CCLOG("tinyxml2 CCDictionary %d writeToFile %s", dict->m_uID, fullPath.c_str());
|
||||
bool bRet = false;
|
||||
//CCLOG("tinyxml2 CCDictionary %d writeToFile %s", dict->m_uID, fullPath.c_str());
|
||||
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
||||
if (NULL == pDoc)
|
||||
return false;
|
||||
|
||||
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
||||
if (NULL == pDeclaration)
|
||||
{
|
||||
delete pDoc;
|
||||
return false;
|
||||
}
|
||||
|
||||
pDoc->LinkEndChild(pDeclaration);
|
||||
tinyxml2::XMLElement *docType = pDoc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
||||
|
@ -355,19 +357,23 @@ bool CCFileUtils::writeToFile(cocos2d::CCDictionary *dict, const std::string &fu
|
|||
tinyxml2::XMLElement *pRootEle = pDoc->NewElement("plist");
|
||||
pRootEle->SetAttribute("version", "1.0");
|
||||
if (NULL == pRootEle)
|
||||
{
|
||||
delete pDoc;
|
||||
return false;
|
||||
}
|
||||
pDoc->LinkEndChild(pRootEle);
|
||||
|
||||
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, pDoc);
|
||||
if (NULL == innerDict )
|
||||
{
|
||||
delete pDoc;
|
||||
return false;
|
||||
}
|
||||
pRootEle->LinkEndChild(innerDict);
|
||||
|
||||
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(fullPath.c_str());
|
||||
|
||||
if(pDoc)
|
||||
delete pDoc;
|
||||
bool bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(fullPath.c_str());
|
||||
|
||||
delete pDoc;
|
||||
return bRet;
|
||||
}
|
||||
|
||||
|
@ -376,34 +382,25 @@ bool CCFileUtils::writeToFile(cocos2d::CCDictionary *dict, const std::string &fu
|
|||
*/
|
||||
static tinyxml2::XMLElement* generateElementForObject(cocos2d::CCObject *object, tinyxml2::XMLDocument *pDoc)
|
||||
{
|
||||
tinyxml2::XMLElement* rootNode = NULL;
|
||||
// object is CCString
|
||||
CCString *str = dynamic_cast<CCString *>(object);
|
||||
if (str)
|
||||
if (CCString *str = dynamic_cast<CCString *>(object))
|
||||
{
|
||||
rootNode = pDoc->NewElement("string");
|
||||
tinyxml2::XMLElement* node = pDoc->NewElement("string");
|
||||
tinyxml2::XMLText* content = pDoc->NewText(str->getCString());
|
||||
rootNode->LinkEndChild(content);
|
||||
return rootNode;
|
||||
node->LinkEndChild(content);
|
||||
return node;
|
||||
}
|
||||
|
||||
// object is CCArray
|
||||
CCArray *array = dynamic_cast<CCArray *>(object);
|
||||
if (array)
|
||||
{
|
||||
rootNode = generateElementForArray(array, pDoc);
|
||||
return rootNode;
|
||||
}
|
||||
if (CCArray *array = dynamic_cast<CCArray *>(object))
|
||||
return generateElementForArray(array, pDoc);
|
||||
|
||||
// object is CCDictionary
|
||||
CCDictionary *innerDict = dynamic_cast<CCDictionary *>(object);
|
||||
if (innerDict)
|
||||
{
|
||||
rootNode = generateElementForDict(innerDict, pDoc);
|
||||
return rootNode;
|
||||
}
|
||||
if (CCDictionary *innerDict = dynamic_cast<CCDictionary *>(object))
|
||||
return generateElementForDict(innerDict, pDoc);
|
||||
|
||||
return rootNode;
|
||||
CCLOG("This type cannot appear in property list");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -352,9 +352,6 @@ protected:
|
|||
*/
|
||||
virtual bool writeToFile(CCDictionary *dict, const std::string& fullPath);
|
||||
|
||||
// for test
|
||||
//virtual bool writeToFileTest(CCDictionary *dict, const std::string& fullPath);
|
||||
|
||||
/**
|
||||
* Creates an array by the contents of a file.
|
||||
* @note This method is used internally.
|
||||
|
|
|
@ -308,7 +308,7 @@ CCDictionary* CCFileUtilsIOS::createCCDictionaryWithContentsOfFile(const std::st
|
|||
|
||||
bool CCFileUtilsIOS::writeToFile(CCDictionary *dict, const std::string &fullPath)
|
||||
{
|
||||
CCLOG("iOS||Mac CCDictionary %d write to file %s", dict->m_uID, fullPath.c_str());
|
||||
//CCLOG("iOS||Mac CCDictionary %d write to file %s", dict->m_uID, fullPath.c_str());
|
||||
NSMutableDictionary *nsDict = [NSMutableDictionary dictionary];
|
||||
|
||||
CCDictElement *element = NULL;
|
||||
|
|
|
@ -5,12 +5,14 @@ TESTLAYER_CREATE_FUNC(TestResolutionDirectories);
|
|||
TESTLAYER_CREATE_FUNC(TestSearchPath);
|
||||
TESTLAYER_CREATE_FUNC(TestFilenameLookup);
|
||||
TESTLAYER_CREATE_FUNC(TestIsFileExist);
|
||||
TESTLAYER_CREATE_FUNC(TextWritePlist);
|
||||
|
||||
static NEWTESTFUNC createFunctions[] = {
|
||||
CF(TestResolutionDirectories),
|
||||
CF(TestSearchPath),
|
||||
CF(TestFilenameLookup),
|
||||
CF(TestIsFileExist)
|
||||
CF(TestIsFileExist),
|
||||
CF(TextWritePlist),
|
||||
};
|
||||
|
||||
static int sceneIdx=-1;
|
||||
|
@ -348,3 +350,63 @@ string TestIsFileExist::subtitle()
|
|||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
#pragma mark - TestWritePlist
|
||||
|
||||
void TextWritePlist::onEnter()
|
||||
{
|
||||
FileUtilsDemo::onEnter();
|
||||
CCDictionary *root = CCDictionary::create();
|
||||
CCString *string = CCString::create("string element value");
|
||||
root->setObject(string, "string element key");
|
||||
|
||||
CCArray *array = CCArray::create();
|
||||
|
||||
CCDictionary *dictInArray = CCDictionary::create();
|
||||
dictInArray->setObject(CCString::create("string in dictInArray value 0"), "string in dictInArray key 0");
|
||||
dictInArray->setObject(CCString::create("string in dictInArray value 1"), "string in dictInArray key 1");
|
||||
array->addObject(dictInArray);
|
||||
|
||||
array->addObject(CCString::create("string in array"));
|
||||
|
||||
CCArray *arrayInArray = CCArray::create();
|
||||
arrayInArray->addObject(CCString::create("string 0 in arrayInArray"));
|
||||
arrayInArray->addObject(CCString::create("string 1 in arrayInArray"));
|
||||
array->addObject(arrayInArray);
|
||||
|
||||
root->setObject(array, "array");
|
||||
|
||||
CCDictionary *dictInDict = CCDictionary::create();
|
||||
dictInDict->setObject(CCString::create("string in dictInDict value"), "string in dictInDict key");
|
||||
|
||||
root->setObject(dictInDict, "dictInDict");
|
||||
|
||||
// end with /
|
||||
std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath();
|
||||
std::string fullPath = writablePath + "text.plist";
|
||||
if(root->writeToFile(fullPath.c_str()))
|
||||
CCLog("see the plist file at %s", fullPath.c_str());
|
||||
else
|
||||
CCLog("write plist file failed");
|
||||
|
||||
CCLabelTTF *label = CCLabelTTF::create(fullPath.c_str(), "Thonburi", 6);
|
||||
this->addChild(label);
|
||||
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
||||
label->setPosition(ccp(winSize.width/2, winSize.height/3));
|
||||
}
|
||||
|
||||
void TextWritePlist::onExit()
|
||||
{
|
||||
FileUtilsDemo::onExit();
|
||||
}
|
||||
|
||||
string TextWritePlist::title()
|
||||
{
|
||||
return "FileUtils: CCDictionary to plist";
|
||||
}
|
||||
|
||||
string TextWritePlist::subtitle()
|
||||
{
|
||||
std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath().c_str();
|
||||
return ("See plist file at your writablePath");
|
||||
}
|
||||
|
|
|
@ -64,4 +64,13 @@ public:
|
|||
virtual string subtitle();
|
||||
};
|
||||
|
||||
class TextWritePlist : public FileUtilsDemo
|
||||
{
|
||||
public:
|
||||
virtual void onEnter();
|
||||
virtual void onExit();
|
||||
virtual string title();
|
||||
virtual string subtitle();
|
||||
};
|
||||
|
||||
#endif /* __FILEUTILSTEST_H__ */
|
||||
|
|
Loading…
Reference in New Issue