sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "T_FileTextData.h"
|
sl@0
|
20 |
|
sl@0
|
21 |
// constants
|
sl@0
|
22 |
const TInt KReadLength = 64;
|
sl@0
|
23 |
|
sl@0
|
24 |
const TBool KMandatory = EFalse;
|
sl@0
|
25 |
|
sl@0
|
26 |
_LIT( KStrSeekEnd, "ESeekEnd" );
|
sl@0
|
27 |
_LIT( KStrSeekStart, "ESeekStart" );
|
sl@0
|
28 |
_LIT( KStrSeekAddress, "ESeekAddress");
|
sl@0
|
29 |
_LIT( KStrSeekCurrent, "ESeekCurrent");
|
sl@0
|
30 |
// Commands
|
sl@0
|
31 |
_LIT( KCmdDestructor, "~" );
|
sl@0
|
32 |
_LIT( KCmdNew, "new" );
|
sl@0
|
33 |
_LIT( KCmdRead, "Read" );
|
sl@0
|
34 |
_LIT( KCmdSeek, "Seek" );
|
sl@0
|
35 |
_LIT( KCmdSet, "Set" );
|
sl@0
|
36 |
_LIT( KCmdWrite, "Write" );
|
sl@0
|
37 |
|
sl@0
|
38 |
// Parameters
|
sl@0
|
39 |
_LIT( KParamExpectedString, "expected_str" );
|
sl@0
|
40 |
_LIT( KParamObjectName, "object_name" );
|
sl@0
|
41 |
_LIT( KParamSeekMode, "seek_mode" );
|
sl@0
|
42 |
_LIT( KParamText, "text" );
|
sl@0
|
43 |
_LIT( KParamBufferLength, "buffer_length");
|
sl@0
|
44 |
|
sl@0
|
45 |
CT_FileTextData* CT_FileTextData::NewL()
|
sl@0
|
46 |
/**
|
sl@0
|
47 |
* Two phase constructor
|
sl@0
|
48 |
*/
|
sl@0
|
49 |
{
|
sl@0
|
50 |
CT_FileTextData* ret = new (ELeave) CT_FileTextData();
|
sl@0
|
51 |
CleanupStack::PushL( ret );
|
sl@0
|
52 |
ret->ConstructL();
|
sl@0
|
53 |
CleanupStack::Pop( ret );
|
sl@0
|
54 |
return ret;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
CT_FileTextData::CT_FileTextData():
|
sl@0
|
58 |
iFileText(NULL)
|
sl@0
|
59 |
/**
|
sl@0
|
60 |
* Protected constructor. First phase construction
|
sl@0
|
61 |
*/
|
sl@0
|
62 |
{
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
void CT_FileTextData::ConstructL()
|
sl@0
|
66 |
/**
|
sl@0
|
67 |
* Protected constructor. Second phase construction
|
sl@0
|
68 |
*/
|
sl@0
|
69 |
{
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
/**
|
sl@0
|
73 |
* Destructor.
|
sl@0
|
74 |
*/
|
sl@0
|
75 |
CT_FileTextData::~CT_FileTextData()
|
sl@0
|
76 |
{
|
sl@0
|
77 |
DoCleanup();
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
TAny* CT_FileTextData::GetObject()
|
sl@0
|
81 |
/**
|
sl@0
|
82 |
* Return a pointer to the object that the data wraps
|
sl@0
|
83 |
*
|
sl@0
|
84 |
* @return pointer to the object that the data wraps
|
sl@0
|
85 |
*/
|
sl@0
|
86 |
{
|
sl@0
|
87 |
return iFileText;
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
TBool CT_FileTextData::DoCommandL( const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/ )
|
sl@0
|
91 |
/**
|
sl@0
|
92 |
* Process a command read from the ini file
|
sl@0
|
93 |
*
|
sl@0
|
94 |
* @param aCommand the command to process
|
sl@0
|
95 |
* @param aSection the entry in the ini file requiring the command to be processed
|
sl@0
|
96 |
*
|
sl@0
|
97 |
* @return ETrue if the command is processed
|
sl@0
|
98 |
*/
|
sl@0
|
99 |
{
|
sl@0
|
100 |
TBool retVal = ETrue;
|
sl@0
|
101 |
|
sl@0
|
102 |
if ( aCommand == KCmdDestructor )
|
sl@0
|
103 |
{
|
sl@0
|
104 |
DoCleanup();
|
sl@0
|
105 |
}
|
sl@0
|
106 |
else if ( aCommand == KCmdNew )
|
sl@0
|
107 |
{
|
sl@0
|
108 |
DoCmdNew();
|
sl@0
|
109 |
}
|
sl@0
|
110 |
else if ( aCommand == KCmdRead )
|
sl@0
|
111 |
{
|
sl@0
|
112 |
DoCmdRead( aSection );
|
sl@0
|
113 |
}
|
sl@0
|
114 |
else if ( aCommand == KCmdSeek )
|
sl@0
|
115 |
{
|
sl@0
|
116 |
DoCmdSeek( aSection );
|
sl@0
|
117 |
}
|
sl@0
|
118 |
else if ( aCommand == KCmdSet )
|
sl@0
|
119 |
{
|
sl@0
|
120 |
DoCmdSet( aSection );
|
sl@0
|
121 |
}
|
sl@0
|
122 |
else if ( aCommand == KCmdWrite )
|
sl@0
|
123 |
{
|
sl@0
|
124 |
DoCmdWrite( aSection );
|
sl@0
|
125 |
}
|
sl@0
|
126 |
else
|
sl@0
|
127 |
{
|
sl@0
|
128 |
retVal = EFalse;
|
sl@0
|
129 |
}
|
sl@0
|
130 |
|
sl@0
|
131 |
return retVal;
|
sl@0
|
132 |
}
|
sl@0
|
133 |
|
sl@0
|
134 |
void CT_FileTextData::DoCleanup()
|
sl@0
|
135 |
/**
|
sl@0
|
136 |
* Deletes TFileText class instance
|
sl@0
|
137 |
*/
|
sl@0
|
138 |
{
|
sl@0
|
139 |
INFO_PRINTF1( _L( "Delete TFileText class instance" ) );
|
sl@0
|
140 |
delete iFileText;
|
sl@0
|
141 |
iFileText = NULL;
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
void CT_FileTextData::DoCmdNew()
|
sl@0
|
145 |
/**
|
sl@0
|
146 |
* Creates new TFileText class instance
|
sl@0
|
147 |
*/
|
sl@0
|
148 |
{
|
sl@0
|
149 |
DoCleanup();
|
sl@0
|
150 |
|
sl@0
|
151 |
INFO_PRINTF1( _L( "Create new TFileText class instance." ) );
|
sl@0
|
152 |
TRAPD( err, iFileText = new (ELeave) TFileText() );
|
sl@0
|
153 |
if ( err != KErrNone )
|
sl@0
|
154 |
{
|
sl@0
|
155 |
ERR_PRINTF2( _L( "new TFileText() error %d" ), err );
|
sl@0
|
156 |
SetError( err );
|
sl@0
|
157 |
}
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
void CT_FileTextData::DoCmdRead( const TDesC& aSection )
|
sl@0
|
161 |
/**
|
sl@0
|
162 |
* Reads one line form file using Read()
|
sl@0
|
163 |
*/
|
sl@0
|
164 |
{
|
sl@0
|
165 |
INFO_PRINTF1( _L( "Read one line from file." ) );
|
sl@0
|
166 |
|
sl@0
|
167 |
RBuf readLine;
|
sl@0
|
168 |
TInt bufferLength = KReadLength;
|
sl@0
|
169 |
|
sl@0
|
170 |
GET_OPTIONAL_INT_PARAMETER( KParamBufferLength, aSection, bufferLength );
|
sl@0
|
171 |
TInt err = readLine.Create(bufferLength);
|
sl@0
|
172 |
|
sl@0
|
173 |
if(err == KErrNone)
|
sl@0
|
174 |
{
|
sl@0
|
175 |
err = iFileText->Read( readLine );
|
sl@0
|
176 |
|
sl@0
|
177 |
INFO_PRINTF2( _L( "FileText::Read() result - %S" ), &readLine );
|
sl@0
|
178 |
|
sl@0
|
179 |
if ( err != KErrNone )
|
sl@0
|
180 |
{
|
sl@0
|
181 |
ERR_PRINTF2( _L( "Function returned error %d." ), err );
|
sl@0
|
182 |
SetError( err );
|
sl@0
|
183 |
}
|
sl@0
|
184 |
else
|
sl@0
|
185 |
{
|
sl@0
|
186 |
TPtrC expectedLine;
|
sl@0
|
187 |
if ( GET_OPTIONAL_STRING_PARAMETER( KParamExpectedString, aSection, expectedLine ) )
|
sl@0
|
188 |
{
|
sl@0
|
189 |
if ( readLine.Compare(expectedLine)!=0 )
|
sl@0
|
190 |
{
|
sl@0
|
191 |
ERR_PRINTF3( _L( "Read line \"%S\", expected \"%S\"." ), &readLine, &expectedLine );
|
sl@0
|
192 |
SetBlockResult( EFail );
|
sl@0
|
193 |
}
|
sl@0
|
194 |
}
|
sl@0
|
195 |
}
|
sl@0
|
196 |
readLine.Close();
|
sl@0
|
197 |
}
|
sl@0
|
198 |
else
|
sl@0
|
199 |
{
|
sl@0
|
200 |
SetBlockResult( EFail );
|
sl@0
|
201 |
ERR_PRINTF1( _L( "RBuf initialization failed." ));
|
sl@0
|
202 |
}
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
void CT_FileTextData::DoCmdSeek( const TDesC& aSection )
|
sl@0
|
206 |
/**
|
sl@0
|
207 |
* performs a seek to start or end of file using Seek()
|
sl@0
|
208 |
*/
|
sl@0
|
209 |
{
|
sl@0
|
210 |
TSeek mode;
|
sl@0
|
211 |
if ( GetSeekMode( aSection, mode, KMandatory ) )
|
sl@0
|
212 |
{
|
sl@0
|
213 |
TInt err = iFileText->Seek( mode );
|
sl@0
|
214 |
|
sl@0
|
215 |
if ( err != KErrNone )
|
sl@0
|
216 |
{
|
sl@0
|
217 |
ERR_PRINTF2( _L( "Function returned %d." ), err);
|
sl@0
|
218 |
SetError( err );
|
sl@0
|
219 |
}
|
sl@0
|
220 |
}
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
void CT_FileTextData::DoCmdSet( const TDesC& aSection )
|
sl@0
|
224 |
/**
|
sl@0
|
225 |
* Sets the file to be read from or written to using Set()
|
sl@0
|
226 |
*/
|
sl@0
|
227 |
{
|
sl@0
|
228 |
TPtrC fileObjectName;
|
sl@0
|
229 |
if ( GET_MANDATORY_STRING_PARAMETER( KParamObjectName, aSection, fileObjectName ) )
|
sl@0
|
230 |
{
|
sl@0
|
231 |
INFO_PRINTF2( _L( "Set( %S )" ), &fileObjectName );
|
sl@0
|
232 |
|
sl@0
|
233 |
RFile* fileObject = NULL;
|
sl@0
|
234 |
TRAPD( err, fileObject = (RFile*)GetDataObjectL( fileObjectName ) );
|
sl@0
|
235 |
|
sl@0
|
236 |
if ( err == KErrNone )
|
sl@0
|
237 |
{
|
sl@0
|
238 |
iFileText->Set( *fileObject );
|
sl@0
|
239 |
}
|
sl@0
|
240 |
else
|
sl@0
|
241 |
{
|
sl@0
|
242 |
ERR_PRINTF3( _L( "Unable to access object %S (error = %d)"), &fileObjectName, err );
|
sl@0
|
243 |
SetBlockResult( EFail );
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
}
|
sl@0
|
247 |
}
|
sl@0
|
248 |
|
sl@0
|
249 |
void CT_FileTextData::DoCmdWrite( const TDesC& aSection )
|
sl@0
|
250 |
/**
|
sl@0
|
251 |
* writes one line of text into file using Write()
|
sl@0
|
252 |
*/
|
sl@0
|
253 |
{
|
sl@0
|
254 |
TPtrC writeLine;
|
sl@0
|
255 |
if ( GET_MANDATORY_STRING_PARAMETER( KParamText, aSection, writeLine ) )
|
sl@0
|
256 |
{
|
sl@0
|
257 |
|
sl@0
|
258 |
INFO_PRINTF2( _L( "Write \"%S\" into file" ), &writeLine );
|
sl@0
|
259 |
TInt err = iFileText->Write( writeLine );
|
sl@0
|
260 |
if ( err != KErrNone)
|
sl@0
|
261 |
{
|
sl@0
|
262 |
ERR_PRINTF2( _L( "Function returned %d." ), err);
|
sl@0
|
263 |
SetError( err );
|
sl@0
|
264 |
}
|
sl@0
|
265 |
}
|
sl@0
|
266 |
}
|
sl@0
|
267 |
|
sl@0
|
268 |
|
sl@0
|
269 |
|
sl@0
|
270 |
TBool CT_FileTextData::GetSeekMode( const TDesC& aSection, TSeek& aMode, TBool aOptional )
|
sl@0
|
271 |
/**
|
sl@0
|
272 |
* retrieves "seek_mode" parameter value and converts it into its TSeek representation
|
sl@0
|
273 |
* @param aSection - the entry in the ini file requiring the command to be processed
|
sl@0
|
274 |
* @param aMode - the returned TSeek representation of "seek_mode" command parameter
|
sl@0
|
275 |
* @param aOptional - represents the function which is called to retrieve the value
|
sl@0
|
276 |
* KOptional - GET_OPTIONAL_STRING_PARAMETER is called
|
sl@0
|
277 |
* KMandatory - GET_MANDATORY_STRING_PARAMETER is called
|
sl@0
|
278 |
*
|
sl@0
|
279 |
* @return ETrue if "seek_mode" parameter is present and has been sucessfully converted
|
sl@0
|
280 |
*/
|
sl@0
|
281 |
{
|
sl@0
|
282 |
TBool result = ETrue;
|
sl@0
|
283 |
|
sl@0
|
284 |
TPtrC strSeekMode;
|
sl@0
|
285 |
|
sl@0
|
286 |
// Get "seek_mode" command parameter string value
|
sl@0
|
287 |
if ( aOptional )
|
sl@0
|
288 |
{
|
sl@0
|
289 |
result = GET_OPTIONAL_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
|
sl@0
|
290 |
}
|
sl@0
|
291 |
else
|
sl@0
|
292 |
{
|
sl@0
|
293 |
result = GET_MANDATORY_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
|
sl@0
|
294 |
}
|
sl@0
|
295 |
|
sl@0
|
296 |
// Convert it into TSeek representation
|
sl@0
|
297 |
if ( result )
|
sl@0
|
298 |
{
|
sl@0
|
299 |
if ( strSeekMode == KStrSeekEnd )
|
sl@0
|
300 |
{
|
sl@0
|
301 |
aMode = ESeekEnd;
|
sl@0
|
302 |
}
|
sl@0
|
303 |
else if ( strSeekMode == KStrSeekStart )
|
sl@0
|
304 |
{
|
sl@0
|
305 |
aMode = ESeekStart;
|
sl@0
|
306 |
}
|
sl@0
|
307 |
else if ( strSeekMode == KStrSeekAddress )
|
sl@0
|
308 |
{
|
sl@0
|
309 |
aMode = ESeekAddress;
|
sl@0
|
310 |
}
|
sl@0
|
311 |
else if ( strSeekMode == KStrSeekCurrent )
|
sl@0
|
312 |
{
|
sl@0
|
313 |
aMode = ESeekCurrent;
|
sl@0
|
314 |
}
|
sl@0
|
315 |
else
|
sl@0
|
316 |
{
|
sl@0
|
317 |
ERR_PRINTF2( _L( "Unrecognized seek_mode value: %S" ), &strSeekMode );
|
sl@0
|
318 |
result = EFalse;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
}
|
sl@0
|
321 |
|
sl@0
|
322 |
return result;
|
sl@0
|
323 |
}
|
sl@0
|
324 |
|
sl@0
|
325 |
|