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 "datawrapperbase.h" sl@0: sl@0: /*@{*/ sl@0: /// Constant Literals used. sl@0: _LIT(KPrefixHex, "0x"); sl@0: _LIT(KPrefixOctal, "0"); sl@0: _LIT(KSuffixBinary, "b"); sl@0: sl@0: _LIT(KIncludeSection, "include"); sl@0: _LIT(KFile, "file%d"); sl@0: _LIT(KMatch, "*{*,*}*"); sl@0: _LIT(KStart, "{"); sl@0: _LIT(KSeparator, ","); sl@0: _LIT(KEnd, "}"); sl@0: _LIT(KDataRead, "INI READ : %S %S %S"); sl@0: sl@0: /*@}*/ sl@0: sl@0: CDataWrapperBase::CDataWrapperBase() sl@0: : CDataWrapper() sl@0: { sl@0: } sl@0: sl@0: /** sl@0: * Public destructor sl@0: */ sl@0: CDataWrapperBase::~CDataWrapperBase() sl@0: { sl@0: iInclude.ResetAndDestroy(); sl@0: iBuffer.ResetAndDestroy(); sl@0: iFs.Close(); sl@0: } sl@0: sl@0: void CDataWrapperBase::InitialiseL() sl@0: { sl@0: iTimer.CreateLocal(); sl@0: CDataWrapper::InitialiseL(); sl@0: TBuf tempStore; sl@0: TPtrC fileName; sl@0: TBool moreData=ETrue; sl@0: TBool index=0; sl@0: while ( moreData ) sl@0: { sl@0: tempStore.Format(KFile(), ++index); sl@0: moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName); sl@0: sl@0: if (moreData) sl@0: { sl@0: CIniData* iniData=CIniData::NewL(fileName); sl@0: CleanupStack::PushL(iniData); sl@0: iInclude.Append(iniData); sl@0: CleanupStack::Pop(iniData); sl@0: } sl@0: } sl@0: User::LeaveIfError(iFs.Connect()); sl@0: } sl@0: sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TBool reference passed in possible values TRUE, FALSE sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - The value of the boolean sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult) sl@0: { sl@0: TBool ret=EFalse; sl@0: TPtrC result; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: if ( ret ) sl@0: { sl@0: _LIT(KTrue,"true"); sl@0: aResult=(result.FindF(KTrue) != KErrNotFound); sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TInt reference passed in sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - The value of the integer sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult) sl@0: { sl@0: TPtrC result; sl@0: TBool ret=EFalse; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: if ( ret ) sl@0: { sl@0: TLex lex(result); sl@0: ret=(lex.Val(aResult)==KErrNone); sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TReal reference passed in sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - The value of the real sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetRealFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TReal& aResult) sl@0: { sl@0: TPtrC result; sl@0: TBool ret=EFalse; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret = EFalse; sl@0: } sl@0: if ( ret ) sl@0: { sl@0: TLex lex(result); sl@0: ret = ( lex.Val(aResult)==KErrNone ); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TPtrC reference passed in sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - Reference to the string on the heap sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult) sl@0: { sl@0: TBool ret=EFalse; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - The integer value of the Hex input sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult) sl@0: { sl@0: TPtrC result; sl@0: TBool ret=EFalse; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: if ( ret ) sl@0: { sl@0: TLex lex; sl@0: if( result.FindC(KPrefixHex)==KErrNone ) sl@0: { sl@0: lex=result.Mid(KPrefixHex().Length()); sl@0: } sl@0: else sl@0: { sl@0: lex=result; sl@0: } sl@0: ret=(lex.Val((TUint &)aResult, EHex)==KErrNone); sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Reads the value present from the test steps ini file within the mentioned section name and key name sl@0: * Copies the value to the TUint reference passed in. sl@0: * If the value is prefixed with 0x the value is read as a hexidecimal value sl@0: * If the value is suffixed with b the value is read as a binary value sl@0: * If the value is prefixed with a 0 the value is read as an octal value sl@0: * If it does not match the above it is read in as an integer sl@0: * @param aSectName - Section within the test steps ini file sl@0: * @param aKeyName - Name of a key within a section sl@0: * @return aResult - The integer value of the Hex input sl@0: * @return TBool - ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult) sl@0: { sl@0: TPtrC result; sl@0: TBool ret=EFalse; sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: if ( ret ) sl@0: { sl@0: TLex lex(result); sl@0: if( result.FindC(KPrefixHex)==KErrNone ) sl@0: { sl@0: lex=result.Mid(KPrefixHex().Length()); sl@0: ret=(lex.Val(aResult, EHex)==KErrNone); sl@0: } sl@0: else sl@0: { sl@0: TInt binarySuffixPosition=result.Length()-KSuffixBinary().Length(); sl@0: if ( result.FindC(KSuffixBinary)==binarySuffixPosition ) sl@0: { sl@0: lex=result.Left(binarySuffixPosition); sl@0: ret=(lex.Val(aResult, EBinary)==KErrNone); sl@0: } sl@0: else sl@0: { sl@0: if( result.FindC(KPrefixOctal)==KErrNone ) sl@0: { sl@0: ret=(lex.Val(aResult, EOctal)==KErrNone); sl@0: } sl@0: else sl@0: { sl@0: TInt intResult; sl@0: ret=(lex.Val(intResult)==KErrNone); sl@0: if ( ret ) sl@0: { sl@0: aResult=(TUint)intResult; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Return array of string parameters i.e. key=a1,a2,a3 returns array which contains sl@0: * String a1, a2 and a3. sl@0: * @return ret - EFalse if can't get a String parameter from Config file. ETrue if KErrNone sl@0: */ sl@0: TBool CDataWrapperBase::GetArrayRectFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray& aResult) sl@0: { sl@0: TBool ret=EFalse; sl@0: TPtrC completeArray; sl@0: sl@0: TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, completeArray)); sl@0: if ( err != KErrNone ) sl@0: { sl@0: ret=EFalse; sl@0: } sl@0: sl@0: TLex16 lex(completeArray); // Here we have the array as a string i.e. "a1,a2,a3" sl@0: TBuf<256> buf; sl@0: TChar chr; sl@0: sl@0: while(!lex.Eos()) sl@0: { sl@0: chr = lex.Get(); sl@0: // Check if there was a list separator sl@0: if ((chr == ',') && (lex.Peek() == '(')) sl@0: { sl@0: HBufC* param = buf.AllocLC(); sl@0: buf.Zero(); sl@0: aResult.Append(param); sl@0: CleanupStack::Pop(param); // pointer to buf is stored in RPointerArray sl@0: } sl@0: // If not separator character we can store the character into array sl@0: else sl@0: { sl@0: buf.Append(chr); sl@0: } sl@0: } sl@0: // Remember to put last token into array (,a3) sl@0: HBufC* param = buf.AllocLC(); sl@0: aResult.Append(param); sl@0: CleanupStack::Pop(param); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Reads the parameter asociated to the specified command sl@0: * @param aSectName Section on ini file sl@0: * @param aKeyName Name of the parameter sl@0: * @param aResult descriptor containing parameter sl@0: * @return TBool ETrue for found, EFalse for not found sl@0: */ sl@0: TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult) sl@0: { sl@0: TBool ret=EFalse; sl@0: sl@0: if ( aSectName.Length()!=0 ) sl@0: { sl@0: ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult); sl@0: sl@0: for ( TInt index=iInclude.Count(); (index>0) && (!ret); ) sl@0: { sl@0: ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult); sl@0: } sl@0: } sl@0: sl@0: if ( ret ) sl@0: { sl@0: if ( aResult.Match(KMatch)!=KErrNotFound ) sl@0: { sl@0: // We have an entry of the format sl@0: // entry =*{section,entry}* sl@0: // where * is one or more characters sl@0: // We need to construct this from other data in the ini file replacing {*,*} sl@0: // with the data from sl@0: // [section] sl@0: // entry =some_value sl@0: HBufC* buffer=HBufC::NewLC(aResult.Length()); sl@0: buffer->Des().Copy(aResult); sl@0: sl@0: TInt startLength=KStart().Length(); sl@0: TInt sparatorLength=KSeparator().Length(); sl@0: TInt endLength=KEnd().Length(); sl@0: TInt bufferLength; sl@0: TInt start; sl@0: TInt sparator; sl@0: TInt end; sl@0: TPtrC remaining; sl@0: TLex lex; sl@0: do sl@0: { sl@0: bufferLength=buffer->Length(); sl@0: start=buffer->Find(KStart); sl@0: sl@0: remaining.Set(buffer->Des().Right(bufferLength-start-startLength)); sl@0: sparator=remaining.Find(KSeparator); sl@0: remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength)); sl@0: sparator += (start + startLength); sl@0: sl@0: end=remaining.Find(KEnd) + sparator + sparatorLength; sl@0: sl@0: TPtrC sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength); sl@0: TPtrC keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength); sl@0: sectionName.Set(TLex(sectionName).NextToken()); sl@0: keyName.Set(TLex(keyName).NextToken()); sl@0: sl@0: TInt entrySize=0; sl@0: TPtrC entryData; sl@0: TBool found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData); sl@0: for ( TInt index=iInclude.Count(); (index>0) && (!found); ) sl@0: { sl@0: found=iInclude[--index]->FindVar(sectionName, keyName, entryData); sl@0: } sl@0: if ( found ) sl@0: { sl@0: entrySize=entryData.Length(); sl@0: } sl@0: sl@0: TInt newLength=start + bufferLength - end - endLength + entrySize; sl@0: HBufC* bufferNew=HBufC::NewLC(newLength); sl@0: bufferNew->Des().Copy(buffer->Ptr(), start); sl@0: if ( entrySize>0 ) sl@0: { sl@0: bufferNew->Des().Append(entryData); sl@0: } sl@0: bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength); sl@0: CleanupStack::Pop(bufferNew); sl@0: CleanupStack::PopAndDestroy(buffer); sl@0: buffer=bufferNew; sl@0: CleanupStack::PushL(buffer); sl@0: } sl@0: while ( buffer->Match(KMatch)!=KErrNotFound ); sl@0: iBuffer.Append(buffer); sl@0: CleanupStack::Pop(buffer); sl@0: aResult.Set(*buffer); sl@0: INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult); sl@0: } sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /** sl@0: * Utility function to produce time delay sl@0: * @param aTimeoutInSecs Times in micro seconds sl@0: */ sl@0: void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs) sl@0: { sl@0: TRequestStatus status; sl@0: iTimer.After(status, aTimeoutInSecs); sl@0: User::WaitForRequest(status); sl@0: } sl@0: sl@0: TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult) sl@0: { sl@0: TPtrC str; sl@0: TBool ret=GetStringFromConfig(aSectName, aKeyName, str); sl@0: sl@0: if ( ret ) sl@0: { sl@0: TBool found=EFalse; sl@0: TInt index=0; sl@0: while ( (aTable[index].iValue!=-1) && !found ) sl@0: { sl@0: if ( aTable[index].iString==str ) sl@0: { sl@0: found=ETrue; sl@0: aResult=aTable[index].iValue; sl@0: } sl@0: else sl@0: { sl@0: ++index; sl@0: } sl@0: } sl@0: sl@0: if ( !found ) sl@0: { sl@0: ret=GetIntFromConfig(aSectName, aKeyName, aResult); sl@0: } sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: