• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            專職C++

            不能停止的腳步

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(28)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            本文地址:http://www.shnenglu.com/zdhsoft/archive/2014/09/03/208216.html
            本文基于cocos2dx 3.2
            cocos2dx 提供了一個基于xml的用戶數據存貯類,給基于cocos2dx開發的用戶數據存貯,這個類名就是UserDefault,在cocos2dx 2.x中是CCUserDefault。我的程序用的就是這個,但是最近老出錯,于是分析源代碼,發現了一個讓我震驚的東西。經過分析,發現用UserDefault每讀寫一次數據,都會創建一個tinyxml對象,然后讀取xml內容。如果是寫數據,還是寫入xml一次。下面是對應的代碼:
            讀取key,所以各種讀取key的操作,都是類似這樣。
            double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
            {
                const char* value = nullptr;
                tinyxml2::XMLElement* rootNode;
                tinyxml2::XMLDocument* doc;
                tinyxml2::XMLElement* node;
                node =  getXMLNodeForKey(pKey, &rootNode, &doc);
                // find the node
                if (node && node->FirstChild())
                {
                    value = (const char*)(node->FirstChild()->Value());
                }

                double ret = defaultValue;

                if (value)
                {
                    ret = utils::atof(value);
                }

                if (doc) delete doc;

                return ret;
            }
             關于getXMLNodeForKey的實現
            /**
             * define the functions here because we don't want to
             * export xmlNodePtr and other types in "CCUserDefault.h"
             
            */

            static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
            {
                tinyxml2::XMLElement* curNode = nullptr;

                // check the key value
                if (! pKey)
                {
                    return nullptr;
                }

                do 
                {
                     tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
                    *doc = xmlDoc;

                    std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());

                    if (xmlBuffer.empty())
                    {
                        CCLOG("can not read xml file");
                        break;
                    }
                    xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());

                    // get root node
                    *rootNode = xmlDoc->RootElement();
                    if (nullptr == *rootNode)
                    {
                        CCLOG("read root node error");
                        break;
                    }
                    // find the node
                    curNode = (*rootNode)->FirstChildElement();
                    while (nullptr != curNode)
                    {
                        const char* nodeName = curNode->Value();
                        if (!strcmp(nodeName, pKey))
                        {
                            break;
                        }

                        curNode = curNode->NextSiblingElement();
                    }
                } while (0);

                return curNode;
            }
            關于setValueForKey的實現
            static void setValueForKey(const char* pKey, const char* pValue)
            {
                 tinyxml2::XMLElement* rootNode;
                tinyxml2::XMLDocument* doc;
                tinyxml2::XMLElement* node;
                // check the params
                if (! pKey || ! pValue)
                {
                    return;
                }
                // find the node
                node = getXMLNodeForKey(pKey, &rootNode, &doc);
                // if node exist, change the content
                if (node)
                {
                    if (node->FirstChild())
                    {
                        node->FirstChild()->SetValue(pValue);
                    }
                    else
                    {
                        tinyxml2::XMLText* content = doc->NewText(pValue);
                        node->LinkEndChild(content);
                    }
                }
                else
                {
                    if (rootNode)
                    {
                        tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
                        rootNode->LinkEndChild(tmpNode);
                        tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
                        tmpNode->LinkEndChild(content);
                    }    
                }

                // save file and free doc
                if (doc)
                {
                    doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
                    delete doc;
                }
            }
            它的flush方法也有驚人的發現:
            void UserDefault::flush()
            {
            }
            它是一個空函數,也就是說,你在寫入數據的時候,會以為最后會通過flush才會寫入數據,沒想全錯了!
            如果你用它存貯比較多的字段時,你就會現,你悲劇了。
            幸好發現及時,這里不建議大家使用UserDefault做為你的數據存貯。
            可以可以用自定義的方式文件讀寫
            如可以通過標準的C讀寫 fopen,fwrite等或iostream也都可以,重點是讀寫的文件路徑,會有所不同,下面是得到文件路徑的例子
            std::string strFullFileName = FileUtils::getInstance()->getWritablePath() + DATA_FILE_NAME;

            最后:不要求寫太高質量的代碼,但也不要寫的太低質量了
            posted on 2014-09-03 10:23 冬瓜 閱讀(7170) 評論(0)  編輯 收藏 引用 所屬分類: cocos2dx
            国产精自产拍久久久久久蜜| 久久久久久国产a免费观看黄色大片| 亚洲国产精品久久电影欧美| 国产午夜免费高清久久影院| 久久亚洲精品中文字幕三区| 久久精品不卡| 国产精品久久网| 久久精品久久久久观看99水蜜桃 | 亚洲精品无码久久久久sm| 精品久久久久香蕉网| 综合久久精品色| 97精品伊人久久久大香线蕉| 精品久久久久久国产| 久久青青国产| 久久最新精品国产| 亚洲成色www久久网站夜月| 久久亚洲国产成人影院网站 | 2020久久精品亚洲热综合一本| 久久综合久久综合九色| 精品久久久噜噜噜久久久| 久久久久久久波多野结衣高潮| 精品久久久久久国产牛牛app| 精品久久久久久中文字幕人妻最新| 国产免费久久精品99re丫y| 久久精品国产亚洲7777| 国内精品久久久久影院老司| 久久久久黑人强伦姧人妻| 精品久久一区二区三区| 2021精品国产综合久久| 麻豆成人久久精品二区三区免费 | 亚洲七七久久精品中文国产| 精品国产91久久久久久久a| 国产精品女同一区二区久久| 国产精品激情综合久久| 久久精品www| 久久国产精品视频| 人人狠狠综合久久亚洲| 亚洲精品国产自在久久| 偷窥少妇久久久久久久久| 久久精品国产男包| 国产V综合V亚洲欧美久久|