在用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
使用样例#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;
}
if (zdh::isNotOK(STRING_MGR->Load(strStringMgrFileName)))
{
STREAM_INFO << "load "<<strStringMgrFileName << "Fail!";
return false;
}
else
{
STREAM_INFO << "Load String:" << STRING_PLAY;
}