1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmapitest/devsoundhaitest/common/src/DataWrapperBase.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,447 @@
1.4 +/*
1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "datawrapperbase.h"
1.23 +
1.24 +/*@{*/
1.25 +/// Constant Literals used.
1.26 +_LIT(KPrefixHex, "0x");
1.27 +_LIT(KPrefixOctal, "0");
1.28 +_LIT(KSuffixBinary, "b");
1.29 +
1.30 +_LIT(KIncludeSection, "include");
1.31 +_LIT(KFile, "file%d");
1.32 +_LIT(KMatch, "*{*,*}*");
1.33 +_LIT(KStart, "{");
1.34 +_LIT(KSeparator, ",");
1.35 +_LIT(KEnd, "}");
1.36 +_LIT(KDataRead, "INI READ : %S %S %S");
1.37 +
1.38 +/*@}*/
1.39 +
1.40 +CDataWrapperBase::CDataWrapperBase()
1.41 +: CDataWrapper()
1.42 + {
1.43 + }
1.44 +
1.45 +/**
1.46 + * Public destructor
1.47 + */
1.48 +CDataWrapperBase::~CDataWrapperBase()
1.49 + {
1.50 + iInclude.ResetAndDestroy();
1.51 + iBuffer.ResetAndDestroy();
1.52 + iFs.Close();
1.53 + }
1.54 +
1.55 +void CDataWrapperBase::InitialiseL()
1.56 + {
1.57 + iTimer.CreateLocal();
1.58 + CDataWrapper::InitialiseL();
1.59 + TBuf<KMaxTestExecuteCommandLength> tempStore;
1.60 + TPtrC fileName;
1.61 + TBool moreData=ETrue;
1.62 + TBool index=0;
1.63 + while ( moreData )
1.64 + {
1.65 + tempStore.Format(KFile(), ++index);
1.66 + moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
1.67 +
1.68 + if (moreData)
1.69 + {
1.70 + CIniData* iniData=CIniData::NewL(fileName);
1.71 + CleanupStack::PushL(iniData);
1.72 + iInclude.Append(iniData);
1.73 + CleanupStack::Pop(iniData);
1.74 + }
1.75 + }
1.76 + User::LeaveIfError(iFs.Connect());
1.77 + }
1.78 +
1.79 +/**
1.80 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.81 + * Copies the value to the TBool reference passed in possible values TRUE, FALSE
1.82 + * @param aSectName - Section within the test steps ini file
1.83 + * @param aKeyName - Name of a key within a section
1.84 + * @return aResult - The value of the boolean
1.85 + * @return TBool - ETrue for found, EFalse for not found
1.86 + */
1.87 +TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
1.88 + {
1.89 + TBool ret=EFalse;
1.90 + TPtrC result;
1.91 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
1.92 + if ( err != KErrNone )
1.93 + {
1.94 + ret=EFalse;
1.95 + }
1.96 + if ( ret )
1.97 + {
1.98 + _LIT(KTrue,"true");
1.99 + aResult=(result.FindF(KTrue) != KErrNotFound);
1.100 + }
1.101 +
1.102 + return ret;
1.103 + }
1.104 +
1.105 +/**
1.106 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.107 + * Copies the value to the TInt reference passed in
1.108 + * @param aSectName - Section within the test steps ini file
1.109 + * @param aKeyName - Name of a key within a section
1.110 + * @return aResult - The value of the integer
1.111 + * @return TBool - ETrue for found, EFalse for not found
1.112 + */
1.113 +TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
1.114 + {
1.115 + TPtrC result;
1.116 + TBool ret=EFalse;
1.117 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
1.118 + if ( err != KErrNone )
1.119 + {
1.120 + ret=EFalse;
1.121 + }
1.122 + if ( ret )
1.123 + {
1.124 + TLex lex(result);
1.125 + ret=(lex.Val(aResult)==KErrNone);
1.126 + }
1.127 +
1.128 + return ret;
1.129 + }
1.130 +/**
1.131 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.132 + * Copies the value to the TReal reference passed in
1.133 + * @param aSectName - Section within the test steps ini file
1.134 + * @param aKeyName - Name of a key within a section
1.135 + * @return aResult - The value of the real
1.136 + * @return TBool - ETrue for found, EFalse for not found
1.137 + */
1.138 +TBool CDataWrapperBase::GetRealFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TReal& aResult)
1.139 + {
1.140 + TPtrC result;
1.141 + TBool ret=EFalse;
1.142 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
1.143 + if ( err != KErrNone )
1.144 + {
1.145 + ret = EFalse;
1.146 + }
1.147 + if ( ret )
1.148 + {
1.149 + TLex lex(result);
1.150 + ret = ( lex.Val(aResult)==KErrNone );
1.151 + }
1.152 + return ret;
1.153 + }
1.154 +
1.155 +/**
1.156 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.157 + * Copies the value to the TPtrC reference passed in
1.158 + * @param aSectName - Section within the test steps ini file
1.159 + * @param aKeyName - Name of a key within a section
1.160 + * @return aResult - Reference to the string on the heap
1.161 + * @return TBool - ETrue for found, EFalse for not found
1.162 + */
1.163 +TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
1.164 + {
1.165 + TBool ret=EFalse;
1.166 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
1.167 + if ( err != KErrNone )
1.168 + {
1.169 + ret=EFalse;
1.170 + }
1.171 + return ret;
1.172 + }
1.173 +
1.174 +/**
1.175 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.176 + * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x
1.177 + * @param aSectName - Section within the test steps ini file
1.178 + * @param aKeyName - Name of a key within a section
1.179 + * @return aResult - The integer value of the Hex input
1.180 + * @return TBool - ETrue for found, EFalse for not found
1.181 + */
1.182 +TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
1.183 + {
1.184 + TPtrC result;
1.185 + TBool ret=EFalse;
1.186 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
1.187 + if ( err != KErrNone )
1.188 + {
1.189 + ret=EFalse;
1.190 + }
1.191 + if ( ret )
1.192 + {
1.193 + TLex lex;
1.194 + if( result.FindC(KPrefixHex)==KErrNone )
1.195 + {
1.196 + lex=result.Mid(KPrefixHex().Length());
1.197 + }
1.198 + else
1.199 + {
1.200 + lex=result;
1.201 + }
1.202 + ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
1.203 + }
1.204 +
1.205 + return ret;
1.206 + }
1.207 +
1.208 +/**
1.209 + * Reads the value present from the test steps ini file within the mentioned section name and key name
1.210 + * Copies the value to the TUint reference passed in.
1.211 + * If the value is prefixed with 0x the value is read as a hexidecimal value
1.212 + * If the value is suffixed with b the value is read as a binary value
1.213 + * If the value is prefixed with a 0 the value is read as an octal value
1.214 + * If it does not match the above it is read in as an integer
1.215 + * @param aSectName - Section within the test steps ini file
1.216 + * @param aKeyName - Name of a key within a section
1.217 + * @return aResult - The integer value of the Hex input
1.218 + * @return TBool - ETrue for found, EFalse for not found
1.219 + */
1.220 +TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult)
1.221 + {
1.222 + TPtrC result;
1.223 + TBool ret=EFalse;
1.224 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
1.225 + if ( err != KErrNone )
1.226 + {
1.227 + ret=EFalse;
1.228 + }
1.229 + if ( ret )
1.230 + {
1.231 + TLex lex(result);
1.232 + if( result.FindC(KPrefixHex)==KErrNone )
1.233 + {
1.234 + lex=result.Mid(KPrefixHex().Length());
1.235 + ret=(lex.Val(aResult, EHex)==KErrNone);
1.236 + }
1.237 + else
1.238 + {
1.239 + TInt binarySuffixPosition=result.Length()-KSuffixBinary().Length();
1.240 + if ( result.FindC(KSuffixBinary)==binarySuffixPosition )
1.241 + {
1.242 + lex=result.Left(binarySuffixPosition);
1.243 + ret=(lex.Val(aResult, EBinary)==KErrNone);
1.244 + }
1.245 + else
1.246 + {
1.247 + if( result.FindC(KPrefixOctal)==KErrNone )
1.248 + {
1.249 + ret=(lex.Val(aResult, EOctal)==KErrNone);
1.250 + }
1.251 + else
1.252 + {
1.253 + TInt intResult;
1.254 + ret=(lex.Val(intResult)==KErrNone);
1.255 + if ( ret )
1.256 + {
1.257 + aResult=(TUint)intResult;
1.258 + }
1.259 + }
1.260 + }
1.261 + }
1.262 + }
1.263 +
1.264 + return ret;
1.265 + }
1.266 +
1.267 +/**
1.268 + * Return array of string parameters i.e. key=a1,a2,a3 returns array which contains
1.269 + * String a1, a2 and a3.
1.270 + * @return ret - EFalse if can't get a String parameter from Config file. ETrue if KErrNone
1.271 + */
1.272 +TBool CDataWrapperBase::GetArrayRectFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray<HBufC>& aResult)
1.273 + {
1.274 + TBool ret=EFalse;
1.275 + TPtrC completeArray;
1.276 +
1.277 + TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, completeArray));
1.278 + if ( err != KErrNone )
1.279 + {
1.280 + ret=EFalse;
1.281 + }
1.282 +
1.283 + TLex16 lex(completeArray); // Here we have the array as a string i.e. "a1,a2,a3"
1.284 + TBuf<256> buf;
1.285 + TChar chr;
1.286 +
1.287 + while(!lex.Eos())
1.288 + {
1.289 + chr = lex.Get();
1.290 + // Check if there was a list separator
1.291 + if ((chr == ',') && (lex.Peek() == '('))
1.292 + {
1.293 + HBufC* param = buf.AllocLC();
1.294 + buf.Zero();
1.295 + aResult.Append(param);
1.296 + CleanupStack::Pop(param); // pointer to buf is stored in RPointerArray
1.297 + }
1.298 + // If not separator character we can store the character into array
1.299 + else
1.300 + {
1.301 + buf.Append(chr);
1.302 + }
1.303 + }
1.304 + // Remember to put last token into array (,a3)
1.305 + HBufC* param = buf.AllocLC();
1.306 + aResult.Append(param);
1.307 + CleanupStack::Pop(param);
1.308 +
1.309 + return ret;
1.310 + }
1.311 +
1.312 +/**
1.313 + * Reads the parameter asociated to the specified command
1.314 + * @param aSectName Section on ini file
1.315 + * @param aKeyName Name of the parameter
1.316 + * @param aResult descriptor containing parameter
1.317 + * @return TBool ETrue for found, EFalse for not found
1.318 + */
1.319 +TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
1.320 + {
1.321 + TBool ret=EFalse;
1.322 +
1.323 + if ( aSectName.Length()!=0 )
1.324 + {
1.325 + ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
1.326 +
1.327 + for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
1.328 + {
1.329 + ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
1.330 + }
1.331 + }
1.332 +
1.333 + if ( ret )
1.334 + {
1.335 + if ( aResult.Match(KMatch)!=KErrNotFound )
1.336 + {
1.337 + // We have an entry of the format
1.338 + // entry =*{section,entry}*
1.339 + // where * is one or more characters
1.340 + // We need to construct this from other data in the ini file replacing {*,*}
1.341 + // with the data from
1.342 + // [section]
1.343 + // entry =some_value
1.344 + HBufC* buffer=HBufC::NewLC(aResult.Length());
1.345 + buffer->Des().Copy(aResult);
1.346 +
1.347 + TInt startLength=KStart().Length();
1.348 + TInt sparatorLength=KSeparator().Length();
1.349 + TInt endLength=KEnd().Length();
1.350 + TInt bufferLength;
1.351 + TInt start;
1.352 + TInt sparator;
1.353 + TInt end;
1.354 + TPtrC remaining;
1.355 + TLex lex;
1.356 + do
1.357 + {
1.358 + bufferLength=buffer->Length();
1.359 + start=buffer->Find(KStart);
1.360 +
1.361 + remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
1.362 + sparator=remaining.Find(KSeparator);
1.363 + remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
1.364 + sparator += (start + startLength);
1.365 +
1.366 + end=remaining.Find(KEnd) + sparator + sparatorLength;
1.367 +
1.368 + TPtrC sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
1.369 + TPtrC keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
1.370 + sectionName.Set(TLex(sectionName).NextToken());
1.371 + keyName.Set(TLex(keyName).NextToken());
1.372 +
1.373 + TInt entrySize=0;
1.374 + TPtrC entryData;
1.375 + TBool found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
1.376 + for ( TInt index=iInclude.Count(); (index>0) && (!found); )
1.377 + {
1.378 + found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
1.379 + }
1.380 + if ( found )
1.381 + {
1.382 + entrySize=entryData.Length();
1.383 + }
1.384 +
1.385 + TInt newLength=start + bufferLength - end - endLength + entrySize;
1.386 + HBufC* bufferNew=HBufC::NewLC(newLength);
1.387 + bufferNew->Des().Copy(buffer->Ptr(), start);
1.388 + if ( entrySize>0 )
1.389 + {
1.390 + bufferNew->Des().Append(entryData);
1.391 + }
1.392 + bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
1.393 + CleanupStack::Pop(bufferNew);
1.394 + CleanupStack::PopAndDestroy(buffer);
1.395 + buffer=bufferNew;
1.396 + CleanupStack::PushL(buffer);
1.397 + }
1.398 + while ( buffer->Match(KMatch)!=KErrNotFound );
1.399 + iBuffer.Append(buffer);
1.400 + CleanupStack::Pop(buffer);
1.401 + aResult.Set(*buffer);
1.402 + INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
1.403 + }
1.404 + }
1.405 +
1.406 + return ret;
1.407 + }
1.408 +
1.409 +/**
1.410 + * Utility function to produce time delay
1.411 + * @param aTimeoutInSecs Times in micro seconds
1.412 + */
1.413 +void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs)
1.414 + {
1.415 + TRequestStatus status;
1.416 + iTimer.After(status, aTimeoutInSecs);
1.417 + User::WaitForRequest(status);
1.418 + }
1.419 +
1.420 +TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult)
1.421 + {
1.422 + TPtrC str;
1.423 + TBool ret=GetStringFromConfig(aSectName, aKeyName, str);
1.424 +
1.425 + if ( ret )
1.426 + {
1.427 + TBool found=EFalse;
1.428 + TInt index=0;
1.429 + while ( (aTable[index].iValue!=-1) && !found )
1.430 + {
1.431 + if ( aTable[index].iString==str )
1.432 + {
1.433 + found=ETrue;
1.434 + aResult=aTable[index].iValue;
1.435 + }
1.436 + else
1.437 + {
1.438 + ++index;
1.439 + }
1.440 + }
1.441 +
1.442 + if ( !found )
1.443 + {
1.444 + ret=GetIntFromConfig(aSectName, aKeyName, aResult);
1.445 + }
1.446 + }
1.447 +
1.448 + return ret;
1.449 + }
1.450 +