applayerprotocols/httptransportfw/Test/t_utils/ScriptSection.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TestScripts.h"
       
    17 
       
    18 //
       
    19 // CScriptSection
       
    20 //
       
    21 
       
    22 CScriptSection::CScriptSection()
       
    23 	: CBase()
       
    24 	{
       
    25 	}
       
    26 
       
    27 EXPORT_C CScriptSection* CScriptSection::NewLC(const TDesC& aSectionName, CScriptSection& aDefaults)
       
    28 	{
       
    29 	CScriptSection* self = new (ELeave) CScriptSection();
       
    30 	CleanupStack::PushL(self);
       
    31 	self->iDefaults = aDefaults.CopyLC();
       
    32 	CleanupStack::Pop(self->iDefaults);
       
    33 	self->ConstructL(aSectionName);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 EXPORT_C CScriptSection* CScriptSection::NewLC(const TDesC& aSectionName)
       
    38 	{
       
    39 	CScriptSection* self = new (ELeave) CScriptSection();
       
    40 	CleanupStack::PushL(self);
       
    41 	self->ConstructL(aSectionName);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 void CScriptSection::ConstructL(const TDesC& aSectionName)
       
    46 	{
       
    47 	iSectionName = aSectionName.AllocL();
       
    48 	iItems = new(ELeave) CArrayPtrFlat<CScriptSectionItem>(1);
       
    49 	}
       
    50 
       
    51 EXPORT_C const TDesC& CScriptSection::ItemValue(const TDesC& aItem, const TDesC& aDefault)
       
    52 	{
       
    53 	CScriptSectionItem* sectionItem = Item(aItem);
       
    54 
       
    55 	if (sectionItem != NULL)
       
    56 		{
       
    57 		TPtrC value(sectionItem->Value());
       
    58 
       
    59 		if (iDefaults != NULL && (value.CompareF(KDefault1) == 0 || value.CompareF(KDefault2) == 0))
       
    60 			{
       
    61 			return iDefaults->ItemValue(aItem, aDefault);
       
    62 			}
       
    63 		else
       
    64 			{
       
    65 			return sectionItem->Value();
       
    66 			}
       
    67 		}
       
    68 	else if (iDefaults != NULL)
       
    69 		{
       
    70 		return iDefaults->ItemValue(aItem, aDefault);
       
    71 		}
       
    72 	else
       
    73 		{
       
    74 		return aDefault;
       
    75 		}
       
    76 	}
       
    77 
       
    78 EXPORT_C TInt CScriptSection::ItemValue(const TDesC& aItem, const TInt aDefault)
       
    79 	{
       
    80 	const TDesC& value = ItemValue(aItem, KNullDesC);
       
    81 
       
    82 	if (value.Length() == 0)
       
    83 		{
       
    84 		if (iDefaults != NULL)
       
    85 			return iDefaults->ItemValue(aItem, aDefault);
       
    86 		else
       
    87 			return aDefault;
       
    88 		}
       
    89 
       
    90 	TLex input(value);
       
    91 	TInt ret = aDefault;
       
    92 	TInt err = input.Val(ret);
       
    93 	
       
    94 	if (err)
       
    95 		{
       
    96 		if (iDefaults != NULL)
       
    97 			return iDefaults->ItemValue(aItem, aDefault);
       
    98 		else
       
    99 			return aDefault;
       
   100 		}
       
   101 
       
   102 	return ret;
       
   103 	}
       
   104 
       
   105 EXPORT_C CScriptSectionItem& CScriptSection::AddItemL(const TDesC& aItem, const TDesC& aValue)
       
   106 	{
       
   107 	CScriptSectionItem* sectionItem = CScriptSectionItem::NewLC(*this, aItem, aValue);
       
   108 	iItems->AppendL(sectionItem);
       
   109 	CleanupStack::Pop(sectionItem);
       
   110 	iLastItem = sectionItem;
       
   111 	return *sectionItem;
       
   112 	}
       
   113 
       
   114 EXPORT_C void CScriptSection::AddItemIfNotExistL(const TDesC& aItem, const TDesC& aValue)
       
   115 	{
       
   116 	CScriptSectionItem* sectionItem = Item(aItem);
       
   117 
       
   118 	if (sectionItem == NULL)
       
   119 		{
       
   120 		AddItemL(aItem, aValue);
       
   121 		}
       
   122 	}
       
   123 
       
   124 EXPORT_C CScriptSectionItem& CScriptSection::ReplaceItemL(const TDesC& aItem, const TDesC& aValue)
       
   125 	{
       
   126 	DeleteItemsL(aItem);
       
   127 	return AddItemL(aItem, aValue);
       
   128 	}
       
   129 
       
   130 EXPORT_C void CScriptSection::DeleteItemsL(const TDesC& aItem)
       
   131 	{
       
   132 	iLastItem = NULL;
       
   133 	TInt count = iItems->Count();
       
   134 
       
   135 	//Remove existing CScriptSectionItems that have the same item name
       
   136 	while (count--)
       
   137 		{
       
   138 		CScriptSectionItem* sectionItem = iItems->At(count);
       
   139 
       
   140 		if (sectionItem->Item().CompareF(aItem) == 0)
       
   141 			{
       
   142 			delete sectionItem;
       
   143 			iItems->Delete(count);
       
   144 			}
       
   145 		}	
       
   146 	}
       
   147 
       
   148 
       
   149 EXPORT_C CScriptSection::~CScriptSection()
       
   150 	{
       
   151 	if (iItems)
       
   152 		iItems->ResetAndDestroy();
       
   153 
       
   154 	delete iItems;
       
   155 	delete iSectionName;
       
   156 	delete iDefaults;
       
   157 	}
       
   158 
       
   159 EXPORT_C CScriptSection* CScriptSection::CopyLC()
       
   160 	{
       
   161 	TInt count = iItems->Count();
       
   162 	CScriptSection* copy = NULL;
       
   163 
       
   164 	if (iDefaults != NULL)
       
   165 		copy = CScriptSection::NewLC(SectionName(), *iDefaults);
       
   166 	else
       
   167 		copy = CScriptSection::NewLC(SectionName());
       
   168 		
       
   169 	while (count--)
       
   170 		{
       
   171 		copy->iItems->AppendL(Item(count).CopyLC());
       
   172 		CleanupStack::Pop();
       
   173 		}
       
   174 
       
   175 	return copy;
       
   176 	}