Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "T_FileTextData.h"
22 const TInt KReadLength = 64;
24 const TBool KMandatory = EFalse;
26 _LIT( KStrSeekEnd, "ESeekEnd" );
27 _LIT( KStrSeekStart, "ESeekStart" );
28 _LIT( KStrSeekAddress, "ESeekAddress");
29 _LIT( KStrSeekCurrent, "ESeekCurrent");
31 _LIT( KCmdDestructor, "~" );
32 _LIT( KCmdNew, "new" );
33 _LIT( KCmdRead, "Read" );
34 _LIT( KCmdSeek, "Seek" );
35 _LIT( KCmdSet, "Set" );
36 _LIT( KCmdWrite, "Write" );
39 _LIT( KParamExpectedString, "expected_str" );
40 _LIT( KParamObjectName, "object_name" );
41 _LIT( KParamSeekMode, "seek_mode" );
42 _LIT( KParamText, "text" );
43 _LIT( KParamBufferLength, "buffer_length");
45 CT_FileTextData* CT_FileTextData::NewL()
47 * Two phase constructor
50 CT_FileTextData* ret = new (ELeave) CT_FileTextData();
51 CleanupStack::PushL( ret );
53 CleanupStack::Pop( ret );
57 CT_FileTextData::CT_FileTextData():
60 * Protected constructor. First phase construction
65 void CT_FileTextData::ConstructL()
67 * Protected constructor. Second phase construction
75 CT_FileTextData::~CT_FileTextData()
80 TAny* CT_FileTextData::GetObject()
82 * Return a pointer to the object that the data wraps
84 * @return pointer to the object that the data wraps
90 TBool CT_FileTextData::DoCommandL( const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/ )
92 * Process a command read from the ini file
94 * @param aCommand the command to process
95 * @param aSection the entry in the ini file requiring the command to be processed
97 * @return ETrue if the command is processed
100 TBool retVal = ETrue;
102 if ( aCommand == KCmdDestructor )
106 else if ( aCommand == KCmdNew )
110 else if ( aCommand == KCmdRead )
112 DoCmdRead( aSection );
114 else if ( aCommand == KCmdSeek )
116 DoCmdSeek( aSection );
118 else if ( aCommand == KCmdSet )
120 DoCmdSet( aSection );
122 else if ( aCommand == KCmdWrite )
124 DoCmdWrite( aSection );
134 void CT_FileTextData::DoCleanup()
136 * Deletes TFileText class instance
139 INFO_PRINTF1( _L( "Delete TFileText class instance" ) );
144 void CT_FileTextData::DoCmdNew()
146 * Creates new TFileText class instance
151 INFO_PRINTF1( _L( "Create new TFileText class instance." ) );
152 TRAPD( err, iFileText = new (ELeave) TFileText() );
153 if ( err != KErrNone )
155 ERR_PRINTF2( _L( "new TFileText() error %d" ), err );
160 void CT_FileTextData::DoCmdRead( const TDesC& aSection )
162 * Reads one line form file using Read()
165 INFO_PRINTF1( _L( "Read one line from file." ) );
168 TInt bufferLength = KReadLength;
170 GET_OPTIONAL_INT_PARAMETER( KParamBufferLength, aSection, bufferLength );
171 TInt err = readLine.Create(bufferLength);
175 err = iFileText->Read( readLine );
177 INFO_PRINTF2( _L( "FileText::Read() result - %S" ), &readLine );
179 if ( err != KErrNone )
181 ERR_PRINTF2( _L( "Function returned error %d." ), err );
187 if ( GET_OPTIONAL_STRING_PARAMETER( KParamExpectedString, aSection, expectedLine ) )
189 if ( readLine.Compare(expectedLine)!=0 )
191 ERR_PRINTF3( _L( "Read line \"%S\", expected \"%S\"." ), &readLine, &expectedLine );
192 SetBlockResult( EFail );
200 SetBlockResult( EFail );
201 ERR_PRINTF1( _L( "RBuf initialization failed." ));
205 void CT_FileTextData::DoCmdSeek( const TDesC& aSection )
207 * performs a seek to start or end of file using Seek()
211 if ( GetSeekMode( aSection, mode, KMandatory ) )
213 TInt err = iFileText->Seek( mode );
215 if ( err != KErrNone )
217 ERR_PRINTF2( _L( "Function returned %d." ), err);
223 void CT_FileTextData::DoCmdSet( const TDesC& aSection )
225 * Sets the file to be read from or written to using Set()
228 TPtrC fileObjectName;
229 if ( GET_MANDATORY_STRING_PARAMETER( KParamObjectName, aSection, fileObjectName ) )
231 INFO_PRINTF2( _L( "Set( %S )" ), &fileObjectName );
233 RFile* fileObject = NULL;
234 TRAPD( err, fileObject = (RFile*)GetDataObjectL( fileObjectName ) );
236 if ( err == KErrNone )
238 iFileText->Set( *fileObject );
242 ERR_PRINTF3( _L( "Unable to access object %S (error = %d)"), &fileObjectName, err );
243 SetBlockResult( EFail );
249 void CT_FileTextData::DoCmdWrite( const TDesC& aSection )
251 * writes one line of text into file using Write()
255 if ( GET_MANDATORY_STRING_PARAMETER( KParamText, aSection, writeLine ) )
258 INFO_PRINTF2( _L( "Write \"%S\" into file" ), &writeLine );
259 TInt err = iFileText->Write( writeLine );
260 if ( err != KErrNone)
262 ERR_PRINTF2( _L( "Function returned %d." ), err);
270 TBool CT_FileTextData::GetSeekMode( const TDesC& aSection, TSeek& aMode, TBool aOptional )
272 * retrieves "seek_mode" parameter value and converts it into its TSeek representation
273 * @param aSection - the entry in the ini file requiring the command to be processed
274 * @param aMode - the returned TSeek representation of "seek_mode" command parameter
275 * @param aOptional - represents the function which is called to retrieve the value
276 * KOptional - GET_OPTIONAL_STRING_PARAMETER is called
277 * KMandatory - GET_MANDATORY_STRING_PARAMETER is called
279 * @return ETrue if "seek_mode" parameter is present and has been sucessfully converted
282 TBool result = ETrue;
286 // Get "seek_mode" command parameter string value
289 result = GET_OPTIONAL_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
293 result = GET_MANDATORY_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
296 // Convert it into TSeek representation
299 if ( strSeekMode == KStrSeekEnd )
303 else if ( strSeekMode == KStrSeekStart )
307 else if ( strSeekMode == KStrSeekAddress )
309 aMode = ESeekAddress;
311 else if ( strSeekMode == KStrSeekCurrent )
313 aMode = ESeekCurrent;
317 ERR_PRINTF2( _L( "Unrecognized seek_mode value: %S" ), &strSeekMode );