📄 wordfilter.cpp
字号:
// WordFilter.cpp: implementation of the CWordFilter class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
#include "WordFilter.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <io.h>
#include <fcntl.h>
#include <iostream.h>
#include <error.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//CLASS CWordData
CWordData::CWordData()
{
memset(&m_cWord, 0, sizeof(m_cWord));
m_nLength = 0;
m_pNext = NULL;
}
CWordData::~CWordData()
{
}
//CLASS CWordFilter
CWordFilter::CWordFilter()
{
memset(&m_cFileName, 0, sizeof(m_cFileName));
m_nHandle = 0;
m_pSrc = NULL;
m_nBufSize = 0;
m_pWordList = NULL;
return;
}
CWordFilter::~CWordFilter()
{
if (m_pSrc != NULL)
delete [] m_pSrc;
if (m_pWordList != NULL)
DestoryList();
return;
}
int CWordFilter::InitWordFilter(char *pFile)
{
if (pFile == NULL)
return -1;
SetSrcFile(pFile);
if (InitWordList() != 0)
{
cout << "InitWordList() error!" << endl;
return -1;
}
if (!CreateWordTable())
{
cout << "CreateWorTable() error!" << endl;
return -1;
}
return 0;
}
int CWordFilter::InitWordList(void)
{
if (m_pSrc != NULL)
{
delete [] m_pSrc;
m_pSrc = NULL;
}
if (m_nHandle > 0)
{
close(m_nHandle);
m_nHandle = -1;
}
if (strlen(m_cFileName) == 0)
{
cout << "pls select src file first!" <<endl;
return -1;
}
m_nHandle = open((char *)&m_cFileName, O_RDONLY);
if (m_nHandle < 0)
{
cout << "open file error!" << m_cFileName <<endl;
return -1;
}
m_nBufSize = lseek(m_nHandle, 0, SEEK_END);
m_pSrc = new char[m_nBufSize + 1];
if (m_pSrc == NULL)
{
cout << "apply memory error!"<<endl;
close(m_nHandle);
m_nHandle = -1;
return -1;
}
memset(m_pSrc, 0, (m_nBufSize+1));
int nReadLen = 0;
int nLeft = m_nBufSize - nReadLen;
char *pTemp = NULL;
pTemp = m_pSrc;
lseek(m_nHandle, 0, SEEK_SET);
while (nLeft > 0)
{
nReadLen = 0;
if ((nReadLen = read(m_nHandle, pTemp, nLeft)) < 0)
{
if (errno == EINTR)
continue;
cout << "read from file error!"<<endl;
close(m_nHandle);
m_nHandle = -1;
delete [] m_pSrc;
m_pSrc = NULL;
m_nBufSize = 0;
return -1;
}
//if ((nReadLen == 0) && (eof(m_nHandle)))
if (nReadLen == 0)
break;
pTemp += nReadLen;
nLeft -= nReadLen;
//lseek(m_nHandle, (m_nBufSize-nLeft), SEEK_SET);
}
//cout << "src mobile:\n" << m_pSrc << endl;
cout << "InitWordList succeed!"<<endl;
close(m_nHandle);
m_nHandle = -1;
return 0;
}
void CWordFilter::SetSrcFile(char *pFile)
{
strcpy((char *)&m_cFileName, pFile);
return;
}
#ifdef __DEBUG__
int g_nTT = 0;
#endif
bool CWordFilter::CreateWordTable(void)
{
char cWordStr[20];
char *pBase = NULL;
char *pTemp = NULL;
pBase = m_pSrc;
int i = 0;
while (pBase <= (m_pSrc + m_nBufSize))
{
while ((*pBase == ' ') || (*pBase == '\t') ||
(*pBase == '\r') || (*pBase == '\n'))
{
pBase++;
i++;
if (pBase > (m_pSrc + m_nBufSize))
break;
}
if (pBase > (m_pSrc + m_nBufSize))
break;
pTemp = pBase;
while ((*pTemp != ' ') && (*pTemp != '\t') &&
(*pTemp != '\r') && (*pTemp != '\n') &&
(*pTemp!= '\0') )
{
if (pTemp > (m_pSrc + m_nBufSize))
break;
pTemp++;
i++;
}
memset((char *)&cWordStr, 0, sizeof(cWordStr));
if (pBase < pTemp)
{
memcpy(&cWordStr, pBase, (pTemp - pBase));
#ifdef __DEBUG__
g_nTT++;
cout << cWordStr << "\t\t" << g_nTT << endl;
#endif
InsData((char *)&cWordStr, (pTemp - pBase));
}
pBase = pTemp+1;
i++;
}
#ifdef __DEBUG__
cout << "file len = "<< m_nBufSize << "\t i = " << i << endl;
#endif
delete [] m_pSrc;
m_pSrc = NULL;
m_nBufSize = 0;
return true;
}
bool CWordFilter::LocateFilterWord(const char *pBuf, int nBufLen)
{
char *pSrc = NULL;
char *pMvPoint = NULL;
char *pTemp = NULL;
CWordData *pData = NULL;
pSrc = (char *)pBuf;
for (int i=0; i<nBufLen; i++, pSrc++)
{
pData = m_pWordList;
while (pData != NULL)
{
pTemp = pData->m_cWord;
pMvPoint = pSrc;
int n = 0;
for (n=0; n<pData->m_nLength; n++)
{
if (*pTemp == *pMvPoint)
{
pTemp++;
pMvPoint++;
continue;
}
break;
}
if (n == pData->m_nLength)
{
cout << "MUST FILTER WORLD:" << pData->m_cWord <<endl;
return true;
}
pData = pData->m_pNext;
}
}
return false;
}
bool CWordFilter::ReplaceFilterWord(char *pBuf, int nBufLen)
{
char *pSrc = NULL;
char *pMvPoint = NULL;
char *pTemp = NULL;
CWordData *pData = NULL;
bool bExist = false;
pSrc = (char *)pBuf;
for (int i=0; i<nBufLen; i++, pSrc++)
{
pData = m_pWordList;
while (pData != NULL)
{
pTemp = pData->m_cWord;
pMvPoint = pSrc;
int n = 0;
for (n=0; n<pData->m_nLength; n++)
{
if (*pTemp == *pMvPoint)
{
pTemp++;
pMvPoint++;
continue;
}
break;
}
if (n == pData->m_nLength)
{
cout << "MUST FILTER WORLD:" << pData->m_cWord <<endl;
bExist = true;
for (int t=1; t<=n; t++)
{
*(pMvPoint - t) = '*';
}
continue;
//return true;
}
pData = pData->m_pNext;
}
}
return bExist;
}
void CWordFilter::DestoryList(void)
{
if (m_pWordList == NULL)
return;
CWordData *pBase = NULL;
CWordData *pNext = NULL;
pBase = m_pWordList;
while ( pBase->m_pNext != NULL)
{
pNext = pBase->m_pNext;
delete pBase;
pBase = pNext;
}
delete pBase;
return;
}
void CWordFilter::InsData( char *pWord, int nLen)
{
if (m_pWordList == NULL)
{
m_pWordList = new CWordData();
strncpy((char *)&m_pWordList->m_cWord, pWord, nLen);
m_pWordList->m_nLength = nLen;
m_pWordList->m_pNext = NULL;
}
else
{
CWordData *pBase = NULL;
CWordData *pTemp = NULL;
pBase = m_pWordList;
while (pBase->m_pNext != NULL)
{
//have exist in the list
int nMaxLen = strlen((char *)&pBase->m_cWord);
nMaxLen = (nMaxLen >= nLen) ? nMaxLen : nLen;
if (strncmp((char *)&pBase->m_cWord, pWord, nMaxLen) == 0)
return;
pBase = pBase->m_pNext;
}
pTemp = new CWordData();
strncpy((char *)&pTemp->m_cWord, pWord, nLen);
pTemp->m_nLength = nLen;
pTemp->m_pNext = NULL;
pBase->m_pNext = pTemp;
}
return;
}
bool CWordFilter::DelData(char *pWord, int nLen)
{
CWordData *pBase = NULL;
CWordData *pTemp = NULL;
pBase = m_pWordList;
//when del in the head
int nMaxLen = strlen((char *)&pBase->m_cWord);
nMaxLen = (nMaxLen >= nLen) ? nMaxLen : nLen;
if (strncmp((char *)&pBase->m_cWord, pWord, nMaxLen) == 0)
{
m_pWordList = pBase->m_pNext;
delete pBase;
return true;
}
while (pBase->m_pNext != NULL)
{
//have exist in the list
nMaxLen = strlen((char *)&pBase->m_pNext->m_cWord);
nMaxLen = (nMaxLen >= nLen) ? nMaxLen : nLen;
if (strncmp((char *)&pBase->m_pNext->m_cWord, pWord, nMaxLen) == 0)
{
pTemp = pBase->m_pNext;
pBase->m_pNext = pBase->m_pNext->m_pNext;
delete pTemp;
return true;
}
pBase = pBase->m_pNext;
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -