os/graphics/graphicsapitest/screendriverhaitest/common/src/datawrapperbase.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicsapitest/screendriverhaitest/common/src/datawrapperbase.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,421 @@
     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 +#include "datawrapperbase.h"
    1.22 +
    1.23 +/*@{*/
    1.24 +///	Constant Literals used.
    1.25 +_LIT(KPrefixHex,"0x");
    1.26 +_LIT(KPrefixOctal,"0");
    1.27 +_LIT(KSuffixBinary,			"b");
    1.28 +
    1.29 +_LIT(KIncludeSection,		"include");
    1.30 +_LIT(KFile,					"file%d");
    1.31 +_LIT(KMatch,				"*{*,*}*");
    1.32 +_LIT(KStart,				"{");
    1.33 +_LIT(KSeparator,			",");
    1.34 +_LIT(KEnd,					"}");
    1.35 +_LIT(KDataRead,				"INI READ : %S %S %S");
    1.36 +
    1.37 +/*@}*/
    1.38 +
    1.39 +CDataWrapperBase::CDataWrapperBase()
    1.40 +:	CDataWrapper()
    1.41 +	{
    1.42 +	}
    1.43 +
    1.44 +/**
    1.45 + * Public destructor
    1.46 + */
    1.47 +CDataWrapperBase::~CDataWrapperBase()
    1.48 +	{
    1.49 +	iInclude.ResetAndDestroy();
    1.50 +	iBuffer.ResetAndDestroy();
    1.51 +	iFs.Close();
    1.52 +	}
    1.53 +
    1.54 +void CDataWrapperBase::InitialiseL()
    1.55 +	{
    1.56 +	iTimer.CreateLocal();
    1.57 +	CDataWrapper::InitialiseL();
    1.58 +	TBuf<KMaxTestExecuteCommandLength>	tempStore;
    1.59 +	TPtrC		fileName;
    1.60 +	TBool		moreData=ETrue;
    1.61 +	TBool		index=0;
    1.62 +	while ( moreData )
    1.63 +		{
    1.64 +		tempStore.Format(KFile(), ++index);
    1.65 +		moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
    1.66 +
    1.67 +		if (moreData)
    1.68 +			{
    1.69 +			CIniData*	iniData=CIniData::NewL(fileName);
    1.70 +			CleanupStack::PushL(iniData);
    1.71 +			iInclude.Append(iniData);
    1.72 +			CleanupStack::Pop(iniData);
    1.73 +			}
    1.74 +		}
    1.75 +	User::LeaveIfError(iFs.Connect());
    1.76 +	}
    1.77 +
    1.78 +/**
    1.79 + * Reads the value present from the test steps ini file within the mentioned section name and key name
    1.80 + * Copies the value to the TBool reference passed in possible values TRUE, FALSE
    1.81 + * @param aSectName - Section within the test steps ini file
    1.82 + * @param aKeyName - Name of a key within a section
    1.83 + * @return aResult - The value of the boolean
    1.84 + * @return TBool - ETrue for found, EFalse for not found 
    1.85 + */
    1.86 +TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
    1.87 +	{
    1.88 +	TBool	ret=EFalse;
    1.89 +	TPtrC	result;
    1.90 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
    1.91 +	if ( err != KErrNone )
    1.92 +		{
    1.93 +		ret=EFalse;
    1.94 +		}
    1.95 +	if ( ret )
    1.96 +		{
    1.97 +		_LIT(KTrue,"true");
    1.98 +		aResult=(result.FindF(KTrue) != KErrNotFound);
    1.99 +		}
   1.100 +
   1.101 +	return ret;
   1.102 +	}
   1.103 +
   1.104 +/**
   1.105 + * Reads the value present from the test steps ini file within the mentioned section name and key name
   1.106 + * Copies the value to the TInt reference passed in
   1.107 + * @param aSectName - Section within the test steps ini file
   1.108 + * @param aKeyName - Name of a key within a section
   1.109 + * @return aResult - The value of the integer
   1.110 + * @return TBool - ETrue for found, EFalse for not found 
   1.111 + */
   1.112 +TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
   1.113 +	{
   1.114 +	TPtrC	result;
   1.115 +	TBool	ret=EFalse;
   1.116 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   1.117 +	if ( err != KErrNone )
   1.118 +		{
   1.119 +		ret=EFalse;
   1.120 +		}
   1.121 +	if ( ret )
   1.122 +		{
   1.123 +		TLex	lex(result);
   1.124 +		ret=(lex.Val(aResult)==KErrNone);
   1.125 +		}
   1.126 +
   1.127 +	return ret;
   1.128 +	}
   1.129 +
   1.130 +/**
   1.131 + * Reads the value present from the test steps ini file within the mentioned section name and key name
   1.132 + * Copies the value to the TPtrC reference passed in
   1.133 + * @param aSectName - Section within the test steps ini file
   1.134 + * @param aKeyName - Name of a key within a section
   1.135 + * @return aResult - Reference to the string on the heap
   1.136 + * @return TBool - ETrue for found, EFalse for not found 
   1.137 + */
   1.138 +TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   1.139 +	{
   1.140 +	TBool	ret=EFalse;
   1.141 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
   1.142 +	if ( err != KErrNone )
   1.143 +		{
   1.144 +		ret=EFalse;
   1.145 +		}
   1.146 +	return ret;
   1.147 +	}
   1.148 +
   1.149 +/**
   1.150 + * Reads the value present from the test steps ini file within the mentioned section name and key name
   1.151 + * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x
   1.152 + * @param aSectName - Section within the test steps ini file
   1.153 + * @param aKeyName - Name of a key within a section
   1.154 + * @return aResult - The integer value of the Hex input
   1.155 + * @return TBool - ETrue for found, EFalse for not found 
   1.156 + */	
   1.157 +TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
   1.158 +	{
   1.159 +	TPtrC	result;
   1.160 +	TBool	ret=EFalse;
   1.161 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   1.162 +	if ( err != KErrNone )
   1.163 +		{
   1.164 +		ret=EFalse;
   1.165 +		}
   1.166 +	if ( ret )
   1.167 +		{
   1.168 +		TLex	lex;
   1.169 +		if( result.FindC(KPrefixHex)==KErrNone )
   1.170 +			{
   1.171 +			lex=result.Mid(KPrefixHex().Length());
   1.172 +			}
   1.173 +		else
   1.174 +			{
   1.175 +			lex=result;
   1.176 +			}
   1.177 +		ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
   1.178 +		}
   1.179 +
   1.180 +	return ret;
   1.181 +	}
   1.182 +
   1.183 +/**
   1.184 + * Reads the value present from the test steps ini file within the mentioned section name and key name
   1.185 + * Copies the value to the TUint reference passed in.
   1.186 + * If the value is prefixed with 0x the value is read as a hexidecimal value
   1.187 + * If the value is suffixed with b the value is read as a binary value
   1.188 + * If the value is prefixed with a 0 the value is read as an octal value
   1.189 + * If it does not match the above it is read in as an integer
   1.190 + * @param aSectName - Section within the test steps ini file
   1.191 + * @param aKeyName - Name of a key within a section
   1.192 + * @return aResult - The integer value of the Hex input
   1.193 + * @return TBool - ETrue for found, EFalse for not found 
   1.194 + */	
   1.195 +TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult)
   1.196 +	{
   1.197 +	TPtrC	result;
   1.198 +	TBool	ret=EFalse;
   1.199 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   1.200 +	if ( err != KErrNone )
   1.201 +		{
   1.202 +		ret=EFalse;
   1.203 +		}
   1.204 +	if ( ret )
   1.205 +		{
   1.206 +		TLex	lex(result);
   1.207 +		if( result.FindC(KPrefixHex)==KErrNone )
   1.208 +			{
   1.209 +			lex=result.Mid(KPrefixHex().Length());
   1.210 +			ret=(lex.Val(aResult, EHex)==KErrNone);
   1.211 +			}
   1.212 +		else
   1.213 +			{
   1.214 +			TInt	binarySuffixPosition=result.Length()-KSuffixBinary().Length();
   1.215 +			if ( result.FindC(KSuffixBinary)==binarySuffixPosition )
   1.216 +				{
   1.217 +				lex=result.Left(binarySuffixPosition);
   1.218 +				ret=(lex.Val(aResult, EBinary)==KErrNone);
   1.219 +				}
   1.220 +			else
   1.221 +				{
   1.222 +				if( result.FindC(KPrefixOctal)==KErrNone )
   1.223 +					{
   1.224 +					ret=(lex.Val(aResult, EOctal)==KErrNone);
   1.225 +					}
   1.226 +				else
   1.227 +					{
   1.228 +					TInt	intResult;
   1.229 +					ret=(lex.Val(intResult)==KErrNone);
   1.230 +					if ( ret )
   1.231 +						{
   1.232 +						aResult=(TUint)intResult;
   1.233 +						}
   1.234 +					}
   1.235 +				}
   1.236 +			}
   1.237 +		}
   1.238 +
   1.239 +	return ret;
   1.240 +	}
   1.241 +
   1.242 +/**
   1.243 + *   Return array of string parameters i.e. key=a1,a2,a3 returns array which contains
   1.244 + *   String a1, a2 and a3.
   1.245 + *   @return ret - EFalse if can't get a String parameter from Config file.  ETrue if KErrNone
   1.246 + */
   1.247 +TBool CDataWrapperBase::GetArrayRectFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray<HBufC>& aResult)
   1.248 +	{
   1.249 +	TBool	ret=EFalse;
   1.250 +	TPtrC completeArray;
   1.251 +	
   1.252 +	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, completeArray));
   1.253 +	if ( err != KErrNone )
   1.254 +		{
   1.255 +		ret=EFalse;
   1.256 +		}
   1.257 +
   1.258 +    TLex16 lex(completeArray); // Here we have the array as a string i.e. "a1,a2,a3"
   1.259 +    TBuf<256> buf;
   1.260 +    TChar chr;
   1.261 +    
   1.262 +    while(!lex.Eos())
   1.263 +        {
   1.264 +        chr = lex.Get();
   1.265 +        // Check if there was a list separator
   1.266 +        if ((chr == ',') && (lex.Peek() == '('))
   1.267 +            {
   1.268 +            HBufC* param = buf.AllocLC();
   1.269 +            buf.Zero();
   1.270 +            aResult.Append(param);
   1.271 +            CleanupStack::Pop(param); // pointer to buf is stored in RPointerArray
   1.272 +            }
   1.273 +        // If not separator character we can store the character into array
   1.274 +        else
   1.275 +            {
   1.276 +            buf.Append(chr);
   1.277 +            }
   1.278 +        }
   1.279 +    // Remember to put last token into array (,a3)
   1.280 +    HBufC* param = buf.AllocLC();
   1.281 +    aResult.Append(param);
   1.282 +    CleanupStack::Pop(param);
   1.283 +    
   1.284 +    return ret;
   1.285 +	}
   1.286 +
   1.287 +/**
   1.288 + * Reads the parameter	asociated to the specified command
   1.289 + * @param aSectName	Section on ini file
   1.290 + * @param aKeyName		Name of the parameter
   1.291 + * @param aResult			descriptor containing parameter
   1.292 + * @return TBool  ETrue for found, EFalse for not found 
   1.293 + */
   1.294 +TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   1.295 +	{
   1.296 +	TBool	ret=EFalse;
   1.297 +
   1.298 +	if ( aSectName.Length()!=0 )
   1.299 +		{
   1.300 +		ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
   1.301 +
   1.302 +		for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
   1.303 +			{
   1.304 +			ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
   1.305 +			}
   1.306 +		}
   1.307 +
   1.308 +	if ( ret )
   1.309 +		{
   1.310 +		if ( aResult.Match(KMatch)!=KErrNotFound )
   1.311 +			{
   1.312 +			//	We have an entry of the format
   1.313 +			//	entry =*{section,entry}*
   1.314 +			//	where * is one or more characters
   1.315 +			//	We need to construct this from other data in the ini file replacing {*,*}
   1.316 +			//	with the data from
   1.317 +			//	[section]
   1.318 +			//	entry =some_value
   1.319 +			HBufC*	buffer=HBufC::NewLC(aResult.Length());
   1.320 +			buffer->Des().Copy(aResult);
   1.321 +
   1.322 +			TInt	startLength=KStart().Length();
   1.323 +			TInt	sparatorLength=KSeparator().Length();
   1.324 +			TInt	endLength=KEnd().Length();
   1.325 +			TInt	bufferLength;
   1.326 +			TInt	start;
   1.327 +			TInt	sparator;
   1.328 +			TInt	end;
   1.329 +			TPtrC	remaining;
   1.330 +			TLex	lex;
   1.331 +			do
   1.332 +				{
   1.333 +				bufferLength=buffer->Length();
   1.334 +				start=buffer->Find(KStart);
   1.335 +
   1.336 +				remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
   1.337 +				sparator=remaining.Find(KSeparator);
   1.338 +				remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
   1.339 +				sparator += (start + startLength);
   1.340 +
   1.341 +				end=remaining.Find(KEnd) + sparator + sparatorLength;
   1.342 +
   1.343 +				TPtrC	sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
   1.344 +				TPtrC	keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
   1.345 +				sectionName.Set(TLex(sectionName).NextToken());
   1.346 +				keyName.Set(TLex(keyName).NextToken());
   1.347 +
   1.348 +				TInt	entrySize=0;
   1.349 +				TPtrC	entryData;
   1.350 +				TBool	found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
   1.351 +				for ( TInt index=iInclude.Count(); (index>0) && (!found);  )
   1.352 +					{
   1.353 +					found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
   1.354 +					}
   1.355 +				if ( found )
   1.356 +					{
   1.357 +					entrySize=entryData.Length();
   1.358 +					}
   1.359 +
   1.360 +				TInt	newLength=start + bufferLength - end - endLength + entrySize;
   1.361 +				HBufC*	bufferNew=HBufC::NewLC(newLength);
   1.362 +				bufferNew->Des().Copy(buffer->Ptr(), start);
   1.363 +				if ( entrySize>0 )
   1.364 +					{
   1.365 +					bufferNew->Des().Append(entryData);
   1.366 +					}
   1.367 +				bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
   1.368 +				CleanupStack::Pop(bufferNew);
   1.369 +				CleanupStack::PopAndDestroy(buffer);
   1.370 +				buffer=bufferNew;
   1.371 +				CleanupStack::PushL(buffer);
   1.372 +				}
   1.373 +			while ( buffer->Match(KMatch)!=KErrNotFound );
   1.374 +			iBuffer.Append(buffer);
   1.375 +			CleanupStack::Pop(buffer);
   1.376 +			aResult.Set(*buffer);
   1.377 +			INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
   1.378 +			}
   1.379 +		}
   1.380 +
   1.381 +	return ret;
   1.382 +	}
   1.383 +
   1.384 +TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult)
   1.385 +	{
   1.386 +	TPtrC	str;
   1.387 +	TBool	ret=GetStringFromConfig(aSectName, aKeyName, str);
   1.388 +
   1.389 +	if ( ret )
   1.390 +		{
   1.391 +		TBool	found=EFalse;
   1.392 +		TInt	index=0;
   1.393 +		while ( (aTable[index].iValue!=-1) && !found )
   1.394 +			{
   1.395 +			if ( aTable[index].iString==str )
   1.396 +				{
   1.397 +				found=ETrue;
   1.398 +				aResult=aTable[index].iValue;
   1.399 +				}
   1.400 +			else
   1.401 +				{
   1.402 +				++index;
   1.403 +				}
   1.404 +			}
   1.405 +
   1.406 +		if ( !found )
   1.407 +			{
   1.408 +			ret=GetIntFromConfig(aSectName, aKeyName, aResult);
   1.409 +			}
   1.410 +		}
   1.411 +
   1.412 +	return ret;
   1.413 +	}
   1.414 +
   1.415 +/**
   1.416 + * Utility function to produce time delay
   1.417 + * @param aTimeoutInSecs Times in micro seconds
   1.418 + */
   1.419 +void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs)
   1.420 +	{
   1.421 +	TRequestStatus	status;
   1.422 +	iTimer.After(status, aTimeoutInSecs);
   1.423 +	User::WaitForRequest(status);
   1.424 +	}