highlights.cpp
来自「MudMaster 2000 的C++源码」· C++ 代码 · 共 623 行 · 第 1/2 页
CPP
623 行
/************************************************************************************
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 "Highlights.h"
CHighlights::CHighlights()
{
m_ptrList.SetSize(DEFAULT_HIGHLIGHT_ARRAY_SIZE,DEFAULT_HIGHLIGHT_GROW_SIZE);
m_nCount = 0;
m_nGetIndex = 0;
}
CHighlights::~CHighlights()
{
RemoveAll();
}
void CHighlights::RemoveAll()
{
HIGHLIGHT *pHigh;
for (int i=0;i<m_nCount;i++)
{
pHigh = (HIGHLIGHT *)m_ptrList.GetAt(i);
delete pHigh;
}
m_ptrList.RemoveAll();
m_nCount = 0;
m_nGetIndex = 0;
}
int CHighlights::RemoveGroup(const char *pszGroup)
{
HIGHLIGHT *pHigh;
int nCount = 0;
for (int i=m_nCount-1;i>-1;i--)
{
pHigh = (HIGHLIGHT *)m_ptrList.GetAt(i);
if (pHigh->strGroup == pszGroup)
{
Remove(i);
nCount++;
}
}
return(nCount);
}
int CHighlights::Add(const char *pszMask, const char *pszColor, const char *pszGroup)
{
int nLen = strlen(pszMask);
if (!nLen)
return(FALSE);
// Parese the colors.
WORD wAttr = F_LIGHTGREY | B_BLACK;
int nColorLen = strlen(pszColor);
char *pColor = new char[nColorLen+1];
strcpy(pColor,pszColor);
char *ptr = strtok(pColor,",");
if (ptr == NULL)
wAttr = TextToAttr(pszColor,wAttr);
else
while(ptr != NULL)
{
wAttr = TextToAttr(ptr,wAttr);
ptr = strtok(NULL,",");
}
delete [] pColor;
HIGHLIGHT high;
high.bAnchored = FALSE;
high.nTokens = 0;
high.nVars = 0;
high.bStartVar = FALSE;
high.strGroup = pszGroup;
high.bEnabled = TRUE;
high.wAttr = wAttr;
if (*pszMask == '^')
{
high.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) == '%')
{
high.strToken[high.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)
high.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 (high.strToken[high.nTokens].IsEmpty() && (nIndex != 0 && high.bStartVar != TRUE))
return(FALSE);
// Advance the token counter only if we are not starting with a var.
if (!nIndex && high.bStartVar)
{
// Don't advance the token count.
}
else
high.nTokens++;
// Should have more than 10 vars either.
if (high.nVars > 9)
return(FALSE);
high.nVars++;
nIndex += 2;
continue;
}
// Need to make sure we don't try to store more thant 10
// tokens.
if (high.nTokens > 9)
return(FALSE);
high.strToken[high.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 (!high.strToken[high.nTokens].IsEmpty())
high.nTokens++;
// A highlight declared as anchored, but having a variable at the
// front of it is pointless. Make sure it doesn't happen.
if (high.bAnchored && high.bStartVar)
high.bAnchored = FALSE;
// Put the pointer back to how it used to be for comparisons.
if (high.bAnchored)
{
nLen++;
pszMask--;
}
CString strTemp;
HIGHLIGHT *pHigh;
for (int i=0;i<m_nCount;i++)
{
pHigh = (HIGHLIGHT *)m_ptrList.GetAt(i);
HighlightToMask(pHigh,strTemp);
// high already exists.
if (strTemp == pszMask)
{
CopyHighlight(pHigh,&high);
return(i+1);
}
if (stricmp((pHigh->bAnchored ? strTemp.Right(strTemp.GetLength()-1) : strTemp),
(*pszMask == '^' ? pszMask+1 : pszMask)) > 0)
{
HIGHLIGHT *pNew = new HIGHLIGHT;
CopyHighlight(pNew,&high);
m_ptrList.InsertAt(i,pNew);
m_nCount++;
return(i+1);
}
}
// Fell thru the loop, must go at the end.
HIGHLIGHT *pNew = new HIGHLIGHT;
CopyHighlight(pNew,&high);
m_ptrList.SetAtGrow(i,pNew);
m_nCount++;
return(m_nCount);
}
BOOL CHighlights::Remove(const char *pszMask)
{
HIGHLIGHT *pHigh = FindExact(pszMask);
if (pHigh == NULL)
return(FALSE);
m_ptrList.RemoveAt(m_nGetIndex);
m_nCount--;
delete pHigh;
return(TRUE);
}
BOOL CHighlights::Remove(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
HIGHLIGHT *pHigh = (HIGHLIGHT *)m_ptrList.GetAt(nIndex);
if (pHigh == NULL)
return(FALSE);
m_ptrList.RemoveAt(nIndex);
m_nCount--;
delete pHigh;
return(TRUE);
}
void CHighlights::CopyHighlight(HIGHLIGHT *pDest, HIGHLIGHT *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;
pDest->wAttr = pSource->wAttr;
}
void CHighlights::HighlightToMask(HIGHLIGHT *pHigh, CString &strMask)
{
strMask.Empty();
if (pHigh->bAnchored)
strMask += "^";
int nVarIndex = 0;
CString strTemp;
if (pHigh->bStartVar)
{
strTemp.Format("%%%c",(char)(nVarIndex+'0'));
strMask += strTemp;
nVarIndex++;
}
for (int i=0;i<pHigh->nTokens;i++)
{
strMask += pHigh->strToken[i];
if (nVarIndex < pHigh->nVars)
{
strTemp.Format("%%%c",(char)(nVarIndex+'0'));
strMask += strTemp;
nVarIndex++;
}
}
}
int CHighlights::DisableGroup(const char *pszGroup)
{
int nCount = 0;
HIGHLIGHT *pHigh = GetFirst();
while(pHigh != NULL)
{
if (pHigh->strGroup == pszGroup)
{
pHigh->bEnabled = FALSE;
nCount++;
}
pHigh = GetNext();
}
return(nCount);
}
int CHighlights::EnableGroup(const char *pszGroup)
{
int nCount = 0;
HIGHLIGHT *pHigh = GetFirst();
while(pHigh != NULL)
{
if (pHigh->strGroup == pszGroup)
{
pHigh->bEnabled = TRUE;
nCount++;
}
pHigh = GetNext();
}
return(nCount);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?