00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <bautils.h>
00032 #include <FileStream.rsg>
00033
00034 #include "FileStreamAppUi.h"
00035 #include "FileStreamMainView.h"
00036 #include "FileStream.hrh"
00037 #include "employee.h"
00038
00039
00040 _LIT(KInt32FileName, "c:\\data\\myfile.dat");
00041 _LIT(KEmployeeFileName, "c:\\data\\employee.dat");
00042
00043 _LIT(KName, "John Doe");
00044 _LIT(KPhoneNumber, "+1-222-333-444");
00045
00046
00047 const TChar KEndOfLine = '\f';
00048
00049
00050 const TInt KMaxNumber = 10;
00051 const TInt KMaxBuffer = 100;
00052
00053
00054
00055
00056
00057
00058 void CFileStreamAppUi::ConstructL()
00059 {
00060
00061
00062 BaseConstructL(EAknEnableSkin);
00063
00064 iMainView = CFileStreamMainView::NewL(ClientRect());
00065 }
00066
00067
00068
00069
00070 CFileStreamAppUi::~CFileStreamAppUi()
00071 {
00072 if (iMainView)
00073 {
00074 delete iMainView;
00075 iMainView = NULL;
00076 }
00077 }
00078
00079
00080
00081
00082 void CFileStreamAppUi::HandleCommandL(TInt aCommand)
00083 {
00084 switch ( aCommand )
00085 {
00086
00087
00088 case EAknSoftkeyExit:
00089 case EEikCmdExit:
00090 {
00091 Exit();
00092 break;
00093 }
00094
00095 case EFileStreamWriteInt32:
00096 {
00097
00098 const TInt32 x = 12345;
00099 WriteInt32ToStreamL(iCoeEnv->FsSession(), KInt32FileName, x);
00100
00101
00102 HBufC* message = iCoeEnv->AllocReadResourceLC(
00103 R_FILESTREAM_INT32WRITTEN);
00104 iMainView->SetTextL(*message);
00105 CleanupStack::PopAndDestroy(message);
00106 break;
00107 }
00108
00109 case EFileStreamReadInt32:
00110 {
00111
00112 TInt32 x = ReadInt32FromStreamL(iCoeEnv->FsSession(),
00113 KInt32FileName);
00114
00115
00116 TBuf<KMaxNumber> buffer;
00117 buffer.AppendNum(x);
00118 iMainView->SetTextL(buffer);
00119 break;
00120 }
00121
00122 case EFileStreamAppendInt32:
00123 {
00124
00125 const TInt32 x = 98765;
00126 AppendInt32ToStreamL(iCoeEnv->FsSession(), KInt32FileName, x);
00127
00128
00129 HBufC* message = iCoeEnv->AllocReadResourceLC(
00130 R_FILESTREAM_INT32APPENDED);
00131 iMainView->SetTextL(*message);
00132 CleanupStack::PopAndDestroy(message);
00133 break;
00134 }
00135
00136 case EFileStreamReadAllInt32:
00137 {
00138
00139 RArray<TInt> array;
00140 CleanupClosePushL(array);
00141
00142
00143 ReadAllTInt32FromStreamL(iCoeEnv->FsSession(), KInt32FileName, array);
00144
00145
00146 RBuf buffer;
00147 buffer.CreateL(KMaxBuffer);
00148 CleanupClosePushL(buffer);
00149 for (TInt i = 0; i < array.Count(); i++)
00150 {
00151
00152
00153
00154 buffer.AppendNum(array[i]);
00155 buffer.Append(KEndOfLine);
00156 }
00157 iMainView->SetTextL(buffer);
00158
00159 CleanupStack::PopAndDestroy(2, &array);
00160
00161 break;
00162 }
00163
00164 case EFileStreamWriteEmployee:
00165 {
00166 CEmployee* employee = new (ELeave) CEmployee();
00167 CleanupStack::PushL(employee);
00168
00169
00170 employee->SetIdentifier(1);
00171 employee->SetName(KName);
00172 employee->SetPhoneNumber(KPhoneNumber);
00173 WriteEmployeeToStreamL(iCoeEnv->FsSession(), KEmployeeFileName,
00174 *employee);
00175
00176 CleanupStack::PopAndDestroy(employee);
00177
00178
00179 HBufC* message = iCoeEnv->AllocReadResourceLC(
00180 R_FILESTREAM_EMPLOYEEWRITTEN);
00181 iMainView->SetTextL(*message);
00182 CleanupStack::PopAndDestroy(message);
00183 break;
00184 }
00185
00186 case EFileStreamReadEmployee:
00187 {
00188 CEmployee* employee = new (ELeave) CEmployee();
00189 CleanupStack::PushL(employee);
00190
00191
00192 ReadEmployeeFromStreamL(iCoeEnv->FsSession(), KEmployeeFileName,
00193 *employee);
00194
00195
00196 RBuf buffer;
00197 buffer.CreateL(KMaxBuffer);
00198 CleanupClosePushL(buffer);
00199 buffer.AppendNum(employee->Identifier());
00200 buffer.Append(KEndOfLine);
00201 buffer.Append(employee->Name());
00202 buffer.Append(KEndOfLine);
00203 buffer.Append(employee->PhoneNumber());
00204 buffer.Append(KEndOfLine);
00205 iMainView->SetTextL(buffer);
00206
00207 CleanupStack::PopAndDestroy(2, employee);
00208 break;
00209 }
00210
00211 default:
00212
00213 break;
00214 }
00215 }
00216
00217
00218
00219
00220
00221
00222 void CFileStreamAppUi::HandleResourceChangeL(TInt aType)
00223 {
00224 CAknAppUi::HandleResourceChangeL(aType);
00225 iMainView->SetRect(ClientRect());
00226 }
00227
00228
00229 void CFileStreamAppUi::WriteInt32ToStreamL(RFs& aFs,
00230 const TDesC& aFileName, TInt32 aValue)
00231 {
00232
00233 RFileWriteStream writeStream;
00234 User::LeaveIfError(writeStream.Replace(aFs, aFileName, EFileWrite));
00235 writeStream.PushL();
00236
00237
00238 writeStream.WriteInt32L(aValue);
00239
00240
00241 writeStream.CommitL();
00242 CleanupStack::Pop(&writeStream);
00243 writeStream.Release();
00244 }
00245
00246 TInt32 CFileStreamAppUi::ReadInt32FromStreamL(RFs& aFs,
00247 const TDesC& aFileName)
00248 {
00249
00250 RFileReadStream readStream;
00251 User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00252 CleanupClosePushL(readStream);
00253
00254
00255 TInt32 value;
00256 readStream >> value;
00257
00258
00259 CleanupStack::PopAndDestroy(&readStream);
00260 return value;
00261 }
00262
00263 void CFileStreamAppUi::AppendInt32ToStreamL(RFs& aFs,
00264 const TDesC& aFileName, TInt32 aValue)
00265 {
00266
00267 RFileWriteStream writeStream;
00268 TInt err = KErrNone;
00269 if (BaflUtils::FileExists(iCoeEnv->FsSession(), aFileName))
00270 {
00271 err = writeStream.Open(aFs, aFileName, EFileWrite);
00272 }
00273 else
00274 {
00275 err = writeStream.Create(aFs, aFileName, EFileWrite);
00276 }
00277 User::LeaveIfError(err);
00278 writeStream.PushL();
00279
00280
00281 writeStream.Sink()->SeekL(MStreamBuf::EWrite, EStreamEnd, 0);
00282
00283
00284 writeStream.WriteInt32L(aValue);
00285
00286
00287 writeStream.CommitL();
00288 CleanupStack::Pop(&writeStream);
00289 writeStream.Release();
00290 }
00291
00292 void CFileStreamAppUi::ReadAllTInt32FromStreamL(RFs& aFs,
00293 const TDesC& aFileName, RArray<TInt>& aArray)
00294 {
00295
00296 RFileReadStream readStream;
00297 User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00298 CleanupClosePushL(readStream);
00299
00300
00301 TStreamPos eofPos(readStream.Source()->SizeL());
00302
00303
00304 TStreamPos currentPos(0);
00305
00306
00307 while (currentPos < eofPos)
00308 {
00309 TInt32 value;
00310 readStream >> value;
00311 aArray.Append(value);
00312
00313 currentPos = readStream.Source()->TellL(MStreamBuf::ERead);
00314 }
00315
00316 CleanupStack::PopAndDestroy(&readStream);
00317 }
00318
00319 void CFileStreamAppUi::WriteEmployeeToStreamL(RFs& aFs, const TDesC& aFileName,
00320 const CEmployee& aEmployee)
00321 {
00322
00323 RFileWriteStream writeStream;
00324 User::LeaveIfError(writeStream.Replace(aFs, aFileName, EFileWrite));
00325 CleanupClosePushL(writeStream);
00326
00327
00328 writeStream << aEmployee;
00329
00330
00331 writeStream.CommitL();
00332 CleanupStack::PopAndDestroy(&writeStream);
00333 }
00334
00335 void CFileStreamAppUi::ReadEmployeeFromStreamL(RFs& aFs, const TDesC& aFileName,
00336 CEmployee& aEmployee)
00337 {
00338
00339 RFileReadStream readStream;
00340 User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00341 CleanupClosePushL(readStream);
00342
00343
00344 readStream >> aEmployee;
00345
00346
00347 CleanupStack::PopAndDestroy(&readStream);
00348 }
00349
00350