Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/solution_2src_2_descriptor_lab_8cpp_source.html
Initial contribution of the Adaptation Documentation.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04305.cb1/solution/src/DescriptorLab.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04305.cb1/solution/src/DescriptorLab.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright: (c) 2006 Nokia Ltd. All rights reserved.</span>
<a name="l00002"></a>00002
<a name="l00003"></a>00003 <span class="preprocessor">#include <e32cons.h></span>
<a name="l00004"></a>00004 <span class="preprocessor">#include "descriptorlab.h"</span>
<a name="l00005"></a>00005
<a name="l00006"></a>00006
<a name="l00008"></a>00008 <span class="comment">//</span>
<a name="l00009"></a>00009 <span class="comment">// Main function called by E32</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00012"></a>00012 <span class="comment"></span>GLDEF_C TInt E32Main()
<a name="l00013"></a>00013 {
<a name="l00014"></a>00014 CTrapCleanup* cleanup=CTrapCleanup::New();
<a name="l00015"></a>00015
<a name="l00016"></a>00016 <span class="comment">// Catch any Leaves thrown </span>
<a name="l00017"></a>00017 TRAPD(
<a name="l00018"></a>00018 error,
<a name="l00019"></a>00019 <a class="code" href="class_c_descriptor_lab.html">CDescriptorLab</a>* lab = <a class="code" href="class_c_descriptor_lab.html#a25698463240723af038ed5270df2ff77">CDescriptorLab::NewLC</a>();
<a name="l00020"></a>00020 lab-><a class="code" href="class_c_descriptor_lab.html#a16fa40a57579ad45f26ae86882da04aa">StartL</a>();
<a name="l00021"></a>00021 CleanupStack::PopAndDestroy();
<a name="l00022"></a>00022 );
<a name="l00023"></a>00023
<a name="l00024"></a>00024 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KMsgPanic,<span class="stringliteral">"Error in Descriptor Lab: "</span>);
<a name="l00025"></a>00025 __ASSERT_ALWAYS(!error, User::Panic(KMsgPanic, error));
<a name="l00026"></a>00026
<a name="l00027"></a>00027 <span class="keyword">delete</span> cleanup;
<a name="l00028"></a>00028 <span class="keywordflow">return</span> 0;
<a name="l00029"></a>00029 }
<a name="l00030"></a>00030
<a name="l00031"></a>00031
<a name="l00032"></a>00032
<a name="l00033"></a>00033
<a name="l00034"></a>00034 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00035"></a>00035 <span class="comment">// CDescriptorLab::NewLC()</span>
<a name="l00036"></a>00036 <span class="comment">// Two-phase constructor</span>
<a name="l00037"></a>00037 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00038"></a>00038 <span class="comment">//</span>
<a name="l00039"></a><a class="code" href="class_c_descriptor_lab.html#a25698463240723af038ed5270df2ff77">00039</a> <a class="code" href="class_c_descriptor_lab.html">CDescriptorLab</a>* <a class="code" href="class_c_descriptor_lab.html#a25698463240723af038ed5270df2ff77">CDescriptorLab::NewLC</a>()
<a name="l00040"></a>00040 {
<a name="l00041"></a>00041 <a class="code" href="class_c_descriptor_lab.html">CDescriptorLab</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_descriptor_lab.html">CDescriptorLab</a>;
<a name="l00042"></a>00042 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00043"></a>00043 <span class="keyword">self</span>->ConstructL();
<a name="l00044"></a>00044 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00045"></a>00045 }
<a name="l00046"></a>00046
<a name="l00047"></a>00047 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00048"></a>00048 <span class="comment">// CDescriptorLab::~CDescriptorLab()</span>
<a name="l00049"></a>00049 <span class="comment">// Destructor</span>
<a name="l00050"></a>00050 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00051"></a>00051 <span class="comment">//</span>
<a name="l00052"></a><a class="code" href="class_c_descriptor_lab.html#a3c025131d5a030b352ed21ee377913da">00052</a> <a class="code" href="class_c_descriptor_lab.html#a3c025131d5a030b352ed21ee377913da">CDescriptorLab::~CDescriptorLab</a>()
<a name="l00053"></a>00053 {
<a name="l00054"></a>00054 <span class="keyword">delete</span> iConsole;
<a name="l00055"></a>00055 }
<a name="l00056"></a>00056
<a name="l00057"></a>00057
<a name="l00058"></a>00058 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00059"></a>00059 <span class="comment">// CDescriptorLab::CDescriptorLab()</span>
<a name="l00060"></a>00060 <span class="comment">// Default C++ constructor</span>
<a name="l00061"></a>00061 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00062"></a>00062 <span class="comment">//</span>
<a name="l00063"></a>00063 CDescriptorLab::CDescriptorLab()
<a name="l00064"></a>00064 {
<a name="l00065"></a>00065 }
<a name="l00066"></a>00066
<a name="l00067"></a>00067 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00068"></a>00068 <span class="comment">// CDescriptorLab::ConstructL()</span>
<a name="l00069"></a>00069 <span class="comment">// Second-phase constructor</span>
<a name="l00070"></a>00070 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00071"></a>00071 <span class="comment">//</span>
<a name="l00072"></a>00072 <span class="keywordtype">void</span> CDescriptorLab::ConstructL()
<a name="l00073"></a>00073 {
<a name="l00074"></a>00074 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KLabTitle,<span class="stringliteral">"Descriptors Lab"</span>);
<a name="l00075"></a>00075 iConsole = Console::NewL(KLabTitle, TSize(KConsFullScreen, KConsFullScreen));
<a name="l00076"></a>00076 }
<a name="l00077"></a>00077
<a name="l00078"></a>00078 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00079"></a>00079 <span class="comment">// CDescriptorLab::StartL()</span>
<a name="l00080"></a>00080 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00081"></a>00081 <span class="comment">//</span>
<a name="l00082"></a><a class="code" href="class_c_descriptor_lab.html#a16fa40a57579ad45f26ae86882da04aa">00082</a> <span class="keywordtype">void</span> <a class="code" href="class_c_descriptor_lab.html#a16fa40a57579ad45f26ae86882da04aa">CDescriptorLab::StartL</a>()
<a name="l00083"></a>00083 {
<a name="l00084"></a>00084 UseBufferDes();
<a name="l00085"></a>00085 UseHeapDesL();
<a name="l00086"></a>00086 <span class="comment">// Continue</span>
<a name="l00087"></a>00087 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KMsgPressAnyKey,<span class="stringliteral">"Press any key to end"</span>);
<a name="l00088"></a>00088 iConsole->Printf(KMsgPressAnyKey);
<a name="l00089"></a>00089 iConsole->Getch();
<a name="l00090"></a>00090
<a name="l00091"></a>00091 }
<a name="l00092"></a>00092
<a name="l00093"></a>00093 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00094"></a>00094 <span class="comment">// CDescriptorLab::UseBufferDes()</span>
<a name="l00095"></a>00095 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00096"></a>00096 <span class="comment">//</span>
<a name="l00097"></a>00097 <span class="keywordtype">void</span> CDescriptorLab::UseBufferDes()
<a name="l00098"></a>00098 {
<a name="l00099"></a>00099 <span class="comment">// Edit 1: Uncomment the following line to declare a literal string </span>
<a name="l00100"></a>00100 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KBufferTitle, <span class="stringliteral">"BUF DESCRIPTOR EG:\n"</span>);
<a name="l00101"></a>00101 iConsole->Printf(KBufferTitle);
<a name="l00102"></a>00102
<a name="l00103"></a>00103 <span class="keyword">const</span> TInt KBufSize = 12;
<a name="l00104"></a>00104
<a name="l00105"></a>00105 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KEnterString, <span class="stringliteral">"Enter upto %d chars. Finish with space:\n"</span>);
<a name="l00106"></a>00106 iConsole->Printf(KEnterString, KBufSize);
<a name="l00107"></a>00107
<a name="l00108"></a>00108 <span class="comment">// Look at key input</span>
<a name="l00109"></a>00109 <span class="comment">// Edit 2: Declare a TBuf named buf of max size KBufSize. </span>
<a name="l00110"></a>00110 <span class="comment">// Initialise the length of the buffer to 0</span>
<a name="l00111"></a>00111 TBuf<KBufSize> buf(0);
<a name="l00112"></a>00112
<a name="l00113"></a>00113 <span class="comment">// Edit 3: Uncomment the following 2 lines of code to get a string </span>
<a name="l00114"></a>00114 <span class="comment">// of upto KBufSize characters from the user. A character is then </span>
<a name="l00115"></a>00115 <span class="comment">// got from the user and the number of occurances of that character </span>
<a name="l00116"></a>00116 <span class="comment">// in the string buf printed to the screen.</span>
<a name="l00117"></a>00117 GetStringFromUser(buf);
<a name="l00118"></a>00118 CharOccurance(buf);
<a name="l00119"></a>00119 }
<a name="l00120"></a>00120
<a name="l00121"></a>00121
<a name="l00122"></a>00122 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00123"></a>00123 <span class="comment">// CDescriptorLab::UseHeapDesL()</span>
<a name="l00124"></a>00124 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00125"></a>00125 <span class="comment">//</span>
<a name="l00126"></a>00126 <span class="keywordtype">void</span> CDescriptorLab::UseHeapDesL()
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KHeapTitle, <span class="stringliteral">"\nHEAP DESCRIPTOR EG:\n"</span>);
<a name="l00129"></a>00129 iConsole->Printf(KHeapTitle);
<a name="l00130"></a>00130 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KEnterString, <span class="stringliteral">"Enter as many chars as you wish.\nFinish with space:\n"</span>);
<a name="l00131"></a>00131 iConsole->Printf(KEnterString);
<a name="l00132"></a>00132 <span class="comment">// Edit 9: Declare a HBufC* called buf that is initialised to the return </span>
<a name="l00133"></a>00133 <span class="comment">// value of a call to HBufC* CDescriptorLab::StringFromUserL()</span>
<a name="l00134"></a>00134 HBufC* buf = StringFromUserL();
<a name="l00135"></a>00135 <span class="comment">// Edit 10: Call CDescriptorLab::CharOccurance() passing the dereferenced </span>
<a name="l00136"></a>00136 <span class="comment">// value of buf</span>
<a name="l00137"></a>00137 CharOccurance(*buf);
<a name="l00138"></a>00138 <span class="comment">// Edit 11: delete the memory associated with buf</span>
<a name="l00139"></a>00139 <span class="keyword">delete</span> buf;
<a name="l00140"></a>00140 }
<a name="l00141"></a>00141
<a name="l00142"></a>00142 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00143"></a>00143 <span class="comment">// CDescriptorLab::GetStringFromUser(TDes& aBuf)</span>
<a name="l00144"></a>00144 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00145"></a>00145 <span class="comment">//</span>
<a name="l00146"></a>00146 <span class="keywordtype">void</span> CDescriptorLab::GetStringFromUser(TDes& aBuf)
<a name="l00147"></a>00147 {
<a name="l00148"></a>00148 <span class="comment">// Get a character from the user</span>
<a name="l00149"></a>00149 TText key = CharFromUser();
<a name="l00150"></a>00150
<a name="l00151"></a>00151 <span class="comment">// Edit 4: Declare a TInt maxLen that is initialised </span>
<a name="l00152"></a>00152 <span class="comment">// to the maximum length of the descriptor parameter.</span>
<a name="l00153"></a>00153 <span class="comment">// Hint: use aBuf.MaxLength()</span>
<a name="l00154"></a>00154 TInt maxLen = aBuf.MaxLength();
<a name="l00155"></a>00155
<a name="l00156"></a>00156 <span class="keywordflow">while</span> (key != EKeySpace && aBuf.Length() < maxLen)
<a name="l00157"></a>00157 {
<a name="l00158"></a>00158 <span class="comment">// Edit 5: Append the user entered character onto the end </span>
<a name="l00159"></a>00159 <span class="comment">// of the descriptor passed in as a parameter</span>
<a name="l00160"></a>00160 <span class="comment">// Hint: Use aBuf.Append(key)</span>
<a name="l00161"></a>00161 aBuf.Append(key);
<a name="l00162"></a>00162
<a name="l00163"></a>00163 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCharEntered, <span class="stringliteral">"%c"</span>);
<a name="l00164"></a>00164 iConsole->Printf(KCharEntered, key);
<a name="l00165"></a>00165 key = CharFromUser();
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167 }
<a name="l00168"></a>00168
<a name="l00169"></a>00169 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00170"></a>00170 <span class="comment">// CDescriptorLab::StringFromUserL()</span>
<a name="l00171"></a>00171 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00172"></a>00172 <span class="comment">//</span>
<a name="l00173"></a>00173 HBufC* CDescriptorLab::StringFromUserL()
<a name="l00174"></a>00174 {
<a name="l00175"></a>00175 <span class="keyword">const</span> TInt KGranularity = 10;
<a name="l00176"></a>00176 <span class="comment">// Edit 12: Declare a HBufC* called heapBuf and construct </span>
<a name="l00177"></a>00177 <span class="comment">// via HBufC::NewL(KGranularity) </span>
<a name="l00178"></a>00178 HBufC* heapBuf = HBufC::NewL(KGranularity);
<a name="l00179"></a>00179
<a name="l00180"></a>00180 <span class="comment">// Edit 13: Declare a TPtr called ptr that is initialised to heapBuf->Des()</span>
<a name="l00181"></a>00181 TPtr ptr(heapBuf->Des());
<a name="l00182"></a>00182 TText key = CharFromUser();
<a name="l00183"></a>00183 <span class="keywordflow">while</span> (key != EKeySpace)
<a name="l00184"></a>00184 {
<a name="l00185"></a>00185 <span class="keywordflow">if</span> (ptr.Length() == ptr.MaxLength())
<a name="l00186"></a>00186 {
<a name="l00187"></a>00187 TInt newLen = ptr.MaxLength() + KGranularity;
<a name="l00188"></a>00188 <span class="comment">// Edit 14: Uncomment the line below to increase the maxlength of </span>
<a name="l00189"></a>00189 <span class="comment">// heapBuf and retain the existing contents and length</span>
<a name="l00190"></a>00190 heapBuf = heapBuf->ReAllocL(newLen);
<a name="l00191"></a>00191
<a name="l00192"></a>00192 <span class="comment">// Edit 15: Reset ptr to point to the new location of heapBuf</span>
<a name="l00193"></a>00193 <span class="comment">// Hint: call ptr.Set(heapBuf->Des()) rather than</span>
<a name="l00194"></a>00194 <span class="comment">// ptr = heapBuf->Des() as the latter will cause a panic</span>
<a name="l00195"></a>00195 ptr.Set(heapBuf->Des());
<a name="l00196"></a>00196 }
<a name="l00197"></a>00197 ptr.Append(key);
<a name="l00198"></a>00198 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCharEntered, <span class="stringliteral">"%c"</span>);
<a name="l00199"></a>00199 iConsole->Printf(KCharEntered, key);
<a name="l00200"></a>00200 key = CharFromUser();
<a name="l00201"></a>00201 }
<a name="l00202"></a>00202 <span class="keywordflow">return</span> heapBuf;
<a name="l00203"></a>00203 }
<a name="l00204"></a>00204
<a name="l00205"></a>00205 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00206"></a>00206 <span class="comment">// CDescriptorLab::CharFromUser()</span>
<a name="l00207"></a>00207 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00208"></a>00208 <span class="comment">//</span>
<a name="l00209"></a>00209 TText CDescriptorLab::CharFromUser()
<a name="l00210"></a>00210 {
<a name="l00211"></a>00211 TKeyCode key = iConsole->Getch();
<a name="l00212"></a>00212 TText ret = <span class="keyword">static_cast<</span>TText<span class="keyword">></span>(key);
<a name="l00213"></a>00213 <span class="keywordflow">return</span> ret;
<a name="l00214"></a>00214 }
<a name="l00215"></a>00215
<a name="l00216"></a>00216
<a name="l00217"></a>00217 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00218"></a>00218 <span class="comment">// CDescriptorLab::CharOccurance(TDesC& aSearchStr)</span>
<a name="l00219"></a>00219 <span class="comment">// ---------------------------------------------------------</span>
<a name="l00220"></a>00220 <span class="comment">//</span>
<a name="l00221"></a>00221 <span class="keywordtype">void</span> CDescriptorLab::CharOccurance(TDesC& aSearchStr)
<a name="l00222"></a>00222 {
<a name="l00223"></a>00223 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KEnterChar, <span class="stringliteral">"\nEnter search char:"</span>);
<a name="l00224"></a>00224 iConsole->Printf(KEnterChar);
<a name="l00225"></a>00225
<a name="l00226"></a>00226 TText key = CharFromUser();
<a name="l00227"></a>00227 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCharEntered, <span class="stringliteral">"%c"</span>);
<a name="l00228"></a>00228 iConsole->Printf(KCharEntered, key);
<a name="l00229"></a>00229
<a name="l00230"></a>00230 TInt numChars = 0;
<a name="l00231"></a>00231 <span class="comment">// Edit 6: Declare a TPtrC called subStr and initialise it to </span>
<a name="l00232"></a>00232 <span class="comment">// the aSearchStr parameter</span>
<a name="l00233"></a>00233 TPtrC subStr = aSearchStr;
<a name="l00234"></a>00234
<a name="l00235"></a>00235 <span class="comment">// Edit 7: Uncomment the line of code below to find the position of the </span>
<a name="l00236"></a>00236 <span class="comment">// first occurence of the char entered by the user in aSearchString</span>
<a name="l00237"></a>00237 TInt pos = subStr.Locate(key);
<a name="l00238"></a>00238 <span class="keywordflow">while</span> (KErrNotFound != pos)
<a name="l00239"></a>00239 {
<a name="l00240"></a>00240 numChars++;
<a name="l00241"></a>00241 <span class="comment">// Edit 8: Uncomment the line of code below to reset subStr to point to the </span>
<a name="l00242"></a>00242 <span class="comment">// remainder of aSearchStr after the position of the last found char</span>
<a name="l00243"></a>00243 subStr.Set(subStr.Mid(pos + 1));
<a name="l00244"></a>00244 pos = subStr.Locate(key);
<a name="l00245"></a>00245 }
<a name="l00246"></a>00246 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KNumChars, <span class="stringliteral">"\nNumber of '%c's in\n\"%S\" = %d\n"</span>);
<a name="l00247"></a>00247 iConsole->Printf(KNumChars, key, &aSearchStr, numChars);
<a name="l00248"></a>00248 }
<a name="l00249"></a>00249
<a name="l00250"></a>00250
<a name="l00251"></a>00251
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>