home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
pserv.cpl
/
pserv-2.4.exe
/
source
/
FindDialog.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-01-05
|
8KB
|
343 lines
// FindDialog.cpp : implementation file
//
#include "stdafx.h"
#include "pserv2.h"
#include "CListViewTools.h"
#include "FindDialog.h"
#include "CConfiguration.h"
#include "RemoteConnectDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CSearchContext::CSearchContext()
: m_bChanged(FALSE),
m_bSearchUp(FALSE),
m_bCaseSensitive(FALSE),
m_bWholeWordsOnly(FALSE),
m_nLastFoundIndex(-1),
m_pEntries( NULL ),
m_bHighlightAllResults(FALSE)
{
}
int CSearchContext::FindNextIndices(CListViewEntries* pEntries, CUIntArray& results)
{
m_pEntries = pEntries;
ASSERT( results.GetSize() == 0 );
if( m_bSearchUp )
{
return FindNextIndicesUp(&m_pEntries->m_Entries,results);
}
else
{
return FindNextIndicesDown(&m_pEntries->m_Entries,results);
}
}
int CSearchContext::FindNextIndicesUp(CObArray* pServices, CUIntArray& results)
{
int nIndex = m_nLastFoundIndex-1;
int nMinIndex = 0;
// loop through elements in lower half
while( nIndex >= nMinIndex )
{
if( !ContinueSearchLoop(pServices, nIndex--, results) )
{
return results.GetSize();
}
}
// not found, search from the bottom
nIndex = pServices->GetSize()-1;
while( nIndex >= m_nLastFoundIndex )
{
if( !ContinueSearchLoop(pServices, nIndex--, results) )
{
return results.GetSize();
}
}
return results.GetSize();
}
BOOL FindCaseSensitiveSubstring(LPCTSTR lpszString, LPCTSTR lpszFind)
{
int nLenToFind = lstrlen(lpszFind);
int nLenOfString = lstrlen(lpszString);
int nIndex = 0;
int nMaxIndex = nLenOfString - nLenToFind;
while( nIndex <= nMaxIndex )
{
if( tstrncmp(lpszString + nIndex, lpszFind, nLenToFind) == 0 )
{
return TRUE;
}
nIndex++;
}
return FALSE;
}
BOOL FindSubstringIgnoreCase(LPCTSTR lpszString, LPCTSTR lpszFind)
{
int nLenToFind = lstrlen(lpszFind);
int nLenOfString = lstrlen(lpszString);
int nIndex = 0;
int nMaxIndex = nLenOfString - nLenToFind;
while( nIndex <= nMaxIndex )
{
if( tstrnicmp(lpszString + nIndex, lpszFind, nLenToFind) == 0 )
{
return TRUE;
}
nIndex++;
}
return FALSE;
}
class CWholeWords
{
public:
CWholeWords(LPCTSTR lpszString);
virtual ~CWholeWords();
LPCTSTR GetNextWord();
protected:
LPTSTR m_lpszCopyOfString;
LPTSTR m_lpszTempString;
int m_nReadPos;
};
CWholeWords::CWholeWords(LPCTSTR lpszString)
{
m_lpszCopyOfString = tstrdup(lpszString);
m_lpszTempString = NULL;
m_nReadPos = 0;
}
#define IsWordChar(c) ( \
( (c >= 'A') && (c <= 'Z') ) || \
( (c >= 'a') && (c <= 'z') ) || \
( (c >= '0') && (c <= '9') ) || \
( (c >= 0x7F ) ) || \
( (c == '_' ) ) )
LPCTSTR CWholeWords::GetNextWord()
{
delete m_lpszTempString;
m_lpszTempString = NULL;
int nStartOfString = -1;
while( true )
{
TCHAR c = m_lpszCopyOfString[m_nReadPos];
if( IsWordChar(c) )
{
if( nStartOfString < 0 )
{
nStartOfString = m_nReadPos;
}
}
else
{
if( nStartOfString >= 0 )
{
// end of string reached, copy substring
int len = m_nReadPos-nStartOfString;
m_lpszTempString = new TCHAR[len+1];
if( m_lpszTempString )
{
tstrncpy( m_lpszTempString, m_lpszCopyOfString+nStartOfString, len );
m_lpszTempString[len] = 0;
return m_lpszTempString;
}
}
if( !c )
{
return NULL;
}
}
m_nReadPos++;
}
}
CWholeWords::~CWholeWords()
{
free( m_lpszCopyOfString );
delete m_lpszTempString;
}
BOOL FindCaseSensitiveSubstringWholeWords(LPCTSTR lpszString, LPCTSTR lpszFind)
{
CWholeWords words(lpszString);
while( true )
{
LPCTSTR word = words.GetNextWord();
if( word )
{
if( lstrcmp(word, lpszFind) == 0 )
{
return TRUE;
}
}
else
{
return FALSE;
}
}
}
BOOL FindSubstringIgnoreCaseWholeWords(LPCTSTR lpszString, LPCTSTR lpszFind)
{
CWholeWords words(lpszString);
while( true )
{
LPCTSTR word = words.GetNextWord();
if( word )
{
if( lstrcmpi(word, lpszFind) == 0 )
{
return TRUE;
}
}
else
{
return FALSE;
}
}
}
BOOL CSearchContext::ContinueSearchLoop( CObArray* pServices, int nIndex, CUIntArray& results)
{
CListViewEntry* pService = (CListViewEntry*) pServices->GetAt(nIndex);
if( m_pEntries->Matches(pService,m_lpfnFindSubstring, m_strFind) )
{
results.Add(nIndex);
m_nLastFoundIndex = nIndex;
return m_bHighlightAllResults;
}
return TRUE;
}
int CSearchContext::FindNextIndicesDown(CObArray* pServices, CUIntArray& results)
{
int nIndex = m_nLastFoundIndex+1;
int nMaxIndex = pServices->GetSize();
// loop through elements in lower half
while( nIndex < nMaxIndex )
{
if( !ContinueSearchLoop(pServices, nIndex++, results) )
{
return results.GetSize();
}
}
// not found, search from the top
nIndex = 0;
while( nIndex <= m_nLastFoundIndex )
{
if( !ContinueSearchLoop(pServices, nIndex++, results) )
{
return results.GetSize();
}
}
return results.GetSize();
}
BOOL CSearchContext::IsValid()
{
return !IsEmptyString(m_strFind);
}
void CSearchContext::Refresh(int nIndex)
{
m_nLastFoundIndex = nIndex;
m_bCaseSensitive = theConfiguration.m_bSearchCaseSensitive;
m_bWholeWordsOnly = theConfiguration.m_bSearchWholeWordsOnly;
m_bHighlightAllResults = theConfiguration.m_bSearchHighlightAllResults;
m_bSearchUp = theConfiguration.m_bSearchUp;
m_strFind = theConfiguration.m_FindStrings.GetAt(0);
if( m_bWholeWordsOnly )
{
if( m_bCaseSensitive )
{
m_lpfnFindSubstring = FindCaseSensitiveSubstringWholeWords;
}
else
{
m_lpfnFindSubstring = FindSubstringIgnoreCaseWholeWords;
}
}
else
{
if( m_bCaseSensitive )
{
m_lpfnFindSubstring = FindCaseSensitiveSubstring;
}
else
{
m_lpfnFindSubstring = FindSubstringIgnoreCase;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CFindDialog dialog
CFindDialog::CFindDialog(CWnd* pParent /*=NULL*/)
: CDialog(CFindDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CFindDialog)
//}}AFX_DATA_INIT
}
void CFindDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFindDialog)
DDX_Control(pDX, IDC_FIND_STRING, m_ecFindString);
DDX_Control(pDX, IDC_SEARCH_UP, m_btSearchUp);
DDX_Control(pDX, IDC_SEARCH_DOWN, m_btSearchDown);
//}}AFX_DATA_MAP
DDX_Check(pDX, IDC_CASE_SENSITIVE, theConfiguration.m_bSearchCaseSensitive);
DDX_Check(pDX, IDC_WHOLE_WORDS_ONLY, theConfiguration.m_bSearchWholeWordsOnly);
DDX_Check(pDX, IDC_SELECT_ALL_RESULTS, theConfiguration.m_bSearchHighlightAllResults);
if( pDX->m_bSaveAndValidate )
{
theConfiguration.m_bSearchUp = (m_btSearchUp.GetCheck() == BST_CHECKED);
}
else
{
if( theConfiguration.m_bSearchUp )
m_btSearchUp.SetCheck(BST_CHECKED);
else
m_btSearchDown.SetCheck(BST_CHECKED);
}
DDX_ExchangeComboArray(pDX, m_ecFindString, theConfiguration.m_FindStrings );
}
BEGIN_MESSAGE_MAP(CFindDialog, CDialog)
//{{AFX_MSG_MAP(CFindDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()