From b2dc16c06592a7fe95d3055b731709d344f5a671 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 4 Jan 2014 14:59:24 +0800 Subject: [PATCH] closed #3580: FileUtilsTest->TextWritePlist crashes --- cocos/base/CCDictionary.cpp | 102 +++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/cocos/base/CCDictionary.cpp b/cocos/base/CCDictionary.cpp index b958ce0be7..ea1d76ba28 100644 --- a/cocos/base/CCDictionary.cpp +++ b/cocos/base/CCDictionary.cpp @@ -26,6 +26,12 @@ #include "CCString.h" #include "CCInteger.h" #include "platform/CCFileUtils.h" +#include "CCString.h" +#include "CCBool.h" +#include "CCInteger.h" +#include "CCFloat.h" +#include "CCDouble.h" +#include "CCArray.h" using namespace std; @@ -461,15 +467,97 @@ __Dictionary* __Dictionary::createWithContentsOfFile(const char *pFileName) return ret; } +static ValueMap ccdictionary_to_valuemap(__Dictionary* dict); + +static ValueVector ccarray_to_valuevector(__Array* arr) +{ + ValueVector ret; + + Object* obj; + CCARRAY_FOREACH(arr, obj) + { + Value arrElement; + + __String* strVal = nullptr; + __Dictionary* dictVal = nullptr; + __Array* arrVal = nullptr; + __Double* doubleVal = nullptr; + __Bool* boolVal = nullptr; + __Float* floatVal = nullptr; + __Integer* intVal = nullptr; + + if ((strVal = dynamic_cast<__String *>(obj))) { + arrElement = Value(strVal->getCString()); + } else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) { + arrElement = ccdictionary_to_valuemap(dictVal); + } else if ((arrVal = dynamic_cast<__Array*>(obj))) { + arrElement = ccarray_to_valuevector(arrVal); + } else if ((doubleVal = dynamic_cast<__Double*>(obj))) { + arrElement = Value(doubleVal->getValue()); + } else if ((floatVal = dynamic_cast<__Float*>(obj))) { + arrElement = Value(floatVal->getValue()); + } else if ((intVal = dynamic_cast<__Integer*>(obj))) { + arrElement = Value(intVal->getValue()); + } else if ((boolVal = dynamic_cast<__Bool*>(obj))) { + arrElement = boolVal->getValue() ? Value(true) : Value(false); + } else { + CCASSERT(false, "the type isn't suppored."); + } + + ret.push_back(arrElement); + } + return ret; +} + +static ValueMap ccdictionary_to_valuemap(__Dictionary* dict) +{ + ValueMap ret; + DictElement* pElement = nullptr; + CCDICT_FOREACH(dict, pElement) + { + Object* obj = pElement->getObject(); + + __String* strVal = nullptr; + __Dictionary* dictVal = nullptr; + __Array* arrVal = nullptr; + __Double* doubleVal = nullptr; + __Bool* boolVal = nullptr; + __Float* floatVal = nullptr; + __Integer* intVal = nullptr; + + Value dictElement; + + if ((strVal = dynamic_cast<__String *>(obj))) { + dictElement = Value(strVal->getCString()); + } else if ((dictVal = dynamic_cast<__Dictionary*>(obj))) { + dictElement = ccdictionary_to_valuemap(dictVal); + } else if ((arrVal = dynamic_cast<__Array*>(obj))) { + dictElement = ccarray_to_valuevector(arrVal); + } else if ((doubleVal = dynamic_cast<__Double*>(obj))) { + dictElement = Value(doubleVal->getValue()); + } else if ((floatVal = dynamic_cast<__Float*>(obj))) { + dictElement = Value(floatVal->getValue()); + } else if ((intVal = dynamic_cast<__Integer*>(obj))) { + dictElement = Value(intVal->getValue()); + } else if ((boolVal = dynamic_cast<__Bool*>(obj))) { + dictElement = boolVal->getValue() ? Value(true) : Value(false); + } else { + CCASSERT(false, "the type isn't suppored."); + } + + const char* key = pElement->getStrKey(); + if (key && strlen(key) > 0) + { + ret[key] = dictElement; + } + } + return ret; +} + + bool __Dictionary::writeToFile(const char *fullPath) { - ValueMap dict; - DictElement* element = nullptr; - CCDICT_FOREACH(this, element) - { - dict[element->getStrKey()] = Value(static_cast<__String*>(element->getObject())->getCString()); - } - + ValueMap dict = ccdictionary_to_valuemap(this); return FileUtils::getInstance()->writeToFile(dict, fullPath); }