examples/SysLibs/FileStores/WriteDirectFS/WriteDirectFS.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 writing out a network of objects
00015 // to a direct file store and then loading them all back in.
00016 // Demonstrates the persistence of a direct file store and the idea
00017 // of the root stream. 
00018 // The example:
00019 // constructs three different types of object,
00020 // puts data into the objects,
00021 // displays the content of the objects,
00022 // creates a direct file store (replacing any existing direct 
00023 // file store of the same name),
00024 // writes the objects to a single stream,
00025 // makes this stream the root stream,
00026 // closes the store and deletes the objects (from memory),
00027 // re-opens the store and restores the objects from the root stream,
00028 // displays the contents of the restored objects
00029 // Notes:
00030 // The file name and extension of the direct file store is 
00031 // "WriteDirectFS.dat" and will be created in the data folder of the writable drive"
00032 //
00033 
00034 
00035 #include "CommonStreamStore.h"
00036 #include <s32file.h>
00037         
00038 
00039                                 // Constructs objects of type CClassA, CClassB and CClassC,
00040                                 // externalizes them to a direct file store and then destroys the
00041                                 // CClassA, CClassB and CClassC objects. 
00042 LOCAL_C void doMakeAndStoreL(const TDesC& aName);
00043 
00044                                 // Constructs "empty" CClassA, ClassB and CCLassC objects and
00045                                 // restores them from the direct file store.
00046 LOCAL_C void doRestoreL(const TDesC& aName);
00047 
00048                                 // Displays the contents of a CClassA object
00049 class CClassA;
00050 LOCAL_C void doShow(const TDesC& aHeading,const CClassA& anA);
00051 
00052                                 // Displays the contents of a CClassB object
00053 class CClassB;
00054 LOCAL_C void doShow(const TDesC& aHeading,const CClassB& aB);
00055 
00056                                 // Displays the contents of a CClassB object
00057 class CClassC;
00058 LOCAL_C void doShow(const TDesC& aHeading,const CClassC& aC);
00059 
00060                                 // definition of CClassA
00061 class CClassA : public CBase
00062         {
00063 public :
00064         static CClassA* NewLC();
00065         ~CClassA();
00066         void     SetTextL(const TDesC& aData);
00067         void     ExternalizeL(RWriteStream& aStream) const;
00068         void     InternalizeL(RReadStream& aStream);
00069 public :
00070         HBufC*   iVarBuffer;
00071         TInt     iIntValue;
00072         TUint    iUintValue;
00073         };
00074 
00075                                 // definition of CClassB
00076 class CClassB : public CBase
00077         {
00078 public :
00079         static CClassB* NewLC();
00080         void ExternalizeL(RWriteStream& aStream) const;
00081         void InternalizeL(RReadStream& aStream);
00082 public :
00083         TBuf<32> iFixBuffer;
00084         TUint    iUintValue;
00085         TInt     iIntValue;
00086         TReal    iRealValue;
00087         };
00088 
00089 // definition of CClassC
00090 
00091 #define KStdirectClassCGranularity 4
00092 
00093 class CClassC : public CBase
00094         {
00095 public :
00096         static CClassC* NewLC();
00097         ~CClassC();
00098         void     ConstructL();
00099         void     AddNumberToArrayL(TInt aValue);
00100         void     ExternalizeL(RWriteStream& aStream) const;
00101         void     InternalizeL(RReadStream& aStream);
00102 public :
00103         TBuf<32> iFixBuffer;
00104         CArrayFixFlat<TInt>* iArray;
00105         };
00106 
00107 
00108 
00109 // The file name, extension and path for the file store
00110         _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WriteDirectFS.dat");
00111 
00112                                 //  Do the example
00113 LOCAL_C void doExampleL()
00114     {
00115                             // make sure directory exists
00116         fsSession.MkDirAll(KFullNameOfFileStore);
00117         doMakeAndStoreL(KFullNameOfFileStore);
00118         doRestoreL(KFullNameOfFileStore);
00119         }
00120 
00121 LOCAL_C void doMakeAndStoreL(const TDesC& aName)
00122         {
00123                                 // Construct an object of type CClassA and put some 
00124                                 // data into it
00125         _LIT(KTxtForClassA,"Text for CClassA");
00126         CClassA* theA = CClassA::NewLC();
00127         theA->SetTextL(KTxtForClassA);  
00128         theA->iIntValue  = -1;
00129         theA->iUintValue = 2;
00130 
00131                                 // Construct an object of type CClassB and put some 
00132                                 // data into it
00133         _LIT(KTxtForClassB,"Text for CClassB");
00134         CClassB* theB = CClassB::NewLC();
00135         theB->iFixBuffer = KTxtForClassB;
00136         theB->iIntValue  = -3;
00137         theB->iUintValue = 4; 
00138         theB->iRealValue = 5.6;
00139 
00140                                 // Construct an object of type CClassC and put some 
00141                                 // data into it
00142         _LIT(KTxtForClassC,"Text for CClassC");
00143         CClassC* theC = CClassC::NewLC();
00144         theC->iFixBuffer = KTxtForClassC;       
00145         theC->AddNumberToArrayL(21);
00146         theC->AddNumberToArrayL(42);
00147         theC->AddNumberToArrayL(101);
00148 
00149                                 // Show contents of the CClassA object
00150         _LIT(KTxtClassAContent,"CClassA content ...");
00151         doShow(KTxtClassAContent,*theA);
00152 
00153                                 // Show contents of the CClassB object
00154         _LIT(KTxtClassBContent,"CClassB content ...");
00155         doShow(KTxtClassBContent,*theB);                                        
00156 
00157                                 // Show contents of the CClassC object
00158         _LIT(KTxtClassCContent,"CClassC content ...");
00159         doShow(KTxtClassCContent,*theC);                                        
00160                                 
00161                                 // Create (replace) the direct file store
00162         TParse  filestorename;
00163         fsSession.Parse(aName,filestorename);
00164         CFileStore* store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00165 
00166                                 // Must say what kind of file store.
00167         store->SetTypeL(KDirectFileStoreLayoutUid);
00168         
00169                                 // Construct the output stream.
00170         RStoreWriteStream outstream;
00171         TStreamId id = outstream.CreateLC(*store);
00172 
00173                                 // Stream out the CClassA object, the
00174                                 // CClassB object and the CClassC object
00175         outstream  << *theA << *theB << *theC; 
00176 
00177                                 // Equally we could have done:
00178                                 //              outstream << *theA;
00179                                 //              outstream << *theB;
00180                                 //      outstream << *theC;
00181                                 // to the same effect
00182                                 
00183                                 // The order in which the objects are written out is
00184                                 // arbitrary, but it is important to note that 
00185                                 // THE ORDER IN WHICH THEY ARE WRITTEN OUT IS THE SAME
00186                                 // AS THE ORDER IN WHICH THEY WILL BE READ IN LATER !
00187                                 
00188                                 // Commit changes to the stream
00189         outstream.CommitL();
00190 
00191                     // Cleanup the stream object
00192     CleanupStack::PopAndDestroy();
00193 
00194                                 // Set this stream id as the root
00195         store->SetRootL(id);
00196 
00197                                 // Commit changes to the store
00198         store->CommitL();
00199 
00200                                 // Destroy the direct file store object (closes the file),
00201                                 // Destroy the CClassC object,
00202                                 // Destroy the CClassB object,
00203                                 // Destroy the CClassA object.
00204         CleanupStack::PopAndDestroy(4); 
00205         }
00206 
00207 LOCAL_C void doRestoreL(const TDesC& aName)
00208         {
00209                                 // Construct "empty" CClassA, CClassB and CClassC objects
00210         CClassA* theA = CClassA::NewLC();
00211         CClassB* theB = CClassB::NewLC();
00212         CClassC* theC = CClassC::NewLC();
00213                                         
00214                                 // Open the direct file store
00215         TParse  filestorename;
00216         fsSession.Parse(aName,filestorename);
00217         CFileStore* store = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00218 
00219                                 // Construct and open the root stream which 
00220                                 // contains the representation of our objects.
00221         RStoreReadStream instream;
00222         instream.OpenLC(*store,store->Root());
00223         
00224                                 // Stream in the CClassA object, the
00225                                 // CClassB object and the CClassC object
00226         instream  >> *theA >> *theB >> *theC; 
00227 
00228                                 // Equally we could have done:
00229                                 //              instream >> *theA;
00230                                 //              instream >> *theB;
00231                                 //      instream >> *theC;
00232                                 // to the same effect
00233 
00234                                 // Cleanup the stream object
00235         CleanupStack::PopAndDestroy();
00236 
00237                                 // Show restored contents of the CClassA object
00238         _LIT(KTxtRestoredCClassA,"Restored CClassA content ...");
00239         doShow(KTxtRestoredCClassA,*theA);
00240 
00241                                 // Show restored contents of the CClassB object
00242         _LIT(KTxtRestoredCClassB,"Restored CClassB content ...");
00243         doShow(KTxtRestoredCClassB,*theB);                                      
00244 
00245                                 // Show restored contents of the CClassC object
00246         _LIT(KTxtRestoredCClassC,"Restored CClassC content ...");
00247         doShow(KTxtRestoredCClassC,*theC);      
00248                                 
00249                                 // Destroy the direct file store object (closes the file) 
00250                                 // Destroy the CClassC object, 
00251                                 // Destroy the CClassB object, 
00252                                 // Destroy the CClassA object, 
00253         CleanupStack::PopAndDestroy(4);
00254         }
00255         
00256                                                                         
00257 _LIT(KTxtNewLine,"\n");
00258 _LIT(KFormatType1,"\n%S, ");
00259 _LIT(KFormatType2,"%d, ");
00260 _LIT(KFormatType3,"%u  ");
00261 _LIT(KFormatType4,"%u, ");
00262 _LIT(KFormatType5,"%f  ");                                      
00263                         
00264 LOCAL_C void doShow(const TDesC& aHeading,const CClassA& anA)
00265         {
00266         console->Printf(KTxtNewLine);
00267         console->Printf(aHeading);
00268         console->Printf(KFormatType1,anA.iVarBuffer);
00269         console->Printf(KFormatType2,anA.iIntValue);
00270         console->Printf(KFormatType3,anA.iUintValue);
00271         console->Printf(KTxtNewLine);
00272         }
00273 
00274 LOCAL_C void doShow(const TDesC& aHeading,const CClassB& aB)
00275         {
00276         console->Printf(KTxtNewLine);
00277         console->Printf(aHeading);
00278         console->Printf(KFormatType1,&aB.iFixBuffer);
00279         console->Printf(KFormatType2,aB.iIntValue);
00280         console->Printf(KFormatType4,aB.iUintValue);
00281         console->Printf(KFormatType5,aB.iRealValue);
00282         console->Printf(KTxtNewLine);
00283         }
00284 
00285 _LIT(KTxtNoArrayItems,"<No array items>");
00286 _LIT(KTxtColonsep,": ");
00287 _LIT(KTxtCommasep,", ");
00288 _LIT(KFormatType6,"%d array item(s)");
00289 _LIT(KFormatType7,"%S%d");
00290 
00291 LOCAL_C void doShow(const TDesC& aHeading,const CClassC& aC)
00292         {
00293         console->Printf(KTxtNewLine);
00294         console->Printf(aHeading);
00295         console->Printf(KFormatType1,&aC.iFixBuffer);
00296         
00297         TInt count = aC.iArray->Count();
00298         if (!count)
00299                 console->Printf(KTxtNoArrayItems);
00300         else
00301                 {
00302                 TPtrC   ptr;
00303                 ptr.Set(KTxtColonsep);
00304                 console->Printf(KFormatType6,count);
00305                 for (TInt index = 0; index < count; index++)
00306                         {
00307                         console->Printf(KFormatType7,&ptr,(*aC.iArray)[index]);
00308                         ptr.Set(KTxtCommasep);          
00309                         }
00310                 }
00311         console->Printf(KTxtNewLine);
00312         }
00313 
00314 //***************************************************************
00315 //***************************************************************
00316 CClassA* CClassA::NewLC()
00317         {
00318         CClassA* self = new (ELeave) CClassA;
00319         CleanupStack::PushL(self);
00320         return self;
00321         }
00322         
00323 CClassA::~CClassA()
00324         {
00325         delete iVarBuffer;
00326         }
00327 
00328 void CClassA::SetTextL(const TDesC& aData)
00329         {
00330         iVarBuffer = aData.AllocL();    
00331         }
00332 
00333 void CClassA::ExternalizeL(RWriteStream& aStream) const
00334         {
00335         aStream.WriteInt32L(iVarBuffer->Des().MaxLength());
00336         aStream << *iVarBuffer;
00337         aStream.WriteInt32L(iIntValue);
00338         aStream.WriteUint32L(iUintValue);
00339         }  
00340  
00341 void CClassA::InternalizeL(RReadStream& aStream)
00342         {
00343         TInt maxlen;
00344         maxlen     = aStream.ReadInt32L();
00345         iVarBuffer = HBufC::NewL(aStream,maxlen);
00346         iIntValue  = aStream.ReadInt32L();
00347         iUintValue = aStream.ReadUint32L();
00348         }  
00349                 
00350 
00351 //***************************************************************
00352 //***************************************************************
00353 CClassB* CClassB::NewLC()
00354         {
00355         CClassB* self = new (ELeave) CClassB;
00356         CleanupStack::PushL(self);
00357         return self;
00358         }
00359 
00360 void CClassB::ExternalizeL(RWriteStream& aStream) const
00361         {
00362         aStream << iFixBuffer;
00363         aStream.WriteInt32L(iIntValue);
00364         aStream.WriteUint32L(iUintValue);
00365         aStream.WriteReal64L(iRealValue);
00366         }  
00367  
00368 void CClassB::InternalizeL(RReadStream& aStream)
00369         {
00370         aStream >> iFixBuffer;
00371         iIntValue  = aStream.ReadInt32L();
00372         iUintValue = aStream.ReadUint32L();
00373         iRealValue = aStream.ReadReal64L();
00374         }  
00375 
00376 
00377 //***************************************************************
00378 //***************************************************************
00379 CClassC* CClassC::NewLC()
00380         {
00381         CClassC* self = new (ELeave) CClassC;
00382         CleanupStack::PushL(self);
00383         self->ConstructL();
00384         return self;
00385         }
00386         
00387 void CClassC::ConstructL()
00388         {
00389         iArray = new (ELeave) CArrayFixFlat<TInt>(KStdirectClassCGranularity);
00390         }
00391 
00392 void CClassC::AddNumberToArrayL(TInt aValue)
00393         {
00394         iArray->AppendL(aValue);
00395         }
00396 
00397 CClassC::~CClassC()
00398         {
00399         delete iArray;
00400         }
00401 
00402 void CClassC::ExternalizeL(RWriteStream& aStream) const
00403         {
00404         aStream << iFixBuffer;
00405 
00406         TInt count;
00407         count = iArray->Count();
00408         aStream.WriteInt32L(count);
00409         for (TInt index = 0; index < count; index++)
00410                 aStream.WriteInt32L((*iArray)[index]);
00411         }  
00412  
00413 void CClassC::InternalizeL(RReadStream& aStream)
00414         {
00415         aStream >> iFixBuffer;
00416 
00417         TInt count;
00418         count  = aStream.ReadInt32L();
00419         
00420         for (TInt index = 0; index < count; index++)
00421                 iArray->AppendL(aStream.ReadInt32L());
00422         }  
00423                 
00424 
00425                 
00426 
00427 
00428 
00429         
00430         

Generated by  doxygen 1.6.2