?? hbufc2.cpp
字號:
// HBufC2.cpp
//
// Copyright (c) 2005 Neusoft Institute of Information Technology.Chengdu
// All rights reserved.
// written by hewei
//
// version 1.0
// Date: 2005-10-12
// This program demostrate how to use HBufC.
// NOTE: the structure of this example is different to standard E32 examples
#include <e32cons.h>
// All messages written to this
LOCAL_D CConsoleBase* console;
// Function prototypes
void callExampleL();
void doExampleL();
//////////////////////////////////////////////////////////////////////////////
//
// -----> CStudent (definition)
//
// The class is used by the example code
//
//////////////////////////////////////////////////////////////////////////////
class CHBufC : public CBase
{
public:
static CHBufC* NewL(const TDesC& aDes,CConsoleBase* aCOnsole);
static CHBufC* NewLC(const TDesC& aDes,CConsoleBase* aConsole);
public :
TInt Length(); //Length;
TInt Compare(const TDesC& aDes); //compare two Descriptors
TInt FindOut(const TDesC& aDes); //find substring
TInt LocateChar(TChar aChar); //lacate char in descriptor
TInt HBufCMatch(const TDesC& aDes); //Match two desriptors
TPtrC16 MidHbufc(TInt aPos); //Mid
TPtrC16 LeftHbufc(TInt aLength); // Left
TPtrC16 RightHbufc(TInt aLength); // Right
TInt HBufCSize(); // Desvriptor size
void otherOperation(); //other operation
HBufC* HBufCAppend(const TDesC& aDes); //Append
~CHBufC();
private:
CHBufC(CConsoleBase* aConsole);
void ConstructL(const TDesC& aDesC);
private :
HBufC* iBuf;
TInt iLength;
CConsoleBase* iConsole;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CHBufC (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CHBufC* CHBufC::NewL(const TDesC& aDes,CConsoleBase* aConsole)
{
CHBufC* Hbufc=new (ELeave)CHBufC(aConsole);
Hbufc->ConstructL(aDes);
return Hbufc;
}
CHBufC* CHBufC::NewLC(const TDesC& aDes,CConsoleBase* aConsole)
{
CHBufC* Hbufc=CHBufC::NewL(aDes,aConsole);
CleanupStack::PushL(Hbufc);
return Hbufc;
}
CHBufC::CHBufC(CConsoleBase* aConsole)
{
iConsole=aConsole;
iLength=0;
}
void CHBufC::ConstructL(const TDesC& aDes)
{
iBuf=HBufC::NewLC(aDes.Length()+20);
TPtr16 ptr16=iBuf->Des();
ptr16.Copy(aDes);
CleanupStack::Pop();
}
TInt CHBufC::Length()
{
iLength=iBuf->Length();
return iLength;
}
TInt CHBufC::Compare(const TDesC& aDes)
{
return iBuf->Compare(aDes);
}
TInt CHBufC::FindOut(const TDesC& aDes)
{
return iBuf->Find(aDes);
}
TInt CHBufC::LocateChar(TChar aChar)
{
return iBuf->Locate(aChar);
}
TInt CHBufC::HBufCMatch(const TDesC& aDes)
{
return iBuf->Match(aDes);
}
TPtrC16 CHBufC::MidHbufc(TInt aPos)
{
return iBuf->Mid(aPos);
}
TPtrC16 CHBufC::LeftHbufc(TInt aLength)
{
return iBuf->Left(aLength);
}
TPtrC16 CHBufC::RightHbufc(TInt aLength)
{
return iBuf->Right(aLength);
}
TInt CHBufC::HBufCSize()
{
return iBuf->Size();
}
void CHBufC::otherOperation()
{
_LIT(KFORMAT1,"address of HBufC is %x\n");
_LIT(KFORMAT2,"address of data in HBufC is %x\n");
iConsole->Printf(KFORMAT1,iBuf);
iConsole->Printf(KFORMAT2,iBuf->Ptr());
}
HBufC* CHBufC::HBufCAppend(const TDesC& aDes)
{
TPtr16 ptr16(iBuf->Des());
ptr16.Append(aDes);
return iBuf;
}
CHBufC::~CHBufC()
{
delete iBuf;
}
//////////////////////////////////////////////////////////////////////////////
//
// Main function called by E32
//
//////////////////////////////////////////////////////////////////////////////
GLDEF_C TInt E32Main()
{
// Get cleanup stack
CTrapCleanup* cleanup=CTrapCleanup::New();
// Some more initialization, then do the example
TRAPD(error,doExampleL());
// callExampleL() should never leave.
_LIT(KMsgPanicEpoc32ex,"EPOC32EX");
__ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
// destroy the cleanup stack
delete cleanup;
// return
return 0;
}
//////////////////////////////////////////////////////////////////////////////
//
//doExample() and CallExample()
//
//////////////////////////////////////////////////////////////////////////////
void doExampleL()
{
_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);
}
else{
_LIT(KNOLEAVE,"No Leave!\n");
console->Printf(KNOLEAVE);
}
console->Printf(KPressAnyKey);
console->Getch();
// Remove the console object from the cleanupstack
// and destroy it.
CleanupStack::PopAndDestroy();
}
void callExampleL()
{
_LIT(KSTRING,"hewei");
_LIT(KSTRING2,"heping");
_LIT(KSubString,"we");
_LIT(KLENGTH,"length of HBufC is %d\n");
_LIT(KSIZE,"size of HBufC is %d\n");
_LIT(KINFO1,"bufc is grater than string2.\n");
_LIT(KINFO2,"bufc is equal to string2.\n");
_LIT(KINFO3,"bufc is grater than string2.\n");
_LIT(KFIND,"find!.pos is %d\n");
_LIT(KMATCH,"position of first match is %d\n");
_LIT(KNEWBUF,"New buf is %s\n");
_LIT(KFORMAT,"%s\n");
CHBufC* bufc=CHBufC::NewLC(KSTRING,console);
//print length and size of descriptor;
console->Printf(KLENGTH,bufc->Length());
console->Printf(KSIZE,bufc->HBufCSize());
//compare two descriptors;
if(bufc->Compare(KSTRING2)>0)
console->Printf(KINFO1);
else if(bufc->Compare(KSTRING2)==0)
console->Printf(KINFO2);
else
console->Printf(KINFO3);
//match two descriptors
console->Printf(KMATCH,bufc->HBufCMatch(KSTRING2));
// find
console->Printf(KFIND,bufc->FindOut(KSubString));
console->Getch();
//left、right、Mid
console->Printf(KFORMAT,bufc->LeftHbufc(1).Ptr());
console->Printf(KFORMAT,bufc->MidHbufc(2).Ptr());
console->Printf(KFORMAT,bufc->RightHbufc(2).Ptr());
//Locate a char
TChar ch('e');
console->Printf(KFIND,bufc->LocateChar(ch));
//other operation
bufc->otherOperation();
//Append
console->Printf(KNEWBUF,bufc->HBufCAppend(KSTRING2)->Ptr());
CleanupStack::PopAndDestroy();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -