📄 nqpinyinconverter.cpp
字号:
/*
* CNqPinyinConverter class
* Text Pinyin Engine for NetQin
* Date: 2008.06.11
* Author: Tomken
*/
#include "NqPinyinConverter.h"
#include <charconv.h>
static TUint Gbcode2Letter(TUint aCode);
static TUint Des82GbCode(const TDesC8& aDes8);
TInt ConvertToPinyinFirstLetterL(TDes& aPinyin, const TDesC& aText, RFs& aFs)
{
TInt res = 0;
CNqPinyinConverter* pc = CNqPinyinConverter::NewL(aFs);
CleanupStack::PushL(pc);
res = pc->ConvertToPinyinFirstLetter(aPinyin, aText);
CleanupStack::PopAndDestroy(pc);
return res;
}
TInt ConvertToPinyinFullLetterL(TDes& aPinyin, const TDesC& aText, RFs& aFs)
{
TInt res = 0;
CNqPinyinConverter* pc = CNqPinyinConverter::NewL(aFs);
CleanupStack::PushL(pc);
res = pc->ConvertToPinyinFullLetter(aPinyin, aText);
CleanupStack::PopAndDestroy(pc);
return res;
}
CNqPinyinConverter* CNqPinyinConverter::NewL(RFs& aFs)
{
CNqPinyinConverter* self = new (ELeave) CNqPinyinConverter(aFs);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CNqPinyinConverter::CNqPinyinConverter(RFs& aFs) : iFs(aFs)
{
}
CNqPinyinConverter::~CNqPinyinConverter()
{
delete iCharsetConverter;
}
void CNqPinyinConverter::ConstructL()
{
iCharsetConverter = CCnvCharacterSetConverter::NewL();
if ( CCnvCharacterSetConverter::EAvailable != iCharsetConverter->PrepareToConvertToOrFromL( KCharacterSetIdentifierGb2312, iFs))
{
delete iCharsetConverter;
iCharsetConverter = NULL;
}
}
TInt CNqPinyinConverter::ConvertToPinyinFirstLetter(TDes& aPinyin, const TDesC& aText)
{
if(!iCharsetConverter)
return KErrNotSupported;
TBuf8<4> gbCode;
TInt toCode;
for(TInt i=0; i<aText.Length(); i++)
{
TPtrC singleChar = aText.Mid(i, 1);
gbCode.SetLength(0);
iCharsetConverter->ConvertFromUnicode(gbCode, singleChar);
toCode = Des82GbCode(gbCode);
toCode = Gbcode2Letter(toCode);
if(toCode > 'z')
aPinyin.Append(singleChar);
else
aPinyin.Append(toCode);
}
return KErrNone;
}
TInt CNqPinyinConverter::ConvertToPinyinFullLetter(TDes& aPinyin, const TDesC& aText)
{
return ConvertToPinyinFirstLetter(aPinyin, aText);
}
static TUint Gbcode2Letter(TUint aCode)
{
if (aCode < 0xB0A1) return aCode;
if (aCode < 0xB0C5) return 'a';
if (aCode < 0xB2C1) return 'b';
if (aCode < 0xB4EE) return 'c';
if (aCode < 0xB6EA) return 'd';
if (aCode < 0xB7A2) return 'e';
if (aCode < 0xB8C1) return 'f';
if (aCode < 0xB9FE) return 'g';
if (aCode < 0xBBF7) return 'h';
if (aCode < 0xBFA6) return 'j';
if (aCode < 0xC0AC) return 'k';
if (aCode < 0xC2E8) return 'l';
if (aCode < 0xC4C3) return 'm';
if (aCode < 0xC5B6) return 'n';
if (aCode < 0xC5BE) return 'o';
if (aCode < 0xC6DA) return 'p';
if (aCode < 0xC8BB) return 'q';
if (aCode < 0xC8F6) return 'r';
if (aCode < 0xCBFA) return 's';
if (aCode < 0xCDDA) return 't';
if (aCode < 0xCEF4) return 'w';
if (aCode < 0xD1B9) return 'x';
if (aCode < 0xD4D1) return 'y';
if (aCode < 0xD7FA) return 'z';
return aCode;
}
static TUint Des82GbCode(const TDesC8& aDes8)
{
TUint res = 0;
if(aDes8.Length() < 1)
return res;
TUint8 lowVal = aDes8[0];
if(aDes8.Length() < 2)
return lowVal;
TUint8 heightVal = aDes8[1];
res = (TUint16)((lowVal << 8) | heightVal);
return res;
}
// end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -