os/kernelhwsrv/baseapitest/basesvs/common/src/DataWrapperBase.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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(KIncludeSection,		"include");
    24 _LIT(KFile,					"file%d");
    25 _LIT(KMatch,				"*{*,*}*");
    26 _LIT(KStart,				"{");
    27 _LIT(KSeparator,			",");
    28 _LIT(KEnd,					"}");
    29 _LIT(KDataRead,				"INI READ : %S %S %S");
    30 /*@}*/
    31 
    32 CDataWrapperBase::CDataWrapperBase()
    33 :	CDataWrapper()
    34 	{
    35 	}
    36 
    37 CDataWrapperBase::~CDataWrapperBase()
    38 /**
    39  * Public destructor
    40  */
    41 	{
    42 	iInclude.ResetAndDestroy();
    43 	iBuffer.ResetAndDestroy();
    44 	}
    45 
    46 void CDataWrapperBase::InitialiseL()
    47 	{
    48 	CDataWrapper::InitialiseL();
    49 
    50 	TBuf<KMaxTestExecuteCommandLength>	tempStore;
    51 	TPtrC		fileName;
    52 	TBool		moreData=ETrue;
    53 	TBool		index=0;
    54 	while ( moreData )
    55 		{
    56 		tempStore.Format(KFile(), ++index);
    57 		moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
    58 		
    59 		if (moreData)
    60 			{
    61 			CIniData*	iniData=CIniData::NewL(fileName);
    62 			CleanupStack::PushL(iniData);
    63 			iInclude.Append(iniData);
    64 			CleanupStack::Pop(iniData);
    65 			}
    66 		}
    67 	}
    68 
    69 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
    70 	{
    71 	TBool	ret=EFalse;
    72 	TPtrC	result;
    73 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
    74 	if ( err != KErrNone )
    75 		{
    76 		ret=EFalse;
    77 		}
    78 	if ( ret )
    79 		{
    80 		_LIT(KTrue,"true");
    81 		aResult=(result.FindF(KTrue) != KErrNotFound);
    82 		}
    83 
    84 	return ret;
    85 	}
    86 
    87 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
    88 	{
    89 	TPtrC	result;
    90 	TBool	ret=EFalse;
    91 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
    92 	if ( err != KErrNone )
    93 		{
    94 		ret=EFalse;
    95 		}
    96 	if ( ret )
    97 		{
    98 		TLex	lex(result);
    99 		ret=(lex.Val(aResult)==KErrNone);
   100 		}
   101 
   102 	return ret;
   103 	}
   104 
   105 TBool CDataWrapperBase::GetInt64FromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt64& aResult)
   106      {
   107      TPtrC     result;
   108      TBool     ret=EFalse;
   109      TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   110      if ( err != KErrNone )
   111           {
   112           ret=EFalse;
   113           }
   114      if ( ret )
   115           {
   116           TLex     lex(result);
   117           ret=(lex.Val(aResult)==KErrNone);
   118           }
   119 
   120      return ret;
   121      }
   122 
   123 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   124 	{
   125 	TBool	ret=EFalse;
   126 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
   127 	if ( err != KErrNone )
   128 		{
   129 		ret=EFalse;
   130 		}
   131 	return ret;
   132 	}
   133 
   134 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
   135 	{
   136 	TPtrC	result;
   137 	TBool	ret=EFalse;
   138 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
   139 	if ( err != KErrNone )
   140 		{
   141 		ret=EFalse;
   142 		}
   143 	if ( ret )
   144 		{
   145 		TLex	lex(result);
   146 		ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
   147 		}
   148 
   149 	return ret;
   150 	}
   151 
   152 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
   153 	{
   154 	TBool	ret=EFalse;
   155 
   156 	if ( aSectName.Length()!=0 )
   157 		{
   158 		ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
   159 
   160 		for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
   161 			{
   162 			ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
   163 			}
   164 		}
   165 
   166 	if ( ret )
   167 		{
   168 		if ( aResult.Match(KMatch)!=KErrNotFound )
   169 			{
   170 			//	We have an entry of the format
   171 			//	entry =*{section,entry}*
   172 			//	where * is one or more characters
   173 			//	We need to construct this from other data in the ini file replacing {*,*}
   174 			//	with the data from
   175 			//	[section]
   176 			//	entry =some_value
   177 			HBufC*	buffer=HBufC::NewLC(aResult.Length());
   178 			buffer->Des().Copy(aResult);
   179 
   180 			TInt	startLength=KStart().Length();
   181 			TInt	sparatorLength=KSeparator().Length();
   182 			TInt	endLength=KEnd().Length();
   183 			TInt	bufferLength;
   184 			TInt	start;
   185 			TInt	sparator;
   186 			TInt	end;
   187 			TPtrC	remaining;
   188 			TLex	lex;
   189 			do
   190 				{
   191 				bufferLength=buffer->Length();
   192 				start=buffer->Find(KStart);
   193 
   194 				remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
   195 				sparator=remaining.Find(KSeparator);
   196 				remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
   197 				sparator += (start + startLength);
   198 
   199 				end=remaining.Find(KEnd) + sparator + sparatorLength;
   200 
   201 				TPtrC	sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
   202 				TPtrC	keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
   203 				sectionName.Set(TLex(sectionName).NextToken());
   204 				keyName.Set(TLex(keyName).NextToken());
   205 
   206 				TInt	entrySize=0;
   207 				TPtrC	entryData;
   208 				TBool	found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
   209 				for ( TInt index=iInclude.Count(); (index>0) && (!found);  )
   210 					{
   211 					found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
   212 					}
   213 				if ( found )
   214 					{
   215 					entrySize=entryData.Length();
   216 					}
   217 
   218 				TInt	newLength=start + bufferLength - end - endLength + entrySize;
   219 				HBufC*	bufferNew=HBufC::NewLC(newLength);
   220 				bufferNew->Des().Copy(buffer->Ptr(), start);
   221 				if ( entrySize>0 )
   222 					{
   223 					bufferNew->Des().Append(entryData);
   224 					}
   225 				bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
   226 				CleanupStack::Pop(bufferNew);
   227 				CleanupStack::PopAndDestroy(buffer);
   228 				buffer=bufferNew;
   229 				CleanupStack::PushL(buffer);
   230 				}
   231 			while ( buffer->Match(KMatch)!=KErrNotFound );
   232 			iBuffer.Append(buffer);
   233 			CleanupStack::Pop(buffer);
   234 			aResult.Set(*buffer);
   235 			INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
   236 			}
   237 		}
   238 
   239 	return ret;
   240 	}
   241 
   242 TBool CDataWrapperBase::GetCommandStringParameter(const TDesC& aParameterName, const TDesC& aSection, TPtrC& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
   243 	{
   244 	TBool	ret = GetStringFromConfig(aSection, aParameterName, aResult);
   245 	if (aMandatory && !ret)
   246 		{
   247 		Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
   248 		SetBlockResult(EFail);
   249 		}
   250 	return ret;
   251 	}
   252 
   253 TBool CDataWrapperBase::GetCommandIntParameter(const TDesC& aParameterName, const TDesC& aSection, TInt& aResult, TText8* aFileName, TInt aLine, TBool aMandatory)
   254 	{
   255 	TBool	ret = GetIntFromConfig(aSection, aParameterName, aResult);
   256 	if (aMandatory && !ret)
   257 		{
   258 		Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
   259 		SetBlockResult(EFail);
   260 		}
   261 	return ret;
   262 	}
   263 	
   264 TBool CDataWrapperBase::GetCommandInt64Parameter(const TDesC& aParameterName, const TDesC& aSection, TInt64& aResult, TText8* aFileName, TInt aLine, TBool aMandatory)
   265 	{
   266 	TBool	ret = GetInt64FromConfig(aSection, aParameterName, aResult);
   267 	if (aMandatory && !ret)
   268 		{
   269 		Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
   270 		SetBlockResult(EFail);
   271 		}
   272 	return ret;
   273 	}
   274 
   275 TBool CDataWrapperBase::GetCommandBoolParameter(const TDesC& aParameterName, const TDesC& aSection, TBool& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
   276 	{
   277 	TBool	ret = GetBoolFromConfig(aSection, aParameterName, aResult);
   278 	if (aMandatory && !ret)
   279 		{
   280 		Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
   281 		SetBlockResult(EFail);
   282 		}
   283 	return ret;
   284 	}