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,"TAnyandRObjects1.dat");
00023
00024
00025 _LIT(KTestData,"Test data for TAnyandRObjects1\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 };
00047
00048
00050
00051
00052
00054 void RFileWithCleanup::Cleanup(TAny *aPtr)
00055 {
00056 _LIT(KMsgDoingCleanup,"Doing cleanup of file.\n");
00057 console->Printf(KMsgDoingCleanup);
00058
00059 ((RFileWithCleanup *)aPtr)->Close();
00060 }
00061
00062 RFileWithCleanup::operator TCleanupItem()
00063 {
00064 return TCleanupItem(Cleanup,this);
00065 }
00066
00067 LOCAL_C void createDataFileL()
00068 {
00069
00070 RFs createFileSession;
00071 RFileWithCleanup createFile;
00072
00073
00074 User::LeaveIfError(createFileSession.Connect());
00075
00076
00077
00078
00079
00080
00081
00082 createFileSession.CreatePrivatePath(RFs::GetSystemDrive());
00083
00084
00085
00086 createFileSession.SetSessionToPrivate(RFs::GetSystemDrive());
00087
00088
00089 User::LeaveIfError(createFile.Replace(createFileSession,
00090 KFileName,
00091 EFileWrite|EFileStreamText));
00092
00093
00094
00095
00096
00097 TPtrC8 representation((TUint8*)(&KTestData)->Ptr(), (&KTestData)->Size());
00098
00099
00100
00101 User::LeaveIfError(createFile.Write(representation));
00102 User::LeaveIfError(createFile.Flush());
00103 _LIT(KMsgDataWritten,"Data written to file\n");
00104 console->Printf(KMsgDataWritten);
00105
00106
00107
00108
00109 createFile.Close();
00110 createFileSession.Close();
00111 }
00112
00113 void useBufferL(TPtr8& bufferPtr)
00114 {
00115 printBuffer(0,bufferPtr);
00116
00117
00118
00119 }
00120
00121
00122 LOCAL_C void doExampleL()
00123 {
00124
00125 createDataFileL();
00126
00127
00128
00129
00130 TText8* buffer=(TText8*) User::Alloc(100*sizeof(TText8));
00131
00132
00133 CleanupStack::PushL(buffer);
00134
00135
00136 TPtr8 bufferPtr(buffer,100);
00137
00138
00139 RFs fsSession;
00140 _LIT(KMsgOpeningSession,"Opening session\n");
00141 console->Printf(KMsgOpeningSession);
00142
00143
00144 User::LeaveIfError(fsSession.Connect());
00145
00146
00147 RFileWithCleanup myFile;
00148 _LIT(KMsgOpeningFile,"Opening file\n");
00149 console->Printf(KMsgOpeningFile);
00150
00151
00152 User::LeaveIfError(myFile.Open(fsSession,KFileName,EFileStreamText|EFileRead));
00153
00154
00155 CleanupStack::PushL(myFile);
00156
00157
00158 _LIT(KMsgReadingFile,"Reading file into buffer.\n");
00159 console->Printf(KMsgReadingFile);
00160 User::LeaveIfError(myFile.Read(bufferPtr));
00161
00162
00163
00164
00165
00166
00167 CleanupStack::PopAndDestroy();
00168
00169 fsSession.Close();
00170
00171
00172 useBufferL(bufferPtr);
00173
00174
00175 CleanupStack::PopAndDestroy();
00176 }
00177