Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



Dynamic buffers example code


DynamicBuffers: using dynamic buffers

Found in: examples\Base\BufsAndStrings\DynamicBuffers

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// DynamicBuffers.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

#include "CommonFramework.h"

//
// Common literal text
//
_LIT(KTxtNewLine,"\n");

LOCAL_C void writeBuf(CBufBase* aBuf);
LOCAL_C void standardBufferStuffL(CBufBase* aBuf);
LOCAL_C void showExpandL();
LOCAL_C void waitForKey();
//
// do the example
//
LOCAL_C void doExampleL()
    {
    //
    // do flat buffer demonstration
    //
    _LIT(KTxtFlatBufDemo,"Flat buffer demonstration\n");
    console->Printf(KTxtFlatBufDemo);
    CBufFlat* flatBuf=CBufFlat::NewL(4);
    CleanupStack::PushL(flatBuf);
    flatBuf->SetReserveL(32);
    _LIT(KTxtFlatBufCapacity,"flat buffer capacity=%d\n");
    console->Printf(KTxtFlatBufCapacity,flatBuf->Capacity());
    standardBufferStuffL(flatBuf);
    CleanupStack::PopAndDestroy();
    waitForKey();
    //
    // do segmented buffer demonstration
    //
    _LIT(KTxtSegBufDemo,"Segmented buffer demonstration\n");
    console->Printf(KTxtSegBufDemo);
    CBufSeg* segBuf=CBufSeg::NewL(4);
    CleanupStack::PushL(segBuf);
    standardBufferStuffL(segBuf);
    CleanupStack::PopAndDestroy();
    waitForKey();
    //
    // show ExpandL() and ResizeL()
    //
    showExpandL();
    }


LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
    {
    //
    // insert text into buffer (6 chars)
    //
    _LIT(KTxtHello,"Hello!");
    aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
    writeBuf(aBuf);
    //
    // append more text into buffer (another 6 chars)
    //
    //
    _LIT(KTxtWorld," world");
    aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
    writeBuf(aBuf);
    //
    // read the 5 characters starting at character
    // position 3 from the buffer into a descriptor.
    //
    //
    TBuf<5> des;
    aBuf->Read(6,(TAny*)des.Ptr(),des.Size());
    _LIT(KTxtRead,"read: %S\n");
    console->Printf(KTxtRead,&des);
    //
    // [over]write 5 characters at character position 6
    //
    //
    _LIT(KTxtFolks,"folks");
    aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
    writeBuf(aBuf);
    //
    // delete characters
    //
    TInt startpos = 5;
    TInt length   = 6;
    startpos <<= 1;
    length   <<= 1;
    aBuf->Delete(startpos,length);
    writeBuf(aBuf);
    //
    // compress
    //
    aBuf->Compress();
    writeBuf(aBuf);
    }

LOCAL_C void writeBuf(CBufBase* aBuf)
    {
    //
    // print, segment by segment
    //
    _LIT(KTxtBuffer,"buffer:");
    console->Printf(KTxtBuffer);
    TInt  bufpos=0;
    TPtrC8 bufptr=aBuf->Ptr(bufpos);
    while (bufptr.Length()>0)
        {
        //
        // write out this segment of the buffer.
        // Note that the descriptor 'display' is built differently
        // for Unicode; it also assumes an even number of bytes;
        // this is valid because the granularity of the buffer is 4.
        //
        TPtrC display;
        display.Set((TUint16*)bufptr.Ptr(),(bufptr.Length()>>1));
        _LIT(KFormat1," [%d,%d] %S");
        console->Printf(KFormat1, bufpos, bufptr.Length(), &display);
        //
        // update position within the buffer
        // and the pointer-descriptor.
        //
        bufpos+=bufptr.Length();       // update position
        bufptr.Set(aBuf->Ptr(bufpos)); // should be the next segment
        }
    console->Printf(KTxtNewLine);
    }

    

LOCAL_C void showExpandL()
    {
    _LIT(KTxtShowExpand,"Showing ExpandL()\n");
    console->Printf(KTxtShowExpand);
    //
    // allocate the segmented buffer with 
    // a granularity of 4
    //
    CBufBase* buf=CBufSeg::NewL(4);
    CleanupStack::PushL(buf);
    //
    // insert text into buffer (12 UNICODE chars)
    //
    _LIT(KTxtHelloWorld,"Hello world!");
    buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
    //
    // reserve space for (or 16 16-bit chars (32 bytes))
    //
    buf->ExpandL(12,32); // expand by 32 - may fail
    _LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
    console->Printf(KTxtBufExpanded);
    writeBuf(buf);
    //
    // now insert 16 16-bit characters
    // one at time.
    //
    // This is CLEARLY INEFFICIENT but shows
    // how successive calls to Write() can be done
    // without risk of failing for lack of memory.
    //
    _LIT(KTxtAtoP,"abcdefghijklmnop");
    TBufC<16> source(KTxtAtoP);
    for (TInt i=0; i<16; i++)
        {
        buf->Write((i+6)<<1,(TAny*)&source[i],2);
        }
    _LIT(KTxtExpandedFilled,"expanded space filled: ");
    console->Printf(KTxtExpandedFilled);
    writeBuf(buf);
    //
    // now adjust size down to 18
    //
    buf->ResizeL(18);
    _LIT(KTxtResized,"resized:");
    console->Printf(KTxtResized);
    writeBuf(buf);
    //
    // destroy buffer
    //
    CleanupStack::PopAndDestroy();
    }

LOCAL_C void waitForKey()
    {
    _LIT(KTxtPressAnyKey,"[press any key]");
    console->Printf(KTxtPressAnyKey);
    console->Getch();
    console->Printf(KTxtNewLine);
    }

// BLD.INF
// Component description file
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

PRJ_MMPFILES

WriteToFile.mmp

// WriteToFile.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// using relative paths for source and userinclude directories

// No explicit capabilities required to run this.

// Please note that the 2nd UID listed here has not been
// allocated from the central pool of UI's and is not 
// guaranteed to be unique.
// The value is used for demonstration purposes only.
//

TARGET        WriteToFile.exe
TARGETTYPE    exe
UID           0 0x0FFFFF00
VENDORID      0x70000001

SOURCEPATH    .
SOURCE        WriteToFile.cpp

USERINCLUDE   .
USERINCLUDE   ..\..\CommonFramework
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib efsrv.lib

CAPABILITY    None


Description

The example shows how dynamic arrays are constructed and used. It shows a number of operations on a flat dynamic buffer and a segmented dynamic buffer.


Classes used


Security issues

The example requires no specific capabilities in order to run - and does not demonstrate any security issues.