diff -r f345bda72bc4 -r 43e37759235e Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/rbufexample_8cpp-source.html --- a/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/rbufexample_8cpp-source.html Tue Mar 30 11:56:28 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,546 +0,0 @@ - - -TB10.1 Example Applications: examples/Base/BufsAndStrings/rbufexample/rbufexample.cpp Source File - - - - -

examples/Base/BufsAndStrings/rbufexample/rbufexample.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
-00002 // All rights reserved.
-00003 // This component and the accompanying materials are made available
-00004 // under the terms of "Eclipse Public License v1.0"
-00005 // which accompanies this distribution, and is available
-00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
-00007 //
-00008 // Initial Contributors:
-00009 // Nokia Corporation - initial contribution.
-00010 //
-00011 // Contributors:
-00012 //
-00013 // Description:
-00014 // This example program demonstrates how to use the resizable buffer descriptor class RBuf.
-00015 //
-00016 
-00017 
-00018 
-00022 #include "rbufexample.h"
-00023 
-00024 const TInt KLargeBufSize = 15;
-00025 const TInt KSmallBufSize = 3;
-00026 const TInt KMedBufSize = 7;
-00027 
-00028 _LIT(KFailed, "\n Error occurred");
-00029 _LIT(KPressAKey, "\n\n Press any key to continue the example\n");
-00030 _LIT(KLength,"\n  Length= %d");
-00031 _LIT(KMaxLength,"\n  MaxLength= %d");
-00032 _LIT(KBuffer1,"11111");
-00033 _LIT(KBuffer2,"22222");
-00034 
-00038 CRBufExample::CRBufExample()
-00039         {
-00040         }
-00041 
-00042 void CRBufExample::ConstructL()
-00043         {
-00044         _LIT(KTitle," RBuf Example");
-00045         iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
-00046         
-00047         _LIT(KWelcome," Welcome to the RBuf example application");
-00048         iConsole->Printf(KWelcome);
-00049         
-00050         _LIT(KPressAKeyMsg, "\n\n Press any key to step through the example\n");
-00051         iConsole->Printf(KPressAKeyMsg );
-00052         iConsole->Getch();
-00053         }
-00054 
-00058 CRBufExample::~CRBufExample()
-00059         {
-00060         delete iConsole;
-00061         }
-00062 
-00067 CRBufExample* CRBufExample::NewL()
-00068         {
-00069         CRBufExample* self=new(ELeave)CRBufExample();
-00070         CleanupStack::PushL(self);
-00071         self->ConstructL();
-00072         CleanupStack::Pop(self);
-00073         return self;
-00074         }
-00075     
-00079 void CRBufExample::CreateRBufL()
-00080         {
-00081         _LIT(KCreate,"\n Creating an RBuf: \n");
-00082 
-00083         iConsole->Printf(KCreate);
-00084         
-00085         RBuf buf;
-00086         
-00087         // Create an empty buffer descriptor
-00088         // The length is zero, the maxlength is 3
-00089         User::LeaveIfError(buf.Create(KSmallBufSize));
-00090         CleanupClosePushL(buf);
-00091         
-00092         // Get the length of the buffer
-00093         TInt length= buf.Length();
-00094         
-00095         // Get the maximum length of the buffer
-00096         TInt maxLength= buf.MaxLength();
-00097         
-00098         // Check that length and maximum length are not equal because it was created using Create()
-00099         if(length != maxLength)
-00100                 {
-00101                 _LIT(KUsingCreate, "\n Using Create() API: ");
-00102                 iConsole->Printf(KUsingCreate);
-00103                 iConsole->Printf(KLength, length);
-00104                 iConsole->Printf(KMaxLength, maxLength);
-00105                 }
-00106         else
-00107                 {
-00108                 iConsole->Printf(KFailed);
-00109                 }       
-00110 
-00111         // Deallocate the memory assigned to the buffer
-00112         CleanupStack::PopAndDestroy(&buf);
-00113         
-00114         // Create buffer descriptor
-00115         // The length is 3, the maxlength is 3
-00116         User::LeaveIfError(buf.CreateMax(KSmallBufSize));
-00117         CleanupClosePushL(buf);
-00118         
-00119         // Get the length of the buffer.
-00120         length= buf.Length();
-00121         
-00122         // Get the maximum length of the buffer.
-00123         maxLength=buf.MaxLength();
-00124         
-00125         // Check that length and maximum length are equal because it was created using CreateMax().
-00126         if(length == maxLength)
-00127                 {
-00128                 _LIT(KCreateInAdvance, "\n Using CreateMax() API: ");
-00129                 iConsole->Printf(KCreateInAdvance);     
-00130                 iConsole->Printf(KLength, length);
-00131                 iConsole->Printf(KMaxLength, maxLength);
-00132                 }
-00133         else
-00134                 {
-00135                 iConsole->Printf(KFailed);
-00136                 }       
-00137         
-00138         // Deallocate the memory assigned to the buffer.
-00139         CleanupStack::PopAndDestroy(&buf);
-00140         } 
-00141         
-00145 void CRBufExample::CreateRBufFromExistingDesL()
-00146         {
-00147         RBuf buf;
-00148         
-00149         // Create a buffer descriptor, initialised with some text
-00150         buf.CreateL(KBuffer1);
-00151         
-00152         _LIT(KCreateFromExistingDes, "\n From an existing descriptor ");
-00153         iConsole->Printf(KCreateFromExistingDes);
-00154 
-00155         // Deallocates the memory assigned to the buffer
-00156         buf.Close();
-00157         } 
-00158         
-00162 void CRBufExample::CreateRBufFromHBufCL()
-00163         {
-00164         // Create a pointer to a heap descriptor
-00165         HBufC* hptr = HBufC::NewL(KBuffer1().Length());
-00166         
-00167         // Assign some data to the heap descriptor.
-00168         *hptr = KBuffer1; 
-00169         
-00170         RBuf buf;
-00171         
-00172         // Assign the HBufC to the buffer. This transfers ownership of the heap descriptor.
-00173         buf.Assign(hptr);
-00174         
-00175         _LIT(KCreateFromHBufC, "\n From HBufC ");
-00176         iConsole->Printf(KCreateFromHBufC);
-00177         
-00178         // Deallocate the memory assigned to the buffer. 
-00179         // There is no need to free the HBufC because Assign() transferred ownership.
-00180         buf.Close(); 
-00181         }
-00182         
-00186 void CRBufExample::CreateRBufFromAnotherRBufL()
-00187         {
-00188         RBuf buf;
-00189         
-00190         // Create a buffer descriptor, initialised with some text
-00191         User::LeaveIfError(buf.Create(KBuffer1));
-00192                 
-00193         RBuf targetBuf;
-00194         
-00195         // Assign one buffer to the other. 
-00196         // Transfers ownership of the source buffer's memory to the target buffer.
-00197         // Note that targetBuf should have no memory allocated to it beforehand
-00198         // otherwise a memory leak will occur.
-00199         targetBuf.Assign(buf);
-00200         
-00201         _LIT(KCreateFromAnotherRBuf, "\n From another RBuf");
-00202         iConsole->Printf(KCreateFromAnotherRBuf);
-00203 
-00204         // Deallocate the memory assigned to the buffer.
-00205         targetBuf.Close();
-00206         }
-00207                 
-00211 void CRBufExample::CreateRBufUsingRReadStreamL()
-00212         {
-00213         // A handle to a file server session. 
-00214         // This is used to create and write to/read from a file.
-00215         RFs fs;
-00216         
-00217         // Connect to the file server.
-00218         User::LeaveIfError(fs.Connect());
-00219         CleanupClosePushL(fs);
-00220         // Create the session path at the process's private path on the system drive.
-00221         User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
-00222 
-00223         // Set the session path to point to the process's private path on the system drive.
-00224         User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
-00225         
-00226         // Declare a file stream to write to.
-00227         RFileWriteStream wStream;
-00228         
-00229         _LIT(KFileName,"stream.dat");
-00230         
-00231         // Create a file, associates it with the stream, and prepares the stream for writing.
-00232         User::LeaveIfError(wStream.Replace(fs, KFileName, EFileWrite));
-00233         CleanupClosePushL(wStream);
-00234         
-00235         _LIT(KText, "RBuf Example");
-00236 
-00237         // Write some text to the stream.
-00238         wStream << KText();
-00239         wStream.CommitL();
-00240         CleanupStack::PopAndDestroy(&wStream);
-00241         
-00242         // Declare a file stream to read from.
-00243         RFileReadStream rStream;
-00244         
-00245         // Open the file
-00246         User::LeaveIfError(rStream.Open(fs, KFileName, EFileRead));
-00247         CleanupClosePushL(rStream);
-00248         
-00249         RBuf buf;
-00250         CleanupClosePushL(buf);
-00251         
-00252         // Create the buffer by passing the open stream object to CreateL().
-00253         buf.CreateL(rStream, KLargeBufSize);
-00254         
-00255         _LIT(KCreateUsingRReadStream, "\n Using RReadStream ");
-00256         iConsole->Printf(KCreateUsingRReadStream);
-00257         
-00258         // Deallocate the memory assigned to the buffer, close the stream and close the file server connection
-00259         CleanupStack::PopAndDestroy(&buf);
-00260         CleanupStack::PopAndDestroy(&rStream);
-00261         CleanupStack::PopAndDestroy(&fs);
-00262         }
-00263 
-00267 void CRBufExample::CreateRBufFromAllocatedMemoryL()
-00268         {
-00269         RBuf buf;
-00270         
-00271         // Allocate some memory.
-00272         TUint16* allocatedMemory = static_cast<TUint16*>( User::Alloc( KLargeBufSize * sizeof( TUint16) ) ) ;
-00273 
-00274         // Transfer ownership of the memory to the descriptor.
-00275         buf.Assign(allocatedMemory, KLargeBufSize);
-00276         
-00277         _LIT(KCreateFromAllocatedMemory, "\n By transferring ownership of allocated memory");
-00278         iConsole->Printf(KCreateFromAllocatedMemory);
-00279         
-00280         iConsole->Printf(KPressAKey);
-00281         iConsole->Getch();
-00282         
-00283         // Deallocate the memory assigned to the buffer.
-00284         // There is no need to free memory here because Assign() transferred ownership.
-00285         buf.Close(); 
-00286         }
-00287 
-00291 void CRBufExample::SwapTwoRBufsL()
-00292         {
-00293         _LIT(KSwapAndCopy,"\n Swapping and copying data: ");
-00294         iConsole->Printf(KSwapAndCopy);
-00295         
-00296         RBuf buf1;
-00297         
-00298         // Create a buffer descriptor, initialised with some text
-00299         User::LeaveIfError(buf1.Create(KBuffer1));
-00300         CleanupClosePushL(buf1);
-00301         
-00302         RBuf buf2;
-00303         
-00304         // Create a second buffer descriptor, initialised with some text
-00305         User::LeaveIfError(buf2.Create(KBuffer2));
-00306         CleanupClosePushL(buf2);
-00307         
-00308         _LIT(KBeforeSwapping, "\n Before swapping: ");
-00309         iConsole->Printf(KBeforeSwapping);
-00310         
-00311         // Print out the contents of the 2 descriptors
-00312         _LIT(KPrintFirstdata,"\n  Data present in first descriptor is: ");
-00313         iConsole->Printf(KPrintFirstdata);      
-00314         iConsole->Printf(buf1);
-00315         
-00316         _LIT(KPrintSecondData,"\n  Data present in second descriptor is: ");
-00317         iConsole->Printf(KPrintSecondData);     
-00318         iConsole->Printf(buf2);
-00319         
-00320         // Swap them and print out the new contents
-00321         buf1.Swap(buf2);
-00322         
-00323         _LIT(KAfterSwapping, "\n After swapping: ");
-00324         iConsole->Printf(KAfterSwapping);
-00325         
-00326         iConsole->Printf(KPrintFirstdata);      
-00327         iConsole->Printf(buf1);
-00328         
-00329         iConsole->Printf(KPrintSecondData);     
-00330         iConsole->Printf(buf2);
-00331         
-00332         _LIT(KSwap, "\n Swapping between two RBufs is successful");
-00333         iConsole->Printf(KSwap);
-00334                         
-00335         // Deallocate memory assigned to the 2 buffer descriptors.
-00336         CleanupStack::PopAndDestroy(2); // buf1, buf2
-00337         }
-00338         
-00342 void CRBufExample::CopyDataUsingAssignmentOperatorL()
-00343         {
-00344         RBuf buf1;
-00345         
-00346         // Create a buffer descriptor, initialised with some text
-00347         buf1.Create(KBuffer1);
-00348         CleanupClosePushL(buf1);
-00349 
-00350         RBuf buf2;
-00351         
-00352         // Create a second buffer descriptor, initialised with some text
-00353         User::LeaveIfError(buf2.Create(KBuffer2));
-00354         CleanupClosePushL(buf2);
-00355 
-00356         // Copy data using assignment operator from one to the other. 
-00357         // Target buffer must be long enough, otherwise a panic occurs.
-00358         buf2= buf1;
-00359         
-00360         // Check the value of the buffers .
-00361         if (buf1==buf2)
-00362                 {
-00363                 _LIT(KCopyDataUsingAssignmentOperator,"\n Copying data using assignment operator from descriptor, and RBuf is successful");
-00364                 iConsole->Printf(KCopyDataUsingAssignmentOperator);
-00365                 }
-00366         else
-00367                 {
-00368                 iConsole->Printf(KFailed);      
-00369                 }       
-00370         
-00371         iConsole->Printf(KPressAKey);
-00372     iConsole->Getch();
-00373         
-00374         // Deallocate memory assigned to the 2 buffer descriptors.
-00375         CleanupStack::PopAndDestroy(2); // buf1, buf2
-00376         }
-00377         
-00381 void CRBufExample::ReallocateAndFreeTheMemoryBufferL()
-00382         {
-00383         _LIT(KReAllocAndFree,"\n Realloc and free:");
-00384         iConsole->Printf(KReAllocAndFree);
-00385         
-00386         RBuf buf;
-00387         
-00388         // Create empty buffer descriptor with max length of KSmallBufSize.
-00389         User::LeaveIfError(buf.Create(KSmallBufSize));
-00390         
-00391         // Get the length of the buffer.
-00392         TInt length= buf.Length();
-00393         
-00394         // Get the maximum length of the buffer.
-00395         TInt maxLength= buf.MaxLength();
-00396         
-00397         _LIT(KBeforeReAlloc,"\n Before ReAlloc: ");
-00398         iConsole->Printf(KBeforeReAlloc);
-00399         
-00400         // Print out the length and max length
-00401         iConsole->Printf(KLength, length);
-00402         iConsole->Printf(KMaxLength, maxLength);        
-00403 
-00404         // Re-allocate the buffer descriptor. Length doesnt change but maxlength does.
-00405         User::LeaveIfError(buf.ReAlloc(KMedBufSize));
-00406         
-00407         // Get the length of the buffer.
-00408         length= buf.Length();
-00409         
-00410         // Get the maximum length of the reallocated buffer.
-00411         maxLength= buf.MaxLength();
-00412         
-00413         _LIT(KAfterReAlloc,"\n After ReAlloc: ");
-00414         iConsole->Printf(KAfterReAlloc);
-00415 
-00416         // Print out the length and new max length
-00417         iConsole->Printf(KLength, length);
-00418         iConsole->Printf(KMaxLength, maxLength);        
-00419         
-00420         // Free the memory buffer. It sets the length and maxlength to zero.
-00421         User::LeaveIfError(buf.ReAlloc(0));
-00422         
-00423         // Get the length of the buffer.
-00424         length= buf.Length();
-00425         
-00426         // Get the maximum length of the buffer.
-00427         maxLength= buf.MaxLength();
-00428         
-00429         _LIT(KFreeBuffer,"\n After free: ");
-00430         iConsole->Printf(KFreeBuffer);
-00431         
-00432         iConsole->Printf(KLength, length);
-00433         iConsole->Printf(KMaxLength, maxLength);
-00434         }
-00435         
-00439 void CRBufExample::ReplaceAndModifyTheDataL()
-00440         {
-00441         RBuf buf;
-00442         
-00443         // Create a buffer descriptor, initialised with some text
-00444         User::LeaveIfError(buf.Create(KBuffer1)); 
-00445         CleanupClosePushL(buf);
-00446                         
-00447         _LIT(KReplaceAndModify,"\n Replace and modify: ");
-00448         iConsole->Printf(KReplaceAndModify);
-00449         
-00450         // Print the original data present in the buffer.
-00451         _LIT(KOriginalData,"\n Data present in RBuf is: ");
-00452         iConsole->Printf(KOriginalData);
-00453         iConsole->Printf(buf);
-00454         
-00455         TInt pos= 1;
-00456         
-00457         _LIT(KReplacedMessage,"22");
-00458         
-00459         TInt length= KReplacedMessage().Length();
-00460 
-00461         // Replace a portion of the data in the buffer descriptor.
-00462         buf.Replace(pos,length, KReplacedMessage);
-00463         
-00464         // Print the buffer's new contents.
-00465         _LIT(KReplacedData,"\n After replacement, data held in RBuf is: ");
-00466         iConsole->Printf(KReplacedData);
-00467         iConsole->Printf(buf);
-00468         
-00469         pos=3;
-00470         length=1;
-00471         
-00472         // Delete the replacement text.
-00473     buf.Delete(pos,length);
-00474     
-00475     // Print the buffer's contents again.
-00476     _LIT(KModifiedData,"\n After modification, data held in RBuf is: ");
-00477     iConsole->Printf(KModifiedData);
-00478     iConsole->Printf(buf);
-00479     
-00480     _LIT(KReplaceAndModifyData,"\n Replacing and modifying the data held in RBuf is successful");
-00481     iConsole->Printf(KReplaceAndModifyData);
-00482     
-00483     // Deallocate the memory assigned to the buffer. 
-00484         CleanupStack::PopAndDestroy(&buf);
-00485         }
-00486         
-00490 void CRBufExample::CleanUpRulesL()
-00491         {
-00492         RBuf buf;
-00493         
-00494         // Create a buffer descriptor, initialised with some text
-00495         User::LeaveIfError(buf.Create(KBuffer1));
-00496         
-00497         // To avoid memory leaks use RBuf::CleanupClosePushL() to push a cleanup item for the RBuf onto the cleanup stack.
-00498         // The effect is to cause Close() to be called on the buffer descriptor if a leave occurs, 
-00499         // or when CleanupStack::PopAndDestroy() is called.
-00500         buf.CleanupClosePushL();
-00501         
-00502         // Causes Close() to be called to deallocate the memory assigned to the buffer.
-00503         CleanupStack::PopAndDestroy(&buf);
-00504         
-00505         _LIT(KCleanUp,"\n RBuf cleanup is successful");
-00506         iConsole->Printf(KCleanUp);
-00507         
-00508         _LIT(KExitMsg, "\n\n Press any key to exit the example");
-00509     iConsole->Printf(KExitMsg);
-00510         iConsole->Getch();
-00511         }       
-00512 
-00513 void MainL()
-00514         {
-00515         CRBufExample* app= CRBufExample::NewL();
-00516         CleanupStack::PushL(app);
-00517         
-00518         // Creates empty RBuf.
-00519         app->CreateRBufL();
-00520         
-00521         // Creates RBuf from an existing descriptor.
-00522         app->CreateRBufFromExistingDesL(); 
-00523         
-00524         // Creates RBuf from an HBufC.
-00525         app->CreateRBufFromHBufCL();
-00526         
-00527         // Creates RBuf from another RBuf.
-00528         app->CreateRBufFromAnotherRBufL();
-00529         
-00530         // Create RBuf using RReadStream
-00531         app->CreateRBufUsingRReadStreamL();
-00532         
-00533         // Creates RBuf from allocated memory.
-00534         app->CreateRBufFromAllocatedMemoryL();
-00535                 
-00536         // Swaps two RBufs.
-00537         app->SwapTwoRBufsL();
-00538         
-00539         // Copies data using assignment operator.
-00540         app->CopyDataUsingAssignmentOperatorL();
-00541         
-00542         // Reallocates and frees the memory buffer.
-00543         app->ReallocateAndFreeTheMemoryBufferL();
-00544         
-00545         // Replaces and modifies the data in the descriptor.
-00546         app->ReplaceAndModifyTheDataL();
-00547         
-00548         // Demonstrates cleanup.
-00549         app->CleanUpRulesL();
-00550         
-00551         CleanupStack::PopAndDestroy(app);
-00552         } 
-00553 
-00554 GLDEF_C TInt E32Main()
-00555         {
-00556         __UHEAP_MARK;
-00557 
-00558         CTrapCleanup* cleanup = CTrapCleanup::New();
-00559         if(cleanup == NULL)
-00560                 {
-00561                 return KErrNoMemory;
-00562                 }
-00563         TRAPD(err, MainL());
-00564         delete cleanup;
-00565 
-00566         if(err !=KErrNone)
-00567                 {
-00568                 User::Panic(KFailed, err);
-00569                 }       
-00570                 
-00571         __UHEAP_MARKEND;
-00572         return KErrNone;
-00573         }
-00574   
-00575   
-00576   
-00577   
-00578  
-00579  
-00580  
-

Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by  - -doxygen 1.5.3
- -