📄 aliasmap.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 "AliasMap.h"
CAliasMap::CAliasMap()
{
m_ptrList.SetSize(DEFAULT_ALIAS_ARRAY_SIZE,DEFAULT_ALIAS_GROW_SIZE);
m_nCount = 0;
m_nGetIndex = 0;
}
CAliasMap::~CAliasMap()
{
RemoveAll();
}
void CAliasMap::RemoveAll()
{
ALIAS_MAP *pMap;
for (int i=0;i<m_nCount;i++)
{
pMap = (ALIAS_MAP *)m_ptrList.GetAt(i);
delete pMap;
}
m_ptrList.RemoveAll();
m_nCount = 0;
m_nGetIndex = 0;
HashIt();
}
int CAliasMap::RemoveGroup(const char *pszGroup)
{
ALIAS_MAP *pAlias;
int nCount = 0;
for (int i=m_nCount-1;i>-1;i--)
{
pAlias = (ALIAS_MAP *)m_ptrList.GetAt(i);
if (pAlias->strGroup == pszGroup)
{
Remove(i);
nCount++;
}
}
HashIt();
return(nCount);
}
int CAliasMap::DisableGroup(const char *pszGroup)
{
int nCount = 0;
ALIAS_MAP *pMap = GetFirst();
while(pMap != NULL)
{
if (pMap->strGroup == pszGroup)
{
pMap->bEnabled = FALSE;
nCount++;
}
pMap = GetNext();
}
return(nCount);
}
int CAliasMap::EnableGroup(const char *pszGroup)
{
int nCount = 0;
ALIAS_MAP *pMap = GetFirst();
while(pMap != NULL)
{
if (pMap->strGroup == pszGroup)
{
pMap->bEnabled = TRUE;
nCount++;
}
pMap = GetNext();
}
return(nCount);
}
int CAliasMap::Add(const char *pszKey, const char *pszText,
const char *pszGroup)
{
// Empty strings are not valid.
if (pszKey == NULL || *pszKey == '\x0' || pszText == NULL || *pszText == '\x0')
return(FALSE);
// Aliases cannot start with a variable.
if (*pszKey == '%')
return(FALSE);
CString strKey(pszKey);
BOOL bHasVar;
char nVarNum;
int nIndex = strKey.Find('%');
if (nIndex == -1)
bHasVar = FALSE;
else
{
bHasVar = TRUE;
nVarNum = strKey[nIndex+1] - '0';
if (nVarNum < 0 || nVarNum > 9)
{
CString strError;
strError.Format("Invalid variable number: %s",pszKey);
PrintMessage(strError);
return(FALSE);
}
strKey = strKey.Left(nIndex-1);
}
ALIAS_MAP *pMap;
for (int i=0;i<m_nCount;i++)
{
pMap = (ALIAS_MAP *)m_ptrList.GetAt(i);
// If the alias already exists, need to replace
// the info about it with new info.
if (strKey == pMap->strKey)
{
pMap->bHasVar = bHasVar;
pMap->nVarNum = nVarNum;
pMap->strText = pszText;
pMap->strGroup = pszGroup;
HashIt();
return(i+1);
}
// Insert the new entry.
if (strKey < pMap->strKey)
{
ALIAS_MAP *pNew = new ALIAS_MAP;
pNew->strKey = strKey;
pNew->bHasVar = bHasVar;
pNew->nVarNum = nVarNum;
pNew->strText = pszText;
pNew->strGroup = pszGroup;
pNew->bEnabled = TRUE;
m_ptrList.InsertAt(i,pNew);
m_nCount++;
HashIt();
return(i+1);
}
}
// If fell thru the loops, must be a new entry, but
// goes at the tail of the list.
ALIAS_MAP *pNew = new ALIAS_MAP;
pNew->strKey = strKey;
pNew->bHasVar = bHasVar;
pNew->nVarNum = nVarNum;
pNew->strText = pszText;
pNew->strGroup = pszGroup;
pNew->bEnabled = TRUE;
m_ptrList.SetAtGrow(i,pNew);
m_nCount++;
HashIt();
return(m_nCount);
}
BOOL CAliasMap::Remove(const char *pszKey)
{
ALIAS_MAP *pMap = FindExact(pszKey);
if (pMap == NULL)
return(FALSE);
m_ptrList.RemoveAt(m_nGetIndex);
m_nCount--;
delete pMap;
HashIt();
return(TRUE);
}
BOOL CAliasMap::Remove(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
ALIAS_MAP *pMap = (ALIAS_MAP *)m_ptrList.GetAt(nIndex);
if (pMap == NULL)
return(FALSE);
m_ptrList.RemoveAt(nIndex);
m_nCount--;
delete pMap;
HashIt();
return(TRUE);
}
ALIAS_MAP* CAliasMap::FindExact(const char *pszText)
{
if (!*pszText)
return(NULL);
HASHITEM *phi = m_htHash[*pszText];
if (!phi || phi->nStart == HASHITEM_EMPTY || phi->nEnd == HASHITEM_EMPTY)
return(NULL);
ALIAS_MAP *pMap;
CString strAlias;
for (int i=phi->nStart;i<=phi->nEnd && i<m_nCount;i++)
{
pMap = (ALIAS_MAP *)m_ptrList.GetAt(i);
strAlias = pMap->strKey;
if (pMap->bHasVar)
{
CString strTemp;
strTemp.Format(" %%%d",pMap->nVarNum);
strAlias += strTemp;
}
if (strAlias == pszText)
{
m_nGetIndex = i;
return(pMap);
}
}
return(NULL);
}
ALIAS_MAP* CAliasMap::GetFirst()
{
if (!m_nCount)
return(NULL);
m_nGetIndex = 0;
return((ALIAS_MAP *)m_ptrList.GetAt(m_nGetIndex));
}
ALIAS_MAP* CAliasMap::GetNext()
{
if (m_nGetIndex+1 == m_nCount)
return(NULL);
m_nGetIndex++;
return((ALIAS_MAP *)m_ptrList.GetAt(m_nGetIndex));
}
ALIAS_MAP* CAliasMap::GetAt(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(NULL);
return((ALIAS_MAP*)m_ptrList.GetAt(nIndex));
}
void CAliasMap::MapToText(ALIAS_MAP *pMap, CString &strText, BOOL bIncludeGroup)
{
strText = CString("{") + pMap->strKey;
if (pMap->bHasVar)
{
CString strTemp;
strTemp.Format(" %%%d}",pMap->nVarNum);
strText += strTemp;
}
else
strText += "}";
strText += " {";
strText += pMap->strText + "}";
if (bIncludeGroup)
{
strText += " {";
strText += pMap->strGroup;
strText += "}";
}
}
void CAliasMap::MapToCommand(ALIAS_MAP *pMap, CString &strCommand)
{
CString strTemp;
MapToText(pMap,strTemp);
strCommand = CString(_config.chCommand) + CString("alias ") + strTemp;
}
ALIAS_MAP* CAliasMap::FindMatch(const char *pszKey)
{
if (!*pszKey)
return(NULL);
HASHITEM *phi = m_htHash[*pszKey];
if (!phi || phi->nStart == HASHITEM_EMPTY || phi->nEnd == HASHITEM_EMPTY)
return(NULL);
// Separate the key portion and the variable portion.
CString strKey, strVar;
CString strTemp(pszKey);
int nIndex = strTemp.Find(' ');
if (nIndex != -1)
{
strKey = strTemp.Left(nIndex);
strVar = strTemp.Right(strTemp.GetLength()-nIndex-1);
}
else
strKey = strTemp;
ALIAS_MAP *pMap;
for (int i=phi->nStart;i<=phi->nEnd && i<m_nCount;i++)
{
pMap = (ALIAS_MAP *)m_ptrList.GetAt(i);
if (!pMap->bEnabled)
continue;
if (pMap->strKey == strKey)
{
if (pMap->bHasVar)
_strGlobalVars[pMap->nVarNum] = strVar;
m_nGetIndex = i;
return(pMap);
}
}
return(NULL);
}
void CAliasMap::HashIt()
{
ALIAS_MAP *pAlias;
UINT nCurEntry; // Current hash table entry.
UINT nEntry; // Entry of the item being checked.
int i;
int nStart;
int nEnd;
m_htHash.Clear();
nStart = HASHITEM_EMPTY;
nEnd = HASHITEM_EMPTY;
for (i=0;i<m_nCount;i++)
{
pAlias = (ALIAS_MAP *)m_ptrList.GetAt(i);
if (!pAlias)
continue;
nEntry = pAlias->strKey.GetAt(0);
// Will only be this value first time thru.
if (!i)
{
nCurEntry = nEntry;
nStart = i;
}
// If the entry we just found is not equal the to the entry we are working
// with, need to finish off the last entry and start a new one.
if (nEntry != nCurEntry)
{
m_htHash.SetAt(nCurEntry,nStart,nEnd);
nStart = i;
nCurEntry = nEntry;
}
nEnd = i;
}
// Last time thru loop need to add last hash entry.
if (m_nCount)
m_htHash.SetAt(nCurEntry,nStart,nEnd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -