00001
00002
00003 #include <e32cons.h>
00004 #include "descriptorlab.h"
00005
00006
00008
00009
00010
00012 GLDEF_C TInt E32Main()
00013 {
00014 CTrapCleanup* cleanup=CTrapCleanup::New();
00015
00016
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
00036
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
00049
00050
00051
00052 CDescriptorLab::~CDescriptorLab()
00053 {
00054 delete iConsole;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 CDescriptorLab::CDescriptorLab()
00064 {
00065 }
00066
00067
00068
00069
00070
00071
00072 void CDescriptorLab::ConstructL()
00073 {
00074 _LIT(KLabTitle,"Descriptors Lab");
00075 iConsole = Console::NewL(KLabTitle, TSize(KConsFullScreen, KConsFullScreen));
00076 }
00077
00078
00079
00080
00081
00082 void CDescriptorLab::StartL()
00083 {
00084 UseBufferDes();
00085 UseHeapDesL();
00086
00087 _LIT(KMsgPressAnyKey,"Press any key to end");
00088 iConsole->Printf(KMsgPressAnyKey);
00089 iConsole->Getch();
00090
00091 }
00092
00093
00094
00095
00096
00097 void CDescriptorLab::UseBufferDes()
00098 {
00099
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
00109
00110
00111 TBuf<KBufSize> buf(0);
00112
00113
00114
00115
00116
00117 GetStringFromUser(buf);
00118 CharOccurance(buf);
00119 }
00120
00121
00122
00123
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
00133
00134 HBufC* buf = StringFromUserL();
00135
00136
00137 CharOccurance(*buf);
00138
00139 delete buf;
00140 }
00141
00142
00143
00144
00145
00146 void CDescriptorLab::GetStringFromUser(TDes& aBuf)
00147 {
00148
00149 TText key = CharFromUser();
00150
00151
00152
00153
00154 TInt maxLen = aBuf.MaxLength();
00155
00156 while (key != EKeySpace && aBuf.Length() < maxLen)
00157 {
00158
00159
00160
00161 aBuf.Append(key);
00162
00163 _LIT(KCharEntered, "%c");
00164 iConsole->Printf(KCharEntered, key);
00165 key = CharFromUser();
00166 }
00167 }
00168
00169
00170
00171
00172
00173 HBufC* CDescriptorLab::StringFromUserL()
00174 {
00175 const TInt KGranularity = 10;
00176
00177
00178 HBufC* heapBuf = HBufC::NewL(KGranularity);
00179
00180
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
00189
00190 heapBuf = heapBuf->ReAllocL(newLen);
00191
00192
00193
00194
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
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
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
00232
00233 TPtrC subStr = aSearchStr;
00234
00235
00236
00237 TInt pos = subStr.Locate(key);
00238 while (KErrNotFound != pos)
00239 {
00240 numChars++;
00241
00242
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