00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00035 #include "rbufexample.h"
00036
00037 const TInt KLargeBufSize = 15;
00038 const TInt KSmallBufSize = 3;
00039 const TInt KMedBufSize = 7;
00040
00041 _LIT(KFailed, "\n Error occurred");
00042 _LIT(KPressAKey, "\n\n Press any key to continue the example\n");
00043 _LIT(KLength,"\n Length= %d");
00044 _LIT(KMaxLength,"\n MaxLength= %d");
00045 _LIT(KBuffer1,"11111");
00046 _LIT(KBuffer2,"22222");
00047
00051 CRBufExample::CRBufExample()
00052 {
00053 }
00054
00055 void CRBufExample::ConstructL()
00056 {
00057 _LIT(KTitle," RBuf Example");
00058 iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
00059
00060 _LIT(KWelcome," Welcome to the RBuf example application");
00061 iConsole->Printf(KWelcome);
00062
00063 _LIT(KPressAKeyMsg, "\n\n Press any key to step through the example\n");
00064 iConsole->Printf(KPressAKeyMsg );
00065 iConsole->Getch();
00066 }
00067
00071 CRBufExample::~CRBufExample()
00072 {
00073 delete iConsole;
00074 }
00075
00080 CRBufExample* CRBufExample::NewL()
00081 {
00082 CRBufExample* self=new(ELeave)CRBufExample();
00083 CleanupStack::PushL(self);
00084 self->ConstructL();
00085 CleanupStack::Pop(self);
00086 return self;
00087 }
00088
00092 void CRBufExample::CreateRBufL()
00093 {
00094 _LIT(KCreate,"\n Creating an RBuf: \n");
00095
00096 iConsole->Printf(KCreate);
00097
00098 RBuf buf;
00099
00100
00101
00102 User::LeaveIfError(buf.Create(KSmallBufSize));
00103 CleanupClosePushL(buf);
00104
00105
00106 TInt length= buf.Length();
00107
00108
00109 TInt maxLength= buf.MaxLength();
00110
00111
00112 if(length != maxLength)
00113 {
00114 _LIT(KUsingCreate, "\n Using Create() API: ");
00115 iConsole->Printf(KUsingCreate);
00116 iConsole->Printf(KLength, length);
00117 iConsole->Printf(KMaxLength, maxLength);
00118 }
00119 else
00120 {
00121 iConsole->Printf(KFailed);
00122 }
00123
00124
00125 CleanupStack::PopAndDestroy(&buf);
00126
00127
00128
00129 User::LeaveIfError(buf.CreateMax(KSmallBufSize));
00130 CleanupClosePushL(buf);
00131
00132
00133 length= buf.Length();
00134
00135
00136 maxLength=buf.MaxLength();
00137
00138
00139 if(length == maxLength)
00140 {
00141 _LIT(KCreateInAdvance, "\n Using CreateMax() API: ");
00142 iConsole->Printf(KCreateInAdvance);
00143 iConsole->Printf(KLength, length);
00144 iConsole->Printf(KMaxLength, maxLength);
00145 }
00146 else
00147 {
00148 iConsole->Printf(KFailed);
00149 }
00150
00151
00152 CleanupStack::PopAndDestroy(&buf);
00153 }
00154
00158 void CRBufExample::CreateRBufFromExistingDesL()
00159 {
00160 RBuf buf;
00161
00162
00163 buf.CreateL(KBuffer1);
00164
00165 _LIT(KCreateFromExistingDes, "\n From an existing descriptor ");
00166 iConsole->Printf(KCreateFromExistingDes);
00167
00168
00169 buf.Close();
00170 }
00171
00175 void CRBufExample::CreateRBufFromHBufCL()
00176 {
00177
00178 HBufC* hptr = HBufC::NewL(KBuffer1().Length());
00179
00180
00181 *hptr = KBuffer1;
00182
00183 RBuf buf;
00184
00185
00186 buf.Assign(hptr);
00187
00188 _LIT(KCreateFromHBufC, "\n From HBufC ");
00189 iConsole->Printf(KCreateFromHBufC);
00190
00191
00192
00193 buf.Close();
00194 }
00195
00199 void CRBufExample::CreateRBufFromAnotherRBufL()
00200 {
00201 RBuf buf;
00202
00203
00204 User::LeaveIfError(buf.Create(KBuffer1));
00205
00206 RBuf targetBuf;
00207
00208
00209
00210
00211
00212 targetBuf.Assign(buf);
00213
00214 _LIT(KCreateFromAnotherRBuf, "\n From another RBuf");
00215 iConsole->Printf(KCreateFromAnotherRBuf);
00216
00217
00218 targetBuf.Close();
00219 }
00220
00224 void CRBufExample::CreateRBufUsingRReadStreamL()
00225 {
00226
00227
00228 RFs fs;
00229
00230
00231 User::LeaveIfError(fs.Connect());
00232 CleanupClosePushL(fs);
00233
00234 User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
00235
00236
00237 User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
00238
00239
00240 RFileWriteStream wStream;
00241
00242 _LIT(KFileName,"stream.dat");
00243
00244
00245 User::LeaveIfError(wStream.Replace(fs, KFileName, EFileWrite));
00246 CleanupClosePushL(wStream);
00247
00248 _LIT(KText, "RBuf Example");
00249
00250
00251 wStream << KText();
00252 wStream.CommitL();
00253 CleanupStack::PopAndDestroy(&wStream);
00254
00255
00256 RFileReadStream rStream;
00257
00258
00259 User::LeaveIfError(rStream.Open(fs, KFileName, EFileRead));
00260 CleanupClosePushL(rStream);
00261
00262 RBuf buf;
00263 CleanupClosePushL(buf);
00264
00265
00266 buf.CreateL(rStream, KLargeBufSize);
00267
00268 _LIT(KCreateUsingRReadStream, "\n Using RReadStream ");
00269 iConsole->Printf(KCreateUsingRReadStream);
00270
00271
00272 CleanupStack::PopAndDestroy(&buf);
00273 CleanupStack::PopAndDestroy(&rStream);
00274 CleanupStack::PopAndDestroy(&fs);
00275 }
00276
00280 void CRBufExample::CreateRBufFromAllocatedMemoryL()
00281 {
00282 RBuf buf;
00283
00284
00285 TUint16* allocatedMemory = static_cast<TUint16*>( User::Alloc( KLargeBufSize * sizeof( TUint16) ) ) ;
00286
00287
00288 buf.Assign(allocatedMemory, KLargeBufSize);
00289
00290 _LIT(KCreateFromAllocatedMemory, "\n By transferring ownership of allocated memory");
00291 iConsole->Printf(KCreateFromAllocatedMemory);
00292
00293 iConsole->Printf(KPressAKey);
00294 iConsole->Getch();
00295
00296
00297
00298 buf.Close();
00299 }
00300
00304 void CRBufExample::SwapTwoRBufsL()
00305 {
00306 _LIT(KSwapAndCopy,"\n Swapping and copying data: ");
00307 iConsole->Printf(KSwapAndCopy);
00308
00309 RBuf buf1;
00310
00311
00312 User::LeaveIfError(buf1.Create(KBuffer1));
00313 CleanupClosePushL(buf1);
00314
00315 RBuf buf2;
00316
00317
00318 User::LeaveIfError(buf2.Create(KBuffer2));
00319 CleanupClosePushL(buf2);
00320
00321 _LIT(KBeforeSwapping, "\n Before swapping: ");
00322 iConsole->Printf(KBeforeSwapping);
00323
00324
00325 _LIT(KPrintFirstdata,"\n Data present in first descriptor is: ");
00326 iConsole->Printf(KPrintFirstdata);
00327 iConsole->Printf(buf1);
00328
00329 _LIT(KPrintSecondData,"\n Data present in second descriptor is: ");
00330 iConsole->Printf(KPrintSecondData);
00331 iConsole->Printf(buf2);
00332
00333
00334 buf1.Swap(buf2);
00335
00336 _LIT(KAfterSwapping, "\n After swapping: ");
00337 iConsole->Printf(KAfterSwapping);
00338
00339 iConsole->Printf(KPrintFirstdata);
00340 iConsole->Printf(buf1);
00341
00342 iConsole->Printf(KPrintSecondData);
00343 iConsole->Printf(buf2);
00344
00345 _LIT(KSwap, "\n Swapping between two RBufs is successful");
00346 iConsole->Printf(KSwap);
00347
00348
00349 CleanupStack::PopAndDestroy(2);
00350 }
00351
00355 void CRBufExample::CopyDataUsingAssignmentOperatorL()
00356 {
00357 RBuf buf1;
00358
00359
00360 buf1.Create(KBuffer1);
00361 CleanupClosePushL(buf1);
00362
00363 RBuf buf2;
00364
00365
00366 User::LeaveIfError(buf2.Create(KBuffer2));
00367 CleanupClosePushL(buf2);
00368
00369
00370
00371 buf2= buf1;
00372
00373
00374 if (buf1==buf2)
00375 {
00376 _LIT(KCopyDataUsingAssignmentOperator,"\n Copying data using assignment operator from descriptor, and RBuf is successful");
00377 iConsole->Printf(KCopyDataUsingAssignmentOperator);
00378 }
00379 else
00380 {
00381 iConsole->Printf(KFailed);
00382 }
00383
00384 iConsole->Printf(KPressAKey);
00385 iConsole->Getch();
00386
00387
00388 CleanupStack::PopAndDestroy(2);
00389 }
00390
00394 void CRBufExample::ReallocateAndFreeTheMemoryBufferL()
00395 {
00396 _LIT(KReAllocAndFree,"\n Realloc and free:");
00397 iConsole->Printf(KReAllocAndFree);
00398
00399 RBuf buf;
00400
00401
00402 User::LeaveIfError(buf.Create(KSmallBufSize));
00403
00404
00405 TInt length= buf.Length();
00406
00407
00408 TInt maxLength= buf.MaxLength();
00409
00410 _LIT(KBeforeReAlloc,"\n Before ReAlloc: ");
00411 iConsole->Printf(KBeforeReAlloc);
00412
00413
00414 iConsole->Printf(KLength, length);
00415 iConsole->Printf(KMaxLength, maxLength);
00416
00417
00418 User::LeaveIfError(buf.ReAlloc(KMedBufSize));
00419
00420
00421 length= buf.Length();
00422
00423
00424 maxLength= buf.MaxLength();
00425
00426 _LIT(KAfterReAlloc,"\n After ReAlloc: ");
00427 iConsole->Printf(KAfterReAlloc);
00428
00429
00430 iConsole->Printf(KLength, length);
00431 iConsole->Printf(KMaxLength, maxLength);
00432
00433
00434 User::LeaveIfError(buf.ReAlloc(0));
00435
00436
00437 length= buf.Length();
00438
00439
00440 maxLength= buf.MaxLength();
00441
00442 _LIT(KFreeBuffer,"\n After free: ");
00443 iConsole->Printf(KFreeBuffer);
00444
00445 iConsole->Printf(KLength, length);
00446 iConsole->Printf(KMaxLength, maxLength);
00447 }
00448
00452 void CRBufExample::ReplaceAndModifyTheDataL()
00453 {
00454 RBuf buf;
00455
00456
00457 User::LeaveIfError(buf.Create(KBuffer1));
00458 CleanupClosePushL(buf);
00459
00460 _LIT(KReplaceAndModify,"\n Replace and modify: ");
00461 iConsole->Printf(KReplaceAndModify);
00462
00463
00464 _LIT(KOriginalData,"\n Data present in RBuf is: ");
00465 iConsole->Printf(KOriginalData);
00466 iConsole->Printf(buf);
00467
00468 TInt pos= 1;
00469
00470 _LIT(KReplacedMessage,"22");
00471
00472 TInt length= KReplacedMessage().Length();
00473
00474
00475 buf.Replace(pos,length, KReplacedMessage);
00476
00477
00478 _LIT(KReplacedData,"\n After replacement, data held in RBuf is: ");
00479 iConsole->Printf(KReplacedData);
00480 iConsole->Printf(buf);
00481
00482 pos=3;
00483 length=1;
00484
00485
00486 buf.Delete(pos,length);
00487
00488
00489 _LIT(KModifiedData,"\n After modification, data held in RBuf is: ");
00490 iConsole->Printf(KModifiedData);
00491 iConsole->Printf(buf);
00492
00493 _LIT(KReplaceAndModifyData,"\n Replacing and modifying the data held in RBuf is successful");
00494 iConsole->Printf(KReplaceAndModifyData);
00495
00496
00497 CleanupStack::PopAndDestroy(&buf);
00498 }
00499
00503 void CRBufExample::CleanUpRulesL()
00504 {
00505 RBuf buf;
00506
00507
00508 User::LeaveIfError(buf.Create(KBuffer1));
00509
00510
00511
00512
00513 buf.CleanupClosePushL();
00514
00515
00516 CleanupStack::PopAndDestroy(&buf);
00517
00518 _LIT(KCleanUp,"\n RBuf cleanup is successful");
00519 iConsole->Printf(KCleanUp);
00520
00521 _LIT(KExitMsg, "\n\n Press any key to exit the example");
00522 iConsole->Printf(KExitMsg);
00523 iConsole->Getch();
00524 }
00525
00526 void MainL()
00527 {
00528 CRBufExample* app= CRBufExample::NewL();
00529 CleanupStack::PushL(app);
00530
00531
00532 app->CreateRBufL();
00533
00534
00535 app->CreateRBufFromExistingDesL();
00536
00537
00538 app->CreateRBufFromHBufCL();
00539
00540
00541 app->CreateRBufFromAnotherRBufL();
00542
00543
00544 app->CreateRBufUsingRReadStreamL();
00545
00546
00547 app->CreateRBufFromAllocatedMemoryL();
00548
00549
00550 app->SwapTwoRBufsL();
00551
00552
00553 app->CopyDataUsingAssignmentOperatorL();
00554
00555
00556 app->ReallocateAndFreeTheMemoryBufferL();
00557
00558
00559 app->ReplaceAndModifyTheDataL();
00560
00561
00562 app->CleanUpRulesL();
00563
00564 CleanupStack::PopAndDestroy(app);
00565 }
00566
00567 GLDEF_C TInt E32Main()
00568 {
00569 __UHEAP_MARK;
00570
00571 CTrapCleanup* cleanup = CTrapCleanup::New();
00572 if(cleanup == NULL)
00573 {
00574 return KErrNoMemory;
00575 }
00576 TRAPD(err, MainL());
00577 delete cleanup;
00578
00579 if(err !=KErrNone)
00580 {
00581 User::Panic(KFailed, err);
00582 }
00583
00584 __UHEAP_MARKEND;
00585 return KErrNone;
00586 }
00587
00588
00589
00590
00591
00592
00593