?? hbufc.cpp
字號(hào):
// HBufC.cpp
// All rights reserved.
//Author: LiuLiping
// version 1.0
// Date: 2006-2-11
// This program demostrate how to use HBufC.
#include <e32cons.h>
LOCAL_D CConsoleBase* console; // All messages written to this
void callExampleL(); // Function prototypes
void doExampleL();
void useHBufC();
void WaitForKey()
{
_LIT(KMessage,"Press any key to continue\n\n");
console->Printf(KMessage);
console->Getch();
}
///////////////////////////////////////////////////////////////////
//
//
// class for a people's address book
//
//
//////////////////////////////////////////////////////////////////
//
class CAddressBook:public CBase
{
public:
static CAddressBook* NewL(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress);
static CAddressBook* NewLC(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress);
public:
HBufC* GetName(); //得到姓名
HBufC* GetTel(); //得到電話號(hào)碼
HBufC* GetEmail(); //得到郵箱
HBufC* GetAddress(); //得到地址
TInt Length(); //得到描述符的元素的個(gè)數(shù)
TInt MaxLength(); //得到描述符的最大長(zhǎng)度
TInt Size(); //得到描述符的元素的字節(jié)數(shù)
TInt Find(const TDesC& aDes); //在描述符中查找子串a(chǎn)Des第一次出現(xiàn)的位置
TInt Compare(const TDesC& aDes); //比較兩個(gè)描述符的大小
TPtrC16 Left(TInt aLength); //得到描述符最左邊aLength個(gè)元素
TPtrC16 Mid(TInt aPos); //得到描述符aPos位置的元素以及它后面的所有元素
TPtrC16 Right(TInt aLength); //得到描述符最右邊的aLength個(gè)元素
private:
void ConstructL(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress);
~CAddressBook();
private:
HBufC* iName;
HBufC* iTel;
HBufC* iEmail;
HBufC* iAddress;
};
CAddressBook* CAddressBook::NewL(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress)
{
CAddressBook* self=NewLC(aName,aTel,aEmail,aAddress);
CleanupStack::Pop(self);
return self;
}
CAddressBook* CAddressBook::NewLC(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress)
{
CAddressBook* self = new (ELeave)CAddressBook();
CleanupStack::PushL(self);
self->ConstructL(aName,aTel,aEmail,aAddress);
return self;
}
void CAddressBook::ConstructL(const TDesC& aName, const TDesC& aTel, const TDesC& aEmail, const TDesC& aAddress)
{
iName = aName.Alloc(); //給iName分配空間,并初始化
iTel = aTel.Alloc();
iEmail = aEmail.Alloc();
iAddress = aAddress.Alloc();
}
CAddressBook::~CAddressBook()
{
delete iName;
delete iTel;
delete iEmail;
delete iAddress;
}
HBufC* CAddressBook::GetName()
{
return iName;
}
HBufC* CAddressBook::GetTel()
{
return iTel;
}
HBufC* CAddressBook::GetEmail()
{
return iEmail;
}
HBufC* CAddressBook::GetAddress()
{
return iAddress;
}
TInt CAddressBook::Length()
{
return TInt(iAddress->Length()); //得到地址的長(zhǎng)度
}
TInt CAddressBook::Size()
{
return iAddress->Size(); //得到地址的字節(jié)數(shù)
}
TInt CAddressBook::MaxLength()
{
TPtr ptr = iName->Des();
return ptr.MaxLength(); //得到名字的最大長(zhǎng)度
}
TInt CAddressBook::Find(const TDesC& aDes)
{
return iAddress->Find(aDes); //在地址中查找aDes,若找到則返回aDes在地址描述符中第一次出現(xiàn)的位置
}
TInt CAddressBook::Compare(const TDesC& aDes)
{
return iName->Compare(aDes); //比較描述符iName與aDes的大小
}
TPtrC16 CAddressBook::Left(TInt aLength)
{
return iTel->Left(aLength); //得到電話號(hào)碼最左邊的aLength個(gè)元元素
}
TPtrC16 CAddressBook::Mid(TInt aPos)
{
return iEmail->Mid(aPos); //得到郵箱從第aPos位以及它后面的所有元素
}
TPtrC16 CAddressBook::Right(TInt aLength)
{
return iTel->Right(aLength); //得到電話號(hào)碼最右邊的aLength個(gè)元元素
}
void useHBufC()
{
_LIT(KName,"小小");
_LIT(kTel,"028-87654321");
_LIT(KAddress,"成都東軟");
_LIT(KEmail,"xxx@163.com");
_LIT(KNameStr,"Name: ");
_LIT(KTelStr,"Tel: ");
_LIT(KAddressStr,"Address: ");
_LIT(KEmailStr,"Email: ");
_LIT(KEnter,"\n");
_LIT(KLength,"The Address's length is %d\n");
_LIT(KSize,"The Adress's size is %d\n");
_LIT(KMaxLength,"The name's maxlength is %d\n");
_LIT(KLeft,"The area code is %S\n");
_LIT(KRight,"The Tel number is %S\n");
_LIT(KMid,"The email's backward is %S\n");
_LIT(KFindString,"東軟");
_LIT(KFind,"Find, the Pos is %d\n");
CAddressBook* people = CAddressBook::NewL(KName,kTel,KEmail,KAddress);
console->Printf(KNameStr);
console->Printf(*(people->GetName())); //打印姓名
console->Printf(KEnter);
console->Printf(KTelStr);
console->Printf(*(people->GetTel())); //打印電話號(hào)碼
console->Printf(KEnter);
console->Printf(KEmailStr);
console->Printf(*(people->GetEmail())); //打印郵箱
console->Printf(KEnter);
console->Printf(KAddressStr);
console->Printf(*(people->GetAddress()));//打印地址
console->Printf(KEnter);
console->Printf(KLength,people->Length());//打印地址的長(zhǎng)度
console->Printf(KSize,people->Size()); //打印地址的字節(jié)數(shù)
console->Printf(KMaxLength,people->MaxLength());//打印姓名的最大長(zhǎng)度
WaitForKey();
console->Printf(KLeft,&(people->Left(3)));//打印電話號(hào)碼最左邊的3個(gè)元素(即:區(qū)號(hào))
WaitForKey();
console->Printf(KRight,&(people->Right(8)));//打印電話號(hào)碼最右邊的8個(gè)元素(即:電話號(hào)碼)
WaitForKey();
console->Printf(KMid,&(people->Mid(3)));//打印郵箱的后半部分(@163.com)
WaitForKey();
console->Printf(KFind,people->Find(KFindString));//在地址中查找子串“東軟”返回第一次找到的位置
WaitForKey();
if(people->Compare(KAddress)==0) //比較姓名與地址的長(zhǎng)度
console->Printf(_L("The Name's length equl to Address's length"));
if(people->Compare(KAddress)<0)
console->Printf(_L("The Name's length is longer than the Address's length"));
else
console->Printf(_L("The Name's length is shorter than the Address's length"));
console->Printf(KEnter);
}
GLDEF_C TInt E32Main()
{
CTrapCleanup* cleanup=CTrapCleanup::New(); // Get cleanup stack
TRAPD(error,doExampleL()); // callExampleL() should never leave.
_LIT(KMsgPanicEpoc32ex,"EPOC32EX");
__ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
delete cleanup; // destroy the cleanup stack
return 0; // return
}
void doExampleL() //doExample() and CallExample()
{
_LIT(KPressAnyKey,"[Press any key...OK]");
//Get Console;
_LIT(KMsgExampleCode,"Symbian OS Example Code");
console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
//Put console onto the cleanup stack.
CleanupStack::PushL(console);
int error;
TRAP(error,callExampleL()); //TRAPD(error,callExampleL());
if(error)
{
_LIT(KERROR,"error occured!\n");
console->Printf(KERROR);
}
console->Printf(KPressAnyKey);
console->Getch();
CleanupStack::PopAndDestroy();
}
void callExampleL()
{
useHBufC();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -