• [cocos2dx笔记005]一个字符串管理配置类


    在用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;
        }

  • 相关阅读:
    插件制作入门
    Hank老师推荐的一些博客
    高级iOS工程师的博客
    查看一个文件是否支持64位 方法 ,[symbol(s) not found for architecture x86_64]相关
    ssh-keygen的使用方法及配置authorized_keys两台linux机器相互认证
    使用git在两台机器间同步代码
    scp远程拷贝命令
    后台启动程序并重定向输出信息脚本
    automake 工具的使用
    minigui启动过程
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7340307.html
Copyright © 2020-2023  润新知