📄 description.cpp
字号:
// Description.cpp
// All rights reserved.
//Author: LiuLiping
// version 1.0
// Date: 2006-2-12
// This program demostrate how to use TBuf,TBufC,TPtrandTPtrC.
#include <e32cons.h>
LOCAL_D CConsoleBase* console; // All messages written to this
void callExampleL(); // Function prototypes
void doExampleL();
void useTBuf();
void useTBufC();
void useTPtr();
void useTPtrC();
void WaitForKey()
{
_LIT(KMessage,"Press any key to continue\n\n");
console->Printf(KMessage);
console->Getch();
}
void useTBuf()
{
console->Printf(_L("\n*****useTBuf*****\n"));
_LIT(KMessage1,"There are cats");
_LIT(KMessage2," and dogs.");
_LIT(KMessage3,"white ");
_LIT(KMessage4,"black ");
_LIT(KMessage5,"HBufC alloc length is %d");
_LIT(KMessage6,"HBufC ReAlloc length is %d");
_LIT(KMessage7,"The fisrt address of buf is %x");
_LIT(KEnter,"\n");
TBuf<40> buf; //定义一个TBuf类对象buf
buf=KMessage1; //初始化buf
console->Printf(buf); //打印buf
console->Printf(KEnter);
buf.Append(KMessage2); //在buf末尾添加KMessage2
console->Printf(buf);
console->Printf(KEnter);
buf.Insert(10,KMessage3); //从buf第10位开始查如今KMessage3
buf.Insert(25,KMessage4); //从buf第25位开始查如今KMessage4
console->Printf(buf);
console->Printf(KEnter);
HBufC* hbufc = HBufC::New(40); //定义一个HBufC类的指针hbufc
hbufc = buf.Alloc(); //初始化hbufc
TPtr ptr1 = hbufc->Des(); //把不可改变的hbufc变为可改变的描述符赋给ptr通过ptr来对hbufc进行改变的操作
console->Printf(ptr1);
console->Printf(KEnter);
console->Printf(KMessage5,ptr1.MaxLength());//打印HBufC的初始最大长度
console->Printf(KEnter);
WaitForKey();
hbufc->ReAlloc(50);
TPtr ptr2 = hbufc->Des();
console->Printf(KMessage6,ptr2.MaxLength());//打印HBufC重新分配后的最大长度
console->Printf(KEnter);
WaitForKey();
console->Printf(KMessage7,buf.Ptr()); //打印buf的起始地址
console->Printf(KEnter);
WaitForKey();
}
void useTBufC()
{
console->Printf(_L("*****useTBufC*****\n"));
_LIT(KMessage1,"There are flowers");
_LIT(KMessage2," and trees.");
_LIT(KMessage3,"buautiful ");
_LIT(KMessage4,"HBufC alloc length is %d");
_LIT(KMessage5,"HBufC ReAlloc length is %d");
_LIT(KMessage6,"The fisrt address of bufc is %x");
_LIT(KEnter,"\n");
TBufC<40> bufc; //定义一个TBufC类的对象bufc
bufc=KMessage1; //初始化bufc
console->Printf(bufc); //打印bufc
console->Printf(KEnter);
TPtr ptr = bufc.Des(); //把不可改变的bufc变为可改变的描述符赋给ptr通过ptr来对bufc进行改变的操作
ptr.Append(KMessage2);
console->Printf(bufc);
console->Printf(KEnter);
ptr.Insert(10,KMessage3);
console->Printf(bufc);
console->Printf(KEnter);
HBufC* hbufc = HBufC::New(40);
hbufc = bufc.Alloc();
TPtr ptr1 = hbufc->Des();
console->Printf(ptr1);
console->Printf(KEnter);
console->Printf(KMessage4,ptr1.MaxLength());
console->Printf(KEnter);
WaitForKey();
hbufc->ReAlloc(50);
TPtr ptr2 = hbufc->Des();
console->Printf(KMessage5,ptr2.MaxLength());
console->Printf(KEnter);
WaitForKey();
console->Printf(KMessage6,bufc.Ptr());
console->Printf(KEnter);
WaitForKey();
}
void useTPtr()
{
console->Printf(_L("*****useTPtr*****\n"));
_LIT(KMessage1,"There are pandas");
_LIT(KMessage2," and deers.");
_LIT(KMessage3,"small lovely ");
_LIT(KMessage4,"HBufC alloc length is %d");
_LIT(KMessage5,"HBufC ReAlloc length is %d");
_LIT(KMessage6,"The fisrt address of ptr is %x");
_LIT(KEnter,"\n");
TBufC<40> bufc(KMessage1); //定义一个TBufC类的对象bufc并用KMessage1来初始化bufc
TPtr16 ptr(bufc.Des()); //定义一个TPtr16类的对象并用bufc来初始化它
console->Printf(ptr); //打印ptr
console->Printf(KEnter);
WaitForKey();
ptr.Append(KMessage2);
console->Printf(ptr);
console->Printf(KEnter);
ptr.Insert(10,KMessage3);
console->Printf(ptr);
console->Printf(KEnter);
HBufC* hbufc = HBufC::New(40);
hbufc = ptr.Alloc();
TPtr ptr1 = hbufc->Des();
console->Printf(ptr1);
console->Printf(KEnter);
console->Printf(KMessage4,ptr1.MaxLength());
console->Printf(KEnter);
WaitForKey();
hbufc->ReAlloc(50);
TPtr ptr2 = hbufc->Des();
console->Printf(KMessage5,ptr2.MaxLength());
console->Printf(KEnter);
WaitForKey();
console->Printf(KMessage6,ptr.Ptr());
console->Printf(KEnter);
WaitForKey();
}
void useTPtrC() //因为TPtrC类没有函数Des()所以该类是不可改变的
{
console->Printf(_L("*****useTPtrC*****\n"));
_LIT(KMessage1,"There are books");
_LIT(KMessage2,"HBufC alloc length is %d");
_LIT(KMessage3,"HBufC ReAlloc length is %d");
_LIT(KMessage4,"The fisrt address of ptrc is %x");
_LIT(KEnter,"\n");
TPtrC ptrc(KMessage1); //定义一个TPtrC类的对象ptrc并用KMessage1初始化它
console->Printf(ptrc); //打印ptrc
console->Printf(KEnter);
HBufC* hbufc = HBufC::New(40);
hbufc = ptrc.Alloc();
TPtr ptr1 = hbufc->Des();
console->Printf(ptr1);
console->Printf(KEnter);
console->Printf(KMessage2,ptr1.MaxLength());
console->Printf(KEnter);
hbufc->ReAlloc(50);
TPtr ptr2 = hbufc->Des();
console->Printf(KMessage3,ptr2.MaxLength());
console->Printf(KEnter);
console->Printf(KMessage4,ptrc.Ptr());
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()
{
useTBuf();
useTBufC();
useTPtr();
useTPtrC();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -