examples/Base/BufsAndStrings/DynamicBuffers/DynamicBuffers.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "CommonFramework.h"
00032 
00033 //
00034 // Common literal text
00035 //
00036 _LIT(KTxtNewLine,"\n");
00037 
00038 LOCAL_C void writeBuf(CBufBase* aBuf);
00039 LOCAL_C void standardBufferStuffL(CBufBase* aBuf);
00040 LOCAL_C void showExpandL();
00041 LOCAL_C void waitForKey();
00042 //
00043 // do the example
00044 //
00045 LOCAL_C void doExampleL()
00046     {
00047         //
00048         // do flat buffer demonstration
00049         //
00050         _LIT(KTxtFlatBufDemo,"Flat buffer demonstration\n");
00051         console->Printf(KTxtFlatBufDemo);
00052         CBufFlat* flatBuf=CBufFlat::NewL(4);
00053         CleanupStack::PushL(flatBuf);
00054         flatBuf->SetReserveL(32);
00055         _LIT(KTxtFlatBufCapacity,"flat buffer capacity=%d\n");
00056         console->Printf(KTxtFlatBufCapacity,flatBuf->Capacity());
00057         standardBufferStuffL(flatBuf);
00058         CleanupStack::PopAndDestroy();
00059         waitForKey();
00060         //
00061         // do segmented buffer demonstration
00062         //
00063         _LIT(KTxtSegBufDemo,"Segmented buffer demonstration\n");
00064         console->Printf(KTxtSegBufDemo);
00065         CBufSeg* segBuf=CBufSeg::NewL(4);
00066         CleanupStack::PushL(segBuf);
00067         standardBufferStuffL(segBuf);
00068         CleanupStack::PopAndDestroy();
00069         waitForKey();
00070         //
00071         // show ExpandL() and ResizeL()
00072         //
00073         showExpandL();
00074         }
00075 
00076 
00077 LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
00078         {
00079         //
00080         // insert text into buffer (6 chars)
00081         //
00082         _LIT(KTxtHello,"Hello!");
00083         aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
00084         writeBuf(aBuf);
00085         //
00086         // append more text into buffer (another 6 chars)
00087         //
00088         //
00089         _LIT(KTxtWorld," world");
00090         aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
00091         writeBuf(aBuf);
00092         //
00093         // read the 5 characters starting at character
00094         // position 3 from the buffer into a descriptor.
00095         //
00096         //
00097         TBuf<5> des;
00098         aBuf->Read(6,(TAny*)des.Ptr(),des.Size());
00099         _LIT(KTxtRead,"read: %S\n");
00100         console->Printf(KTxtRead,&des);
00101         //
00102         // [over]write 5 characters at character position 6
00103         //
00104         //
00105         _LIT(KTxtFolks,"folks");
00106         aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
00107         writeBuf(aBuf);
00108         //
00109         // delete characters
00110         //
00111         TInt startpos = 5;
00112         TInt length   = 6;
00113         startpos <<= 1;
00114         length   <<= 1;
00115         aBuf->Delete(startpos,length);
00116         writeBuf(aBuf);
00117         //
00118         // compress
00119         //
00120         aBuf->Compress();
00121         writeBuf(aBuf);
00122         }
00123 
00124 LOCAL_C void writeBuf(CBufBase* aBuf)
00125         {
00126         //
00127         // print, segment by segment
00128         //
00129         _LIT(KTxtBuffer,"buffer:");
00130         console->Printf(KTxtBuffer);
00131         TInt  bufpos=0;
00132         TPtrC8 bufptr=aBuf->Ptr(bufpos);
00133         while (bufptr.Length()>0)
00134                 {
00135                 //
00136                 // write out this segment of the buffer.
00137                 // Note that the descriptor 'display' is built differently
00138                 // for Unicode; it also assumes an even number of bytes;
00139                 // this is valid because the granularity of the buffer is 4.
00140                 //
00141                 TPtrC display;
00142                 display.Set((TUint16*)bufptr.Ptr(),(bufptr.Length()>>1));
00143                 _LIT(KFormat1," [%d,%d] %S");
00144                 console->Printf(KFormat1, bufpos, bufptr.Length(), &display);
00145                 //
00146                 // update position within the buffer
00147                 // and the pointer-descriptor.
00148                 //
00149                 bufpos+=bufptr.Length();       // update position
00150                 bufptr.Set(aBuf->Ptr(bufpos)); // should be the next segment
00151                 }
00152         console->Printf(KTxtNewLine);
00153         }
00154 
00155         
00156 
00157 LOCAL_C void showExpandL()
00158         {
00159         _LIT(KTxtShowExpand,"Showing ExpandL()\n");
00160         console->Printf(KTxtShowExpand);
00161         //
00162         // allocate the segmented buffer with 
00163         // a granularity of 4
00164         //
00165         CBufBase* buf=CBufSeg::NewL(4);
00166         CleanupStack::PushL(buf);
00167         //
00168         // insert text into buffer (12 UNICODE chars)
00169         //
00170         _LIT(KTxtHelloWorld,"Hello world!");
00171         buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
00172         //
00173         // reserve space for (or 16 16-bit chars (32 bytes))
00174         //
00175         buf->ExpandL(12,32); // expand by 32 - may fail
00176         _LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
00177         console->Printf(KTxtBufExpanded);
00178         writeBuf(buf);
00179         //
00180         // now insert 16 16-bit characters
00181         // one at time.
00182         //
00183         // This is CLEARLY INEFFICIENT but shows
00184         // how successive calls to Write() can be done
00185         // without risk of failing for lack of memory.
00186         //
00187         _LIT(KTxtAtoP,"abcdefghijklmnop");
00188         TBufC<16> source(KTxtAtoP);
00189         for (TInt i=0; i<16; i++)
00190                 {
00191                 buf->Write((i+6)<<1,(TAny*)&source[i],2);
00192                 }
00193         _LIT(KTxtExpandedFilled,"expanded space filled: ");
00194         console->Printf(KTxtExpandedFilled);
00195         writeBuf(buf);
00196         //
00197         // now adjust size down to 18
00198         //
00199         buf->ResizeL(18);
00200         _LIT(KTxtResized,"resized:");
00201         console->Printf(KTxtResized);
00202         writeBuf(buf);
00203         //
00204         // destroy buffer
00205         //
00206         CleanupStack::PopAndDestroy();
00207         }
00208 
00209 LOCAL_C void waitForKey()
00210         {
00211         _LIT(KTxtPressAnyKey,"[press any key]");
00212         console->Printf(KTxtPressAnyKey);
00213         console->Getch();
00214         console->Printf(KTxtNewLine);
00215         }

Generated by  doxygen 1.6.2