www.pudn.com > symbianex.rar > TBufAndTBufC.cpp


// TBufAndTBufC.cpp 
// 
// Copyright (c) 2005 CCNIIT.  All rights reserved. 
 
// author: hewei 
// copyright 1.0 
// Date: 2005-10-14 
 
// Example shows attempt to use TBuf and TBufC 
// NOTE: the structure of this example is different to standard E32 examples 
 
 
#include  
 
  // All messages written to this 
LOCAL_D CConsoleBase* console; 
const TInt KMAXLENGTH=20; 
// Function prototypes 
 void callExampleL(); 
 void doExampleL(); 
 void useTBuf(); 
 void useTBufC(); 
////////////////////////////////////////////////////////////////////////////// 
// 
//use TBuf in useTBuf function 
// 
////////////////////////////////////////////////////////////////////////////// 
void useTBuf() 
{ 
	_LIT(KNEWLINE,"\n"); 
	_LIT(KFIRST,"hello"); 
    _LIT(KSECOND," world!"); 
	_LIT(KHEWEI,"hewei!"); 
 
	TBuf buf(KFIRST);//object of TBuf; 
     
    // print TBuf 
    console->Printf(buf); 
	console->Printf(KNEWLINE); 
    //Append another string to buf 
	buf.Append(KSECOND); 
    console->Printf(buf); 
    console->Printf(KNEWLINE); 
	//Insert a string to buf 
	buf.Insert(0,KHEWEI); 
	console->Printf(buf); 
    console->Printf(KNEWLINE); 
	//print length and size of buf 
	_LIT(KINFO,"length=%d,size=%d\n"); 
	console->Printf(KINFO,buf.Length(),buf.Size()); 
	//left right mid 
    _LIT(KLEFT,"left="); 
	console->Printf(KLEFT); 
	console->Printf(buf.Left(3)); 
    console->Printf(KNEWLINE); 
	_LIT(KRIGHT,"right="); 
	console->Printf(KRIGHT); 
    console->Printf(buf.Right(3)); 
    console->Printf(KNEWLINE); 
	_LIT(KMID,"mid="); 
	console->Printf(KMID); 
	console->Printf(buf.Mid(4)); 
	console->Printf(KNEWLINE); 
	//delete 
	buf.Delete(0,5); 
	console->Printf(buf); 
    console->Printf(KNEWLINE); 
} 
void useTBufC() 
{ 
    _LIT(KNEWLINE,"\n"); 
	_LIT(KFIRST,"hello"); 
    _LIT(KSECOND," world!"); 
	_LIT(KHEWEI,"hewei!"); 
 
	TBufC bufc(KFIRST);//object of TBufC; 
     
    // print TBufC 
    console->Printf(bufc); 
	console->Printf(KNEWLINE); 
	//get the modifiable description; 
	TPtr ptr=bufc.Des(); 
    //Append another string to buf 
	ptr.Append(KSECOND); 
    console->Printf(bufc); 
    console->Printf(KNEWLINE); 
	//Insert a string to buf 
	ptr.Insert(0,KHEWEI); 
	console->Printf(bufc); 
    console->Printf(KNEWLINE); 
	//print length and size of buf 
	_LIT(KINFO,"length=%d,size=%d\n"); 
	console->Printf(KINFO,bufc.Length(),bufc.Size()); 
	 
	//delete 
	ptr.Delete(0,5); 
	console->Printf(bufc); 
    console->Printf(KNEWLINE); 
} 
////////////////////////////////////////////////////////////////////////////// 
// 
// 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; 
    } 
 
 
////////////////////////////////////////////////////////////////////////////// 
// 
// 
// 
////////////////////////////////////////////////////////////////////////////// 
void doExampleL() 
{ 
  	//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->Getch(); 
		// Remove the console object from the cleanupstack 
	  // and destroy it. 	 
	CleanupStack::PopAndDestroy(); 
} 
 
void callExampleL()  
    { 
    useTBuf(); 
	console->Getch(); 
    useTBufC(); 
   } 
 
 
////////////////////////////////////////////////////////////////////////////// 
// 
// Do the example 
// 
//////////////////////////////////////////////////////////////////////////////