First public contribution.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "DataWrapperBase.h"
22 /// Constant Literals used.
23 _LIT(KIncludeSection, "include");
24 _LIT(KFile, "file%d");
25 _LIT(KMatch, "*{*,*}*");
27 _LIT(KSeparator, ",");
29 _LIT(KDataRead, "INI READ : %S %S %S");
32 CDataWrapperBase::CDataWrapperBase()
37 CDataWrapperBase::~CDataWrapperBase()
42 iInclude.ResetAndDestroy();
43 iBuffer.ResetAndDestroy();
46 void CDataWrapperBase::InitialiseL()
48 CDataWrapper::InitialiseL();
50 TBuf<KMaxTestExecuteCommandLength> tempStore;
56 tempStore.Format(KFile(), ++index);
57 moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
61 CIniData* iniData=CIniData::NewL(fileName);
62 CleanupStack::PushL(iniData);
63 iInclude.Append(iniData);
64 CleanupStack::Pop(iniData);
69 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
73 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
74 if ( err != KErrNone )
81 aResult=(result.FindF(KTrue) != KErrNotFound);
87 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
91 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
92 if ( err != KErrNone )
99 ret=(lex.Val(aResult)==KErrNone);
105 TBool CDataWrapperBase::GetInt64FromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt64& aResult)
109 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
110 if ( err != KErrNone )
117 ret=(lex.Val(aResult)==KErrNone);
123 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
126 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
127 if ( err != KErrNone )
134 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
138 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
139 if ( err != KErrNone )
146 ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
152 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
156 if ( aSectName.Length()!=0 )
158 ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
160 for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
162 ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
168 if ( aResult.Match(KMatch)!=KErrNotFound )
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
177 HBufC* buffer=HBufC::NewLC(aResult.Length());
178 buffer->Des().Copy(aResult);
180 TInt startLength=KStart().Length();
181 TInt sparatorLength=KSeparator().Length();
182 TInt endLength=KEnd().Length();
191 bufferLength=buffer->Length();
192 start=buffer->Find(KStart);
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);
199 end=remaining.Find(KEnd) + sparator + sparatorLength;
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());
208 TBool found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
209 for ( TInt index=iInclude.Count(); (index>0) && (!found); )
211 found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
215 entrySize=entryData.Length();
218 TInt newLength=start + bufferLength - end - endLength + entrySize;
219 HBufC* bufferNew=HBufC::NewLC(newLength);
220 bufferNew->Des().Copy(buffer->Ptr(), start);
223 bufferNew->Des().Append(entryData);
225 bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
226 CleanupStack::Pop(bufferNew);
227 CleanupStack::PopAndDestroy(buffer);
229 CleanupStack::PushL(buffer);
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);
242 TBool CDataWrapperBase::GetCommandStringParameter(const TDesC& aParameterName, const TDesC& aSection, TPtrC& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
244 TBool ret = GetStringFromConfig(aSection, aParameterName, aResult);
245 if (aMandatory && !ret)
247 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
248 SetBlockResult(EFail);
253 TBool CDataWrapperBase::GetCommandIntParameter(const TDesC& aParameterName, const TDesC& aSection, TInt& aResult, TText8* aFileName, TInt aLine, TBool aMandatory)
255 TBool ret = GetIntFromConfig(aSection, aParameterName, aResult);
256 if (aMandatory && !ret)
258 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
259 SetBlockResult(EFail);
264 TBool CDataWrapperBase::GetCommandInt64Parameter(const TDesC& aParameterName, const TDesC& aSection, TInt64& aResult, TText8* aFileName, TInt aLine, TBool aMandatory)
266 TBool ret = GetInt64FromConfig(aSection, aParameterName, aResult);
267 if (aMandatory && !ret)
269 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
270 SetBlockResult(EFail);
275 TBool CDataWrapperBase::GetCommandBoolParameter(const TDesC& aParameterName, const TDesC& aSection, TBool& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
277 TBool ret = GetBoolFromConfig(aSection, aParameterName, aResult);
278 if (aMandatory && !ret)
280 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
281 SetBlockResult(EFail);