⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hbufc.cpp

📁 《基于symbian os的手机开发与应用》
💻 CPP
字号:
	// 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();					//得到电话号码
	  HBufC* GetEmail();				//得到邮箱
	  HBufC* GetAddress();				//得到地址
	  TInt Length();					//得到描述符的元素的个数
	  TInt MaxLength();					//得到描述符的最大长度
	  TInt Size();						//得到描述符的元素的字节数
	  TInt Find(const TDesC& aDes);		//在描述符中查找子串aDes第一次出现的位置
	  TInt Compare(const TDesC& aDes);	//比较两个描述符的大小
	  TPtrC16 Left(TInt aLength);		//得到描述符最左边aLength个元素
	  TPtrC16 Mid(TInt aPos);			//得到描述符aPos位置的元素以及它后面的所有元素
	  TPtrC16 Right(TInt aLength);		//得到描述符最右边的aLength个元素
  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());	//得到地址的长度
}
TInt CAddressBook::Size()
{
	return iAddress->Size();			//得到地址的字节数
}
TInt CAddressBook::MaxLength()
{
	TPtr ptr = iName->Des();
	return ptr.MaxLength();				//得到名字的最大长度
}
TInt CAddressBook::Find(const TDesC& aDes)
{
    return iAddress->Find(aDes);		//在地址中查找aDes,若找到则返回aDes在地址描述符中第一次出现的位置
}
TInt CAddressBook::Compare(const TDesC& aDes)
{
	return iName->Compare(aDes);		//比较描述符iName与aDes的大小
}
TPtrC16 CAddressBook::Left(TInt aLength)
{
	return iTel->Left(aLength);			//得到电话号码最左边的aLength个元元素
}
TPtrC16 CAddressBook::Mid(TInt aPos)
{
	return iEmail->Mid(aPos);			//得到邮箱从第aPos位以及它后面的所有元素
}
TPtrC16 CAddressBook::Right(TInt aLength)
{
	return iTel->Right(aLength);		//得到电话号码最右边的aLength个元元素
}

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()));	//打印电话号码
	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());//打印地址的长度
	console->Printf(KSize,people->Size());	  //打印地址的字节数
	console->Printf(KMaxLength,people->MaxLength());//打印姓名的最大长度
	WaitForKey();
	console->Printf(KLeft,&(people->Left(3)));//打印电话号码最左边的3个元素(即:区号)
	WaitForKey();
	console->Printf(KRight,&(people->Right(8)));//打印电话号码最右边的8个元素(即:电话号码)
	WaitForKey();
	console->Printf(KMid,&(people->Mid(3)));//打印邮箱的后半部分(@163.com)
	WaitForKey();
	console->Printf(KFind,people->Find(KFindString));//在地址中查找子串“东软”返回第一次找到的位置
	WaitForKey();
	if(people->Compare(KAddress)==0)	//比较姓名与地址的长度
	  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();
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -