home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / porttool / ptfind.c < prev    next >
C/C++ Source or Header  |  1995-10-28  |  5KB  |  200 lines

  1. #include "PortTool.h"
  2.  
  3. /* global search string */
  4. char    lpszSearch[MAXSEARCHSTRING+1] = "";
  5. HWND    hDlgSearch;
  6.  
  7. FINDREPLACE    frSearch;
  8.  
  9.  
  10. /* compare two substrings */
  11. BOOL WINAPI RealSlowCompare (WORD, char *, char *);
  12. BOOL WINAPI RealSlowCompare (
  13.     WORD    wCase,
  14.     char    *lpszSubject,
  15.     char    *lpszTarget)
  16. {
  17.     if (wCase)
  18.     {
  19.     while (*lpszTarget)
  20.         if (*lpszTarget++ != *lpszSubject++)
  21.         return FALSE;
  22.     }
  23.     else
  24.     {
  25.     while (*lpszTarget) {
  26.         if (IsDBCSLeadByte(*lpszSubject) ||
  27.         IsDBCSLeadByte(*lpszTarget)) {
  28.         if (*lpszTarget++ != *lpszSubject++ ||
  29.             *lpszTarget++ != *lpszSubject++) {
  30.             return FALSE;
  31.         }
  32.         } else {
  33.         if ((CHAR)(DWORD)CharLower ((char *)(DWORD)(BYTE)*lpszTarget++)
  34.          != (CHAR)(DWORD)CharLower ((char *)(DWORD)(BYTE)*lpszSubject++)) {
  35.             return FALSE;
  36.         }
  37.         }
  38.     }
  39.     }
  40.     return TRUE;
  41. }
  42.  
  43.  
  44.  
  45.  
  46. /* invoke the common search/replace dialog */
  47. BOOL WINAPI FindDialog (
  48.     HWND    hWnd,
  49.     WORD    wCase,
  50.     WORD    wDir,
  51.     char    *lpszInit)
  52. {
  53.     frSearch.lStructSize      = sizeof (FINDREPLACE);
  54.     frSearch.hwndOwner        = hWnd;
  55.     frSearch.hInstance        = (HANDLE)GetWindowLong (hWnd, GWL_HINSTANCE);
  56.     frSearch.Flags            = FR_HIDEWHOLEWORD;
  57.  
  58.     /* if wCase, case sensitive */
  59.     if (wCase)
  60.     frSearch.Flags        |= FR_MATCHCASE;
  61.     /* if wDir, search forward */
  62.     if (wDir)
  63.     frSearch.Flags        |= FR_DOWN;
  64.  
  65.     frSearch.lpstrFindWhat    = lpszInit;
  66.     frSearch.lpstrReplaceWith = NULL;
  67.     frSearch.wFindWhatLen     = MAXSEARCHSTRING+1;
  68.     frSearch.wReplaceWithLen  = 0;
  69.     frSearch.lCustData        = 0;
  70.     frSearch.lpfnHook         = NULL;
  71.     frSearch.lpTemplateName   = NULL;
  72.  
  73.     /* call common search dialog */
  74.     if (hDlgSearch = FindText (&frSearch))
  75.     return TRUE;
  76.     else
  77.     return FALSE;
  78. }
  79.  
  80.  
  81.  
  82. /* perform the actual text searching in the edit control data */
  83. BOOL WINAPI LocateText (
  84.     HWND        hWnd,
  85.     WORD        wCase,
  86.     WORD        wDir,
  87.     char        *lpszStr)
  88. {
  89.     UINT    uBegSel, uEndSel, uOrgBegSel, uOrgEndSel;
  90.     HANDLE  hEditData;
  91.     HWND    hWndEdit = (HANDLE)GetWindowLong (hWnd, WL_HWNDEDIT);
  92.     char    *lpEditData;
  93.     char    *lpEditHead;
  94.     UINT    uLen;
  95.     int     nStrLen = strlen (lpszStr);
  96.     int     nChars;
  97.  
  98.     /* test for valid string */
  99.     if (!*lpszStr)
  100.     return FALSE;
  101.  
  102.     /* locate beginning of selected text */
  103.     SendMessage (hWndEdit, EM_GETSEL, (UINT)&uBegSel, (UINT)&uEndSel);
  104.     uOrgBegSel = uBegSel;
  105.     uOrgEndSel = uEndSel;
  106.  
  107.     /* get length of the text */
  108.     uLen = (UINT)SendMessage (hWndEdit, WM_GETTEXTLENGTH, 0, 0);
  109.  
  110.     /* Get handle to edit text data and lock it */
  111.  
  112. #if !defined (WIN32)
  113.  
  114.     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
  115.     lpEditData = LocalLock (hEditData);
  116.     lpEditHead = lpEditData;
  117.  
  118. #else
  119.  
  120.     hEditData = LocalAlloc (LHND, uLen);
  121.     lpEditData = LocalLock (hEditData);
  122.     lpEditHead = lpEditData;
  123.     GetWindowText (hWndEdit, lpEditData, uLen);
  124.  
  125. #endif
  126.  
  127.     /* advance starting point past selection one char */
  128.     if (wDir) {
  129.         if (IsDBCSLeadByte(lpEditData[uBegSel])) {
  130.             uBegSel += 2;
  131.         } else {
  132.             uBegSel += 1;
  133.         }
  134.     } else {
  135.         if (IsDBCSLeadByte(*CharPrev(lpEditData, lpEditData + uBegSel))) {
  136.             uBegSel -= 2;
  137.         } else {
  138.             uBegSel -= 1;
  139.         }
  140.     }
  141.     lpEditData += uBegSel;
  142.  
  143.     /* count characters to search (either forward to end of file or back to beginning) */
  144.     if (wDir)
  145.     nChars = (int)(uLen - uBegSel + 1 - nStrLen);
  146.     else
  147.     nChars = (int)uBegSel;
  148.  
  149.     /* compare character by character for a substring match */
  150.     //DBCS_FIX
  151.     while ((wDir && nChars >= nStrLen) || (!wDir && nChars >= 0))
  152.     {
  153.     /* compare this substring for a match */
  154.     if (RealSlowCompare (wCase, lpEditData, lpszStr))
  155.         {
  156.         /* string found, cleanup and go away */
  157.         LocalUnlock(hEditData);
  158.  
  159.         /* scroll parent edit control and select offending text */
  160.         SendMessage (hWndEdit, EM_LINESCROLL, 0,
  161.         SendMessage (hWndEdit, EM_LINEFROMCHAR, uBegSel, 0) -
  162.         SendMessage (hWndEdit, EM_GETFIRSTVISIBLELINE, 0, 0));
  163.  
  164.         /* Select the located string */
  165.         uEndSel = uBegSel + nStrLen;
  166.         SendMessage(hWndEdit, EM_SETSEL, uBegSel, uEndSel);
  167.  
  168.         /* return success */
  169.         return TRUE;
  170.         }
  171.  
  172.     if (wDir) {
  173.         if (IsDBCSLeadByte(*lpEditData)) {
  174.         nChars -= 2;
  175.         lpEditData += 2;
  176.         uBegSel += 2;
  177.         } else {
  178.         nChars--;
  179.         lpEditData++;
  180.         uBegSel++;
  181.         }
  182.     } else {
  183.         lpEditData = CharPrev(lpEditHead, lpEditData);
  184.         if (IsDBCSLeadByte(*lpEditData)) {
  185.         nChars -= 2;
  186.         uBegSel -= 2;
  187.         } else {
  188.         nChars--;
  189.         uBegSel--;
  190.         }
  191.     }
  192.     }
  193.  
  194.     LocalUnlock (hEditData);
  195.     SendMessage (hWndEdit, EM_SETSEL, uOrgBegSel, uOrgEndSel);
  196.  
  197.     /* return failed search  */
  198.     return FALSE;
  199. }
  200.