os/kernelhwsrv/baseapitest/basesvs/validation/f32/sfsrv/src/T_FileTextData.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/baseapitest/basesvs/validation/f32/sfsrv/src/T_FileTextData.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "T_FileTextData.h"
    1.23 +
    1.24 +// constants
    1.25 +const TInt	KReadLength		= 64;
    1.26 +
    1.27 +const TBool KMandatory			= EFalse;
    1.28 +
    1.29 +_LIT( KStrSeekEnd,				"ESeekEnd" );
    1.30 +_LIT( KStrSeekStart,			"ESeekStart" );
    1.31 +_LIT( KStrSeekAddress,			"ESeekAddress");
    1.32 +_LIT( KStrSeekCurrent,			"ESeekCurrent");
    1.33 +// Commands
    1.34 +_LIT( KCmdDestructor,			"~" );
    1.35 +_LIT( KCmdNew,					"new" );
    1.36 +_LIT( KCmdRead,					"Read" );
    1.37 +_LIT( KCmdSeek,					"Seek" );
    1.38 +_LIT( KCmdSet,					"Set" );
    1.39 +_LIT( KCmdWrite,				"Write" );
    1.40 +
    1.41 +// Parameters
    1.42 +_LIT( KParamExpectedString,		"expected_str" );
    1.43 +_LIT( KParamObjectName,			"object_name" );
    1.44 +_LIT( KParamSeekMode,			"seek_mode" );
    1.45 +_LIT( KParamText,				"text" );
    1.46 +_LIT( KParamBufferLength,		"buffer_length");
    1.47 +
    1.48 +CT_FileTextData* CT_FileTextData::NewL()
    1.49 +/**
    1.50 +* Two phase constructor
    1.51 +*/
    1.52 +	{
    1.53 +	CT_FileTextData* ret = new (ELeave) CT_FileTextData();
    1.54 +	CleanupStack::PushL( ret );
    1.55 +	ret->ConstructL();
    1.56 +	CleanupStack::Pop( ret );
    1.57 +	return ret;
    1.58 +	}
    1.59 +
    1.60 +CT_FileTextData::CT_FileTextData():
    1.61 +iFileText(NULL)
    1.62 +/**
    1.63 +* Protected constructor. First phase construction
    1.64 +*/
    1.65 +	{
    1.66 +	}
    1.67 +	
    1.68 +void CT_FileTextData::ConstructL()
    1.69 +/**
    1.70 +* Protected constructor. Second phase construction
    1.71 +*/
    1.72 +	{
    1.73 +	}
    1.74 +	
    1.75 +/**
    1.76 +* Destructor.
    1.77 +*/
    1.78 +CT_FileTextData::~CT_FileTextData()
    1.79 +	{
    1.80 +	DoCleanup();
    1.81 +	}
    1.82 +	
    1.83 +TAny* CT_FileTextData::GetObject()
    1.84 +/**
    1.85 +* Return a pointer to the object that the data wraps
    1.86 +*
    1.87 +* @return pointer to the object that the data wraps
    1.88 +*/
    1.89 +	{
    1.90 +	return iFileText;
    1.91 +	}
    1.92 +
    1.93 +TBool CT_FileTextData::DoCommandL( const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/ )
    1.94 +/**
    1.95 +* Process a command read from the ini file
    1.96 +*
    1.97 +* @param aCommand	the command to process
    1.98 +* @param aSection		the entry in the ini file requiring the command to be processed
    1.99 +*
   1.100 +* @return ETrue if the command is processed
   1.101 +*/
   1.102 +	{
   1.103 +	TBool retVal = ETrue;
   1.104 +	
   1.105 +	if ( aCommand == KCmdDestructor )
   1.106 +		{
   1.107 +		DoCleanup();
   1.108 +		}
   1.109 +	else if ( aCommand == KCmdNew )
   1.110 +		{
   1.111 +		DoCmdNew();
   1.112 +		}
   1.113 +	else if ( aCommand == KCmdRead )
   1.114 +		{
   1.115 +		DoCmdRead( aSection );
   1.116 +		}
   1.117 +	else if ( aCommand == KCmdSeek )
   1.118 +		{
   1.119 +		DoCmdSeek( aSection );
   1.120 +		}
   1.121 +	else if ( aCommand == KCmdSet )
   1.122 +		{
   1.123 +		DoCmdSet( aSection );
   1.124 +		}
   1.125 +	else if ( aCommand == KCmdWrite )
   1.126 +		{
   1.127 +		DoCmdWrite( aSection );
   1.128 +		}
   1.129 +	else
   1.130 +	    {
   1.131 +	    retVal = EFalse;    
   1.132 +	    }
   1.133 +	
   1.134 +	return retVal;
   1.135 +	}
   1.136 +	
   1.137 +void CT_FileTextData::DoCleanup()
   1.138 +/**
   1.139 +* Deletes TFileText class instance
   1.140 +*/
   1.141 +    {
   1.142 +	INFO_PRINTF1( _L( "Delete TFileText class instance" ) );
   1.143 +	delete iFileText;
   1.144 +	iFileText = NULL;
   1.145 +	}
   1.146 +	
   1.147 +void CT_FileTextData::DoCmdNew()
   1.148 +/**
   1.149 +* Creates new TFileText class instance
   1.150 +*/
   1.151 +	{
   1.152 +	DoCleanup();
   1.153 +	
   1.154 +	INFO_PRINTF1( _L( "Create new TFileText class instance." ) );
   1.155 +	TRAPD( err, iFileText = new (ELeave) TFileText() );
   1.156 +	if ( err != KErrNone )
   1.157 +		{
   1.158 +		ERR_PRINTF2( _L( "new TFileText() error %d" ), err );
   1.159 +		SetError( err );
   1.160 +		}
   1.161 +	}
   1.162 +	
   1.163 +void CT_FileTextData::DoCmdRead( const TDesC& aSection )
   1.164 +/**
   1.165 +* Reads one line form file using Read()
   1.166 +*/
   1.167 +	{
   1.168 +	INFO_PRINTF1( _L( "Read one line from file." ) );
   1.169 +	
   1.170 +	RBuf readLine;
   1.171 +	TInt bufferLength = KReadLength;
   1.172 +	
   1.173 +	GET_OPTIONAL_INT_PARAMETER( KParamBufferLength, aSection, bufferLength );
   1.174 +	TInt err = readLine.Create(bufferLength);
   1.175 +		
   1.176 +	if(err == KErrNone)
   1.177 +		{
   1.178 +		err = iFileText->Read( readLine );
   1.179 +	
   1.180 +		INFO_PRINTF2( _L( "FileText::Read() result - %S" ), &readLine );
   1.181 +		
   1.182 +		if ( err != KErrNone )
   1.183 +			{
   1.184 +			ERR_PRINTF2( _L( "Function returned error %d." ), err );
   1.185 +			SetError( err );
   1.186 +			}
   1.187 +		else
   1.188 +			{
   1.189 +			TPtrC expectedLine;
   1.190 +			if ( GET_OPTIONAL_STRING_PARAMETER( KParamExpectedString, aSection, expectedLine ) )
   1.191 +				{
   1.192 +				if ( readLine.Compare(expectedLine)!=0 )
   1.193 +					{
   1.194 +					ERR_PRINTF3( _L( "Read line \"%S\", expected \"%S\"." ), &readLine, &expectedLine );
   1.195 +					SetBlockResult( EFail );
   1.196 +					}
   1.197 +				}
   1.198 +			}
   1.199 +		readLine.Close();
   1.200 +		}
   1.201 +	else
   1.202 +		{
   1.203 +		SetBlockResult( EFail );
   1.204 +		ERR_PRINTF1( _L( "RBuf initialization failed." ));
   1.205 +		}
   1.206 +	}
   1.207 +	
   1.208 +void CT_FileTextData::DoCmdSeek( const TDesC& aSection )
   1.209 +/**
   1.210 +* performs a seek to start or end of file using Seek()
   1.211 +*/
   1.212 +	{
   1.213 +	TSeek mode;
   1.214 +	if ( GetSeekMode( aSection, mode, KMandatory ) )
   1.215 +		{
   1.216 +		TInt err = iFileText->Seek( mode );
   1.217 +	
   1.218 +		if ( err != KErrNone )
   1.219 +			{
   1.220 +			ERR_PRINTF2( _L( "Function returned %d." ), err);
   1.221 +			SetError( err );
   1.222 +			}
   1.223 +		}
   1.224 +	}
   1.225 +
   1.226 +void CT_FileTextData::DoCmdSet( const TDesC& aSection )
   1.227 +/**
   1.228 +* Sets the file to be read from or written to using Set()
   1.229 +*/
   1.230 +	{
   1.231 +	TPtrC fileObjectName;
   1.232 +	if ( GET_MANDATORY_STRING_PARAMETER( KParamObjectName, aSection, fileObjectName ) )
   1.233 +		{
   1.234 +		INFO_PRINTF2( _L( "Set( %S )" ), &fileObjectName );
   1.235 +		
   1.236 +		RFile* fileObject = NULL;
   1.237 +		TRAPD( err, fileObject = (RFile*)GetDataObjectL( fileObjectName ) );
   1.238 +
   1.239 +		if ( err == KErrNone )
   1.240 +			{
   1.241 +			iFileText->Set( *fileObject );
   1.242 +			}
   1.243 +		else
   1.244 +			{
   1.245 +			ERR_PRINTF3( _L( "Unable to access object %S (error = %d)"), &fileObjectName, err );
   1.246 +			SetBlockResult( EFail );
   1.247 +			}
   1.248 +		
   1.249 +		}
   1.250 +	}
   1.251 +	
   1.252 +void CT_FileTextData::DoCmdWrite( const TDesC& aSection )
   1.253 +/**
   1.254 +* writes one line of text into file using Write()
   1.255 +*/
   1.256 +	{
   1.257 +	TPtrC writeLine;
   1.258 +	if ( GET_MANDATORY_STRING_PARAMETER( KParamText, aSection, writeLine ) )
   1.259 +		{
   1.260 +		
   1.261 +		INFO_PRINTF2( _L( "Write \"%S\" into file" ), &writeLine );
   1.262 +		TInt err = iFileText->Write( writeLine );
   1.263 +		if ( err != KErrNone)
   1.264 +			{
   1.265 +			ERR_PRINTF2( _L( "Function returned %d." ), err);
   1.266 +			SetError( err );
   1.267 +			}
   1.268 +		}
   1.269 +	}
   1.270 +
   1.271 +	
   1.272 +
   1.273 +TBool CT_FileTextData::GetSeekMode( const TDesC& aSection, TSeek& aMode, TBool aOptional )
   1.274 +/** 
   1.275 +* retrieves "seek_mode" parameter value and converts it into its TSeek representation
   1.276 +* @param aSection 	- the entry in the ini file requiring the command to be processed
   1.277 +* @param aMode		- the returned TSeek representation of "seek_mode" command parameter
   1.278 +* @param aOptional	- represents the function which is called to retrieve the value
   1.279 +*					KOptional	- GET_OPTIONAL_STRING_PARAMETER is called
   1.280 +*					KMandatory	- GET_MANDATORY_STRING_PARAMETER is called
   1.281 +*
   1.282 +* @return ETrue if "seek_mode" parameter is present and has been sucessfully converted
   1.283 +*/
   1.284 +	{
   1.285 +	TBool result = ETrue;
   1.286 +	
   1.287 +	TPtrC strSeekMode;
   1.288 +	
   1.289 +	// Get "seek_mode" command parameter string value
   1.290 +	if ( aOptional )
   1.291 +		{
   1.292 +		result = GET_OPTIONAL_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
   1.293 +		}
   1.294 +	else
   1.295 +		{
   1.296 +		result = GET_MANDATORY_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
   1.297 +		}
   1.298 +	
   1.299 +	// Convert it into TSeek representation
   1.300 +	if ( result )
   1.301 +		{
   1.302 +		if ( strSeekMode == KStrSeekEnd )
   1.303 +			{
   1.304 +			aMode = ESeekEnd;
   1.305 +			}
   1.306 +		else if ( strSeekMode == KStrSeekStart )
   1.307 +			{
   1.308 +			aMode = ESeekStart;
   1.309 +			}
   1.310 +		else if ( strSeekMode == KStrSeekAddress )
   1.311 +			{
   1.312 +			aMode = ESeekAddress;
   1.313 +			}
   1.314 +		else if ( strSeekMode == KStrSeekCurrent )
   1.315 +			{
   1.316 +			aMode = ESeekCurrent;
   1.317 +			}
   1.318 +		else
   1.319 +			{
   1.320 +			ERR_PRINTF2( _L( "Unrecognized seek_mode value: %S" ), &strSeekMode );
   1.321 +			result = EFalse;
   1.322 +			}
   1.323 +		}
   1.324 +			
   1.325 +	return result;
   1.326 +	}
   1.327 + 
   1.328 +