00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00088
00089 User::LeaveIfError(buf.Create(KSmallBufSize));
00090 CleanupClosePushL(buf);
00091
00092
00093 TInt length= buf.Length();
00094
00095
00096 TInt maxLength= buf.MaxLength();
00097
00098
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
00112 CleanupStack::PopAndDestroy(&buf);
00113
00114
00115
00116 User::LeaveIfError(buf.CreateMax(KSmallBufSize));
00117 CleanupClosePushL(buf);
00118
00119
00120 length= buf.Length();
00121
00122
00123 maxLength=buf.MaxLength();
00124
00125
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
00139 CleanupStack::PopAndDestroy(&buf);
00140 }
00141
00145 void CRBufExample::CreateRBufFromExistingDesL()
00146 {
00147 RBuf buf;
00148
00149
00150 buf.CreateL(KBuffer1);
00151
00152 _LIT(KCreateFromExistingDes, "\n From an existing descriptor ");
00153 iConsole->Printf(KCreateFromExistingDes);
00154
00155
00156 buf.Close();
00157 }
00158
00162 void CRBufExample::CreateRBufFromHBufCL()
00163 {
00164
00165 HBufC* hptr = HBufC::NewL(KBuffer1().Length());
00166
00167
00168 *hptr = KBuffer1;
00169
00170 RBuf buf;
00171
00172
00173 buf.Assign(hptr);
00174
00175 _LIT(KCreateFromHBufC, "\n From HBufC ");
00176 iConsole->Printf(KCreateFromHBufC);
00177
00178
00179
00180 buf.Close();
00181 }
00182
00186 void CRBufExample::CreateRBufFromAnotherRBufL()
00187 {
00188 RBuf buf;
00189
00190
00191 User::LeaveIfError(buf.Create(KBuffer1));
00192
00193 RBuf targetBuf;
00194
00195
00196
00197
00198
00199 targetBuf.Assign(buf);
00200
00201 _LIT(KCreateFromAnotherRBuf, "\n From another RBuf");
00202 iConsole->Printf(KCreateFromAnotherRBuf);
00203
00204
00205 targetBuf.Close();
00206 }
00207
00211 void CRBufExample::CreateRBufUsingRReadStreamL()
00212 {
00213
00214
00215 RFs fs;
00216
00217
00218 User::LeaveIfError(fs.Connect());
00219 CleanupClosePushL(fs);
00220
00221 User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
00222
00223
00224 User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
00225
00226
00227 RFileWriteStream wStream;
00228
00229 _LIT(KFileName,"stream.dat");
00230
00231
00232 User::LeaveIfError(wStream.Replace(fs, KFileName, EFileWrite));
00233 CleanupClosePushL(wStream);
00234
00235 _LIT(KText, "RBuf Example");
00236
00237
00238 wStream << KText();
00239 wStream.CommitL();
00240 CleanupStack::PopAndDestroy(&wStream);
00241
00242
00243 RFileReadStream rStream;
00244
00245
00246 User::LeaveIfError(rStream.Open(fs, KFileName, EFileRead));
00247 CleanupClosePushL(rStream);
00248
00249 RBuf buf;
00250 CleanupClosePushL(buf);
00251
00252
00253 buf.CreateL(rStream, KLargeBufSize);
00254
00255 _LIT(KCreateUsingRReadStream, "\n Using RReadStream ");
00256 iConsole->Printf(KCreateUsingRReadStream);
00257
00258
00259 CleanupStack::PopAndDestroy(&buf);
00260 CleanupStack::PopAndDestroy(&rStream);
00261 CleanupStack::PopAndDestroy(&fs);
00262 }
00263
00267 void CRBufExample::CreateRBufFromAllocatedMemoryL()
00268 {
00269 RBuf buf;
00270
00271
00272 TUint16* allocatedMemory = static_cast<TUint16*>( User::Alloc( KLargeBufSize * sizeof( TUint16) ) ) ;
00273
00274
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
00284
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
00299 User::LeaveIfError(buf1.Create(KBuffer1));
00300 CleanupClosePushL(buf1);
00301
00302 RBuf buf2;
00303
00304
00305 User::LeaveIfError(buf2.Create(KBuffer2));
00306 CleanupClosePushL(buf2);
00307
00308 _LIT(KBeforeSwapping, "\n Before swapping: ");
00309 iConsole->Printf(KBeforeSwapping);
00310
00311
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
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
00336 CleanupStack::PopAndDestroy(2);
00337 }
00338
00342 void CRBufExample::CopyDataUsingAssignmentOperatorL()
00343 {
00344 RBuf buf1;
00345
00346
00347 buf1.Create(KBuffer1);
00348 CleanupClosePushL(buf1);
00349
00350 RBuf buf2;
00351
00352
00353 User::LeaveIfError(buf2.Create(KBuffer2));
00354 CleanupClosePushL(buf2);
00355
00356
00357
00358 buf2= buf1;
00359
00360
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
00375 CleanupStack::PopAndDestroy(2);
00376 }
00377
00381 void CRBufExample::ReallocateAndFreeTheMemoryBufferL()
00382 {
00383 _LIT(KReAllocAndFree,"\n Realloc and free:");
00384 iConsole->Printf(KReAllocAndFree);
00385
00386 RBuf buf;
00387
00388
00389 User::LeaveIfError(buf.Create(KSmallBufSize));
00390
00391
00392 TInt length= buf.Length();
00393
00394
00395 TInt maxLength= buf.MaxLength();
00396
00397 _LIT(KBeforeReAlloc,"\n Before ReAlloc: ");
00398 iConsole->Printf(KBeforeReAlloc);
00399
00400
00401 iConsole->Printf(KLength, length);
00402 iConsole->Printf(KMaxLength, maxLength);
00403
00404
00405 User::LeaveIfError(buf.ReAlloc(KMedBufSize));
00406
00407
00408 length= buf.Length();
00409
00410
00411 maxLength= buf.MaxLength();
00412
00413 _LIT(KAfterReAlloc,"\n After ReAlloc: ");
00414 iConsole->Printf(KAfterReAlloc);
00415
00416
00417 iConsole->Printf(KLength, length);
00418 iConsole->Printf(KMaxLength, maxLength);
00419
00420
00421 User::LeaveIfError(buf.ReAlloc(0));
00422
00423
00424 length= buf.Length();
00425
00426
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
00444 User::LeaveIfError(buf.Create(KBuffer1));
00445 CleanupClosePushL(buf);
00446
00447 _LIT(KReplaceAndModify,"\n Replace and modify: ");
00448 iConsole->Printf(KReplaceAndModify);
00449
00450
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
00462 buf.Replace(pos,length, KReplacedMessage);
00463
00464
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
00473 buf.Delete(pos,length);
00474
00475
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
00484 CleanupStack::PopAndDestroy(&buf);
00485 }
00486
00490 void CRBufExample::CleanUpRulesL()
00491 {
00492 RBuf buf;
00493
00494
00495 User::LeaveIfError(buf.Create(KBuffer1));
00496
00497
00498
00499
00500 buf.CleanupClosePushL();
00501
00502
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
00519 app->CreateRBufL();
00520
00521
00522 app->CreateRBufFromExistingDesL();
00523
00524
00525 app->CreateRBufFromHBufCL();
00526
00527
00528 app->CreateRBufFromAnotherRBufL();
00529
00530
00531 app->CreateRBufUsingRReadStreamL();
00532
00533
00534 app->CreateRBufFromAllocatedMemoryL();
00535
00536
00537 app->SwapTwoRBufsL();
00538
00539
00540 app->CopyDataUsingAssignmentOperatorL();
00541
00542
00543 app->ReallocateAndFreeTheMemoryBufferL();
00544
00545
00546 app->ReplaceAndModifyTheDataL();
00547
00548
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