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 #include "CommonFramework.h"
00032
00033
00034
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
00044
00045 LOCAL_C void doExampleL()
00046 {
00047
00048
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
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
00072
00073 showExpandL();
00074 }
00075
00076
00077 LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
00078 {
00079
00080
00081
00082 _LIT(KTxtHello,"Hello!");
00083 aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
00084 writeBuf(aBuf);
00085
00086
00087
00088
00089 _LIT(KTxtWorld," world");
00090 aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
00091 writeBuf(aBuf);
00092
00093
00094
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
00103
00104
00105 _LIT(KTxtFolks,"folks");
00106 aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
00107 writeBuf(aBuf);
00108
00109
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
00119
00120 aBuf->Compress();
00121 writeBuf(aBuf);
00122 }
00123
00124 LOCAL_C void writeBuf(CBufBase* aBuf)
00125 {
00126
00127
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
00137
00138
00139
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
00147
00148
00149 bufpos+=bufptr.Length();
00150 bufptr.Set(aBuf->Ptr(bufpos));
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
00163
00164
00165 CBufBase* buf=CBufSeg::NewL(4);
00166 CleanupStack::PushL(buf);
00167
00168
00169
00170 _LIT(KTxtHelloWorld,"Hello world!");
00171 buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
00172
00173
00174
00175 buf->ExpandL(12,32);
00176 _LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
00177 console->Printf(KTxtBufExpanded);
00178 writeBuf(buf);
00179
00180
00181
00182
00183
00184
00185
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
00198
00199 buf->ResizeL(18);
00200 _LIT(KTxtResized,"resized:");
00201 console->Printf(KTxtResized);
00202 writeBuf(buf);
00203
00204
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 }