00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CommonFramework.h"
00020
00021
00022 _LIT(KFileName,"TAnyandRObjects2.dat");
00023
00024
00025 _LIT(KTestData,"Test data for TAnyandRObjects2\n");
00026
00027
00028 #include <f32file.h>
00029 #include "EUHEXDMP.H"
00030
00031
00033
00034
00035
00036
00037
00038
00040 class RFileWithCleanup : public RFile
00041 {
00042 private:
00043 static void Cleanup(TAny *aPtr);
00044 public:
00045 operator TCleanupItem();
00046 public:
00047 void OpenL(RFs &aFs,const TDesC &aName,TUint aMode);
00048 void OpenLC(RFs &aFs,const TDesC &aName,TUint aMode);
00049 };
00050
00051
00053
00054
00055
00057 void RFileWithCleanup::Cleanup(TAny *aPtr)
00058 {
00059 _LIT(KMsgDoingCleanup,"Doing cleanup of file.\n");
00060 console->Printf(KMsgDoingCleanup);
00061
00062 ((RFileWithCleanup *)aPtr)->Close();
00063 }
00064
00065 RFileWithCleanup::operator TCleanupItem()
00066 {
00067 return TCleanupItem(Cleanup,this);
00068 }
00069
00070 void RFileWithCleanup::OpenL(RFs &aFs,const TDesC &aName,TUint aMode)
00071 {
00072 User::LeaveIfError(RFile::Open(aFs,aName,aMode));
00073 }
00074
00075 void RFileWithCleanup::OpenLC(RFs &aFs,const TDesC &aName,TUint aMode)
00076 {
00077 OpenL(aFs,aName,aMode);
00078 CleanupStack::PushL(*this);
00079 }
00080
00081 LOCAL_C void createDataFileL()
00082 {
00083
00084 RFs createFileSession;
00085 RFileWithCleanup createFile;
00086
00087
00088 User::LeaveIfError(createFileSession.Connect());
00089
00090
00091
00092
00093
00094
00095
00096
00097 createFileSession.CreatePrivatePath(RFs::GetSystemDrive());
00098
00099
00100
00101 createFileSession.SetSessionToPrivate(RFs::GetSystemDrive());
00102
00103
00104 User::LeaveIfError(createFile.Replace(createFileSession,
00105 KFileName,
00106 EFileWrite|EFileStreamText));
00107
00108
00109
00110
00111
00112 TPtrC8 representation((TUint8*)(&KTestData)->Ptr(), (&KTestData)->Size());
00113
00114
00115 User::LeaveIfError(createFile.Write(representation));
00116 User::LeaveIfError(createFile.Flush());
00117 _LIT(KMsgDataWritten,"Data written to file\n");
00118 console->Printf(KMsgDataWritten);
00119
00120
00121
00122
00123 createFile.Close();
00124 createFileSession.Close();
00125 }
00126
00127 void useBufferL(TPtr8& bufferPtr)
00128 {
00129 printBuffer(0,bufferPtr);
00130
00131
00132
00133 }
00134
00135
00136 LOCAL_C void doExampleL()
00137 {
00138
00139 createDataFileL();
00140
00141
00142
00143
00144 TText8* buffer=(TText8*) User::Alloc(100*sizeof(TText8));
00145
00146
00147 CleanupStack::PushL(buffer);
00148
00149
00150 TPtr8 bufferPtr(buffer,100);
00151
00152
00153 RFs fsSession;
00154 _LIT(KMsgOpeningSession,"Opening session\n");
00155 console->Printf(KMsgOpeningSession);
00156
00157
00158 User::LeaveIfError(fsSession.Connect());
00159
00160
00161 RFileWithCleanup myFile;
00162 _LIT(KMsgOpeningFile,"Opening file\n");
00163 console->Printf(KMsgOpeningFile);
00164
00165
00166 myFile.OpenLC(fsSession,KFileName,EFileStreamText|EFileRead);
00167
00168
00169 _LIT(KMsgReadingFile,"Reading file into buffer.\n");
00170 console->Printf(KMsgReadingFile);
00171 User::LeaveIfError(myFile.Read(bufferPtr));
00172
00173
00174
00175
00176
00177 CleanupStack::PopAndDestroy();
00178
00179 fsSession.Close();
00180
00181
00182 useBufferL(bufferPtr);
00183
00184 CleanupStack::PopAndDestroy();
00185 }
00186