00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "CommonFramework.h"
00017
00018
00019
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
00029
00030 LOCAL_C void doExampleL()
00031 {
00032
00033
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
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
00057
00058 showExpandL();
00059 }
00060
00061
00062 LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
00063 {
00064
00065
00066
00067 _LIT(KTxtHello,"Hello!");
00068 aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
00069 writeBuf(aBuf);
00070
00071
00072
00073
00074 _LIT(KTxtWorld," world");
00075 aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
00076 writeBuf(aBuf);
00077
00078
00079
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
00088
00089
00090 _LIT(KTxtFolks,"folks");
00091 aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
00092 writeBuf(aBuf);
00093
00094
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
00104
00105 aBuf->Compress();
00106 writeBuf(aBuf);
00107 }
00108
00109 LOCAL_C void writeBuf(CBufBase* aBuf)
00110 {
00111
00112
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
00122
00123
00124
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
00132
00133
00134 bufpos+=bufptr.Length();
00135 bufptr.Set(aBuf->Ptr(bufpos));
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
00148
00149
00150 CBufBase* buf=CBufSeg::NewL(4);
00151 CleanupStack::PushL(buf);
00152
00153
00154
00155 _LIT(KTxtHelloWorld,"Hello world!");
00156 buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
00157
00158
00159
00160 buf->ExpandL(12,32);
00161 _LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
00162 console->Printf(KTxtBufExpanded);
00163 writeBuf(buf);
00164
00165
00166
00167
00168
00169
00170
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
00183
00184 buf->ResizeL(18);
00185 _LIT(KTxtResized,"resized:");
00186 console->Printf(KTxtResized);
00187 writeBuf(buf);
00188
00189
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 }