syslibsapitest/syslibssvs/common/src/DataWrapperBase.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 	iFs.Close();
       
    45 	}
       
    46 
       
    47 void CDataWrapperBase::InitialiseL()
       
    48 	{
       
    49 	CDataWrapper::InitialiseL();
       
    50 
       
    51 	TBuf<KMaxTestExecuteCommandLength>	tempStore;
       
    52 	TPtrC		fileName;
       
    53 	TBool		moreData=ETrue;
       
    54 	TBool		index=0;
       
    55 	while ( moreData )
       
    56 		{
       
    57 		tempStore.Format(KFile(), ++index);
       
    58 		moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
       
    59 		
       
    60 		if (moreData)
       
    61 			{
       
    62 			CIniData*	iniData=CIniData::NewL(fileName);
       
    63 			CleanupStack::PushL(iniData);
       
    64 			iInclude.Append(iniData);
       
    65 			CleanupStack::Pop(iniData);
       
    66 			}
       
    67 		}
       
    68 	User::LeaveIfError(iFs.Connect());
       
    69 	}
       
    70 
       
    71 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
       
    72 	{
       
    73 	TBool	ret=EFalse;
       
    74 	TPtrC	result;
       
    75 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
    76 	if ( err != KErrNone )
       
    77 		{
       
    78 		ret=EFalse;
       
    79 		}
       
    80 	if ( ret )
       
    81 		{
       
    82 		_LIT(KTrue,"true");
       
    83 		aResult=(result.FindF(KTrue) != KErrNotFound);
       
    84 		}
       
    85 
       
    86 	return ret;
       
    87 	}
       
    88 
       
    89 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
       
    90 	{
       
    91 	TPtrC	result;
       
    92 	TBool	ret=EFalse;
       
    93 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
    94 	if ( err != KErrNone )
       
    95 		{
       
    96 		ret=EFalse;
       
    97 		}
       
    98 	if ( ret )
       
    99 		{
       
   100 		TLex	lex(result);
       
   101 		ret=(lex.Val(aResult)==KErrNone);
       
   102 		}
       
   103 
       
   104 	return ret;
       
   105 	}
       
   106 
       
   107 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
       
   108 	{
       
   109 	TBool	ret=EFalse;
       
   110 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
       
   111 	if ( err != KErrNone )
       
   112 		{
       
   113 		ret=EFalse;
       
   114 		}
       
   115 	return ret;
       
   116 	}
       
   117 
       
   118 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
       
   119 	{
       
   120 	TPtrC	result;
       
   121 	TBool	ret=EFalse;
       
   122 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
   123 	if ( err != KErrNone )
       
   124 		{
       
   125 		ret=EFalse;
       
   126 		}
       
   127 	if ( ret )
       
   128 		{
       
   129 		TLex	lex(result);
       
   130 		ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
       
   131 		}
       
   132 
       
   133 	return ret;
       
   134 	}
       
   135 
       
   136 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
       
   137 	{
       
   138 	TBool	ret=EFalse;
       
   139 
       
   140 	if ( aSectName.Length()!=0 )
       
   141 		{
       
   142 		ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
       
   143 
       
   144 		for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
       
   145 			{
       
   146 			ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
       
   147 			}
       
   148 		}
       
   149 
       
   150 	if ( ret )
       
   151 		{
       
   152 		if ( aResult.Match(KMatch)!=KErrNotFound )
       
   153 			{
       
   154 			//	We have an entry of the format
       
   155 			//	entry =*{section,entry}*
       
   156 			//	where * is one or more characters
       
   157 			//	We need to construct this from other data in the ini file replacing {*,*}
       
   158 			//	with the data from
       
   159 			//	[section]
       
   160 			//	entry =some_value
       
   161 			HBufC*	buffer=HBufC::NewLC(aResult.Length());
       
   162 			buffer->Des().Copy(aResult);
       
   163 
       
   164 			TInt	startLength=KStart().Length();
       
   165 			TInt	sparatorLength=KSeparator().Length();
       
   166 			TInt	endLength=KEnd().Length();
       
   167 			TInt	bufferLength;
       
   168 			TInt	start;
       
   169 			TInt	sparator;
       
   170 			TInt	end;
       
   171 			TPtrC	remaining;
       
   172 			TLex	lex;
       
   173 			do
       
   174 				{
       
   175 				bufferLength=buffer->Length();
       
   176 				start=buffer->Find(KStart);
       
   177 
       
   178 				remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
       
   179 				sparator=remaining.Find(KSeparator);
       
   180 				remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
       
   181 				sparator += (start + startLength);
       
   182 
       
   183 				end=remaining.Find(KEnd) + sparator + sparatorLength;
       
   184 
       
   185 				TPtrC	sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
       
   186 				TPtrC	keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
       
   187 				sectionName.Set(TLex(sectionName).NextToken());
       
   188 				keyName.Set(TLex(keyName).NextToken());
       
   189 
       
   190 				TInt	entrySize=0;
       
   191 				TPtrC	entryData;
       
   192 				TBool	found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
       
   193 				for ( TInt index=iInclude.Count(); (index>0) && (!found);  )
       
   194 					{
       
   195 					found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
       
   196 					}
       
   197 				if ( found )
       
   198 					{
       
   199 					entrySize=entryData.Length();
       
   200 					}
       
   201 
       
   202 				TInt	newLength=start + bufferLength - end - endLength + entrySize;
       
   203 				HBufC*	bufferNew=HBufC::NewLC(newLength);
       
   204 				bufferNew->Des().Copy(buffer->Ptr(), start);
       
   205 				if ( entrySize>0 )
       
   206 					{
       
   207 					bufferNew->Des().Append(entryData);
       
   208 					}
       
   209 				bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
       
   210 				CleanupStack::Pop(bufferNew);
       
   211 				CleanupStack::PopAndDestroy(buffer);
       
   212 				buffer=bufferNew;
       
   213 				CleanupStack::PushL(buffer);
       
   214 				}
       
   215 			while ( buffer->Match(KMatch)!=KErrNotFound );
       
   216 			iBuffer.Append(buffer);
       
   217 			CleanupStack::Pop(buffer);
       
   218 			aResult.Set(*buffer);
       
   219 			INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
       
   220 			}
       
   221 		}
       
   222 
       
   223 	return ret;
       
   224 	}