⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmlif.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:

				/* Set iname to 0 (string) */
				pchResult=NEWARRAY(WCHAR,2);
				if (pchResult!=NULL)
				{
					pchResult[0]=(WCHAR)('0');
					pchResult[1]=0;

					WML_SetVariable(pUser,pSelectElm->pchIName,pchResult,FALSE);
					DEALLOC(&pchResult);
				}
				WML_SetVariable(pUser,pSelectElm->pchIName,NULL,FALSE);
			}
		}
	}
}

/* Add the element after the specified element */
void WML_AddNumberToList(pNUMBERSTRUCT pElement, pNUMBERSTRUCT *ppAfter)
{
	if (pElement!=NULL)
	{
		if (*ppAfter==NULL)
		{
			*ppAfter=pElement;
		}
		else
		{
			(*ppAfter)->pNext=pElement;
		}
	}
}

/*  All selected options are found and a list NUMBERSTRUCT
	with the indices is returned. Indata should be
	a pointer to an option or optgroup. */
pNUMBERSTRUCT WML_GetSelectedOptions (pELEMENTTYPE pElement, pNUMBERSTRUCT *ppAfter, 
								  BOOL *pfError, UINT16 *piIndex)
{
	pELEMENTTYPE pContent=NULL;
	pNUMBERSTRUCT pResult=NULL;
	pOPTIONELEMENT pOption=NULL;
	pNUMBERSTRUCT pTemp=NULL;

	/* Step through all elements on this level */
	while ((pElement!=NULL)&&(!*pfError))
	{
		/* Check OPTGROUP or OPTION */
		if (pElement->iType==Type_OPTGROUP)
		{
			pContent=WML_GetContent(pElement);
			pTemp=WML_GetSelectedOptions(pContent,ppAfter,pfError,piIndex);

			if (pTemp!=NULL)
			{
				if (pResult==NULL)
				{
					pResult=pTemp;
				}
			}
		}
		else if (pElement->iType==Type_OPTION)
		{
			pOption=(OPTIONELEMENT*)(pElement);
			if (pOption!=NULL)
			{
				(*piIndex)++;

				if (pOption->fSelected)
				{
					/* Create new NUMBERSTRUCT */
					pTemp=NEWSTRUCT(NUMBERSTRUCT);

					if (pTemp!=NULL)
					{
						pTemp->iIndex=*piIndex;
						pTemp->pNext=NULL;
					}
					else
					{
						/* Out of memory */
						*pfError=TRUE;
					}

					/* Add to list */
					WML_AddNumberToList(pTemp,ppAfter);

					if (pResult==NULL)
					{
						pResult=pTemp;
					}
					*ppAfter=pTemp;
				}
			}
		}
		pElement=pElement->pNextElement;
	}
	return pResult;
}


/* Returns the number of options in a SELECT element 
   (including those in optgroups). */
UINT16 WML_CountOptions (pELEMENTTYPE pElement, BOOL *fError)
{
	UINT16 iNbr=0;
	pELEMENTTYPE pContent=NULL;

	/* Step through all elements on this level */
	while (pElement!=NULL)
	{
		/* Check OPTGROUP or OPTION */
		if (pElement->iType==Type_OPTGROUP)
		{
			pContent=WML_GetContent(pElement);
			iNbr+=WML_CountOptions(pContent, fError);
		}
		else if (pElement->iType==Type_OPTION)
		{
			iNbr++;
		}
		else 
		{
			/* Error! */
			*fError=TRUE;
		}
		pElement=pElement->pNextElement;
	}
	return iNbr;
}

pNUMBERSTRUCT WML_CreateNUMBERSTRUCT (void)
{
	pNUMBERSTRUCT pTemp=NULL;

	pTemp=NEWSTRUCT(NUMBERSTRUCT);
	if (pTemp!=NULL)
	{
		pTemp->iIndex=0;
		pTemp->pNext=NULL;
	}
	return pTemp;
}

void WML_DeleteNUMBERSTRUCTList (pNUMBERSTRUCT pNumber)
{
	pNUMBERSTRUCT pTemp=NULL;

	while (pNumber!=NULL)
	{
		pTemp=pNumber;
		pNumber=pNumber->pNext;
		DEALLOC(&pTemp);
	}
}

/* Helpnction for ValidateIndex. */
void WML_AddIndexNumber (pNUMBERSTRUCT *ppList, UINT16 iIndex, UINT16 iMax)
{
	pNUMBERSTRUCT pTemp=NULL;
	pNUMBERSTRUCT pBefore=NULL;
	pNUMBERSTRUCT pAfter=NULL;
	pNUMBERSTRUCT pIndex=NULL;
	BOOL fInsert=FALSE;

	if ((iIndex>0)&&(iIndex<=iMax))
	{
		pTemp=*ppList;

		if (*ppList==NULL)
		{
			fInsert=TRUE;
		}

		/* Find where to insert element */
		while ((!fInsert)&&(pTemp!=NULL))
		{
			pAfter=pTemp->pNext;

			if (iIndex<pTemp->iIndex)
			{
				pAfter=pTemp;
				fInsert=TRUE;
			}
			else if (iIndex>pTemp->iIndex)
			{
				pBefore=pTemp;
				pAfter=pTemp->pNext;
				pTemp=pTemp->pNext;
				if (pAfter!=NULL)
				{
					if (iIndex<pAfter->iIndex)
					{
						fInsert=TRUE;
					}
				}
				else
				{
					fInsert=TRUE;
				}
			}
			else
			{
				/* Equal - don't insert */
				fInsert=FALSE;
				pTemp=NULL;
			}
		}

		if (fInsert)
		{
			pIndex=NEWSTRUCT(NUMBERSTRUCT);
			if (pIndex!=NULL)
			{
				pIndex->iIndex=iIndex;
				pIndex->pNext=NULL;
			}

			if (*ppList==NULL)
			{
				*ppList=pIndex;
			}
			else
			{
				pIndex->pNext=pAfter;

				if (pBefore!=NULL)
				{
					pBefore->pNext=pIndex;
				}
				else
				{
					/* First in list */
					*ppList=pIndex;
				}
			}
		}
	}
}

/* Function for ordering the indices in a SELECT element.
   The function sorts the numbers and removes duplicate 
   indices. It also checks if the number is a valid option. 
   The result is stored in a NUMBERSTRUCT. */
pNUMBERSTRUCT WML_ValidateIndex (WCHAR *pchIndex, UINT16 iMax)
{
	pNUMBERSTRUCT pResult=NULL;
	WCHAR chChar;
	BOOL fError=FALSE;
	BOOL fStarted=FALSE;
	BOOL fStopped=FALSE;
	UINT16 iIndex=0;

	if (pchIndex!=NULL)
	{
		chChar=*pchIndex;

		while (chChar!=0)
		{
			if (chChar==' ')
			{
				if (fStarted)
				{
					fStopped=TRUE;	
				}
			}
			else if (chChar=='.')
			{
				/* Decimal index - not allowed */
				fError=TRUE;
				iIndex=0;
			}
			else if ((chChar>=WC('0'))&&(chChar<=WC('9')))
			{
				if (!fStopped)
				{
					iIndex=(UINT16)((iIndex*10)+chChar-WC('0'));
					fStarted=TRUE;
				}
				else 
				{
					fError=TRUE;
				}
			}
			else if (chChar==WC(';'))
			{
				if ((fStarted)&&(!fError))
				{
					WML_AddIndexNumber(&pResult,iIndex,iMax);
				}
	
				iIndex=0;
				fError=FALSE;
				fStarted=FALSE;
				fStopped=FALSE;
			}
			else
			{
				fError=TRUE;
			}
			pchIndex++;
			chChar=*pchIndex;
		}

		/* End of text. Add last index. */
		if ((fStarted)&&(!fError))
		{
			WML_AddIndexNumber(&pResult,iIndex,iMax);
		}
	}
	return pResult;
}

/* Help function for FindIndexNumber. */
UINT16 WML_FindOption (pUA pUser, pELEMENTTYPE pElement, BOOL *pfError, 
				   BOOL *pfFound, WCHAR *pchString)
{
	UINT16 iNbr=0;
	pELEMENTTYPE pContent=NULL;
	pOPTIONELEMENT pOption=NULL;
	WCHAR *pchTemp=NULL;

	/* Step through all elements on this level */
	while ((pElement!=NULL)&&(!*pfFound))
	{
		/* Check OPTGROUP or OPTION */
		if (pElement->iType==Type_OPTGROUP)
		{
			pContent=WML_GetContent(pElement);
			iNbr+=WML_FindOption(pUser,pContent,pfError,pfFound,pchString);
		}
		else if (pElement->iType==Type_OPTION)
		{
			iNbr++;

			/* Get value */
			pOption=(OPTIONELEMENT*)(pElement);

			if (pOption!=NULL)
			{
				pchTemp=WML_GetString(pUser,pOption->pValue);
				if (pchTemp!=NULL)
				{
					/* Remove trailing and leading blanks (?)
					pchTrimTemp=TrimString(pchTemp); */

					if (pchTemp!=NULL)
					{
						/* Compare strings */
						if (COMPARESTRING(pchTemp,pchString)==0)
						{
							*pfFound=TRUE;
						}
					}
					DEALLOC(&pchTemp);
				}
			}
		}
		else
		{
			/* Error! */
			*pfError=TRUE;
		}
		pElement=pElement->pNextElement;
	}
	return iNbr;
}

/* Returns the number of the option with the value
   equal of to that of the indata. (The strings are
   compared.) The function also removes an item from
   a ';'-separated list if necessary.) */
UINT16 WML_FindIndexNumber (pUA pUser, WCHAR *pchString, UINT16 iStrLen, 
						pELEMENTTYPE pOption)
{
	WCHAR *pchTestString=NULL;
	UINT16 iIndex=0;
	BOOL fError=FALSE;
	BOOL fFound=FALSE;

	pchTestString=NEWARRAY(WCHAR,iStrLen+1);
	
	if (pchTestString!=NULL)
	{
		COPYSTRINGN(pchTestString,pchString,iStrLen);
		*(pchTestString+(iStrLen))=0;

		/* Remove trailing and leading blanks (?)
		pchTrimString=TrimString(pchTestString);*/

		if (pchTestString!=NULL)
		{
			/* Find in values in the options */
			iIndex=WML_FindOption(pUser,pOption,&fError,&fFound,pchTestString);
			
			if ((fError)||(!fFound))
			{
				iIndex=0;
			}
		}
		DEALLOC(&pchTestString);
	}
	return iIndex;
}

/* All options indicated in the pchIndex is located and a list
   of the indices is returned (NUMBERSTRUCT). If fMultiple is
   set the different options are found (i.e. "option1;option2"
   means "option1" and "option2" should be set. If fMultiple
   is not set "option1;option2" is considered as ONE item) */
pNUMBERSTRUCT WML_SetKeyIndex (pUA pUser, WCHAR *pchIndex, BOOL fMultiple, 
						   pELEMENTTYPE pOption, UINT16 iMax)
{
	pNUMBERSTRUCT pResult=NULL;
	WCHAR chChar;
	WCHAR *pchStart=NULL;
	UINT16 iIndex=0;

	if (pchIndex!=NULL)
	{
		chChar=*pchIndex;
		pchStart=pchIndex;

		if (fMultiple)
		{
			while (chChar!=0)
			{
				if (chChar==WC(';'))
				{
					iIndex=WML_FindIndexNumber (pUser,pchStart,(UINT16)(pchIndex-pchStart),pOption);
					WML_AddIndexNumber (&pResult, iIndex, iMax);
					pchStart=pchIndex+1;
				}
				pchIndex++;
				chChar=*pchIndex;
			}

			/* End of text. Check last index. */
			iIndex=WML_FindIndexNumber (pUser,pchStart,(UINT16)(pchIndex-pchStart),pOption);
			WML_AddIndexNumber (&pResult, iIndex, iMax);
		}
		else
		{
			/* Find end of string */
			while (chChar!=0)
			{
				pchIndex++;
				chChar=*pchIndex;
			}

			/* Treat whole string as one value, even if it 
			   contains ';'.								*/
			iIndex=WML_FindIndexNumber (pUser,pchStart,(UINT16)(pchIndex-pchStart),pOption);
			WML_AddIndexNumber (&pResult, iIndex, iMax);
		}
	}
	return pResult;
}

/*========================================================================
	WML_SetSelectDefault
==========================================================================*/

/* Sets the default value for a SELECT element. */
BOOL WML_SetSelectDefault (pELEMENTTYPE pSelect, pUA pUser)
{
	pSELECTELEMENT pSelectElm=NULL;
	pELEMENTTYPE pContent=NULL;
	UINT16 iNbrOfOptions=0;
	BOOL fError=FALSE;
	pNUMBERSTRUCT pDefaultOption=NULL;
	pNUMBERSTRUCT pSelectedTemp=NULL;
	WCHAR *pchOptions=NULL;
	WCHAR *pchTempText=NULL;
	UINT16 iNbr1=0;
	UINT16 iNbr2=0;

	if (pUser!=NULL) 
	{
		if (pSelect!=NULL) 
		{
			if (pSelect->iType==Type_SELECT) 
			{
				pSelectElm=(SELECTELEMENT*)(pSelect);

				pContent=WML_GetContent(pSelect);

				/* Count the options */
				iNbrOfOptions=WML_CountOptions (pContent,&fError);

				/* Determine the default option indices */
				if (pSelectElm->pchIName!=NULL)
				{
					/* IName */

					/* Get variable */
					pchOptions=WML_GetVariable(pUser,pSelectElm->pchIName);

					if (pchOptions!=NULL)
					{
						/* Get options */
						pDefaultOption=WML_ValidateIndex(pchOptions,iNbrOfOptions);

						DEALLOC(&pchOptions);
					}
				}
				if ((pDefaultOption==NULL)&&(pSelectElm->pIValue!=NULL))
				{
					/* IDefault */
					pchTempText=WML_GetString(pUser,pSelectElm->pIValue);

					if (pchTempText!=NULL)
					{
						/* Get options */
						pDefaultOption=WML_ValidateIndex(pchTempText,iNbrOfOptions);
	
						DEALLOC(&pchTempText);
					}
				}
				if ((pDefaultOption==NULL)&&(pSelectElm->pchName!=NULL))
				{
					/* Key */
					pchOptions=WML_GetVariable(pUser,pSelectElm->pchName);

					if (pchOptions!=NULL)
					{
						/* Get options */
						pDefaultOption=WML_SetKeyIndex(pUser,pchOptions,
									pSelectElm->fMultiple, pContent,iNbrOfOptions);

						DEALLOC(&pchOptions);
					}
				}
				if ((pDefaultOption==NULL)&&(pSelectElm->pValue!=NULL))
				{
					/* Default */
					pchTempText=WML_GetString(pUser,pSelectElm->pValue);

					if (pchTempText!=NULL)
					{
						/* Get options */
						pDefaultOption=WML_SetKeyIndex(pUser,pchTempText,
										pSelectElm->fMultiple,pContent,iNbrOfOptions);
	
						DEALLOC(&pchTempText);
					}
				}
				if ((pDefaultOption==NULL)&&(pSelectElm->fMultiple))
				{
					/* Multi-choice */
				}
				if ((pDefaultOption==NULL)&&(!pSelectElm->fMultiple))
				{
					/* Single-choice */
					if (iNbrOfOptions>0)
					{
						pDefaultOption=WML_CreateNUMBERSTRUCT ();
						if (pDefaultOption!=NULL)
						{
							pDefaultOption->iIndex=1;
						}
					}
				}

				/* pre-select options */
				pSelectedTemp=pDefaultOption;

				WML_PreSelectOptions (pContent,&pSelectedTemp,&iNbr1,&iNbr2);

				/* Initialise variables */
				WML_SetSelectVariables(pUser, pSelect, pDefaultOption);

				WML_DeleteNUMBERSTRUCTList (pDefaultOption);
				return TRUE;
			}
		}
	}
	return FALSE;
}


/*========================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -