Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/rbufexample_8cpp-source.html
Week 12 contribution of SDK documentation_content. See release notes for details. Fixes Bug 1892, Bug 1522, Bug 1520, Bug 394, Bug 1319, Bug 344, Bug 1897
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>TB10.1 Example Applications: examples/Base/BufsAndStrings/rbufexample/rbufexample.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.3 -->
<h1>examples/Base/BufsAndStrings/rbufexample/rbufexample.cpp</h1><a href="rbufexample_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Description:</span>
<a name="l00014"></a>00014 <span class="comment">// This example program demonstrates how to use the resizable buffer descriptor class RBuf.</span>
<a name="l00015"></a>00015 <span class="comment">//</span>
<a name="l00016"></a>00016
<a name="l00017"></a>00017
<a name="l00018"></a>00018
<a name="l00022"></a>00022 <span class="preprocessor">#include "rbufexample.h"</span>
<a name="l00023"></a>00023
<a name="l00024"></a><a class="code" href="rbufexample_8cpp.html#aa9f486ebd20badddb8451883a82e763">00024</a> <span class="keyword">const</span> TInt <a class="code" href="rbufexample_8cpp.html#aa9f486ebd20badddb8451883a82e763">KLargeBufSize</a> = 15;
<a name="l00025"></a><a class="code" href="rbufexample_8cpp.html#daf0758eb2344d655702792acb8ced64">00025</a> <span class="keyword">const</span> TInt <a class="code" href="rbufexample_8cpp.html#daf0758eb2344d655702792acb8ced64">KSmallBufSize</a> = 3;
<a name="l00026"></a><a class="code" href="rbufexample_8cpp.html#9b8374bb15a2980f3b92ac9a002e8cf5">00026</a> <span class="keyword">const</span> TInt <a class="code" href="rbufexample_8cpp.html#9b8374bb15a2980f3b92ac9a002e8cf5">KMedBufSize</a> = 7;
<a name="l00027"></a>00027
<a name="l00028"></a>00028 _LIT(KFailed, <span class="stringliteral">"\n Error occurred"</span>);
<a name="l00029"></a>00029 _LIT(KPressAKey, <span class="stringliteral">"\n\n Press any key to continue the example\n"</span>);
<a name="l00030"></a>00030 _LIT(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>,<span class="stringliteral">"\n Length= %d"</span>);
<a name="l00031"></a>00031 _LIT(KMaxLength,<span class="stringliteral">"\n MaxLength= %d"</span>);
<a name="l00032"></a>00032 _LIT(KBuffer1,<span class="stringliteral">"11111"</span>);
<a name="l00033"></a>00033 _LIT(KBuffer2,<span class="stringliteral">"22222"</span>);
<a name="l00034"></a>00034
<a name="l00038"></a>00038 CRBufExample::CRBufExample()
<a name="l00039"></a>00039 {
<a name="l00040"></a>00040 }
<a name="l00041"></a>00041
<a name="l00042"></a>00042 <span class="keywordtype">void</span> CRBufExample::ConstructL()
<a name="l00043"></a>00043 {
<a name="l00044"></a>00044 _LIT(KTitle,<span class="stringliteral">" RBuf Example"</span>);
<a name="l00045"></a>00045 iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
<a name="l00046"></a>00046
<a name="l00047"></a>00047 _LIT(KWelcome,<span class="stringliteral">" Welcome to the RBuf example application"</span>);
<a name="l00048"></a>00048 iConsole->Printf(KWelcome);
<a name="l00049"></a>00049
<a name="l00050"></a>00050 _LIT(KPressAKeyMsg, <span class="stringliteral">"\n\n Press any key to step through the example\n"</span>);
<a name="l00051"></a>00051 iConsole->Printf(KPressAKeyMsg );
<a name="l00052"></a>00052 iConsole->Getch();
<a name="l00053"></a>00053 }
<a name="l00054"></a>00054
<a name="l00058"></a><a class="code" href="class_c_r_buf_example.html#70b4f8db8bcc7c567f48594d2f7371c5">00058</a> <a class="code" href="class_c_r_buf_example.html#70b4f8db8bcc7c567f48594d2f7371c5">CRBufExample::~CRBufExample</a>()
<a name="l00059"></a>00059 {
<a name="l00060"></a>00060 <span class="keyword">delete</span> iConsole;
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062
<a name="l00067"></a><a class="code" href="class_c_r_buf_example.html#8bd254efe876ae2b5138f79a101272d6">00067</a> <a class="code" href="class_c_r_buf_example.html">CRBufExample</a>* <a class="code" href="class_c_r_buf_example.html#8bd254efe876ae2b5138f79a101272d6">CRBufExample::NewL</a>()
<a name="l00068"></a>00068 {
<a name="l00069"></a>00069 <a class="code" href="class_c_r_buf_example.html">CRBufExample</a>* <span class="keyword">self</span>=<span class="keyword">new</span>(ELeave)<a class="code" href="class_c_r_buf_example.html">CRBufExample</a>();
<a name="l00070"></a>00070 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00071"></a>00071 <span class="keyword">self</span>->ConstructL();
<a name="l00072"></a>00072 CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00073"></a>00073 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00074"></a>00074 }
<a name="l00075"></a>00075
<a name="l00079"></a><a class="code" href="class_c_r_buf_example.html#57856a0e131215ccef45af713f5aad5f">00079</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#57856a0e131215ccef45af713f5aad5f">CRBufExample::CreateRBufL</a>()
<a name="l00080"></a>00080 {
<a name="l00081"></a>00081 _LIT(KCreate,<span class="stringliteral">"\n Creating an RBuf: \n"</span>);
<a name="l00082"></a>00082
<a name="l00083"></a>00083 iConsole->Printf(KCreate);
<a name="l00084"></a>00084
<a name="l00085"></a>00085 RBuf buf;
<a name="l00086"></a>00086
<a name="l00087"></a>00087 <span class="comment">// Create an empty buffer descriptor</span>
<a name="l00088"></a>00088 <span class="comment">// The length is zero, the maxlength is 3</span>
<a name="l00089"></a>00089 User::LeaveIfError(buf.Create(<a class="code" href="rbufexample_8cpp.html#daf0758eb2344d655702792acb8ced64">KSmallBufSize</a>));
<a name="l00090"></a>00090 CleanupClosePushL(buf);
<a name="l00091"></a>00091
<a name="l00092"></a>00092 <span class="comment">// Get the length of the buffer</span>
<a name="l00093"></a>00093 TInt length= buf.Length();
<a name="l00094"></a>00094
<a name="l00095"></a>00095 <span class="comment">// Get the maximum length of the buffer</span>
<a name="l00096"></a>00096 TInt maxLength= buf.MaxLength();
<a name="l00097"></a>00097
<a name="l00098"></a>00098 <span class="comment">// Check that length and maximum length are not equal because it was created using Create()</span>
<a name="l00099"></a>00099 <span class="keywordflow">if</span>(length != maxLength)
<a name="l00100"></a>00100 {
<a name="l00101"></a>00101 _LIT(KUsingCreate, <span class="stringliteral">"\n Using Create() API: "</span>);
<a name="l00102"></a>00102 iConsole->Printf(KUsingCreate);
<a name="l00103"></a>00103 iConsole->Printf(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>, length);
<a name="l00104"></a>00104 iConsole->Printf(KMaxLength, maxLength);
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106 <span class="keywordflow">else</span>
<a name="l00107"></a>00107 {
<a name="l00108"></a>00108 iConsole->Printf(KFailed);
<a name="l00109"></a>00109 }
<a name="l00110"></a>00110
<a name="l00111"></a>00111 <span class="comment">// Deallocate the memory assigned to the buffer</span>
<a name="l00112"></a>00112 CleanupStack::PopAndDestroy(&buf);
<a name="l00113"></a>00113
<a name="l00114"></a>00114 <span class="comment">// Create buffer descriptor</span>
<a name="l00115"></a>00115 <span class="comment">// The length is 3, the maxlength is 3</span>
<a name="l00116"></a>00116 User::LeaveIfError(buf.CreateMax(<a class="code" href="rbufexample_8cpp.html#daf0758eb2344d655702792acb8ced64">KSmallBufSize</a>));
<a name="l00117"></a>00117 CleanupClosePushL(buf);
<a name="l00118"></a>00118
<a name="l00119"></a>00119 <span class="comment">// Get the length of the buffer.</span>
<a name="l00120"></a>00120 length= buf.Length();
<a name="l00121"></a>00121
<a name="l00122"></a>00122 <span class="comment">// Get the maximum length of the buffer.</span>
<a name="l00123"></a>00123 maxLength=buf.MaxLength();
<a name="l00124"></a>00124
<a name="l00125"></a>00125 <span class="comment">// Check that length and maximum length are equal because it was created using CreateMax().</span>
<a name="l00126"></a>00126 <span class="keywordflow">if</span>(length == maxLength)
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128 _LIT(KCreateInAdvance, <span class="stringliteral">"\n Using CreateMax() API: "</span>);
<a name="l00129"></a>00129 iConsole->Printf(KCreateInAdvance);
<a name="l00130"></a>00130 iConsole->Printf(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>, length);
<a name="l00131"></a>00131 iConsole->Printf(KMaxLength, maxLength);
<a name="l00132"></a>00132 }
<a name="l00133"></a>00133 <span class="keywordflow">else</span>
<a name="l00134"></a>00134 {
<a name="l00135"></a>00135 iConsole->Printf(KFailed);
<a name="l00136"></a>00136 }
<a name="l00137"></a>00137
<a name="l00138"></a>00138 <span class="comment">// Deallocate the memory assigned to the buffer.</span>
<a name="l00139"></a>00139 CleanupStack::PopAndDestroy(&buf);
<a name="l00140"></a>00140 }
<a name="l00141"></a>00141
<a name="l00145"></a><a class="code" href="class_c_r_buf_example.html#c33844cce7df0b1c2a57f6edc3ac0906">00145</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#c33844cce7df0b1c2a57f6edc3ac0906">CRBufExample::CreateRBufFromExistingDesL</a>()
<a name="l00146"></a>00146 {
<a name="l00147"></a>00147 RBuf buf;
<a name="l00148"></a>00148
<a name="l00149"></a>00149 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00150"></a>00150 buf.CreateL(KBuffer1);
<a name="l00151"></a>00151
<a name="l00152"></a>00152 _LIT(KCreateFromExistingDes, <span class="stringliteral">"\n From an existing descriptor "</span>);
<a name="l00153"></a>00153 iConsole->Printf(KCreateFromExistingDes);
<a name="l00154"></a>00154
<a name="l00155"></a>00155 <span class="comment">// Deallocates the memory assigned to the buffer</span>
<a name="l00156"></a>00156 buf.Close();
<a name="l00157"></a>00157 }
<a name="l00158"></a>00158
<a name="l00162"></a><a class="code" href="class_c_r_buf_example.html#398c08c23d056f1705f82ea5117c07e8">00162</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#398c08c23d056f1705f82ea5117c07e8">CRBufExample::CreateRBufFromHBufCL</a>()
<a name="l00163"></a>00163 {
<a name="l00164"></a>00164 <span class="comment">// Create a pointer to a heap descriptor</span>
<a name="l00165"></a>00165 HBufC* hptr = HBufC::NewL(KBuffer1().Length());
<a name="l00166"></a>00166
<a name="l00167"></a>00167 <span class="comment">// Assign some data to the heap descriptor.</span>
<a name="l00168"></a>00168 *hptr = KBuffer1;
<a name="l00169"></a>00169
<a name="l00170"></a>00170 RBuf buf;
<a name="l00171"></a>00171
<a name="l00172"></a>00172 <span class="comment">// Assign the HBufC to the buffer. This transfers ownership of the heap descriptor.</span>
<a name="l00173"></a>00173 buf.Assign(hptr);
<a name="l00174"></a>00174
<a name="l00175"></a>00175 _LIT(KCreateFromHBufC, <span class="stringliteral">"\n From HBufC "</span>);
<a name="l00176"></a>00176 iConsole->Printf(KCreateFromHBufC);
<a name="l00177"></a>00177
<a name="l00178"></a>00178 <span class="comment">// Deallocate the memory assigned to the buffer. </span>
<a name="l00179"></a>00179 <span class="comment">// There is no need to free the HBufC because Assign() transferred ownership.</span>
<a name="l00180"></a>00180 buf.Close();
<a name="l00181"></a>00181 }
<a name="l00182"></a>00182
<a name="l00186"></a><a class="code" href="class_c_r_buf_example.html#0a4263b7828a5214748786dae31e66b7">00186</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#0a4263b7828a5214748786dae31e66b7">CRBufExample::CreateRBufFromAnotherRBufL</a>()
<a name="l00187"></a>00187 {
<a name="l00188"></a>00188 RBuf buf;
<a name="l00189"></a>00189
<a name="l00190"></a>00190 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00191"></a>00191 User::LeaveIfError(buf.Create(KBuffer1));
<a name="l00192"></a>00192
<a name="l00193"></a>00193 RBuf targetBuf;
<a name="l00194"></a>00194
<a name="l00195"></a>00195 <span class="comment">// Assign one buffer to the other. </span>
<a name="l00196"></a>00196 <span class="comment">// Transfers ownership of the source buffer's memory to the target buffer.</span>
<a name="l00197"></a>00197 <span class="comment">// Note that targetBuf should have no memory allocated to it beforehand</span>
<a name="l00198"></a>00198 <span class="comment">// otherwise a memory leak will occur.</span>
<a name="l00199"></a>00199 targetBuf.Assign(buf);
<a name="l00200"></a>00200
<a name="l00201"></a>00201 _LIT(KCreateFromAnotherRBuf, <span class="stringliteral">"\n From another RBuf"</span>);
<a name="l00202"></a>00202 iConsole->Printf(KCreateFromAnotherRBuf);
<a name="l00203"></a>00203
<a name="l00204"></a>00204 <span class="comment">// Deallocate the memory assigned to the buffer.</span>
<a name="l00205"></a>00205 targetBuf.Close();
<a name="l00206"></a>00206 }
<a name="l00207"></a>00207
<a name="l00211"></a><a class="code" href="class_c_r_buf_example.html#15c0b84f3b6704d60b1e4f3ff50f590b">00211</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#15c0b84f3b6704d60b1e4f3ff50f590b">CRBufExample::CreateRBufUsingRReadStreamL</a>()
<a name="l00212"></a>00212 {
<a name="l00213"></a>00213 <span class="comment">// A handle to a file server session. </span>
<a name="l00214"></a>00214 <span class="comment">// This is used to create and write to/read from a file.</span>
<a name="l00215"></a>00215 RFs fs;
<a name="l00216"></a>00216
<a name="l00217"></a>00217 <span class="comment">// Connect to the file server.</span>
<a name="l00218"></a>00218 User::LeaveIfError(fs.Connect());
<a name="l00219"></a>00219 CleanupClosePushL(fs);
<a name="l00220"></a>00220 <span class="comment">// Create the session path at the process's private path on the system drive.</span>
<a name="l00221"></a>00221 User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
<a name="l00222"></a>00222
<a name="l00223"></a>00223 <span class="comment">// Set the session path to point to the process's private path on the system drive.</span>
<a name="l00224"></a>00224 User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
<a name="l00225"></a>00225
<a name="l00226"></a>00226 <span class="comment">// Declare a file stream to write to.</span>
<a name="l00227"></a>00227 RFileWriteStream wStream;
<a name="l00228"></a>00228
<a name="l00229"></a>00229 _LIT(KFileName,<span class="stringliteral">"stream.dat"</span>);
<a name="l00230"></a>00230
<a name="l00231"></a>00231 <span class="comment">// Create a file, associates it with the stream, and prepares the stream for writing.</span>
<a name="l00232"></a>00232 User::LeaveIfError(wStream.Replace(fs, KFileName, EFileWrite));
<a name="l00233"></a>00233 CleanupClosePushL(wStream);
<a name="l00234"></a>00234
<a name="l00235"></a>00235 _LIT(KText, <span class="stringliteral">"RBuf Example"</span>);
<a name="l00236"></a>00236
<a name="l00237"></a>00237 <span class="comment">// Write some text to the stream.</span>
<a name="l00238"></a>00238 wStream << KText();
<a name="l00239"></a>00239 wStream.CommitL();
<a name="l00240"></a>00240 CleanupStack::PopAndDestroy(&wStream);
<a name="l00241"></a>00241
<a name="l00242"></a>00242 <span class="comment">// Declare a file stream to read from.</span>
<a name="l00243"></a>00243 RFileReadStream rStream;
<a name="l00244"></a>00244
<a name="l00245"></a>00245 <span class="comment">// Open the file</span>
<a name="l00246"></a>00246 User::LeaveIfError(rStream.Open(fs, KFileName, EFileRead));
<a name="l00247"></a>00247 CleanupClosePushL(rStream);
<a name="l00248"></a>00248
<a name="l00249"></a>00249 RBuf buf;
<a name="l00250"></a>00250 CleanupClosePushL(buf);
<a name="l00251"></a>00251
<a name="l00252"></a>00252 <span class="comment">// Create the buffer by passing the open stream object to CreateL().</span>
<a name="l00253"></a>00253 buf.CreateL(rStream, <a class="code" href="rbufexample_8cpp.html#aa9f486ebd20badddb8451883a82e763">KLargeBufSize</a>);
<a name="l00254"></a>00254
<a name="l00255"></a>00255 _LIT(KCreateUsingRReadStream, <span class="stringliteral">"\n Using RReadStream "</span>);
<a name="l00256"></a>00256 iConsole->Printf(KCreateUsingRReadStream);
<a name="l00257"></a>00257
<a name="l00258"></a>00258 <span class="comment">// Deallocate the memory assigned to the buffer, close the stream and close the file server connection</span>
<a name="l00259"></a>00259 CleanupStack::PopAndDestroy(&buf);
<a name="l00260"></a>00260 CleanupStack::PopAndDestroy(&rStream);
<a name="l00261"></a>00261 CleanupStack::PopAndDestroy(&fs);
<a name="l00262"></a>00262 }
<a name="l00263"></a>00263
<a name="l00267"></a><a class="code" href="class_c_r_buf_example.html#7815a44916e13bd2a567a6a25a1ffc6d">00267</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#7815a44916e13bd2a567a6a25a1ffc6d">CRBufExample::CreateRBufFromAllocatedMemoryL</a>()
<a name="l00268"></a>00268 {
<a name="l00269"></a>00269 RBuf buf;
<a name="l00270"></a>00270
<a name="l00271"></a>00271 <span class="comment">// Allocate some memory.</span>
<a name="l00272"></a>00272 TUint16* allocatedMemory = <span class="keyword">static_cast<</span>TUint16*<span class="keyword">></span>( User::Alloc( <a class="code" href="rbufexample_8cpp.html#aa9f486ebd20badddb8451883a82e763">KLargeBufSize</a> * <span class="keyword">sizeof</span>( TUint16) ) ) ;
<a name="l00273"></a>00273
<a name="l00274"></a>00274 <span class="comment">// Transfer ownership of the memory to the descriptor.</span>
<a name="l00275"></a>00275 buf.Assign(allocatedMemory, <a class="code" href="rbufexample_8cpp.html#aa9f486ebd20badddb8451883a82e763">KLargeBufSize</a>);
<a name="l00276"></a>00276
<a name="l00277"></a>00277 _LIT(KCreateFromAllocatedMemory, <span class="stringliteral">"\n By transferring ownership of allocated memory"</span>);
<a name="l00278"></a>00278 iConsole->Printf(KCreateFromAllocatedMemory);
<a name="l00279"></a>00279
<a name="l00280"></a>00280 iConsole->Printf(KPressAKey);
<a name="l00281"></a>00281 iConsole->Getch();
<a name="l00282"></a>00282
<a name="l00283"></a>00283 <span class="comment">// Deallocate the memory assigned to the buffer.</span>
<a name="l00284"></a>00284 <span class="comment">// There is no need to free memory here because Assign() transferred ownership.</span>
<a name="l00285"></a>00285 buf.Close();
<a name="l00286"></a>00286 }
<a name="l00287"></a>00287
<a name="l00291"></a><a class="code" href="class_c_r_buf_example.html#6de7e813c107211fc904905e39582ee0">00291</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#6de7e813c107211fc904905e39582ee0">CRBufExample::SwapTwoRBufsL</a>()
<a name="l00292"></a>00292 {
<a name="l00293"></a>00293 _LIT(KSwapAndCopy,<span class="stringliteral">"\n Swapping and copying data: "</span>);
<a name="l00294"></a>00294 iConsole->Printf(KSwapAndCopy);
<a name="l00295"></a>00295
<a name="l00296"></a>00296 RBuf buf1;
<a name="l00297"></a>00297
<a name="l00298"></a>00298 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00299"></a>00299 User::LeaveIfError(buf1.Create(KBuffer1));
<a name="l00300"></a>00300 CleanupClosePushL(buf1);
<a name="l00301"></a>00301
<a name="l00302"></a>00302 RBuf buf2;
<a name="l00303"></a>00303
<a name="l00304"></a>00304 <span class="comment">// Create a second buffer descriptor, initialised with some text</span>
<a name="l00305"></a>00305 User::LeaveIfError(buf2.Create(KBuffer2));
<a name="l00306"></a>00306 CleanupClosePushL(buf2);
<a name="l00307"></a>00307
<a name="l00308"></a>00308 _LIT(KBeforeSwapping, <span class="stringliteral">"\n Before swapping: "</span>);
<a name="l00309"></a>00309 iConsole->Printf(KBeforeSwapping);
<a name="l00310"></a>00310
<a name="l00311"></a>00311 <span class="comment">// Print out the contents of the 2 descriptors</span>
<a name="l00312"></a>00312 _LIT(KPrintFirstdata,<span class="stringliteral">"\n Data present in first descriptor is: "</span>);
<a name="l00313"></a>00313 iConsole->Printf(KPrintFirstdata);
<a name="l00314"></a>00314 iConsole->Printf(buf1);
<a name="l00315"></a>00315
<a name="l00316"></a>00316 _LIT(KPrintSecondData,<span class="stringliteral">"\n Data present in second descriptor is: "</span>);
<a name="l00317"></a>00317 iConsole->Printf(KPrintSecondData);
<a name="l00318"></a>00318 iConsole->Printf(buf2);
<a name="l00319"></a>00319
<a name="l00320"></a>00320 <span class="comment">// Swap them and print out the new contents</span>
<a name="l00321"></a>00321 buf1.Swap(buf2);
<a name="l00322"></a>00322
<a name="l00323"></a>00323 _LIT(KAfterSwapping, <span class="stringliteral">"\n After swapping: "</span>);
<a name="l00324"></a>00324 iConsole->Printf(KAfterSwapping);
<a name="l00325"></a>00325
<a name="l00326"></a>00326 iConsole->Printf(KPrintFirstdata);
<a name="l00327"></a>00327 iConsole->Printf(buf1);
<a name="l00328"></a>00328
<a name="l00329"></a>00329 iConsole->Printf(KPrintSecondData);
<a name="l00330"></a>00330 iConsole->Printf(buf2);
<a name="l00331"></a>00331
<a name="l00332"></a>00332 _LIT(KSwap, <span class="stringliteral">"\n Swapping between two RBufs is successful"</span>);
<a name="l00333"></a>00333 iConsole->Printf(KSwap);
<a name="l00334"></a>00334
<a name="l00335"></a>00335 <span class="comment">// Deallocate memory assigned to the 2 buffer descriptors.</span>
<a name="l00336"></a>00336 CleanupStack::PopAndDestroy(2); <span class="comment">// buf1, buf2</span>
<a name="l00337"></a>00337 }
<a name="l00338"></a>00338
<a name="l00342"></a><a class="code" href="class_c_r_buf_example.html#fd4b2bcfb6b0a1fbef134f021ee7bcac">00342</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#fd4b2bcfb6b0a1fbef134f021ee7bcac">CRBufExample::CopyDataUsingAssignmentOperatorL</a>()
<a name="l00343"></a>00343 {
<a name="l00344"></a>00344 RBuf buf1;
<a name="l00345"></a>00345
<a name="l00346"></a>00346 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00347"></a>00347 buf1.Create(KBuffer1);
<a name="l00348"></a>00348 CleanupClosePushL(buf1);
<a name="l00349"></a>00349
<a name="l00350"></a>00350 RBuf buf2;
<a name="l00351"></a>00351
<a name="l00352"></a>00352 <span class="comment">// Create a second buffer descriptor, initialised with some text</span>
<a name="l00353"></a>00353 User::LeaveIfError(buf2.Create(KBuffer2));
<a name="l00354"></a>00354 CleanupClosePushL(buf2);
<a name="l00355"></a>00355
<a name="l00356"></a>00356 <span class="comment">// Copy data using assignment operator from one to the other. </span>
<a name="l00357"></a>00357 <span class="comment">// Target buffer must be long enough, otherwise a panic occurs.</span>
<a name="l00358"></a>00358 buf2= buf1;
<a name="l00359"></a>00359
<a name="l00360"></a>00360 <span class="comment">// Check the value of the buffers .</span>
<a name="l00361"></a>00361 <span class="keywordflow">if</span> (buf1==buf2)
<a name="l00362"></a>00362 {
<a name="l00363"></a>00363 _LIT(KCopyDataUsingAssignmentOperator,<span class="stringliteral">"\n Copying data using assignment operator from descriptor, and RBuf is successful"</span>);
<a name="l00364"></a>00364 iConsole->Printf(KCopyDataUsingAssignmentOperator);
<a name="l00365"></a>00365 }
<a name="l00366"></a>00366 <span class="keywordflow">else</span>
<a name="l00367"></a>00367 {
<a name="l00368"></a>00368 iConsole->Printf(KFailed);
<a name="l00369"></a>00369 }
<a name="l00370"></a>00370
<a name="l00371"></a>00371 iConsole->Printf(KPressAKey);
<a name="l00372"></a>00372 iConsole->Getch();
<a name="l00373"></a>00373
<a name="l00374"></a>00374 <span class="comment">// Deallocate memory assigned to the 2 buffer descriptors.</span>
<a name="l00375"></a>00375 CleanupStack::PopAndDestroy(2); <span class="comment">// buf1, buf2</span>
<a name="l00376"></a>00376 }
<a name="l00377"></a>00377
<a name="l00381"></a><a class="code" href="class_c_r_buf_example.html#da558a684cebf4b9034706007ad7bda0">00381</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#da558a684cebf4b9034706007ad7bda0">CRBufExample::ReallocateAndFreeTheMemoryBufferL</a>()
<a name="l00382"></a>00382 {
<a name="l00383"></a>00383 _LIT(KReAllocAndFree,<span class="stringliteral">"\n Realloc and free:"</span>);
<a name="l00384"></a>00384 iConsole->Printf(KReAllocAndFree);
<a name="l00385"></a>00385
<a name="l00386"></a>00386 RBuf buf;
<a name="l00387"></a>00387
<a name="l00388"></a>00388 <span class="comment">// Create empty buffer descriptor with max length of KSmallBufSize.</span>
<a name="l00389"></a>00389 User::LeaveIfError(buf.Create(<a class="code" href="rbufexample_8cpp.html#daf0758eb2344d655702792acb8ced64">KSmallBufSize</a>));
<a name="l00390"></a>00390
<a name="l00391"></a>00391 <span class="comment">// Get the length of the buffer.</span>
<a name="l00392"></a>00392 TInt length= buf.Length();
<a name="l00393"></a>00393
<a name="l00394"></a>00394 <span class="comment">// Get the maximum length of the buffer.</span>
<a name="l00395"></a>00395 TInt maxLength= buf.MaxLength();
<a name="l00396"></a>00396
<a name="l00397"></a>00397 _LIT(KBeforeReAlloc,<span class="stringliteral">"\n Before ReAlloc: "</span>);
<a name="l00398"></a>00398 iConsole->Printf(KBeforeReAlloc);
<a name="l00399"></a>00399
<a name="l00400"></a>00400 <span class="comment">// Print out the length and max length</span>
<a name="l00401"></a>00401 iConsole->Printf(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>, length);
<a name="l00402"></a>00402 iConsole->Printf(KMaxLength, maxLength);
<a name="l00403"></a>00403
<a name="l00404"></a>00404 <span class="comment">// Re-allocate the buffer descriptor. Length doesnt change but maxlength does.</span>
<a name="l00405"></a>00405 User::LeaveIfError(buf.ReAlloc(<a class="code" href="rbufexample_8cpp.html#9b8374bb15a2980f3b92ac9a002e8cf5">KMedBufSize</a>));
<a name="l00406"></a>00406
<a name="l00407"></a>00407 <span class="comment">// Get the length of the buffer.</span>
<a name="l00408"></a>00408 length= buf.Length();
<a name="l00409"></a>00409
<a name="l00410"></a>00410 <span class="comment">// Get the maximum length of the reallocated buffer.</span>
<a name="l00411"></a>00411 maxLength= buf.MaxLength();
<a name="l00412"></a>00412
<a name="l00413"></a>00413 _LIT(KAfterReAlloc,<span class="stringliteral">"\n After ReAlloc: "</span>);
<a name="l00414"></a>00414 iConsole->Printf(KAfterReAlloc);
<a name="l00415"></a>00415
<a name="l00416"></a>00416 <span class="comment">// Print out the length and new max length</span>
<a name="l00417"></a>00417 iConsole->Printf(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>, length);
<a name="l00418"></a>00418 iConsole->Printf(KMaxLength, maxLength);
<a name="l00419"></a>00419
<a name="l00420"></a>00420 <span class="comment">// Free the memory buffer. It sets the length and maxlength to zero.</span>
<a name="l00421"></a>00421 User::LeaveIfError(buf.ReAlloc(0));
<a name="l00422"></a>00422
<a name="l00423"></a>00423 <span class="comment">// Get the length of the buffer.</span>
<a name="l00424"></a>00424 length= buf.Length();
<a name="l00425"></a>00425
<a name="l00426"></a>00426 <span class="comment">// Get the maximum length of the buffer.</span>
<a name="l00427"></a>00427 maxLength= buf.MaxLength();
<a name="l00428"></a>00428
<a name="l00429"></a>00429 _LIT(KFreeBuffer,<span class="stringliteral">"\n After free: "</span>);
<a name="l00430"></a>00430 iConsole->Printf(KFreeBuffer);
<a name="l00431"></a>00431
<a name="l00432"></a>00432 iConsole->Printf(<a class="code" href="localeupdate_8h.html#c930ea536278a3ea6eca1814afcd7897">KLength</a>, length);
<a name="l00433"></a>00433 iConsole->Printf(KMaxLength, maxLength);
<a name="l00434"></a>00434 }
<a name="l00435"></a>00435
<a name="l00439"></a><a class="code" href="class_c_r_buf_example.html#9ee071c3749b927f6af99dcb04c6bd4d">00439</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#9ee071c3749b927f6af99dcb04c6bd4d">CRBufExample::ReplaceAndModifyTheDataL</a>()
<a name="l00440"></a>00440 {
<a name="l00441"></a>00441 RBuf buf;
<a name="l00442"></a>00442
<a name="l00443"></a>00443 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00444"></a>00444 User::LeaveIfError(buf.Create(KBuffer1));
<a name="l00445"></a>00445 CleanupClosePushL(buf);
<a name="l00446"></a>00446
<a name="l00447"></a>00447 _LIT(KReplaceAndModify,<span class="stringliteral">"\n Replace and modify: "</span>);
<a name="l00448"></a>00448 iConsole->Printf(KReplaceAndModify);
<a name="l00449"></a>00449
<a name="l00450"></a>00450 <span class="comment">// Print the original data present in the buffer.</span>
<a name="l00451"></a>00451 _LIT(KOriginalData,<span class="stringliteral">"\n Data present in RBuf is: "</span>);
<a name="l00452"></a>00452 iConsole->Printf(KOriginalData);
<a name="l00453"></a>00453 iConsole->Printf(buf);
<a name="l00454"></a>00454
<a name="l00455"></a>00455 TInt pos= 1;
<a name="l00456"></a>00456
<a name="l00457"></a>00457 _LIT(KReplacedMessage,<span class="stringliteral">"22"</span>);
<a name="l00458"></a>00458
<a name="l00459"></a>00459 TInt length= KReplacedMessage().Length();
<a name="l00460"></a>00460
<a name="l00461"></a>00461 <span class="comment">// Replace a portion of the data in the buffer descriptor.</span>
<a name="l00462"></a>00462 buf.Replace(pos,length, KReplacedMessage);
<a name="l00463"></a>00463
<a name="l00464"></a>00464 <span class="comment">// Print the buffer's new contents.</span>
<a name="l00465"></a>00465 _LIT(KReplacedData,<span class="stringliteral">"\n After replacement, data held in RBuf is: "</span>);
<a name="l00466"></a>00466 iConsole->Printf(KReplacedData);
<a name="l00467"></a>00467 iConsole->Printf(buf);
<a name="l00468"></a>00468
<a name="l00469"></a>00469 pos=3;
<a name="l00470"></a>00470 length=1;
<a name="l00471"></a>00471
<a name="l00472"></a>00472 <span class="comment">// Delete the replacement text.</span>
<a name="l00473"></a>00473 buf.Delete(pos,length);
<a name="l00474"></a>00474
<a name="l00475"></a>00475 <span class="comment">// Print the buffer's contents again.</span>
<a name="l00476"></a>00476 _LIT(KModifiedData,<span class="stringliteral">"\n After modification, data held in RBuf is: "</span>);
<a name="l00477"></a>00477 iConsole->Printf(KModifiedData);
<a name="l00478"></a>00478 iConsole->Printf(buf);
<a name="l00479"></a>00479
<a name="l00480"></a>00480 _LIT(KReplaceAndModifyData,<span class="stringliteral">"\n Replacing and modifying the data held in RBuf is successful"</span>);
<a name="l00481"></a>00481 iConsole->Printf(KReplaceAndModifyData);
<a name="l00482"></a>00482
<a name="l00483"></a>00483 <span class="comment">// Deallocate the memory assigned to the buffer. </span>
<a name="l00484"></a>00484 CleanupStack::PopAndDestroy(&buf);
<a name="l00485"></a>00485 }
<a name="l00486"></a>00486
<a name="l00490"></a><a class="code" href="class_c_r_buf_example.html#1d99cb2bef146c76bd1f99908435ecfb">00490</a> <span class="keywordtype">void</span> <a class="code" href="class_c_r_buf_example.html#1d99cb2bef146c76bd1f99908435ecfb">CRBufExample::CleanUpRulesL</a>()
<a name="l00491"></a>00491 {
<a name="l00492"></a>00492 RBuf buf;
<a name="l00493"></a>00493
<a name="l00494"></a>00494 <span class="comment">// Create a buffer descriptor, initialised with some text</span>
<a name="l00495"></a>00495 User::LeaveIfError(buf.Create(KBuffer1));
<a name="l00496"></a>00496
<a name="l00497"></a>00497 <span class="comment">// To avoid memory leaks use RBuf::CleanupClosePushL() to push a cleanup item for the RBuf onto the cleanup stack.</span>
<a name="l00498"></a>00498 <span class="comment">// The effect is to cause Close() to be called on the buffer descriptor if a leave occurs, </span>
<a name="l00499"></a>00499 <span class="comment">// or when CleanupStack::PopAndDestroy() is called.</span>
<a name="l00500"></a>00500 buf.CleanupClosePushL();
<a name="l00501"></a>00501
<a name="l00502"></a>00502 <span class="comment">// Causes Close() to be called to deallocate the memory assigned to the buffer.</span>
<a name="l00503"></a>00503 CleanupStack::PopAndDestroy(&buf);
<a name="l00504"></a>00504
<a name="l00505"></a>00505 _LIT(KCleanUp,<span class="stringliteral">"\n RBuf cleanup is successful"</span>);
<a name="l00506"></a>00506 iConsole->Printf(KCleanUp);
<a name="l00507"></a>00507
<a name="l00508"></a>00508 _LIT(KExitMsg, <span class="stringliteral">"\n\n Press any key to exit the example"</span>);
<a name="l00509"></a>00509 iConsole->Printf(KExitMsg);
<a name="l00510"></a>00510 iConsole->Getch();
<a name="l00511"></a>00511 }
<a name="l00512"></a>00512
<a name="l00513"></a><a class="code" href="rbufexample_8cpp.html#5fb473d4ee18739183215b04dcad6e12">00513</a> <span class="keywordtype">void</span> <a class="code" href="circularbuffer_8cpp.html#5fb473d4ee18739183215b04dcad6e12">MainL</a>()
<a name="l00514"></a>00514 {
<a name="l00515"></a>00515 <a class="code" href="class_c_r_buf_example.html">CRBufExample</a>* app= <a class="code" href="class_c_r_buf_example.html#8bd254efe876ae2b5138f79a101272d6">CRBufExample::NewL</a>();
<a name="l00516"></a>00516 CleanupStack::PushL(app);
<a name="l00517"></a>00517
<a name="l00518"></a>00518 <span class="comment">// Creates empty RBuf.</span>
<a name="l00519"></a>00519 app-><a class="code" href="class_c_r_buf_example.html#57856a0e131215ccef45af713f5aad5f">CreateRBufL</a>();
<a name="l00520"></a>00520
<a name="l00521"></a>00521 <span class="comment">// Creates RBuf from an existing descriptor.</span>
<a name="l00522"></a>00522 app-><a class="code" href="class_c_r_buf_example.html#c33844cce7df0b1c2a57f6edc3ac0906">CreateRBufFromExistingDesL</a>();
<a name="l00523"></a>00523
<a name="l00524"></a>00524 <span class="comment">// Creates RBuf from an HBufC.</span>
<a name="l00525"></a>00525 app-><a class="code" href="class_c_r_buf_example.html#398c08c23d056f1705f82ea5117c07e8">CreateRBufFromHBufCL</a>();
<a name="l00526"></a>00526
<a name="l00527"></a>00527 <span class="comment">// Creates RBuf from another RBuf.</span>
<a name="l00528"></a>00528 app-><a class="code" href="class_c_r_buf_example.html#0a4263b7828a5214748786dae31e66b7">CreateRBufFromAnotherRBufL</a>();
<a name="l00529"></a>00529
<a name="l00530"></a>00530 <span class="comment">// Create RBuf using RReadStream</span>
<a name="l00531"></a>00531 app-><a class="code" href="class_c_r_buf_example.html#15c0b84f3b6704d60b1e4f3ff50f590b">CreateRBufUsingRReadStreamL</a>();
<a name="l00532"></a>00532
<a name="l00533"></a>00533 <span class="comment">// Creates RBuf from allocated memory.</span>
<a name="l00534"></a>00534 app-><a class="code" href="class_c_r_buf_example.html#7815a44916e13bd2a567a6a25a1ffc6d">CreateRBufFromAllocatedMemoryL</a>();
<a name="l00535"></a>00535
<a name="l00536"></a>00536 <span class="comment">// Swaps two RBufs.</span>
<a name="l00537"></a>00537 app-><a class="code" href="class_c_r_buf_example.html#6de7e813c107211fc904905e39582ee0">SwapTwoRBufsL</a>();
<a name="l00538"></a>00538
<a name="l00539"></a>00539 <span class="comment">// Copies data using assignment operator.</span>
<a name="l00540"></a>00540 app-><a class="code" href="class_c_r_buf_example.html#fd4b2bcfb6b0a1fbef134f021ee7bcac">CopyDataUsingAssignmentOperatorL</a>();
<a name="l00541"></a>00541
<a name="l00542"></a>00542 <span class="comment">// Reallocates and frees the memory buffer.</span>
<a name="l00543"></a>00543 app-><a class="code" href="class_c_r_buf_example.html#da558a684cebf4b9034706007ad7bda0">ReallocateAndFreeTheMemoryBufferL</a>();
<a name="l00544"></a>00544
<a name="l00545"></a>00545 <span class="comment">// Replaces and modifies the data in the descriptor.</span>
<a name="l00546"></a>00546 app-><a class="code" href="class_c_r_buf_example.html#9ee071c3749b927f6af99dcb04c6bd4d">ReplaceAndModifyTheDataL</a>();
<a name="l00547"></a>00547
<a name="l00548"></a>00548 <span class="comment">// Demonstrates cleanup.</span>
<a name="l00549"></a>00549 app-><a class="code" href="class_c_r_buf_example.html#1d99cb2bef146c76bd1f99908435ecfb">CleanUpRulesL</a>();
<a name="l00550"></a>00550
<a name="l00551"></a>00551 CleanupStack::PopAndDestroy(app);
<a name="l00552"></a>00552 }
<a name="l00553"></a>00553
<a name="l00554"></a><a class="code" href="rbufexample_8cpp.html#0f358e9c4355138f629b8c4f37310295">00554</a> GLDEF_C TInt <a class="code" href="dbllist_8cpp.html#0f358e9c4355138f629b8c4f37310295">E32Main</a>()
<a name="l00555"></a>00555 {
<a name="l00556"></a>00556 __UHEAP_MARK;
<a name="l00557"></a>00557
<a name="l00558"></a>00558 CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00559"></a>00559 <span class="keywordflow">if</span>(cleanup == NULL)
<a name="l00560"></a>00560 {
<a name="l00561"></a>00561 <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00562"></a>00562 }
<a name="l00563"></a>00563 TRAPD(err, <a class="code" href="circularbuffer_8cpp.html#5fb473d4ee18739183215b04dcad6e12">MainL</a>());
<a name="l00564"></a>00564 <span class="keyword">delete</span> cleanup;
<a name="l00565"></a>00565
<a name="l00566"></a>00566 <span class="keywordflow">if</span>(err !=KErrNone)
<a name="l00567"></a>00567 {
<a name="l00568"></a>00568 User::Panic(KFailed, err);
<a name="l00569"></a>00569 }
<a name="l00570"></a>00570
<a name="l00571"></a>00571 __UHEAP_MARKEND;
<a name="l00572"></a>00572 <span class="keywordflow">return</span> KErrNone;
<a name="l00573"></a>00573 }
<a name="l00574"></a>00574
<a name="l00575"></a>00575
<a name="l00576"></a>00576
<a name="l00577"></a>00577
<a name="l00578"></a>00578
<a name="l00579"></a>00579
<a name="l00580"></a>00580
</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
</body>
</html>