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