#include "IPT_CPA.h"
#include "MemIPT.h"
#include "ErrIPT.h"
 
#include "IPT_Def.h"
#include "IPT_Hdl.h"
 
#define D_IPT_Input_StructureDefine
#include "IPT_KDef.h"
#include "IPT_Str.h"
#undef D_IPT_Input_StructureDefine
 
#include "IPT_Main.h"
#include "IPT_Scpt.h"
 
#include "ldt.h"
 
/*BEGIN FS 9/16/98*/
/*----------------------------------------------------------------------
 *  Description: callback for InputAction section from .IPT sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/16/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadIPT_InputAction(LDT_tdst_Link *pLink)
{
	IPT_tdxHandleToKeyWordElement hKeyWordOrderElement,hKeyWordElement;
 
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szEntryName;
 
	IPT_tdxHandleToEntryElement hEntryElement;
	SCR_tdst_Link_Value *p_xIndex;
 
	/*Working for BeginSection*/
	Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
	MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
	if ((hEntryElement = (IPT_tdxHandleToEntryElement)Mmg_fn_p_vAlloc4Ch(sizeof(struct IPT_tdstEntryElement_),C_ucMmgDefaultChannel))==NULL)
		M_IPTFatalError(E_uwIPT_AllocNewKeyAction);
 
	memset(hEntryElement,0,sizeof(struct IPT_tdstEntryElement_));
	LDT_SetFileLong(51,(unsigned long)hEntryElement);
	LST2_M_DynamicInitElement(hEntryElement);
	LST2_M_DynamicAddTail(&IPT_g_hInputStructure.hEntryElement,hEntryElement);
	LST2_M_DynamicInitAnchor(&hEntryElement->hKeyWordList);
	hEntryElement->lState = -2;
	hEntryElement->bIsActivate = IPT_C_TRUE;
 
	if (LDT_iGetNbParams()-1>0) /*there are params in "()"?*/
		if (!stricmp(LDT_szGetParam(1),IPT_C_ParamOptions)) /*yes, if the first isn't "Options"*/
		{
			MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
			if ((hEntryElement->p_szEntryName = (char*)Mmg_fn_p_vAlloc4Ch((strlen(LDT_szGetSectionName())+1)*sizeof(char),C_ucMmgDefaultChannel))==NULL)
				M_IPTFatalError(E_uwIPT_AllocNewKeyAction);
			strcpy(hEntryElement->p_szEntryName,LDT_szGetSectionName());
		}
	p_xIndex=SCR_fnp_st_Link_CreateOrGetLinkFromKey(&IPT_g_hInputStructure.stEntryLink,LDT_szGetSectionName());
	if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_NotInitialized)
	{
		SCR_fnp_st_Link_SetValue(&IPT_g_hInputStructure.stEntryLink,LDT_szGetSectionName(),(unsigned long)hEntryElement);
	}
	else if (p_xIndex!=NULL&&p_xIndex->eState==SCR_ELS_Link_Initialized)
	{
		M_IPTFatalError(E_uwIPT_NewKeyActionAlreadyExist);
	}
 
	/*Working for Entries*/
	while( (result=LDT_GetNextEntry())!=ParseResult_EndSection )
	{
		if (result==ParseResult_BeginSection) /*is a SubSection?*/
		{
			LDT_LoadSection(NULL);
			continue;
		}
		szEntryName=LDT_szGetEntryName();
		hKeyWordOrderElement=IPT_fn_hAllocKeyWord();
		hKeyWordOrderElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(szEntryName,IPT_g_hScriptKeyWordValue);
		LST2_M_DynamicInitElement(hKeyWordOrderElement);
		LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordOrderElement);
 
		/**** All following keyword take a key in parameter ****/
		if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyJustPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyJustReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyReleased)
		{
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(LDT_szGetParam(1),IPT_g_hScriptKeyValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
		}
		/**** All following keyword take a pad in parameter ****/
		if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadJustPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadJustReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadJustPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadJustReleased)
		{
			/**** Pad Number ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swPadNumber = atoi(LDT_szGetParam(1));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
			/**** Pad Action ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swPadAction = IPT_fn_swGetKeyValue(LDT_szGetParam(2),IPT_g_hScriptPadValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
		}
		/**** All following keyword take a joystick in parameter ****/
		if ( hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickAxeValue)
		{
			/**** Joystick Number ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(LDT_szGetParam(1));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
			/**** Joystick Action ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyAction = IPT_fn_swGetKeyValue(LDT_szGetParam(2),IPT_g_hScriptPadValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
			/**** Joystick Min Value ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyValueMin = atoi(LDT_szGetParam(3));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
			/**** Joystick Max Value ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyValueMax = atoi(LDT_szGetParam(4));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
		}
		/**** All following keyword take a joystick in parameter ****/
		if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickJustPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickJustReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickReleased)
		{
			/**** Pad Number ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(LDT_szGetParam(1));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
			/**** Pad Action ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyAction = IPT_fn_swGetKeyValue(LDT_szGetParam(2),IPT_g_hScriptPadValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
		}
		/**** All following keyword take a Entry Handle in parameter ****/
		if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustValidated
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionValidated
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionJustInvalidated
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionInvalidated)
		{
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.hEntryElement = IPT_fn_hGetEntryActionHandle(LDT_szGetParam(1));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			if (!IPT_fn_bIsAValidEntryActionHandle(hKeyWordElement->u_ElementUnion.hEntryElement))
			M_IPTFatalError(E_uwIPT_UnknownActionValue);
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hKeyWordElement);
		}
		/**** All following keyword can have time notion ****/
		if (  hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyPressed
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_KeyReleased
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionValidated
			||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_ActionInvalidated)
		{
			if (LDT_iGetNbParams()-1>1) /*more than 1 param in "()"*/
			{
				hKeyWordElement->ucMinCounterInput = atoi(LDT_szGetParam(2));
				if (LDT_iGetNbParams()-1>2) /*more than 2 params in"()"*/
					hKeyWordElement->ucMaxCounterInput = atoi(LDT_szGetParam(3));
			}
		}
		else if (hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadPressed
			   ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_PadReleased
			   ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickPressed
			   ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickReleased
			   ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadPressed
			   ||hKeyWordOrderElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_JoystickOrPadReleased)
		{
			if (LDT_iGetNbParams()-1>2) /*more than 2 params in "()"*/
			{
				hKeyWordElement->ucMinCounterInput = atoi(LDT_szGetParam(3));
				if (LDT_iGetNbParams()-1>3) /*more than 3 params in "()"*/
					hKeyWordElement->ucMaxCounterInput = atoi(LDT_szGetParam(4));
			}
		}
	} /*end while for entries*/
	return 0;
}
/*END FS 9/16/98*/
 
 
/*BEGIN FS 9/16/98*/
/*----------------------------------------------------------------------
 *  Description: callback for Sequence section from .IPT sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/16/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadIPT_Sequence( LDT_tdst_Link *pLink )
{
	IPT_tdxHandleToEntryElement hEntryElement;
	IPT_tdxHandleToKeyWordElement hSequenceKeyWordElement,hSequenceOrderKeyWordElement;
	IPT_tdxHandleToKeyWordElement hKeyWordElement;
	long ulNumberOfSequence=0;
 
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szEntryName;
 
	hEntryElement=(IPT_tdxHandleToEntryElement)LDT_GetFileLong(51);
 
	/*Working for BeginSection*/
	hSequenceKeyWordElement=IPT_fn_hAllocKeyWord();
	hSequenceKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(IPT_C_SequenceSection,IPT_g_hScriptKeyWordValue);
	LST2_M_DynamicInitElement(hSequenceKeyWordElement);
	LST2_M_DynamicAddTail(&hEntryElement->hKeyWordList,hSequenceKeyWordElement);
 
	/*Working for Entries*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		szEntryName=LDT_szGetEntryName();
 
		ulNumberOfSequence++;
 
		hSequenceOrderKeyWordElement=IPT_fn_hAllocKeyWord();
		hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(szEntryName,IPT_g_hScriptKeyWordValue);
		LST2_M_DynamicInitElement(hSequenceOrderKeyWordElement);
		LST2_M_DynamicInsertBetween(hSequenceOrderKeyWordElement,hSequenceKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceKeyWordElement));
 
		if (hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_SequenceKey)
		{
			hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequenceKeyEnd;
			if (LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)!=NULL)
				LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequenceKey;
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(LDT_szGetParam(1),IPT_g_hScriptKeyValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			if (LDT_iGetNbParams()-1>1)
			{
				hKeyWordElement->ucMinCounterInput = atoi(LDT_szGetParam(2));
				if (LDT_iGetNbParams()-1>2)
					hKeyWordElement->ucMaxCounterInput = atoi(LDT_szGetParam(3));
			}
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
		}
		else if (hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord==IPT_E_KeyWord_SequencePad)
		{
			hSequenceOrderKeyWordElement->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequencePadEnd;
			if (LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)!=NULL)
				LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement)->u_ElementUnion.swKeyWord=IPT_E_KeyWord_SequencePad;
			/**** Pad Action ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swKeyWord = IPT_fn_swGetKeyValue(LDT_szGetParam(2),IPT_g_hScriptPadValue);
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			if (LDT_iGetNbParams()-1>2)
			{
				hKeyWordElement->ucMinCounterInput = atoi(LDT_szGetParam(3));
				if (LDT_iGetNbParams()-1>3)
					hKeyWordElement->ucMaxCounterInput = atoi(LDT_szGetParam(4));
			}
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
			/**** Pad Number ****/
			hKeyWordElement=IPT_fn_hAllocKeyWord();
			hKeyWordElement->u_ElementUnion.swJoyNumber = atoi(LDT_szGetParam(1));
			hKeyWordElement->ucMinCounterInput = 0;
			hKeyWordElement->ucMaxCounterInput = 255;
			LST2_M_DynamicInitElement(hKeyWordElement);
			LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceOrderKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceOrderKeyWordElement));
		}
		result=LDT_GetNextEntry();
	} /*end while for entries*/
 
	/*Working for EndSection*/
	hKeyWordElement=IPT_fn_hAllocKeyWord();
	hKeyWordElement->u_ElementUnion.ulNumberOfSequence = ulNumberOfSequence;
	LST2_M_DynamicInitElement(hKeyWordElement);
	LST2_M_DynamicInsertBetween(hKeyWordElement,hSequenceKeyWordElement,LST2_M_DynamicGetNextElement(hSequenceKeyWordElement));
 
	return 0;
}
/*END FS 9/16/98*/
 
/*BEGIN FS 9/16/98*/
/*----------------------------------------------------------------------
 *  Description: callback for ActivateDevices section from .IPT sript file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/16/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadIPT_ActivateDevices( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szEntryName;
	unsigned long ulPadNumber;
 
	/*Working for Entries*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		szEntryName=LDT_szGetEntryName();
		switch (*(long*)(szEntryName))
		{
		case 'daP': /*"Pad"*/
			ulPadNumber = atoi(LDT_szGetParam(1));
			if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber]==IPT_C_ActivateJoystickDevice)
				M_IPTFatalError(E_uwIPT_AlreadyActivateAsAJoystick);
			if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] == IPT_C_ValideDevice)
			{
				fn_wJoyAddActiveJoystick((short)ulPadNumber);
				IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] = IPT_C_ActivatePadDevice;
				IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ActivateDevice;
			}
			break;
		case 'syoJ': /*"Joystick"*/
			ulPadNumber = atoi(LDT_szGetParam(1));
			if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber]==IPT_C_ActivatePadDevice)
				M_IPTFatalError(E_uwIPT_AlreadyActivateAsAPad);
			if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] == IPT_C_ValideDevice)
			{
				fn_wJoyAddActiveJoystick((short)ulPadNumber);
				IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_Pad00DeviceEvent+ulPadNumber] = IPT_C_ActivateJoystickDevice;
				IPT_g_hInputStructure.ucOnePadActivate = IPT_C_ActivateDevice;
			}
			break;
		case 'ByeK': /*"KeyBoard"*/
			if (IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] == IPT_C_ValideDevice)
				IPT_g_hInputStructure.p_ucValideAndActiveDevice[IPT_E_KeyboardDeviceEvent] = IPT_C_ActivateDevice;
			break;
		}
		result=LDT_GetNextEntry();
	} /*end while for entries*/
	return 0;
}
/*END FS 9/16/98*/
 
/*BEGIN FS 9/16/98*/
/*----------------------------------------------------------------------
 *  Description: callback for KeyDefine section from .IPT script file
 *----------------------------------------------------------------------
 *  Input:
 *  Output:
 *----------------------------------------------------------------------
 *  Author: FS
 *  Creation date:9/16/98
 *  Modif date:
 *----------------------------------------------------------------------*/
int LoadIPT_KeyDefine( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
	char *szEntryName;
 
	unsigned long ulKeyNumber,ulKeyMaxNumber;
 
	/*Working for BeginSection*/
	ulKeyMaxNumber = atoi(LDT_szGetParam(1));
	Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
	MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
	if ((IPT_gd_hKeyAndPadDefine = (struct IPT_tdstKeyAndPadDefine_ *)Mmg_fn_p_vAlloc4Ch((ulKeyMaxNumber+1)*sizeof(struct IPT_tdstKeyAndPadDefine_),C_ucMmgDefaultChannel))==NULL)
		M_IPTFatalError(E_uwIPT_NoMemory);
	ulKeyNumber = 0;
 
	/*Working for Entries*/
	result=LDT_GetNextEntry();
	while( result!=ParseResult_EndSection )
	{
		szEntryName=LDT_szGetEntryName();
		switch (*(long*)(szEntryName))
		{
		case 'KweN': /*"NewKeyDefine"*/
			if (ulKeyNumber>ulKeyMaxNumber)
				M_IPTFatalError(E_uwIPT_TooMuchKeyDefine);
 
			if (IPT_fn_p_szGetKeyPointerOfName(LDT_szGetParam(1),IPT_g_hScriptKeyValue)==NULL)
			{
				if (IPT_fn_p_szGetKeyPointerOfName(LDT_szGetParam(1),IPT_g_hScriptPadValue)==NULL)
				{
					M_IPTFatalError(E_uwIPT_UnknownKeyValue);
				}
				else
				{
					IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = IPT_fn_swGetKeyValue(LDT_szGetParam(1),IPT_g_hScriptPadValue)|0x8000;
					IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey   =
					IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_fn_p_szGetKeyPointerOfName(LDT_szGetParam(1),IPT_g_hScriptPadValue);
				}
			}
			else
			{
				IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = IPT_fn_swGetKeyValue(LDT_szGetParam(1),IPT_g_hScriptKeyValue);
				IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey   =
				IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_fn_p_szGetKeyPointerOfName(LDT_szGetParam(1),IPT_g_hScriptKeyValue);
			}
			if ((LDT_iGetNbParams()-1)>1)
			{
				Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
				MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
				if ((IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey = (char *)Mmg_fn_p_vAlloc4Ch(strlen(LDT_szGetParam(2))+1,C_ucMmgDefaultChannel))==NULL)
					M_IPTFatalError(E_uwIPT_NoMemory);
				strcpy(IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey,LDT_szGetParam(2));
				if ((LDT_iGetNbParams()-1)>2)
				{
					Mmg_M_SetModeAlloc4Ch(IPT,E_ucIPTMemoryBlock,C_ucMmgDefaultChannel);
					MMG_fn_vAddMemoryInfo(MMG_C_lTypeIPT,MMG_C_lSubTypeIPT,0);
					if ((IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = (char *)Mmg_fn_p_vAlloc4Ch(strlen(LDT_szGetParam(3))+1,C_ucMmgDefaultChannel))==NULL)
					M_IPTFatalError(E_uwIPT_NoMemory);
					strcpy(IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey,LDT_szGetParam(3));
				}
				else
				{
					IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szAmericanKey = IPT_gd_hKeyAndPadDefine[ulKeyNumber].p_szFrenchKey;
				}
			}
			ulKeyNumber++;
			break;
		}
		result=LDT_GetNextEntry();
	} /*end while for entries*/
 
	/*Working for EndSection*/
	if (ulKeyNumber>ulKeyMaxNumber)
		M_IPTFatalError(E_uwIPT_TooMuchKeyDefine);
	IPT_gd_hKeyAndPadDefine[ulKeyNumber].uwBasedKey = 0xffff;
 
	return 0;
}
/*END FS 9/16/98*/
 
