examples/SysLibs/FileStores/WritePermFS2/WritePermFS2.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Example to demonstrate use of CPermanentFileStore
00015 // The Store root stream is an example of a persistent data structure
00016 // The internal representation is the class CItemArray
00017 //
00018 
00019 
00020 #include "CommonStreamStore.h"
00021 #include <s32file.h>
00022         
00023                                 // Create a permanent file store
00024                                 // and initialise its stream structure
00025 LOCAL_C void doMakeStoreL(const TDesC& aName);
00026 
00027                                 // Use the store, preserving the stream structure
00028 LOCAL_C void doUseStoreL(const TDesC& aName);
00029 
00030                                 // Update the data in the store
00031 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore);
00032 
00033                                 // Display the store contents
00034 LOCAL_C void doShowL(const TDesC& aHeading,CPersistentStore& aStore);
00035 
00036 
00037 typedef TBuf<100> TItem;
00038 
00039                                 // The main object's in-memory representation
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 // The file name, extension and path for the file store
00067 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WritePermFS2.dat");
00068 
00069 
00070 //  Do the example
00071 LOCAL_C void doExampleL()
00072     {
00073                             // make sure directory exists
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                                 // construct file store object - the file to contain the
00085                                 // the store replaces any existing file of the same name.
00086         CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00087                                 // Easy way to set the layout type
00088     store->SetTypeL(store->Layout());
00089                 
00090                                 // create the required stream for CItemArray
00091         TStreamId id=CItemArray::CreateL(*store);
00092 
00093                                 // make it the root
00094         store->SetRootL(id);
00095                         
00096                                 // Commit changes to the store
00097         store->CommitL();
00098 
00099                                 // Show contents of the store
00100         _LIT(KTxtStoreContent,"Store content ...");
00101         doShowL(KTxtStoreContent,*store);
00102 
00103                                 // Cleanup the store
00104         CleanupStack::PopAndDestroy();
00105         }
00106 
00107 
00108 LOCAL_C void doUseStoreL(const TDesC& aName)
00109         {
00110         TParse  filestorename;
00111         fsSession.Parse(aName,filestorename);
00112                                 // construct file store object - specifying the file
00113                                 // containing the store.
00114                                 // Do not need to specify the file store type, this is
00115                                 // specified by the file itself
00116         CFileStore* store = CFileStore::OpenL(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00117 
00118                                 // The standard form for using permanent file stores:
00119                                 // 1. The store object is not owned by the updating code
00120                                 // 2  Failure at any point during update, including the
00121                                 //    final commit, should result in Revert() being called
00122                                 //    on the store (before destruction).
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                                 // the store is not on the cleanup stack
00134         delete store;
00135         }
00136 
00137 
00138 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore)
00139         {
00140                                 // get the root stream into memory
00141         CItemArray* array=CItemArray::NewLC(aStore,aStore.Root());
00142 
00143                                 // Add some items
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                                 // Re-write the root stream with new data
00153         array->StoreL();
00154                                 // commit all changes
00155         aStore.CommitL();
00156 
00157         _LIT(KTxtAfterAdding,"After adding...");
00158         doShowL(KTxtAfterAdding,aStore);
00159 
00160                                 // remove an item
00161         array->RemoveItemL(1);          // " world!"
00162                                 // Re-write the root stream with new data
00163         array->StoreL();
00164                                 // commit all changes
00165         aStore.CommitL();
00166 
00167         _LIT(KTxtAfterRemoving,"After removing...");
00168         doShowL(KTxtAfterRemoving,aStore);
00169 
00170                                 // Add an item
00171         _LIT(KTxtCapWorld," WORLD!");
00172         item= KTxtCapWorld;
00173         array->AddItemL(item);
00174                                 // Re-write the root stream with new data
00175         array->StoreL();
00176                                 // Discard all changes since last store commit
00177         aStore.Revert();
00178 
00179         _LIT(KTxtAfterRevert,"After revert...");
00180         doShowL(KTxtAfterRevert,aStore);
00181 
00182                                 // array and aStore are not in snych after revert...
00183                                 // restore in-memory version to match store version
00184         array->RestoreL();
00185 
00186                                         // Add the item again
00187         array->AddItemL(item);
00188                                 // Re-write the root stream with new data
00189         array->StoreL();
00190                                 // commit all changes
00191         aStore.CommitL();
00192 
00193         _LIT(KTxtAfterCommit,"After commit...");
00194         doShowL(KTxtAfterCommit,aStore);
00195 
00196                                 // cleanup array
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                                 // Get an in-memory representation of the root stream
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                                 // for each item in the array
00217                                 //    get the item
00218                 TItem item;
00219                 array->GetItemL(item,ii);
00220                                 //    display the data
00221                 console->Printf(KFormatType2,&item);
00222                 }
00223                                 // cleanup the array
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 // create the stream representation of the class
00245         {
00246                                 // use a temporary CItemArray
00247         CItemArray* self=new(ELeave) CItemArray(aStore);
00248         CleanupStack::PushL(self);
00249                                 // construct object
00250         self->ConstructL();
00251                                 // create new stream
00252         RStoreWriteStream outstream;
00253         TStreamId id=outstream.CreateLC(aStore);
00254                                 // write  external rep
00255         self->ExternalizeL(outstream);
00256                                 // commit stream
00257         outstream.CommitL();
00258                                 // cleanup stream and temporary self
00259         CleanupStack::PopAndDestroy(2);
00260         return id;
00261         }
00262 
00263 CItemArray* CItemArray::NewLC(CStreamStore& aStore,TStreamId anId)
00264 // construct a CItemArray from persistent storage
00265         {
00266         CItemArray* self=new(ELeave) CItemArray(aStore);
00267         CleanupStack::PushL(self);
00268                                 // construct object
00269         self->ConstructL();
00270                                 // set the stream id for StoreL/RestoreL
00271         self->iMyId=anId;
00272                                 // restore the internal rep.
00273         self->RestoreL();
00274         return self;
00275         }
00276 
00277 void CItemArray::StoreL() const
00278 // replace external rep. with internal one
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 // replace internal rep with external one
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 // add item to the collection
00299         {
00300                                 // write external rep of item
00301         RStoreWriteStream outstream;
00302         TStreamId id=outstream.CreateLC(iStore);
00303         outstream<<anItem;
00304         outstream.CommitL();
00305         CleanupStack::PopAndDestroy();
00306                                 // add new stream id to the internal array
00307         iArray->AppendL(id);
00308         }
00309 
00310 void CItemArray::RemoveItemL(TInt anIndex)
00311 // remove an item from the collection
00312         {
00313                                 // remove the stream from the store
00314         iStore.DeleteL((*iArray)[anIndex]);
00315                                 // remove the entry from the internal array
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 // retrieve an item from the store
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                                 // stream out the array
00341         aStream<<*iArray;
00342         }
00343 
00344 void CItemArray::InternalizeL(RReadStream& aStream)
00345         {
00346                                 // stream in the array
00347         aStream>>*iArray;
00348         }

Generated by  doxygen 1.6.2