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 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 00135 // Edit 10: Call CDescriptorLab::CharOccurance() passing the dereferenced 00136 // value of buf 00137 00138 // Edit 11: delete the memory associated with buf 00139 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 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 00162 _LIT(KCharEntered, "%c"); 00163 iConsole->Printf(KCharEntered, key); 00164 key = CharFromUser(); 00165 } 00166 } 00167 00168 // --------------------------------------------------------- 00169 // CDescriptorLab::StringFromUserL() 00170 // --------------------------------------------------------- 00171 // 00172 HBufC* CDescriptorLab::StringFromUserL() 00173 { 00174 const TInt KGranularity = 10; 00175 // Edit 12: Declare a HBufC* called heapBuf and construct 00176 // via HBufC::NewL(KGranularity) 00177 00178 00179 // Edit 13: Declare a TPtr called ptr that is initialised to heapBuf->Des() 00180 00181 TText key = CharFromUser(); 00182 while (key != EKeySpace) 00183 { 00184 if (ptr.Length() == ptr.MaxLength()) 00185 { 00186 TInt newLen = ptr.MaxLength() + KGranularity; 00187 // Edit 14: Uncomment the line below to increase the maxlength of 00188 // heapBuf and retain the existing contents and length 00189 // heapBuf = heapBuf->ReAllocL(newLen); 00190 00191 // Edit 15: Reset ptr to point to the new location of heapBuf 00192 // Hint: call ptr.Set(heapBuf->Des()) rather than 00193 // ptr = heapBuf->Des() as the latter will cause a panic 00194 00195 } 00196 ptr.Append(key); 00197 _LIT(KCharEntered, "%c"); 00198 iConsole->Printf(KCharEntered, key); 00199 key = CharFromUser(); 00200 } 00201 return heapBuf; 00202 } 00203 00204 // --------------------------------------------------------- 00205 // CDescriptorLab::CharFromUser() 00206 // --------------------------------------------------------- 00207 // 00208 TText CDescriptorLab::CharFromUser() 00209 { 00210 TKeyCode key = iConsole->Getch(); 00211 TText ret = static_cast<TText>(key); 00212 return ret; 00213 } 00214 00215 00216 // --------------------------------------------------------- 00217 // CDescriptorLab::CharOccurance(TDesC& aSearchStr) 00218 // --------------------------------------------------------- 00219 // 00220 void CDescriptorLab::CharOccurance(TDesC& aSearchStr) 00221 { 00222 _LIT(KEnterChar, "\nEnter search char:"); 00223 iConsole->Printf(KEnterChar); 00224 00225 TText key = CharFromUser(); 00226 _LIT(KCharEntered, "%c"); 00227 iConsole->Printf(KCharEntered, key); 00228 00229 TInt numChars = 0; 00230 // Edit 6: Declare a TPtrC called subStr and initialise it to 00231 // the aSearchStr parameter 00232 00233 00234 // Edit 7: Uncomment the line of code below to find the position of the 00235 // first occurence of the char entered by the user in aSearchString 00236 // TInt pos = subStr.Locate(key); 00237 while (KErrNotFound != pos) 00238 { 00239 numChars++; 00240 // Edit 8: Uncomment the line of code below to reset subStr to point to the 00241 // remainder of aSearchStr after the position of the last found char 00242 // subStr.Set(subStr.Mid(pos + 1)); 00243 pos = subStr.Locate(key); 00244 } 00245 _LIT(KNumChars, "\nNumber of '%c's in\n\"%S\" = %d\n"); 00246 iConsole->Printf(KNumChars, key, &aSearchStr, numChars); 00247 } 00248 00249 00250