📄 stringdemoview.cpp
字号:
// StringDemoView.cpp : implementation of
// the CStringDemoView class
//
#include "stdafx.h"
#include "StringDemo.h"
#include "StringDemoDoc.h"
#include "StringDemoView.h"
#include "GetWord.h"
#include "SearchReplace.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStringDemoView
IMPLEMENT_DYNCREATE(CStringDemoView, CView)
BEGIN_MESSAGE_MAP(CStringDemoView, CView)
//{{AFX_MSG_MAP(CStringDemoView)
ON_COMMAND(ID_FILE_PERFORMWORDCOUNT, OnFilePerformwordcount)
ON_COMMAND(ID_FILE_RESTOREORIGINALFILEDATA, OnFileRestoreoriginalfiledata)
ON_COMMAND(ID_FILE_SEARCHANDREPLACE, OnFileSearchandreplace)
ON_COMMAND(ID_FILE_VIEWALTERATIONSINNOTEPAD, OnFileViewalterationsinnotepad)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStringDemoView construction/destruction
CStringDemoView::CStringDemoView()
{
// Attempt to open the data file.
CFile cf;
if( cf.Open( "AESOP.txt", CFile::modeRead ) ){
// Get the file length.
int nFileLength = cf.GetLength();
// Allocate a temporary buffer.
char *lpBuffer = new char [nFileLength+1];
try{
// Read the data.
cf.Read( lpBuffer, nFileLength );
}
// Catch a Read exception if it's
// thrown.
catch( CFileException *e ){
e->Delete();
delete [] lpBuffer;
return;
}
// NULL terminate the string.
lpBuffer[nFileLength] = 0;
// Set the m_strOriginalText and
// m_strAlteredText CString objects.
m_strOriginalText = lpBuffer;
m_strAlteredText = m_strOriginalText;
// Free the temporary buffer.
delete [] lpBuffer;
}
}
CStringDemoView::~CStringDemoView()
{
}
BOOL CStringDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CStringDemoView drawing
void CStringDemoView::OnDraw(CDC* pDC)
{
CStringDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Let users know if the file was
// loaded or not.
if( m_strOriginalText.GetLength() > 0 )
pDC->TextOut( 10, 10, "AESOP.txt loaded." );
else
pDC->TextOut( 10, 10, "AESOP.txt not loaded." );
}
/////////////////////////////////////////////////////////////////////////////
// CStringDemoView diagnostics
#ifdef _DEBUG
void CStringDemoView::AssertValid() const
{
CView::AssertValid();
}
void CStringDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CStringDemoDoc* CStringDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStringDemoDoc)));
return (CStringDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStringDemoView message handlers
void CStringDemoView::OnFilePerformwordcount()
{
CGetWord GetWord;
// Get the word in a dialog box.
if( GetWord.DoModal() == IDOK ){
// Set the count to 0.
int nCount = 0;
int nIndex;
CString strTempString;
// Set a temporary CString object
// to contain the altered text.
strTempString = m_strAlteredText;
// Set a temporary string to
// contain the word for which
// we're searching.
CString strTempWord =
GetWord.m_strWordToCount;
// If we're not making a case sensitive
// search, make both strings upper case.
if( !GetWord.m_bCaseSensitive ){
strTempWord.MakeUpper();
strTempString.MakeUpper();
}
do{
// Attempt to find the word.
nIndex =
strTempString.Find( strTempWord );
// If nIndex != -1, we found the word.
if( nIndex != -1 ){
// Make sure the character before the
// word is not an alphanumeric character.
// If it is, the word we found is
// embedded in something else.
if( nIndex == 0 ||
!isalnum( strTempString.GetAt( nIndex - 1 ) )){
// Make sure the character after the
// word is not an alphanumeric character.
// If it is, the word we found is
// embedded in something else.
if( !isalnum( strTempString.GetAt(
nIndex + strTempWord.GetLength() ) ) )
// Increment the count.
nCount++;
}
// Shorten the string containing the
// body of data.
strTempString =
strTempString.Right(
strTempString.GetLength() -
strTempWord.GetLength() -
nIndex );
}
} while( nIndex != -1 );
// Format a string and tell users
// how many matches were found.
CString strInfo;
strInfo.Format( "The count was %d.", nCount );
AfxMessageBox( strInfo );
}
}
void CStringDemoView::OnFileRestoreoriginalfiledata()
{
// Restore the working text string
// with the original text string.
m_strAlteredText = m_strOriginalText;
}
void CStringDemoView::OnFileSearchandreplace()
{
CSearchReplace SearchReplace;
// Get the search and replace word.
if( SearchReplace.DoModal() == IDOK ){
// See if both strings are identical.
if( SearchReplace.m_strWordToFind ==
SearchReplace.m_strWordToReplace ){
AfxMessageBox( "Those two strings are identical!");
return;
}
// Set the count to 0.
int nCount = 0;
int nIndex;
CString strTempString;
// Set a temporary CString object
// to contain the altered text.
strTempString = m_strAlteredText;
// Set a temporary string to
// contain the word for which
// we're searching.
CString strTempWord =
SearchReplace.m_strWordToFind ;
// If we're not making a case sensitive
// search, make both strings upper case.
if( !SearchReplace.m_bCaseSensitive ){
strTempWord.MakeUpper();
strTempString.MakeUpper();
}
do{
BOOL bReplaced = FALSE;
// Attempt to find the word.
nIndex =
strTempString.Find( strTempWord );
// If nIndex != -1, we found the word.
if( nIndex != -1 ){
// Make sure the character before the
// word is not an alphanumeric character.
// If it is, the word we found is
// embedded in something else.
if( nIndex == 0 ||
!isalnum( strTempString.GetAt( nIndex - 1 ) )){
// Make sure the character after the
// word is not an alphanumeric character.
// If it is, the word we found is
// embedded in something else.
if( !isalnum( strTempString.GetAt(
nIndex + strTempWord.GetLength() ) ) ){
// Increment the count.
nCount++;
// Calculate the number of
// characters to the right
// of the word that was found.
int nRightLength =
strTempString.GetLength() -
nIndex -
strTempWord.GetLength();
// Calculate the number of
// characters to the left
// of the word that was found.
int nLeftLength =
m_strAlteredText.GetLength() -
nRightLength -
strTempWord.GetLength();
// Form a left and right string.
CString strLeft, strRight;
strLeft =
m_strAlteredText.Left( nLeftLength );
strRight =
m_strAlteredText.Right( nRightLength );
// Put the altered text string
// together.
m_strAlteredText = strLeft +
SearchReplace.m_strWordToReplace +
strRight;
}
}
// Shorten the temporary string.
strTempString =
strTempString.Right(
strTempString.GetLength() -
strTempWord.GetLength() -
nIndex );
}
} while( nIndex != -1 );
// Format a string and tell users
// how many replacements were made.
CString strInfo;
strInfo.Format( "%d replacements made.", nCount );
AfxMessageBox( strInfo );
}
}
void CStringDemoView::OnFileViewalterationsinnotepad()
{
CFile Out;
// Attempt to create a temporary file
// which will be loaded into wordpad.
if( Out.Open( "StringDemoTempFile.txt",
CFile::modeCreate | CFile::modeWrite ) ){
try{
// Write the data.
Out.Write( m_strAlteredText,
m_strAlteredText.GetLength() );
}
// Catch a write exception if it's thrown.
catch( CFileException *e ){
e->Delete();
return;
}
// Close the file so that wordpad
// won't get a sharing violation.
Out.Close();
// Use WinExec() to bring up wordpad.
WinExec( "WRITE.EXE StringDemoTempFile.txt",
SW_NORMAL );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -