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 <f32file.h>
00032 #include <bautils.h>
00033 #include <BinaryFile.rsg>
00034
00035 #include "BinaryFileAppUi.h"
00036 #include "BinaryFileMainView.h"
00037 #include "BinaryFile.hrh"
00038
00039
00040 const TInt KMaxBuffer = 512;
00041 _LIT(KNewFileName, "c:\\data\\newfile.dat");
00042 _LIT(KSourceFileName, "c:\\data\\sourcefile.dat");
00043 _LIT(KDestFileName, "c:\\data\\destfile.dat");
00044
00045
00046
00047
00048
00049
00050 void CBinaryFileAppUi::ConstructL()
00051 {
00052 BaseConstructL(EAknEnableSkin);
00053 iMainView = CBinaryFileMainView::NewL(ClientRect());
00054 BaflUtils::EnsurePathExistsL(iCoeEnv->FsSession(), KNewFileName);
00055 }
00056
00057
00058
00059
00060 CBinaryFileAppUi::~CBinaryFileAppUi()
00061 {
00062 if (iMainView)
00063 {
00064 delete iMainView;
00065 iMainView = NULL;
00066 }
00067 }
00068
00069
00070
00071
00072 void CBinaryFileAppUi::HandleCommandL(TInt aCommand)
00073 {
00074 switch ( aCommand )
00075 {
00076
00077
00078 case EAknSoftkeyExit:
00079
00080 case EEikCmdExit:
00081 {
00082 Exit();
00083 break;
00084 }
00085
00086 case EBinaryFileWriteToFile:
00087 {
00088 RFs& fileServer = iCoeEnv->FsSession();
00089
00090
00091 RBuf8 buffer;
00092 User::LeaveIfError(buffer.CreateMax(KMaxBuffer));
00093 CleanupClosePushL(buffer);
00094
00095
00096
00097 for (TInt i = 0; i < KMaxBuffer; i++)
00098 {
00099 buffer[i] = i;
00100 }
00101
00102
00103 WriteDescToFileL(fileServer, KNewFileName, buffer);
00104
00105
00106 CleanupStack::PopAndDestroy(&buffer);
00107
00108 iEikonEnv->InfoWinL(
00109 R_BINARYFILE_CAPTION, R_BINARYFILE_FILEWRITTEN);
00110 break;
00111 }
00112
00113 case EBinaryFileReadFromFile:
00114 {
00115 RFs& fileServer = iCoeEnv->FsSession();
00116
00117
00118 RBuf8 buffer;
00119 User::LeaveIfError(buffer.CreateMax(KMaxBuffer));
00120 CleanupClosePushL(buffer);
00121
00122
00123
00124
00125 ReadDescFromFileL(fileServer, KSourceFileName, buffer);
00126
00127
00128 WriteDescToFileL(fileServer, KDestFileName, buffer);
00129
00130
00131 CleanupStack::PopAndDestroy(&buffer);
00132
00133 iEikonEnv->InfoWinL(R_BINARYFILE_CAPTION, R_BINARYFILE_FILEREAD);
00134 break;
00135 }
00136
00137 default:
00138
00139 break;
00140 }
00141 }
00142
00143
00144
00145
00146
00147
00148
00149 void CBinaryFileAppUi::HandleResourceChangeL(TInt aType)
00150 {
00151 CAknAppUi::HandleResourceChangeL(aType);
00152 iMainView->SetRect(ClientRect());
00153 }
00154
00155
00156
00157
00158
00159
00160 void CBinaryFileAppUi::WriteDescToFileL(RFs& aFs, const TDesC& aFileName,
00161 const TDesC8& aBuffer)
00162 {
00163 RFile file;
00164 User::LeaveIfError(file.Replace(aFs, aFileName, EFileWrite));
00165 CleanupClosePushL(file);
00166
00167 User::LeaveIfError(file.Write(aBuffer, aBuffer.Length()));
00168
00169 CleanupStack::PopAndDestroy(&file);
00170 }
00171
00172
00173
00174
00175
00176
00177 void CBinaryFileAppUi::ReadDescFromFileL(RFs& aFs, const TDesC& aFileName,
00178 TDes8& aBuffer)
00179 {
00180 RFile file;
00181 User::LeaveIfError(file.Open(aFs, aFileName, EFileRead));
00182 CleanupClosePushL(file);
00183
00184
00185 TInt fileSize;
00186 User::LeaveIfError(file.Size(fileSize));
00187
00188
00189
00190 if (aBuffer.MaxLength() < fileSize)
00191 {
00192 User::Leave(KErrOverflow);
00193 }
00194 User::LeaveIfError(file.Read(aBuffer, fileSize));
00195
00196 CleanupStack::PopAndDestroy(&file);
00197 }
00198
00199