sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "T_FileTextData.h" sl@0: sl@0: // constants sl@0: const TInt KReadLength = 64; sl@0: sl@0: const TBool KMandatory = EFalse; sl@0: sl@0: _LIT( KStrSeekEnd, "ESeekEnd" ); sl@0: _LIT( KStrSeekStart, "ESeekStart" ); sl@0: _LIT( KStrSeekAddress, "ESeekAddress"); sl@0: _LIT( KStrSeekCurrent, "ESeekCurrent"); sl@0: // Commands sl@0: _LIT( KCmdDestructor, "~" ); sl@0: _LIT( KCmdNew, "new" ); sl@0: _LIT( KCmdRead, "Read" ); sl@0: _LIT( KCmdSeek, "Seek" ); sl@0: _LIT( KCmdSet, "Set" ); sl@0: _LIT( KCmdWrite, "Write" ); sl@0: sl@0: // Parameters sl@0: _LIT( KParamExpectedString, "expected_str" ); sl@0: _LIT( KParamObjectName, "object_name" ); sl@0: _LIT( KParamSeekMode, "seek_mode" ); sl@0: _LIT( KParamText, "text" ); sl@0: _LIT( KParamBufferLength, "buffer_length"); sl@0: sl@0: CT_FileTextData* CT_FileTextData::NewL() sl@0: /** sl@0: * Two phase constructor sl@0: */ sl@0: { sl@0: CT_FileTextData* ret = new (ELeave) CT_FileTextData(); sl@0: CleanupStack::PushL( ret ); sl@0: ret->ConstructL(); sl@0: CleanupStack::Pop( ret ); sl@0: return ret; sl@0: } sl@0: sl@0: CT_FileTextData::CT_FileTextData(): sl@0: iFileText(NULL) sl@0: /** sl@0: * Protected constructor. First phase construction sl@0: */ sl@0: { sl@0: } sl@0: sl@0: void CT_FileTextData::ConstructL() sl@0: /** sl@0: * Protected constructor. Second phase construction sl@0: */ sl@0: { sl@0: } sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: CT_FileTextData::~CT_FileTextData() sl@0: { sl@0: DoCleanup(); sl@0: } sl@0: sl@0: TAny* CT_FileTextData::GetObject() sl@0: /** sl@0: * Return a pointer to the object that the data wraps sl@0: * sl@0: * @return pointer to the object that the data wraps sl@0: */ sl@0: { sl@0: return iFileText; sl@0: } sl@0: sl@0: TBool CT_FileTextData::DoCommandL( const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/ ) sl@0: /** sl@0: * Process a command read from the ini file sl@0: * sl@0: * @param aCommand the command to process sl@0: * @param aSection the entry in the ini file requiring the command to be processed sl@0: * sl@0: * @return ETrue if the command is processed sl@0: */ sl@0: { sl@0: TBool retVal = ETrue; sl@0: sl@0: if ( aCommand == KCmdDestructor ) sl@0: { sl@0: DoCleanup(); sl@0: } sl@0: else if ( aCommand == KCmdNew ) sl@0: { sl@0: DoCmdNew(); sl@0: } sl@0: else if ( aCommand == KCmdRead ) sl@0: { sl@0: DoCmdRead( aSection ); sl@0: } sl@0: else if ( aCommand == KCmdSeek ) sl@0: { sl@0: DoCmdSeek( aSection ); sl@0: } sl@0: else if ( aCommand == KCmdSet ) sl@0: { sl@0: DoCmdSet( aSection ); sl@0: } sl@0: else if ( aCommand == KCmdWrite ) sl@0: { sl@0: DoCmdWrite( aSection ); sl@0: } sl@0: else sl@0: { sl@0: retVal = EFalse; sl@0: } sl@0: sl@0: return retVal; sl@0: } sl@0: sl@0: void CT_FileTextData::DoCleanup() sl@0: /** sl@0: * Deletes TFileText class instance sl@0: */ sl@0: { sl@0: INFO_PRINTF1( _L( "Delete TFileText class instance" ) ); sl@0: delete iFileText; sl@0: iFileText = NULL; sl@0: } sl@0: sl@0: void CT_FileTextData::DoCmdNew() sl@0: /** sl@0: * Creates new TFileText class instance sl@0: */ sl@0: { sl@0: DoCleanup(); sl@0: sl@0: INFO_PRINTF1( _L( "Create new TFileText class instance." ) ); sl@0: TRAPD( err, iFileText = new (ELeave) TFileText() ); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ERR_PRINTF2( _L( "new TFileText() error %d" ), err ); sl@0: SetError( err ); sl@0: } sl@0: } sl@0: sl@0: void CT_FileTextData::DoCmdRead( const TDesC& aSection ) sl@0: /** sl@0: * Reads one line form file using Read() sl@0: */ sl@0: { sl@0: INFO_PRINTF1( _L( "Read one line from file." ) ); sl@0: sl@0: RBuf readLine; sl@0: TInt bufferLength = KReadLength; sl@0: sl@0: GET_OPTIONAL_INT_PARAMETER( KParamBufferLength, aSection, bufferLength ); sl@0: TInt err = readLine.Create(bufferLength); sl@0: sl@0: if(err == KErrNone) sl@0: { sl@0: err = iFileText->Read( readLine ); sl@0: sl@0: INFO_PRINTF2( _L( "FileText::Read() result - %S" ), &readLine ); sl@0: sl@0: if ( err != KErrNone ) sl@0: { sl@0: ERR_PRINTF2( _L( "Function returned error %d." ), err ); sl@0: SetError( err ); sl@0: } sl@0: else sl@0: { sl@0: TPtrC expectedLine; sl@0: if ( GET_OPTIONAL_STRING_PARAMETER( KParamExpectedString, aSection, expectedLine ) ) sl@0: { sl@0: if ( readLine.Compare(expectedLine)!=0 ) sl@0: { sl@0: ERR_PRINTF3( _L( "Read line \"%S\", expected \"%S\"." ), &readLine, &expectedLine ); sl@0: SetBlockResult( EFail ); sl@0: } sl@0: } sl@0: } sl@0: readLine.Close(); sl@0: } sl@0: else sl@0: { sl@0: SetBlockResult( EFail ); sl@0: ERR_PRINTF1( _L( "RBuf initialization failed." )); sl@0: } sl@0: } sl@0: sl@0: void CT_FileTextData::DoCmdSeek( const TDesC& aSection ) sl@0: /** sl@0: * performs a seek to start or end of file using Seek() sl@0: */ sl@0: { sl@0: TSeek mode; sl@0: if ( GetSeekMode( aSection, mode, KMandatory ) ) sl@0: { sl@0: TInt err = iFileText->Seek( mode ); sl@0: sl@0: if ( err != KErrNone ) sl@0: { sl@0: ERR_PRINTF2( _L( "Function returned %d." ), err); sl@0: SetError( err ); sl@0: } sl@0: } sl@0: } sl@0: sl@0: void CT_FileTextData::DoCmdSet( const TDesC& aSection ) sl@0: /** sl@0: * Sets the file to be read from or written to using Set() sl@0: */ sl@0: { sl@0: TPtrC fileObjectName; sl@0: if ( GET_MANDATORY_STRING_PARAMETER( KParamObjectName, aSection, fileObjectName ) ) sl@0: { sl@0: INFO_PRINTF2( _L( "Set( %S )" ), &fileObjectName ); sl@0: sl@0: RFile* fileObject = NULL; sl@0: TRAPD( err, fileObject = (RFile*)GetDataObjectL( fileObjectName ) ); sl@0: sl@0: if ( err == KErrNone ) sl@0: { sl@0: iFileText->Set( *fileObject ); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF3( _L( "Unable to access object %S (error = %d)"), &fileObjectName, err ); sl@0: SetBlockResult( EFail ); sl@0: } sl@0: sl@0: } sl@0: } sl@0: sl@0: void CT_FileTextData::DoCmdWrite( const TDesC& aSection ) sl@0: /** sl@0: * writes one line of text into file using Write() sl@0: */ sl@0: { sl@0: TPtrC writeLine; sl@0: if ( GET_MANDATORY_STRING_PARAMETER( KParamText, aSection, writeLine ) ) sl@0: { sl@0: sl@0: INFO_PRINTF2( _L( "Write \"%S\" into file" ), &writeLine ); sl@0: TInt err = iFileText->Write( writeLine ); sl@0: if ( err != KErrNone) sl@0: { sl@0: ERR_PRINTF2( _L( "Function returned %d." ), err); sl@0: SetError( err ); sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: TBool CT_FileTextData::GetSeekMode( const TDesC& aSection, TSeek& aMode, TBool aOptional ) sl@0: /** sl@0: * retrieves "seek_mode" parameter value and converts it into its TSeek representation sl@0: * @param aSection - the entry in the ini file requiring the command to be processed sl@0: * @param aMode - the returned TSeek representation of "seek_mode" command parameter sl@0: * @param aOptional - represents the function which is called to retrieve the value sl@0: * KOptional - GET_OPTIONAL_STRING_PARAMETER is called sl@0: * KMandatory - GET_MANDATORY_STRING_PARAMETER is called sl@0: * sl@0: * @return ETrue if "seek_mode" parameter is present and has been sucessfully converted sl@0: */ sl@0: { sl@0: TBool result = ETrue; sl@0: sl@0: TPtrC strSeekMode; sl@0: sl@0: // Get "seek_mode" command parameter string value sl@0: if ( aOptional ) sl@0: { sl@0: result = GET_OPTIONAL_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode ); sl@0: } sl@0: else sl@0: { sl@0: result = GET_MANDATORY_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode ); sl@0: } sl@0: sl@0: // Convert it into TSeek representation sl@0: if ( result ) sl@0: { sl@0: if ( strSeekMode == KStrSeekEnd ) sl@0: { sl@0: aMode = ESeekEnd; sl@0: } sl@0: else if ( strSeekMode == KStrSeekStart ) sl@0: { sl@0: aMode = ESeekStart; sl@0: } sl@0: else if ( strSeekMode == KStrSeekAddress ) sl@0: { sl@0: aMode = ESeekAddress; sl@0: } sl@0: else if ( strSeekMode == KStrSeekCurrent ) sl@0: { sl@0: aMode = ESeekCurrent; sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF2( _L( "Unrecognized seek_mode value: %S" ), &strSeekMode ); sl@0: result = EFalse; sl@0: } sl@0: } sl@0: sl@0: return result; sl@0: } sl@0: sl@0: