// Chapter 6 Examples
//
// Copyright (c) Symbian Software Ltd 1999 - 2007. All rights reserved.
//
// This file contains the source for the various examples in Chapter 6.
// It uses the console. Note that this example is made to run on the emulator only.
#include <e32base.h>
#include <e32cons.h>
CConsoleBase* console;
/*----------------------------------------------------------------------------
Stub example from console section.
-----------------------------------------------------------------------------*/
void PressAnyKey()
{
_LIT(KAnyKey,"[Press any key]\n");
console->Printf(KAnyKey);
console->Getch();
}
/*------------------------------------------------------------------------
From example comparing string handling in C and Symbian OS.
-------------------------------------------------------------------------*/
_LIT(KNamePrefix,"Name: ");
void makeName(const TDesC& aName)
{
TBuf<80> str;
str.Copy(KNamePrefix);
str.Append(aName);
_LIT(KFormatStr,"str = %S\n");
console->Printf(KFormatStr,&str);
}
void MainFunc()
{
_LIT(KFunctionStr,"---MainFunc()---");
console->Printf(KFunctionStr);
makeName(_L("Sharon")); // Note that this demonstrates _L, but _LIT should be used instead
PressAnyKey();
}
/*-------------------------------------------------------------------------------
Example from heap descriptor section
--------------------------------------------------------------------------------*/
void heapDesExampleL()
{
_LIT(KFunctionStr,"---heapDesExampleL()---");
console->Printf(KFunctionStr);
_LIT(KString1,"Test");
_LIT(KString2,"My Heap String");
HBufC* myHeapDes = HBufC::NewL(KString1().Length());
CleanupStack::PushL(myHeapDes);
*myHeapDes = KString1;
_LIT(KFormat1,"myHeapDes = \"%S\" length = %d\n");
console->Printf(KFormat1,myHeapDes,myHeapDes->Length());
myHeapDes->ReAllocL(KString2().Length()); // if you do not do this,
// next line would panic
/* Replace entire string in the HBufC */
*myHeapDes = KString2;
console->Printf(KFormat1,myHeapDes,myHeapDes->Length());
/* Get a modifiable pointer to the HBufC's data buffer */
TPtr myPtr = myHeapDes->Des();
_LIT(KString3,"Hello");
_LIT(KString4,"!!!");
/* Modify the HBufC buffer area through the TPtr, using Copy()
* and Append()
*/
myPtr.Copy(KString3);
myPtr.Append(KString4);
_LIT(KFormat2,"myHeapDes = \"%S\" length = %d myPtr = \"%S\" length = %d\n");
console->Printf(KFormat2,myHeapDes,myHeapDes->Length(), &myPtr,myPtr.Length());
CleanupStack::PopAndDestroy(myHeapDes);
PressAnyKey();
}
/*-------------------------------------------------------------------------
Examples from 'Touring the Descriptor Methods' section.
--------------------------------------------------------------------------*/
void CompareExample()
{
_LIT(KFunctionStr,"---CompareExample()---");
console->Printf(KFunctionStr);
_LIT(KString1,"My String");
_LIT(KString2,"MY STRING");
_LIT(KString3,"Another string");
TBuf<20> str1(KString1);
TBuf<20> str2(KString2);
TInt res;
/* Compare shows a match since str1 is initialized to KString1 */
res = str1.Compare(KString1);
_LIT(KFormatCompare1,"Compare() string 1 and string 1 = %d\n");
console->Printf(KFormatCompare1, res);
/* Compare shows a no match since str1 and str2 contents do
* not exactly match
*/
res = str1.Compare(str2);
_LIT(KFormatCompare2,"Compare() string 1 and string 2 = %d\n");
console->Printf(KFormatCompare2, res);
/* Compare shows a match since a folded compare is case insensitive */
res = str1.CompareF(str2);
_LIT(KFormatCompare3,"CompareF() string 1 and string 2 = %d\n");
console->Printf(KFormatCompare3, res);
/* Compare shows a mismatch since string 1 and 3 are different */
res = str1.Compare(KString3);
_LIT(KFormatCompare4,"Compare() string 1 and string 3 = %d\n");
console->Printf(KFormatCompare4, res);
PressAnyKey();
}
void FindExample()
{
_LIT(KFunctionStr,"---FindExample()---");
console->Printf(KFunctionStr);
_LIT(KString1,"This is a test string");
_LIT(KString2,"test");
_LIT(KString3,"car");
_LIT(KString4,"TEST");
TBuf<40> buf(KString1);
TInt res;
/* Find returns position of "test" in KString1 */
res = buf.Find(KString2);
_LIT(KFormat1,"Find of string 2 in string 1 res = %d\n");
console->Printf(KFormat1,res);
/* "car" does not occur in KString1, so KErrNotFound reported */
res = buf.Find(KString3);
_LIT(KFormat2,"Find of string 3 in string 1 res = %d\n");
console->Printf(KFormat2,res);
/* Since FindF does a fold compare, "TEST" is found and position
* is returned
*/
res = buf.FindF(KString4);
_LIT(KFormat3,"Find of string 4 in string 1 res = %d\n");
console->Printf(KFormat3,res);
PressAnyKey();
}
void MatchExample()
{
_LIT(KFunctionStr,"---MatchExample()---");
console->Printf(KFunctionStr);
_LIT(KString1,"This is test string A");
_LIT(KString2,"This is test string ?");
_LIT(KString3,"*is test string ?");
_LIT(KString4,"*");
_LIT(KString5,"*B");
TBuf<40> buf(KString1);
TInt res;
/* A match since ? indicates any single character */
res = buf.Match(KString2);
_LIT(KFormat1,"Match: string 2 and string 1 res = %d\n");
console->Printf(KFormat1,res);
/* A match using '*' and '?' in string */
res = buf.Match(KString3);
_LIT(KFormat2,"Match: string 3 and string 1 res = %d\n");
console->Printf(KFormat2,res);
/* '*' matches any string */
res = buf.Match(KString4);
_LIT(KFormat3,"Match: string 4 and string 1 res = %d\n");
console->Printf(KFormat3,res);
/* no match since KString1 does not end in 'B' */
res = buf.Match(KString5);
_LIT(KFormat4,"Match: string 5 and string 1 res = %d\n");
console->Printf(KFormat4,res);
PressAnyKey();
}
void SubstringExample()
{
_LIT(KFunctionStr,"---SubstringExample()---");
console->Printf(KFunctionStr);
_LIT(KString1,"This is my string");
TBufC<40> buff(KString1);
TPtrC SubStr= buff.Left(4);
/* Get left 4 characters of string */
_LIT(KFormat1,"Left(4): SubStr = \"%S\"\n");
console->Printf(KFormat1,&SubStr);
/* Get right 3 characters of string */
SubStr.Set(buff.Right(3));
_LIT(KFormat2,"Right(3): SubStr = \"%S\"\n");
console->Printf(KFormat2,&SubStr);
/* get 6 characters in middle, starting at position 8 */
SubStr.Set(buff.Mid(8,6));
_LIT(KFormat3,"Mid(8,6): SubStr = \"%S\"\n");
console->Printf(KFormat3,&SubStr);
PressAnyKey();
}
void CopyExample()
{
_LIT(KFunctionStr,"---CopyExample()---");
console->Printf(KFunctionStr);
TUint8 binData[6] = {0xB0,0xB1,0xB2,0xB3,0xB4,0xB5};
/* Copy standard C array into binary descriptor */
TBuf8<sizeof(binData)> binDes;
binDes.Copy(binData,sizeof(binData));
_LIT(KFormat1,"binDes[0]=%x binDes[1]=%x\n");
console->Printf(KFormat1,binDes[0],binDes[1]);
/* Copy binary descriptor to another 8 bit binary descriptor */
TBuf8<20> buf8;
buf8.Copy(binDes);
_LIT(KFormat2,"buf8[0]=%x buf8[1]=%x\n");
console->Printf(KFormat2,buf8[0],buf8[1]);
/* Copy literal into descriptor */
_LIT(KString1,"My string");
TBuf<20> buf16;
buf16.C