📄 gags.cpp
字号:
/************************************************************************************
Copyright (c) 2000 Aaron O'Neil
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1) Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3) Redistributions in binary form must reproduce the above copyright notice on
program startup. Additional credits for program modification are acceptable
but original copyright and credits must be visible at startup.
4) You may charge a reasonable copying fee for any distribution of Mud Master.
You may charge any fee you choose for support of Mud Master. You may not
charge a fee for Mud Master itself.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/
#include "StdAfx.h"
#include "Extern.h"
#include "Gags.h"
CGags::CGags()
{
m_ptrList.SetSize(DEFAULT_GAG_ARRAY_SIZE,DEFAULT_GAG_GROW_SIZE);
m_nCount = 0;
m_nGetIndex = 0;
}
CGags::~CGags()
{
RemoveAll();
}
void CGags::RemoveAll()
{
GAG *pGag;
for (int i=0;i<m_nCount;i++)
{
pGag = (GAG *)m_ptrList.GetAt(i);
delete pGag;
}
m_ptrList.RemoveAll();
m_nCount = 0;
m_nGetIndex = 0;
}
int CGags::RemoveGroup(const char *pszGroup)
{
GAG *pGag;
int nCount = 0;
for (int i=m_nCount-1;i>-1;i--)
{
pGag = (GAG *)m_ptrList.GetAt(i);
if (pGag->strGroup == pszGroup)
{
Remove(i);
nCount++;
}
}
return(nCount);
}
int CGags::Add(const char *pszMask, const char *pszGroup)
{
int nLen = strlen(pszMask);
if (!nLen)
return(FALSE);
GAG gag;
gag.bAnchored = FALSE;
gag.nTokens = 0;
gag.nVars = 0;
gag.bStartVar = FALSE;
gag.strGroup = pszGroup;
gag.bEnabled = TRUE;
if (*pszMask == '^')
{
gag.bAnchored = TRUE;
nLen--;
pszMask++;
}
int nIndex = 0;
while(nIndex < nLen)
{
// Need to check for the escape char to see if they
// are just trying to use a % instead of indicating
// a variable.
if (*(pszMask+nIndex) == _config.chEscape && nIndex+1 < nLen && *(pszMask+nIndex+1) == '%')
{
gag.strToken[gag.nTokens] += '%';
nIndex += 2;
continue;
}
// Found a variable.
if (*(pszMask+nIndex) == '%' && nIndex+1 < nLen && isdigit(*(pszMask+nIndex+1)))
{
// Need to know if the trigger starts with a variable.
if (!nIndex)
gag.bStartVar = TRUE;
// Should not ever have an empty token. This would be caused
// by creating a trigger like this: "%0%1 stuff" which would
// be impossible to evaluate.
if (gag.strToken[gag.nTokens].IsEmpty() && (nIndex != 0 && gag.bStartVar != TRUE))
return(FALSE);
// Advance the token counter only if we are not starting with a var.
if (!nIndex && gag.bStartVar)
{
// Don't advance the token count.
}
else
gag.nTokens++;
// Should have more than 10 vars either.
if (gag.nVars > 9)
return(FALSE);
gag.nVars++;
nIndex += 2;
continue;
}
// Need to make sure we don't try to store more thant 10
// tokens.
if (gag.nTokens > 9)
return(FALSE);
gag.strToken[gag.nTokens] += *(pszMask+nIndex);
nIndex++;
}
// If dropped out of loop because of reaching the end of line
// should have to increment the token counter unless it is
// empty.
if (!gag.strToken[gag.nTokens].IsEmpty())
gag.nTokens++;
// A gag declared as anchored, but having a variable at the
// front of it is pointless. Make sure it doesn't happen.
if (gag.bAnchored && gag.bStartVar)
gag.bAnchored = FALSE;
// Put the pointer back to how it used to be for comparisons.
if (gag.bAnchored)
{
nLen++;
pszMask--;
}
CString strTemp;
GAG *pGag;
for (int i=0;i<m_nCount;i++)
{
pGag = (GAG *)m_ptrList.GetAt(i);
GagToMask(pGag,strTemp);
// Gag already exists.
if (strTemp == pszMask)
{
CopyGag(pGag,&gag);
return(i+1);
}
if (stricmp((pGag->bAnchored ? strTemp.Right(strTemp.GetLength()-1) : strTemp),
(*pszMask == '^' ? pszMask+1 : pszMask)) > 0)
{
GAG *pNew = new GAG;
CopyGag(pNew,&gag);
m_ptrList.InsertAt(i,pNew);
m_nCount++;
return(i+1);
}
}
// Fell thru the loop, must go at the end.
GAG *pNew = new GAG;
CopyGag(pNew,&gag);
m_ptrList.SetAtGrow(m_nCount,pNew);
m_nCount++;
return(m_nCount);
}
BOOL CGags::Remove(const char *pszMask)
{
GAG *pGag = FindExact(pszMask);
if (pGag == NULL)
return(FALSE);
m_ptrList.RemoveAt(m_nGetIndex);
m_nCount--;
delete pGag;
return(TRUE);
}
BOOL CGags::Remove(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
GAG *pGag = (GAG *)m_ptrList.GetAt(nIndex);
if (pGag == NULL)
return(FALSE);
m_ptrList.RemoveAt(nIndex);
m_nCount--;
delete pGag;
return(TRUE);
}
void CGags::CopyGag(GAG *pDest, GAG *pSource)
{
pDest->bAnchored = pSource->bAnchored;
pDest->nTokens = pSource->nTokens;
pDest->nVars = pSource->nVars;
for (int i=0;i<10;i++)
pDest->strToken[i] = pSource->strToken[i];
pDest->bStartVar = pSource->bStartVar;
pDest->strGroup = pSource->strGroup;
pDest->bEnabled = pSource->bEnabled;
}
void CGags::GagToMask(GAG *pGag, CString &strMask)
{
strMask.Empty();
if (pGag->bAnchored)
strMask += "^";
int nVarIndex = 0;
CString strTemp;
if (pGag->bStartVar)
{
strTemp.Format("%%%c",(char)(nVarIndex+'0'));
strMask += strTemp;
nVarIndex++;
}
for (int i=0;i<pGag->nTokens;i++)
{
strMask += pGag->strToken[i];
if (nVarIndex < pGag->nVars)
{
strTemp.Format("%%%c",(char)(nVarIndex+'0'));
strMask += strTemp;
nVarIndex++;
}
}
}
int CGags::DisableGroup(const char *pszGroup)
{
int nCount = 0;
GAG *pGag = GetFirst();
while(pGag != NULL)
{
if (pGag->strGroup == pszGroup)
{
pGag->bEnabled = FALSE;
nCount++;
}
pGag = GetNext();
}
return(nCount);
}
int CGags::EnableGroup(const char *pszGroup)
{
int nCount = 0;
GAG *pGag = GetFirst();
while(pGag != NULL)
{
if (pGag->strGroup == pszGroup)
{
pGag->bEnabled = TRUE;
nCount++;
}
pGag = GetNext();
}
return(nCount);
}
GAG* CGags::GetFirst()
{
if (!m_nCount)
return(NULL);
m_nGetIndex = 0;
return((GAG *)m_ptrList.GetAt(m_nGetIndex));
}
GAG* CGags::GetNext()
{
if (m_nGetIndex+1 == m_nCount)
return(NULL);
m_nGetIndex++;
return((GAG *)m_ptrList.GetAt(m_nGetIndex));
}
GAG* CGags::GetAt(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(NULL);
return((GAG *)m_ptrList.GetAt(nIndex));
}
GAG* CGags::FindExact(const char *pszMask)
{
GAG *pGag;
CString strMask;
for (int i=0;i<m_nCount;i++)
{
pGag = (GAG *)m_ptrList.GetAt(i);
GagToMask(pGag,strMask);
if (strMask == pszMask)
{
m_nGetIndex = i;
return(pGag);
}
}
return(NULL);
}
BOOL CGags::CheckGag(const char *pszLine)
{
CString strLine(pszLine);
const char *ptr;
CString strTemp;
GAG *pGag;
int nVarCount;
int nOffset;
int nLineLen = strLine.GetLength();
int j;
for (int i=0;i<m_nCount;i++)
{
pGag = (GAG *)m_ptrList.GetAt(i);
if (!pGag->bEnabled)
continue;
// If there aren't any vars, it is really simple. Just see if
// we can find the test in the line.
if (!pGag->nVars)
{
if ((pGag->bAnchored && strLine == pGag->strToken[0]) ||
(!pGag->bAnchored && strLine.Find(pGag->strToken[0]) != -1))
return(TRUE);
}
nVarCount = 0;
nOffset = 0;
for (j=0;j<pGag->nTokens;j++)
{
ptr = strstr(pszLine+nOffset,pGag->strToken[j]);
if (ptr == NULL)
break;
// If the action starts with a variable and we are on the
// very first token.
if (!j)
{
// If it is anchored, need to make sure that ptr is at
// the beginning of the text.
if (pGag->bAnchored && ptr != pszLine)
break;
if (pGag->bStartVar)
{
// If the string we just found is pointing at the
// front of the line, then there is no var data to
// be picked off the front.
if (ptr == pszLine)
break;
nVarCount++;
}
// Offset needs to point to 1 place after the first token
// ptr-pszLine will skip over the var data + the lenght of the token
// +1 because vars cannot be butted up against on another.
nOffset += ptr-pszLine + pGag->strToken[j].GetLength() + 1;
continue;
}
strTemp = strLine.Mid(nOffset-1,ptr-pszLine-nOffset+1);
nOffset += strTemp.GetLength() + pGag->strToken[j].GetLength();
nVarCount++;
} // for (j=0;j<pAction->nTokens;j++)
// If we exits and we don't have all the variables, the trigger must have
// ended with a variable.
if (nVarCount < pGag->nVars)
nVarCount++;
if (j == pGag->nTokens)
return(TRUE);
} // for (int i=0;i<m_nCount;i++)
return(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -