[cocos2dx筆記005]一個字串管理配置類

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   width   

在用vs開發cocos2dx過程中,要顯示的中文,要求是UTF-8格式的才能正常顯示出來,但VS一般是ANSI格式儲存,這樣,在代碼中寫入的中文字串,運行後,顯示的就是亂碼。
為了正確顯示中文,或支援多語言,我這裡定義一個簡單的字串管理類,來滿足上述要求。
這個類使用了我的開原始碼中的XAnsiString和XMap,TextIni這幾個類,可以在我的開放代碼找到下載。
下面是代碼://字串資源管理員#ifndef _X_STRING_MANAGER_H_
#define _X_STRING_MANAGER_H_
#include <xstring.h>
#include <xini.h>
#include <xmap.h>
#include <xset.h>
#include <xsingleton.h>
namespace zdh
{
    typedef XMap<XAnsiString, XAnsiString> TStringKeyValue;
    typedef XMap<XAnsiString, TStringKeyValue> TStringSection;

    class XStringMgr
    {
    public:
        XStringMgr()
        {}
        ~XStringMgr()
        {
            m_Map.Clear();
        }
        XInt Load(const XAnsiString & paramFileName, bool paramClear = true)
        {
            if (paramClear) m_Map.Clear();
            XIniText stIni;
            if (!stIni.Load(paramFileName)) return ERR_FAIL;
            for (int i = 0; i < stIni.getSectionCount(); i++)
            {
                XIniText::TSection * pSection = stIni.getSection(i);
                
                TStringKeyValue & stKeyValue = m_Map[pSection->getSectionName()];
                for (int j = 0; j < pSection->getLength(); j++)
                {
                    XIniText::TEntry * pEntry = pSection->getEntry(j);
                    if (isNULL(pEntry)) continue;
                    if (pEntry->getEntryType() != EIET_COMMON_ENTRY) continue;
                    XIniText::TEntryCommon * pCommonEntry = dynamic_cast<XIniText::TEntryCommon *>(pEntry);
                    if (isNULL(pCommonEntry)) continue;
                    stKeyValue[pCommonEntry->getKey().getField()] = pCommonEntry->getValue().getField();
                }
            }
            return ERR_OK;
        }
        //取指定字串對象,如果不存在,返回NULL
        const XAnsiString * getStringEx(const XAnsiString & paramSection, const XAnsiString & paramKey)
        {
            int iSectionIndex = m_Map.getIndexBykey(paramSection);
            if (!m_Map.isValidIndex(iSectionIndex)) return NULL;
            const TStringKeyValue & stKeyValue = m_Map.getValue(iSectionIndex);
            int iValueIndex = stKeyValue.getIndexBykey(paramKey);
            if (!stKeyValue.isValidIndex(iValueIndex)) return NULL;
            return &stKeyValue.getValue(iValueIndex);
        }
        //取指定的字串,如果不存在,則返回空串
        const char * getString(const XAnsiString & paramSection, const XAnsiString & paramKey)
        {
            const XAnsiString * pRet = getStringEx(paramSection, paramKey);
            if (isNULL(pRet)) return "";
            else return pRet->c_str();
        }

        const TStringSection & getMap() const
        {
            return m_Map;
        }

    private:
        TStringSection m_Map;
    };
}
    #define STRING_MGR zdh::XSingletonSample<zdh::XStringMgr, 0>::getInstance()
    #define STRING_SECTION_MAIN "main"
    #define STRING_PLAY (STRING_MGR->getString(STRING_SECTION_MAIN, "play"))
    #define STRING_FONT (STRING_MGR->getString(STRING_SECTION_MAIN, "font"))
#endif使用例子
    XAnsiString strStringMgrFileName("string_zh.ini");
    if (zdh::isNotOK(STRING_MGR->Load(strStringMgrFileName)))
    {
        STREAM_INFO << "load "<<strStringMgrFileName << "Fail!";
        return false;
    }
    else
    {
        STREAM_INFO << "Load String:" << STRING_PLAY;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.