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 simple use of Streaming 00015 // 00016 00017 00018 #include "CommonStreamStore.h" 00019 #include <s32file.h> 00020 00021 // Constructs a TSimple object and 00022 // externalizes it to a single stream. 00023 static void doMakeAndExternalizeL(const TDesC& aName); 00024 00025 // Internalizes a TSimple object from 00026 // the stream 00027 static void doInternalizeL(const TDesC& aName); 00028 00029 // Displays content of a TSimple object 00030 class TSimple; 00031 static void doShow(const TDesC& aHeading,const TSimple& theSimple); 00032 00033 // enumeration used by the TSimple class 00034 enum TXxx {EX1,EX2,EX3}; 00035 00036 // definition of the TSimple class 00037 class TSimple 00038 { 00039 public : 00040 void ExternalizeL(RWriteStream& aStream) const; 00041 void InternalizeL(RReadStream& aStream); 00042 public : 00043 TXxx iTheEnum; 00044 TBuf<32> iBuffer; 00045 TInt iIntValue; 00046 TUint iUintValue; 00047 TReal iRealValue; 00048 }; 00049 00050 00051 // The file name, extension and path for the file store 00052 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\SimpleClassToSimpleStream.dat"); 00053 00054 00055 // Do the example 00056 static void doExampleL() 00057 { 00058 // make sure directory exists 00059 fsSession.MkDirAll(KFullNameOfFileStore); 00060 doMakeAndExternalizeL(KFullNameOfFileStore); 00061 doInternalizeL(KFullNameOfFileStore); 00062 } 00063 00064 static void doMakeAndExternalizeL(const TDesC& aName) 00065 { 00066 TParse filestorename; 00067 fsSession.Parse(aName,filestorename); 00068 // construct file store object - the file to contain the 00069 // the store replaces any existing file of the same name. 00070 CFileStore* store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite); 00071 00072 // Must say what kind of file store 00073 store->SetTypeL(KDirectFileStoreLayoutUid); 00074 00075 // Construct an object of type TSimple and put some 00076 // data into it 00077 _LIT(KTxtSomeText,"Some text"); 00078 TSimple thesimple; 00079 thesimple.iTheEnum = EX3; 00080 thesimple.iBuffer = KTxtSomeText; 00081 thesimple.iIntValue = -1; 00082 thesimple.iUintValue = 2; 00083 thesimple.iRealValue = 3.4; 00084 00085 // Show contents of the TSimple object 00086 _LIT(KTxtTSimpleContent,"TSimple content ..."); 00087 doShow(KTxtTSimpleContent,thesimple); 00088 00089 // Construct and create the output stream 00090 // object. 00091 // The stream id (there is only one) will 00092 // be saved (later) in the store as the 00093 // root stream id 00094 RStoreWriteStream outstream; 00095 TStreamId id = outstream.CreateLC(*store); 00096 00097 // Stream out the TSimple object 00098 outstream << thesimple; 00099 00100 // Commit changes to the stream 00101 outstream.CommitL(); 00102 00103 // Cleanup the stream object 00104 CleanupStack::PopAndDestroy(); 00105 00106 // Set the stream id as the root 00107 store->SetRootL(id); 00108 00109 // Commit changes to the store 00110 store->CommitL(); 00111 00112 // destroy the store object (this also closes 00113 // the file containing the store) 00114 CleanupStack::PopAndDestroy(); 00115 } 00116 00117 static void doInternalizeL(const TDesC& aName) 00118 { 00119 TParse filestorename; 00120 fsSession.Parse(aName,filestorename); 00121 // construct file store object - specifying the file 00122 // containing the store. 00123 CFileStore* store = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead); 00124 00125 // Construct and open the input stream object. We want 00126 // to access the root stream from the store 00127 // in this example. 00128 RStoreReadStream instream; 00129 instream.OpenLC(*store,store->Root()); 00130 00131 // Stream in the TSimple object 00132 TSimple thesimple; 00133 instream >> thesimple; 00134 00135 // Cleanup the stream object 00136 CleanupStack::PopAndDestroy(); 00137 00138 // destroy the store object (this also closes the file 00139 // containing the store) 00140 CleanupStack::PopAndDestroy(); 00141 00142 // Show contents of the TSimple object 00143 _LIT(KTxtRestoredTSimpleContent,"Restored TSimple content ..."); 00144 doShow(KTxtRestoredTSimpleContent,thesimple); 00145 } 00146 00147 _LIT(KTxtNewLine,"\n"); 00148 _LIT(KFormatType1,"\n%d"); 00149 _LIT(KFormatType2,"\n%S"); 00150 _LIT(KFormatType3,"\n%u"); 00151 _LIT(KFormatType4,"\n%f"); 00152 00153 static void doShow(const TDesC& aHeading,const TSimple& aSimple) 00154 { 00155 console->Printf(KTxtNewLine); 00156 console->Printf(aHeading); 00157 console->Printf(KFormatType1,aSimple.iTheEnum); 00158 console->Printf(KFormatType2,&aSimple.iBuffer); 00159 console->Printf(KFormatType1,aSimple.iIntValue); 00160 console->Printf(KFormatType3,aSimple.iUintValue); 00161 console->Printf(KFormatType4,aSimple.iRealValue); 00162 console->Printf(KTxtNewLine); 00163 } 00164 00165 //*************************************************************** 00166 //*************************************************************** 00167 00168 // Explicit versions of the << and >> operators written to handle the 00169 // enumerator type TXxx 00170 00171 RWriteStream& operator<<(RWriteStream& aStream, const TXxx& anXxx) 00172 { 00173 aStream.WriteInt8L(anXxx); 00174 return aStream; 00175 } 00176 00177 RReadStream& operator>>(RReadStream& aStream, TXxx& anXxx) 00178 { 00179 anXxx = TXxx(aStream.ReadInt8L()); 00180 return aStream; 00181 } 00182 00183 //*************************************************************** 00184 //*************************************************************** 00185 00186 void TSimple::ExternalizeL(RWriteStream& aStream) const 00187 { 00188 aStream << iTheEnum; 00189 aStream << iBuffer; 00190 aStream.WriteInt32L(iIntValue); 00191 aStream.WriteUint32L(iUintValue); 00192 aStream.WriteReal64L(iRealValue); 00193 } 00194 00195 void TSimple::InternalizeL(RReadStream& aStream) 00196 { 00197 aStream >> iTheEnum; 00198 aStream >> iBuffer; 00199 iIntValue = aStream.ReadInt32L(); 00200 iUintValue = aStream.ReadUint32L(); 00201 iRealValue = aStream.ReadReal64L(); 00202 } 00203 00204 00205 00206 00207 00208
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.