Annotation of mstools/samples/porttool/ptfind.c, revision 1.1.1.1

1.1       root        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:        // convert to lower case before comparing
                     26:        CharLower ((char *)lpszTarget);
                     27:        while (*lpszTarget)
                     28:            if (*lpszTarget++ != (CHAR)(DWORD)CharLower ((char *)(DWORD)(BYTE)*lpszSubject++))
                     29:                return FALSE;
                     30:        }
                     31:     return TRUE;
                     32: }
                     33: 
                     34: 
                     35: 
                     36: 
                     37: // invoke the common search/replace dialog
                     38: BOOL WINAPI FindDialog (
                     39:     HWND    hWnd,
                     40:     WORD    wCase,
                     41:     WORD    wDir,
                     42:     char    *lpszInit)
                     43: {
                     44:     frSearch.lStructSize      = sizeof (FINDREPLACE);
                     45:     frSearch.hwndOwner       = hWnd;
                     46:     frSearch.hInstance       = (HANDLE)GetWindowLong (hWnd, GWL_HINSTANCE);
                     47:     frSearch.Flags           = 0;
                     48: 
                     49:     // if wCase, case sensitive
                     50:     if (wCase)
                     51:        frSearch.Flags        |= FR_MATCHCASE;
                     52:     // if wDir, search forward
                     53:     if (wDir)
                     54:        frSearch.Flags        |= FR_DOWN;
                     55: 
                     56:     frSearch.lpstrFindWhat    = lpszInit;
                     57:     frSearch.lpstrReplaceWith = NULL;
                     58:     frSearch.wFindWhatLen     = MAXSEARCHSTRING+1;
                     59:     frSearch.wReplaceWithLen  = 0;
                     60:     frSearch.lCustData       = NULL;
                     61:     frSearch.lpfnHook        = NULL;
                     62:     frSearch.lpTemplateName   = NULL;
                     63: 
                     64:     // call common search dialog
                     65:     if (hDlgSearch = FindText (&frSearch))
                     66:        return TRUE;
                     67:     else
                     68:        return FALSE;
                     69: }
                     70: 
                     71: 
                     72: 
                     73: // perform the actual text searching in the edit control data
                     74: BOOL WINAPI LocateText (
                     75:     HWND       hWnd,
                     76:     WORD       wCase,
                     77:     WORD       wDir,
                     78:     char       *lpszStr)
                     79: {
                     80:     UINT    uBegSel, uEndSel, uOrgBegSel, uOrgEndSel;
                     81:     HANDLE  hEditData;
                     82:     HWND    hWndEdit = (HANDLE)GetWindowLong (hWnd, WL_HWNDEDIT);
                     83:     char    *lpEditData;
                     84:     UINT    uLen;
                     85:     int     nStrLen = strlen (lpszStr);
                     86:     int     nChars;
                     87: 
                     88:     // test for valid string
                     89:     if (!*lpszStr)
                     90:        return FALSE;
                     91: 
                     92:     // locate beginning of selected text
                     93:     SendMessage (hWndEdit, EM_GETSEL, (UINT)&uBegSel, (UINT)&uEndSel);
                     94:     uOrgBegSel = uBegSel;
                     95:     uOrgEndSel = uEndSel;
                     96: 
                     97:     // get length of the text
                     98:     uLen = (UINT)SendMessage (hWndEdit, WM_GETTEXTLENGTH, 0, 0);
                     99: 
                    100:     // Get handle to edit text data and lock it
                    101:     hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
                    102:     lpEditData = LocalLock (hEditData);
                    103: 
                    104:     // advance starting point past selection one char
                    105:     lpEditData += (wDir ? ++uBegSel : --uBegSel);
                    106: 
                    107:     // count characters to search (either forward to end of file or back to beginning)
                    108:     if (wDir)
                    109:        nChars = (int)(uLen - uBegSel + 1 - nStrLen);
                    110:     else
                    111:        nChars = (int)uBegSel;
                    112: 
                    113:     // compare character by character for a substring match
                    114:     while (nChars >= nStrLen)
                    115:        {
                    116:        // compare this substring for a match
                    117:        if (RealSlowCompare (wCase, lpEditData, lpszStr))
                    118:            {
                    119:            // string found, cleanup and go away
                    120:            LocalUnlock(hEditData);
                    121: 
                    122:            // scroll parent edit control and select offending text
                    123:            SendMessage (hWndEdit, EM_LINESCROLL, 0,
                    124:                SendMessage (hWndEdit, EM_LINEFROMCHAR, uBegSel, 0) -
                    125:                SendMessage (hWndEdit, EM_GETFIRSTVISIBLELINE, 0, 0));
                    126: 
                    127:            // Select the located string
                    128:            uEndSel = uBegSel + nStrLen;
                    129:            SendMessage(hWndEdit, EM_SETSEL, uBegSel, uEndSel);
                    130: 
                    131:            // return success
                    132:            return TRUE;
                    133:            }
                    134: 
                    135:        nChars--;
                    136: 
                    137:        // increment/decrement start position by 1
                    138:        lpEditData += (wDir ? 1 : -1);
                    139:        wDir ? uBegSel++ : uBegSel--;
                    140:        }
                    141: 
                    142:     LocalUnlock (hEditData);
                    143:     SendMessage (hWndEdit, EM_SETSEL, uOrgBegSel, uOrgEndSel);
                    144: 
                    145:     // return failed search
                    146:     return FALSE;
                    147: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.