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

📄 si_libs.c

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

	StrFind_Delete( &listOfMatches );

	Var_Delete( &separatorStr );
	Var_Delete( &elIndex );
	Var_Delete( &str );
	Var_Delete( &separatorChar );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_replaceAt( pstructSI si, UINT16 libIndex, UINT8 funcIndex )
{
	pstructVar			separatorStr =	RE_Pop( si->RE );
	pstructVar			elIndex =				RE_Pop( si->RE );
	pstructVar			newEl =					RE_Pop( si->RE );
	pstructVar			str =						RE_Pop( si->RE );
	pstructVar			result =				Var_New();
	pstructVar			separatorChar = Var_New();
	pstructStrFind	listOfMatches = NULL;
	pstructStrFind	el =						NULL;
	INT32						nbrOfElements = 0;
	UINT32					theIndex = 0;
	UINT32					i = 0;
	UINT32					startStrPos = 0;
	UINT32					endStrPos = 0;
	UINT32					newStrLen = 0;
	pstructStrFind	startEl =				NULL; /* the el that has the pos for the start of the element */
	pstructStrFind	endEl =					NULL;	/* the el that has the pos for the end of the element */

	libIndex=libIndex; /* just to get rid of a compiler warning */
	funcIndex=funcIndex; /* just to get rid of a compiler warning */

	if ( (VCR_OK == Var_Convert( str, typeString )) && (VCR_OK == Var_Convert( separatorStr, typeString )) &&
			(VCR_OK == Var_Convert( newEl, typeString )) &&	Var_ConvertMethod(CONVERT_INT_FLOAT, elIndex, NULL)) {

		if (elIndex->type == typeFloat) {
			OpS_Push( si->RE->OpS, &elIndex );
			CallLibraryFunction( si, 1, 0 ); /* Float.int */
			elIndex = RE_Pop( si->RE );
		}

		if ((elIndex->type == typeInteger) && (separatorStr->theStringLen > 0))
    {
			Var_NewString( separatorChar, 1 );
			separatorChar->val.theString[0] = separatorStr->val.theString[0];

			listOfMatches = FindSubstrings( str, separatorChar, TRUE );
			if (listOfMatches->subStrPos != -1) {
				/* now count how many matches were found */
				el = listOfMatches;
				while (el != NULL) {
					nbrOfElements++;
					el = el->next;
				}
				/* when wanting elements the result is the amount of separator matches + 1 */
				nbrOfElements += 1;
			}
			else {
				nbrOfElements = (str->theStringLen == 0) ? (0) : (1);
			}

			/* ensure that the index is within the boundaries */
			theIndex = MAX( 0, elIndex->val.theInt );
			theIndex = MIN( theIndex, (UINT32)(nbrOfElements-1) );

			if (nbrOfElements > 1) {
				endEl = listOfMatches;
				i = 0;
				while (i < theIndex) {
					/* step forward in the list of matches so the element's boundaries are
							found by getting the separator positions around the element */
					startEl = endEl;
					endEl = endEl->next;
					i++;
				}
				startStrPos = (startEl==NULL) ? ( 0 ):( (startEl->subStrPos)+1 );
				endStrPos   = (endEl==NULL)   ? ( str->theStringLen-1 ):( (endEl->subStrPos)-1 );
					/* the separator is not part of the replacement */

				/* create the result string */
				newStrLen = str->theStringLen + (newEl->theStringLen - MAX( 0, (INT32)(endStrPos+1 - startStrPos)));/* the endStrPos can be lower than the start (e.g if is the last element and it is empty)  */
				Var_NewString( result, newStrLen );
				/* copy the string part before the element */
				if (startStrPos > 0) {
					COPYSTRINGN( result->val.theString, str->val.theString, startStrPos );
				}
				/* put in the new element instead of the old one */
				if (newEl->theStringLen > 0) {
					COPYSTRINGN( &(result->val.theString[startStrPos]),
						newEl->val.theString, newEl->theStringLen );
				}
				/* copy the string part after the element */
				if ((endStrPos+1) < str->theStringLen) {
					COPYSTRINGN( &(result->val.theString[startStrPos + newEl->theStringLen]),
						&(str->val.theString[endStrPos+1]), (str->theStringLen - (endStrPos+1)) );
				}
			}
			else {
				/* the string will be replaced by the new element */
				Var_Delete( &result );
				result = newEl;
				newEl = NULL;
			}
		}
		else {
			Var_AssignInvalid( result );
		}
	}
	else {
		Var_AssignInvalid( result );
	}

	StrFind_Delete( &listOfMatches );

	Var_Delete( &separatorStr );
	Var_Delete( &elIndex );
	Var_Delete( &newEl );
	Var_Delete( &str );
	Var_Delete( &separatorChar );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_insertAt( pstructSI si, UINT16 libIndex, UINT8 funcIndex )
{
	pstructVar			separatorStr =	RE_Pop( si->RE );
	pstructVar			elIndex =				RE_Pop( si->RE );
	pstructVar			newEl =					RE_Pop( si->RE );
	pstructVar			str =						RE_Pop( si->RE );
	pstructVar			result =				Var_New();
	pstructVar			separatorChar = Var_New();
	pstructStrFind	listOfMatches = NULL;
	pstructStrFind	el =						NULL;
	INT32						nbrOfElements = 0;
	UINT32					theIndex;
	UINT32					i;
	UINT32					startStrPos = 0;
	UINT32					newStrLen;
	pstructStrFind	startEl =				NULL; /* the el that has the pos for the start of the element */

	libIndex=libIndex; /* just to get rid of a compiler warning */
	funcIndex=funcIndex; /* just to get rid of a compiler warning */

	if ( (VCR_OK == Var_Convert( str, typeString )) && (VCR_OK == Var_Convert( separatorStr, typeString )) &&
			(VCR_OK == Var_Convert( newEl, typeString )) &&	Var_ConvertMethod(CONVERT_INT_FLOAT, elIndex, NULL)) {

		if (elIndex->type == typeFloat) {
			OpS_Push( si->RE->OpS, &elIndex );
			CallLibraryFunction( si, 1, 0 ); /* Float.int */
			elIndex = RE_Pop( si->RE );
		}

		if ((elIndex->type == typeInteger) && (separatorStr->theStringLen > 0))
    {
			Var_NewString( separatorChar, 1 );
			separatorChar->val.theString[0] = separatorStr->val.theString[0];

			listOfMatches = FindSubstrings( str, separatorChar, TRUE );
			if (listOfMatches->subStrPos != -1) {
				/* now count how many matches were found */
				el = listOfMatches;
				while (el != NULL) {
					nbrOfElements++;
					el = el->next;
				}
				/* when wanting elements the result is the amount of separator matches + 1 */
				nbrOfElements += 1;
			}
			else {
				nbrOfElements = (str->theStringLen == 0) ? (0) : (1);
			}

			/* ensure that the index is within the boundaries */
			theIndex = MAX( 0, elIndex->val.theInt );
			theIndex = MIN( theIndex, (UINT32)(nbrOfElements) );
				/* to "append" a new element the index must be able to be "last index + 1" */

			if (nbrOfElements > 0) {

				if (theIndex == 0) {
					startStrPos = 0;
				}
				else if ((INT32)theIndex == nbrOfElements) {
					/* appending an element */
					startStrPos = str->theStringLen;
				}
				else {
					startEl = listOfMatches;
					i = 1;
					while (i < theIndex) {
						/* step forward in the list of matches so the element's boundaries are
								found by getting the separator position */
						startEl = startEl->next;
						i++;
					}
					startStrPos = (startEl==NULL) ? ( 0 ):( (startEl->subStrPos)+1 );
				}

				/* create the result string */
				newStrLen = str->theStringLen + newEl->theStringLen + 1; /* a new separator is needed */
				Var_NewString( result, newStrLen );
				/* copy the string part before the element */
				if (startStrPos > 0) {
					COPYSTRINGN( result->val.theString, str->val.theString, startStrPos );
				}
				/* insert the new element */
				if (startStrPos == str->theStringLen) {
					/* if the new element is inserted at the end, a separatot must be inserted before the element */
					result->val.theString[startStrPos] = separatorChar->val.theString[0];
					startStrPos++; /* the newEl str is now dislocated one step due to this inserted separator
														this dislocation is later removed with "--" (see below) */
				}
				if (newEl->theStringLen > 0) {
					COPYSTRINGN( &(result->val.theString[startStrPos]),
						newEl->val.theString, newEl->theStringLen );
				}
				if (startStrPos != str->theStringLen) {
					/* if not inserted at the end, a separator is inserted after the new element */
					result->val.theString[startStrPos + newEl->theStringLen] = separatorChar->val.theString[0];
					startStrPos++; /* no real meaning but so that the "--" is correct */
				}
				startStrPos--; /* take away the separator dislocation compensation (this is the "--" ;) ) */
				/* copy the string part after the element */
				if (startStrPos < str->theStringLen) {
					COPYSTRINGN( &(result->val.theString[startStrPos + newEl->theStringLen + 1]),
						&(str->val.theString[startStrPos]), (str->theStringLen - startStrPos) );
				}
			}
			else {
				/* the string doesn't contain any elements and the result is the new element */
				Var_Delete( &result );
				result = newEl;
				newEl = NULL;
			}
		}
		else {
			Var_AssignInvalid( result );
		}
	}
	else {
		Var_AssignInvalid( result );
	}

	StrFind_Delete( &listOfMatches );

	Var_Delete( &separatorStr );
	Var_Delete( &elIndex );
	Var_Delete( &newEl );
	Var_Delete( &str );
	Var_Delete( &separatorChar );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_squeeze( pstructSI si, UINT16 libIndex, UINT8 funcIndex )
{
	pstructVar			str =										RE_Pop( si->RE );
	pstructVar			result =								Var_New(); /* init to an empty string */
	UINT32					newStrLen;
	BOOL						lastCharWasWhitespace	=	FALSE;
	UINT32					srcPos;
	UINT32					dstPos;

	libIndex=libIndex; /* just to get rid of a compiler warning */
	funcIndex=funcIndex; /* just to get rid of a compiler warning */

	if (VCR_OK == Var_Convert( str, typeString )) {
		if (str->theStringLen > 0) {
			/* first find out how long the result will be */
			newStrLen = 0;
			srcPos = 0;
			while (srcPos < str->theStringLen) {
				if ( IsWhitespaceChar(str->val.theString[srcPos]) ) {
					if ( ! lastCharWasWhitespace) {
						newStrLen++;
						lastCharWasWhitespace = TRUE;
					}
				}
				else {
					newStrLen++;
					lastCharWasWhitespace = FALSE;
				}

				srcPos++;
			}
			/* now create the new string */
			if (newStrLen < str->theStringLen) {
				/* whitespaces to be removed */
				Var_NewString( result, newStrLen );
				lastCharWasWhitespace = FALSE;
				dstPos = 0;
				srcPos = 0;
				while (srcPos < str->theStringLen) {
					if ( IsWhitespaceChar(str->val.theString[srcPos]) ) {
						if ( ! lastCharWasWhitespace) {
							result->val.theString[dstPos] = str->val.theString[srcPos];
							dstPos++;
							lastCharWasWhitespace = TRUE;
						}
					}
					else {
						result->val.theString[dstPos] = str->val.theString[srcPos];
						dstPos++;
						lastCharWasWhitespace = FALSE;
					}

					srcPos++;
				}
			}
			else {
				/* no whitespaces to be removed means that the result is the same as str */
				Var_Delete( &result );
				result = str;
				str = NULL;
			}
		}
		/* else the result is an empty string */
	}
	else {
		Var_AssignInvalid( result );
	}

	Var_Delete( &str );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_trim( pstructSI si, UINT16 libIndex, UINT8 funcIndex )
{
	pstructVar			str =										RE_Pop( si->RE );
	pstructVar			result =								Var_New(); /* init to an empty string */
	UINT32					newStrLen;
	UINT32					firstRealCharPos;
	UINT32					lastRealCharPos;

	libIndex=libIndex; /* just to get rid of a compiler warning */
	funcIndex=funcIndex; /* just to get rid of a compiler warning */

	if (VCR_OK == Var_Convert( str, typeString )) {
		firstRealCharPos = 0;
		while (	(firstRealCharPos < str->theStringLen) &&
						IsWhitespaceChar(str->val.theString[firstRealCharPos]) )
    {
			firstRealCharPos++;
		}

		if (firstRealCharPos < str->theStringLen)
    {
    	lastRealCharPos = str->theStringLen -1;
			while (	(lastRealCharPos > 0) &&
							IsWhitespaceChar(str->val.theString[lastRealCharPos]) )
      {
				lastRealCharPos--;
			}
			if ((firstRealCharPos == 0) && (lastRealCharPos == str->theStringLen -1))
      {
				/* no whitespace to remove thus the result is str */
				Var_Delete( &result );
				result = str;
				str = NULL;
			}
			else
      {
				newStrLen = lastRealCharPos - firstRealCharPos +1;
				Var_NewString( result, newStrLen );
				COPYSTRINGN( result->val.theString, &(str->val.theString[firstRealCharPos]), newStrLen );
			}
		}
		/* else => the whole string is filled with whitespace and the result is then an empty string */
	}
	else {
		Var_AssignInvalid( result );
	}

	Var_Delete( &str );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_compare( pstructSI si, UINT16 libIndex, UINT8 funcIndex )
{
	pstructVar			str2 =			RE_Pop( si->RE );
	pstructVar			str1 =			RE_Pop( si->RE );
	pstructVar			result =		Var_New(); /* init to an empty string */
	UINT32					i;
	UINT32					minLen;

	libIndex=libIndex; /* just to get rid of a compiler warning */
	funcIndex=funcIndex; /* just to get rid of a compiler warning */

	if ( (VCR_OK == Var_Convert( str1, typeString )) && (VCR_OK == Var_Convert( str2, typeString ))) {
		Var_AssignInt( result, 10 ); /* anything that is not -1, 0 or 1 */
		minLen = MIN ( str1->theStringLen, str2->theStringLen );
		i = 0;
		while ((result->val.theInt == 10) && (i < minLen) ) {
			if (str1->val.theString[i] == str2->val.theString[i]) {
				/* equal */
				i++;
			}
			else if (str1->val.theString[i] < str2->val.theString[i]) {
				/* str1 < str2 */
				Var_AssignInt( result, -1 );
			}
			else {
				/* str1 > str2 */
				Var_AssignInt( result, 1 );
			}
		}

		if (result->val.theInt == 10) {
			/* they matched, but their lengths may be different */
			if (str1->theStringLen == str2->theStringLen) {
				Var_AssignInt( result, 0 );
			}
			else if (str1->theStringLen < str2->theStringLen) {
				/* str1 < str2 */
				Var_AssignInt( result, -1 );
			}
			else {
				/* str1 > str2 */
				Var_AssignInt( result, 1 );
			}
		}
	}
	else {
		Var_AssignInvalid( result );
	}
	Var_Delete( &str2 );
	Var_Delete( &str1 );

	OpS_Push( si->RE->OpS, &result );
	return ERR_WAE_WMLS_NONE;
}


enumErrorCode Call_String_toString( pstructSI si, UINT16 libIndex, UINT8 f

⌨️ 快捷键说明

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