• <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://m.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 冬瓜 閱讀(7178) 評論(0)  編輯 收藏 引用 所屬分類: cocos2dx
            亚洲午夜久久久精品影院| 久久强奷乱码老熟女网站| 国产午夜精品久久久久免费视| 久久久久久久久久久久中文字幕 | 久久久久国产成人精品亚洲午夜| 久久精品人人做人人爽电影| 久久精品女人天堂AV麻| 亚洲欧美日韩中文久久| 岛国搬运www久久| 色欲av伊人久久大香线蕉影院| 国产精品99久久久久久宅男| 亚洲人成网亚洲欧洲无码久久 | 久久精品国产亚洲av麻豆图片 | 久久精品免费全国观看国产| 久久无码av三级| 日产精品久久久久久久| 国产精品中文久久久久久久| 国产 亚洲 欧美 另类 久久| 麻豆成人久久精品二区三区免费| 亚洲人成无码网站久久99热国产 | 久久久久久亚洲精品影院| 久久综合综合久久97色| 看久久久久久a级毛片| 久久亚洲AV无码精品色午夜 | 国内精品久久久久影院一蜜桃| 亚洲欧美日韩精品久久亚洲区 | 人妻少妇精品久久| 久久久久久无码国产精品中文字幕| 99国产欧美久久久精品蜜芽| 国产成人无码精品久久久性色 | 久久er国产精品免费观看8| 久久91精品久久91综合| 99久久免费国产特黄| 热re99久久6国产精品免费| 亚洲午夜久久久久久久久电影网 | 狠狠色丁香久久婷婷综合五月 | 久久99精品国产一区二区三区| 狠狠色婷婷综合天天久久丁香| 久久99热国产这有精品| 久久亚洲国产欧洲精品一| 国产巨作麻豆欧美亚洲综合久久|