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

📄 tintinif.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

------------------------------------------------------------------------------

	TinTin class miscellaneous methods.  Originally modified from TinTin++,
	(T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t, originally coded by
	Peter Unold 1992.

----------------------------------------------------------------------------*/

// $Header: /home/cvs/chaco/modules/client/msw/ChWorld/TinTinIf.cpp,v 2.7 1996/09/12 19:10:16 pritham Exp $

#include "headers.h"
#if !defined(CH_PUEBLO_PLUGIN)
#include "resource.h"
#else
#include "vwrres.h"
#endif

#include "TinTin.h"


/*----------------------------------------------------------------------------
	Constants
----------------------------------------------------------------------------*/

#define MATH_STACK_SIZE		100


/*----------------------------------------------------------------------------
	Static variables
----------------------------------------------------------------------------*/

CH_INTERN_VAR MathOps	mathStack[MATH_STACK_SIZE];


/*----------------------------------------------------------------------------
	Utility functions
----------------------------------------------------------------------------*/

inline void MakeString( int iVal, string& strVal )
{
	strVal.Format( "%d", iVal );
}


/*----------------------------------------------------------------------------
	TinTin class protected methods
----------------------------------------------------------------------------*/

void TinTin::DoIf( const string& strArgs )
{
	const char*		pstrArgs = strArgs;
	string			strCondition;
	string			strThen;
	string			strElse;
	string			strTemp;

	pstrArgs = GetArgInBraces( pstrArgs, strCondition, false );
	pstrArgs = GetArgInBraces( pstrArgs, strThen, true );
	pstrArgs = GetArgInBraces( pstrArgs, strElse, true );

	SubstituteVars( strCondition, strTemp, true );
	SubstituteMyVars( strTemp, strCondition, true );

	EvalExpression( strCondition, strTemp );
	if (strTemp.IsEmpty() || strTemp == m_strFalse)
	{
		SubstituteVars( strElse, strTemp );
		SubstituteMyVars( strTemp, strElse );

		ParseInput( strElse );
	}
	else
	{
		SubstituteVars( strThen, strTemp );
		SubstituteMyVars( strTemp, strThen );

		ParseInput( strThen );
	}
}


void TinTin::DoMath( const string& strArgs )
{
	const char*		pstrArgs = strArgs;
	string			strVar;
	string			strRight;
	string			strTemp;
	TinTinListNode*	pNode;

	pstrArgs = GetArgInBraces( pstrArgs, strVar, false );
	pstrArgs = GetArgInBraces( pstrArgs, strRight, true );

	if (strVar.IsEmpty() || strRight.IsEmpty())
	{
		string		strMessage;

		LOADSTRING( IDS_TINTIN_MATH_PARAM_ERR, strMessage );
		ErrMessage( strMessage );
	}
	else if (!IsVarNameValid( strVar ))
	{
		string		strFormat;
		string		strMessage;

		LOADSTRING( IDS_TINTIN_VAR_NAME_ERR, strFormat );
		strMessage.Format( strFormat, (const char*)strVar );
		ErrMessage( strMessage );
	}
	else
	{
		SubstituteVars( strRight, strTemp, true );
		SubstituteMyVars( strTemp, strRight, true );

		EvalExpression( strRight, strTemp );

		if (pNode = GetListVars()->Search( strVar ))
		{
			GetListVars()->DeleteNode( pNode );
		}

		GetListVars()->InsertNode( strVar, strTemp, "0" );
	}
}


void TinTin::EvalExpression( const char* pstrArgs, string& strResult )
{
	::MakeString( true, strResult );
											// Parse the expression onto the stack
	if (CompileExpression( pstrArgs ))
	{
		bool	boolDone = false;

		while (!boolDone)
		{
			int		iIndex = 0;
			int		iBegin = -1;
			int		iEnd = -1;
			int		iPrev = -1;
			bool	boolCloseParenFound = false;

			while (mathStack[iIndex].GetNext() && !boolCloseParenFound)
			{
				if (mathStack[iIndex].GetOp() == opOpenParen)
				{
					iBegin = iIndex;
				}
				else if (mathStack[iIndex].GetOp() == opCloseParen)
				{
					iEnd = iIndex;
					boolCloseParenFound = true;
				}

				iPrev = iIndex;
				iIndex = mathStack[iIndex].GetNext();
			}

			if ((!boolCloseParenFound && (iBegin != -1)) ||
					(boolCloseParenFound && (iBegin == -1)))
			{
				ErrMessage( "#Error - Unmatched parentheses." );
				boolDone = true;
				::MakeString( false, strResult );
			}
			else if (!boolCloseParenFound)
			{
				if (iPrev == -1)
				{
					boolDone = true;

					if (mathStack[0].IsString())
					{
						strResult = *(mathStack[0].GetString());
					}
					else
					{
						::MakeString( mathStack[0].GetInt(), strResult );
					}
				}
				else
				{
					iBegin = -1;
					iEnd = iIndex;
				}
			}
			
			if (!boolDone && !DoOneInside( iBegin, iEnd ))
			{
				string		strMessage;

				strMessage.Format( "# Error - Invalid expression to evaluate in {%s}",
										pstrArgs );
				ErrMessage( strMessage );
				::MakeString( false, strResult );
				boolDone = true;
			}
		}
	}
	else
	{
		::MakeString( false, strResult );
	}

	ResetExpression();
}


bool TinTin::CompileExpression( const char* pstrArgs )
{
	int			iIndex = 0;

	while (*pstrArgs)
	{
		if (isdigit( *pstrArgs ))
		{
			const char*		pstrStart;
			int				iVal;

			mathStack[iIndex].SetOp( opInt );

			pstrStart = pstrArgs;
			while (isdigit( *pstrArgs ))
			{
				pstrArgs++;
			}

			sscanf( pstrStart, "%d", &iVal );
			mathStack[iIndex].Set( iVal );
			pstrArgs--;
		}
		else
		{
			switch( *pstrArgs )
			{
				case ' ':
				{
					break;
				}

				case '(':
				{
					mathStack[iIndex].SetOp( opOpenParen );
					break;
				}

				case ')':
				{
					mathStack[iIndex].SetOp( opCloseParen );
					break;
				}

				case '!':
				{
					if (*(pstrArgs + 1) == '=')
					{
						mathStack[iIndex].SetOp( opCompNotEqual );
						pstrArgs++;
					}
					else
					{
						mathStack[iIndex].SetOp( opBooleanNot );
					}
					break;
				}

				case '_':
				{
					mathStack[iIndex].SetOp( opLowercase );
					break;
				}

				case '*':
				{
					mathStack[iIndex].SetOp( opMult );
					break;
				}

				case '/':
				{
					mathStack[iIndex].SetOp( opDiv );
					break;
				}

				case '+':
				{
					mathStack[iIndex].SetOp( opAdd );
					break;
				}

				case '-':
				{
					TTOperator		oper = opInvalid;

					if (iIndex > 0)
					{
						oper = mathStack[iIndex-1].GetOp();
					}

					if (oper == opInt)
					{
						mathStack[iIndex].SetOp( opSubtract );
					}
					else
					{
						const char*		pstrStart;
						int				iVal;

						pstrStart = pstrArgs;
						pstrArgs++;
						while (isdigit(*pstrArgs))
						{
							pstrArgs++;
						}

						sscanf( pstrStart, "%d", &iVal );
						mathStack[iIndex].Set( iVal );
						mathStack[iIndex].SetOp( opInt );
						pstrArgs--;
					}
					break;
				}

				case '>':
				{
					if (*(pstrArgs + 1) == '=')
					{
						mathStack[iIndex].SetOp( opCompGreaterEqual );
						pstrArgs++;
					}
					else
					{
						mathStack[iIndex].SetOp( opCompGreater );
					}
					break;
				}

				case '<':
				{
					if (*(pstrArgs+1) == '=')
					{
						pstrArgs++;
						mathStack[iIndex].SetOp( opCompLessEqual );
					}
					else
					{
						mathStack[iIndex].SetOp( opCompLess );
					}
					break;
				}

				case '=':
				{
					mathStack[iIndex].SetOp( opCompEqual );

					if (*(pstrArgs + 1) == '=')
					{
						pstrArgs++;
					}
					break;
				}

				case '&':
				{
					mathStack[iIndex].SetOp( opBoolAnd );

					if (*(pstrArgs + 1) == '&')
					{
						pstrArgs++;
					}
					break;
				}

				case '|':
				{
					mathStack[iIndex].SetOp( opBoolOr );

					if (*(pstrArgs+1) == '|')
					{
						pstrArgs++;
					}
					break;
				}
											/* Strings can start with a single
												or double quote */
				case '\'':
				case '"':
				{
					char	cQuote = *pstrArgs;
					string	strTemp;

					mathStack[iIndex].SetOp( opInt );

					pstrArgs++;
					while (*pstrArgs && (*pstrArgs != cQuote))
					{
						if ('\\' == *pstrArgs)
						{
							pstrArgs++;
						}

						if (*pstrArgs)
						{
							strTemp += *pstrArgs;
							pstrArgs++;
						}
					}

					mathStack[iIndex].SetOp( opString );
					mathStack[iIndex].Set( strTemp );
					break;
				}

				case 'T':
				{
					mathStack[iIndex].SetOp( opInt );
					mathStack[iIndex].Set( true );
					break;
				}

				case 'F':
				{
					mathStack[iIndex].SetOp( opInt );
					mathStack[iIndex].Set( false );
					break;
				}

				default:
				{
					Message( "# Error - Invalid expression in #if or #math." );
					return false;

⌨️ 快捷键说明

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