专职C++

不能停止的脚步

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

常用链接

留言簿(28)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

在用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;
    }
posted on 2014-06-30 14:59 冬瓜 阅读(1255) 评论(0)  编辑 收藏 引用 所属分类: 原创cocos2dx

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理