telephonyserverplugins/simtsy/testconfigfileparser/src/testconfigsection.cpp
branchRCL_3
changeset 20 07a122eea281
parent 19 630d2f34d719
child 21 4814c5a49428
equal deleted inserted replaced
19:630d2f34d719 20:07a122eea281
     1 /*
       
     2 * Copyright (c) 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 #include "testconfigfileparser.h"
       
    18 #include <f32file.h>
       
    19 
       
    20 
       
    21 //
       
    22 // CTestConfigSection
       
    23 //
       
    24 
       
    25 CTestConfigSection::CTestConfigSection()
       
    26 	: CBase()
       
    27 	{
       
    28 	}
       
    29 
       
    30 EXPORT_C CTestConfigSection* CTestConfigSection::NewLC(const TDesC8& aSectionName, CTestConfigSection& aDefaults)
       
    31 	{
       
    32 	CTestConfigSection* self = new (ELeave) CTestConfigSection();
       
    33 	CleanupStack::PushL(self);
       
    34 	self->iDefaults = aDefaults.CopyLC();
       
    35 	CleanupStack::Pop(self->iDefaults);
       
    36 	self->ConstructL(aSectionName);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 EXPORT_C CTestConfigSection* CTestConfigSection::NewLC(const TDesC8& aSectionName)
       
    41 	{
       
    42 	CTestConfigSection* self = new (ELeave) CTestConfigSection();
       
    43 	CleanupStack::PushL(self);
       
    44 	self->ConstructL(aSectionName);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 void CTestConfigSection::ConstructL(const TDesC8& aSectionName)
       
    49 	{
       
    50 	iSectionName = aSectionName.AllocL();
       
    51 	}
       
    52 
       
    53 EXPORT_C const TDesC8& CTestConfigSection::ItemValue(const TDesC8& aItem, const TDesC8& aDefault) const
       
    54 	{
       
    55 	const CTestConfigItem* sectionItem = Item(aItem);
       
    56 
       
    57 	if (sectionItem != NULL)
       
    58 		{
       
    59 		const TPtrC8 value(sectionItem->Value());
       
    60 
       
    61 		if (iDefaults != NULL && (value.CompareF(KScriptDefault1) == 0 || value.CompareF(KScriptDefault2) == 0))
       
    62 			{
       
    63 			return iDefaults->ItemValue(aItem, aDefault);
       
    64 			}
       
    65 		else
       
    66 			{
       
    67 			return sectionItem->Value();
       
    68 			}
       
    69 		}
       
    70 	else if (iDefaults != NULL)
       
    71 		{
       
    72 		return iDefaults->ItemValue(aItem, aDefault);
       
    73 		}
       
    74 	else
       
    75 		{
       
    76 		return aDefault;
       
    77 		}
       
    78 	}
       
    79 
       
    80 
       
    81 EXPORT_C TInt CTestConfigSection::ItemValue(const TDesC8& aItem, const TInt aDefault) const
       
    82 	{
       
    83 	const TDesC8& value = ItemValue(aItem, KNullDesC8);
       
    84 
       
    85 	if (value.Length() == 0)
       
    86 		{
       
    87 		if (iDefaults != NULL)
       
    88 			return iDefaults->ItemValue(aItem, aDefault);
       
    89 		else
       
    90 			return aDefault;
       
    91 		}
       
    92 
       
    93 	TLex8 input(value);
       
    94 	TInt ret = aDefault;
       
    95 	TInt err = input.Val(ret);
       
    96 	
       
    97 	if (err)
       
    98 		{
       
    99 		if (iDefaults != NULL)
       
   100 			return iDefaults->ItemValue(aItem, aDefault);
       
   101 		else
       
   102 			return aDefault;
       
   103 		}
       
   104 
       
   105 	return ret;
       
   106 	}
       
   107 
       
   108 EXPORT_C CTestConfigItem& CTestConfigSection::AddItemL(const TDesC8& aItem, const TDesC8& aValue)
       
   109 	{
       
   110 	CTestConfigItem* sectionItem = CTestConfigItem::NewLC(*this, aItem, aValue);
       
   111 	User::LeaveIfError(iItems.Append(sectionItem));
       
   112 	CleanupStack::Pop(sectionItem);
       
   113 	return *sectionItem;
       
   114 	}
       
   115 
       
   116 EXPORT_C void CTestConfigSection::DeleteItemsL(const TDesC8& aItem)
       
   117 	{
       
   118 	TInt count = iItems.Count();
       
   119 
       
   120 	//Remove existing CTestConfigItems that have the same item name
       
   121 	while (count--)
       
   122 		{
       
   123 		CTestConfigItem* sectionItem = iItems[count];
       
   124 
       
   125 		if (sectionItem->Item().CompareF(aItem) == 0)
       
   126 			{
       
   127 			delete sectionItem;
       
   128 			iItems.Remove(count);
       
   129 			}
       
   130 		}	
       
   131 	}
       
   132 
       
   133 
       
   134 EXPORT_C CTestConfigSection::~CTestConfigSection()
       
   135 	{
       
   136 	iItems.ResetAndDestroy();
       
   137 	iItems.Close();
       
   138 
       
   139 	delete iSectionName;
       
   140 	delete iDefaults;
       
   141 	}
       
   142 
       
   143 EXPORT_C CTestConfigSection* CTestConfigSection::CopyLC() const
       
   144 	{
       
   145 	TInt count = iItems.Count();
       
   146 	CTestConfigSection* copy = NULL;
       
   147 
       
   148 	if (iDefaults != NULL)
       
   149 		copy = CTestConfigSection::NewLC(SectionName(), *iDefaults);
       
   150 	else
       
   151 		copy = CTestConfigSection::NewLC(SectionName());
       
   152 		
       
   153 	while (count--)
       
   154 		{
       
   155 		CTestConfigItem* item = iItems[count]->CopyLC();
       
   156 		User::LeaveIfError(copy->iItems.Append(item));
       
   157 		CleanupStack::Pop(item);
       
   158 		}
       
   159 
       
   160 	return copy;
       
   161 	}
       
   162 
       
   163 void CTestConfigSection::WriteL(RFile& aFile) const
       
   164 	{
       
   165 	//Write the section name
       
   166 	HBufC8* buf = HBufC8::NewLC(iSectionName->Length());
       
   167 	buf->Des().Copy(*iSectionName);
       
   168 
       
   169 	User::LeaveIfError(aFile.Write(KScriptSectionStart));
       
   170 	User::LeaveIfError(aFile.Write(*buf));
       
   171 	User::LeaveIfError(aFile.Write(KScriptSectionEnd));
       
   172 	User::LeaveIfError(aFile.Write(KScriptCRLF8));
       
   173 
       
   174 	CleanupStack::PopAndDestroy(buf);
       
   175 	const TInt count = iItems.Count();
       
   176 
       
   177 	//Write the items
       
   178 	for (TInt i = 0; i < count; i++)
       
   179 		iItems[i]->WriteL(aFile);
       
   180 
       
   181 	User::LeaveIfError(aFile.Write(KScriptCRLF8));
       
   182 	}
       
   183 
       
   184 TBool CTestConfigSection::operator==(const CTestConfigSection& aSection) const
       
   185 	{
       
   186 	TInt count = iItems.Count();
       
   187 
       
   188 	if (count != aSection.Items().Count())
       
   189 		return EFalse;
       
   190 
       
   191 	TBool retVal = (*iSectionName == aSection.SectionName());
       
   192 
       
   193 	while (count-- && retVal)
       
   194 		{
       
   195 		retVal = retVal && (*iItems[count] == aSection[count]);
       
   196 		}
       
   197 
       
   198 	return retVal;
       
   199 	}
       
   200 
       
   201 EXPORT_C const CTestConfigItem* CTestConfigSection::Item(const TDesC8& aItem) const
       
   202 	{
       
   203 	const CTestConfigItem* sectionItem = NULL;
       
   204 	const TInt count = iItems.Count();
       
   205 	for (TInt i = 0; i < count; i++)
       
   206 		{
       
   207 		if (iItems[i]->Item().CompareF(aItem) == 0)
       
   208 			{
       
   209 			sectionItem = iItems[i];
       
   210 			break;
       
   211 			}
       
   212 		}
       
   213 	return sectionItem;
       
   214 	}
       
   215 
       
   216 EXPORT_C CTestConfigItem* CTestConfigSection::Item(const TDesC8& aItem)
       
   217 	{
       
   218 	CTestConfigItem* sectionItem = NULL;
       
   219 	const TInt count = iItems.Count();
       
   220 	for (TInt i = 0; i < count; i++)
       
   221 		{
       
   222 		if (iItems[i]->Item().CompareF(aItem) == 0)
       
   223 			{
       
   224 			sectionItem = iItems[i];
       
   225 			break;
       
   226 			}
       
   227 		}
       
   228 	return sectionItem;
       
   229 	}
       
   230 
       
   231 EXPORT_C const CTestConfigItem* CTestConfigSection::Item(const TDesC8& aItem,TInt aIndex) const
       
   232 	{
       
   233 	const CTestConfigItem* sectionItem = NULL;
       
   234 	const TInt count = iItems.Count();
       
   235 	TInt foundItemCnt=0;
       
   236 	for (TInt i = 0; i < count; i++)
       
   237 		{
       
   238 		if (iItems[i]->Item().CompareF(aItem) == 0)
       
   239 			{
       
   240 			if(foundItemCnt++==aIndex)
       
   241 				{
       
   242 				sectionItem = iItems[i];
       
   243 				break;
       
   244 				}
       
   245 			}
       
   246 		}
       
   247 	return sectionItem;
       
   248 	}
       
   249 
       
   250 EXPORT_C CTestConfigItem* CTestConfigSection::Item(const TDesC8& aItem,TInt aIndex)
       
   251 	{
       
   252 	CTestConfigItem* sectionItem = NULL;
       
   253 	const TInt count = iItems.Count();
       
   254 	TInt foundItemCnt=0;
       
   255 	for (TInt i = 0; i < count; i++)
       
   256 		{
       
   257 		if (iItems[i]->Item().CompareF(aItem) == 0)
       
   258 			{
       
   259 			if(foundItemCnt++==aIndex)
       
   260 				{
       
   261 				sectionItem = iItems[i];
       
   262 				break;
       
   263 				}
       
   264 			}
       
   265 		}
       
   266 	return sectionItem;
       
   267 	}
       
   268 
       
   269 EXPORT_C void CTestConfigSection::ItemsL(RPointerArray<CTestConfigItem>& aArray, const TDesC8& aItemTag)
       
   270 	{
       
   271 	aArray.Reset();
       
   272 	const TInt count = iItems.Count();
       
   273 
       
   274 	for (TInt i = 0; i < count; i++) //< Order important
       
   275 		{
       
   276 		CTestConfigItem& item = *iItems[i];
       
   277 
       
   278 		if (item.Item().CompareF(aItemTag) == 0)
       
   279 			{
       
   280 			User::LeaveIfError(aArray.Append(&item));
       
   281 			}
       
   282 		}
       
   283 	}
       
   284 
       
   285 EXPORT_C void CTestConfigSection::ItemsL(RPointerArray<const CTestConfigItem>& aArray, const TDesC8& aItemTag) const
       
   286 	{
       
   287 	aArray.Reset();
       
   288 	const TInt count = iItems.Count();
       
   289 
       
   290 	for (TInt i = 0; i < count; i++) //< Order important
       
   291 		{
       
   292 		const CTestConfigItem& item = *iItems[i];
       
   293 
       
   294 		if (item.Item().CompareF(aItemTag) == 0)
       
   295 			{
       
   296 			User::LeaveIfError(aArray.Append(&item));
       
   297 			}
       
   298 		}
       
   299 	}
       
   300 
       
   301 
       
   302 EXPORT_C TInt CTestConfigSection::ItemCount(const TDesC8& aItem) const
       
   303 	{
       
   304 	const TInt count = iItems.Count();
       
   305 	TInt foundItemCnt=0;
       
   306 	for (TInt i = 0; i < count; i++)
       
   307 		{
       
   308 		if (iItems[i]->Item().CompareF(aItem) == 0)
       
   309 			foundItemCnt++;
       
   310 		}
       
   311 	return foundItemCnt;
       
   312 	}