|
1 // t_ioread.cpp |
|
2 // |
|
3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include <fshell/ioutils.h> |
|
14 |
|
15 using namespace IoUtils; |
|
16 |
|
17 // enum corresponding to RIoReadHandle::TReadMode |
|
18 _LIT(KReadModeEnum, "full,line,oneormore"); |
|
19 |
|
20 static const TInt KDefaultReadSize = 0x100; |
|
21 |
|
22 class CIoReadTest : public CCommandBase |
|
23 { |
|
24 public: |
|
25 static CCommandBase* NewLC(); |
|
26 ~CIoReadTest(); |
|
27 private: |
|
28 CIoReadTest(); |
|
29 void ReadFileL(); |
|
30 private: // From CCommandBase. |
|
31 virtual const TDesC& Name() const; |
|
32 virtual const TDesC& Description() const; |
|
33 virtual void DoRunL(); |
|
34 virtual void ArgumentsL(RCommandArgumentList& aArguments); |
|
35 virtual void OptionsL(RCommandOptionList& aOptions); |
|
36 private: |
|
37 TFileName2 iFile; |
|
38 TInt iReadSize; |
|
39 RIoReadHandle::TReadMode iReadMode; |
|
40 RBuf iFileContents; |
|
41 |
|
42 RIoFile iIoFile; |
|
43 RIoReadHandle iFileReader; |
|
44 RBuf iReadBuf; |
|
45 CTextBuffer* iFileBuffer; |
|
46 }; |
|
47 |
|
48 |
|
49 CCommandBase* CIoReadTest::NewLC() |
|
50 { |
|
51 CIoReadTest* self = new(ELeave) CIoReadTest(); |
|
52 CleanupStack::PushL(self); |
|
53 self->BaseConstructL(); |
|
54 return self; |
|
55 } |
|
56 |
|
57 CIoReadTest::~CIoReadTest() |
|
58 { |
|
59 iFileContents.Close(); |
|
60 iIoFile.Close(); |
|
61 iFileReader.Close(); |
|
62 iReadBuf.Close(); |
|
63 delete iFileBuffer; |
|
64 } |
|
65 |
|
66 CIoReadTest::CIoReadTest() |
|
67 : iReadSize(KDefaultReadSize) |
|
68 { |
|
69 } |
|
70 |
|
71 const TDesC& CIoReadTest::Name() const |
|
72 { |
|
73 _LIT(KName, "t_ioread"); |
|
74 return KName; |
|
75 } |
|
76 |
|
77 const TDesC& CIoReadTest::Description() const |
|
78 { |
|
79 _LIT(KDescription, "Test harness for iosrv file reading functionality"); |
|
80 return KDescription; |
|
81 } |
|
82 |
|
83 void CIoReadTest::ReadFileL() |
|
84 { |
|
85 RFile file; |
|
86 LeaveIfErr(file.Open(Fs(), iFile, EFileRead | EFileShareExclusive), _L("Cannot open file '%S'"), &iFile); |
|
87 CleanupClosePushL(file); |
|
88 TInt fileSize; |
|
89 User::LeaveIfError(file.Size(fileSize)); |
|
90 |
|
91 RBuf8 contents; |
|
92 contents.CreateL(fileSize); |
|
93 CleanupClosePushL(contents); |
|
94 User::LeaveIfError(file.Read(contents)); |
|
95 |
|
96 // convert data to 16-bit which is how it'll come out of the io server |
|
97 iFileContents.CreateL(fileSize); |
|
98 iFileContents.Copy(contents); // copy & expand |
|
99 CleanupStack::PopAndDestroy(2, &file); // contents, file |
|
100 } |
|
101 |
|
102 void CIoReadTest::DoRunL() |
|
103 { |
|
104 // read the file into memory |
|
105 ReadFileL(); |
|
106 |
|
107 LeaveIfErr(iIoFile.Create(IoSession(), iFile, RIoFile::ERead), _L("Cannot create file end point for '%S'"), &iFile); |
|
108 |
|
109 iFileReader.CreateL(IoSession()); |
|
110 iIoFile.AttachL(iFileReader, RIoEndPoint::EForeground); |
|
111 |
|
112 iFileReader.SetReadModeL(iReadMode); |
|
113 |
|
114 iReadBuf.CreateL(iReadSize); |
|
115 |
|
116 |
|
117 iFileBuffer = CTextBuffer::NewL(0x20); |
|
118 TInt err; |
|
119 do |
|
120 { |
|
121 iReadBuf.Zero(); |
|
122 err = iFileReader.Read(iReadBuf); |
|
123 iFileBuffer->AppendL(iReadBuf); |
|
124 } while (err == KErrNone); |
|
125 |
|
126 if (err!=KErrEof) LeaveIfErr(err, _L("Unexpected error from RIoReadHandle::Read")); |
|
127 |
|
128 TInt completion = 0; |
|
129 if (iFileBuffer->Length() != iFileContents.Length()) |
|
130 { |
|
131 PrintWarning(_L("Amount of data read from file (%d) differs from file size (%d)."), iFileBuffer->Length(), iFileContents.Length()); |
|
132 completion = 1; |
|
133 } |
|
134 if (iFileBuffer->Descriptor().Compare(iFileContents) != 0) |
|
135 { |
|
136 PrintWarning(_L("Data read from file differs from file contents.")); |
|
137 completion = 1; |
|
138 } |
|
139 Complete(completion); |
|
140 } |
|
141 |
|
142 void CIoReadTest::ArgumentsL(RCommandArgumentList& aArguments) |
|
143 { |
|
144 _LIT(KFile, "file"); |
|
145 _LIT(KFileDescription, "File to test with"); |
|
146 aArguments.AppendFileNameL(iFile, KFile, KFileDescription); |
|
147 } |
|
148 |
|
149 void CIoReadTest::OptionsL(RCommandOptionList& aOptions) |
|
150 { |
|
151 _LIT(KReadSize, "size"); |
|
152 _LIT(KReadSizeDescription, "The size of each read to issue"); |
|
153 aOptions.AppendIntL(iReadSize, 's', KReadSize, KReadSizeDescription); |
|
154 |
|
155 _LIT(KReadMode, "mode"); |
|
156 _LIT(KReadModeDescription, "The type of read to issue"); |
|
157 aOptions.AppendEnumL((TInt&)iReadMode, 'm', KReadMode, KReadModeDescription, KReadModeEnum); |
|
158 } |
|
159 |
|
160 |
|
161 EXE_BOILER_PLATE(CIoReadTest) |
|
162 |