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