📄 nfasearcherview.cpp
字号:
// NFASearcherView.cpp : implementation of the CNFASearcherView class
//
#include "stdafx.h"
#include "NFASearcher.h"
#include "NFASearcherDoc.h"
#include "NFASearcherView.h"
#include "InputDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNFASearcherView
IMPLEMENT_DYNCREATE(CNFASearcherView, CView)
BEGIN_MESSAGE_MAP(CNFASearcherView, CView)
//{{AFX_MSG_MAP(CNFASearcherView)
ON_COMMAND(ID_OPEN, OnOpen)
ON_COMMAND(ID_SEARCH, OnSearch)
ON_UPDATE_COMMAND_UI(ID_SEARCH, OnUpdateSearch)
ON_COMMAND(ID_INPUT, OnInput)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNFASearcherView construction/destruction
CNFASearcherView::CNFASearcherView()
{
// TODO: add construction code here
m_bInput = FALSE;
m_bOpen = FALSE;
m_strFileName = _T("");
m_strInput = _T("");
m_QState = 0;
m_nFound = 0;
}
CNFASearcherView::~CNFASearcherView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CNFASearcherView drawing
void CNFASearcherView::OnDraw(CDC* pDC)
{
CNFASearcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CFont* pFont = new CFont;
pFont->CreateFont(20, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
CFont* newFont = new CFont;
newFont->CreateFont(20, 10, 0, 0, FW_NORMAL, TRUE, TRUE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
CFont* oldFont = pDC->SelectObject(pFont);
if(m_bOpen == FALSE)
pDC->TextOut(100, 100, "没有指定搜索文件");
else
{
CString strFileName = _T("你已经指定的搜索文件是 ");
pDC->TextOut(100, 100, strFileName);
CFont* o1Font = pDC->SelectObject(newFont);
pDC->TextOut(230, 120, m_strFileName);
pDC->SelectObject(o1Font);
}
if(m_bInput == FALSE)
pDC->TextOut(100, 150, "没有指定搜索字符串!");
else
{
CString strInput = _T("你已经指定的搜索字符串是 ");
pDC->TextOut(100, 150, strInput);
CFont* o2Font = pDC->SelectObject(newFont);
pDC->TextOut(230, 170, m_strInput);
pDC->SelectObject(o2Font);
}
if(m_nFound == 1)
{
CString strRecord = _T("字符串在文件中的位置为: ");
CString str1;
CString str2;
for(int i=0; i<100; i++)
{
if(m_nRecord[i] == 0)
break;
str1.Format("%d",m_nRecord[i]);
str1 += _T(" ");
str2 += str1;
}
pDC->TextOut(100, 200, strRecord);
CFont* o3Font = pDC->SelectObject(newFont);
pDC->TextOut(230, 220, str2);
pDC->SelectObject(o3Font);
}
else if(m_nFound == -1)
{
pDC->TextOut(100, 200, "没有在文件中发现字符串");
}
pDC->SelectObject(oldFont);
}
/////////////////////////////////////////////////////////////////////////////
// CNFASearcherView diagnostics
#ifdef _DEBUG
void CNFASearcherView::AssertValid() const
{
CView::AssertValid();
}
void CNFASearcherView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CNFASearcherDoc* CNFASearcherView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNFASearcherDoc)));
return (CNFASearcherDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNFASearcherView message handlers
void CNFASearcherView::OnOpen()
{
// TODO: Add your command handler code here
CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_HIDEREADONLY, "All Files(*.txt)|*.txt||", NULL);
if( FileDialog.DoModal() == IDOK)
{
m_strFileName = FileDialog.GetPathName();
m_bOpen = TRUE;
Invalidate();
}
}
void CNFASearcherView::OnSearch()
{
// TODO: Add your command handler code here
char buffer[1];
CFile file;
int flag = 0, pos =0;
m_QState = -1;
m_nFound = -1;
BOOL bStart = FALSE;
int nLength = m_strInput.GetLength();
if(file.Open(m_strFileName,CFile::modeRead) == FALSE)
AfxMessageBox("打开文件出错");
file.SeekToBegin();
TRACE("Start");
for(;;)
{
if(file.Read(buffer, 1) == 0)
break;
if( (!bStart) && (buffer[0] == m_strInput.GetAt(0)) ) //如果开始第一位匹配
{
bStart = TRUE; m_QState = 0; flag = 1;
}
else if( bStart ) //如果已经开始匹配
{
if(buffer[0] == m_strInput.GetAt(flag))
{
flag++; m_QState++;
}
else if(buffer[0] == m_strInput.GetAt(0))
{
flag = 1; m_QState = 0; //回到初始状态
}
else if(buffer[0] != m_strInput.GetAt(0))
{
flag = 0; m_QState = -1; bStart = FALSE;
}
}
// TRACE("nLength = %d\n", nLength);
// TRACE("m_QState = %d\n", m_QState);
if(m_QState == nLength - 1)
{
flag =0; m_QState = -1; bStart = FALSE;
m_nRecord[pos] = file.GetPosition() - nLength + 1;
pos++;
m_nFound = 1;
// TRACE("m_nRecord[pos] = %d\n", m_nRecord[pos]);
}
}
Invalidate();
}
void CNFASearcherView::OnUpdateSearch(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if( (m_bInput == FALSE) || (m_bOpen == FALSE) )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
void CNFASearcherView::OnInput()
{
// TODO: Add your command handler code here
CInputDlg InputDlg;
if( InputDlg.DoModal() == IDOK )
{
m_strInput = InputDlg.m_strInput;
m_bInput = TRUE;
Invalidate();
}
}
void CNFASearcherView::OnInitialUpdate()
{
CView::OnInitialUpdate();
for(int i=0; i<100; i++)
m_nRecord[i] = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -