examples/Base/BufsAndStrings/DynamicBuffers/DynamicBuffers.cpp

00001 // Copyright (c) 2000-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 //
00015 
00016 #include "CommonFramework.h"
00017 
00018 //
00019 // Common literal text
00020 //
00021 _LIT(KTxtNewLine,"\n");
00022 
00023 LOCAL_C void writeBuf(CBufBase* aBuf);
00024 LOCAL_C void standardBufferStuffL(CBufBase* aBuf);
00025 LOCAL_C void showExpandL();
00026 LOCAL_C void waitForKey();
00027 //
00028 // do the example
00029 //
00030 LOCAL_C void doExampleL()
00031     {
00032         //
00033         // do flat buffer demonstration
00034         //
00035         _LIT(KTxtFlatBufDemo,"Flat buffer demonstration\n");
00036         console->Printf(KTxtFlatBufDemo);
00037         CBufFlat* flatBuf=CBufFlat::NewL(4);
00038         CleanupStack::PushL(flatBuf);
00039         flatBuf->SetReserveL(32);
00040         _LIT(KTxtFlatBufCapacity,"flat buffer capacity=%d\n");
00041         console->Printf(KTxtFlatBufCapacity,flatBuf->Capacity());
00042         standardBufferStuffL(flatBuf);
00043         CleanupStack::PopAndDestroy();
00044         waitForKey();
00045         //
00046         // do segmented buffer demonstration
00047         //
00048         _LIT(KTxtSegBufDemo,"Segmented buffer demonstration\n");
00049         console->Printf(KTxtSegBufDemo);
00050         CBufSeg* segBuf=CBufSeg::NewL(4);
00051         CleanupStack::PushL(segBuf);
00052         standardBufferStuffL(segBuf);
00053         CleanupStack::PopAndDestroy();
00054         waitForKey();
00055         //
00056         // show ExpandL() and ResizeL()
00057         //
00058         showExpandL();
00059         }
00060 
00061 
00062 LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
00063         {
00064         //
00065         // insert text into buffer (6 chars)
00066         //
00067         _LIT(KTxtHello,"Hello!");
00068         aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
00069         writeBuf(aBuf);
00070         //
00071         // append more text into buffer (another 6 chars)
00072         //
00073         //
00074         _LIT(KTxtWorld," world");
00075         aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
00076         writeBuf(aBuf);
00077         //
00078         // read the 5 characters starting at character
00079         // position 3 from the buffer into a descriptor.
00080         //
00081         //
00082         TBuf<5> des;
00083         aBuf->Read(6,(TAny*)des.Ptr(),des.Size());
00084         _LIT(KTxtRead,"read: %S\n");
00085         console->Printf(KTxtRead,&des);
00086         //
00087         // [over]write 5 characters at character position 6
00088         //
00089         //
00090         _LIT(KTxtFolks,"folks");
00091         aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
00092         writeBuf(aBuf);
00093         //
00094         // delete characters
00095         //
00096         TInt startpos = 5;
00097         TInt length   = 6;
00098         startpos <<= 1;
00099         length   <<= 1;
00100         aBuf->Delete(startpos,length);
00101         writeBuf(aBuf);
00102         //
00103         // compress
00104         //
00105         aBuf->Compress();
00106         writeBuf(aBuf);
00107         }
00108 
00109 LOCAL_C void writeBuf(CBufBase* aBuf)
00110         {
00111         //
00112         // print, segment by segment
00113         //
00114         _LIT(KTxtBuffer,"buffer:");
00115         console->Printf(KTxtBuffer);
00116         TInt  bufpos=0;
00117         TPtrC8 bufptr=aBuf->Ptr(bufpos);
00118         while (bufptr.Length()>0)
00119                 {
00120                 //
00121                 // write out this segment of the buffer.
00122                 // Note that the descriptor 'display' is built differently
00123                 // for Unicode; it also assumes an even number of bytes;
00124                 // this is valid because the granularity of the buffer is 4.
00125                 //
00126                 TPtrC display;
00127                 display.Set((TUint16*)bufptr.Ptr(),(bufptr.Length()>>1));
00128                 _LIT(KFormat1," [%d,%d] %S");
00129                 console->Printf(KFormat1, bufpos, bufptr.Length(), &display);
00130                 //
00131                 // update position within the buffer
00132                 // and the pointer-descriptor.
00133                 //
00134                 bufpos+=bufptr.Length();       // update position
00135                 bufptr.Set(aBuf->Ptr(bufpos)); // should be the next segment
00136                 }
00137         console->Printf(KTxtNewLine);
00138         }
00139 
00140         
00141 
00142 LOCAL_C void showExpandL()
00143         {
00144         _LIT(KTxtShowExpand,"Showing ExpandL()\n");
00145         console->Printf(KTxtShowExpand);
00146         //
00147         // allocate the segmented buffer with 
00148         // a granularity of 4
00149         //
00150         CBufBase* buf=CBufSeg::NewL(4);
00151         CleanupStack::PushL(buf);
00152         //
00153         // insert text into buffer (12 UNICODE chars)
00154         //
00155         _LIT(KTxtHelloWorld,"Hello world!");
00156         buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
00157         //
00158         // reserve space for (or 16 16-bit chars (32 bytes))
00159         //
00160         buf->ExpandL(12,32); // expand by 32 - may fail
00161         _LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
00162         console->Printf(KTxtBufExpanded);
00163         writeBuf(buf);
00164         //
00165         // now insert 16 16-bit characters
00166         // one at time.
00167         //
00168         // This is CLEARLY INEFFICIENT but shows
00169         // how successive calls to Write() can be done
00170         // without risk of failing for lack of memory.
00171         //
00172         _LIT(KTxtAtoP,"abcdefghijklmnop");
00173         TBufC<16> source(KTxtAtoP);
00174         for (TInt i=0; i<16; i++)
00175                 {
00176                 buf->Write((i+6)<<1,(TAny*)&source[i],2);
00177                 }
00178         _LIT(KTxtExpandedFilled,"expanded space filled: ");
00179         console->Printf(KTxtExpandedFilled);
00180         writeBuf(buf);
00181         //
00182         // now adjust size down to 18
00183         //
00184         buf->ResizeL(18);
00185         _LIT(KTxtResized,"resized:");
00186         console->Printf(KTxtResized);
00187         writeBuf(buf);
00188         //
00189         // destroy buffer
00190         //
00191         CleanupStack::PopAndDestroy();
00192         }
00193 
00194 LOCAL_C void waitForKey()
00195         {
00196         _LIT(KTxtPressAnyKey,"[press any key]");
00197         console->Printf(KTxtPressAnyKey);
00198         console->Getch();
00199         console->Printf(KTxtNewLine);
00200         }

Generated by  doxygen 1.6.2