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 >
C/C++ Source or Header  |  2005-01-05  |  8KB  |  343 lines

  1. // FindDialog.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pserv2.h"
  6. #include "CListViewTools.h"
  7. #include "FindDialog.h"
  8. #include "CConfiguration.h"
  9. #include "RemoteConnectDialog.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. CSearchContext::CSearchContext()
  18.     :   m_bChanged(FALSE),
  19.         m_bSearchUp(FALSE),
  20.         m_bCaseSensitive(FALSE),
  21.         m_bWholeWordsOnly(FALSE),
  22.         m_nLastFoundIndex(-1),
  23.         m_pEntries( NULL ),
  24.         m_bHighlightAllResults(FALSE)
  25. {
  26. }
  27.  
  28. int CSearchContext::FindNextIndices(CListViewEntries* pEntries, CUIntArray& results)
  29. {
  30.     m_pEntries = pEntries;
  31.  
  32.     ASSERT( results.GetSize() == 0 );
  33.     if( m_bSearchUp )
  34.     {
  35.         return FindNextIndicesUp(&m_pEntries->m_Entries,results);
  36.     }
  37.     else
  38.     {
  39.         return FindNextIndicesDown(&m_pEntries->m_Entries,results);
  40.     }
  41. }
  42.  
  43. int CSearchContext::FindNextIndicesUp(CObArray* pServices, CUIntArray& results)
  44. {
  45.     int nIndex = m_nLastFoundIndex-1;
  46.     int nMinIndex = 0;
  47.  
  48.     // loop through elements in lower half
  49.     while( nIndex >= nMinIndex )
  50.     {
  51.         if( !ContinueSearchLoop(pServices, nIndex--, results) )
  52.         {
  53.             return results.GetSize();
  54.         }
  55.     }
  56.  
  57.     // not found, search from the bottom
  58.     nIndex = pServices->GetSize()-1;
  59.     while( nIndex >= m_nLastFoundIndex )
  60.     {
  61.         if( !ContinueSearchLoop(pServices, nIndex--, results) )
  62.         {
  63.             return results.GetSize();
  64.         }
  65.     }
  66.     
  67.     return results.GetSize();
  68. }
  69.  
  70.  
  71.  
  72. BOOL FindCaseSensitiveSubstring(LPCTSTR lpszString, LPCTSTR lpszFind)
  73. {
  74.     int nLenToFind = lstrlen(lpszFind);
  75.     int nLenOfString = lstrlen(lpszString);
  76.     int nIndex = 0;
  77.     int nMaxIndex = nLenOfString - nLenToFind;
  78.     while( nIndex <= nMaxIndex )
  79.     {
  80.         if( tstrncmp(lpszString + nIndex, lpszFind, nLenToFind) == 0 )
  81.         {
  82.             return TRUE;
  83.         }
  84.         nIndex++;
  85.     }
  86.     return FALSE;
  87. }
  88.  
  89. BOOL FindSubstringIgnoreCase(LPCTSTR lpszString, LPCTSTR lpszFind)
  90. {
  91.     int nLenToFind = lstrlen(lpszFind);
  92.     int nLenOfString = lstrlen(lpszString);
  93.     int nIndex = 0;
  94.     int nMaxIndex = nLenOfString - nLenToFind;
  95.     while( nIndex <= nMaxIndex )
  96.     {
  97.         if( tstrnicmp(lpszString + nIndex, lpszFind, nLenToFind) == 0 )
  98.         {
  99.             return TRUE;
  100.         }
  101.         nIndex++;
  102.     }
  103.     return FALSE;
  104. }
  105.  
  106. class CWholeWords
  107.     {
  108.     public:
  109.         CWholeWords(LPCTSTR lpszString);
  110.         virtual ~CWholeWords();
  111.         LPCTSTR GetNextWord();
  112.  
  113.     protected:
  114.         LPTSTR m_lpszCopyOfString;
  115.         LPTSTR m_lpszTempString;
  116.         int m_nReadPos;
  117.     };
  118.  
  119. CWholeWords::CWholeWords(LPCTSTR lpszString)
  120. {
  121.     m_lpszCopyOfString = tstrdup(lpszString);
  122.     m_lpszTempString = NULL;
  123.     m_nReadPos = 0;
  124. }
  125.  
  126. #define IsWordChar(c) (             \
  127.     ( (c >= 'A') && (c <= 'Z') ) || \
  128.     ( (c >= 'a') && (c <= 'z') ) || \
  129.     ( (c >= '0') && (c <= '9') ) || \
  130.     ( (c >= 0x7F ) ) ||             \
  131.     ( (c == '_' ) ) )
  132.  
  133. LPCTSTR CWholeWords::GetNextWord()
  134. {
  135.     delete m_lpszTempString;
  136.     m_lpszTempString = NULL; 
  137.     int nStartOfString = -1;
  138.     while( true )
  139.     {
  140.         TCHAR c = m_lpszCopyOfString[m_nReadPos];
  141.         if( IsWordChar(c) )
  142.         {
  143.             if( nStartOfString < 0 )
  144.             {
  145.                 nStartOfString = m_nReadPos;
  146.             }
  147.         }
  148.         else
  149.         {
  150.             if( nStartOfString >= 0 )
  151.             {
  152.                 // end of string reached, copy substring
  153.                 int len = m_nReadPos-nStartOfString;
  154.                 m_lpszTempString = new TCHAR[len+1];
  155.                 if( m_lpszTempString )
  156.                 {
  157.                     tstrncpy( m_lpszTempString, m_lpszCopyOfString+nStartOfString, len );
  158.                     m_lpszTempString[len] = 0;
  159.                     return m_lpszTempString;
  160.                 }
  161.             }
  162.             if( !c )
  163.             {
  164.                 return NULL;
  165.             }
  166.         }
  167.         m_nReadPos++;
  168.     }
  169. }
  170.  
  171.  
  172. CWholeWords::~CWholeWords()
  173. {
  174.     free( m_lpszCopyOfString );
  175.     delete m_lpszTempString;
  176. }
  177.  
  178. BOOL FindCaseSensitiveSubstringWholeWords(LPCTSTR lpszString, LPCTSTR lpszFind)
  179. {
  180.     CWholeWords words(lpszString);
  181.     while( true )
  182.     {
  183.         LPCTSTR word = words.GetNextWord();
  184.         if( word )
  185.         {
  186.             if( lstrcmp(word, lpszFind) == 0 )
  187.             {
  188.                 return TRUE;
  189.             }
  190.         }
  191.         else
  192.         {
  193.             return FALSE;
  194.         }
  195.     }
  196. }
  197.  
  198.  
  199. BOOL FindSubstringIgnoreCaseWholeWords(LPCTSTR lpszString, LPCTSTR lpszFind)
  200. {
  201.     CWholeWords words(lpszString);
  202.     while( true )
  203.     {
  204.         LPCTSTR word = words.GetNextWord();
  205.         if( word )
  206.         {
  207.             if( lstrcmpi(word, lpszFind) == 0 )
  208.             {
  209.                 return TRUE;
  210.             }
  211.         }
  212.         else
  213.         {
  214.             return FALSE;
  215.         }
  216.     }
  217. }
  218.  
  219. BOOL CSearchContext::ContinueSearchLoop( CObArray* pServices, int nIndex, CUIntArray& results)
  220.     CListViewEntry* pService = (CListViewEntry*) pServices->GetAt(nIndex);
  221.  
  222.     if( m_pEntries->Matches(pService,m_lpfnFindSubstring, m_strFind) )
  223.     {
  224.         results.Add(nIndex);
  225.         m_nLastFoundIndex = nIndex;
  226.         return m_bHighlightAllResults;
  227.     }
  228.     return TRUE;
  229. }
  230.  
  231.  
  232. int CSearchContext::FindNextIndicesDown(CObArray* pServices, CUIntArray& results)
  233. {
  234.     int nIndex = m_nLastFoundIndex+1;
  235.     int nMaxIndex = pServices->GetSize();
  236.  
  237.     // loop through elements in lower half
  238.     while( nIndex < nMaxIndex )
  239.     {
  240.         if( !ContinueSearchLoop(pServices, nIndex++, results) )
  241.         {
  242.             return results.GetSize();
  243.         }
  244.     }
  245.  
  246.     // not found, search from the top
  247.     nIndex = 0;
  248.     while( nIndex <= m_nLastFoundIndex )
  249.     {
  250.         if( !ContinueSearchLoop(pServices, nIndex++, results) )
  251.         {
  252.             return results.GetSize();
  253.         }
  254.     }
  255.     
  256.     return results.GetSize();
  257. }
  258.  
  259. BOOL CSearchContext::IsValid()
  260. {
  261.     return !IsEmptyString(m_strFind);
  262. }
  263.  
  264. void CSearchContext::Refresh(int nIndex)
  265. {
  266.     m_nLastFoundIndex = nIndex;
  267.     m_bCaseSensitive = theConfiguration.m_bSearchCaseSensitive;
  268.     m_bWholeWordsOnly = theConfiguration.m_bSearchWholeWordsOnly;
  269.     m_bHighlightAllResults = theConfiguration.m_bSearchHighlightAllResults;
  270.     m_bSearchUp = theConfiguration.m_bSearchUp;
  271.     m_strFind = theConfiguration.m_FindStrings.GetAt(0);
  272.     if( m_bWholeWordsOnly )
  273.     {
  274.         if( m_bCaseSensitive )
  275.         {
  276.             m_lpfnFindSubstring = FindCaseSensitiveSubstringWholeWords;
  277.         }
  278.         else
  279.         {
  280.             m_lpfnFindSubstring = FindSubstringIgnoreCaseWholeWords;
  281.         }
  282.     }
  283.     else
  284.     {
  285.         if( m_bCaseSensitive )
  286.         {
  287.             m_lpfnFindSubstring = FindCaseSensitiveSubstring;
  288.         }
  289.         else
  290.         {
  291.             m_lpfnFindSubstring = FindSubstringIgnoreCase;
  292.         }
  293.     }
  294. }
  295.  
  296. /////////////////////////////////////////////////////////////////////////////
  297. // CFindDialog dialog
  298.  
  299. CFindDialog::CFindDialog(CWnd* pParent /*=NULL*/)
  300.     : CDialog(CFindDialog::IDD, pParent)
  301. {
  302.     //{{AFX_DATA_INIT(CFindDialog)
  303.     //}}AFX_DATA_INIT
  304. }
  305.  
  306.  
  307.  
  308.  
  309. void CFindDialog::DoDataExchange(CDataExchange* pDX)
  310. {
  311.     CDialog::DoDataExchange(pDX);
  312.     //{{AFX_DATA_MAP(CFindDialog)
  313.     DDX_Control(pDX, IDC_FIND_STRING, m_ecFindString);
  314.     DDX_Control(pDX, IDC_SEARCH_UP, m_btSearchUp);
  315.     DDX_Control(pDX, IDC_SEARCH_DOWN, m_btSearchDown);
  316.     //}}AFX_DATA_MAP
  317.     DDX_Check(pDX, IDC_CASE_SENSITIVE, theConfiguration.m_bSearchCaseSensitive);
  318.     DDX_Check(pDX, IDC_WHOLE_WORDS_ONLY, theConfiguration.m_bSearchWholeWordsOnly);
  319.     DDX_Check(pDX, IDC_SELECT_ALL_RESULTS, theConfiguration.m_bSearchHighlightAllResults);
  320.  
  321.     if( pDX->m_bSaveAndValidate )
  322.     {
  323.         theConfiguration.m_bSearchUp = (m_btSearchUp.GetCheck() == BST_CHECKED);
  324.     }
  325.     else
  326.     {
  327.         if( theConfiguration.m_bSearchUp )
  328.             m_btSearchUp.SetCheck(BST_CHECKED);
  329.         else
  330.             m_btSearchDown.SetCheck(BST_CHECKED);
  331.     }
  332.     DDX_ExchangeComboArray(pDX, m_ecFindString, theConfiguration.m_FindStrings );
  333.  
  334. }
  335.  
  336.  
  337. BEGIN_MESSAGE_MAP(CFindDialog, CDialog)
  338.     //{{AFX_MSG_MAP(CFindDialog)
  339.     //}}AFX_MSG_MAP
  340. END_MESSAGE_MAP()
  341.  
  342.