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
00032
00033
00034
00035
00036 #include "CommonStreamStore.h"
00037 #include <s32file.h>
00038
00039
00040
00041 LOCAL_C void doMakeStoreL(const TDesC& aName);
00042
00043
00044 LOCAL_C void doUseStoreL(const TDesC& aName);
00045
00046
00047 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore);
00048
00049
00050 LOCAL_C void doShowL(const TDesC& aHeading,CPersistentStore& aStore);
00051
00052
00053 typedef TBuf<100> TItem;
00054
00055
00056 class CItemArray : public CBase
00057 {
00058 public:
00059 static TStreamId CreateL(CStreamStore& aStore);
00060
00061 ~CItemArray();
00062 static CItemArray* NewLC(CStreamStore& aStore,TStreamId anId);
00063 void RestoreL();
00064 void StoreL() const;
00065 void ExternalizeL(RWriteStream& aStream) const;
00066
00067 void AddItemL(const TItem& anItem);
00068 void RemoveItemL(TInt anIndex);
00069 TInt Count() const;
00070 void GetItemL(TItem& anItem,TInt anIndex) const;
00071 protected:
00072 CItemArray(CStreamStore& aStore);
00073 void ConstructL();
00074 void InternalizeL(RReadStream& aStream);
00075 private:
00076 CStreamStore& iStore;
00077 TStreamId iMyId;
00078 CArrayFixFlat<TStreamId>* iArray;
00079 };
00080
00081
00082
00083 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WritePermFS2.dat");
00084
00085
00086
00087 LOCAL_C void doExampleL()
00088 {
00089
00090 fsSession.MkDirAll(KFullNameOfFileStore);
00091 doMakeStoreL(KFullNameOfFileStore);
00092 doUseStoreL(KFullNameOfFileStore);
00093 }
00094
00095
00096 LOCAL_C void doMakeStoreL(const TDesC& aName)
00097 {
00098 TParse filestorename;
00099 fsSession.Parse(aName,filestorename);
00100
00101
00102 CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00103
00104 store->SetTypeL(store->Layout());
00105
00106
00107 TStreamId id=CItemArray::CreateL(*store);
00108
00109
00110 store->SetRootL(id);
00111
00112
00113 store->CommitL();
00114
00115
00116 _LIT(KTxtStoreContent,"Store content ...");
00117 doShowL(KTxtStoreContent,*store);
00118
00119
00120 CleanupStack::PopAndDestroy();
00121 }
00122
00123
00124 LOCAL_C void doUseStoreL(const TDesC& aName)
00125 {
00126 TParse filestorename;
00127 fsSession.Parse(aName,filestorename);
00128
00129
00130
00131
00132 CFileStore* store = CFileStore::OpenL(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00133
00134
00135
00136
00137
00138
00139
00140 _LIT(KTxtErrorOccurred,"\n** Error %d occured during store update");
00141
00142 TRAPD(error,doUpdateStoreL(*store));
00143 if (error!=KErrNone)
00144 {
00145 store->Revert();
00146 console->Printf(KTxtErrorOccurred);
00147 }
00148
00149
00150 delete store;
00151 }
00152
00153
00154 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore)
00155 {
00156
00157 CItemArray* array=CItemArray::NewLC(aStore,aStore.Root());
00158
00159
00160 _LIT(KTxtHello,"hello");
00161 _LIT(KTxtWorld," world!");
00162
00163 TItem item;
00164 item = KTxtHello;
00165 array->AddItemL(item);
00166 item = KTxtWorld;
00167 array->AddItemL(item);
00168
00169 array->StoreL();
00170
00171 aStore.CommitL();
00172
00173 _LIT(KTxtAfterAdding,"After adding...");
00174 doShowL(KTxtAfterAdding,aStore);
00175
00176
00177 array->RemoveItemL(1);
00178
00179 array->StoreL();
00180
00181 aStore.CommitL();
00182
00183 _LIT(KTxtAfterRemoving,"After removing...");
00184 doShowL(KTxtAfterRemoving,aStore);
00185
00186
00187 _LIT(KTxtCapWorld," WORLD!");
00188 item= KTxtCapWorld;
00189 array->AddItemL(item);
00190
00191 array->StoreL();
00192
00193 aStore.Revert();
00194
00195 _LIT(KTxtAfterRevert,"After revert...");
00196 doShowL(KTxtAfterRevert,aStore);
00197
00198
00199
00200 array->RestoreL();
00201
00202
00203 array->AddItemL(item);
00204
00205 array->StoreL();
00206
00207 aStore.CommitL();
00208
00209 _LIT(KTxtAfterCommit,"After commit...");
00210 doShowL(KTxtAfterCommit,aStore);
00211
00212
00213 CleanupStack::PopAndDestroy();
00214 }
00215
00216 _LIT(KTxtNewLine,"\n");
00217 _LIT(KFormatType1,"\n%d item(s):");
00218 _LIT(KFormatType2,"\n %S");
00219 _LIT(KFormatType3,"\n[any key to continue]\n");
00220
00221
00222 LOCAL_C void doShowL(const TDesC& aHeading,CPersistentStore& aStore)
00223 {
00224
00225 CItemArray* array=CItemArray::NewLC(aStore,aStore.Root());
00226
00227 console->Printf(KTxtNewLine);
00228 console->Printf(aHeading);
00229 console->Printf(KFormatType1,array->Count());
00230 for (TInt ii=0;ii<array->Count();++ii)
00231 {
00232
00233
00234 TItem item;
00235 array->GetItemL(item,ii);
00236
00237 console->Printf(KFormatType2,&item);
00238 }
00239
00240 CleanupStack::PopAndDestroy();
00241
00242 console->Printf(KFormatType3);
00243 console->Getch();
00244 }
00245
00246
00247
00248
00249
00250 CItemArray::~CItemArray()
00251 {
00252 delete iArray;
00253 }
00254
00255 CItemArray::CItemArray(CStreamStore& aStore)
00256 : iStore(aStore)
00257 {}
00258
00259 TStreamId CItemArray::CreateL(CStreamStore& aStore)
00260
00261 {
00262
00263 CItemArray* self=new(ELeave) CItemArray(aStore);
00264 CleanupStack::PushL(self);
00265
00266 self->ConstructL();
00267
00268 RStoreWriteStream outstream;
00269 TStreamId id=outstream.CreateLC(aStore);
00270
00271 self->ExternalizeL(outstream);
00272
00273 outstream.CommitL();
00274
00275 CleanupStack::PopAndDestroy(2);
00276 return id;
00277 }
00278
00279 CItemArray* CItemArray::NewLC(CStreamStore& aStore,TStreamId anId)
00280
00281 {
00282 CItemArray* self=new(ELeave) CItemArray(aStore);
00283 CleanupStack::PushL(self);
00284
00285 self->ConstructL();
00286
00287 self->iMyId=anId;
00288
00289 self->RestoreL();
00290 return self;
00291 }
00292
00293 void CItemArray::StoreL() const
00294
00295 {
00296 RStoreWriteStream outstream;
00297 outstream.ReplaceLC(iStore,iMyId);
00298 ExternalizeL(outstream);
00299 outstream.CommitL();
00300 CleanupStack::PopAndDestroy();
00301 }
00302
00303 void CItemArray::RestoreL()
00304
00305 {
00306 iArray->Reset();
00307 RStoreReadStream instream;
00308 instream.OpenLC(iStore,iMyId);
00309 InternalizeL(instream);
00310 CleanupStack::PopAndDestroy();
00311 }
00312
00313 void CItemArray::AddItemL(const TItem& anItem)
00314
00315 {
00316
00317 RStoreWriteStream outstream;
00318 TStreamId id=outstream.CreateLC(iStore);
00319 outstream<<anItem;
00320 outstream.CommitL();
00321 CleanupStack::PopAndDestroy();
00322
00323 iArray->AppendL(id);
00324 }
00325
00326 void CItemArray::RemoveItemL(TInt anIndex)
00327
00328 {
00329
00330 iStore.DeleteL((*iArray)[anIndex]);
00331
00332 iArray->Delete(anIndex);
00333 }
00334
00335 TInt CItemArray::Count() const
00336 {
00337 return iArray->Count();
00338 }
00339
00340 void CItemArray::GetItemL(TItem& anItem,TInt anIndex) const
00341
00342 {
00343 RStoreReadStream instream;
00344 instream.OpenLC(iStore,(*iArray)[anIndex]);
00345 instream>>anItem;
00346 CleanupStack::PopAndDestroy();
00347 }
00348
00349 void CItemArray::ConstructL()
00350 {
00351 iArray=new(ELeave) CArrayFixFlat<TStreamId>(8);
00352 }
00353
00354 void CItemArray::ExternalizeL(RWriteStream& aStream) const
00355 {
00356
00357 aStream<<*iArray;
00358 }
00359
00360 void CItemArray::InternalizeL(RReadStream& aStream)
00361 {
00362
00363 aStream>>*iArray;
00364 }