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

Generated by  doxygen 1.6.2