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(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: CDataWrapperBase::CDataWrapperBase() sl@0: : CDataWrapper() sl@0: { sl@0: } sl@0: sl@0: CDataWrapperBase::~CDataWrapperBase() sl@0: /** sl@0: * Public destructor sl@0: */ sl@0: { sl@0: iInclude.ResetAndDestroy(); sl@0: iBuffer.ResetAndDestroy(); sl@0: } sl@0: sl@0: void CDataWrapperBase::InitialiseL() sl@0: { sl@0: CDataWrapper::InitialiseL(); sl@0: 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: } 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: 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: TBool CDataWrapperBase::GetInt64FromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt64& 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: 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: 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(result); sl@0: ret=(lex.Val((TUint &)aResult, EHex)==KErrNone); sl@0: } sl@0: sl@0: return ret; sl@0: } 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: TBool CDataWrapperBase::GetCommandStringParameter(const TDesC& aParameterName, const TDesC& aSection, TPtrC& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory) sl@0: { sl@0: TBool ret = GetStringFromConfig(aSection, aParameterName, aResult); sl@0: if (aMandatory && !ret) sl@0: { sl@0: Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName); sl@0: SetBlockResult(EFail); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: TBool CDataWrapperBase::GetCommandIntParameter(const TDesC& aParameterName, const TDesC& aSection, TInt& aResult, TText8* aFileName, TInt aLine, TBool aMandatory) sl@0: { sl@0: TBool ret = GetIntFromConfig(aSection, aParameterName, aResult); sl@0: if (aMandatory && !ret) sl@0: { sl@0: Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName); sl@0: SetBlockResult(EFail); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: TBool CDataWrapperBase::GetCommandInt64Parameter(const TDesC& aParameterName, const TDesC& aSection, TInt64& aResult, TText8* aFileName, TInt aLine, TBool aMandatory) sl@0: { sl@0: TBool ret = GetInt64FromConfig(aSection, aParameterName, aResult); sl@0: if (aMandatory && !ret) sl@0: { sl@0: Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName); sl@0: SetBlockResult(EFail); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: TBool CDataWrapperBase::GetCommandBoolParameter(const TDesC& aParameterName, const TDesC& aSection, TBool& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory) sl@0: { sl@0: TBool ret = GetBoolFromConfig(aSection, aParameterName, aResult); sl@0: if (aMandatory && !ret) sl@0: { sl@0: Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName); sl@0: SetBlockResult(EFail); sl@0: } sl@0: return ret; sl@0: }