#include "StdAfx.h"
#include "ParamList.h"

CParamList::CParamList(void)
{
}

CParamList::~CParamList(void)
{
}

///////////////////////////////////////////////////////////////////////////////
///
///	Assignment operator
/// 
/// \param	[IN] sParamList
///			Parameter list to copy from
/// 
/// \return	Instance of itself
///
///////////////////////////////////////////////////////////////////////////////
CParamList & CParamList::operator=(CParamList & sParamList)
{
	CString szLabel, szValue;

	for (int i=0; i<sParamList.GetCount(); i++)
	{
		if (sParamList.GetAt(i, szLabel, szValue))
			Add(szLabel, szValue);
	}

	return *this;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Add a parameter to list.  Duplicate parameter names are not allowed.
/// If a duplicate name is found, the item is updated with the new value.
/// 
/// \param	[IN] szLabel
///			Name of parameter
/// 
/// \param	[IN] szValue
///			Value of the parameter.  Note that the value is only stored as a
///			string.  Conversion has to be handled by the caller if another
///			type is required.	
/// 
/// \return	TRUE if parameter successfully added to list, FALSE otherwise
///
///////////////////////////////////////////////////////////////////////////////
BOOL CParamList::Add(CString szLabel, CString szValue)
{
	// Label must be at least one character
	if (!szLabel.GetLength())
		return FALSE;

	STRUCT_FuncParam sFuncParam;
	sFuncParam.szLabel = szLabel;
	sFuncParam.szValue = szValue;

	// do not allow duplicate labels
	POSITION pos = m_lstParams.Find(sFuncParam);

	if (pos)
		m_lstParams.SetAt(pos, sFuncParam);
	else
		m_lstParams.AddTail(sFuncParam);

	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Get the function parameter at the specified index.
/// 
/// \param	[IN] iIndex
///			Index of function parameter in the list.
/// 
/// \param	[OUT] szLabel
///			Name of parameter
/// 
/// \param	[OUT] szValue
///			Value of parameter
/// 
/// \return	TRUE if parameter successfully retrieved, FALSE otherwise
///
///////////////////////////////////////////////////////////////////////////////
BOOL CParamList::GetAt(UINT iIndex, CString &szLabel, CString &szValue)
{
	POSITION pos = m_lstParams.FindIndex(iIndex);
	
	if (pos)
	{
		szLabel = m_lstParams.GetAt(pos).szLabel;
		szValue = m_lstParams.GetAt(pos).szValue;
		return TRUE;
	}

	return FALSE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Retrieve the value of the specified label.  If label not found, NULL string
/// is returned.
/// 
/// \param	[IN] szLabel
///			Label of parameter to search for
/// 
/// \return	Value of parameter associated with the specified label.  NULL
///			string is returned if specified label not found.
///
///////////////////////////////////////////////////////////////////////////////
CString CParamList::GetValue(CString szLabel)
{
	POSITION pos = NULL;

	// cycle through all the parameters
	for (int i=0; i<m_lstParams.GetCount(); i++)
	{
		pos = m_lstParams.FindIndex(i);

		if (pos)
		{
			if (0 == szLabel.Compare(m_lstParams.GetAt(pos).szLabel))
			{
				return m_lstParams.GetAt(pos).szValue;
			}
		}
	}

	return L"";
}