[Cocos2dx note 005] A string management configuration class

Source: Internet
Author: User

[Cocos2dx note 005] A string management configuration class
In the development of cocos2dx with vs process, to display the Chinese, the requirement is the UTF-8 format can be normally displayed, but VS is generally saved in ANSI format, in this way, the Chinese character string written in the Code. After running, garbled characters are displayed.
To correctly display Chinese characters or support multiple languages, I define a simple string management class to meet the above requirements.
This class uses the XAnsiString, XMap, and texini classes in my open source code and can be found and downloaded in my open code.
The following code: // string resource manager # ifndef _ X_STRING_MANAGER_H _
# Define _ X_STRING_MANAGER_H _
# Include
# Include
# Include
# Include
# Include
Namespace zdh
{
Typedef XMap TStringKeyValue;
Typedef XMap 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 * ction = stIni. getSection (I );

TStringKeyValue & stKeyValue = m_Map [ction-> getSectionName ()];
For (int j = 0; j <ction-> getLength (); j ++)
{
XIniText: TEntry * pEntry = ction-> getEntry (j );
If (isNULL (pEntry) continue;
If (pEntry-> getEntryType ()! = EIET_COMMON_ENTRY) continue;
XIniText: TEntryCommon * pCommonEntry = dynamic_cast (PEntry );
If (isNULL (pCommonEntry) continue;
StKeyValue [pCommonEntry-> getKey (). getField ()] = pCommonEntry-> getValue (). getField ();
}
}
Return ERR_ OK;
}
// Retrieves the specified String object. If it does not exist, NULL is returned.
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 );
}
// Obtain the specified string. If the string does not exist, an empty string is returned.
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 : 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 example
XAnsiString strStringMgrFileName ("string_zh.ini ");
If (zdh: isNotOK (STRING_MGR-> Load (strStringMgrFileName )))
{
STREAM_INFO <"load" < Return false;
}
Else
{
STREAM_INFO <"Load String:" <STRING_PLAY;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.