examples/SysLibs/FileStores/WritePermFS2/WritePermFS2.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 Example to demonstrate use of CPermanentFileStore
00029 The Store root stream is an example of a persistent data structure
00030 The internal representation is the class CItemArray
00031 */
00032 
00033 
00034 
00035 
00036 #include "CommonStreamStore.h"
00037 #include <s32file.h>
00038         
00039                                 // Create a permanent file store
00040                                 // and initialise its stream structure
00041 LOCAL_C void doMakeStoreL(const TDesC& aName);
00042 
00043                                 // Use the store, preserving the stream structure
00044 LOCAL_C void doUseStoreL(const TDesC& aName);
00045 
00046                                 // Update the data in the store
00047 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore);
00048 
00049                                 // Display the store contents
00050 LOCAL_C void doShowL(const TDesC& aHeading,CPersistentStore& aStore);
00051 
00052 
00053 typedef TBuf<100> TItem;
00054 
00055                                 // The main object's in-memory representation
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 // The file name, extension and path for the file store
00083 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WritePermFS2.dat");
00084 
00085 
00086 //  Do the example
00087 LOCAL_C void doExampleL()
00088     {
00089                             // make sure directory exists
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                                 // construct file store object - the file to contain the
00101                                 // the store replaces any existing file of the same name.
00102         CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00103                                 // Easy way to set the layout type
00104     store->SetTypeL(store->Layout());
00105                 
00106                                 // create the required stream for CItemArray
00107         TStreamId id=CItemArray::CreateL(*store);
00108 
00109                                 // make it the root
00110         store->SetRootL(id);
00111                         
00112                                 // Commit changes to the store
00113         store->CommitL();
00114 
00115                                 // Show contents of the store
00116         _LIT(KTxtStoreContent,"Store content ...");
00117         doShowL(KTxtStoreContent,*store);
00118 
00119                                 // Cleanup the store
00120         CleanupStack::PopAndDestroy();
00121         }
00122 
00123 
00124 LOCAL_C void doUseStoreL(const TDesC& aName)
00125         {
00126         TParse  filestorename;
00127         fsSession.Parse(aName,filestorename);
00128                                 // construct file store object - specifying the file
00129                                 // containing the store.
00130                                 // Do not need to specify the file store type, this is
00131                                 // specified by the file itself
00132         CFileStore* store = CFileStore::OpenL(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00133 
00134                                 // The standard form for using permanent file stores:
00135                                 // 1. The store object is not owned by the updating code
00136                                 // 2  Failure at any point during update, including the
00137                                 //    final commit, should result in Revert() being called
00138                                 //    on the store (before destruction).
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                                 // the store is not on the cleanup stack
00150         delete store;
00151         }
00152 
00153 
00154 LOCAL_C void doUpdateStoreL(CPersistentStore& aStore)
00155         {
00156                                 // get the root stream into memory
00157         CItemArray* array=CItemArray::NewLC(aStore,aStore.Root());
00158 
00159                                 // Add some items
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                                 // Re-write the root stream with new data
00169         array->StoreL();
00170                                 // commit all changes
00171         aStore.CommitL();
00172 
00173         _LIT(KTxtAfterAdding,"After adding...");
00174         doShowL(KTxtAfterAdding,aStore);
00175 
00176                                 // remove an item
00177         array->RemoveItemL(1);          // " world!"
00178                                 // Re-write the root stream with new data
00179         array->StoreL();
00180                                 // commit all changes
00181         aStore.CommitL();
00182 
00183         _LIT(KTxtAfterRemoving,"After removing...");
00184         doShowL(KTxtAfterRemoving,aStore);
00185 
00186                                 // Add an item
00187         _LIT(KTxtCapWorld," WORLD!");
00188         item= KTxtCapWorld;
00189         array->AddItemL(item);
00190                                 // Re-write the root stream with new data
00191         array->StoreL();
00192                                 // Discard all changes since last store commit
00193         aStore.Revert();
00194 
00195         _LIT(KTxtAfterRevert,"After revert...");
00196         doShowL(KTxtAfterRevert,aStore);
00197 
00198                                 // array and aStore are not in snych after revert...
00199                                 // restore in-memory version to match store version
00200         array->RestoreL();
00201 
00202                                         // Add the item again
00203         array->AddItemL(item);
00204                                 // Re-write the root stream with new data
00205         array->StoreL();
00206                                 // commit all changes
00207         aStore.CommitL();
00208 
00209         _LIT(KTxtAfterCommit,"After commit...");
00210         doShowL(KTxtAfterCommit,aStore);
00211 
00212                                 // cleanup array
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                                 // Get an in-memory representation of the root stream
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                                 // for each item in the array
00233                                 //    get the item
00234                 TItem item;
00235                 array->GetItemL(item,ii);
00236                                 //    display the data
00237                 console->Printf(KFormatType2,&item);
00238                 }
00239                                 // cleanup the array
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 // create the stream representation of the class
00261         {
00262                                 // use a temporary CItemArray
00263         CItemArray* self=new(ELeave) CItemArray(aStore);
00264         CleanupStack::PushL(self);
00265                                 // construct object
00266         self->ConstructL();
00267                                 // create new stream
00268         RStoreWriteStream outstream;
00269         TStreamId id=outstream.CreateLC(aStore);
00270                                 // write  external rep
00271         self->ExternalizeL(outstream);
00272                                 // commit stream
00273         outstream.CommitL();
00274                                 // cleanup stream and temporary self
00275         CleanupStack::PopAndDestroy(2);
00276         return id;
00277         }
00278 
00279 CItemArray* CItemArray::NewLC(CStreamStore& aStore,TStreamId anId)
00280 // construct a CItemArray from persistent storage
00281         {
00282         CItemArray* self=new(ELeave) CItemArray(aStore);
00283         CleanupStack::PushL(self);
00284                                 // construct object
00285         self->ConstructL();
00286                                 // set the stream id for StoreL/RestoreL
00287         self->iMyId=anId;
00288                                 // restore the internal rep.
00289         self->RestoreL();
00290         return self;
00291         }
00292 
00293 void CItemArray::StoreL() const
00294 // replace external rep. with internal one
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 // replace internal rep with external one
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 // add item to the collection
00315         {
00316                                 // write external rep of item
00317         RStoreWriteStream outstream;
00318         TStreamId id=outstream.CreateLC(iStore);
00319         outstream<<anItem;
00320         outstream.CommitL();
00321         CleanupStack::PopAndDestroy();
00322                                 // add new stream id to the internal array
00323         iArray->AppendL(id);
00324         }
00325 
00326 void CItemArray::RemoveItemL(TInt anIndex)
00327 // remove an item from the collection
00328         {
00329                                 // remove the stream from the store
00330         iStore.DeleteL((*iArray)[anIndex]);
00331                                 // remove the entry from the internal array
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 // retrieve an item from the store
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                                 // stream out the array
00357         aStream<<*iArray;
00358         }
00359 
00360 void CItemArray::InternalizeL(RReadStream& aStream)
00361         {
00362                                 // stream in the array
00363         aStream>>*iArray;
00364         }

Generated by  doxygen 1.6.2