os/mm/mmapitest/devsoundexthaitest/common/src/DataWrapperBase.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "datawrapperbase.h"
    20 
    21 /*@{*/
    22 ///	Constant Literals used.
    23 _LIT(KPrefixHex,			"0x");
    24 _LIT(KPrefixOctal,			"0");
    25 _LIT(KSuffixBinary,			"b");
    26 
    27 _LIT(KIncludeSection,		"include");
    28 _LIT(KFile,					"file%d");
    29 _LIT(KMatch,				"*{*,*}*");
    30 _LIT(KStart,				"{");
    31 _LIT(KSeparator,			",");
    32 _LIT(KEnd,					"}");
    33 _LIT(KDataRead,				"INI READ : %S %S %S");
    34 
    35 /*@}*/
    36 
    37 CDataWrapperBase::CDataWrapperBase()
    38 :	CDataWrapper()
    39 	{
    40 	}
    41 
    42 /**
    43  * Public destructor
    44  */
    45 CDataWrapperBase::~CDataWrapperBase()
    46 	{
    47 	iInclude.ResetAndDestroy();
    48 	iBuffer.ResetAndDestroy();
    49 	iFs.Close();
    50 	}
    51 
    52 void CDataWrapperBase::InitialiseL()
    53 	{
    54 	iTimer.CreateLocal();
    55 	CDataWrapper::InitialiseL();
    56 	TBuf<KMaxTestExecuteCommandLength>	tempStore;
    57 	TPtrC		fileName;
    58 	TBool		moreData=ETrue;
    59 	TBool		index=0;
    60 	while ( moreData )
    61 		{
    62 		tempStore.Format(KFile(), ++index);
    63 		moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
    64 
    65 		if (moreData)
    66 			{
    67 			CIniData*	iniData=CIniData::NewL(fileName);
    68 			CleanupStack::PushL(iniData);
    69 			iInclude.Append(iniData);
    70 			CleanupStack::Pop(iniData);
    71 			}
    72 		}
    73 	User::LeaveIfError(iFs.Connect());
    74 	}
    75 
    76 /**
    77  * Reads the value present from the test steps ini file within the mentioned section name and key name
    78  * Copies the value to the TBool reference passed in possible values TRUE, FALSE
    79  * @param aSectName - Section within the test steps ini file
    80  * @param aKeyName - Name of a key within a section
    81  * @return aResult - The value of the boolean
    82  * @return TBool - ETrue for found, EFalse for not found 
    83  */
    84 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
    85 	{
    86 	TBool	ret=EFalse;
    87 	TPtrC	result;
    88 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
    89 	if ( err != KErrNone )
    90 		{
    91 		ret=EFalse;
    92 		}
    93 	if ( ret )
    94 		{
    95 		_LIT(KTrue,"true");
    96 		aResult=(result.FindF(KTrue) != KErrNotFound);
    97 		}
    98 
    99 	return ret;
   100 	}
   101 
   102 /**
   103  * Reads the value present from the test steps ini file within the mentioned section name and key name
   104  * Copies the value to the TInt reference passed in
   105  * @param aSectName - Section within the test steps ini file
   106  * @param aKeyName - Name of a key within a section
   107  * @return aResult - The value of the integer
   108  * @return TBool - ETrue for found, EFalse for not found 
   109  */
   110 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
   111 	{
   112 	TPtrC	result;
   113 	TBool	ret=EFalse;
   114 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   115 	if ( err != KErrNone )
   116 		{
   117 		ret=EFalse;
   118 		}
   119 	if ( ret )
   120 		{
   121 		TLex	lex(result);
   122 		ret=(lex.Val(aResult)==KErrNone);
   123 		}
   124 
   125 	return ret;
   126 	}
   127 /**
   128  * Reads the value present from the test steps ini file within the mentioned section name and key name
   129  * Copies the value to the TReal reference passed in
   130  * @param aSectName - Section within the test steps ini file
   131  * @param aKeyName - Name of a key within a section
   132  * @return aResult - The value of the real
   133  * @return TBool - ETrue for found, EFalse for not found 
   134  */
   135 TBool CDataWrapperBase::GetRealFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TReal& aResult)
   136 	{
   137 	TPtrC result;
   138 	TBool ret=EFalse;
   139 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   140 	if ( err != KErrNone )
   141 		{
   142 		ret = EFalse;
   143 		}
   144 	if ( ret )
   145 		{
   146 		TLex lex(result);
   147 		ret = ( lex.Val(aResult)==KErrNone );
   148 		}
   149 	return ret;
   150 	}
   151 
   152 /**
   153  * Reads the value present from the test steps ini file within the mentioned section name and key name
   154  * Copies the value to the TPtrC reference passed in
   155  * @param aSectName - Section within the test steps ini file
   156  * @param aKeyName - Name of a key within a section
   157  * @return aResult - Reference to the string on the heap
   158  * @return TBool - ETrue for found, EFalse for not found 
   159  */
   160 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   161 	{
   162 	TBool	ret=EFalse;
   163 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
   164 	if ( err != KErrNone )
   165 		{
   166 		ret=EFalse;
   167 		}
   168 	return ret;
   169 	}
   170 
   171 /**
   172  * Reads the value present from the test steps ini file within the mentioned section name and key name
   173  * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x
   174  * @param aSectName - Section within the test steps ini file
   175  * @param aKeyName - Name of a key within a section
   176  * @return aResult - The integer value of the Hex input
   177  * @return TBool - ETrue for found, EFalse for not found 
   178  */	
   179 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
   180 	{
   181 	TPtrC	result;
   182 	TBool	ret=EFalse;
   183 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   184 	if ( err != KErrNone )
   185 		{
   186 		ret=EFalse;
   187 		}
   188 	if ( ret )
   189 		{
   190 		TLex	lex;
   191 		if( result.FindC(KPrefixHex)==KErrNone )
   192 			{
   193 			lex=result.Mid(KPrefixHex().Length());
   194 			}
   195 		else
   196 			{
   197 			lex=result;
   198 			}
   199 		ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
   200 		}
   201 
   202 	return ret;
   203 	}
   204 
   205 /**
   206  * Reads the value present from the test steps ini file within the mentioned section name and key name
   207  * Copies the value to the TUint reference passed in.
   208  * If the value is prefixed with 0x the value is read as a hexidecimal value
   209  * If the value is suffixed with b the value is read as a binary value
   210  * If the value is prefixed with a 0 the value is read as an octal value
   211  * If it does not match the above it is read in as an integer
   212  * @param aSectName - Section within the test steps ini file
   213  * @param aKeyName - Name of a key within a section
   214  * @return aResult - The integer value of the Hex input
   215  * @return TBool - ETrue for found, EFalse for not found 
   216  */	
   217 TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult)
   218 	{
   219 	TPtrC	result;
   220 	TBool	ret=EFalse;
   221 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   222 	if ( err != KErrNone )
   223 		{
   224 		ret=EFalse;
   225 		}
   226 	if ( ret )
   227 		{
   228 		TLex	lex(result);
   229 		if( result.FindC(KPrefixHex)==KErrNone )
   230 			{
   231 			lex=result.Mid(KPrefixHex().Length());
   232 			ret=(lex.Val(aResult, EHex)==KErrNone);
   233 			}
   234 		else
   235 			{
   236 			TInt	binarySuffixPosition=result.Length()-KSuffixBinary().Length();
   237 			if ( result.FindC(KSuffixBinary)==binarySuffixPosition )
   238 				{
   239 				lex=result.Left(binarySuffixPosition);
   240 				ret=(lex.Val(aResult, EBinary)==KErrNone);
   241 				}
   242 			else
   243 				{
   244 				if( result.FindC(KPrefixOctal)==KErrNone )
   245 					{
   246 					ret=(lex.Val(aResult, EOctal)==KErrNone);
   247 					}
   248 				else
   249 					{
   250 					TInt	intResult;
   251 					ret=(lex.Val(intResult)==KErrNone);
   252 					if ( ret )
   253 						{
   254 						aResult=(TUint)intResult;
   255 						}
   256 					}
   257 				}
   258 			}
   259 		}
   260 
   261 	return ret;
   262 	}
   263 
   264 /**
   265  *   Return array of string parameters i.e. key=a1,a2,a3 returns array which contains
   266  *   String a1, a2 and a3.
   267  *   @return ret - EFalse if can't get a String parameter from Config file.  ETrue if KErrNone
   268  */
   269 TBool CDataWrapperBase::GetArrayRectFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray<HBufC>& aResult)
   270 	{
   271 	TBool	ret=EFalse;
   272 	TPtrC completeArray;
   273 	
   274 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, completeArray));
   275 	if ( err != KErrNone )
   276 		{
   277 		ret=EFalse;
   278 		}
   279 
   280     TLex16 lex(completeArray); // Here we have the array as a string i.e. "a1,a2,a3"
   281     TBuf<256> buf;
   282     TChar chr;
   283     
   284     while(!lex.Eos())
   285         {
   286         chr = lex.Get();
   287         // Check if there was a list separator
   288         if ((chr == ',') && (lex.Peek() == '('))
   289             {
   290             HBufC* param = buf.AllocLC();
   291             buf.Zero();
   292             aResult.Append(param);
   293             CleanupStack::Pop(param); // pointer to buf is stored in RPointerArray
   294             }
   295         // If not separator character we can store the character into array
   296         else
   297             {
   298             buf.Append(chr);
   299             }
   300         }
   301     // Remember to put last token into array (,a3)
   302     HBufC* param = buf.AllocLC();
   303     aResult.Append(param);
   304     CleanupStack::Pop(param);
   305     
   306     return ret;
   307 	}
   308 
   309 /**
   310  * Reads the parameter	asociated to the specified command
   311  * @param aSectName	Section on ini file
   312  * @param aKeyName		Name of the parameter
   313  * @param aResult			descriptor containing parameter
   314  * @return TBool  ETrue for found, EFalse for not found 
   315  */
   316 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   317 	{
   318 	TBool	ret=EFalse;
   319 
   320 	if ( aSectName.Length()!=0 )
   321 		{
   322 		ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
   323 
   324 		for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
   325 			{
   326 			ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
   327 			}
   328 		}
   329 
   330 	if ( ret )
   331 		{
   332 		if ( aResult.Match(KMatch)!=KErrNotFound )
   333 			{
   334 			//	We have an entry of the format
   335 			//	entry =*{section,entry}*
   336 			//	where * is one or more characters
   337 			//	We need to construct this from other data in the ini file replacing {*,*}
   338 			//	with the data from
   339 			//	[section]
   340 			//	entry =some_value
   341 			HBufC*	buffer=HBufC::NewLC(aResult.Length());
   342 			buffer->Des().Copy(aResult);
   343 
   344 			TInt	startLength=KStart().Length();
   345 			TInt	sparatorLength=KSeparator().Length();
   346 			TInt	endLength=KEnd().Length();
   347 			TInt	bufferLength;
   348 			TInt	start;
   349 			TInt	sparator;
   350 			TInt	end;
   351 			TPtrC	remaining;
   352 			TLex	lex;
   353 			do
   354 				{
   355 				bufferLength=buffer->Length();
   356 				start=buffer->Find(KStart);
   357 
   358 				remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
   359 				sparator=remaining.Find(KSeparator);
   360 				remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
   361 				sparator += (start + startLength);
   362 
   363 				end=remaining.Find(KEnd) + sparator + sparatorLength;
   364 
   365 				TPtrC	sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
   366 				TPtrC	keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
   367 				sectionName.Set(TLex(sectionName).NextToken());
   368 				keyName.Set(TLex(keyName).NextToken());
   369 
   370 				TInt	entrySize=0;
   371 				TPtrC	entryData;
   372 				TBool	found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
   373 				for ( TInt index=iInclude.Count(); (index>0) && (!found);  )
   374 					{
   375 					found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
   376 					}
   377 				if ( found )
   378 					{
   379 					entrySize=entryData.Length();
   380 					}
   381 
   382 				TInt	newLength=start + bufferLength - end - endLength + entrySize;
   383 				HBufC*	bufferNew=HBufC::NewLC(newLength);
   384 				bufferNew->Des().Copy(buffer->Ptr(), start);
   385 				if ( entrySize>0 )
   386 					{
   387 					bufferNew->Des().Append(entryData);
   388 					}
   389 				bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
   390 				CleanupStack::Pop(bufferNew);
   391 				CleanupStack::PopAndDestroy(buffer);
   392 				buffer=bufferNew;
   393 				CleanupStack::PushL(buffer);
   394 				}
   395 			while ( buffer->Match(KMatch)!=KErrNotFound );
   396 			iBuffer.Append(buffer);
   397 			CleanupStack::Pop(buffer);
   398 			aResult.Set(*buffer);
   399 			INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
   400 			}
   401 		}
   402 
   403 	return ret;
   404 	}
   405 
   406 /**
   407  * Utility function to produce time delay
   408  * @param aTimeoutInSecs Times in micro seconds
   409  */
   410 void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs)
   411 	{
   412 	TRequestStatus	status;
   413 	iTimer.After(status, aTimeoutInSecs);
   414 	User::WaitForRequest(status);
   415 	}
   416 
   417 TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult)
   418 	{
   419 	TPtrC	str;
   420 	TBool	ret=GetStringFromConfig(aSectName, aKeyName, str);
   421 
   422 	if ( ret )
   423 		{
   424 		TBool	found=EFalse;
   425 		TInt	index=0;
   426 		while ( (aTable[index].iValue!=-1) && !found )
   427 			{
   428 			if ( aTable[index].iString==str )
   429 				{
   430 				found=ETrue;
   431 				aResult=aTable[index].iValue;
   432 				}
   433 			else
   434 				{
   435 				++index;
   436 				}
   437 			}
   438 
   439 		if ( !found )
   440 			{
   441 			ret=GetIntFromConfig(aSectName, aKeyName, aResult);
   442 			}
   443 		}
   444 
   445 	return ret;
   446 	}
   447