#include "StdAfx.h"
#include "FuncList.h"


CFuncList::CFuncList(void)
{
}

CFuncList::~CFuncList(void)
{
}

///////////////////////////////////////////////////////////////////////////////
///
///	Add function to list.  Duplicate functions are allowed.
///
///	\param	[IN] szFunction:	
///			Function to add
///
///	\return	TRUE if function successfully added to list, FALSE otherwise			
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::Add(CString szType, CString szFunction, CParamList* pParams /*= NULL*/)
{
	STRUCT_Function sFunc;
	sFunc.szName = szFunction;
	sFunc.szType = szType;

	if (pParams)
		sFunc.sParams = *pParams;

	m_FuncList.AddTail(sFunc);
	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Remove all functions from list
///
///	\return	TRUE if all functions successfully removed from list, FALSE otherwise			
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::RemoveAll(void)
{
	m_FuncList.RemoveAll();
	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Find function in list
///
///	\param	[IN] szFunction:	
///			Function to search for
///
///	\return	TRUE function is in list, FALSE otherwise			
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::Find(CString szFunction)
{
	POSITION pos = m_FuncList.GetHeadPosition();

	for (int i=0; i<m_FuncList.GetCount(); i++)
	{
		if (0 == szFunction.Compare(m_FuncList.GetNext(pos).szName))
			return TRUE;
	}

	return FALSE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Get function name at specified index
///
///	\param	[IN] nIndex
///			Index of function
/// 
/// \param	[OUT] pszType
///			Type of function
///
///	\return	Function at specified index.  NULL string if empty.		
///
///////////////////////////////////////////////////////////////////////////////
CString CFuncList::GetFunctionName(UINT nIndex, CString* pszType /*=NULL*/)
{
	POSITION pos = m_FuncList.FindIndex(nIndex);

	if (!pos)
		return L"";

	if (pszType)
		*pszType = m_FuncList.GetAt(pos).szType;

	return m_FuncList.GetAt(pos).szName;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Get total number of functions in list
///
///	\return	Total number of functions in list			
///
///////////////////////////////////////////////////////////////////////////////
int CFuncList::GetCount()
{
	return (int)m_FuncList.GetCount();
}

///////////////////////////////////////////////////////////////////////////////
///
///	Copy functions to specified list
/// 
/// \param [OUT] pFuncList
///		   Copy functions to this list
///
///	\return	TRUE if functions successfully copied, FALSE otherwise		
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::Copy(CFuncList* pFuncList)
{
	POSITION pos = NULL;

	for (int i=0; i<m_FuncList.GetCount(); i++)
	{
		pos = m_FuncList.FindIndex(i);

		if (pos)
		{
			pFuncList->Add(m_FuncList.GetAt(pos).szType, m_FuncList.GetAt(pos).szName, &m_FuncList.GetAt(pos).sParams);
		}
	}

	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Retrieve the list of parameters for the specified function 
/// TODO: Need to add support for duplicate function names.  Currently acts on
/// the first match.
/// 
/// \param	[IN] szFunction
///			Name of function of interest
/// 
/// \param	[OUT] sFuncParams
///			List of parameters for the specified function
///
///	\return	TRUE if list of parameters successfully retrieved, FALSE otherwise		
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::GetFuncParams(CString szFunction, CParamList &sFuncParams)
{
	POSITION pos = NULL;

	for (int i=0; i<m_FuncList.GetCount(); i++)
	{
		pos = m_FuncList.FindIndex(i);

		if (pos)
		{
			if (0 == szFunction.Compare(m_FuncList.GetAt(pos).szName))
			{
				sFuncParams = m_FuncList.GetAt(pos).sParams;
				return TRUE;
			}
		}
	}

	return FALSE;
}

///////////////////////////////////////////////////////////////////////////////
///
///	Save the list of parameters for the specified function.  
/// TODO: Need to add support for duplicate function names.  Currently acts on
/// the first match.
/// 
/// \param	[IN] szFunction
///			Name of function of interest
/// 
/// \param	[IN] pFuncParams
///			List of parameters to save for the specified function
///
///	\return	TRUE if list of parameters successfully saved, FALSE otherwise		
///
///////////////////////////////////////////////////////////////////////////////
BOOL CFuncList::SetFuncParams(CString szFunction, CParamList* pFuncParams)
{
	POSITION pos = NULL;

	for (int i=0; i<m_FuncList.GetCount(); i++)
	{
		pos = m_FuncList.FindIndex(i);

		if (pos)
		{
			if (0 == szFunction.Compare(m_FuncList.GetAt(pos).szName))
			{
				m_FuncList.GetAt(pos).sParams = *pFuncParams;
				return TRUE;
			}
		}
	}

	return FALSE;
}