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

    }

最新文章

  1. util.js
  2. [AX2012]Claims user
  3. MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程
  4. Ubuntu 14.10 下sed命令详解
  5. Convolution and Deconvolution
  6. MEMS Gyroscope Technology
  7. 解决Spring中singleton的Bean依赖于prototype的Bean的问题
  8. 关于frameset的一些小总结
  9. Python并发编程__多进程
  10. layui框架中关于table方法级渲染和自动化渲染之间的区别简单介绍
  11. OSFPv3的配置
  12. TreeTagger
  13. 在Windows命令行中编译运行C/C++程序
  14. centos7.2 使用rpm安装jdk8
  15. 【python】python与正则 re的主要用到的方法列举
  16. [转帖]HDD磁盘,非4K无以致远
  17. AngularJs HTTP响应拦截器实现登陆、权限校验
  18. LeetCode: Max Points on a Line 解题报告
  19. 沉淀再出发:PHP的中级内容
  20. 垃圾回收算法与 JVM 垃圾回收器综述(转)

热门文章

  1. Android入门之简单拨号器
  2. Jetty:配置上下文
  3. cmd命令怎么玩
  4. atitit.提升开发效率---MDA 软件开发方式的革命(3)----自己主动化建表
  5. 【Linux】gvim封装至gvi命令
  6. 关于NHibernate中存在于Session中实例的3种状态的简单分析
  7. 如何使用Dockerfile构建Tomcat镜像并部署war
  8. MySQL-SQL语句中SELECT语句的执行顺序
  9. 利用Windows 2003系统中实现两个网段的路由
  10. MySQL 获取子分类ID的所有父分类ID和Name的集合