examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04305.cb1/solution/src/DescriptorLab.cpp

00001 // Copyright: (c) 2006 Nokia Ltd.  All rights reserved.
00002 
00003 #include <e32cons.h>
00004 #include "descriptorlab.h"
00005 
00006 
00008 //
00009 // Main function called by E32
00010 //
00012 GLDEF_C TInt E32Main()
00013     {
00014         CTrapCleanup* cleanup=CTrapCleanup::New();
00015 
00016     // Catch any Leaves thrown 
00017     TRAPD(
00018         error, 
00019         CDescriptorLab* lab = CDescriptorLab::NewLC();
00020         lab->StartL();
00021         CleanupStack::PopAndDestroy();
00022         );
00023 
00024         _LIT(KMsgPanic,"Error in Descriptor Lab: ");
00025         __ASSERT_ALWAYS(!error, User::Panic(KMsgPanic, error));
00026 
00027     delete cleanup;
00028         return 0;
00029     }
00030 
00031 
00032 
00033 
00034 // ---------------------------------------------------------
00035 // CDescriptorLab::NewLC()
00036 // Two-phase constructor
00037 // ---------------------------------------------------------
00038 //
00039 CDescriptorLab* CDescriptorLab::NewLC()
00040     {
00041     CDescriptorLab* self = new (ELeave) CDescriptorLab;
00042     CleanupStack::PushL(self);
00043     self->ConstructL();
00044     return self;
00045     }
00046 
00047 // ---------------------------------------------------------
00048 // CDescriptorLab::~CDescriptorLab()
00049 // Destructor
00050 // ---------------------------------------------------------
00051 //
00052 CDescriptorLab::~CDescriptorLab()
00053     {
00054     delete iConsole;
00055     }
00056 
00057 
00058 // ---------------------------------------------------------
00059 // CDescriptorLab::CDescriptorLab()
00060 // Default C++ constructor
00061 // ---------------------------------------------------------
00062 //
00063 CDescriptorLab::CDescriptorLab()
00064     {
00065     }
00066 
00067 // ---------------------------------------------------------
00068 // CDescriptorLab::ConstructL()
00069 // Second-phase constructor
00070 // ---------------------------------------------------------
00071 //
00072 void CDescriptorLab::ConstructL()
00073     {
00074     _LIT(KLabTitle,"Descriptors Lab");
00075     iConsole = Console::NewL(KLabTitle, TSize(KConsFullScreen, KConsFullScreen));
00076     }
00077 
00078 // ---------------------------------------------------------
00079 // CDescriptorLab::StartL()
00080 // ---------------------------------------------------------
00081 //
00082 void CDescriptorLab::StartL() 
00083     {
00084     UseBufferDes();
00085     UseHeapDesL();
00086     // Continue
00087         _LIT(KMsgPressAnyKey,"Press any key to end");
00088         iConsole->Printf(KMsgPressAnyKey);
00089         iConsole->Getch();
00090     
00091     }
00092 
00093 // ---------------------------------------------------------
00094 // CDescriptorLab::UseBufferDes()
00095 // ---------------------------------------------------------
00096 //
00097 void CDescriptorLab::UseBufferDes()
00098     {
00099     // Edit 1: Uncomment the following line to declare a literal string 
00100     _LIT(KBufferTitle, "BUF DESCRIPTOR EG:\n");
00101     iConsole->Printf(KBufferTitle);
00102 
00103     const TInt KBufSize = 12;
00104     
00105     _LIT(KEnterString, "Enter upto %d chars. Finish with space:\n");
00106     iConsole->Printf(KEnterString, KBufSize);
00107 
00108     // Look at key input
00109     // Edit 2: Declare a TBuf named buf of max size KBufSize.  
00110     //          Initialise the length of the buffer to 0
00111     TBuf<KBufSize> buf(0);
00112 
00113     // Edit 3: Uncomment the following 2 lines of code to get a string 
00114     // of upto KBufSize characters from the user.  A character is then 
00115     // got from the user and the number of occurances of that character 
00116     // in the string buf printed to the screen.
00117     GetStringFromUser(buf);
00118     CharOccurance(buf);
00119     }
00120 
00121 
00122 // ---------------------------------------------------------
00123 // CDescriptorLab::UseHeapDesL()
00124 // ---------------------------------------------------------
00125 //
00126 void CDescriptorLab::UseHeapDesL()
00127     {
00128     _LIT(KHeapTitle, "\nHEAP DESCRIPTOR EG:\n");
00129     iConsole->Printf(KHeapTitle);
00130     _LIT(KEnterString, "Enter as many chars as you wish.\nFinish with space:\n");
00131     iConsole->Printf(KEnterString);
00132     // Edit 9: Declare a HBufC* called buf that is initialised to the return 
00133     //          value of a call to HBufC* CDescriptorLab::StringFromUserL()
00134         HBufC* buf = StringFromUserL();
00135     // Edit 10: Call CDescriptorLab::CharOccurance() passing the dereferenced 
00136     //          value of buf
00137         CharOccurance(*buf);
00138     // Edit 11: delete the memory associated with buf
00139         delete buf;
00140     }
00141 
00142 // ---------------------------------------------------------
00143 // CDescriptorLab::GetStringFromUser(TDes& aBuf)
00144 // ---------------------------------------------------------
00145 //
00146 void CDescriptorLab::GetStringFromUser(TDes& aBuf)
00147     {
00148     // Get a character from the user
00149     TText key = CharFromUser();
00150 
00151     // Edit 4: Declare a TInt maxLen that is initialised  
00152     // to the maximum length of the descriptor parameter.
00153     // Hint: use aBuf.MaxLength()
00154         TInt maxLen = aBuf.MaxLength();
00155 
00156     while (key != EKeySpace &&  aBuf.Length() < maxLen)
00157         {
00158         // Edit 5: Append the user entered character onto the end 
00159         // of the descriptor passed in as a parameter
00160         // Hint: Use aBuf.Append(key)
00161         aBuf.Append(key);
00162 
00163         _LIT(KCharEntered, "%c");
00164         iConsole->Printf(KCharEntered, key);
00165         key = CharFromUser();
00166         }
00167     }
00168 
00169 // ---------------------------------------------------------
00170 // CDescriptorLab::StringFromUserL()
00171 // ---------------------------------------------------------
00172 //
00173 HBufC* CDescriptorLab::StringFromUserL()
00174     {
00175     const TInt KGranularity = 10;
00176     // Edit 12: Declare a HBufC* called heapBuf and construct 
00177     //          via HBufC::NewL(KGranularity) 
00178         HBufC* heapBuf = HBufC::NewL(KGranularity);
00179 
00180     // Edit 13: Declare a TPtr called ptr that is initialised to heapBuf->Des()
00181         TPtr ptr(heapBuf->Des());
00182     TText key = CharFromUser();
00183     while (key != EKeySpace)
00184         {
00185         if (ptr.Length() == ptr.MaxLength())
00186             {
00187             TInt newLen = ptr.MaxLength() + KGranularity;
00188             // Edit 14: Uncomment the line below to increase the maxlength of 
00189             //          heapBuf and retain the existing contents and length
00190             heapBuf = heapBuf->ReAllocL(newLen);
00191 
00192             // Edit 15: Reset ptr to point to the new location of heapBuf
00193             //          Hint: call ptr.Set(heapBuf->Des()) rather than
00194             //          ptr = heapBuf->Des() as the latter will cause a panic
00195                         ptr.Set(heapBuf->Des());
00196             }
00197         ptr.Append(key);
00198         _LIT(KCharEntered, "%c");
00199         iConsole->Printf(KCharEntered, key);
00200         key = CharFromUser();
00201         }
00202     return heapBuf;
00203     }
00204 
00205 // ---------------------------------------------------------
00206 // CDescriptorLab::CharFromUser()
00207 // ---------------------------------------------------------
00208 //
00209 TText CDescriptorLab::CharFromUser()
00210     {
00211     TKeyCode key = iConsole->Getch();
00212     TText ret = static_cast<TText>(key);
00213     return ret;
00214     }
00215 
00216 
00217 // ---------------------------------------------------------
00218 // CDescriptorLab::CharOccurance(TDesC& aSearchStr)
00219 // ---------------------------------------------------------
00220 //
00221 void CDescriptorLab::CharOccurance(TDesC& aSearchStr)
00222     {
00223     _LIT(KEnterChar, "\nEnter search char:");
00224     iConsole->Printf(KEnterChar);
00225 
00226     TText key = CharFromUser();
00227     _LIT(KCharEntered, "%c");
00228     iConsole->Printf(KCharEntered, key);
00229 
00230     TInt numChars = 0;
00231     // Edit 6: Declare a TPtrC called subStr and initialise it to 
00232     //          the aSearchStr parameter
00233         TPtrC subStr = aSearchStr;
00234 
00235     // Edit 7: Uncomment the line of code below to find the position of the 
00236     //          first occurence of the char entered by the user in aSearchString
00237     TInt pos = subStr.Locate(key);
00238     while (KErrNotFound != pos)
00239         {
00240         numChars++;
00241         // Edit 8: Uncomment the line of code below to reset subStr to point to the 
00242         //          remainder of aSearchStr after the position of the last found char
00243         subStr.Set(subStr.Mid(pos + 1));
00244         pos = subStr.Locate(key);
00245         }
00246     _LIT(KNumChars, "\nNumber of '%c's in\n\"%S\" = %d\n");
00247     iConsole->Printf(KNumChars, key, &aSearchStr, numChars);
00248     }
00249 
00250 
00251 

Generated by  doxygen 1.6.2