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