• <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 冬瓜 閱讀(7170) 評論(0)  編輯 收藏 引用 所屬分類: cocos2dx
            国产成人久久激情91| 中文字幕日本人妻久久久免费| 少妇内射兰兰久久| 久久久久久亚洲精品成人| 伊人色综合久久| 性做久久久久久免费观看| 久久国产欧美日韩精品免费| 亚洲精品无码久久久久AV麻豆| 久久夜色精品国产www| 中文成人无码精品久久久不卡 | 久久久中文字幕日本| 久久精品极品盛宴观看| 久久精品国产亚洲Aⅴ香蕉| 精品久久无码中文字幕| 久久er热视频在这里精品| 日韩精品久久久久久久电影| 久久国产欧美日韩精品| 欧美久久综合性欧美| 亚洲人AV永久一区二区三区久久 | 亚洲精品午夜国产va久久| 高清免费久久午夜精品| 18禁黄久久久AAA片| 久久精品无码一区二区日韩AV| 蜜臀久久99精品久久久久久小说| 久久伊人亚洲AV无码网站| 成人午夜精品久久久久久久小说| 国产精品岛国久久久久| 久久午夜无码鲁丝片| 中文字幕乱码人妻无码久久| 三级三级久久三级久久| 色狠狠久久综合网| 99精品久久久久中文字幕| 久久A级毛片免费观看| 99久久www免费人成精品| 日本高清无卡码一区二区久久 | 国产99久久久国产精品~~牛| 久久青草国产精品一区| 亚洲欧美精品一区久久中文字幕| 亚洲国产精品成人AV无码久久综合影院 | 国产精品久久久久久久久免费| 777米奇久久最新地址|