245 lines
4.1 KiB
C++
245 lines
4.1 KiB
C++
#include "stdafx.h"
|
|
#include "FindParser.h"
|
|
|
|
#if (ADS < 16)
|
|
class __declspec(dllexport) CString;
|
|
#endif;
|
|
|
|
//#include <acutads.h>
|
|
//#include <ctype.h>
|
|
//#include <WinNls.h>
|
|
|
|
// Zyu add on 101013,根据搜索选项匹配字符串
|
|
XFindParser::XFindParser()
|
|
{
|
|
m_dwMask = FP_DEFAULT;
|
|
}
|
|
|
|
XFindParser::~XFindParser()
|
|
{
|
|
}
|
|
|
|
// 查找选项的Set/Get函数
|
|
DWORD XFindParser::GetFindOption() const
|
|
{
|
|
return m_dwMask;
|
|
}
|
|
|
|
void XFindParser::SetFindOption(DWORD dwMask)
|
|
{
|
|
m_dwMask = dwMask;
|
|
}
|
|
|
|
// 获得目标字符串
|
|
const CString& XFindParser::GetResult() const
|
|
{
|
|
return m_sResult;
|
|
}
|
|
|
|
// 获得源字符串
|
|
const CString& XFindParser::GetSrcString() const
|
|
{
|
|
return m_sSrc;
|
|
}
|
|
|
|
// 获得目标字符串
|
|
const CString& XFindParser::GetDestString() const
|
|
{
|
|
return m_sDest;
|
|
}
|
|
|
|
// 搜索字符串
|
|
bool XFindParser::Find(LPCTSTR pszSrc, LPCTSTR pszDest, int &iPos)
|
|
{
|
|
// 保存源和目标字符串
|
|
m_sSrc = pszSrc;
|
|
m_sDest = pszDest;
|
|
|
|
// 临时的源和目标字符串
|
|
CString sSrc = m_sSrc;
|
|
CString sDest = m_sDest;
|
|
// 临时的搜索选项
|
|
DWORD dwMaskBak = m_dwMask;
|
|
|
|
// 如果源串为空,则返回整个被搜索的串
|
|
if (sSrc.IsEmpty())
|
|
{
|
|
sSrc = sDest;
|
|
dwMaskBak = FP_DEFAULT;
|
|
}
|
|
|
|
// 目标串长度
|
|
int iLen = sDest.GetLength();
|
|
// 搜索得到的位置
|
|
int iLoc = -1;
|
|
|
|
// 不区分大小写
|
|
if (!(dwMaskBak & FP_MATCH_CASE))
|
|
{
|
|
sSrc.MakeLower();
|
|
sDest.MakeLower();
|
|
}
|
|
|
|
// 不区分半/全角
|
|
if (!(dwMaskBak & FP_WORD_WIDTH))
|
|
{
|
|
MapString(sDest, LCMAP_HALFWIDTH);
|
|
MapString(sSrc, LCMAP_HALFWIDTH);
|
|
}
|
|
|
|
// 增量搜索
|
|
if (dwMaskBak & FP_INCREMENT)
|
|
{
|
|
CString sDestSym;
|
|
CString sDestNum;
|
|
DivideString(sDest, sDestSym, sDestNum);
|
|
CString sSrcSym;
|
|
CString sSrcNum;
|
|
DivideString(sSrc, sSrcSym, sSrcNum);
|
|
|
|
// 如果字符部分相同,数字部分目标串大于源串,则认为查找成功
|
|
if ((sDestSym == sSrcSym) && ( _tstof(sDestNum) - _tstof(sSrcNum) >= 0))
|
|
{
|
|
sSrc = sDest;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 通配符搜索
|
|
if (dwMaskBak & FP_WILDCARD)
|
|
{
|
|
// 查找起始位置
|
|
int iStart = (iPos == -1 ? 0 : iPos);
|
|
|
|
// 先对整个串进行一次匹配
|
|
if (acutWcMatch(sDest.Right(iLen - iStart), sSrc) == RTNORM)
|
|
{
|
|
sSrc = sDest.Right(iLen - iStart);
|
|
iLoc = iStart;
|
|
}
|
|
// 如果不能匹配,再对子串匹配
|
|
else
|
|
{
|
|
for (int i = iStart; i < iLen; i++)
|
|
{
|
|
for (int j = i; j < iLen; j++)
|
|
{
|
|
if (acutWcMatch(sDest.Mid(i, j - i + 1), sSrc) == RTNORM)
|
|
{
|
|
sSrc = sDest.Mid(i, j - i + 1);
|
|
iLoc = i;
|
|
i = j = iLen;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 进行一次普通查找
|
|
else
|
|
{
|
|
// 查找起始位置
|
|
int iStart = (iPos == -1 ? 0 : iPos);
|
|
iLoc = sDest.Find(sSrc, iStart);
|
|
}
|
|
|
|
// 全字匹配
|
|
if (dwMaskBak & FP_WHOLE_WORD)
|
|
{
|
|
if ((iPos <= 0) && (sDest == sSrc))
|
|
{
|
|
iLoc = 0;
|
|
}
|
|
else
|
|
{
|
|
iLoc = -1;
|
|
}
|
|
}
|
|
|
|
// 查找成功,保存结果和查找到的位置
|
|
if (-1 != iLoc)
|
|
{
|
|
iPos = iLoc;
|
|
m_sResult = m_sDest.Mid(iPos, sSrc.GetLength());
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 将一个字符串分成字符和数字,用于增量查找
|
|
bool XFindParser::DivideString(LPCTSTR pszDest, CString &sSymble, CString &sNumber) const
|
|
{
|
|
CString sTemp(pszDest);
|
|
int iLen = sTemp.GetLength();
|
|
int iStart = 0; // 分割位置
|
|
int iDotPos = -1; // 小数点的位置,-1为小数点不存在
|
|
TCHAR chCheck = 0; // 被检测的字符
|
|
|
|
for (int i = 0; i < iLen; i++)
|
|
{
|
|
chCheck = sTemp.GetAt(i);
|
|
// 当前是非数字
|
|
if (!isdigit(chCheck))
|
|
{
|
|
// 是一个小数点
|
|
if (chCheck == _T('.'))
|
|
{
|
|
// 之前未出现小数点
|
|
if (iDotPos == -1)
|
|
{
|
|
if (i == iStart)
|
|
{
|
|
iStart = i + 1;
|
|
}
|
|
else
|
|
{
|
|
iDotPos = i;
|
|
}
|
|
}
|
|
// 之前出现过小数点
|
|
else
|
|
{
|
|
if (i - iDotPos == 1)
|
|
{
|
|
iStart = i + 1;
|
|
iDotPos = -1;
|
|
}
|
|
else
|
|
{
|
|
iStart = iDotPos + 1;
|
|
iDotPos = i;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
iStart = i + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
sSymble = sTemp.Left(iStart);
|
|
sNumber = sTemp.Right(iLen - iStart);
|
|
|
|
return true;
|
|
}
|
|
|
|
// 映射字符串,参数参照LCMapString函数,用于将字符串转为半角
|
|
bool XFindParser::MapString(CString &sDest, DWORD dwMapFlags) const
|
|
{
|
|
CString sTemp;
|
|
int iLength = sDest.GetLength() + 1;
|
|
int iRet = ::LCMapString(LANG_SYSTEM_DEFAULT, dwMapFlags, sDest, iLength, sTemp.GetBuffer(iLength), iLength);
|
|
sTemp.ReleaseBuffer();
|
|
if (iRet != 0)
|
|
{
|
|
sDest = sTemp;
|
|
}
|
|
return (iRet != 0);
|
|
}
|